mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:07:35 +00:00
LibJS: Assign folding regions for JS files
Each {} block is now treated as a folding region, so that they can be collapsed and expanded in TextEditor, HackStudio, and anywhere else that uses the syntax highlighter. :^)
This commit is contained in:
parent
cac14a3f0a
commit
0511059d60
1 changed files with 25 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -54,6 +55,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
|
|||
Lexer lexer(text);
|
||||
|
||||
Vector<GUI::TextDocumentSpan> spans;
|
||||
Vector<GUI::TextDocumentFoldingRegion> folding_regions;
|
||||
GUI::TextPosition position { 0, 0 };
|
||||
GUI::TextPosition start { 0, 0 };
|
||||
|
||||
|
@ -92,16 +94,39 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
|
|||
span.range.end().line(), span.range.end().column());
|
||||
};
|
||||
|
||||
struct TokenData {
|
||||
Token token;
|
||||
GUI::TextRange range;
|
||||
};
|
||||
Vector<TokenData> folding_region_start_tokens;
|
||||
|
||||
bool was_eof = false;
|
||||
for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
|
||||
append_token(token.trivia(), token, true);
|
||||
|
||||
auto token_start_position = position;
|
||||
append_token(token.value(), token, false);
|
||||
|
||||
if (token.type() == TokenType::Eof)
|
||||
was_eof = true;
|
||||
|
||||
// Create folding regions for {} blocks
|
||||
if (token.type() == TokenType::CurlyOpen) {
|
||||
folding_region_start_tokens.append({ .token = token,
|
||||
.range = { token_start_position, position } });
|
||||
} else if (token.type() == TokenType::CurlyClose) {
|
||||
if (!folding_region_start_tokens.is_empty()) {
|
||||
auto curly_open = folding_region_start_tokens.take_last();
|
||||
GUI::TextDocumentFoldingRegion region;
|
||||
region.range.set_start(curly_open.range.end());
|
||||
region.range.set_end(token_start_position);
|
||||
folding_regions.append(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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue