ladybird/Libraries/LibGUI/SyntaxHighlighter.cpp
Andreas Kling bb8e65be41 LibGUI+HackStudio: Move syntax highlighting from HackStudio to LibGUI
This patch introduces the GUI::SyntaxHighlighter class, which can be
attached to a GUI::TextEditor to provide syntax highlighting.

The C++ syntax highlighting from HackStudio becomes a new class called
GUI::CppSyntaxHighlighter. This will make it possible to get C++ syntax
highlighting in any app that uses a GUI::TextEditor. :^)

Sidenote: It does feel a bit weird having a C++ lexer in a GUI toolkit
library, and we'll probably end up moving this out to a separate place
as this functionality grows larger.
2020-02-07 20:07:15 +01:00

37 lines
979 B
C++

#include <LibGUI/SyntaxHighlighter.h>
#include <LibGUI/TextEditor.h>
namespace GUI {
SyntaxHighlighter::~SyntaxHighlighter()
{
}
void SyntaxHighlighter::attach(TextEditor& editor)
{
ASSERT(!m_editor);
m_editor = editor.make_weak_ptr();
}
void SyntaxHighlighter::detach()
{
ASSERT(m_editor);
m_editor = nullptr;
}
void SyntaxHighlighter::cursor_did_change()
{
ASSERT(m_editor);
auto& document = m_editor->document();
if (m_has_brace_buddies) {
if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < document.spans().size())
document.set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < document.spans().size())
document.set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
m_has_brace_buddies = false;
m_editor->update();
}
highlight_matching_token_pair();
}
}