HackStudio: Abstract over syntax highlighter

This commit is contained in:
Oriko 2020-03-12 17:23:54 +02:00 committed by Andreas Kling
parent bacd3dd57a
commit 137d68a2ae
Notes: sideshowbarker 2024-07-19 08:20:36 +09:00
5 changed files with 32 additions and 16 deletions

View file

@ -31,7 +31,6 @@
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibGUI/Application.h>
#include <LibGUI/CppLexer.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGUI/SyntaxHighlighter.h>
@ -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<GUI::CppToken::Type>(reinterpret_cast<size_t>(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<GUI::CppToken::Type>(reinterpret_cast<size_t>(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<GUI::CppToken::Type>(reinterpret_cast<size_t>(span.data));
if (token != GUI::CppToken::Type::IncludePath) {
if (!highlighter->is_navigatable(span.data)) {
GUI::TextEditor::mousedown_event(event);
return;
}

View file

@ -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<GUI::CppToken::Type>(reinterpret_cast<size_t>(token));
return cpp_token == GUI::CppToken::Type::Identifier;
}
bool CppSyntaxHighlighter::is_navigatable(void* token) const
{
auto cpp_token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(token));
return cpp_token == GUI::CppToken::Type::IncludePath;
}
void CppSyntaxHighlighter::rehighlight()
{
ASSERT(m_editor);

View file

@ -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;

View file

@ -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();

View file

@ -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<SyntaxHighlighter> highlighter)