From c7736ef41017288b2bb8d41cf51f2b1366ed5a35 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 3 Mar 2023 14:40:58 +0000 Subject: [PATCH] LibWeb: Add some folding regions to HTML syntax highlighter This adds the regions generated from embedded CSS and JS, and also for HTML block comments. The glaring omission is that we don't add them for start/end tags. HTML allows start and end tags to not always match up, and I believe that's going to require some variation on the adoption-agency algorithm to make it work correctly. --- .../HTML/SyntaxHighlighter/SyntaxHighlighter.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp b/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp index feb247cde8..7b94272def 100644 --- a/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp +++ b/Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp @@ -41,6 +41,8 @@ void SyntaxHighlighter::rehighlight(Palette const& palette) auto text = m_client->get_text(); clear_nested_token_pairs(); + // FIXME: Add folding regions for start and end tags. + Vector folding_regions; Vector spans; auto highlight = [&](auto start_line, auto start_column, auto end_line, auto end_column, Gfx::TextAttributes attributes, AugmentedTokenKind kind) { if (start_line > end_line || (start_line == end_line && start_column >= end_column)) { @@ -101,6 +103,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette) } spans.extend(proxy_client.corrected_spans()); + folding_regions.extend(proxy_client.corrected_folding_regions()); substring_builder.clear(); } else if (state == State::CSS) { Syntax::ProxyHighlighterClient proxy_client { @@ -118,6 +121,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette) } spans.extend(proxy_client.corrected_spans()); + folding_regions.extend(proxy_client.corrected_folding_regions()); substring_builder.clear(); } state = State::HTML; @@ -138,6 +142,11 @@ void SyntaxHighlighter::rehighlight(Palette const& palette) token->end_position().column, { palette.syntax_comment(), {} }, AugmentedTokenKind::Comment); + + GUI::TextDocumentFoldingRegion region; + region.range.set_start({ token->start_position().line, token->start_position().column + comment_prefix()->length() }); + region.range.set_end({ token->end_position().line, token->end_position().column - comment_suffix()->length() }); + folding_regions.append(move(region)); } else if (token->is_start_tag() || token->is_end_tag()) { highlight( token->start_position().line, @@ -183,6 +192,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();