mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:38:11 +00:00
LibGUI: Add underlines to highlighting
This commit is contained in:
parent
b58893cfe1
commit
6d89f48dd8
6 changed files with 25 additions and 2 deletions
|
@ -56,7 +56,7 @@ void CppSyntaxHighlighter::rehighlight()
|
|||
span.color = style.color;
|
||||
span.font = style.font;
|
||||
span.is_skippable = token.m_type == CppToken::Type::Whitespace;
|
||||
span.data = (void*)token.m_type;
|
||||
span.data = reinterpret_cast<void*>(token.m_type);
|
||||
spans.append(span);
|
||||
}
|
||||
m_editor->document().set_spans(spans);
|
||||
|
|
|
@ -7,8 +7,9 @@ namespace GUI {
|
|||
class CppSyntaxHighlighter final : public SyntaxHighlighter {
|
||||
public:
|
||||
CppSyntaxHighlighter() {}
|
||||
|
||||
virtual ~CppSyntaxHighlighter() override;
|
||||
|
||||
virtual SyntaxLanguage language() const override { return SyntaxLanguage::Cpp; }
|
||||
virtual void rehighlight() override;
|
||||
virtual void highlight_matching_token_pair() override;
|
||||
};
|
||||
|
|
|
@ -6,6 +6,11 @@
|
|||
|
||||
namespace GUI {
|
||||
|
||||
enum class SyntaxLanguage {
|
||||
PlainText,
|
||||
Cpp
|
||||
};
|
||||
|
||||
class SyntaxHighlighter {
|
||||
AK_MAKE_NONCOPYABLE(SyntaxHighlighter);
|
||||
AK_MAKE_NONMOVABLE(SyntaxHighlighter);
|
||||
|
@ -13,6 +18,7 @@ class SyntaxHighlighter {
|
|||
public:
|
||||
virtual ~SyntaxHighlighter();
|
||||
|
||||
virtual SyntaxLanguage language() const = 0;
|
||||
virtual void rehighlight() = 0;
|
||||
virtual void highlight_matching_token_pair() = 0;
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ struct TextDocumentSpan {
|
|||
Color color;
|
||||
Optional<Color> background_color;
|
||||
bool is_skippable { false };
|
||||
bool is_underlined { false };
|
||||
const Gfx::Font* font { nullptr };
|
||||
void* data { nullptr };
|
||||
};
|
||||
|
@ -87,6 +88,7 @@ public:
|
|||
NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
|
||||
|
||||
bool has_spans() const { return !m_spans.is_empty(); }
|
||||
Vector<TextDocumentSpan>& spans() { return m_spans; }
|
||||
const Vector<TextDocumentSpan>& spans() const { return m_spans; }
|
||||
void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[index] = move(span); }
|
||||
|
||||
|
|
|
@ -425,6 +425,7 @@ void TextEditor::paint_event(PaintEvent& event)
|
|||
const Gfx::Font* font = &this->font();
|
||||
Color color;
|
||||
Optional<Color> background_color;
|
||||
bool underline = false;
|
||||
TextPosition physical_position(line_index, start_of_visual_line + i);
|
||||
// FIXME: This is *horribly* inefficient.
|
||||
for (auto& span : document().spans()) {
|
||||
|
@ -434,11 +435,15 @@ void TextEditor::paint_event(PaintEvent& event)
|
|||
if (span.font)
|
||||
font = span.font;
|
||||
background_color = span.background_color;
|
||||
underline = span.is_underlined;
|
||||
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);
|
||||
if (underline) {
|
||||
painter.draw_line(character_rect.bottom_left().translated(0, 1), character_rect.bottom_right().translated(0, 1), color);
|
||||
}
|
||||
character_rect.move_by(advance, 0);
|
||||
}
|
||||
}
|
||||
|
@ -1490,6 +1495,14 @@ void TextEditor::flush_pending_change_notification_if_needed()
|
|||
m_has_pending_change_notification = false;
|
||||
}
|
||||
|
||||
const SyntaxHighlighter* TextEditor::syntax_highlighter() const
|
||||
{
|
||||
if (m_highlighter)
|
||||
return m_highlighter.ptr();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
|
||||
{
|
||||
if (m_highlighter)
|
||||
|
|
|
@ -127,6 +127,7 @@ public:
|
|||
void set_cursor(size_t line, size_t column);
|
||||
void set_cursor(const TextPosition&);
|
||||
|
||||
const SyntaxHighlighter* syntax_highlighter() const;
|
||||
void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);
|
||||
|
||||
protected:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue