From 137d68a2aee129cdbf856dccd0a33b7591ee444a Mon Sep 17 00:00:00 2001 From: Oriko Date: Thu, 12 Mar 2020 17:23:54 +0200 Subject: [PATCH] HackStudio: Abstract over syntax highlighter --- DevTools/HackStudio/Editor.cpp | 25 ++++++++++++----------- Libraries/LibGUI/CppSyntaxHighlighter.cpp | 12 +++++++++++ Libraries/LibGUI/CppSyntaxHighlighter.h | 3 +++ Libraries/LibGUI/SyntaxHighlighter.h | 3 +++ Libraries/LibGUI/TextEditor.cpp | 5 +---- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/DevTools/HackStudio/Editor.cpp b/DevTools/HackStudio/Editor.cpp index abbf06a3a8..1a4b530ebe 100644 --- a/DevTools/HackStudio/Editor.cpp +++ b/DevTools/HackStudio/Editor.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -184,13 +183,16 @@ void Editor::mousemove_event(GUI::MouseEvent& event) return; } + auto highlighter = wrapper().editor().syntax_highlighter(); + if (!highlighter) + return; + bool hide_tooltip = true; - bool is_over_header = false; + bool is_over_link = false; for (auto& span : document().spans()) { if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) { - auto token = static_cast(reinterpret_cast(span.data)); - if (token == GUI::CppToken::Type::IncludePath && span.is_underlined) { + if (highlighter->is_navigatable(span.data) && span.is_underlined) { span.is_underlined = false; wrapper().editor().update(); } @@ -204,15 +206,15 @@ void Editor::mousemove_event(GUI::MouseEvent& event) dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\""; #endif - auto token = static_cast(reinterpret_cast(span.data)); - if (token == GUI::CppToken::Type::IncludePath) { - is_over_header = true; + if (highlighter->is_navigatable(span.data)) { + is_over_link = true; bool was_underlined = span.is_underlined; span.is_underlined = event.modifiers() & Mod_Ctrl; if (span.is_underlined != was_underlined) { wrapper().editor().update(); } - } else if (token == GUI::CppToken::Type::Identifier) { + } + if (highlighter->is_identifier(span.data)) { show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location())); hide_tooltip = false; } @@ -223,13 +225,13 @@ void Editor::mousemove_event(GUI::MouseEvent& event) if (hide_tooltip) m_documentation_tooltip_window->hide(); - m_hovering_link = is_over_header && (event.modifiers() & Mod_Ctrl); + m_hovering_link = is_over_link && (event.modifiers() & Mod_Ctrl); } void Editor::mousedown_event(GUI::MouseEvent& event) { auto highlighter = wrapper().editor().syntax_highlighter(); - if (!highlighter || highlighter->language() != GUI::SyntaxLanguage::Cpp) { + if (!highlighter) { GUI::TextEditor::mousedown_event(event); return; } @@ -247,8 +249,7 @@ void Editor::mousedown_event(GUI::MouseEvent& event) for (auto& span : document().spans()) { if (span.range.contains(text_position)) { - auto token = static_cast(reinterpret_cast(span.data)); - if (token != GUI::CppToken::Type::IncludePath) { + if (!highlighter->is_navigatable(span.data)) { GUI::TextEditor::mousedown_event(event); return; } diff --git a/Libraries/LibGUI/CppSyntaxHighlighter.cpp b/Libraries/LibGUI/CppSyntaxHighlighter.cpp index 76561339e0..aa68539ede 100644 --- a/Libraries/LibGUI/CppSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/CppSyntaxHighlighter.cpp @@ -37,6 +37,18 @@ static TextStyle style_for_token_type(CppToken::Type type) } } +bool CppSyntaxHighlighter::is_identifier(void* token) const +{ + auto cpp_token = static_cast(reinterpret_cast(token)); + return cpp_token == GUI::CppToken::Type::Identifier; +} + +bool CppSyntaxHighlighter::is_navigatable(void* token) const +{ + auto cpp_token = static_cast(reinterpret_cast(token)); + return cpp_token == GUI::CppToken::Type::IncludePath; +} + void CppSyntaxHighlighter::rehighlight() { ASSERT(m_editor); diff --git a/Libraries/LibGUI/CppSyntaxHighlighter.h b/Libraries/LibGUI/CppSyntaxHighlighter.h index 0f77c88cd7..a3f50deb0a 100644 --- a/Libraries/LibGUI/CppSyntaxHighlighter.h +++ b/Libraries/LibGUI/CppSyntaxHighlighter.h @@ -9,6 +9,9 @@ public: CppSyntaxHighlighter() {} virtual ~CppSyntaxHighlighter() override; + virtual bool is_identifier(void*) const override; + virtual bool is_navigatable(void*) const override; + virtual SyntaxLanguage language() const override { return SyntaxLanguage::Cpp; } virtual void rehighlight() override; virtual void highlight_matching_token_pair() override; diff --git a/Libraries/LibGUI/SyntaxHighlighter.h b/Libraries/LibGUI/SyntaxHighlighter.h index 568dd403d4..8a15114381 100644 --- a/Libraries/LibGUI/SyntaxHighlighter.h +++ b/Libraries/LibGUI/SyntaxHighlighter.h @@ -22,6 +22,9 @@ public: virtual void rehighlight() = 0; virtual void highlight_matching_token_pair() = 0; + virtual bool is_identifier(void*) const { return false; }; + virtual bool is_navigatable(void*) const { return false; }; + void attach(TextEditor& editor); void detach(); void cursor_did_change(); diff --git a/Libraries/LibGUI/TextEditor.cpp b/Libraries/LibGUI/TextEditor.cpp index 78bb871bce..babc705810 100644 --- a/Libraries/LibGUI/TextEditor.cpp +++ b/Libraries/LibGUI/TextEditor.cpp @@ -1497,10 +1497,7 @@ void TextEditor::flush_pending_change_notification_if_needed() const SyntaxHighlighter* TextEditor::syntax_highlighter() const { - if (m_highlighter) - return m_highlighter.ptr(); - else - return nullptr; + return m_highlighter.ptr(); } void TextEditor::set_syntax_highlighter(OwnPtr highlighter)