2020-03-13 22:07:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2023-02-23 15:35:57 +00:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-03-13 22:07:44 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-13 22:07:44 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-16 14:51:56 +00:00
|
|
|
#include <AK/Debug.h>
|
2020-03-15 23:05:06 +00:00
|
|
|
#include <LibGfx/Palette.h>
|
2020-03-12 22:52:03 +00:00
|
|
|
#include <LibJS/Lexer.h>
|
2021-02-07 15:56:02 +00:00
|
|
|
#include <LibJS/SyntaxHighlighter.h>
|
2020-03-12 22:52:03 +00:00
|
|
|
#include <LibJS/Token.h>
|
|
|
|
|
2021-02-07 15:56:02 +00:00
|
|
|
namespace JS {
|
2020-03-12 22:52:03 +00:00
|
|
|
|
2023-03-15 12:49:17 +00:00
|
|
|
static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, TokenType type)
|
2020-03-12 22:52:03 +00:00
|
|
|
{
|
2022-04-03 14:19:33 +00:00
|
|
|
switch (Token::category(type)) {
|
|
|
|
case TokenCategory::Invalid:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.syntax_comment() };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::Number:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.syntax_number() };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::String:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.syntax_string() };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::Punctuation:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.syntax_punctuation() };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::Operator:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.syntax_operator() };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::Keyword:
|
2023-03-15 12:49:17 +00:00
|
|
|
return { palette.syntax_keyword(), {}, true };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::ControlKeyword:
|
2023-03-15 12:49:17 +00:00
|
|
|
return { palette.syntax_control_keyword(), {}, true };
|
2022-04-03 14:19:33 +00:00
|
|
|
case TokenCategory::Identifier:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.syntax_identifier() };
|
2020-03-12 22:52:03 +00:00
|
|
|
default:
|
2020-03-15 23:05:06 +00:00
|
|
|
return { palette.base_text() };
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:33:09 +00:00
|
|
|
bool SyntaxHighlighter::is_identifier(u64 token) const
|
2020-03-12 22:52:03 +00:00
|
|
|
{
|
2022-04-03 14:19:33 +00:00
|
|
|
auto js_token = static_cast<TokenType>(static_cast<size_t>(token));
|
|
|
|
return js_token == TokenType::Identifier;
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 07:33:09 +00:00
|
|
|
bool SyntaxHighlighter::is_navigatable([[maybe_unused]] u64 token) const
|
2020-03-12 22:52:03 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void SyntaxHighlighter::rehighlight(Palette const& palette)
|
2020-03-12 22:52:03 +00:00
|
|
|
{
|
2021-02-07 15:56:02 +00:00
|
|
|
auto text = m_client->get_text();
|
2020-03-12 22:52:03 +00:00
|
|
|
|
2022-04-03 14:19:33 +00:00
|
|
|
Lexer lexer(text);
|
2020-03-12 22:52:03 +00:00
|
|
|
|
2023-08-29 09:13:41 +00:00
|
|
|
Vector<Syntax::TextDocumentSpan> spans;
|
|
|
|
Vector<Syntax::TextDocumentFoldingRegion> folding_regions;
|
|
|
|
Syntax::TextPosition position { 0, 0 };
|
|
|
|
Syntax::TextPosition start { 0, 0 };
|
2020-03-12 22:52:03 +00:00
|
|
|
|
2020-03-15 23:05:06 +00:00
|
|
|
auto advance_position = [&position](char ch) {
|
2020-03-12 22:52:03 +00:00
|
|
|
if (ch == '\n') {
|
|
|
|
position.set_line(position.line() + 1);
|
|
|
|
position.set_column(0);
|
|
|
|
} else
|
|
|
|
position.set_column(position.column() + 1);
|
|
|
|
};
|
|
|
|
|
2022-04-03 14:19:33 +00:00
|
|
|
auto append_token = [&](StringView str, Token const& token, bool is_trivia) {
|
2020-03-15 23:05:06 +00:00
|
|
|
if (str.is_empty())
|
|
|
|
return;
|
2020-03-12 22:52:03 +00:00
|
|
|
|
|
|
|
start = position;
|
2021-06-03 21:55:16 +00:00
|
|
|
for (size_t i = 0; i < str.length(); ++i)
|
2020-03-15 23:05:06 +00:00
|
|
|
advance_position(str[i]);
|
|
|
|
|
2023-08-29 09:13:41 +00:00
|
|
|
Syntax::TextDocumentSpan span;
|
2020-03-15 23:05:06 +00:00
|
|
|
span.range.set_start(start);
|
|
|
|
span.range.set_end({ position.line(), position.column() });
|
2022-04-03 14:19:33 +00:00
|
|
|
auto type = is_trivia ? TokenType::Invalid : token.type();
|
2023-03-15 12:49:17 +00:00
|
|
|
span.attributes = style_for_token_type(palette, type);
|
2020-03-15 23:05:06 +00:00
|
|
|
span.is_skippable = is_trivia;
|
2021-06-07 07:33:09 +00:00
|
|
|
span.data = static_cast<u64>(type);
|
2020-03-15 23:05:06 +00:00
|
|
|
spans.append(span);
|
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{}{} @ '{}' {}:{} - {}:{}",
|
2021-01-16 14:51:56 +00:00
|
|
|
token.name(),
|
|
|
|
is_trivia ? " (trivia)" : "",
|
|
|
|
token.value(),
|
|
|
|
span.range.start().line(), span.range.start().column(),
|
|
|
|
span.range.end().line(), span.range.end().column());
|
2020-03-15 23:05:06 +00:00
|
|
|
};
|
|
|
|
|
2023-02-23 15:35:57 +00:00
|
|
|
struct TokenData {
|
|
|
|
Token token;
|
2023-08-29 09:13:41 +00:00
|
|
|
Syntax::TextRange range;
|
2023-02-23 15:35:57 +00:00
|
|
|
};
|
|
|
|
Vector<TokenData> folding_region_start_tokens;
|
|
|
|
|
2020-03-15 23:05:06 +00:00
|
|
|
bool was_eof = false;
|
|
|
|
for (auto token = lexer.next(); !was_eof; token = lexer.next()) {
|
|
|
|
append_token(token.trivia(), token, true);
|
2023-02-23 15:35:57 +00:00
|
|
|
|
|
|
|
auto token_start_position = position;
|
2020-03-15 23:05:06 +00:00
|
|
|
append_token(token.value(), token, false);
|
2020-03-12 22:52:03 +00:00
|
|
|
|
2022-04-03 14:19:33 +00:00
|
|
|
if (token.type() == TokenType::Eof)
|
2020-03-12 22:52:03 +00:00
|
|
|
was_eof = true;
|
2023-02-23 15:35:57 +00:00
|
|
|
|
|
|
|
// 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();
|
2023-08-29 09:13:41 +00:00
|
|
|
Syntax::TextDocumentFoldingRegion region;
|
2023-02-23 15:35:57 +00:00
|
|
|
region.range.set_start(curly_open.range.end());
|
|
|
|
region.range.set_end(token_start_position);
|
|
|
|
folding_regions.append(region);
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 15:56:02 +00:00
|
|
|
m_client->do_set_spans(move(spans));
|
2023-02-23 15:35:57 +00:00
|
|
|
m_client->do_set_folding_regions(move(folding_regions));
|
2020-03-12 22:52:03 +00:00
|
|
|
|
|
|
|
m_has_brace_buddies = false;
|
|
|
|
highlight_matching_token_pair();
|
|
|
|
|
2021-02-07 15:56:02 +00:00
|
|
|
m_client->do_update();
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 07:33:09 +00:00
|
|
|
Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
|
2020-03-12 22:52:03 +00:00
|
|
|
{
|
2021-02-07 14:15:10 +00:00
|
|
|
static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
|
2020-03-12 22:52:03 +00:00
|
|
|
if (pairs.is_empty()) {
|
2022-04-03 14:19:33 +00:00
|
|
|
pairs.append({ static_cast<u64>(TokenType::CurlyOpen), static_cast<u64>(TokenType::CurlyClose) });
|
|
|
|
pairs.append({ static_cast<u64>(TokenType::ParenOpen), static_cast<u64>(TokenType::ParenClose) });
|
|
|
|
pairs.append({ static_cast<u64>(TokenType::BracketOpen), static_cast<u64>(TokenType::BracketClose) });
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|
|
|
|
return pairs;
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:33:09 +00:00
|
|
|
bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
|
2020-03-12 22:52:03 +00:00
|
|
|
{
|
2022-04-03 14:19:33 +00:00
|
|
|
return static_cast<TokenType>(token1) == static_cast<TokenType>(token2);
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|
2022-04-03 14:19:33 +00:00
|
|
|
|
2020-03-12 22:52:03 +00:00
|
|
|
}
|