Parcourir la source

LibWeb: Add folding regions to CSS syntax highlighter

Sam Atkins il y a 2 ans
Parent
commit
cdf80f20e6

+ 15 - 0
Userland/Libraries/LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.cpp

@@ -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();