LibSyntax: Move GUI::Highlighter to Syntax::Highlighter in LibSyntax

This is a move towards dropping more LibGUI dependencies.
This commit is contained in:
Andreas Kling 2021-02-07 15:15:10 +01:00
parent ff2438e0ce
commit 43c7d7d285
Notes: sideshowbarker 2024-07-18 22:32:34 +09:00
22 changed files with 99 additions and 61 deletions

View file

@ -28,7 +28,6 @@
#include "Cell.h"
#include <LibGUI/JSSyntaxHighlighter.h>
#include <LibGUI/SyntaxHighlighter.h>
namespace Spreadsheet {

View file

@ -174,7 +174,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
update();
};
m_cell_value_editor->set_enabled(true);
static_cast<CellSyntaxHighlighter*>(const_cast<GUI::SyntaxHighlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&cell);
static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&cell);
return;
}
@ -207,11 +207,11 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
}
};
m_cell_value_editor->set_enabled(true);
static_cast<CellSyntaxHighlighter*>(const_cast<GUI::SyntaxHighlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(first_cell);
static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(first_cell);
};
m_selected_view->on_selection_dropped = [&]() {
m_cell_value_editor->set_enabled(false);
static_cast<CellSyntaxHighlighter*>(const_cast<GUI::SyntaxHighlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr);
static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr);
m_cell_value_editor->set_text("");
m_current_cell_label->set_enabled(false);
m_current_cell_label->set_text("");

View file

@ -43,7 +43,6 @@
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGUI/ShellSyntaxHighlighter.h>
#include <LibGUI/SyntaxHighlighter.h>
#include <LibGUI/Window.h>
#include <LibMarkdown/Document.h>
#include <LibWeb/DOM/ElementFactory.h>

View file

@ -27,6 +27,7 @@ add_subdirectory(LibProtocol)
add_subdirectory(LibPthread)
add_subdirectory(LibRegex)
add_subdirectory(LibSymbolClient)
add_subdirectory(LibSyntax)
add_subdirectory(LibSystem)
add_subdirectory(LibTar)
add_subdirectory(LibTextCodec)

View file

