Editor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "Editor.h"
  2. #include "CppLexer.h"
  3. #include "EditorWrapper.h"
  4. #include <AK/FileSystemPath.h>
  5. #include <LibCore/CDirIterator.h>
  6. #include <LibCore/CFile.h>
  7. #include <LibGUI/GApplication.h>
  8. #include <LibGUI/GPainter.h>
  9. #include <LibGUI/GScrollBar.h>
  10. #include <LibGUI/GWindow.h>
  11. #include <LibHTML/DOM/ElementFactory.h>
  12. #include <LibHTML/DOM/HTMLHeadElement.h>
  13. #include <LibHTML/DOM/Text.h>
  14. #include <LibHTML/HtmlView.h>
  15. #include <LibHTML/Parser/HTMLParser.h>
  16. #include <LibMarkdown/MDDocument.h>
  17. //#define EDITOR_DEBUG
  18. Editor::Editor(GWidget* parent)
  19. : GTextEditor(GTextEditor::MultiLine, parent)
  20. {
  21. m_documentation_tooltip_window = GWindow::construct();
  22. m_documentation_tooltip_window->set_rect(0, 0, 500, 400);
  23. m_documentation_tooltip_window->set_window_type(GWindowType::Tooltip);
  24. m_documentation_html_view = HtmlView::construct(nullptr);
  25. m_documentation_tooltip_window->set_main_widget(m_documentation_html_view);
  26. }
  27. Editor::~Editor()
  28. {
  29. }
  30. EditorWrapper& Editor::wrapper()
  31. {
  32. return static_cast<EditorWrapper&>(*parent());
  33. }
  34. const EditorWrapper& Editor::wrapper() const
  35. {
  36. return static_cast<const EditorWrapper&>(*parent());
  37. }
  38. void Editor::focusin_event(CEvent& event)
  39. {
  40. wrapper().set_editor_has_focus({}, true);
  41. if (on_focus)
  42. on_focus();
  43. GTextEditor::focusin_event(event);
  44. }
  45. void Editor::focusout_event(CEvent& event)
  46. {
  47. wrapper().set_editor_has_focus({}, false);
  48. GTextEditor::focusout_event(event);
  49. }
  50. void Editor::paint_event(GPaintEvent& event)
  51. {
  52. GTextEditor::paint_event(event);
  53. if (is_focused()) {
  54. GPainter painter(*this);
  55. painter.add_clip_rect(event.rect());
  56. auto rect = frame_inner_rect();
  57. if (vertical_scrollbar().is_visible())
  58. rect.set_width(rect.width() - vertical_scrollbar().width());
  59. if (horizontal_scrollbar().is_visible())
  60. rect.set_height(rect.height() - horizontal_scrollbar().height());
  61. painter.draw_rect(rect, Color::from_rgb(0x955233));
  62. }
  63. }
  64. static HashMap<String, String>& man_paths()
  65. {
  66. static HashMap<String, String> paths;
  67. if (paths.is_empty()) {
  68. // FIXME: This should also search man3, possibly other places..
  69. CDirIterator it("/usr/share/man/man2", CDirIterator::Flags::SkipDots);
  70. while (it.has_next()) {
  71. auto path = String::format("/usr/share/man/man2/%s", it.next_path().characters());
  72. auto title = FileSystemPath(path).title();
  73. paths.set(title, path);
  74. }
  75. }
  76. return paths;
  77. }
  78. void Editor::show_documentation_tooltip_if_available(const String& hovered_token, const Point& screen_location)
  79. {
  80. auto it = man_paths().find(hovered_token);
  81. if (it == man_paths().end()) {
  82. #ifdef EDITOR_DEBUG
  83. dbg() << "no man path for " << hovered_token;
  84. #endif
  85. m_documentation_tooltip_window->hide();
  86. return;
  87. }
  88. if (m_documentation_tooltip_window->is_visible() && hovered_token == m_last_parsed_token) {
  89. return;
  90. }
  91. #ifdef EDITOR_DEBUG
  92. dbg() << "opening " << it->value;
  93. #endif
  94. auto file = CFile::construct(it->value);
  95. if (!file->open(CFile::ReadOnly)) {
  96. dbg() << "failed to open " << it->value << " " << file->error_string();
  97. return;
  98. }
  99. MDDocument man_document;
  100. bool success = man_document.parse(file->read_all());
  101. if (!success) {
  102. dbg() << "failed to parse markdown";
  103. return;
  104. }
  105. auto html_text = man_document.render_to_html();
  106. auto html_document = parse_html_document(html_text);
  107. if (!html_document) {
  108. dbg() << "failed to parse HTML";
  109. return;
  110. }
  111. // FIXME: LibHTML needs a friendlier DOM manipulation API. Something like innerHTML :^)
  112. auto style_element = create_element(*html_document, "style");
  113. style_element->append_child(adopt(*new Text(*html_document, "body { background-color: #dac7b5; }")));
  114. // FIXME: This const_cast should not be necessary.
  115. auto* head_element = const_cast<HTMLHeadElement*>(html_document->head());
  116. ASSERT(head_element);
  117. head_element->append_child(style_element);
  118. m_documentation_html_view->set_document(html_document);
  119. m_documentation_tooltip_window->move_to(screen_location.translated(4, 4));
  120. m_documentation_tooltip_window->show();
  121. m_last_parsed_token = hovered_token;
  122. }
  123. void Editor::mousemove_event(GMouseEvent& event)
  124. {
  125. GTextEditor::mousemove_event(event);
  126. if (document().spans().is_empty())
  127. return;
  128. auto text_position = text_position_at(event.position());
  129. if (!text_position.is_valid()) {
  130. GApplication::the().hide_tooltip();
  131. return;
  132. }
  133. for (auto& span : document().spans()) {
  134. if (span.range.contains(text_position)) {
  135. auto adjusted_range = span.range;
  136. adjusted_range.end().set_column(adjusted_range.end().column() + 1);
  137. auto hovered_span_text = document().text_in_range(adjusted_range);
  138. #ifdef EDITOR_DEBUG
  139. dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
  140. #endif
  141. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  142. return;
  143. }
  144. }
  145. GApplication::the().hide_tooltip();
  146. }
  147. void Editor::highlight_matching_curlies_or_parens()
  148. {
  149. enum class Direction {
  150. Forward,
  151. Backward,
  152. };
  153. auto find_span_of_type = [&](int i, CppToken::Type type, CppToken::Type not_type, Direction direction) {
  154. int nesting_level = 0;
  155. bool forward = direction == Direction::Forward;
  156. for (forward ? ++i : --i; forward ? (i < document().spans().size()) : (i >= 0); forward ? ++i : --i) {
  157. auto& span = document().spans().at(i);
  158. auto span_token_type = (CppToken::Type)((uintptr_t)span.data);
  159. if (span_token_type == not_type) {
  160. ++nesting_level;
  161. } else if (span_token_type == type) {
  162. if (nesting_level-- <= 0)
  163. return i;
  164. }
  165. }
  166. return -1;
  167. };
  168. auto make_buddies = [&](int index0, int index1) {
  169. auto& buddy0 = const_cast<GTextDocumentSpan&>(document().spans()[index0]);
  170. auto& buddy1 = const_cast<GTextDocumentSpan&>(document().spans()[index1]);
  171. m_has_brace_buddies = true;
  172. m_brace_buddies[0].index = index0;
  173. m_brace_buddies[1].index = index1;
  174. m_brace_buddies[0].span_backup = buddy0;
  175. m_brace_buddies[1].span_backup = buddy1;
  176. buddy0.background_color = Color::DarkCyan;
  177. buddy1.background_color = Color::DarkCyan;
  178. buddy0.color = Color::White;
  179. buddy1.color = Color::White;
  180. update();
  181. };
  182. for (int i = 0; i < document().spans().size(); ++i) {
  183. auto& span = const_cast<GTextDocumentSpan&>(document().spans().at(i));
  184. auto token_type = (CppToken::Type)((uintptr_t)span.data);
  185. if (token_type == CppToken::Type::LeftCurly && span.range.start() == cursor()) {
  186. auto buddy = find_span_of_type(i, CppToken::Type::RightCurly, CppToken::Type::LeftCurly, Direction::Forward);
  187. if (buddy != -1)
  188. make_buddies(i, buddy);
  189. return;
  190. }
  191. if (token_type == CppToken::Type::LeftParen && span.range.start() == cursor()) {
  192. auto buddy = find_span_of_type(i, CppToken::Type::RightParen, CppToken::Type::LeftParen, Direction::Forward);
  193. if (buddy != -1)
  194. make_buddies(i, buddy);
  195. return;
  196. }
  197. auto right_of_end = span.range.end();
  198. right_of_end.set_column(right_of_end.column() + 1);
  199. if (token_type == CppToken::Type::RightCurly && right_of_end == cursor()) {
  200. auto buddy = find_span_of_type(i, CppToken::Type::LeftCurly, CppToken::Type::RightCurly, Direction::Backward);
  201. if (buddy != -1)
  202. make_buddies(i, buddy);
  203. return;
  204. }
  205. if (token_type == CppToken::Type::RightParen && right_of_end == cursor()) {
  206. auto buddy = find_span_of_type(i, CppToken::Type::LeftParen, CppToken::Type::RightParen, Direction::Backward);
  207. if (buddy != -1)
  208. make_buddies(i, buddy);
  209. return;
  210. }
  211. }
  212. }
  213. void Editor::cursor_did_change()
  214. {
  215. if (m_has_brace_buddies) {
  216. if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < document().spans().size())
  217. document().set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
  218. if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < document().spans().size())
  219. document().set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
  220. m_has_brace_buddies = false;
  221. update();
  222. }
  223. highlight_matching_curlies_or_parens();
  224. }
  225. void Editor::notify_did_rehighlight()
  226. {
  227. m_has_brace_buddies = false;
  228. highlight_matching_curlies_or_parens();
  229. }