diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-11-18 19:10:06 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-18 19:10:06 +0100 |
commit | c8e02e60a6cfe9b6759c5cf1065d21759a8a4afc (patch) | |
tree | 245a16c0516701323bad3de22d8908a85416dc13 /Libraries | |
parent | 5f7f97355e48e4418d4e664555f019d590bdfcf3 (diff) | |
download | serenity-c8e02e60a6cfe9b6759c5cf1065d21759a8a4afc.zip |
HackStudio+LibGUI: Implement matching curly brace highlighting
This works for C++ syntax highlighted text documents by caching the C++
token type in a new "arbitrary data" member of GTextDocumentSpan.
When the cursor is placed immediately before a '{' or immediately after
a '}', we highlight both of these brace buddies by changing their
corresponding spans to have a different background color.
..and spans can also now have a custom background color. :^)
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/GTextDocument.h | 3 | ||||
-rw-r--r-- | Libraries/LibGUI/GTextEditor.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibGUI/GTextEditor.h | 1 |
3 files changed, 9 insertions, 0 deletions
diff --git a/Libraries/LibGUI/GTextDocument.h b/Libraries/LibGUI/GTextDocument.h index 04cc9e32ba..4c31cd0bb7 100644 --- a/Libraries/LibGUI/GTextDocument.h +++ b/Libraries/LibGUI/GTextDocument.h @@ -15,8 +15,10 @@ class GTextDocumentLine; struct GTextDocumentSpan { GTextRange range; Color color; + Optional<Color> background_color; bool is_skippable { false }; const Font* font { nullptr }; + void* data { nullptr }; }; class GTextDocument : public RefCounted<GTextDocument> { @@ -54,6 +56,7 @@ public: bool has_spans() const { return !m_spans.is_empty(); } const Vector<GTextDocumentSpan>& spans() const { return m_spans; } + void set_span_at_index(int index, GTextDocumentSpan span) { m_spans[index] = move(span); } void append_line(NonnullOwnPtr<GTextDocumentLine>); void remove_line(int line_index); diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index ae5b4fc2ed..19264eee70 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -389,6 +389,7 @@ void GTextEditor::paint_event(GPaintEvent& event) for (int i = 0; i < visual_line_text.length(); ++i) { const Font* font = &this->font(); Color color; + Optional<Color> background_color; GTextPosition physical_position(line_index, start_of_visual_line + i); // FIXME: This is *horribly* inefficient. for (auto& span : document().spans()) { @@ -397,8 +398,11 @@ void GTextEditor::paint_event(GPaintEvent& event) color = span.color; if (span.font) font = span.font; + background_color = span.background_color; break; } + if (background_color.has_value()) + painter.fill_rect(character_rect, background_color.value()); painter.draw_text(character_rect, visual_line_text.substring_view(i, 1), *font, m_text_alignment, color); character_rect.move_by(advance, 0); } @@ -1135,6 +1139,7 @@ void GTextEditor::set_cursor(const GTextPosition& a_position) update(old_cursor_line_rect); update_cursor(); } + cursor_did_change(); if (on_cursor_change) on_cursor_change(); } diff --git a/Libraries/LibGUI/GTextEditor.h b/Libraries/LibGUI/GTextEditor.h index a8bf2d6d09..1684348c5f 100644 --- a/Libraries/LibGUI/GTextEditor.h +++ b/Libraries/LibGUI/GTextEditor.h @@ -120,6 +120,7 @@ protected: virtual void leave_event(CEvent&) override; virtual void context_menu_event(GContextMenuEvent&) override; virtual void resize_event(GResizeEvent&) override; + virtual void cursor_did_change() {} GTextPosition text_position_at(const Point&) const; |