1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:28:10 +00:00

LibSyntax+Libraries: Replace TextStyle with Gfx::TextAttributes

Rather than creating a TextStyle struct, and then copying its fields
over to a TextAttributes, let's just create a TextAttributes to start
with. This also simplifies the syntax highlighting code by letting us
define underlines along with the other text styling.
This commit is contained in:
Sam Atkins 2023-03-15 12:49:17 +00:00 committed by Andreas Kling
parent 6d8f046fd0
commit 406a7ea577
10 changed files with 59 additions and 90 deletions

View file

@ -9,7 +9,7 @@
namespace CMake::Cache { namespace CMake::Cache {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token::Type type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, Token::Type type)
{ {
switch (type) { switch (type) {
case Token::Type::Comment: case Token::Type::Comment:
@ -25,7 +25,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case Token::Type::Value: case Token::Type::Value:
return { palette.syntax_string() }; return { palette.syntax_string() };
case Token::Type::Garbage: case Token::Type::Garbage:
return { palette.red() }; return { palette.red(), {}, false, Gfx::TextAttributes::UnderlineStyle::Wavy, palette.red() };
default: default:
return { palette.base_text() }; return { palette.base_text() };
} }
@ -50,13 +50,7 @@ void SyntaxHighlighter::rehighlight(Gfx::Palette const& palette)
if (!span.range.is_valid()) if (!span.range.is_valid())
return; return;
auto style = style_for_token_type(palette, type); span.attributes = style_for_token_type(palette, type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
if (type == Token::Type::Garbage) {
span.attributes.underline_color = palette.red();
span.attributes.underline_style = Gfx::TextAttributes::UnderlineStyle::Wavy;
}
span.is_skippable = false; span.is_skippable = false;
span.data = static_cast<u64>(type); span.data = static_cast<u64>(type);
spans.append(move(span)); spans.append(move(span));

View file

@ -10,7 +10,7 @@
namespace CMake { namespace CMake {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token::Type type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, Token::Type type)
{ {
switch (type) { switch (type) {
case Token::Type::BracketComment: case Token::Type::BracketComment:
@ -30,7 +30,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case Token::Type::UnquotedArgument: case Token::Type::UnquotedArgument:
return { palette.syntax_parameter() }; return { palette.syntax_parameter() };
case Token::Type::Garbage: case Token::Type::Garbage:
return { palette.red() }; return { palette.red(), {}, false, Gfx::TextAttributes::UnderlineStyle::Wavy, palette.red() };
case Token::Type::VariableReference: case Token::Type::VariableReference:
// This is a bit arbitrary, since we don't have a color specifically for this. // This is a bit arbitrary, since we don't have a color specifically for this.
return { palette.syntax_preprocessor_value() }; return { palette.syntax_preprocessor_value() };
@ -66,13 +66,7 @@ void SyntaxHighlighter::rehighlight(Gfx::Palette const& palette)
if (!span.range.is_valid()) if (!span.range.is_valid())
return; return;
auto style = style_for_token_type(palette, type); span.attributes = style_for_token_type(palette, type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
if (type == Token::Type::Garbage) {
span.attributes.underline_color = palette.red();
span.attributes.underline_style = Gfx::TextAttributes::UnderlineStyle::Wavy;
}
span.is_skippable = false; span.is_skippable = false;
span.data = static_cast<u64>(type); span.data = static_cast<u64>(type);
spans.append(move(span)); spans.append(move(span));

View file

@ -85,44 +85,44 @@ void SemanticSyntaxHighlighter::rehighlight(Palette const& palette)
update_spans(new_tokens_info, palette); update_spans(new_tokens_info, palette);
} }
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, CodeComprehension::TokenInfo::SemanticType type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, CodeComprehension::TokenInfo::SemanticType type)
{ {
switch (type) { switch (type) {
case CodeComprehension::TokenInfo::SemanticType::Unknown: case CodeComprehension::TokenInfo::SemanticType::Unknown:
return { palette.base_text(), false }; return { palette.base_text() };
case CodeComprehension::TokenInfo::SemanticType::Keyword: case CodeComprehension::TokenInfo::SemanticType::Keyword:
return { palette.syntax_keyword(), true }; return { palette.syntax_keyword(), {}, true };
case CodeComprehension::TokenInfo::SemanticType::Type: case CodeComprehension::TokenInfo::SemanticType::Type:
return { palette.syntax_type(), true }; return { palette.syntax_type(), {}, true };
case CodeComprehension::TokenInfo::SemanticType::Identifier: case CodeComprehension::TokenInfo::SemanticType::Identifier:
return { palette.syntax_identifier(), false }; return { palette.syntax_identifier() };
case CodeComprehension::TokenInfo::SemanticType::String: case CodeComprehension::TokenInfo::SemanticType::String:
return { palette.syntax_string(), false }; return { palette.syntax_string() };
case CodeComprehension::TokenInfo::SemanticType::Number: case CodeComprehension::TokenInfo::SemanticType::Number:
return { palette.syntax_number(), false }; return { palette.syntax_number() };
case CodeComprehension::TokenInfo::SemanticType::IncludePath: case CodeComprehension::TokenInfo::SemanticType::IncludePath:
return { palette.syntax_preprocessor_value(), false }; return { palette.syntax_preprocessor_value() };
case CodeComprehension::TokenInfo::SemanticType::PreprocessorStatement: case CodeComprehension::TokenInfo::SemanticType::PreprocessorStatement:
return { palette.syntax_preprocessor_statement(), false }; return { palette.syntax_preprocessor_statement() };
case CodeComprehension::TokenInfo::SemanticType::Comment: case CodeComprehension::TokenInfo::SemanticType::Comment:
return { palette.syntax_comment(), false }; return { palette.syntax_comment() };
case CodeComprehension::TokenInfo::SemanticType::Function: case CodeComprehension::TokenInfo::SemanticType::Function:
return { palette.syntax_function(), false }; return { palette.syntax_function() };
case CodeComprehension::TokenInfo::SemanticType::Variable: case CodeComprehension::TokenInfo::SemanticType::Variable:
return { palette.syntax_variable(), false }; return { palette.syntax_variable() };
case CodeComprehension::TokenInfo::SemanticType::CustomType: case CodeComprehension::TokenInfo::SemanticType::CustomType:
return { palette.syntax_custom_type(), false }; return { palette.syntax_custom_type() };
case CodeComprehension::TokenInfo::SemanticType::Namespace: case CodeComprehension::TokenInfo::SemanticType::Namespace:
return { palette.syntax_namespace(), false }; return { palette.syntax_namespace() };
case CodeComprehension::TokenInfo::SemanticType::Member: case CodeComprehension::TokenInfo::SemanticType::Member:
return { palette.syntax_member(), false }; return { palette.syntax_member() };
case CodeComprehension::TokenInfo::SemanticType::Parameter: case CodeComprehension::TokenInfo::SemanticType::Parameter:
return { palette.syntax_parameter(), false }; return { palette.syntax_parameter() };
case CodeComprehension::TokenInfo::SemanticType::PreprocessorMacro: case CodeComprehension::TokenInfo::SemanticType::PreprocessorMacro:
return { palette.syntax_preprocessor_value(), false }; return { palette.syntax_preprocessor_value() };
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
return { palette.base_text(), false }; return { palette.base_text() };
} }
} }
void SemanticSyntaxHighlighter::update_spans(Vector<CodeComprehension::TokenInfo> const& tokens_info, Gfx::Palette const& palette) void SemanticSyntaxHighlighter::update_spans(Vector<CodeComprehension::TokenInfo> const& tokens_info, Gfx::Palette const& palette)
@ -133,9 +133,7 @@ void SemanticSyntaxHighlighter::update_spans(Vector<CodeComprehension::TokenInfo
GUI::TextDocumentSpan span; GUI::TextDocumentSpan span;
span.range.set_start({ token.start_line, token.start_column }); span.range.set_start({ token.start_line, token.start_column });
span.range.set_end({ token.end_line, token.end_column + 1 }); span.range.set_end({ token.end_line, token.end_column + 1 });
auto style = style_for_token_type(palette, token.type); span.attributes = style_for_token_type(palette, token.type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.is_skippable = token.type == CodeComprehension::TokenInfo::SemanticType::Whitespace; span.is_skippable = token.type == CodeComprehension::TokenInfo::SemanticType::Whitespace;
span.data = static_cast<u64>(token.type); span.data = static_cast<u64>(token.type);
spans.append(span); spans.append(span);

View file

@ -13,33 +13,33 @@
namespace Cpp { namespace Cpp {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Cpp::Token::Type type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, Cpp::Token::Type type)
{ {
switch (type) { switch (type) {
case Cpp::Token::Type::Keyword: case Cpp::Token::Type::Keyword:
return { palette.syntax_keyword(), true }; return { palette.syntax_keyword(), {}, true };
case Cpp::Token::Type::KnownType: case Cpp::Token::Type::KnownType:
return { palette.syntax_type(), true }; return { palette.syntax_type(), {}, true };
case Cpp::Token::Type::Identifier: case Cpp::Token::Type::Identifier:
return { palette.syntax_identifier(), false }; return { palette.syntax_identifier() };
case Cpp::Token::Type::DoubleQuotedString: case Cpp::Token::Type::DoubleQuotedString:
case Cpp::Token::Type::SingleQuotedString: case Cpp::Token::Type::SingleQuotedString:
case Cpp::Token::Type::RawString: case Cpp::Token::Type::RawString:
return { palette.syntax_string(), false }; return { palette.syntax_string() };
case Cpp::Token::Type::Integer: case Cpp::Token::Type::Integer:
case Cpp::Token::Type::Float: case Cpp::Token::Type::Float:
return { palette.syntax_number(), false }; return { palette.syntax_number() };
case Cpp::Token::Type::IncludePath: case Cpp::Token::Type::IncludePath:
return { palette.syntax_preprocessor_value(), false }; return { palette.syntax_preprocessor_value() };
case Cpp::Token::Type::EscapeSequence: case Cpp::Token::Type::EscapeSequence:
return { palette.syntax_keyword(), true }; return { palette.syntax_keyword(), {}, true };
case Cpp::Token::Type::PreprocessorStatement: case Cpp::Token::Type::PreprocessorStatement:
case Cpp::Token::Type::IncludeStatement: case Cpp::Token::Type::IncludeStatement:
return { palette.syntax_preprocessor_statement(), false }; return { palette.syntax_preprocessor_statement() };
case Cpp::Token::Type::Comment: case Cpp::Token::Type::Comment:
return { palette.syntax_comment(), false }; return { palette.syntax_comment() };
default: default:
return { palette.base_text(), false }; return { palette.base_text() };
} }
} }
@ -69,9 +69,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span; GUI::TextDocumentSpan span;
span.range.set_start({ token.start().line, token.start().column }); span.range.set_start({ token.start().line, token.start().column });
span.range.set_end({ token.end().line, token.end().column + 1 }); span.range.set_end({ token.end().line, token.end().column + 1 });
auto style = style_for_token_type(palette, token.type()); span.attributes = style_for_token_type(palette, token.type());
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.is_skippable = token.type() == Cpp::Token::Type::Whitespace; span.is_skippable = token.type() == Cpp::Token::Type::Whitespace;
span.data = static_cast<u64>(token.type()); span.data = static_cast<u64>(token.type());
spans.append(span); spans.append(span);

View file

@ -12,7 +12,7 @@
namespace GUI::GML { namespace GUI::GML {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token::Type type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, Token::Type type)
{ {
switch (type) { switch (type) {
case Token::Type::LeftCurly: case Token::Type::LeftCurly:
@ -21,7 +21,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case Token::Type::ClassMarker: case Token::Type::ClassMarker:
return { palette.syntax_keyword() }; return { palette.syntax_keyword() };
case Token::Type::ClassName: case Token::Type::ClassName:
return { palette.syntax_identifier(), true }; return { palette.syntax_identifier(), {}, true };
case Token::Type::Identifier: case Token::Type::Identifier:
return { palette.syntax_identifier() }; return { palette.syntax_identifier() };
case Token::Type::JsonValue: case Token::Type::JsonValue:
@ -54,9 +54,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span; GUI::TextDocumentSpan span;
span.range.set_start({ token.m_start.line, token.m_start.column }); span.range.set_start({ token.m_start.line, token.m_start.column });
span.range.set_end({ token.m_end.line, token.m_end.column }); span.range.set_end({ token.m_end.line, token.m_end.column });
auto style = style_for_token_type(palette, token.m_type); span.attributes = style_for_token_type(palette, token.m_type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.is_skippable = false; span.is_skippable = false;
span.data = static_cast<u64>(token.m_type); span.data = static_cast<u64>(token.m_type);
spans.append(span); spans.append(span);

View file

@ -10,7 +10,7 @@
#include <LibGfx/Palette.h> #include <LibGfx/Palette.h>
namespace GUI { namespace GUI {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, GitCommitToken::Type type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, GitCommitToken::Type type)
{ {
switch (type) { switch (type) {
case GitCommitToken::Type::Comment: case GitCommitToken::Type::Comment:
@ -31,9 +31,7 @@ void GitCommitSyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span; GUI::TextDocumentSpan span;
span.range.set_start({ token.m_start.line, token.m_start.column }); span.range.set_start({ token.m_start.line, token.m_start.column });
span.range.set_end({ token.m_end.line, token.m_end.column }); span.range.set_end({ token.m_end.line, token.m_end.column });
auto style = style_for_token_type(palette, token.m_type); span.attributes = style_for_token_type(palette, token.m_type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.is_skippable = false; span.is_skippable = false;
span.data = static_cast<u64>(token.m_type); span.data = static_cast<u64>(token.m_type);
spans.append(span); spans.append(span);

View file

@ -11,13 +11,13 @@
namespace GUI { namespace GUI {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, IniToken::Type type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, IniToken::Type type)
{ {
switch (type) { switch (type) {
case IniToken::Type::LeftBracket: case IniToken::Type::LeftBracket:
case IniToken::Type::RightBracket: case IniToken::Type::RightBracket:
case IniToken::Type::Section: case IniToken::Type::Section:
return { palette.syntax_keyword(), true }; return { palette.syntax_keyword(), {}, true };
case IniToken::Type::Name: case IniToken::Type::Name:
return { palette.syntax_identifier() }; return { palette.syntax_identifier() };
case IniToken::Type::Value: case IniToken::Type::Value:
@ -25,7 +25,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, IniTo
case IniToken::Type::Comment: case IniToken::Type::Comment:
return { palette.syntax_comment() }; return { palette.syntax_comment() };
case IniToken::Type::Equal: case IniToken::Type::Equal:
return { palette.syntax_operator(), true }; return { palette.syntax_operator(), {}, true };
default: default:
return { palette.base_text() }; return { palette.base_text() };
} }
@ -52,9 +52,7 @@ void IniSyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span; GUI::TextDocumentSpan span;
span.range.set_start({ token.m_start.line, token.m_start.column }); span.range.set_start({ token.m_start.line, token.m_start.column });
span.range.set_end({ token.m_end.line, token.m_end.column }); span.range.set_end({ token.m_end.line, token.m_end.column });
auto style = style_for_token_type(palette, token.m_type); span.attributes = style_for_token_type(palette, token.m_type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.is_skippable = token.m_type == IniToken::Type::Whitespace; span.is_skippable = token.m_type == IniToken::Type::Whitespace;
span.data = static_cast<u64>(token.m_type); span.data = static_cast<u64>(token.m_type);
spans.append(span); spans.append(span);

View file

@ -13,7 +13,7 @@
namespace JS { namespace JS {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, TokenType type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, TokenType type)
{ {
switch (Token::category(type)) { switch (Token::category(type)) {
case TokenCategory::Invalid: case TokenCategory::Invalid:
@ -27,9 +27,9 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case TokenCategory::Operator: case TokenCategory::Operator:
return { palette.syntax_operator() }; return { palette.syntax_operator() };
case TokenCategory::Keyword: case TokenCategory::Keyword:
return { palette.syntax_keyword(), true }; return { palette.syntax_keyword(), {}, true };
case TokenCategory::ControlKeyword: case TokenCategory::ControlKeyword:
return { palette.syntax_control_keyword(), true }; return { palette.syntax_control_keyword(), {}, true };
case TokenCategory::Identifier: case TokenCategory::Identifier:
return { palette.syntax_identifier() }; return { palette.syntax_identifier() };
default: default:
@ -79,9 +79,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
span.range.set_start(start); span.range.set_start(start);
span.range.set_end({ position.line(), position.column() }); span.range.set_end({ position.line(), position.column() });
auto type = is_trivia ? TokenType::Invalid : token.type(); auto type = is_trivia ? TokenType::Invalid : token.type();
auto style = style_for_token_type(palette, type); span.attributes = style_for_token_type(palette, type);
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.is_skippable = is_trivia; span.is_skippable = is_trivia;
span.data = static_cast<u64>(type); span.data = static_cast<u64>(type);
spans.append(span); spans.append(span);

View file

@ -12,25 +12,25 @@
namespace SQL::AST { namespace SQL::AST {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, TokenType type) static Gfx::TextAttributes style_for_token_type(Gfx::Palette const& palette, TokenType type)
{ {
switch (Token::category(type)) { switch (Token::category(type)) {
case TokenCategory::Keyword: case TokenCategory::Keyword:
return { palette.syntax_keyword(), true }; return { palette.syntax_keyword(), {}, true };
case TokenCategory::Identifier: case TokenCategory::Identifier:
return { palette.syntax_identifier(), false }; return { palette.syntax_identifier() };
case TokenCategory::Number: case TokenCategory::Number:
return { palette.syntax_number(), false }; return { palette.syntax_number() };
case TokenCategory::Blob: case TokenCategory::Blob:
case TokenCategory::String: case TokenCategory::String:
return { palette.syntax_string(), false }; return { palette.syntax_string() };
case TokenCategory::Operator: case TokenCategory::Operator:
return { palette.syntax_operator(), false }; return { palette.syntax_operator() };
case TokenCategory::Punctuation: case TokenCategory::Punctuation:
return { palette.syntax_punctuation(), false }; return { palette.syntax_punctuation() };
case TokenCategory::Invalid: case TokenCategory::Invalid:
default: default:
return { palette.base_text(), false }; return { palette.base_text() };
} }
} }
@ -54,9 +54,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span; GUI::TextDocumentSpan span;
span.range.set_start({ token.start_position().line - 1, token.start_position().column - 1 }); span.range.set_start({ token.start_position().line - 1, token.start_position().column - 1 });
span.range.set_end({ token.end_position().line - 1, token.end_position().column - 1 }); span.range.set_end({ token.end_position().line - 1, token.end_position().column - 1 });
auto style = style_for_token_type(palette, token.type()); span.attributes = style_for_token_type(palette, token.type());
span.attributes.color = style.color;
span.attributes.bold = style.bold;
span.data = static_cast<u64>(token.type()); span.data = static_cast<u64>(token.type());
spans.append(span); spans.append(span);

View file

@ -15,11 +15,6 @@
namespace Syntax { namespace Syntax {
struct TextStyle {
const Gfx::Color color;
bool const bold { false };
};
class Highlighter { class Highlighter {
AK_MAKE_NONCOPYABLE(Highlighter); AK_MAKE_NONCOPYABLE(Highlighter);
AK_MAKE_NONMOVABLE(Highlighter); AK_MAKE_NONMOVABLE(Highlighter);