Editor.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2018-2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Editor.h"
  8. #include "Debugger/Debugger.h"
  9. #include "EditorWrapper.h"
  10. #include "HackStudio.h"
  11. #include "Language.h"
  12. #include <AK/ByteBuffer.h>
  13. #include <AK/Debug.h>
  14. #include <AK/LexicalPath.h>
  15. #include <LibCore/DirIterator.h>
  16. #include <LibCore/File.h>
  17. #include <LibCore/Timer.h>
  18. #include <LibCpp/SemanticSyntaxHighlighter.h>
  19. #include <LibCpp/SyntaxHighlighter.h>
  20. #include <LibGUI/Action.h>
  21. #include <LibGUI/Application.h>
  22. #include <LibGUI/GML/AutocompleteProvider.h>
  23. #include <LibGUI/GML/SyntaxHighlighter.h>
  24. #include <LibGUI/GitCommitSyntaxHighlighter.h>
  25. #include <LibGUI/INISyntaxHighlighter.h>
  26. #include <LibGUI/Label.h>
  27. #include <LibGUI/MessageBox.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Scrollbar.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibJS/SyntaxHighlighter.h>
  32. #include <LibMarkdown/Document.h>
  33. #include <LibSQL/AST/SyntaxHighlighter.h>
  34. #include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h>
  35. #include <LibWeb/DOM/Text.h>
  36. #include <LibWeb/HTML/HTMLHeadElement.h>
  37. #include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
  38. #include <LibWebView/OutOfProcessWebView.h>
  39. #include <Shell/SyntaxHighlighter.h>
  40. #include <fcntl.h>
  41. namespace HackStudio {
  42. enum class TooltipRole {
  43. Documentation,
  44. ParametersHint,
  45. };
  46. static RefPtr<GUI::Window> s_tooltip_window;
  47. static RefPtr<WebView::OutOfProcessWebView> s_tooltip_page_view;
  48. static Optional<TooltipRole> m_tooltip_role;
  49. ErrorOr<NonnullRefPtr<Editor>> Editor::try_create()
  50. {
  51. NonnullRefPtr<Editor> editor = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Editor()));
  52. TRY(initialize_tooltip_window());
  53. return editor;
  54. }
  55. Editor::Editor()
  56. {
  57. create_tokens_info_timer();
  58. set_document(CodeDocument::create());
  59. m_move_execution_to_line_action = GUI::Action::create("Set execution point to line", [this](auto&) {
  60. VERIFY(is_program_running());
  61. auto success = Debugger::the().set_execution_position(currently_open_file(), cursor().line());
  62. if (success) {
  63. set_execution_position(cursor().line());
  64. } else {
  65. GUI::MessageBox::show(window(), "Failed to set execution position"sv, "Error"sv, GUI::MessageBox::Type::Error);
  66. }
  67. });
  68. set_debug_mode(false);
  69. add_custom_context_menu_action(*m_move_execution_to_line_action);
  70. set_gutter_visible(true);
  71. }
  72. ErrorOr<void> Editor::initialize_tooltip_window()
  73. {
  74. if (s_tooltip_window.is_null()) {
  75. s_tooltip_window = GUI::Window::construct();
  76. s_tooltip_window->set_window_type(GUI::WindowType::Tooltip);
  77. }
  78. if (s_tooltip_page_view.is_null()) {
  79. s_tooltip_page_view = TRY(s_tooltip_window->try_set_main_widget<WebView::OutOfProcessWebView>());
  80. }
  81. return {};
  82. }
  83. EditorWrapper& Editor::wrapper()
  84. {
  85. return static_cast<EditorWrapper&>(*parent());
  86. }
  87. EditorWrapper const& Editor::wrapper() const
  88. {
  89. return static_cast<EditorWrapper const&>(*parent());
  90. }
  91. void Editor::focusin_event(GUI::FocusEvent& event)
  92. {
  93. if (on_focus)
  94. on_focus();
  95. GUI::TextEditor::focusin_event(event);
  96. }
  97. void Editor::focusout_event(GUI::FocusEvent& event)
  98. {
  99. GUI::TextEditor::focusout_event(event);
  100. }
  101. Gfx::IntRect Editor::gutter_icon_rect(size_t line_number) const
  102. {
  103. return gutter_content_rect(line_number).translated(ruler_width() + gutter_width() + frame_thickness(), -vertical_scrollbar().value());
  104. }
  105. void Editor::paint_event(GUI::PaintEvent& event)
  106. {
  107. GUI::TextEditor::paint_event(event);
  108. GUI::Painter painter(*this);
  109. if (is_focused()) {
  110. painter.add_clip_rect(event.rect());
  111. auto rect = frame_inner_rect();
  112. if (vertical_scrollbar().is_visible())
  113. rect.set_width(rect.width() - vertical_scrollbar().width());
  114. if (horizontal_scrollbar().is_visible())
  115. rect.set_height(rect.height() - horizontal_scrollbar().height());
  116. painter.draw_rect(rect, palette().selection());
  117. }
  118. if (gutter_visible()) {
  119. size_t first_visible_line = text_position_at(event.rect().top_left()).line();
  120. size_t last_visible_line = text_position_at(event.rect().bottom_right()).line();
  121. for (size_t line : breakpoint_lines()) {
  122. if (line < first_visible_line || line > last_visible_line) {
  123. continue;
  124. }
  125. auto const& icon = breakpoint_icon_bitmap();
  126. painter.blit(gutter_icon_rect(line).top_left(), icon, icon.rect());
  127. }
  128. if (execution_position().has_value()) {
  129. auto const& icon = current_position_icon_bitmap();
  130. painter.blit(gutter_icon_rect(execution_position().value()).top_left(), icon, icon.rect());
  131. }
  132. if (wrapper().git_repo()) {
  133. for (auto& hunk : wrapper().hunks()) {
  134. auto start_line = hunk.target_start_line;
  135. auto finish_line = start_line + hunk.added_lines.size();
  136. auto additions = hunk.added_lines.size();
  137. auto deletions = hunk.removed_lines.size();
  138. for (size_t line_offset = 0; line_offset < additions; line_offset++) {
  139. auto line = start_line + line_offset;
  140. if (line < first_visible_line || line > last_visible_line) {
  141. continue;
  142. }
  143. auto sign = (line_offset < deletions) ? "!"sv : "+"sv;
  144. painter.draw_text(gutter_icon_rect(line), sign, font(), Gfx::TextAlignment::Center);
  145. }
  146. if (additions < deletions) {
  147. auto deletions_line = min(finish_line, line_count() - 1);
  148. if (deletions_line <= last_visible_line) {
  149. painter.draw_text(gutter_icon_rect(deletions_line), "-"sv, font(), Gfx::TextAlignment::Center);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. static HashMap<String, String>& man_paths()
  157. {
  158. static HashMap<String, String> paths;
  159. if (paths.is_empty()) {
  160. // FIXME: This should also search man3, possibly other places..
  161. Core::DirIterator it("/usr/share/man/man2", Core::DirIterator::Flags::SkipDots);
  162. while (it.has_next()) {
  163. auto path = it.next_full_path();
  164. auto title = LexicalPath::title(path);
  165. paths.set(title, path);
  166. }
  167. }
  168. return paths;
  169. }
  170. void Editor::show_documentation_tooltip_if_available(String const& hovered_token, Gfx::IntPoint const& screen_location)
  171. {
  172. auto it = man_paths().find(hovered_token);
  173. if (it == man_paths().end()) {
  174. dbgln_if(EDITOR_DEBUG, "no man path for {}", hovered_token);
  175. if (m_tooltip_role == TooltipRole::Documentation) {
  176. s_tooltip_window->hide();
  177. m_tooltip_role.clear();
  178. }
  179. return;
  180. }
  181. if (s_tooltip_window->is_visible() && m_tooltip_role == TooltipRole::Documentation && hovered_token == m_last_parsed_token) {
  182. return;
  183. }
  184. dbgln_if(EDITOR_DEBUG, "opening {}", it->value);
  185. auto file = Core::File::construct(it->value);
  186. if (!file->open(Core::OpenMode::ReadOnly)) {
  187. dbgln("failed to open {}, {}", it->value, file->error_string());
  188. return;
  189. }
  190. auto man_document = Markdown::Document::parse(file->read_all());
  191. if (!man_document) {
  192. dbgln("failed to parse markdown");
  193. return;
  194. }
  195. s_tooltip_page_view->load_html(man_document->render_to_html("<style>body { background-color: #dac7b5; }</style>"sv), {});
  196. s_tooltip_window->set_rect(0, 0, 500, 400);
  197. s_tooltip_window->move_to(screen_location.translated(4, 4));
  198. m_tooltip_role = TooltipRole::Documentation;
  199. s_tooltip_window->show();
  200. m_last_parsed_token = hovered_token;
  201. }
  202. void Editor::mousemove_event(GUI::MouseEvent& event)
  203. {
  204. GUI::TextEditor::mousemove_event(event);
  205. if (document().spans().is_empty())
  206. return;
  207. auto text_position = text_position_at(event.position());
  208. if (!text_position.is_valid() && m_tooltip_role == TooltipRole::Documentation) {
  209. s_tooltip_window->hide();
  210. m_tooltip_role.clear();
  211. return;
  212. }
  213. auto highlighter = wrapper().editor().syntax_highlighter();
  214. if (!highlighter)
  215. return;
  216. bool hide_tooltip = (m_tooltip_role == TooltipRole::Documentation);
  217. bool is_over_clickable = false;
  218. auto ruler_line_rect = ruler_content_rect(text_position.line());
  219. auto hovering_lines_ruler = (event.position().x() < ruler_line_rect.width());
  220. if (hovering_lines_ruler && !is_in_drag_select())
  221. set_override_cursor(Gfx::StandardCursor::Arrow);
  222. else if (m_hovering_editor)
  223. set_override_cursor(m_hovering_clickable && event.ctrl() ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::IBeam);
  224. for (auto& span : document().spans()) {
  225. bool is_clickable = (highlighter->is_navigatable(span.data) || highlighter->is_identifier(span.data));
  226. if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) {
  227. if (is_clickable && span.attributes.underline) {
  228. span.attributes.underline = false;
  229. wrapper().editor().update();
  230. }
  231. }
  232. if (span.range.contains(text_position)) {
  233. auto hovered_span_text = document().text_in_range(span.range);
  234. dbgln_if(EDITOR_DEBUG, "Hovering: {} \"{}\"", span.range, hovered_span_text);
  235. if (is_clickable) {
  236. is_over_clickable = true;
  237. bool was_underlined = span.attributes.underline;
  238. span.attributes.underline = event.modifiers() & Mod_Ctrl;
  239. if (span.attributes.underline != was_underlined) {
  240. wrapper().editor().update();
  241. }
  242. }
  243. if (highlighter->is_identifier(span.data)) {
  244. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  245. hide_tooltip = false;
  246. }
  247. }
  248. }
  249. m_previous_text_position = text_position;
  250. if (hide_tooltip) {
  251. s_tooltip_window->hide();
  252. m_tooltip_role.clear();
  253. }
  254. m_hovering_clickable = (is_over_clickable) && (event.modifiers() & Mod_Ctrl);
  255. }
  256. void Editor::mousedown_event(GUI::MouseEvent& event)
  257. {
  258. if (m_tooltip_role == TooltipRole::ParametersHint) {
  259. s_tooltip_window->hide();
  260. m_tooltip_role.clear();
  261. }
  262. auto highlighter = wrapper().editor().syntax_highlighter();
  263. if (!highlighter) {
  264. GUI::TextEditor::mousedown_event(event);
  265. return;
  266. }
  267. auto text_position = text_position_at(event.position());
  268. auto ruler_line_rect = ruler_content_rect(text_position.line());
  269. if (event.button() == GUI::MouseButton::Primary && event.position().x() < ruler_line_rect.width()) {
  270. if (!breakpoint_lines().contains_slow(text_position.line())) {
  271. breakpoint_lines().append(text_position.line());
  272. Debugger::the().on_breakpoint_change(wrapper().filename_title(), text_position.line(), BreakpointChange::Added);
  273. } else {
  274. breakpoint_lines().remove_first_matching([&](size_t line) { return line == text_position.line(); });
  275. Debugger::the().on_breakpoint_change(wrapper().filename_title(), text_position.line(), BreakpointChange::Removed);
  276. }
  277. }
  278. if (!(event.modifiers() & Mod_Ctrl)) {
  279. GUI::TextEditor::mousedown_event(event);
  280. return;
  281. }
  282. if (!text_position.is_valid()) {
  283. GUI::TextEditor::mousedown_event(event);
  284. return;
  285. }
  286. if (auto* span = document().span_at(text_position)) {
  287. if (highlighter->is_navigatable(span->data)) {
  288. on_navigatable_link_click(*span);
  289. return;
  290. }
  291. if (highlighter->is_identifier(span->data)) {
  292. on_identifier_click(*span);
  293. return;
  294. }
  295. }
  296. GUI::TextEditor::mousedown_event(event);
  297. }
  298. void Editor::drop_event(GUI::DropEvent& event)
  299. {
  300. event.accept();
  301. if (event.mime_data().has_urls()) {
  302. auto urls = event.mime_data().urls();
  303. if (urls.is_empty())
  304. return;
  305. window()->move_to_front();
  306. if (urls.size() > 1) {
  307. GUI::MessageBox::show(window(), "HackStudio can only open one file at a time!"sv, "One at a time please!"sv, GUI::MessageBox::Type::Error);
  308. return;
  309. }
  310. set_current_editor_wrapper(static_cast<EditorWrapper*>(parent()));
  311. open_file(urls.first().path());
  312. }
  313. }
  314. void Editor::enter_event(Core::Event& event)
  315. {
  316. m_hovering_editor = true;
  317. GUI::TextEditor::enter_event(event);
  318. }
  319. void Editor::leave_event(Core::Event& event)
  320. {
  321. m_hovering_editor = false;
  322. GUI::TextEditor::leave_event(event);
  323. }
  324. static HashMap<String, String>& include_paths()
  325. {
  326. static HashMap<String, String> paths;
  327. auto add_directory = [](String base, Optional<String> recursive, auto handle_directory) -> void {
  328. Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
  329. while (it.has_next()) {
  330. auto path = it.next_full_path();
  331. if (!Core::File::is_directory(path)) {
  332. auto key = path.substring(base.length() + 1, path.length() - base.length() - 1);
  333. dbgln_if(EDITOR_DEBUG, "Adding header \"{}\" in path \"{}\"", key, path);
  334. paths.set(key, path);
  335. } else {
  336. handle_directory(base, path, handle_directory);
  337. }
  338. }
  339. };
  340. if (paths.is_empty()) {
  341. add_directory(".", {}, add_directory);
  342. add_directory("/usr/local/include", {}, add_directory);
  343. add_directory("/usr/local/include/c++/9.2.0", {}, add_directory);
  344. add_directory("/usr/include", {}, add_directory);
  345. }
  346. return paths;
  347. }
  348. void Editor::navigate_to_include_if_available(String path)
  349. {
  350. auto it = include_paths().find(path);
  351. if (it == include_paths().end()) {
  352. dbgln_if(EDITOR_DEBUG, "no header {} found.", path);
  353. return;
  354. }
  355. on_open(it->value);
  356. }
  357. void Editor::set_execution_position(size_t line_number)
  358. {
  359. code_document().set_execution_position(line_number);
  360. scroll_position_into_view({ line_number, 0 });
  361. update(gutter_icon_rect(line_number));
  362. }
  363. void Editor::clear_execution_position()
  364. {
  365. if (!execution_position().has_value()) {
  366. return;
  367. }
  368. size_t previous_position = execution_position().value();
  369. code_document().clear_execution_position();
  370. update(gutter_icon_rect(previous_position));
  371. }
  372. Gfx::Bitmap const& Editor::breakpoint_icon_bitmap()
  373. {
  374. static auto bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/breakpoint.png"sv).release_value_but_fixme_should_propagate_errors();
  375. return *bitmap;
  376. }
  377. Gfx::Bitmap const& Editor::current_position_icon_bitmap()
  378. {
  379. static auto bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png"sv).release_value_but_fixme_should_propagate_errors();
  380. return *bitmap;
  381. }
  382. CodeDocument const& Editor::code_document() const
  383. {
  384. auto const& doc = document();
  385. VERIFY(doc.is_code_document());
  386. return static_cast<CodeDocument const&>(doc);
  387. }
  388. CodeDocument& Editor::code_document()
  389. {
  390. return const_cast<CodeDocument&>(static_cast<Editor const&>(*this).code_document());
  391. }
  392. void Editor::set_document(GUI::TextDocument& doc)
  393. {
  394. if (has_document() && &document() == &doc)
  395. return;
  396. VERIFY(doc.is_code_document());
  397. GUI::TextEditor::set_document(doc);
  398. set_override_cursor(Gfx::StandardCursor::IBeam);
  399. auto& code_document = static_cast<CodeDocument&>(doc);
  400. set_language_client_for(code_document);
  401. set_syntax_highlighter_for(code_document);
  402. if (m_language_client) {
  403. set_autocomplete_provider(make<LanguageServerAidedAutocompleteProvider>(*m_language_client));
  404. // NOTE:
  405. // When a file is opened for the first time in HackStudio, its content is already synced with the filesystem.
  406. // Otherwise, if the file has already been opened before in some Editor instance, it should exist in the LanguageServer's
  407. // FileDB, and the LanguageServer should already have its up-to-date content.
  408. // So it's OK to just pass an fd here (rather than the TextDocument's content).
  409. int fd = open(code_document.file_path().characters(), O_RDONLY | O_NOCTTY);
  410. if (fd < 0) {
  411. perror("open");
  412. return;
  413. }
  414. m_language_client->open_file(code_document.file_path(), fd);
  415. close(fd);
  416. } else {
  417. set_autocomplete_provider_for(code_document);
  418. }
  419. }
  420. Optional<Editor::AutoCompleteRequestData> Editor::get_autocomplete_request_data()
  421. {
  422. if (!wrapper().editor().m_language_client)
  423. return {};
  424. return Editor::AutoCompleteRequestData { cursor() };
  425. }
  426. void Editor::LanguageServerAidedAutocompleteProvider::provide_completions(Function<void(Vector<CodeComprehension::AutocompleteResultEntry>)> callback)
  427. {
  428. auto& editor = static_cast<Editor&>(*m_editor).wrapper().editor();
  429. auto data = editor.get_autocomplete_request_data();
  430. if (!data.has_value())
  431. callback({});
  432. m_language_client.on_autocomplete_suggestions = [callback = move(callback)](auto suggestions) {
  433. callback(suggestions);
  434. };
  435. m_language_client.request_autocomplete(
  436. editor.code_document().file_path(),
  437. data.value().position.line(),
  438. data.value().position.column());
  439. }
  440. void Editor::will_execute(GUI::TextDocumentUndoCommand const& command)
  441. {
  442. if (!m_language_client)
  443. return;
  444. if (is<GUI::InsertTextCommand>(command)) {
  445. auto const& insert_command = static_cast<GUI::InsertTextCommand const&>(command);
  446. m_language_client->insert_text(
  447. code_document().file_path(),
  448. insert_command.text(),
  449. insert_command.range().start().line(),
  450. insert_command.range().start().column());
  451. return;
  452. }
  453. if (is<GUI::RemoveTextCommand>(command)) {
  454. auto const& remove_command = static_cast<GUI::RemoveTextCommand const&>(command);
  455. m_language_client->remove_text(
  456. code_document().file_path(),
  457. remove_command.range().start().line(),
  458. remove_command.range().start().column(),
  459. remove_command.range().end().line(),
  460. remove_command.range().end().column());
  461. return;
  462. }
  463. VERIFY_NOT_REACHED();
  464. }
  465. void Editor::undo()
  466. {
  467. TextEditor::undo();
  468. flush_file_content_to_langauge_server();
  469. }
  470. void Editor::redo()
  471. {
  472. TextEditor::redo();
  473. flush_file_content_to_langauge_server();
  474. }
  475. void Editor::flush_file_content_to_langauge_server()
  476. {
  477. if (!m_language_client)
  478. return;
  479. m_language_client->set_file_content(
  480. code_document().file_path(),
  481. document().text());
  482. }
  483. void Editor::on_navigatable_link_click(const GUI::TextDocumentSpan& span)
  484. {
  485. auto span_text = document().text_in_range(span.range);
  486. auto header_path = span_text.substring(1, span_text.length() - 2);
  487. dbgln_if(EDITOR_DEBUG, "Ctrl+click: {} \"{}\"", span.range, header_path);
  488. navigate_to_include_if_available(header_path);
  489. }
  490. void Editor::on_identifier_click(const GUI::TextDocumentSpan& span)
  491. {
  492. if (!m_language_client)
  493. return;
  494. m_language_client->on_declaration_found = [](String const& file, size_t line, size_t column) {
  495. HackStudio::open_file(file, line, column);
  496. };
  497. m_language_client->search_declaration(code_document().file_path(), span.range.start().line(), span.range.start().column());
  498. }
  499. void Editor::set_cursor(const GUI::TextPosition& a_position)
  500. {
  501. TextEditor::set_cursor(a_position);
  502. }
  503. void Editor::set_syntax_highlighter_for(CodeDocument const& document)
  504. {
  505. switch (document.language()) {
  506. case Language::Cpp:
  507. if (m_use_semantic_syntax_highlighting) {
  508. set_syntax_highlighter(make<Cpp::SemanticSyntaxHighlighter>());
  509. on_token_info_timer_tick();
  510. m_tokens_info_timer->restart();
  511. } else
  512. set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
  513. break;
  514. case Language::CSS:
  515. set_syntax_highlighter(make<Web::CSS::SyntaxHighlighter>());
  516. break;
  517. case Language::GitCommit:
  518. set_syntax_highlighter(make<GUI::GitCommitSyntaxHighlighter>());
  519. break;
  520. case Language::GML:
  521. set_syntax_highlighter(make<GUI::GML::SyntaxHighlighter>());
  522. break;
  523. case Language::HTML:
  524. set_syntax_highlighter(make<Web::HTML::SyntaxHighlighter>());
  525. break;
  526. case Language::JavaScript:
  527. set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  528. break;
  529. case Language::Ini:
  530. set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  531. break;
  532. case Language::Shell:
  533. set_syntax_highlighter(make<Shell::SyntaxHighlighter>());
  534. break;
  535. case Language::SQL:
  536. set_syntax_highlighter(make<SQL::AST::SyntaxHighlighter>());
  537. break;
  538. default:
  539. set_syntax_highlighter({});
  540. }
  541. force_rehighlight();
  542. }
  543. void Editor::set_autocomplete_provider_for(CodeDocument const& document)
  544. {
  545. switch (document.language()) {
  546. case Language::GML:
  547. set_autocomplete_provider(make<GUI::GML::AutocompleteProvider>());
  548. break;
  549. default:
  550. set_autocomplete_provider({});
  551. }
  552. }
  553. void Editor::set_language_client_for(CodeDocument const& document)
  554. {
  555. if (m_language_client && m_language_client->language() == document.language())
  556. return;
  557. if (document.language() == Language::Cpp)
  558. m_language_client = get_language_client<LanguageClients::Cpp::ConnectionToServer>(project().root_path());
  559. if (document.language() == Language::Shell)
  560. m_language_client = get_language_client<LanguageClients::Shell::ConnectionToServer>(project().root_path());
  561. if (m_language_client) {
  562. m_language_client->on_tokens_info_result = [this](Vector<CodeComprehension::TokenInfo> const& tokens_info) {
  563. on_tokens_info_result(tokens_info);
  564. };
  565. }
  566. }
  567. void Editor::keydown_event(GUI::KeyEvent& event)
  568. {
  569. TextEditor::keydown_event(event);
  570. if (m_tooltip_role == TooltipRole::ParametersHint) {
  571. s_tooltip_window->hide();
  572. m_tooltip_role.clear();
  573. }
  574. if (!event.shift() && !event.alt() && event.ctrl() && event.key() == KeyCode::Key_P) {
  575. handle_function_parameters_hint_request();
  576. }
  577. m_tokens_info_timer->restart();
  578. }
  579. void Editor::handle_function_parameters_hint_request()
  580. {
  581. if (!m_language_client)
  582. return;
  583. m_language_client->on_function_parameters_hint_result = [this](Vector<String> const& params, size_t argument_index) {
  584. dbgln("on_function_parameters_hint_result");
  585. StringBuilder html;
  586. for (size_t i = 0; i < params.size(); ++i) {
  587. if (i == argument_index)
  588. html.append("<b>"sv);
  589. html.appendff("{}", params[i]);
  590. if (i == argument_index)
  591. html.append("</b>"sv);
  592. if (i < params.size() - 1)
  593. html.append(", "sv);
  594. }
  595. html.append("<style>body { background-color: #dac7b5; }</style>"sv);
  596. s_tooltip_page_view->load_html(html.build(), {});
  597. auto cursor_rect = current_editor().cursor_content_rect().location().translated(screen_relative_rect().location());
  598. Gfx::Rect content(cursor_rect.x(), cursor_rect.y(), s_tooltip_page_view->children_clip_rect().width(), s_tooltip_page_view->children_clip_rect().height());
  599. m_tooltip_role = TooltipRole::ParametersHint;
  600. s_tooltip_window->set_rect(0, 0, 280, 35);
  601. s_tooltip_window->move_to(cursor_rect.x(), cursor_rect.y() - s_tooltip_window->height() - vertical_scrollbar().value());
  602. s_tooltip_window->show();
  603. };
  604. m_language_client->get_parameters_hint(
  605. code_document().file_path(),
  606. cursor().line(),
  607. cursor().column());
  608. }
  609. void Editor::set_debug_mode(bool enabled)
  610. {
  611. m_move_execution_to_line_action->set_enabled(enabled);
  612. }
  613. void Editor::on_token_info_timer_tick()
  614. {
  615. if (!semantic_syntax_highlighting_is_enabled())
  616. return;
  617. if (!m_language_client || !m_language_client->is_active_client())
  618. return;
  619. m_language_client->get_tokens_info(code_document().file_path());
  620. }
  621. void Editor::on_tokens_info_result(Vector<CodeComprehension::TokenInfo> const& tokens_info)
  622. {
  623. auto highlighter = syntax_highlighter();
  624. if (highlighter && highlighter->is_cpp_semantic_highlighter()) {
  625. auto& semantic_cpp_highlighter = verify_cast<Cpp::SemanticSyntaxHighlighter>(*highlighter);
  626. semantic_cpp_highlighter.update_tokens_info(tokens_info);
  627. force_rehighlight();
  628. }
  629. }
  630. void Editor::create_tokens_info_timer()
  631. {
  632. static constexpr size_t token_info_timer_interval_ms = 1000;
  633. m_tokens_info_timer = Core::Timer::create_repeating((int)token_info_timer_interval_ms, [this] {
  634. on_token_info_timer_tick();
  635. m_tokens_info_timer->stop();
  636. });
  637. m_tokens_info_timer->start();
  638. }
  639. void Editor::set_semantic_syntax_highlighting(bool value)
  640. {
  641. m_use_semantic_syntax_highlighting = value;
  642. set_syntax_highlighter_for(code_document());
  643. }
  644. }