summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-03-15 12:49:17 +0000
committerAndreas Kling <kling@serenityos.org>2023-03-15 14:55:49 +0100
commit406a7ea577f5044e39afd2911de190609b322d23 (patch)
tree3fbfd8285a553b6a7cdce2ab08006aec0eeefef1 /Userland
parent6d8f046fd047bca5f476cb48ec42fa9ce4e1d11f (diff)
downloadserenity-406a7ea577f5044e39afd2911de190609b322d23.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp12
-rw-r--r--Userland/Libraries/LibCMake/SyntaxHighlighter.cpp12
-rw-r--r--Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp40
-rw-r--r--Userland/Libraries/LibCpp/SyntaxHighlighter.cpp26
-rw-r--r--Userland/Libraries/LibGUI/GML/SyntaxHighlighter.cpp8
-rw-r--r--Userland/Libraries/LibGUI/GitCommitSyntaxHighlighter.cpp6
-rw-r--r--Userland/Libraries/LibGUI/INISyntaxHighlighter.cpp10
-rw-r--r--Userland/Libraries/LibJS/SyntaxHighlighter.cpp10
-rw-r--r--Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp20
-rw-r--r--Userland/Libraries/LibSyntax/Highlighter.h5
10 files changed, 59 insertions, 90 deletions
diff --git a/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp b/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp
index f57a209269..ad83a04091 100644
--- a/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp
@@ -9,7 +9,7 @@
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) {
case Token::Type::Comment:
@@ -25,7 +25,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case Token::Type::Value:
return { palette.syntax_string() };
case Token::Type::Garbage:
- return { palette.red() };
+ return { palette.red(), {}, false, Gfx::TextAttributes::UnderlineStyle::Wavy, palette.red() };
default:
return { palette.base_text() };
}
@@ -50,13 +50,7 @@ void SyntaxHighlighter::rehighlight(Gfx::Palette const& palette)
if (!span.range.is_valid())
return;
- auto style = 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.attributes = style_for_token_type(palette, type);
span.is_skippable = false;
span.data = static_cast<u64>(type);
spans.append(move(span));
diff --git a/Userland/Libraries/LibCMake/SyntaxHighlighter.cpp b/Userland/Libraries/LibCMake/SyntaxHighlighter.cpp
index 672e3ff252..6873657f28 100644
--- a/Userland/Libraries/LibCMake/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibCMake/SyntaxHighlighter.cpp
@@ -10,7 +10,7 @@
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) {
case Token::Type::BracketComment:
@@ -30,7 +30,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case Token::Type::UnquotedArgument:
return { palette.syntax_parameter() };
case Token::Type::Garbage:
- return { palette.red() };
+ return { palette.red(), {}, false, Gfx::TextAttributes::UnderlineStyle::Wavy, palette.red() };
case Token::Type::VariableReference:
// This is a bit arbitrary, since we don't have a color specifically for this.
return { palette.syntax_preprocessor_value() };
@@ -66,13 +66,7 @@ void SyntaxHighlighter::rehighlight(Gfx::Palette const& palette)
if (!span.range.is_valid())
return;
- auto style = 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.attributes = style_for_token_type(palette, type);
span.is_skippable = false;
span.data = static_cast<u64>(type);
spans.append(move(span));
diff --git a/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp b/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp
index 017f91c60e..2d147d3afb 100644
--- a/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp
@@ -85,44 +85,44 @@ void SemanticSyntaxHighlighter::rehighlight(Palette const& 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) {
case CodeComprehension::TokenInfo::SemanticType::Unknown:
- return { palette.base_text(), false };
+ return { palette.base_text() };
case CodeComprehension::TokenInfo::SemanticType::Keyword:
- return { palette.syntax_keyword(), true };
+ return { palette.syntax_keyword(), {}, true };
case CodeComprehension::TokenInfo::SemanticType::Type:
- return { palette.syntax_type(), true };
+ return { palette.syntax_type(), {}, true };
case CodeComprehension::TokenInfo::SemanticType::Identifier:
- return { palette.syntax_identifier(), false };
+ return { palette.syntax_identifier() };
case CodeComprehension::TokenInfo::SemanticType::String:
- return { palette.syntax_string(), false };
+ return { palette.syntax_string() };
case CodeComprehension::TokenInfo::SemanticType::Number:
- return { palette.syntax_number(), false };
+ return { palette.syntax_number() };
case CodeComprehension::TokenInfo::SemanticType::IncludePath:
- return { palette.syntax_preprocessor_value(), false };
+ return { palette.syntax_preprocessor_value() };
case CodeComprehension::TokenInfo::SemanticType::PreprocessorStatement:
- return { palette.syntax_preprocessor_statement(), false };
+ return { palette.syntax_preprocessor_statement() };
case CodeComprehension::TokenInfo::SemanticType::Comment:
- return { palette.syntax_comment(), false };
+ return { palette.syntax_comment() };
case CodeComprehension::TokenInfo::SemanticType::Function:
- return { palette.syntax_function(), false };
+ return { palette.syntax_function() };
case CodeComprehension::TokenInfo::SemanticType::Variable:
- return { palette.syntax_variable(), false };
+ return { palette.syntax_variable() };
case CodeComprehension::TokenInfo::SemanticType::CustomType:
- return { palette.syntax_custom_type(), false };
+ return { palette.syntax_custom_type() };
case CodeComprehension::TokenInfo::SemanticType::Namespace:
- return { palette.syntax_namespace(), false };
+ return { palette.syntax_namespace() };
case CodeComprehension::TokenInfo::SemanticType::Member:
- return { palette.syntax_member(), false };
+ return { palette.syntax_member() };
case CodeComprehension::TokenInfo::SemanticType::Parameter:
- return { palette.syntax_parameter(), false };
+ return { palette.syntax_parameter() };
case CodeComprehension::TokenInfo::SemanticType::PreprocessorMacro:
- return { palette.syntax_preprocessor_value(), false };
+ return { palette.syntax_preprocessor_value() };
default:
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)
@@ -133,9 +133,7 @@ void SemanticSyntaxHighlighter::update_spans(Vector<CodeComprehension::TokenInfo
GUI::TextDocumentSpan span;
span.range.set_start({ token.start_line, token.start_column });
span.range.set_end({ token.end_line, token.end_column + 1 });
- auto style = style_for_token_type(palette, token.type);
- span.attributes.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, token.type);
span.is_skippable = token.type == CodeComprehension::TokenInfo::SemanticType::Whitespace;
span.data = static_cast<u64>(token.type);
spans.append(span);
diff --git a/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp b/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp
index f0e2b389dc..dcb1e25116 100644
--- a/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp
@@ -13,33 +13,33 @@
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) {
case Cpp::Token::Type::Keyword:
- return { palette.syntax_keyword(), true };
+ return { palette.syntax_keyword(), {}, true };
case Cpp::Token::Type::KnownType:
- return { palette.syntax_type(), true };
+ return { palette.syntax_type(), {}, true };
case Cpp::Token::Type::Identifier:
- return { palette.syntax_identifier(), false };
+ return { palette.syntax_identifier() };
case Cpp::Token::Type::DoubleQuotedString:
case Cpp::Token::Type::SingleQuotedString:
case Cpp::Token::Type::RawString:
- return { palette.syntax_string(), false };
+ return { palette.syntax_string() };
case Cpp::Token::Type::Integer:
case Cpp::Token::Type::Float:
- return { palette.syntax_number(), false };
+ return { palette.syntax_number() };
case Cpp::Token::Type::IncludePath:
- return { palette.syntax_preprocessor_value(), false };
+ return { palette.syntax_preprocessor_value() };
case Cpp::Token::Type::EscapeSequence:
- return { palette.syntax_keyword(), true };
+ return { palette.syntax_keyword(), {}, true };
case Cpp::Token::Type::PreprocessorStatement:
case Cpp::Token::Type::IncludeStatement:
- return { palette.syntax_preprocessor_statement(), false };
+ return { palette.syntax_preprocessor_statement() };
case Cpp::Token::Type::Comment:
- return { palette.syntax_comment(), false };
+ return { palette.syntax_comment() };
default:
- return { palette.base_text(), false };
+ return { palette.base_text() };
}
}
@@ -69,9 +69,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span;
span.range.set_start({ token.start().line, token.start().column });
span.range.set_end({ token.end().line, token.end().column + 1 });
- auto style = style_for_token_type(palette, token.type());
- span.attributes.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, token.type());
span.is_skippable = token.type() == Cpp::Token::Type::Whitespace;
span.data = static_cast<u64>(token.type());
spans.append(span);
diff --git a/Userland/Libraries/LibGUI/GML/SyntaxHighlighter.cpp b/Userland/Libraries/LibGUI/GML/SyntaxHighlighter.cpp
index d021626559..6cc8a79c87 100644
--- a/Userland/Libraries/LibGUI/GML/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibGUI/GML/SyntaxHighlighter.cpp
@@ -12,7 +12,7 @@
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) {
case Token::Type::LeftCurly:
@@ -21,7 +21,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case Token::Type::ClassMarker:
return { palette.syntax_keyword() };
case Token::Type::ClassName:
- return { palette.syntax_identifier(), true };
+ return { palette.syntax_identifier(), {}, true };
case Token::Type::Identifier:
return { palette.syntax_identifier() };
case Token::Type::JsonValue:
@@ -54,9 +54,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span;
span.range.set_start({ token.m_start.line, token.m_start.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.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, token.m_type);
span.is_skippable = false;
span.data = static_cast<u64>(token.m_type);
spans.append(span);
diff --git a/Userland/Libraries/LibGUI/GitCommitSyntaxHighlighter.cpp b/Userland/Libraries/LibGUI/GitCommitSyntaxHighlighter.cpp
index e531479730..15ef75b051 100644
--- a/Userland/Libraries/LibGUI/GitCommitSyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibGUI/GitCommitSyntaxHighlighter.cpp
@@ -10,7 +10,7 @@
#include <LibGfx/Palette.h>
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) {
case GitCommitToken::Type::Comment:
@@ -31,9 +31,7 @@ void GitCommitSyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span;
span.range.set_start({ token.m_start.line, token.m_start.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.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, token.m_type);
span.is_skippable = false;
span.data = static_cast<u64>(token.m_type);
spans.append(span);
diff --git a/Userland/Libraries/LibGUI/INISyntaxHighlighter.cpp b/Userland/Libraries/LibGUI/INISyntaxHighlighter.cpp
index b66ad42b5d..d46d83c7d1 100644
--- a/Userland/Libraries/LibGUI/INISyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibGUI/INISyntaxHighlighter.cpp
@@ -11,13 +11,13 @@
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) {
case IniToken::Type::LeftBracket:
case IniToken::Type::RightBracket:
case IniToken::Type::Section:
- return { palette.syntax_keyword(), true };
+ return { palette.syntax_keyword(), {}, true };
case IniToken::Type::Name:
return { palette.syntax_identifier() };
case IniToken::Type::Value:
@@ -25,7 +25,7 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, IniTo
case IniToken::Type::Comment:
return { palette.syntax_comment() };
case IniToken::Type::Equal:
- return { palette.syntax_operator(), true };
+ return { palette.syntax_operator(), {}, true };
default:
return { palette.base_text() };
}
@@ -52,9 +52,7 @@ void IniSyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span;
span.range.set_start({ token.m_start.line, token.m_start.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.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, token.m_type);
span.is_skippable = token.m_type == IniToken::Type::Whitespace;
span.data = static_cast<u64>(token.m_type);
spans.append(span);
diff --git a/Userland/Libraries/LibJS/SyntaxHighlighter.cpp b/Userland/Libraries/LibJS/SyntaxHighlighter.cpp
index 2973773967..f7f0529f50 100644
--- a/Userland/Libraries/LibJS/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibJS/SyntaxHighlighter.cpp
@@ -13,7 +13,7 @@
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)) {
case TokenCategory::Invalid:
@@ -27,9 +27,9 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
case TokenCategory::Operator:
return { palette.syntax_operator() };
case TokenCategory::Keyword:
- return { palette.syntax_keyword(), true };
+ return { palette.syntax_keyword(), {}, true };
case TokenCategory::ControlKeyword:
- return { palette.syntax_control_keyword(), true };
+ return { palette.syntax_control_keyword(), {}, true };
case TokenCategory::Identifier:
return { palette.syntax_identifier() };
default:
@@ -79,9 +79,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
span.range.set_start(start);
span.range.set_end({ position.line(), position.column() });
auto type = is_trivia ? TokenType::Invalid : token.type();
- auto style = style_for_token_type(palette, type);
- span.attributes.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, type);
span.is_skippable = is_trivia;
span.data = static_cast<u64>(type);
spans.append(span);
diff --git a/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp b/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp
index a6f327981e..b6898080d5 100644
--- a/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp
@@ -12,25 +12,25 @@
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)) {
case TokenCategory::Keyword:
- return { palette.syntax_keyword(), true };
+ return { palette.syntax_keyword(), {}, true };
case TokenCategory::Identifier:
- return { palette.syntax_identifier(), false };
+ return { palette.syntax_identifier() };
case TokenCategory::Number:
- return { palette.syntax_number(), false };
+ return { palette.syntax_number() };
case TokenCategory::Blob:
case TokenCategory::String:
- return { palette.syntax_string(), false };
+ return { palette.syntax_string() };
case TokenCategory::Operator:
- return { palette.syntax_operator(), false };
+ return { palette.syntax_operator() };
case TokenCategory::Punctuation:
- return { palette.syntax_punctuation(), false };
+ return { palette.syntax_punctuation() };
case TokenCategory::Invalid:
default:
- return { palette.base_text(), false };
+ return { palette.base_text() };
}
}
@@ -54,9 +54,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
GUI::TextDocumentSpan span;
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 });
- auto style = style_for_token_type(palette, token.type());
- span.attributes.color = style.color;
- span.attributes.bold = style.bold;
+ span.attributes = style_for_token_type(palette, token.type());
span.data = static_cast<u64>(token.type());
spans.append(span);
diff --git a/Userland/Libraries/LibSyntax/Highlighter.h b/Userland/Libraries/LibSyntax/Highlighter.h
index 0410a5b5f5..c67777c1b8 100644
--- a/Userland/Libraries/LibSyntax/Highlighter.h
+++ b/Userland/Libraries/LibSyntax/Highlighter.h
@@ -15,11 +15,6 @@
namespace Syntax {
-struct TextStyle {
- const Gfx::Color color;
- bool const bold { false };
-};
-
class Highlighter {
AK_MAKE_NONCOPYABLE(Highlighter);
AK_MAKE_NONMOVABLE(Highlighter);