Editor.cpp 25 KB

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