Editor.cpp 23 KB

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