2020-03-13 22:07:44 +00:00
|
|
|
/*
|
2022-03-04 20:26:19 +00:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2020-03-13 22:07:44 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-13 22:07:44 +00:00
|
|
|
*/
|
|
|
|
|
2021-02-07 15:56:02 +00:00
|
|
|
#include <LibGfx/Color.h>
|
2021-02-07 14:15:10 +00:00
|
|
|
#include <LibSyntax/Highlighter.h>
|
2020-02-07 19:07:15 +00:00
|
|
|
|
2021-02-07 14:15:10 +00:00
|
|
|
namespace Syntax {
|
2020-02-07 19:07:15 +00:00
|
|
|
|
2021-02-07 14:15:10 +00:00
|
|
|
void Highlighter::highlight_matching_token_pair()
|
2020-03-12 22:51:02 +00:00
|
|
|
{
|
2021-02-07 15:56:02 +00:00
|
|
|
auto& document = m_client->get_document();
|
2020-03-12 22:51:02 +00:00
|
|
|
|
|
|
|
enum class Direction {
|
|
|
|
Forward,
|
|
|
|
Backward,
|
|
|
|
};
|
|
|
|
|
2021-06-07 07:33:09 +00:00
|
|
|
auto find_span_of_type = [&](auto i, u64 type, u64 not_type, Direction direction) -> Optional<size_t> {
|
2020-03-12 22:51:02 +00:00
|
|
|
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;
|
2021-01-02 19:31:45 +00:00
|
|
|
buddy0.attributes.background_color = Color::DarkCyan;
|
|
|
|
buddy1.attributes.background_color = Color::DarkCyan;
|
|
|
|
buddy0.attributes.color = Color::White;
|
|
|
|
buddy1.attributes.color = Color::White;
|
2021-02-07 15:56:02 +00:00
|
|
|
m_client->do_update();
|
2020-03-12 22:51:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto pairs = matching_token_pairs();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < document.spans().size(); ++i) {
|
2024-06-02 17:45:47 +00:00
|
|
|
auto& span = const_cast<TextDocumentSpan&>(document.spans().at(i));
|
2020-03-12 22:51:02 +00:00
|
|
|
auto token_type = span.data;
|
|
|
|
|
|
|
|
for (auto& pair : pairs) {
|
2021-02-07 15:56:02 +00:00
|
|
|
if (token_types_equal(token_type, pair.open) && span.range.start() == m_client->get_cursor()) {
|
2020-03-12 22:51:02 +00:00
|
|
|
auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward);
|
|
|
|
if (buddy.has_value())
|
|
|
|
make_buddies(i, buddy.value());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& pair : pairs) {
|
2021-09-03 01:55:41 +00:00
|
|
|
if (token_types_equal(token_type, pair.close) && span.range.end() == m_client->get_cursor()) {
|
2020-03-12 22:51:02 +00:00
|
|
|
auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward);
|
|
|
|
if (buddy.has_value())
|
|
|
|
make_buddies(i, buddy.value());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 15:56:02 +00:00
|
|
|
void Highlighter::attach(HighlighterClient& client)
|
2020-02-07 19:07:15 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_client);
|
2021-02-07 15:56:02 +00:00
|
|
|
m_client = &client;
|
2020-02-07 19:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 14:15:10 +00:00
|
|
|
void Highlighter::detach()
|
2020-02-07 19:07:15 +00:00
|
|
|
{
|
2021-02-07 15:56:02 +00:00
|
|
|
m_client = nullptr;
|
2020-02-07 19:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 14:15:10 +00:00
|
|
|
void Highlighter::cursor_did_change()
|
2020-02-07 19:07:15 +00:00
|
|
|
{
|
2021-02-07 15:56:02 +00:00
|
|
|
auto& document = m_client->get_document();
|
2020-02-07 19:07:15 +00:00
|
|
|
if (m_has_brace_buddies) {
|
2020-02-25 13:49:47 +00:00
|
|
|
if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < static_cast<int>(document.spans().size()))
|
2020-02-07 19:07:15 +00:00
|
|
|
document.set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
|
2020-02-25 13:49:47 +00:00
|
|
|
if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < static_cast<int>(document.spans().size()))
|
2020-02-07 19:07:15 +00:00
|
|
|
document.set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
|
|
|
|
m_has_brace_buddies = false;
|
2021-02-07 15:56:02 +00:00
|
|
|
m_client->do_update();
|
2020-02-07 19:07:15 +00:00
|
|
|
}
|
|
|
|
highlight_matching_token_pair();
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:33:09 +00:00
|
|
|
Vector<Highlighter::MatchingTokenPair> Highlighter::matching_token_pairs() const
|
|
|
|
{
|
|
|
|
auto own_pairs = matching_token_pairs_impl();
|
|
|
|
own_pairs.ensure_capacity(own_pairs.size() + m_nested_token_pairs.size());
|
|
|
|
for (auto& nested_pair : m_nested_token_pairs)
|
|
|
|
own_pairs.append(nested_pair);
|
|
|
|
return own_pairs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Highlighter::register_nested_token_pairs(Vector<MatchingTokenPair> pairs)
|
|
|
|
{
|
|
|
|
for (auto& pair : pairs)
|
|
|
|
m_nested_token_pairs.set(pair);
|
|
|
|
}
|
|
|
|
|
2020-02-07 19:07:15 +00:00
|
|
|
}
|