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

LibWeb: Add folding regions to CSS syntax highlighter

This commit is contained in:
Sam Atkins 2023-03-03 14:19:03 +00:00 committed by Andreas Kling
parent 947855c23b
commit cdf80f20e6

View file

@ -25,6 +25,8 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "(CSS::SyntaxHighlighter) starting rehighlight");
auto text = m_client->get_text();
Vector<Parser::Token> folding_region_start_tokens;
Vector<GUI::TextDocumentFoldingRegion> folding_regions;
Vector<GUI::TextDocumentSpan> spans;
auto highlight = [&](auto start_line, auto start_column, auto end_line, auto end_column, Gfx::TextAttributes attributes, CSS::Parser::Token::Type type) {
@ -49,6 +51,18 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
if (token.is(Parser::Token::Type::EndOfFile))
break;
if (token.is(Parser::Token::Type::OpenCurly)) {
folding_region_start_tokens.append(token);
} else if (token.is(Parser::Token::Type::CloseCurly)) {
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_position().line, start_token.end_position().column });
folding_region.range.set_end({ token.start_position().line, token.start_position().column });
folding_regions.append(move(folding_region));
}
}
switch (token.type()) {
case Parser::Token::Type::Ident:
highlight(token.start_position().line, token.start_position().column, token.end_position().line, token.end_position().column, { palette.syntax_identifier(), {} }, token.type());
@ -135,6 +149,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
}
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();
m_client->do_update();