Skip to content

Commit 6d89f48

Browse files
oriko1010awesomekling
authored andcommitted
LibGUI: Add underlines to highlighting
1 parent b58893c commit 6d89f48

File tree

6 files changed

+25
-2
lines changed

6 files changed

+25
-2
lines changed

Libraries/LibGUI/CppSyntaxHighlighter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void CppSyntaxHighlighter::rehighlight()
5656
span.color = style.color;
5757
span.font = style.font;
5858
span.is_skippable = token.m_type == CppToken::Type::Whitespace;
59-
span.data = (void*)token.m_type;
59+
span.data = reinterpret_cast<void*>(token.m_type);
6060
spans.append(span);
6161
}
6262
m_editor->document().set_spans(spans);

Libraries/LibGUI/CppSyntaxHighlighter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ namespace GUI {
77
class CppSyntaxHighlighter final : public SyntaxHighlighter {
88
public:
99
CppSyntaxHighlighter() {}
10-
1110
virtual ~CppSyntaxHighlighter() override;
11+
12+
virtual SyntaxLanguage language() const override { return SyntaxLanguage::Cpp; }
1213
virtual void rehighlight() override;
1314
virtual void highlight_matching_token_pair() override;
1415
};

Libraries/LibGUI/SyntaxHighlighter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66

77
namespace GUI {
88

9+
enum class SyntaxLanguage {
10+
PlainText,
11+
Cpp
12+
};
13+
914
class SyntaxHighlighter {
1015
AK_MAKE_NONCOPYABLE(SyntaxHighlighter);
1116
AK_MAKE_NONMOVABLE(SyntaxHighlighter);
1217

1318
public:
1419
virtual ~SyntaxHighlighter();
1520

21+
virtual SyntaxLanguage language() const = 0;
1622
virtual void rehighlight() = 0;
1723
virtual void highlight_matching_token_pair() = 0;
1824

Libraries/LibGUI/TextDocument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct TextDocumentSpan {
4646
Color color;
4747
Optional<Color> background_color;
4848
bool is_skippable { false };
49+
bool is_underlined { false };
4950
const Gfx::Font* font { nullptr };
5051
void* data { nullptr };
5152
};
@@ -87,6 +88,7 @@ class TextDocument : public RefCounted<TextDocument> {
8788
NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
8889

8990
bool has_spans() const { return !m_spans.is_empty(); }
91+
Vector<TextDocumentSpan>& spans() { return m_spans; }
9092
const Vector<TextDocumentSpan>& spans() const { return m_spans; }
9193
void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[index] = move(span); }
9294

Libraries/LibGUI/TextEditor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ void TextEditor::paint_event(PaintEvent& event)
425425
const Gfx::Font* font = &this->font();
426426
Color color;
427427
Optional<Color> background_color;
428+
bool underline = false;
428429
TextPosition physical_position(line_index, start_of_visual_line + i);
429430
// FIXME: This is *horribly* inefficient.
430431
for (auto& span : document().spans()) {
@@ -434,11 +435,15 @@ void TextEditor::paint_event(PaintEvent& event)
434435
if (span.font)
435436
font = span.font;
436437
background_color = span.background_color;
438+
underline = span.is_underlined;
437439
break;
438440
}
439441
if (background_color.has_value())
440442
painter.fill_rect(character_rect, background_color.value());
441443
painter.draw_text(character_rect, visual_line_text.substring_view(i, 1), *font, m_text_alignment, color);
444+
if (underline) {
445+
painter.draw_line(character_rect.bottom_left().translated(0, 1), character_rect.bottom_right().translated(0, 1), color);
446+
}
442447
character_rect.move_by(advance, 0);
443448
}
444449
}
@@ -1490,6 +1495,14 @@ void TextEditor::flush_pending_change_notification_if_needed()
14901495
m_has_pending_change_notification = false;
14911496
}
14921497

1498+
const SyntaxHighlighter* TextEditor::syntax_highlighter() const
1499+
{
1500+
if (m_highlighter)
1501+
return m_highlighter.ptr();
1502+
else
1503+
return nullptr;
1504+
}
1505+
14931506
void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
14941507
{
14951508
if (m_highlighter)

Libraries/LibGUI/TextEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class TextEditor
127127
void set_cursor(size_t line, size_t column);
128128
void set_cursor(const TextPosition&);
129129

130+
const SyntaxHighlighter* syntax_highlighter() const;
130131
void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);
131132

132133
protected:

0 commit comments

Comments
 (0)