From 50308f6fda70054c4d50423cb4422b3b74f583c1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Feb 2021 17:07:33 +0100 Subject: [PATCH] Shell: Move Shell syntax highlighter LibShell --- .../TextEditor/TextEditorWidget.cpp | 4 +-- Userland/DevTools/HackStudio/Editor.cpp | 4 +-- Userland/Libraries/LibGUI/CMakeLists.txt | 3 +- Userland/Shell/CMakeLists.txt | 3 +- .../SyntaxHighlighter.cpp} | 28 +++++++++---------- .../SyntaxHighlighter.h} | 8 +++--- 6 files changed, 24 insertions(+), 26 deletions(-) rename Userland/{Libraries/LibGUI/ShellSyntaxHighlighter.cpp => Shell/SyntaxHighlighter.cpp} (95%) rename Userland/{Libraries/LibGUI/ShellSyntaxHighlighter.h => Shell/SyntaxHighlighter.h} (92%) diff --git a/Userland/Applications/TextEditor/TextEditorWidget.cpp b/Userland/Applications/TextEditor/TextEditorWidget.cpp index 243cdebb14..3bff09c7b2 100644 --- a/Userland/Applications/TextEditor/TextEditorWidget.cpp +++ b/Userland/Applications/TextEditor/TextEditorWidget.cpp @@ -47,7 +47,6 @@ #include #include #include -#include #include #include #include @@ -59,6 +58,7 @@ #include #include #include +#include #include TextEditorWidget::TextEditorWidget() @@ -497,7 +497,7 @@ TextEditorWidget::TextEditorWidget() syntax_menu.add_action(*m_ini_highlight); m_shell_highlight = GUI::Action::create_checkable("Shell File", [&](auto&) { - m_editor->set_syntax_highlighter(make()); + m_editor->set_syntax_highlighter(make()); m_editor->update(); }); syntax_actions.add_action(*m_shell_highlight); diff --git a/Userland/DevTools/HackStudio/Editor.cpp b/Userland/DevTools/HackStudio/Editor.cpp index c98e8f43e0..a875c489e4 100644 --- a/Userland/DevTools/HackStudio/Editor.cpp +++ b/Userland/DevTools/HackStudio/Editor.cpp @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -49,6 +48,7 @@ #include #include #include +#include #include namespace HackStudio { @@ -430,7 +430,7 @@ void Editor::set_document(GUI::TextDocument& doc) set_syntax_highlighter(make()); break; case Language::Shell: - set_syntax_highlighter(make()); + set_syntax_highlighter(make()); m_language_client = get_language_client(project().root_path()); break; default: diff --git a/Userland/Libraries/LibGUI/CMakeLists.txt b/Userland/Libraries/LibGUI/CMakeLists.txt index e51ad17046..c3bbd3ed80 100644 --- a/Userland/Libraries/LibGUI/CMakeLists.txt +++ b/Userland/Libraries/LibGUI/CMakeLists.txt @@ -74,7 +74,6 @@ set(SOURCES ScrollBar.cpp ScrollableWidget.cpp SeparatorWidget.cpp - ShellSyntaxHighlighter.cpp Shortcut.cpp Slider.cpp SortingProxyModel.cpp @@ -108,4 +107,4 @@ set(GENERATED_SOURCES ) serenity_lib(LibGUI gui) -target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThread LibShell LibRegex LibSyntax) +target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThread LibRegex LibSyntax) diff --git a/Userland/Shell/CMakeLists.txt b/Userland/Shell/CMakeLists.txt index 706bc5eae1..ae25a8cde9 100644 --- a/Userland/Shell/CMakeLists.txt +++ b/Userland/Shell/CMakeLists.txt @@ -6,10 +6,11 @@ set(SOURCES NodeVisitor.cpp Parser.cpp Shell.cpp + SyntaxHighlighter.cpp ) serenity_lib(LibShell shell) -target_link_libraries(LibShell LibCore LibLine) +target_link_libraries(LibShell LibCore LibLine LibSyntax) set(SOURCES main.cpp diff --git a/Userland/Libraries/LibGUI/ShellSyntaxHighlighter.cpp b/Userland/Shell/SyntaxHighlighter.cpp similarity index 95% rename from Userland/Libraries/LibGUI/ShellSyntaxHighlighter.cpp rename to Userland/Shell/SyntaxHighlighter.cpp index 1518e57687..393ebb3f86 100644 --- a/Userland/Libraries/LibGUI/ShellSyntaxHighlighter.cpp +++ b/Userland/Shell/SyntaxHighlighter.cpp @@ -25,16 +25,14 @@ */ #include -#include #include #include #include #include #include +#include -namespace GUI { - -using namespace Shell; +namespace Shell { enum class AugmentedTokenKind : u32 { __TokenTypeCount = (u32)AST::Node::Kind::__Count, @@ -44,7 +42,7 @@ enum class AugmentedTokenKind : u32 { class HighlightVisitor : public AST::NodeVisitor { public: - HighlightVisitor(Vector& spans, const Gfx::Palette& palette, const TextDocument& document) + HighlightVisitor(Vector& spans, const Gfx::Palette& palette, const GUI::TextDocument& document) : m_spans(spans) , m_palette(palette) , m_document(document) @@ -77,12 +75,12 @@ private: return new_line; } - void set_offset_range_end(TextRange& range, const AST::Position::Line& line, size_t offset = 1) + void set_offset_range_end(GUI::TextRange& range, const AST::Position::Line& line, size_t offset = 1) { auto new_line = offset_line(line, offset); range.set_end({ new_line.line_number, new_line.line_column }); } - void set_offset_range_start(TextRange& range, const AST::Position::Line& line, size_t offset = 1) + void set_offset_range_start(GUI::TextRange& range, const AST::Position::Line& line, size_t offset = 1) { auto new_line = offset_line(line, offset); range.set_start({ new_line.line_number, new_line.line_column }); @@ -476,11 +474,11 @@ private: Vector& m_spans; const Gfx::Palette& m_palette; - const TextDocument& m_document; + const GUI::TextDocument& m_document; bool m_is_first_in_command { false }; }; -bool ShellSyntaxHighlighter::is_identifier(void* token) const +bool SyntaxHighlighter::is_identifier(void* token) const { if (!token) return false; @@ -491,7 +489,7 @@ bool ShellSyntaxHighlighter::is_identifier(void* token) const || kind == (size_t)AST::Node::Kind::Tilde; } -bool ShellSyntaxHighlighter::is_navigatable(void* token) const +bool SyntaxHighlighter::is_navigatable(void* token) const { if (!token) return false; @@ -500,7 +498,7 @@ bool ShellSyntaxHighlighter::is_navigatable(void* token) const return (size_t)kind == (size_t)AST::Node::Kind::BarewordLiteral; } -void ShellSyntaxHighlighter::rehighlight(Gfx::Palette palette) +void SyntaxHighlighter::rehighlight(Gfx::Palette palette) { auto text = m_client->get_text(); @@ -522,9 +520,9 @@ void ShellSyntaxHighlighter::rehighlight(Gfx::Palette palette) m_client->do_update(); } -Vector ShellSyntaxHighlighter::matching_token_pairs() const +Vector SyntaxHighlighter::matching_token_pairs() const { - static Vector pairs; + static Vector pairs; if (pairs.is_empty()) { pairs.append({ (void*)static_cast(AugmentedTokenKind::OpenParen), @@ -534,12 +532,12 @@ Vector ShellSyntaxHighlighter::matching_ return pairs; } -bool ShellSyntaxHighlighter::token_types_equal(void* token0, void* token1) const +bool SyntaxHighlighter::token_types_equal(void* token0, void* token1) const { return token0 == token1; } -ShellSyntaxHighlighter::~ShellSyntaxHighlighter() +SyntaxHighlighter::~SyntaxHighlighter() { } diff --git a/Userland/Libraries/LibGUI/ShellSyntaxHighlighter.h b/Userland/Shell/SyntaxHighlighter.h similarity index 92% rename from Userland/Libraries/LibGUI/ShellSyntaxHighlighter.h rename to Userland/Shell/SyntaxHighlighter.h index 6abe63814e..d870bd2732 100644 --- a/Userland/Libraries/LibGUI/ShellSyntaxHighlighter.h +++ b/Userland/Shell/SyntaxHighlighter.h @@ -28,12 +28,12 @@ #include -namespace GUI { +namespace Shell { -class ShellSyntaxHighlighter : public Syntax::Highlighter { +class SyntaxHighlighter : public Syntax::Highlighter { public: - ShellSyntaxHighlighter() { } - virtual ~ShellSyntaxHighlighter() override; + SyntaxHighlighter() { } + virtual ~SyntaxHighlighter() override; virtual bool is_identifier(void*) const override; virtual bool is_navigatable(void*) const override;