1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:17:35 +00:00

LibSyntax+LibGUI+LibJS: Move JS syntax highlighter to LibJS

This is a little bit messy but the basic idea is:

Syntax::Highlighter now has a Syntax::HighlighterClient to talk to the
outside world. It mostly communicates in LibGUI primitives that are
available in headers, so inlineable.

GUI::TextEditor inherits from Syntax::HighlighterClient.

This let us to move GUI::JSSyntaxHighlighter to JS::SyntaxHighlighter
and remove LibGUI's dependency on LibJS.
This commit is contained in:
Andreas Kling 2021-02-07 16:56:02 +01:00
parent 22baa5e64f
commit ddbf20ecf6
23 changed files with 139 additions and 80 deletions

View file

@ -48,7 +48,6 @@ set(SOURCES
IconView.cpp
ImageWidget.cpp
InputBox.cpp
JSSyntaxHighlighter.cpp
JsonArrayModel.cpp
Label.cpp
Layout.cpp
@ -109,4 +108,4 @@ set(GENERATED_SOURCES
)
serenity_lib(LibGUI gui)
target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThread LibShell LibRegex LibJS LibSyntax)
target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThread LibShell LibRegex LibSyntax)

View file

@ -82,6 +82,7 @@ class TableView;
class TextBox;
class TextDocument;
class TextDocumentLine;
struct TextDocumentSpan;
class TextDocumentUndoCommand;
class TextEditor;
class ThemeChangeEvent;

View file

@ -32,7 +32,7 @@
namespace GUI {
static Syntax::TextStyle style_for_token_type(Gfx::Palette palette, GMLToken::Type type)
static Syntax::TextStyle style_for_token_type(const Gfx::Palette& palette, GMLToken::Type type)
{
switch (type) {
case GMLToken::Type::LeftCurly:
@ -61,8 +61,7 @@ bool GMLSyntaxHighlighter::is_identifier(void* token) const
void GMLSyntaxHighlighter::rehighlight(Gfx::Palette palette)
{
ASSERT(m_editor);
auto text = m_editor->text();
auto text = m_client->get_text();
GMLLexer lexer(text);
auto tokens = lexer.lex();
@ -78,12 +77,12 @@ void GMLSyntaxHighlighter::rehighlight(Gfx::Palette palette)
span.data = reinterpret_cast<void*>(token.m_type);
spans.append(span);
}
m_editor->document().set_spans(spans);
m_client->do_set_spans(move(spans));
m_has_brace_buddies = false;
highlight_matching_token_pair();
m_editor->update();
m_client->do_update();
}
Vector<GMLSyntaxHighlighter::MatchingTokenPair> GMLSyntaxHighlighter::matching_token_pairs() const

View file

@ -60,8 +60,7 @@ bool IniSyntaxHighlighter::is_identifier(void* token) const
void IniSyntaxHighlighter::rehighlight(Gfx::Palette palette)
{
ASSERT(m_editor);
auto text = m_editor->text();
auto text = m_client->get_text();
IniLexer lexer(text);
auto tokens = lexer.lex();
@ -77,12 +76,12 @@ void IniSyntaxHighlighter::rehighlight(Gfx::Palette palette)
span.data = reinterpret_cast<void*>(token.m_type);
spans.append(span);
}
m_editor->document().set_spans(spans);
m_client->do_set_spans(move(spans));
m_has_brace_buddies = false;
highlight_matching_token_pair();
m_editor->update();
m_client->do_update();
}
Vector<IniSyntaxHighlighter::MatchingTokenPair> IniSyntaxHighlighter::matching_token_pairs() const

View file

@ -502,25 +502,24 @@ bool ShellSyntaxHighlighter::is_navigatable(void* token) const
void ShellSyntaxHighlighter::rehighlight(Gfx::Palette palette)
{
ASSERT(m_editor);
auto text = m_editor->text();
auto text = m_client->get_text();
Parser parser(text);
auto ast = parser.parse();
Vector<GUI::TextDocumentSpan> spans;
GUI::TextPosition position { 0, 0 };
HighlightVisitor visitor { spans, palette, m_editor->document() };
HighlightVisitor visitor { spans, palette, m_client->get_document() };
if (ast)
ast->visit(visitor);
quick_sort(spans, [](auto& a, auto& b) { return a.range.start() < b.range.start(); });
m_editor->document().set_spans(spans);
m_client->do_set_spans(move(spans));
m_has_brace_buddies = false;
highlight_matching_token_pair();
m_editor->update();
m_client->do_update();
}
Vector<Syntax::Highlighter::MatchingTokenPair> ShellSyntaxHighlighter::matching_token_pairs() const

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -79,7 +79,7 @@ public:
const TextDocumentLine& line(size_t line_index) const { return m_lines[line_index]; }
TextDocumentLine& line(size_t line_index) { return m_lines[line_index]; }
void set_spans(const Vector<TextDocumentSpan>& spans) { m_spans = spans; }
void set_spans(Vector<TextDocumentSpan> spans) { m_spans = move(spans); }
void set_text(const StringView&);

View file

@ -37,13 +37,15 @@
#include <LibGUI/TextRange.h>
#include <LibGfx/TextAlignment.h>
#include <LibSyntax/Forward.h>
#include <LibSyntax/HighlighterClient.h>
namespace GUI {
class TextEditor
: public ScrollableWidget
, public TextDocument::Client {
C_OBJECT(TextEditor)
, public TextDocument::Client
, public Syntax::HighlighterClient {
C_OBJECT(TextEditor);
public:
enum Type {
@ -241,6 +243,16 @@ private:
virtual void document_did_set_text() override;
virtual void document_did_set_cursor(const TextPosition&) override;
// ^Syntax::HighlighterClient
virtual Vector<TextDocumentSpan>& spans() final { return document().spans(); }
virtual const Vector<TextDocumentSpan>& spans() const final { return document().spans(); }
virtual void highlighter_did_set_spans(Vector<TextDocumentSpan> spans) { document().set_spans(move(spans)); }
virtual void set_span_at_index(size_t index, TextDocumentSpan span) final { document().set_span_at_index(index, move(span)); }
virtual void highlighter_did_request_update() final { update(); }
virtual String highlighter_did_request_text() const final { return text(); }
virtual GUI::TextDocument& highlighter_did_request_document() final { return document(); }
virtual GUI::TextPosition highlighter_did_request_cursor() const final { return m_cursor; }
void create_actions();
void paint_ruler(Painter&);
void update_content_size();