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