From 8ec4328fcb976f9088e0f1c19662310f3b776034 Mon Sep 17 00:00:00 2001 From: Itamar Date: Mon, 7 Feb 2022 08:49:07 +0200 Subject: [PATCH] HackStudio: Allow toggling between simple and semantic highlighting Until it becomes enough stable and performant, semantic highlighting is disabled by default. It can be toggled on via the "Project" menu in HackStudio. --- Userland/DevTools/HackStudio/Editor.cpp | 21 ++++++++++++++----- Userland/DevTools/HackStudio/Editor.h | 7 ++++--- .../DevTools/HackStudio/HackStudioWidget.cpp | 12 +++++++++++ .../DevTools/HackStudio/HackStudioWidget.h | 1 + 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Userland/DevTools/HackStudio/Editor.cpp b/Userland/DevTools/HackStudio/Editor.cpp index 4dcaa76050..f077e7e583 100644 --- a/Userland/DevTools/HackStudio/Editor.cpp +++ b/Userland/DevTools/HackStudio/Editor.cpp @@ -480,8 +480,8 @@ void Editor::set_document(GUI::TextDocument& doc) auto& code_document = static_cast(doc); - set_syntax_highlighter_for(code_document); set_language_client_for(code_document); + set_syntax_highlighter_for(code_document); if (m_language_client) { set_autocomplete_provider(make(*m_language_client)); @@ -500,9 +500,6 @@ void Editor::set_document(GUI::TextDocument& doc) } else { set_autocomplete_provider_for(code_document); } - - on_token_info_timer_tick(); - m_tokens_info_timer->restart(); } Optional Editor::get_autocomplete_request_data() @@ -609,7 +606,13 @@ void Editor::set_syntax_highlighter_for(const CodeDocument& document) { switch (document.language()) { case Language::Cpp: - set_syntax_highlighter(make()); + if (m_use_semantic_syntax_highlighting) { + set_syntax_highlighter(make()); + on_token_info_timer_tick(); + m_tokens_info_timer->restart(); + } else + set_syntax_highlighter(make()); + break; case Language::CSS: set_syntax_highlighter(make()); @@ -638,6 +641,8 @@ void Editor::set_syntax_highlighter_for(const CodeDocument& document) default: set_syntax_highlighter({}); } + + force_rehighlight(); } void Editor::set_autocomplete_provider_for(CodeDocument const& document) @@ -754,4 +759,10 @@ void Editor::create_tokens_info_timer() m_tokens_info_timer->start(); } +void Editor::set_semantic_syntax_highlighting(bool value) +{ + m_use_semantic_syntax_highlighting = value; + set_syntax_highlighter_for(code_document()); +} + } diff --git a/Userland/DevTools/HackStudio/Editor.h b/Userland/DevTools/HackStudio/Editor.h index 1076b15615..705fe5b811 100644 --- a/Userland/DevTools/HackStudio/Editor.h +++ b/Userland/DevTools/HackStudio/Editor.h @@ -57,6 +57,7 @@ public: return *m_language_client; } virtual void set_cursor(const GUI::TextPosition& a_position) override; + void set_semantic_syntax_highlighting(bool value); private: virtual void focusin_event(GUI::FocusEvent&) override; @@ -105,6 +106,8 @@ private: void on_token_info_timer_tick(); void on_tokens_info_result(Vector const& tokens_info); void create_tokens_info_timer(); + ErrorOr initialize_documentation_tooltip(); + ErrorOr initialize_parameters_hint_tooltip(); explicit Editor(); @@ -120,10 +123,8 @@ private: RefPtr m_evaluate_expression_action; RefPtr m_move_execution_to_line_action; RefPtr m_tokens_info_timer; // Used for querying language server for syntax highlighting info - OwnPtr m_language_client; - ErrorOr initialize_documentation_tooltip(); - ErrorOr initialize_parameters_hint_tooltip(); + bool m_use_semantic_syntax_highlighting { false }; }; } diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index 4861d593eb..eb0442b9b6 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -1158,9 +1158,11 @@ void HackStudioWidget::create_project_menu(GUI::Window& window) for (auto& new_file_action : m_new_file_actions) { new_submenu.add_action(new_file_action); } + new_submenu.add_action(*m_new_plain_file_action); new_submenu.add_separator(); new_submenu.add_action(*m_new_directory_action); + project_menu.add_action(create_toggle_syntax_highlighting_mode_action()); } void HackStudioWidget::create_edit_menu(GUI::Window& window) @@ -1520,4 +1522,14 @@ void HackStudioWidget::for_each_open_file(Function fun } } +NonnullRefPtr HackStudioWidget::create_toggle_syntax_highlighting_mode_action() +{ + auto action = GUI::Action::create_checkable("&Semantic Highlighting", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-cplusplus.png").release_value_but_fixme_should_propagate_errors(), [this](auto& action) { + for (auto& editor_wrapper : m_all_editor_wrappers) + editor_wrapper.editor().set_semantic_syntax_highlighting(action.is_checked()); + }); + + return action; +} + } diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.h b/Userland/DevTools/HackStudio/HackStudioWidget.h index 709a0d50e7..07929f4d87 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.h +++ b/Userland/DevTools/HackStudio/HackStudioWidget.h @@ -107,6 +107,7 @@ private: NonnullRefPtr create_build_action(); NonnullRefPtr create_run_action(); NonnullRefPtr create_stop_action(); + NonnullRefPtr create_toggle_syntax_highlighting_mode_action(); void create_location_history_actions(); void add_new_editor(GUI::Widget& parent);