@ -33,7 +33,7 @@
namespace Cpp {
static GUI::TextStyle style_for_token_type(const Gfx::Palette& palette, Cpp::Token::Type type)
static Syntax::TextStyle style_for_token_type(const Gfx::Palette& palette, Cpp::Token::Type type)
{
switch (type) {
case Cpp::Token::Type::Keyword:

View file

@ -26,11 +26,11 @@
#pragma once
#include <LibGUI/SyntaxHighlighter.h>
#include <LibSyntax/Highlighter.h>
namespace Cpp {
class SyntaxHighlighter final : public GUI::SyntaxHighlighter {
class SyntaxHighlighter final : public Syntax::Highlighter {
public:
SyntaxHighlighter() { }
virtual ~SyntaxHighlighter() override;
@ -38,7 +38,7 @@ public:
virtual bool is_identifier(void*) const override;
virtual bool is_navigatable(void*) const override;
virtual GUI::SyntaxLanguage language() const override { return GUI::SyntaxLanguage::Cpp; }
virtual Syntax::Language language() const override { return Syntax::Language::Cpp; }
virtual void rehighlight(Gfx::Palette) override;
protected:

View file

@ -83,7 +83,6 @@ set(SOURCES
Splitter.cpp
StackWidget.cpp
StatusBar.cpp
SyntaxHighlighter.cpp
TabWidget.cpp
TableView.cpp
TextBox.cpp
@ -110,4 +109,4 @@ set(GENERATED_SOURCES
)
serenity_lib(LibGUI gui)
target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThread LibShell LibRegex LibJS)
target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThread LibShell LibRegex LibJS LibSyntax)

View file

@ -77,7 +77,6 @@ class SpinBox;
class Splitter;
class StackWidget;
class StatusBar;
class SyntaxHighlighter;
class TabWidget;
class TableView;
class TextBox;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,7 +32,7 @@
namespace GUI {
static TextStyle style_for_token_type(Gfx::Palette palette, GMLToken::Type type)
static Syntax::TextStyle style_for_token_type(Gfx::Palette palette, GMLToken::Type type)
{
switch (type) {
case GMLToken::Type::LeftCurly:
@ -88,7 +88,7 @@ void GMLSyntaxHighlighter::rehighlight(Gfx::Palette palette)
Vector<GMLSyntaxHighlighter::MatchingTokenPair> GMLSyntaxHighlighter::matching_token_pairs() const
{
static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
static Vector<MatchingTokenPair> pairs;
if (pairs.is_empty()) {
pairs.append({ reinterpret_cast<void*>(GMLToken::Type::LeftCurly), reinterpret_cast<void*>(GMLToken::Type::RightCurly) });
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -26,18 +26,18 @@
#pragma once
#include <LibGUI/SyntaxHighlighter.h>
#include <LibSyntax/Highlighter.h>
namespace GUI {
class GMLSyntaxHighlighter final : public SyntaxHighlighter {
class GMLSyntaxHighlighter final : public Syntax::Highlighter {
public:
GMLSyntaxHighlighter() { }
virtual ~GMLSyntaxHighlighter() override;
virtual bool is_identifier(void*) const override;
virtual SyntaxLanguage language() const override { return SyntaxLanguage::INI; }
virtual Syntax::Language language() const override { return Syntax::Language::GML; }
virtual void rehighlight(Gfx::Palette) override;
protected:

View file

@ -32,7 +32,7 @@
namespace GUI {
static TextStyle style_for_token_type(Gfx::Palette palette, IniToken::Type type)
static Syntax::TextStyle style_for_token_type(const Gfx::Palette& palette, IniToken::Type type)
{
switch (type) {
case IniToken::Type::LeftBracket:
@ -87,7 +87,7 @@ void IniSyntaxHighlighter::rehighlight(Gfx::Palette palette)
Vector<IniSyntaxHighlighter::MatchingTokenPair> IniSyntaxHighlighter::matching_token_pairs() const
{
static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
static Vector<MatchingTokenPair> pairs;
if (pairs.is_empty()) {
pairs.append({ reinterpret_cast<void*>(IniToken::Type::LeftBracket), reinterpret_cast<void*>(IniToken::Type::RightBracket) });
}

View file

@ -26,18 +26,18 @@
#pragma once
#include <LibGUI/SyntaxHighlighter.h>
#include <LibSyntax/Highlighter.h>
namespace GUI {
class IniSyntaxHighlighter final : public SyntaxHighlighter {
class IniSyntaxHighlighter final : public Syntax::Highlighter {
public:
IniSyntaxHighlighter() { }
virtual ~IniSyntaxHighlighter() override;
virtual bool is_identifier(void*) const override;
virtual SyntaxLanguage language() const override { return SyntaxLanguage::INI; }
virtual Syntax::Language language() const override { return Syntax::Language::INI; }
virtual void rehighlight(Gfx::Palette) override;
protected:

View file

@ -34,7 +34,7 @@
namespace GUI {
static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type)
static Syntax::TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type)
{
switch (JS::Token::category(type)) {
case JS::TokenCategory::Invalid:
@ -133,9 +133,9 @@ void JSSyntaxHighlighter::rehighlight(Gfx::Palette palette)
m_editor->update();
}
Vector<SyntaxHighlighter::MatchingTokenPair> JSSyntaxHighlighter::matching_token_pairs() const
Vector<Syntax::Highlighter::MatchingTokenPair> JSSyntaxHighlighter::matching_token_pairs() const
{
static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
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) });

View file

@ -26,11 +26,11 @@
#pragma once
#include <LibGUI/SyntaxHighlighter.h>
#include <LibSyntax/Highlighter.h>
namespace GUI {
class JSSyntaxHighlighter : public SyntaxHighlighter {
class JSSyntaxHighlighter : public Syntax::Highlighter {
public:
JSSyntaxHighlighter() { }
virtual ~JSSyntaxHighlighter() override;
@ -38,7 +38,7 @@ public:
virtual bool is_identifier(void*) const override;
virtual bool is_navigatable(void*) const override;
virtual SyntaxLanguage language() const override { return SyntaxLanguage::JavaScript; }
virtual Syntax::Language language() const override { return Syntax::Language::JavaScript; }
virtual void rehighlight(Gfx::Palette) override;
protected:

View file

@ -523,9 +523,9 @@ void ShellSyntaxHighlighter::rehighlight(Gfx::Palette palette)
m_editor->update();
}
Vector<SyntaxHighlighter::MatchingTokenPair> ShellSyntaxHighlighter::matching_token_pairs() const
Vector<Syntax::Highlighter::MatchingTokenPair> ShellSyntaxHighlighter::matching_token_pairs() const
{
static Vector<SyntaxHighlighter::MatchingTokenPair> pairs;
static Vector<Syntax::Highlighter::MatchingTokenPair> pairs;
if (pairs.is_empty()) {
pairs.append({
(void*)static_cast<size_t>(AugmentedTokenKind::OpenParen),

View file

@ -26,11 +26,11 @@
#pragma once
#include <LibGUI/SyntaxHighlighter.h>
#include <LibSyntax/Highlighter.h>
namespace GUI {
class ShellSyntaxHighlighter : public SyntaxHighlighter {
class ShellSyntaxHighlighter : public Syntax::Highlighter {
public:
ShellSyntaxHighlighter() { }
virtual ~ShellSyntaxHighlighter() override;
@ -38,7 +38,7 @@ public:
virtual bool is_identifier(void*) const override;
virtual bool is_navigatable(void*) const override;
virtual SyntaxLanguage language() const override { return SyntaxLanguage::Shell; }
virtual Syntax::Language language() const override { return Syntax::Language::Shell; }
virtual void rehighlight(Gfx::Palette) override;
protected:

View file

@ -38,13 +38,13 @@
#include <LibGUI/Painter.h>
#include <LibGUI/RegularEditingEngine.h>
#include <LibGUI/ScrollBar.h>
#include <LibGUI/SyntaxHighlighter.h>
#include <LibGUI/TextEditor.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
#include <LibSyntax/Highlighter.h>
#include <ctype.h>
#include <fcntl.h>
#include <math.h>
@ -1586,12 +1586,12 @@ void TextEditor::flush_pending_change_notification_if_needed()
m_has_pending_change_notification = false;
}
const SyntaxHighlighter* TextEditor::syntax_highlighter() const
const Syntax::Highlighter* TextEditor::syntax_highlighter() const
{
return m_highlighter.ptr();
}
void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
void TextEditor::set_syntax_highlighter(OwnPtr<Syntax::Highlighter> highlighter)
{
if (m_highlighter)
m_highlighter->detach();

View file

@ -36,6 +36,7 @@
#include <LibGUI/TextDocument.h>
#include <LibGUI/TextRange.h>
#include <LibGfx/TextAlignment.h>
#include <LibSyntax/Forward.h>
namespace GUI {
@ -173,8 +174,8 @@ public:
void set_cursor(size_t line, size_t column);
void set_cursor(const TextPosition&);
const SyntaxHighlighter* syntax_highlighter() const;
void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);
const Syntax::Highlighter* syntax_highlighter() const;
void set_syntax_highlighter(OwnPtr<Syntax::Highlighter>);
const AutocompleteProvider* autocomplete_provider() const;
void set_autocomplete_provider(OwnPtr<AutocompleteProvider>&&);
@ -344,7 +345,7 @@ private:
NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
OwnPtr<SyntaxHighlighter> m_highlighter;
OwnPtr<Syntax::Highlighter> m_highlighter;
OwnPtr<AutocompleteProvider> m_autocomplete_provider;
OwnPtr<AutocompleteBox> m_autocomplete_box;
bool m_should_keep_autocomplete_box { false };

View file

@ -0,0 +1,6 @@
set(SOURCES
Highlighter.cpp
)
serenity_lib(LibSyntax syntax)
target_link_libraries(LibSyntax LibC)

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace Syntax {
class Highlighter;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2020-2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -24,16 +24,16 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGUI/SyntaxHighlighter.h>
#include <LibGUI/TextEditor.h>
#include <LibSyntax/Highlighter.h>
namespace GUI {
namespace Syntax {
SyntaxHighlighter::~SyntaxHighlighter()
Highlighter::~Highlighter()
{
}
void SyntaxHighlighter::highlight_matching_token_pair()
void Highlighter::highlight_matching_token_pair()
{
ASSERT(m_editor);
auto& document = m_editor->document();
@ -125,19 +125,19 @@ void SyntaxHighlighter::highlight_matching_token_pair()
}
}
void SyntaxHighlighter::attach(TextEditor& editor)
void Highlighter::attach(GUI::TextEditor& editor)
{
ASSERT(!m_editor);
m_editor = editor;
}
void SyntaxHighlighter::detach()
void Highlighter::detach()
{
ASSERT(m_editor);
m_editor = nullptr;
}
void SyntaxHighlighter::cursor_did_change()
void Highlighter::cursor_did_change()
{
ASSERT(m_editor);
auto& document = m_editor->document();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2020-2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -31,43 +31,44 @@
#include <LibGUI/TextDocument.h>
#include <LibGfx/Palette.h>
namespace GUI {
namespace Syntax {
enum class SyntaxLanguage {
enum class Language {
PlainText,
Cpp,
JavaScript,
INI,
GML,
Shell,
};
struct TextStyle {
const Color color;
const Gfx::Color color;
const bool bold { false };
};
class SyntaxHighlighter {
AK_MAKE_NONCOPYABLE(SyntaxHighlighter);
AK_MAKE_NONMOVABLE(SyntaxHighlighter);
class Highlighter {
AK_MAKE_NONCOPYABLE(Highlighter);
AK_MAKE_NONMOVABLE(Highlighter);
public:
virtual ~SyntaxHighlighter();
virtual ~Highlighter();
virtual SyntaxLanguage language() const = 0;
virtual Language language() const = 0;
virtual void rehighlight(Gfx::Palette) = 0;
virtual void highlight_matching_token_pair();
virtual bool is_identifier(void*) const { return false; };
virtual bool is_navigatable(void*) const { return false; };
void attach(TextEditor& editor);
void attach(GUI::TextEditor& editor);
void detach();
void cursor_did_change();
protected:
SyntaxHighlighter() { }
Highlighter() { }
WeakPtr<TextEditor> m_editor;
WeakPtr<GUI::TextEditor> m_editor;
struct MatchingTokenPair {
void* open;