From 676d75603b726660e5ce058c95d67d89c79ec317 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 7 Mar 2023 20:51:15 +0000 Subject: LibCMake+LibSyntax: Add a CMakeCache.txt syntax highlighter --- .../LibCMake/CMakeCache/SyntaxHighlighter.cpp | 89 ++++++++++++++++++++++ .../LibCMake/CMakeCache/SyntaxHighlighter.h | 30 ++++++++ Userland/Libraries/LibCMake/CMakeLists.txt | 1 + Userland/Libraries/LibSyntax/Highlighter.cpp | 4 + Userland/Libraries/LibSyntax/Highlighter.h | 1 + 5 files changed, 125 insertions(+) create mode 100644 Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp create mode 100644 Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.h (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp b/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp new file mode 100644 index 0000000000..2cb7db81a3 --- /dev/null +++ b/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "SyntaxHighlighter.h" +#include + +namespace CMake::Cache { + +static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token::Type type) +{ + switch (type) { + case Token::Type::Comment: + case Token::Type::HelpText: + return { palette.syntax_comment() }; + case Token::Type::Key: + return { palette.syntax_identifier() }; + case Token::Type::Type: + return { palette.syntax_type() }; + case Token::Type::Colon: + case Token::Type::Equals: + return { palette.syntax_punctuation() }; + case Token::Type::Value: + return { palette.syntax_string() }; + case Token::Type::Garbage: + return { palette.red() }; + default: + return { palette.base_text() }; + } +} + +bool SyntaxHighlighter::is_identifier(u64 token_type) const +{ + auto cmake_token = static_cast(token_type); + return cmake_token == Token::Type::Key; +} + +void SyntaxHighlighter::rehighlight(Gfx::Palette const& palette) +{ + auto text = m_client->get_text(); + auto tokens = Lexer::lex(text).release_value_but_fixme_should_propagate_errors(); + + Vector spans; + auto highlight_span = [&](Token::Type type, Position const& start, Position const& end) { + GUI::TextDocumentSpan span; + span.range.set_start({ start.line, start.column }); + span.range.set_end({ end.line, end.column }); + 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 = true; + span.attributes.underline_color = palette.red(); + span.attributes.underline_style = Gfx::TextAttributes::UnderlineStyle::Wavy; + } + span.is_skippable = false; + span.data = static_cast(type); + spans.append(move(span)); + }; + + for (auto const& token : tokens) { + highlight_span(token.type, token.start, token.end); + } + + m_client->do_set_spans(move(spans)); + + m_has_brace_buddies = false; + highlight_matching_token_pair(); + + m_client->do_update(); +} + +Vector SyntaxHighlighter::matching_token_pairs_impl() const +{ + static Vector empty; + return empty; +} + +bool SyntaxHighlighter::token_types_equal(u64 token1, u64 token2) const +{ + return static_cast(token1) == static_cast(token2); +} + +} diff --git a/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.h b/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.h new file mode 100644 index 0000000000..62c7d54610 --- /dev/null +++ b/Userland/Libraries/LibCMake/CMakeCache/SyntaxHighlighter.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace CMake::Cache { + +class SyntaxHighlighter final : public Syntax::Highlighter { +public: + SyntaxHighlighter() = default; + virtual ~SyntaxHighlighter() override = default; + + virtual bool is_identifier(u64) const override; + + virtual Syntax::Language language() const override { return Syntax::Language::CMakeCache; } + virtual Optional comment_prefix() const override { return "#"sv; } + virtual Optional comment_suffix() const override { return {}; } + virtual void rehighlight(Palette const&) override; + +protected: + virtual Vector matching_token_pairs_impl() const override; + virtual bool token_types_equal(u64, u64) const override; +}; + +} diff --git a/Userland/Libraries/LibCMake/CMakeLists.txt b/Userland/Libraries/LibCMake/CMakeLists.txt index 36af8b8e7e..6f07e1a3dd 100644 --- a/Userland/Libraries/LibCMake/CMakeLists.txt +++ b/Userland/Libraries/LibCMake/CMakeLists.txt @@ -1,5 +1,6 @@ set(SOURCES CMakeCache/Lexer.cpp + CMakeCache/SyntaxHighlighter.cpp Lexer.cpp SyntaxHighlighter.cpp Token.cpp diff --git a/Userland/Libraries/LibSyntax/Highlighter.cpp b/Userland/Libraries/LibSyntax/Highlighter.cpp index 93ee9b6aab..48631e8275 100644 --- a/Userland/Libraries/LibSyntax/Highlighter.cpp +++ b/Userland/Libraries/LibSyntax/Highlighter.cpp @@ -143,6 +143,8 @@ StringView language_to_string(Language language) switch (language) { case Language::CMake: return "CMake"sv; + case Language::CMakeCache: + return "CMakeCache"sv; case Language::Cpp: return "C++"sv; case Language::CSS: @@ -172,6 +174,8 @@ StringView common_language_extension(Language language) switch (language) { case Language::CMake: return "cmake"sv; + case Language::CMakeCache: + return {}; case Language::Cpp: return "cpp"sv; case Language::CSS: diff --git a/Userland/Libraries/LibSyntax/Highlighter.h b/Userland/Libraries/LibSyntax/Highlighter.h index 778bf129e7..3ad5af74ac 100644 --- a/Userland/Libraries/LibSyntax/Highlighter.h +++ b/Userland/Libraries/LibSyntax/Highlighter.h @@ -16,6 +16,7 @@ namespace Syntax { enum class Language { CMake, + CMakeCache, Cpp, CSS, GitCommit, -- cgit v1.2.3