mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 13:27:36 +00:00
LibWeb+LibSyntax: Implement nested syntax highlighters
And use them to highlight javascript in HTML source. This commit also changes how TextDocumentSpan::data is interpreted, as it used to be an opaque pointer, but everyone stuffed an enum value inside it, which made the values not unique to each highlighter; that field is now a u64 serial id. The syntax highlighters don't need to change their ways of stuffing token types into that field, but a highlighter that calls another nested highlighter needs to register the nested types for use with token pairs.
This commit is contained in:
parent
3bac14e19e
commit
71b4433b0d
18 changed files with 219 additions and 104 deletions
|
@ -38,13 +38,13 @@ static Syntax::TextStyle style_for_token_type(const Gfx::Palette& palette, JS::T
|
|||
}
|
||||
}
|
||||
|
||||
bool SyntaxHighlighter::is_identifier(void* token) const
|
||||
bool SyntaxHighlighter::is_identifier(u64 token) const
|
||||
{
|
||||
auto js_token = static_cast<JS::TokenType>(reinterpret_cast<size_t>(token));
|
||||
auto js_token = static_cast<JS::TokenType>(static_cast<size_t>(token));
|
||||
return js_token == JS::TokenType::Identifier;
|
||||
}
|
||||
|
||||
bool SyntaxHighlighter::is_navigatable([[maybe_unused]] void* token) const
|
||||
bool SyntaxHighlighter::is_navigatable([[maybe_unused]] u64 token) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ void SyntaxHighlighter::rehighlight(const Palette& palette)
|
|||
span.attributes.color = style.color;
|
||||
span.attributes.bold = style.bold;
|
||||
span.is_skippable = is_trivia;
|
||||
span.data = reinterpret_cast<void*>(static_cast<size_t>(type));
|
||||
span.data = static_cast<u64>(type);
|
||||
spans.append(span);
|
||||
|
||||
dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{}{} @ '{}' {}:{} - {}:{}",
|
||||
|
@ -111,20 +111,20 @@ void SyntaxHighlighter::rehighlight(const Palette& palette)
|
|||
m_client->do_update();
|
||||
}
|
||||
|
||||
Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs() const
|
||||
Vector<Syntax::Highlighter::MatchingTokenPair> SyntaxHighlighter::matching_token_pairs_impl() const
|
||||
{
|
||||
static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
|
||||
if (pairs.is_empty()) {
|
||||
pairs.append({ reinterpret_cast<void*>(JS::TokenType::CurlyOpen), reinterpret_cast<void*>(JS::TokenType::CurlyClose) });
|
||||
pairs.append({ reinterpret_cast<void*>(JS::TokenType::ParenOpen), reinterpret_cast<void*>(JS::TokenType::ParenClose) });
|
||||
pairs.append({ reinterpret_cast<void*>(JS::TokenType::BracketOpen), reinterpret_cast<void*>(JS::TokenType::BracketClose) });
|
||||
pairs.append({ static_cast<u64>(JS::TokenType::CurlyOpen), static_cast<u64>(JS::TokenType::CurlyClose) });
|
||||
pairs.append({ static_cast<u64>(JS::TokenType::ParenOpen), static_cast<u64>(JS::TokenType::ParenClose) });
|
||||
pairs.append({ static_cast<u64>(JS::TokenType::BracketOpen), static_cast<u64>(JS::TokenType::BracketClose) });
|
||||
}
|
||||
return pairs;
|
||||
}
|
||||
|
||||
bool SyntaxHighlighter::token_types_equal(void* token1, void* token2) const
|
||||
bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const
|
||||
{
|
||||
return static_cast<JS::TokenType>(reinterpret_cast<size_t>(token1)) == static_cast<JS::TokenType>(reinterpret_cast<size_t>(token2));
|
||||
return static_cast<JS::TokenType>(token1) == static_cast<JS::TokenType>(token2);
|
||||
}
|
||||
|
||||
SyntaxHighlighter::~SyntaxHighlighter()
|
||||
|
|
|
@ -15,15 +15,15 @@ public:
|
|||
SyntaxHighlighter() { }
|
||||
virtual ~SyntaxHighlighter() override;
|
||||
|
||||
virtual bool is_identifier(void*) const override;
|
||||
virtual bool is_navigatable(void*) const override;
|
||||
virtual bool is_identifier(u64) const override;
|
||||
virtual bool is_navigatable(u64) const override;
|
||||
|
||||
virtual Syntax::Language language() const override { return Syntax::Language::JavaScript; }
|
||||
virtual void rehighlight(const Palette&) override;
|
||||
|
||||
protected:
|
||||
virtual Vector<MatchingTokenPair> matching_token_pairs() const override;
|
||||
virtual bool token_types_equal(void*, void*) const override;
|
||||
virtual Vector<MatchingTokenPair> matching_token_pairs_impl() const override;
|
||||
virtual bool token_types_equal(u64, u64) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue