Editor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Editor.h"
  27. #include "EditorWrapper.h"
  28. #include <AK/ByteBuffer.h>
  29. #include <AK/FileSystemPath.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/CppLexer.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/SyntaxHighlighter.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibMarkdown/MDDocument.h>
  39. #include <LibWeb/DOM/ElementFactory.h>
  40. #include <LibWeb/DOM/HTMLHeadElement.h>
  41. #include <LibWeb/DOM/Text.h>
  42. #include <LibWeb/HtmlView.h>
  43. #include <LibWeb/Parser/HTMLParser.h>
  44. // #define EDITOR_DEBUG
  45. Editor::Editor()
  46. {
  47. m_documentation_tooltip_window = GUI::Window::construct();
  48. m_documentation_tooltip_window->set_rect(0, 0, 500, 400);
  49. m_documentation_tooltip_window->set_window_type(GUI::WindowType::Tooltip);
  50. m_documentation_html_view = m_documentation_tooltip_window->set_main_widget<Web::HtmlView>();
  51. }
  52. Editor::~Editor()
  53. {
  54. }
  55. EditorWrapper& Editor::wrapper()
  56. {
  57. return static_cast<EditorWrapper&>(*parent());
  58. }
  59. const EditorWrapper& Editor::wrapper() const
  60. {
  61. return static_cast<const EditorWrapper&>(*parent());
  62. }
  63. void Editor::focusin_event(Core::Event& event)
  64. {
  65. wrapper().set_editor_has_focus({}, true);
  66. if (on_focus)
  67. on_focus();
  68. GUI::TextEditor::focusin_event(event);
  69. }
  70. void Editor::focusout_event(Core::Event& event)
  71. {
  72. wrapper().set_editor_has_focus({}, false);
  73. GUI::TextEditor::focusout_event(event);
  74. }
  75. void Editor::paint_event(GUI::PaintEvent& event)
  76. {
  77. GUI::TextEditor::paint_event(event);
  78. if (is_focused()) {
  79. GUI::Painter painter(*this);
  80. painter.add_clip_rect(event.rect());
  81. auto rect = frame_inner_rect();
  82. if (vertical_scrollbar().is_visible())
  83. rect.set_width(rect.width() - vertical_scrollbar().width());
  84. if (horizontal_scrollbar().is_visible())
  85. rect.set_height(rect.height() - horizontal_scrollbar().height());
  86. painter.draw_rect(rect, palette().selection());
  87. window()->set_override_cursor(m_hovering_link && m_holding_ctrl ? GUI::StandardCursor::Hand : GUI::StandardCursor::IBeam);
  88. }
  89. }
  90. static HashMap<String, String>& man_paths()
  91. {
  92. static HashMap<String, String> paths;
  93. if (paths.is_empty()) {
  94. // FIXME: This should also search man3, possibly other places..
  95. Core::DirIterator it("/usr/share/man/man2", Core::DirIterator::Flags::SkipDots);
  96. while (it.has_next()) {
  97. auto path = String::format("/usr/share/man/man2/%s", it.next_path().characters());
  98. auto title = FileSystemPath(path).title();
  99. paths.set(title, path);
  100. }
  101. }
  102. return paths;
  103. }
  104. void Editor::show_documentation_tooltip_if_available(const String& hovered_token, const Gfx::Point& screen_location)
  105. {
  106. auto it = man_paths().find(hovered_token);
  107. if (it == man_paths().end()) {
  108. #ifdef EDITOR_DEBUG
  109. dbg() << "no man path for " << hovered_token;
  110. #endif
  111. m_documentation_tooltip_window->hide();
  112. return;
  113. }
  114. if (m_documentation_tooltip_window->is_visible() && hovered_token == m_last_parsed_token) {
  115. return;
  116. }
  117. #ifdef EDITOR_DEBUG
  118. dbg() << "opening " << it->value;
  119. #endif
  120. auto file = Core::File::construct(it->value);
  121. if (!file->open(Core::File::ReadOnly)) {
  122. dbg() << "failed to open " << it->value << " " << file->error_string();
  123. return;
  124. }
  125. MDDocument man_document;
  126. bool success = man_document.parse(file->read_all());
  127. if (!success) {
  128. dbg() << "failed to parse markdown";
  129. return;
  130. }
  131. auto html_text = man_document.render_to_html();
  132. auto html_document = Web::parse_html_document(html_text);
  133. if (!html_document) {
  134. dbg() << "failed to parse HTML";
  135. return;
  136. }
  137. // FIXME: LibWeb needs a friendlier DOM manipulation API. Something like innerHTML :^)
  138. auto style_element = create_element(*html_document, "style");
  139. style_element->append_child(adopt(*new Web::Text(*html_document, "body { background-color: #dac7b5; }")));
  140. // FIXME: This const_cast should not be necessary.
  141. auto* head_element = const_cast<Web::HTMLHeadElement*>(html_document->head());
  142. ASSERT(head_element);
  143. head_element->append_child(style_element);
  144. m_documentation_html_view->set_document(html_document);
  145. m_documentation_tooltip_window->move_to(screen_location.translated(4, 4));
  146. m_documentation_tooltip_window->show();
  147. m_last_parsed_token = hovered_token;
  148. }
  149. void Editor::mousemove_event(GUI::MouseEvent& event)
  150. {
  151. GUI::TextEditor::mousemove_event(event);
  152. if (document().spans().is_empty())
  153. return;
  154. auto text_position = text_position_at(event.position());
  155. if (!text_position.is_valid()) {
  156. m_documentation_tooltip_window->hide();
  157. return;
  158. }
  159. bool hide_tooltip = true;
  160. bool is_over_header = false;
  161. for (auto& span : document().spans()) {
  162. if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) {
  163. auto token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(span.data));
  164. if (token == GUI::CppToken::Type::IncludePath && span.is_underlined) {
  165. span.is_underlined = false;
  166. wrapper().editor().update();
  167. }
  168. }
  169. if (span.range.contains(text_position)) {
  170. auto adjusted_range = span.range;
  171. adjusted_range.end().set_column(adjusted_range.end().column() + 1);
  172. auto hovered_span_text = document().text_in_range(adjusted_range);
  173. #ifdef EDITOR_DEBUG
  174. dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
  175. #endif
  176. auto token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(span.data));
  177. if (token == GUI::CppToken::Type::IncludePath) {
  178. is_over_header = true;
  179. bool was_underlined = span.is_underlined;
  180. span.is_underlined = event.modifiers() & Mod_Ctrl;
  181. if (span.is_underlined != was_underlined) {
  182. wrapper().editor().update();
  183. }
  184. } else if (token == GUI::CppToken::Type::Identifier) {
  185. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  186. hide_tooltip = false;
  187. }
  188. }
  189. }
  190. m_previous_text_position = text_position;
  191. if (hide_tooltip)
  192. m_documentation_tooltip_window->hide();
  193. m_hovering_link = is_over_header && (event.modifiers() & Mod_Ctrl);
  194. }
  195. void Editor::mousedown_event(GUI::MouseEvent& event)
  196. {
  197. auto highlighter = wrapper().editor().syntax_highlighter();
  198. if (!highlighter || highlighter->language() != GUI::SyntaxLanguage::Cpp) {
  199. GUI::TextEditor::mousedown_event(event);
  200. return;
  201. }
  202. if (!(event.modifiers() & Mod_Ctrl)) {
  203. GUI::TextEditor::mousedown_event(event);
  204. return;
  205. }
  206. auto text_position = text_position_at(event.position());
  207. if (!text_position.is_valid()) {
  208. GUI::TextEditor::mousedown_event(event);
  209. return;
  210. }
  211. for (auto& span : document().spans()) {
  212. if (span.range.contains(text_position)) {
  213. auto token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(span.data));
  214. if (token != GUI::CppToken::Type::IncludePath) {
  215. GUI::TextEditor::mousedown_event(event);
  216. return;
  217. }
  218. auto adjusted_range = span.range;
  219. adjusted_range.end().set_column(adjusted_range.end().column() + 1);
  220. auto span_text = document().text_in_range(adjusted_range);
  221. auto header_path = span_text.substring(1, span_text.length() - 2);
  222. #ifdef EDITOR_DEBUG
  223. dbg() << "Ctrl+click: " << adjusted_range << " \"" << header_path << "\"";
  224. #endif
  225. navigate_to_include_if_available(header_path);
  226. return;
  227. }
  228. }
  229. GUI::TextEditor::mousedown_event(event);
  230. }
  231. void Editor::keydown_event(GUI::KeyEvent& event)
  232. {
  233. if (event.key() == Key_Control)
  234. m_holding_ctrl = true;
  235. GUI::TextEditor::keydown_event(event);
  236. }
  237. void Editor::keyup_event(GUI::KeyEvent& event)
  238. {
  239. if (event.key() == Key_Control)
  240. m_holding_ctrl = false;
  241. GUI::TextEditor::keyup_event(event);
  242. }
  243. static HashMap<String, String>& include_paths()
  244. {
  245. static HashMap<String, String> paths;
  246. auto add_directory = [](String base, Optional<String> recursive, auto handle_directory) -> void {
  247. Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
  248. while (it.has_next()) {
  249. auto path = it.next_full_path();
  250. if (!Core::File::is_directory(path)) {
  251. auto key = path.substring(base.length() + 1, path.length() - base.length() - 1);
  252. #ifdef EDITOR_DEBUG
  253. dbg() << "Adding header \"" << key << "\" in path \"" << path << "\"";
  254. #endif
  255. paths.set(key, path);
  256. } else {
  257. handle_directory(base, path, handle_directory);
  258. }
  259. }
  260. };
  261. if (paths.is_empty()) {
  262. add_directory(".", {}, add_directory);
  263. add_directory("/usr/local/include", {}, add_directory);
  264. add_directory("/usr/local/include/c++/9.2.0", {}, add_directory);
  265. add_directory("/usr/include", {}, add_directory);
  266. }
  267. return paths;
  268. }
  269. void Editor::navigate_to_include_if_available(String path)
  270. {
  271. auto it = include_paths().find(path);
  272. if (it == include_paths().end()) {
  273. #ifdef EDITOR_DEBUG
  274. dbg() << "no header " << path << " found.";
  275. #endif
  276. return;
  277. }
  278. on_open(it->value);
  279. }