mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
LibGUI: Abstract token pair matching
This commit is contained in:
parent
ac524b632f
commit
c02037e944
4 changed files with 124 additions and 105 deletions
|
@ -7,6 +7,98 @@ SyntaxHighlighter::~SyntaxHighlighter()
|
|||
{
|
||||
}
|
||||
|
||||
void SyntaxHighlighter::highlight_matching_token_pair()
|
||||
{
|
||||
ASSERT(m_editor);
|
||||
auto& document = m_editor->document();
|
||||
|
||||
enum class Direction {
|
||||
Forward,
|
||||
Backward,
|
||||
};
|
||||
|
||||
auto find_span_of_type = [&](auto i, void* type, void* not_type, Direction direction) -> Optional<size_t> {
|
||||
size_t nesting_level = 0;
|
||||
bool forward = direction == Direction::Forward;
|
||||
|
||||
if (forward) {
|
||||
++i;
|
||||
if (i >= document.spans().size())
|
||||
return {};
|
||||
} else {
|
||||
if (i == 0)
|
||||
return {};
|
||||
--i;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
auto& span = document.spans().at(i);
|
||||
auto span_token_type = span.data;
|
||||
if (token_types_equal(span_token_type, not_type)) {
|
||||
++nesting_level;
|
||||
} else if (token_types_equal(span_token_type, type)) {
|
||||
if (nesting_level-- <= 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
if (forward) {
|
||||
++i;
|
||||
if (i >= document.spans().size())
|
||||
return {};
|
||||
} else {
|
||||
if (i == 0)
|
||||
return {};
|
||||
--i;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
auto make_buddies = [&](int index0, int index1) {
|
||||
auto& buddy0 = document.spans()[index0];
|
||||
auto& buddy1 = document.spans()[index1];
|
||||
m_has_brace_buddies = true;
|
||||
m_brace_buddies[0].index = index0;
|
||||
m_brace_buddies[1].index = index1;
|
||||
m_brace_buddies[0].span_backup = buddy0;
|
||||
m_brace_buddies[1].span_backup = buddy1;
|
||||
buddy0.background_color = Color::DarkCyan;
|
||||
buddy1.background_color = Color::DarkCyan;
|
||||
buddy0.color = Color::White;
|
||||
buddy1.color = Color::White;
|
||||
m_editor->update();
|
||||
};
|
||||
|
||||
auto pairs = matching_token_pairs();
|
||||
|
||||
for (size_t i = 0; i < document.spans().size(); ++i) {
|
||||
auto& span = const_cast<GUI::TextDocumentSpan&>(document.spans().at(i));
|
||||
auto token_type = span.data;
|
||||
|
||||
for (auto& pair : pairs) {
|
||||
if (token_types_equal(token_type, pair.open) && span.range.start() == m_editor->cursor()) {
|
||||
auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward);
|
||||
if (buddy.has_value())
|
||||
make_buddies(i, buddy.value());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
auto right_of_end = span.range.end();
|
||||
right_of_end.set_column(right_of_end.column() + 1);
|
||||
|
||||
for (auto& pair : pairs) {
|
||||
if (token_types_equal(token_type, pair.close) && right_of_end == m_editor->cursor()) {
|
||||
auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward);
|
||||
if (buddy.has_value())
|
||||
make_buddies(i, buddy.value());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SyntaxHighlighter::attach(TextEditor& editor)
|
||||
{
|
||||
ASSERT(!m_editor);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue