Serenity Operating System
at master 125 lines 4.6 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2020-2022, Itamar S. <itamar8910@gmail.com> 4 * Copyright (c) 2018-2022, the SerenityOS developers. 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#pragma once 10 11#include "CodeDocument.h" 12#include "Debugger/BreakpointCallback.h" 13#include "LanguageClient.h" 14#include <AK/Assertions.h> 15#include <AK/Optional.h> 16#include <AK/OwnPtr.h> 17#include <LibGUI/TextEditor.h> 18#include <LibWebView/Forward.h> 19 20namespace HackStudio { 21 22class EditorWrapper; 23 24class Editor final : public GUI::TextEditor { 25 C_OBJECT_ABSTRACT(Editor) 26public: 27 static ErrorOr<NonnullRefPtr<Editor>> try_create(); 28 29 virtual ~Editor() override = default; 30 31 Function<void()> on_focus; 32 Function<void(DeprecatedString)> on_open; 33 34 EditorWrapper& wrapper(); 35 EditorWrapper const& wrapper() const; 36 37 Vector<size_t> const& breakpoint_lines() const { return code_document().breakpoint_lines(); } 38 Vector<size_t>& breakpoint_lines() { return code_document().breakpoint_lines(); } 39 Optional<size_t> execution_position() const { return code_document().execution_position(); } 40 bool is_program_running() const { return execution_position().has_value(); } 41 void set_execution_position(size_t line_number); 42 void clear_execution_position(); 43 void set_debug_mode(bool); 44 45 CodeDocument const& code_document() const; 46 CodeDocument& code_document(); 47 48 virtual void set_document(GUI::TextDocument&) override; 49 virtual void will_execute(GUI::TextDocumentUndoCommand const&) override; 50 51 virtual void undo() override; 52 virtual void redo() override; 53 54 LanguageClient& language_client() 55 { 56 VERIFY(m_language_client); 57 return *m_language_client; 58 } 59 virtual void set_cursor(const GUI::TextPosition& a_position) override; 60 void set_semantic_syntax_highlighting(bool value); 61 62private: 63 virtual void focusin_event(GUI::FocusEvent&) override; 64 virtual void focusout_event(GUI::FocusEvent&) override; 65 virtual void paint_event(GUI::PaintEvent&) override; 66 virtual void mousemove_event(GUI::MouseEvent&) override; 67 virtual void mousedown_event(GUI::MouseEvent&) override; 68 virtual void drag_enter_event(GUI::DragEvent&) override; 69 virtual void drop_event(GUI::DropEvent&) override; 70 virtual void enter_event(Core::Event&) override; 71 virtual void leave_event(Core::Event&) override; 72 virtual void keydown_event(GUI::KeyEvent&) override; 73 74 void show_documentation_tooltip_if_available(DeprecatedString const&, Gfx::IntPoint screen_location); 75 void navigate_to_include_if_available(DeprecatedString); 76 void on_navigatable_link_click(const GUI::TextDocumentSpan&); 77 void on_identifier_click(const GUI::TextDocumentSpan&); 78 79 Gfx::IntRect gutter_icon_rect(size_t line_number) const; 80 static Gfx::Bitmap const& breakpoint_icon_bitmap(); 81 static Gfx::Bitmap const& current_position_icon_bitmap(); 82 static ErrorOr<void> initialize_tooltip_window(); 83 84 struct AutoCompleteRequestData { 85 GUI::TextPosition position; 86 }; 87 88 class LanguageServerAidedAutocompleteProvider final : virtual public GUI::AutocompleteProvider { 89 public: 90 LanguageServerAidedAutocompleteProvider(LanguageClient& client) 91 : m_language_client(client) 92 { 93 } 94 virtual ~LanguageServerAidedAutocompleteProvider() override { } 95 96 private: 97 virtual void provide_completions(Function<void(Vector<CodeComprehension::AutocompleteResultEntry>)> callback) override; 98 LanguageClient& m_language_client; 99 }; 100 101 Optional<AutoCompleteRequestData> get_autocomplete_request_data(); 102 103 void flush_file_content_to_langauge_server(); 104 void set_syntax_highlighter_for(CodeDocument const&); 105 void set_language_client_for(CodeDocument const&); 106 void set_autocomplete_provider_for(CodeDocument const&); 107 void handle_function_parameters_hint_request(); 108 void on_token_info_timer_tick(); 109 void on_tokens_info_result(Vector<CodeComprehension::TokenInfo> const& tokens_info); 110 void create_tokens_info_timer(); 111 112 explicit Editor(); 113 114 DeprecatedString m_last_parsed_token; 115 GUI::TextPosition m_previous_text_position { 0, 0 }; 116 bool m_hovering_editor { false }; 117 bool m_hovering_clickable { false }; 118 bool m_autocomplete_in_focus { false }; 119 RefPtr<GUI::Action> m_move_execution_to_line_action; 120 RefPtr<Core::Timer> m_tokens_info_timer; // Used for querying language server for syntax highlighting info 121 OwnPtr<LanguageClient> m_language_client; 122 bool m_use_semantic_syntax_highlighting { false }; 123}; 124 125}