Editor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2018-2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Editor.h"
  8. #include "Debugger/Debugger.h"
  9. #include "Debugger/EvaluateExpressionDialog.h"
  10. #include "EditorWrapper.h"
  11. #include "HackStudio.h"
  12. #include "Language.h"
  13. #include <AK/ByteBuffer.h>
  14. #include <AK/Debug.h>
  15. #include <AK/LexicalPath.h>
  16. #include <LibCore/DirIterator.h>
  17. #include <LibCore/File.h>
  18. #include <LibCpp/SyntaxHighlighter.h>
  19. #include <LibGUI/Action.h>
  20. #include <LibGUI/Application.h>
  21. #include <LibGUI/GMLAutocompleteProvider.h>
  22. #include <LibGUI/GMLSyntaxHighlighter.h>
  23. #include <LibGUI/INISyntaxHighlighter.h>
  24. #include <LibGUI/Label.h>
  25. #include <LibGUI/MessageBox.h>
  26. #include <LibGUI/Painter.h>
  27. #include <LibGUI/Scrollbar.h>
  28. #include <LibGUI/Window.h>
  29. #include <LibJS/SyntaxHighlighter.h>
  30. #include <LibMarkdown/Document.h>
  31. #include <LibSQL/AST/SyntaxHighlighter.h>
  32. #include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h>
  33. #include <LibWeb/DOM/Text.h>
  34. #include <LibWeb/HTML/HTMLHeadElement.h>
  35. #include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
  36. #include <LibWeb/OutOfProcessWebView.h>
  37. #include <Shell/SyntaxHighlighter.h>
  38. #include <fcntl.h>
  39. namespace HackStudio {
  40. Editor::Editor()
  41. {
  42. set_document(CodeDocument::create());
  43. initialize_documentation_tooltip();
  44. initialize_parameters_hint_tooltip();
  45. m_evaluate_expression_action = GUI::Action::create("Evaluate expression", { Mod_Ctrl, Key_E }, [this](auto&) {
  46. VERIFY(is_program_running());
  47. auto dialog = EvaluateExpressionDialog::construct(window());
  48. dialog->exec();
  49. });
  50. m_move_execution_to_line_action = GUI::Action::create("Set execution point to line", [this](auto&) {
  51. VERIFY(is_program_running());
  52. auto success = Debugger::the().set_execution_position(currently_open_file(), cursor().line());
  53. if (success) {
  54. set_execution_position(cursor().line());
  55. } else {
  56. GUI::MessageBox::show(window(), "Failed to set execution position", "Error", GUI::MessageBox::Type::Error);
  57. }
  58. });
  59. set_debug_mode(false);
  60. add_custom_context_menu_action(*m_evaluate_expression_action);
  61. add_custom_context_menu_action(*m_move_execution_to_line_action);
  62. set_gutter_visible(true);
  63. }
  64. Editor::~Editor()
  65. {
  66. }
  67. void Editor::initialize_documentation_tooltip()
  68. {
  69. m_documentation_tooltip_window = GUI::Window::construct();
  70. m_documentation_tooltip_window->set_rect(0, 0, 500, 400);
  71. m_documentation_tooltip_window->set_window_type(GUI::WindowType::Tooltip);
  72. m_documentation_page_view = m_documentation_tooltip_window->set_main_widget<Web::OutOfProcessWebView>();
  73. }
  74. 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 = m_parameters_hint_tooltip_window->set_main_widget<Web::OutOfProcessWebView>();
  80. }
  81. EditorWrapper& Editor::wrapper()
  82. {
  83. return static_cast<EditorWrapper&>(*parent());
  84. }
  85. const EditorWrapper& Editor::wrapper() const
  86. {
  87. return static_cast<const EditorWrapper&>(*parent());
  88. }
  89. void Editor::focusin_event(GUI::FocusEvent& event)
  90. {
  91. wrapper().set_editor_has_focus({}, true);
  92. if (on_focus)
  93. on_focus();
  94. GUI::TextEditor::focusin_event(event);
  95. }
  96. void Editor::focusout_event(GUI::FocusEvent& event)
  97. {
  98. wrapper().set_editor_has_focus({}, false);
  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. const auto& icon = breakpoint_icon_bitmap();
  126. painter.blit(gutter_icon_rect(line).top_left(), icon, icon.rect());
  127. }
  128. if (execution_position().has_value()) {
  129. const auto& 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. char const* sign = (line_offset < deletions) ? "!" : "+";
  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), "-", 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(const String& hovered_token, const Gfx::IntPoint& 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. m_documentation_tooltip_window->hide();
  176. return;
  177. }
  178. if (m_documentation_tooltip_window->is_visible() && hovered_token == m_last_parsed_token) {
  179. return;
  180. }
  181. dbgln_if(EDITOR_DEBUG, "opening {}", it->value);
  182. auto file = Core::File::construct(it->value);
  183. if (!file->open(Core::OpenMode::ReadOnly)) {
  184. dbgln("failed to open {}, {}", it->value, file->error_string());
  185. return;
  186. }
  187. auto man_document = Markdown::Document::parse(file->read_all());
  188. if (!man_document) {
  189. dbgln("failed to parse markdown");
  190. return;
  191. }
  192. StringBuilder html;
  193. // FIXME: With the InProcessWebView we used to manipulate the document body directly,
  194. // With OutOfProcessWebView this isn't possible (at the moment). The ideal solution
  195. // is probably to tweak Markdown::Document::render_to_html() so we can inject styles
  196. // into the rendered HTML easily.
  197. html.append(man_document->render_to_html());
  198. html.append("<style>body { background-color: #dac7b5; }</style>");
  199. m_documentation_page_view->load_html(html.build(), {});
  200. m_documentation_tooltip_window->move_to(screen_location.translated(4, 4));
  201. m_documentation_tooltip_window->show();
  202. m_last_parsed_token = hovered_token;
  203. }
  204. void Editor::mousemove_event(GUI::MouseEvent& event)
  205. {
  206. GUI::TextEditor::mousemove_event(event);
  207. if (document().spans().is_empty())
  208. return;
  209. auto text_position = text_position_at(event.position());
  210. if (!text_position.is_valid()) {
  211. m_documentation_tooltip_window->hide();
  212. return;
  213. }
  214. auto highlighter = wrapper().editor().syntax_highlighter();
  215. if (!highlighter)
  216. return;
  217. bool hide_tooltip = true;
  218. bool is_over_clickable = false;
  219. auto ruler_line_rect = ruler_content_rect(text_position.line());
  220. auto hovering_lines_ruler = (event.position().x() < ruler_line_rect.width());
  221. if (hovering_lines_ruler && !is_in_drag_select())
  222. set_override_cursor(Gfx::StandardCursor::Arrow);
  223. else if (m_hovering_editor)
  224. set_override_cursor(m_hovering_clickable && event.ctrl() ? Gfx::StandardCursor::Hand : Gfx::StandardCursor::IBeam);
  225. for (auto& span : document().spans()) {
  226. bool is_clickable = (highlighter->is_navigatable(span.data) || highlighter->is_identifier(span.data));
  227. if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) {
  228. if (is_clickable && span.attributes.underline) {
  229. span.attributes.underline = false;
  230. wrapper().editor().update();
  231. }
  232. }
  233. if (span.range.contains(text_position)) {
  234. auto adjusted_range = span.range;
  235. auto end_line_length = document().line(span.range.end().line()).length();
  236. adjusted_range.end().set_column(min(end_line_length, adjusted_range.end().column() + 1));
  237. auto hovered_span_text = document().text_in_range(adjusted_range);
  238. dbgln_if(EDITOR_DEBUG, "Hovering: {} \"{}\"", adjusted_range, hovered_span_text);
  239. if (is_clickable) {
  240. is_over_clickable = true;
  241. bool was_underlined = span.attributes.underline;
  242. span.attributes.underline = event.modifiers() & Mod_Ctrl;
  243. if (span.attributes.underline != was_underlined) {
  244. wrapper().editor().update();
  245. }
  246. }
  247. if (highlighter->is_identifier(span.data)) {
  248. show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
  249. hide_tooltip = false;
  250. }
  251. }
  252. }
  253. m_previous_text_position = text_position;
  254. if (hide_tooltip)
  255. m_documentation_tooltip_window->hide();
  256. m_hovering_clickable = (is_over_clickable) && (event.modifiers() & Mod_Ctrl);
  257. }
  258. void Editor::mousedown_event(GUI::MouseEvent& event)
  259. {
  260. m_parameters_hint_tooltip_window->hide();
  261. auto highlighter = wrapper().editor().syntax_highlighter();
  262. if (!highlighter) {
  263. GUI::TextEditor::mousedown_event(event);
  264. return;
  265. }
  266. auto text_position = text_position_at(event.position());
  267. auto ruler_line_rect = ruler_content_rect(text_position.line());
  268. if (event.button() == GUI::MouseButton::Primary && event.position().x() < ruler_line_rect.width()) {
  269. if (!breakpoint_lines().contains_slow(text_position.line())) {
  270. breakpoint_lines().append(text_position.line());
  271. Debugger::the().on_breakpoint_change(wrapper().filename_label().text(), text_position.line(), BreakpointChange::Added);
  272. } else {
  273. breakpoint_lines().remove_first_matching([&](size_t line) { return line == text_position.line(); });
  274. Debugger::the().on_breakpoint_change(wrapper().filename_label().text(), text_position.line(), BreakpointChange::Removed);
  275. }
  276. }
  277. if (!(event.modifiers() & Mod_Ctrl)) {
  278. GUI::TextEditor::mousedown_event(event);
  279. return;
  280. }
  281. if (!text_position.is_valid()) {
  282. GUI::TextEditor::mousedown_event(event);
  283. return;
  284. }
  285. if (auto* span = document().span_at(text_position)) {
  286. if (highlighter->is_navigatable(span->data)) {
  287. on_navigatable_link_click(*span);
  288. return;
  289. }
  290. if (highlighter->is_identifier(span->data)) {
  291. on_identifier_click(*span);
  292. return;
  293. }
  294. }
  295. GUI::TextEditor::mousedown_event(event);
  296. }
  297. void Editor::drop_event(GUI::DropEvent& event)
  298. {
  299. event.accept();
  300. if (event.mime_data().has_urls()) {
  301. auto urls = event.mime_data().urls();
  302. if (urls.is_empty())
  303. return;
  304. window()->move_to_front();
  305. if (urls.size() > 1) {
  306. GUI::MessageBox::show(window(), "HackStudio can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error);
  307. return;
  308. }
  309. set_current_editor_wrapper(static_cast<EditorWrapper*>(parent()));
  310. open_file(urls.first().path());
  311. }
  312. }
  313. void Editor::enter_event(Core::Event& event)
  314. {
  315. m_hovering_editor = true;
  316. GUI::TextEditor::enter_event(event);
  317. }
  318. void Editor::leave_event(Core::Event& event)
  319. {
  320. m_hovering_editor = false;
  321. GUI::TextEditor::leave_event(event);
  322. }
  323. static HashMap<String, String>& include_paths()
  324. {
  325. static HashMap<String, String> paths;
  326. auto add_directory = [](String base, Optional<String> recursive, auto handle_directory) -> void {
  327. Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
  328. while (it.has_next()) {
  329. auto path = it.next_full_path();
  330. if (!Core::File::is_directory(path)) {
  331. auto key = path.substring(base.length() + 1, path.length() - base.length() - 1);
  332. dbgln_if(EDITOR_DEBUG, "Adding header \"{}\" in path \"{}\"", key, path);
  333. paths.set(key, path);
  334. } else {
  335. handle_directory(base, path, handle_directory);
  336. }
  337. }
  338. };
  339. if (paths.is_empty()) {
  340. add_directory(".", {}, add_directory);
  341. add_directory("/usr/local/include", {}, add_directory);
  342. add_directory("/usr/local/include/c++/9.2.0", {}, add_directory);
  343. add_directory("/usr/include", {}, add_directory);
  344. }
  345. return paths;
  346. }
  347. void Editor::navigate_to_include_if_available(String path)
  348. {
  349. auto it = include_paths().find(path);
  350. if (it == include_paths().end()) {
  351. dbgln_if(EDITOR_DEBUG, "no header {} found.", path);
  352. return;
  353. }
  354. on_open(it->value);
  355. }
  356. void Editor::set_execution_position(size_t line_number)
  357. {
  358. code_document().set_execution_position(line_number);
  359. scroll_position_into_view({ line_number, 0 });
  360. update(gutter_icon_rect(line_number));
  361. }
  362. void Editor::clear_execution_position()
  363. {
  364. if (!execution_position().has_value()) {
  365. return;
  366. }
  367. size_t previous_position = execution_position().value();
  368. code_document().clear_execution_position();
  369. update(gutter_icon_rect(previous_position));
  370. }
  371. const Gfx::Bitmap& Editor::breakpoint_icon_bitmap()
  372. {
  373. static auto bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/breakpoint.png").release_value_but_fixme_should_propagate_errors();
  374. return *bitmap;
  375. }
  376. const Gfx::Bitmap& Editor::current_position_icon_bitmap()
  377. {
  378. static auto bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors();
  379. return *bitmap;
  380. }
  381. const CodeDocument& Editor::code_document() const
  382. {
  383. const auto& doc = document();
  384. VERIFY(doc.is_code_document());
  385. return static_cast<const CodeDocument&>(doc);
  386. }
  387. CodeDocument& Editor::code_document()
  388. {
  389. return const_cast<CodeDocument&>(static_cast<const Editor&>(*this).code_document());
  390. }
  391. void Editor::set_document(GUI::TextDocument& doc)
  392. {
  393. if (has_document() && &document() == &doc)
  394. return;
  395. VERIFY(doc.is_code_document());
  396. GUI::TextEditor::set_document(doc);
  397. set_override_cursor(Gfx::StandardCursor::IBeam);
  398. auto& code_document = static_cast<CodeDocument&>(doc);
  399. set_syntax_highlighter_for(code_document);
  400. set_language_client_for(code_document);
  401. if (m_language_client) {
  402. set_autocomplete_provider(make<LanguageServerAidedAutocompleteProvider>(*m_language_client));
  403. // NOTE:
  404. // When a file is opened for the first time in HackStudio, its content is already synced with the filesystem.
  405. // Otherwise, if the file has already been opened before in some Editor instance, it should exist in the LanguageServer's
  406. // FileDB, and the LanguageServer should already have its up-to-date content.
  407. // So it's OK to just pass an fd here (rather than the TextDocument's content).
  408. int fd = open(code_document.file_path().characters(), O_RDONLY | O_NOCTTY);
  409. if (fd < 0) {
  410. perror("open");
  411. return;
  412. }
  413. m_language_client->open_file(code_document.file_path(), fd);
  414. close(fd);
  415. } else {
  416. set_autocomplete_provider_for(code_document);
  417. }
  418. }
  419. Optional<Editor::AutoCompleteRequestData> Editor::get_autocomplete_request_data()
  420. {
  421. if (!wrapper().editor().m_language_client)
  422. return {};
  423. return Editor::AutoCompleteRequestData { cursor() };
  424. }
  425. void Editor::LanguageServerAidedAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)> callback)
  426. {
  427. auto& editor = static_cast<Editor&>(*m_editor).wrapper().editor();
  428. auto data = editor.get_autocomplete_request_data();
  429. if (!data.has_value())
  430. callback({});
  431. m_language_client.on_autocomplete_suggestions = [callback = move(callback)](auto suggestions) {
  432. callback(suggestions);
  433. };
  434. m_language_client.request_autocomplete(
  435. editor.code_document().file_path(),
  436. data.value().position.line(),
  437. data.value().position.column());
  438. }
  439. void Editor::will_execute(GUI::TextDocumentUndoCommand const& command)
  440. {
  441. if (!m_language_client)
  442. return;
  443. if (is<GUI::InsertTextCommand>(command)) {
  444. auto const& insert_command = static_cast<GUI::InsertTextCommand const&>(command);
  445. m_language_client->insert_text(
  446. code_document().file_path(),
  447. insert_command.text(),
  448. insert_command.range().start().line(),
  449. insert_command.range().start().column());
  450. return;
  451. }
  452. if (is<GUI::RemoveTextCommand>(command)) {
  453. auto const& remove_command = static_cast<GUI::RemoveTextCommand const&>(command);
  454. m_language_client->remove_text(
  455. code_document().file_path(),
  456. remove_command.range().start().line(),
  457. remove_command.range().start().column(),
  458. remove_command.range().end().line(),
  459. remove_command.range().end().column());
  460. return;
  461. }
  462. VERIFY_NOT_REACHED();
  463. }
  464. void Editor::undo()
  465. {
  466. TextEditor::undo();
  467. flush_file_content_to_langauge_server();
  468. }
  469. void Editor::redo()
  470. {
  471. TextEditor::redo();
  472. flush_file_content_to_langauge_server();
  473. }
  474. void Editor::flush_file_content_to_langauge_server()
  475. {
  476. if (!m_language_client)
  477. return;
  478. m_language_client->set_file_content(
  479. code_document().file_path(),
  480. document().text());
  481. }
  482. void Editor::on_navigatable_link_click(const GUI::TextDocumentSpan& span)
  483. {
  484. auto span_text = document().text_in_range(span.range);
  485. auto header_path = span_text.substring(1, span_text.length() - 2);
  486. dbgln_if(EDITOR_DEBUG, "Ctrl+click: {} \"{}\"", span.range, header_path);
  487. navigate_to_include_if_available(header_path);
  488. }
  489. void Editor::on_identifier_click(const GUI::TextDocumentSpan& span)
  490. {
  491. if (!m_language_client)
  492. return;
  493. m_language_client->on_declaration_found = [](const String& file, size_t line, size_t column) {
  494. HackStudio::open_file(file, line, column);
  495. };
  496. m_language_client->search_declaration(code_document().file_path(), span.range.start().line(), span.range.start().column());
  497. }
  498. void Editor::set_cursor(const GUI::TextPosition& a_position)
  499. {
  500. TextEditor::set_cursor(a_position);
  501. }
  502. void Editor::set_syntax_highlighter_for(const CodeDocument& document)
  503. {
  504. switch (document.language()) {
  505. case Language::Cpp:
  506. set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
  507. break;
  508. case Language::CSS:
  509. set_syntax_highlighter(make<Web::CSS::SyntaxHighlighter>());
  510. break;
  511. case Language::GML:
  512. set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
  513. break;
  514. case Language::HTML:
  515. set_syntax_highlighter(make<Web::HTML::SyntaxHighlighter>());
  516. break;
  517. case Language::JavaScript:
  518. set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  519. break;
  520. case Language::Ini:
  521. set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  522. break;
  523. case Language::Shell:
  524. set_syntax_highlighter(make<Shell::SyntaxHighlighter>());
  525. break;
  526. case Language::SQL:
  527. set_syntax_highlighter(make<SQL::AST::SyntaxHighlighter>());
  528. break;
  529. default:
  530. set_syntax_highlighter({});
  531. }
  532. }
  533. void Editor::set_autocomplete_provider_for(CodeDocument const& document)
  534. {
  535. switch (document.language()) {
  536. case Language::GML:
  537. set_autocomplete_provider(make<GUI::GMLAutocompleteProvider>());
  538. break;
  539. default:
  540. set_autocomplete_provider({});
  541. }
  542. }
  543. void Editor::set_language_client_for(const CodeDocument& document)
  544. {
  545. if (m_language_client && m_language_client->language() == document.language())
  546. return;
  547. if (document.language() == Language::Cpp)
  548. m_language_client = get_language_client<LanguageClients::Cpp::ServerConnection>(project().root_path());
  549. if (document.language() == Language::Shell)
  550. m_language_client = get_language_client<LanguageClients::Shell::ServerConnection>(project().root_path());
  551. }
  552. void Editor::keydown_event(GUI::KeyEvent& event)
  553. {
  554. TextEditor::keydown_event(event);
  555. m_parameters_hint_tooltip_window->hide();
  556. if (!event.shift() && !event.alt() && event.ctrl() && event.key() == KeyCode::Key_P) {
  557. handle_function_parameters_hint_request();
  558. }
  559. }
  560. void Editor::handle_function_parameters_hint_request()
  561. {
  562. VERIFY(m_language_client);
  563. m_language_client->on_function_parameters_hint_result = [this](Vector<String> const& params, size_t argument_index) {
  564. dbgln("on_function_parameters_hint_result");
  565. StringBuilder html;
  566. for (size_t i = 0; i < params.size(); ++i) {
  567. if (i == argument_index)
  568. html.append("<b>");
  569. html.appendff("{}", params[i]);
  570. if (i == argument_index)
  571. html.append("</b>");
  572. if (i < params.size() - 1)
  573. html.append(", ");
  574. }
  575. html.append("<style>body { background-color: #dac7b5; }</style>");
  576. m_parameter_hint_page_view->load_html(html.build(), {});
  577. auto cursor_rect = current_editor().cursor_content_rect().location().translated(screen_relative_rect().location());
  578. 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());
  579. m_parameters_hint_tooltip_window->move_to(cursor_rect.x(), cursor_rect.y() - m_parameters_hint_tooltip_window->height() - vertical_scrollbar().value());
  580. m_parameters_hint_tooltip_window->show();
  581. };
  582. m_language_client->get_parameters_hint(
  583. code_document().file_path(),
  584. cursor().line(),
  585. cursor().column());
  586. }
  587. void Editor::set_debug_mode(bool enabled)
  588. {
  589. m_evaluate_expression_action->set_enabled(enabled);
  590. m_move_execution_to_line_action->set_enabled(enabled);
  591. }
  592. }