mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 23:37:43 +00:00
HackStudio: Abstract over syntax highlighter
This commit is contained in:
parent
bacd3dd57a
commit
137d68a2ae
5 changed files with 32 additions and 16 deletions
|
@ -31,7 +31,6 @@
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
#include <LibGUI/CppLexer.h>
|
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGUI/ScrollBar.h>
|
#include <LibGUI/ScrollBar.h>
|
||||||
#include <LibGUI/SyntaxHighlighter.h>
|
#include <LibGUI/SyntaxHighlighter.h>
|
||||||
|
@ -184,13 +183,16 @@ void Editor::mousemove_event(GUI::MouseEvent& event)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto highlighter = wrapper().editor().syntax_highlighter();
|
||||||
|
if (!highlighter)
|
||||||
|
return;
|
||||||
|
|
||||||
bool hide_tooltip = true;
|
bool hide_tooltip = true;
|
||||||
bool is_over_header = false;
|
bool is_over_link = false;
|
||||||
|
|
||||||
for (auto& span : document().spans()) {
|
for (auto& span : document().spans()) {
|
||||||
if (span.range.contains(m_previous_text_position) && !span.range.contains(text_position)) {
|
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 (highlighter->is_navigatable(span.data) && span.is_underlined) {
|
||||||
if (token == GUI::CppToken::Type::IncludePath && span.is_underlined) {
|
|
||||||
span.is_underlined = false;
|
span.is_underlined = false;
|
||||||
wrapper().editor().update();
|
wrapper().editor().update();
|
||||||
}
|
}
|
||||||
|
@ -204,15 +206,15 @@ void Editor::mousemove_event(GUI::MouseEvent& event)
|
||||||
dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
|
dbg() << "Hovering: " << adjusted_range << " \"" << hovered_span_text << "\"";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(span.data));
|
if (highlighter->is_navigatable(span.data)) {
|
||||||
if (token == GUI::CppToken::Type::IncludePath) {
|
is_over_link = true;
|
||||||
is_over_header = true;
|
|
||||||
bool was_underlined = span.is_underlined;
|
bool was_underlined = span.is_underlined;
|
||||||
span.is_underlined = event.modifiers() & Mod_Ctrl;
|
span.is_underlined = event.modifiers() & Mod_Ctrl;
|
||||||
if (span.is_underlined != was_underlined) {
|
if (span.is_underlined != was_underlined) {
|
||||||
wrapper().editor().update();
|
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()));
|
show_documentation_tooltip_if_available(hovered_span_text, event.position().translated(screen_relative_rect().location()));
|
||||||
hide_tooltip = false;
|
hide_tooltip = false;
|
||||||
}
|
}
|
||||||
|
@ -223,13 +225,13 @@ void Editor::mousemove_event(GUI::MouseEvent& event)
|
||||||
if (hide_tooltip)
|
if (hide_tooltip)
|
||||||
m_documentation_tooltip_window->hide();
|
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)
|
void Editor::mousedown_event(GUI::MouseEvent& event)
|
||||||
{
|
{
|
||||||
auto highlighter = wrapper().editor().syntax_highlighter();
|
auto highlighter = wrapper().editor().syntax_highlighter();
|
||||||
if (!highlighter || highlighter->language() != GUI::SyntaxLanguage::Cpp) {
|
if (!highlighter) {
|
||||||
GUI::TextEditor::mousedown_event(event);
|
GUI::TextEditor::mousedown_event(event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -247,8 +249,7 @@ void Editor::mousedown_event(GUI::MouseEvent& event)
|
||||||
|
|
||||||
for (auto& span : document().spans()) {
|
for (auto& span : document().spans()) {
|
||||||
if (span.range.contains(text_position)) {
|
if (span.range.contains(text_position)) {
|
||||||
auto token = static_cast<GUI::CppToken::Type>(reinterpret_cast<size_t>(span.data));
|
if (!highlighter->is_navigatable(span.data)) {
|
||||||
if (token != GUI::CppToken::Type::IncludePath) {
|
|
||||||
GUI::TextEditor::mousedown_event(event);
|
GUI::TextEditor::mousedown_event(event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
void CppSyntaxHighlighter::rehighlight()
|
||||||
{
|
{
|
||||||
ASSERT(m_editor);
|
ASSERT(m_editor);
|
||||||
|
|
|
@ -9,6 +9,9 @@ public:
|
||||||
CppSyntaxHighlighter() {}
|
CppSyntaxHighlighter() {}
|
||||||
virtual ~CppSyntaxHighlighter() override;
|
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 SyntaxLanguage language() const override { return SyntaxLanguage::Cpp; }
|
||||||
virtual void rehighlight() override;
|
virtual void rehighlight() override;
|
||||||
virtual void highlight_matching_token_pair() override;
|
virtual void highlight_matching_token_pair() override;
|
||||||
|
|
|
@ -22,6 +22,9 @@ public:
|
||||||
virtual void rehighlight() = 0;
|
virtual void rehighlight() = 0;
|
||||||
virtual void highlight_matching_token_pair() = 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 attach(TextEditor& editor);
|
||||||
void detach();
|
void detach();
|
||||||
void cursor_did_change();
|
void cursor_did_change();
|
||||||
|
|
|
@ -1497,10 +1497,7 @@ void TextEditor::flush_pending_change_notification_if_needed()
|
||||||
|
|
||||||
const SyntaxHighlighter* TextEditor::syntax_highlighter() const
|
const SyntaxHighlighter* TextEditor::syntax_highlighter() const
|
||||||
{
|
{
|
||||||
if (m_highlighter)
|
return m_highlighter.ptr();
|
||||||
return m_highlighter.ptr();
|
|
||||||
else
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
|
void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue