summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorOriko <oriko1010@protonmail.com>2020-03-12 16:36:25 +0200
committerAndreas Kling <kling@serenityos.org>2020-03-12 19:04:59 +0100
commit6d89f48dd8c0a57e241a69a5db3a7ceeed8ce108 (patch)
treecfd783e01c547cb45fae38ea1be4679cc785f2d7 /Libraries
parentb58893cfe130feb72210f8d65d6162aac592ce1a (diff)
downloadserenity-6d89f48dd8c0a57e241a69a5db3a7ceeed8ce108.zip
LibGUI: Add underlines to highlighting
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibGUI/CppSyntaxHighlighter.cpp2
-rw-r--r--Libraries/LibGUI/CppSyntaxHighlighter.h3
-rw-r--r--Libraries/LibGUI/SyntaxHighlighter.h6
-rw-r--r--Libraries/LibGUI/TextDocument.h2
-rw-r--r--Libraries/LibGUI/TextEditor.cpp13
-rw-r--r--Libraries/LibGUI/TextEditor.h1
6 files changed, 25 insertions, 2 deletions
diff --git a/Libraries/LibGUI/CppSyntaxHighlighter.cpp b/Libraries/LibGUI/CppSyntaxHighlighter.cpp
index c4a200f270..76561339e0 100644
--- a/Libraries/LibGUI/CppSyntaxHighlighter.cpp
+++ b/Libraries/LibGUI/CppSyntaxHighlighter.cpp
@@ -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);
diff --git a/Libraries/LibGUI/CppSyntaxHighlighter.h b/Libraries/LibGUI/CppSyntaxHighlighter.h
index 44d048ea8f..0f77c88cd7 100644
--- a/Libraries/LibGUI/CppSyntaxHighlighter.h
+++ b/Libraries/LibGUI/CppSyntaxHighlighter.h
@@ -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;
};
diff --git a/Libraries/LibGUI/SyntaxHighlighter.h b/Libraries/LibGUI/SyntaxHighlighter.h
index f4a31ca6e7..568dd403d4 100644
--- a/Libraries/LibGUI/SyntaxHighlighter.h
+++ b/Libraries/LibGUI/SyntaxHighlighter.h
@@ -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;
diff --git a/Libraries/LibGUI/TextDocument.h b/Libraries/LibGUI/TextDocument.h
index 76ce03d23a..fb73bc0345 100644
--- a/Libraries/LibGUI/TextDocument.h
+++ b/Libraries/LibGUI/TextDocument.h
@@ -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); }
diff --git a/Libraries/LibGUI/TextEditor.cpp b/Libraries/LibGUI/TextEditor.cpp
index 3920195d0f..78bb871bce 100644
--- a/Libraries/LibGUI/TextEditor.cpp
+++ b/Libraries/LibGUI/TextEditor.cpp
@@ -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)
diff --git a/Libraries/LibGUI/TextEditor.h b/Libraries/LibGUI/TextEditor.h
index 35c1187f82..8aec1a66b7 100644
--- a/Libraries/LibGUI/TextEditor.h
+++ b/Libraries/LibGUI/TextEditor.h
@@ -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: