1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

LibCpp: Add folding regions to syntax highlighters

This just looks at {} blocks. Unfortunately the two highlighters work
differently enough that the code has to be duplicated.
This commit is contained in:
Sam Atkins 2023-03-03 12:53:08 +00:00 committed by Andreas Kling
parent 1c0c59eabe
commit 947855c23b
2 changed files with 33 additions and 0 deletions

View file

@ -23,6 +23,24 @@ void SemanticSyntaxHighlighter::rehighlight(Palette const& palette)
lexer.set_ignore_whitespace(true);
auto current_tokens = lexer.lex();
// Identify folding regions
Vector<Token> folding_region_start_tokens;
Vector<GUI::TextDocumentFoldingRegion> folding_regions;
for (auto& token : current_tokens) {
if (token.type() == Token::Type::LeftCurly) {
folding_region_start_tokens.append(token);
} else if (token.type() == Token::Type::RightCurly) {
if (!folding_region_start_tokens.is_empty()) {
auto start_token = folding_region_start_tokens.take_last();
GUI::TextDocumentFoldingRegion folding_region;
folding_region.range.set_start({ start_token.end().line, start_token.end().column });
folding_region.range.set_end({ token.start().line, token.start().column });
folding_regions.append(move(folding_region));
}
}
}
m_client->do_set_folding_regions(move(folding_regions));
StringBuilder current_tokens_as_lines;
StringBuilder previous_tokens_as_lines;

View file

@ -60,6 +60,8 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
auto text = m_client->get_text();
Cpp::Lexer lexer(text);
Vector<Token> folding_region_start_tokens;
Vector<GUI::TextDocumentFoldingRegion> folding_regions;
Vector<GUI::TextDocumentSpan> spans;
lexer.lex_iterable([&](auto token) {
// FIXME: The +1 for the token end column is a quick hack due to not wanting to modify the lexer (which is also used by the parser). Maybe there's a better way to do this.
@ -73,8 +75,21 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
span.is_skippable = token.type() == Cpp::Token::Type::Whitespace;
span.data = static_cast<u64>(token.type());
spans.append(span);
if (token.type() == Token::Type::LeftCurly) {
folding_region_start_tokens.append(token);
} else if (token.type() == Token::Type::RightCurly) {
if (!folding_region_start_tokens.is_empty()) {
auto start_token = folding_region_start_tokens.take_last();
GUI::TextDocumentFoldingRegion folding_region;
folding_region.range.set_start({ start_token.end().line, start_token.end().column });
folding_region.range.set_end({ token.start().line, token.start().column });
folding_regions.append(move(folding_region));
}
}
});
m_client->do_set_spans(move(spans));
m_client->do_set_folding_regions(move(folding_regions));
m_has_brace_buddies = false;
highlight_matching_token_pair();