mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:48:12 +00:00
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. :^)
This commit is contained in:
parent
5f7f97355e
commit
c8e02e60a6
6 changed files with 89 additions and 1 deletions
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue