Editor.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/LexicalPath.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/Label.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/SyntaxHighlighter.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibMarkdown/Document.h>
  39. #include <LibWeb/DOM/ElementFactory.h>
  40. #include <LibWeb/DOM/Text.h>
  41. #include <LibWeb/HTML/HTMLHeadElement.h>
  42. #include <LibWeb/InProcessWebView.h>
  43. // #define EDITOR_DEBUG
  44. namespace HackStudio {
  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_page_view = m_documentation_tooltip_window->set_main_widget<Web::InProcessWebView>();
  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(GUI::FocusEvent& 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(GUI::FocusEvent& event)
  71. {
  72. wrapper().set_editor_has_focus({}, false);
  73. GUI::TextEditor::focusout_event(event);
  74. }
  75. Gfx::IntRect Editor::breakpoint_icon_rect(size_t line_number) const
  76. {
  77. auto ruler_line_rect = ruler_content_rect(line_number);
  78. auto scroll_value = vertical_scrollbar().value();
  79. ruler_line_rect = ruler_line_rect.translated({ 0, -scroll_value });
  80. auto center = ruler_line_rect.center().translated({ ruler_line_rect.width() - 10, -line_spacing() - 3 });
  81. constexpr int size = 32;
  82. return { center.x() - size / 2, center.y() - size / 2, size, size };
  83. }
  84. void Editor::paint_event(GUI::PaintEvent& event)
  85. {
  86. GUI::TextEditor::paint_event(event);
  87. GUI::Painter painter(*this);
  88. if (is_focused()) {
  89. painter.add_clip_rect(event.rect());
  90. auto rect = frame_inner_rect();
  91. if (vertical_scrollbar().is_visible())
  92. rect.set_width(rect.width() - vertical_scrollbar().width());
  93. if (horizontal_scrollbar().is_visible())
  94. rect.set_height(rect.height() - horizontal_scrollbar().height());
  95. painter.draw_rect(rect, palette().selection());
  96. }
  97. if (ruler_visible()) {
  98. size_t first_visible_line = text_position_at(event.rect().top_left()).line();
  99. size_t last_visible_line = text_position_at(event.rect().bottom_right()).line();
  100. for (size_t line : breakpoint_lines()) {
  101. if (line < first_visible_line || line > last_visible_line) {
  102. continue;
  103. }
  104. const auto& icon = breakpoint_icon_bitmap();
  105. painter.blit(breakpoint_icon_rect(line).center(), icon, icon.rect());
  106. }
  107. if (execution_position().has_value()) {
  108. const auto& icon = current_position_icon_bitmap();
  109. painter.blit(breakpoint_icon_rect(execution_position().value()).center(), icon, icon.rect());
  110. }
  111. }
  112. }
  113. static HashMap<String, String>& man_paths()
  114. {
  115. static HashMap<String, String> paths;
  116. if (paths.is_empty()) {
  117. // FIXME: This should also search man3, possibly other places..
  118. Core::DirIterator it("/usr/share/man/man2", Core::DirIterator::Flags::SkipDots);
  119. while (it.has_next()) {
  120. auto path = String::format("/usr/share/man/man2/%s", it.next_path().characters());
  121. auto title = LexicalPath(path).title();
  122. paths.set(title, path);
  123. }
  124. }
  125. return paths;
  126. }
  127. void Editor::show_documentation_tooltip_if_available(const String& hovered_token, const Gfx::IntPoint& screen_location)
  128. {
  129. auto it = man_paths().find(hovered_token);
  130. if (it == man_paths().end()) {
  131. #ifdef EDITOR_DEBUG
  132. dbg() << "no man path for " << hovered_token;
  133. #endif
  134. m_documentation_tooltip_window->hide();
  135. return;
  136. }
  137. if (m_documentation_tooltip_window->is_visible() && hovered_token == m_last_parsed_token) {
  138. return;
  139. }
  140. #ifdef EDITOR_DEBUG
  141. dbg() << "opening " << it->value;
  142. #endif
  143. auto file = Core::File::construct(it->value);
  144. if (!file->open(Core::File::ReadOnly)) {
  145. dbg() << "failed to open " << it->value << " " << file->error_string();
  146. return;
  147. }
  148. auto man_document = Markdown::Document::parse(file->read_all());
  149. if (!man_document) {
  150. dbg() << "failed to parse markdown";
  151. return;
  152. }
  153. auto html_text = man_document->render_to_html();
  154. m_documentation_page_view->load_html(html_text, {});
  155. if (auto* document = m_documentation_page_view->document()) {
  156. const_cast<Web::HTML::HTMLElement*>(document->body())->set_attribute(Web::HTML::AttributeNames::style, "background-color: #dac7b5;");
  157. }
  158. m_documentation_tooltip_window->move_to(screen_location.translated(4, 4));
  159. m_documentation_tooltip_window->show();
  160. m_last_parsed_token = hovered_token;
  161. }
  162. void Editor::mousemove_event(GUI::MouseEvent& event)
  163. {
  164. GUI::TextEditor::mousemove_event(event);
  165. if (document().spans().is_empty())
  166. return;
  167. auto text_position = text_position_at(event.position());
  168. if (!text_position.is_valid()) {
  169. m_documentation_tooltip_window->hide();
  170. return;
  171. }
  172. auto highlighter = wrapper().editor().syntax_highlighter();
  173. if (!highlighter)
  174. return;
  175. bool hide_tooltip = true;
  176. bool is_over_link = false;
  177. auto ruler_line_rect = ruler_content_rect(text_position.line());
  178. auto hovering_lines_ruler = (event.position().x() < ruler_line_rect.width());
  179. if (hovering_lines_ruler && !is_in_drag_select())
  180. set_override_cursor(Gfx::StandardCursor::Arrow);
  181. else if (m_hovering_editor)
  182. set_override_cursor(m_hovering_link && m_holding_ctrl ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::IBeam);
  183. for (auto& span : document().spans()) {
  184. if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) {
  185. if (highlighter->is_navigatable(span.data) && span.is_underlined) {
  186. span.is_underlined = false;
  187. wrapper().editor().update();
  188. }
  189. }
  190. if (span.range.contains(text_position)) {
  191. auto adjusted_range = span.range;
  192. auto end_line_length = document().line(span.range.end().line()).length();
  193. adjusted_range.end().set_column(min(end_line_length, adjusted_range.end().column() + 1));
  194. auto hovered_span_text = document().text_in_range(adjusted_range);
  195. #ifdef EDITOR_DEBUG
  196. dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
  197. #endif
  198. if (highlighter->is_navigatable(span.data)) {
  199. is_over_link = true;
  200. bool was_underlined = span.is_underlined;
  201. span.is_underlined = event.modifiers() & Mod_Ctrl;
  202. if (span.is_underlined != was_underlined) {
  203. wrapper().editor().update();
  204. }
  205. }
  206. if (highlighter->is_identifier(span.data)) {
  207. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  208. hide_tooltip = false;
  209. }
  210. }
  211. }
  212. m_previous_text_position = text_position;
  213. if (hide_tooltip)
  214. m_documentation_tooltip_window->hide();
  215. m_hovering_link = is_over_link && (event.modifiers() & Mod_Ctrl);
  216. }
  217. void Editor::mousedown_event(GUI::MouseEvent& event)
  218. {
  219. auto highlighter = wrapper().editor().syntax_highlighter();
  220. if (!highlighter) {
  221. GUI::TextEditor::mousedown_event(event);
  222. return;
  223. }
  224. auto text_position = text_position_at(event.position());
  225. auto ruler_line_rect = ruler_content_rect(text_position.line());
  226. if (event.button() == GUI::MouseButton::Left && event.position().x() < ruler_line_rect.width()) {
  227. if (!breakpoint_lines().contains_slow(text_position.line())) {
  228. breakpoint_lines().append(text_position.line());
  229. on_breakpoint_change(wrapper().filename_label().text(), text_position.line(), BreakpointChange::Added);
  230. } else {
  231. breakpoint_lines().remove_first_matching([&](size_t line) { return line == text_position.line(); });
  232. on_breakpoint_change(wrapper().filename_label().text(), text_position.line(), BreakpointChange::Removed);
  233. }
  234. }
  235. if (!(event.modifiers() & Mod_Ctrl)) {
  236. GUI::TextEditor::mousedown_event(event);
  237. return;
  238. }
  239. if (!text_position.is_valid()) {
  240. GUI::TextEditor::mousedown_event(event);
  241. return;
  242. }
  243. for (auto& span : document().spans()) {
  244. if (span.range.contains(text_position)) {
  245. if (!highlighter->is_navigatable(span.data)) {
  246. GUI::TextEditor::mousedown_event(event);
  247. return;
  248. }
  249. auto adjusted_range = span.range;
  250. adjusted_range.end().set_column(adjusted_range.end().column() + 1);
  251. auto span_text = document().text_in_range(adjusted_range);
  252. auto header_path = span_text.substring(1, span_text.length() - 2);
  253. #ifdef EDITOR_DEBUG
  254. dbg() << "Ctrl+click: " << adjusted_range << " \"" << header_path << "\"";
  255. #endif
  256. navigate_to_include_if_available(header_path);
  257. return;
  258. }
  259. }
  260. GUI::TextEditor::mousedown_event(event);
  261. }
  262. void Editor::keydown_event(GUI::KeyEvent& event)
  263. {
  264. if (event.key() == Key_Control)
  265. m_holding_ctrl = true;
  266. GUI::TextEditor::keydown_event(event);
  267. }
  268. void Editor::keyup_event(GUI::KeyEvent& event)
  269. {
  270. if (event.key() == Key_Control)
  271. m_holding_ctrl = false;
  272. GUI::TextEditor::keyup_event(event);
  273. }
  274. void Editor::enter_event(Core::Event& event)
  275. {
  276. m_hovering_editor = true;
  277. GUI::TextEditor::enter_event(event);
  278. }
  279. void Editor::leave_event(Core::Event& event)
  280. {
  281. m_hovering_editor = false;
  282. GUI::TextEditor::leave_event(event);
  283. }
  284. static HashMap<String, String>& include_paths()
  285. {
  286. static HashMap<String, String> paths;
  287. auto add_directory = [](String base, Optional<String> recursive, auto handle_directory) -> void {
  288. Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
  289. while (it.has_next()) {
  290. auto path = it.next_full_path();
  291. if (!Core::File::is_directory(path)) {
  292. auto key = path.substring(base.length() + 1, path.length() - base.length() - 1);
  293. #ifdef EDITOR_DEBUG
  294. dbg() << "Adding header \"" << key << "\" in path \"" << path << "\"";
  295. #endif
  296. paths.set(key, path);
  297. } else {
  298. handle_directory(base, path, handle_directory);
  299. }
  300. }
  301. };
  302. if (paths.is_empty()) {
  303. add_directory(".", {}, add_directory);
  304. add_directory("/usr/local/include", {}, add_directory);
  305. add_directory("/usr/local/include/c++/9.2.0", {}, add_directory);
  306. add_directory("/usr/include", {}, add_directory);
  307. }
  308. return paths;
  309. }
  310. void Editor::navigate_to_include_if_available(String path)
  311. {
  312. auto it = include_paths().find(path);
  313. if (it == include_paths().end()) {
  314. #ifdef EDITOR_DEBUG
  315. dbg() << "no header " << path << " found.";
  316. #endif
  317. return;
  318. }
  319. on_open(it->value);
  320. }
  321. void Editor::set_execution_position(size_t line_number)
  322. {
  323. code_document().set_execution_position(line_number);
  324. scroll_position_into_view({ line_number, 0 });
  325. update(breakpoint_icon_rect(line_number));
  326. }
  327. void Editor::clear_execution_position()
  328. {
  329. if (!execution_position().has_value()) {
  330. return;
  331. }
  332. size_t previous_position = execution_position().value();
  333. code_document().clear_execution_position();
  334. update(breakpoint_icon_rect(previous_position));
  335. }
  336. const Gfx::Bitmap& Editor::breakpoint_icon_bitmap()
  337. {
  338. static auto bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/breakpoint.png");
  339. return *bitmap;
  340. }
  341. const Gfx::Bitmap& Editor::current_position_icon_bitmap()
  342. {
  343. static auto bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png");
  344. return *bitmap;
  345. }
  346. const CodeDocument& Editor::code_document() const
  347. {
  348. const auto& doc = document();
  349. ASSERT(doc.is_code_document());
  350. return static_cast<const CodeDocument&>(doc);
  351. }
  352. CodeDocument& Editor::code_document()
  353. {
  354. return const_cast<CodeDocument&>(static_cast<const Editor&>(*this).code_document());
  355. }
  356. void Editor::set_document(GUI::TextDocument& doc)
  357. {
  358. ASSERT(doc.is_code_document());
  359. GUI::TextEditor::set_document(doc);
  360. }
  361. }