mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +00:00
LibGUI+HackStudio: Move syntax highlighting from HackStudio to LibGUI
This patch introduces the GUI::SyntaxHighlighter class, which can be attached to a GUI::TextEditor to provide syntax highlighting. The C++ syntax highlighting from HackStudio becomes a new class called GUI::CppSyntaxHighlighter. This will make it possible to get C++ syntax highlighting in any app that uses a GUI::TextEditor. :^) Sidenote: It does feel a bit weird having a C++ lexer in a GUI toolkit library, and we'll probably end up moving this out to a separate place as this functionality grows larger.
This commit is contained in:
parent
6cf49c23d4
commit
bb8e65be41
13 changed files with 277 additions and 170 deletions
|
@ -25,7 +25,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Editor.h"
|
#include "Editor.h"
|
||||||
#include "CppLexer.h"
|
|
||||||
#include "EditorWrapper.h"
|
#include "EditorWrapper.h"
|
||||||
#include <AK/FileSystemPath.h>
|
#include <AK/FileSystemPath.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
|
@ -197,98 +196,3 @@ void Editor::mousemove_event(GUI::MouseEvent& event)
|
||||||
}
|
}
|
||||||
GUI::Application::the().hide_tooltip();
|
GUI::Application::the().hide_tooltip();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::highlight_matching_token_pair()
|
|
||||||
{
|
|
||||||
enum class Direction {
|
|
||||||
Forward,
|
|
||||||
Backward,
|
|
||||||
};
|
|
||||||
|
|
||||||
auto find_span_of_type = [&](int i, CppToken::Type type, CppToken::Type not_type, Direction direction) {
|
|
||||||
int nesting_level = 0;
|
|
||||||
bool forward = direction == Direction::Forward;
|
|
||||||
for (forward ? ++i : --i; forward ? (i < document().spans().size()) : (i >= 0); forward ? ++i : --i) {
|
|
||||||
auto& span = document().spans().at(i);
|
|
||||||
auto span_token_type = (CppToken::Type)((uintptr_t)span.data);
|
|
||||||
if (span_token_type == not_type) {
|
|
||||||
++nesting_level;
|
|
||||||
} else if (span_token_type == type) {
|
|
||||||
if (nesting_level-- <= 0)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto make_buddies = [&](int index0, int index1) {
|
|
||||||
auto& buddy0 = const_cast<GUI::TextDocumentSpan&>(document().spans()[index0]);
|
|
||||||
auto& buddy1 = const_cast<GUI::TextDocumentSpan&>(document().spans()[index1]);
|
|
||||||
m_has_brace_buddies = true;
|
|
||||||
m_brace_buddies[0].index = index0;
|
|
||||||
m_brace_buddies[1].index = index1;
|
|
||||||
m_brace_buddies[0].span_backup = buddy0;
|
|
||||||
m_brace_buddies[1].span_backup = buddy1;
|
|
||||||
buddy0.background_color = Color::DarkCyan;
|
|
||||||
buddy1.background_color = Color::DarkCyan;
|
|
||||||
buddy0.color = Color::White;
|
|
||||||
buddy1.color = Color::White;
|
|
||||||
update();
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MatchingTokenPair {
|
|
||||||
CppToken::Type open;
|
|
||||||
CppToken::Type close;
|
|
||||||
};
|
|
||||||
|
|
||||||
MatchingTokenPair pairs[] = {
|
|
||||||
{ CppToken::Type::LeftCurly, CppToken::Type::RightCurly },
|
|
||||||
{ CppToken::Type::LeftParen, CppToken::Type::RightParen },
|
|
||||||
{ CppToken::Type::LeftBracket, CppToken::Type::RightBracket },
|
|
||||||
};
|
|
||||||
|
|
||||||
for (int i = 0; i < document().spans().size(); ++i) {
|
|
||||||
auto& span = const_cast<GUI::TextDocumentSpan&>(document().spans().at(i));
|
|
||||||
auto token_type = (CppToken::Type)((uintptr_t)span.data);
|
|
||||||
|
|
||||||
for (auto& pair : pairs) {
|
|
||||||
if (token_type == pair.open && span.range.start() == cursor()) {
|
|
||||||
auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward);
|
|
||||||
if (buddy != -1)
|
|
||||||
make_buddies(i, buddy);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto right_of_end = span.range.end();
|
|
||||||
right_of_end.set_column(right_of_end.column() + 1);
|
|
||||||
|
|
||||||
for (auto& pair : pairs) {
|
|
||||||
if (token_type == pair.close && right_of_end == cursor()) {
|
|
||||||
auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward);
|
|
||||||
if (buddy != -1)
|
|
||||||
make_buddies(i, buddy);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::cursor_did_change()
|
|
||||||
{
|
|
||||||
if (m_has_brace_buddies) {
|
|
||||||
if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < document().spans().size())
|
|
||||||
document().set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
|
|
||||||
if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < document().spans().size())
|
|
||||||
document().set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
|
|
||||||
m_has_brace_buddies = false;
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
highlight_matching_token_pair();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::notify_did_rehighlight()
|
|
||||||
{
|
|
||||||
m_has_brace_buddies = false;
|
|
||||||
highlight_matching_token_pair();
|
|
||||||
}
|
|
||||||
|
|
|
@ -41,29 +41,17 @@ public:
|
||||||
EditorWrapper& wrapper();
|
EditorWrapper& wrapper();
|
||||||
const EditorWrapper& wrapper() const;
|
const EditorWrapper& wrapper() const;
|
||||||
|
|
||||||
void notify_did_rehighlight();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void focusin_event(Core::Event&) override;
|
virtual void focusin_event(Core::Event&) override;
|
||||||
virtual void focusout_event(Core::Event&) override;
|
virtual void focusout_event(Core::Event&) override;
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
virtual void mousemove_event(GUI::MouseEvent&) override;
|
virtual void mousemove_event(GUI::MouseEvent&) override;
|
||||||
virtual void cursor_did_change() override;
|
|
||||||
|
|
||||||
void show_documentation_tooltip_if_available(const String&, const Gfx::Point& screen_location);
|
void show_documentation_tooltip_if_available(const String&, const Gfx::Point& screen_location);
|
||||||
void highlight_matching_token_pair();
|
|
||||||
|
|
||||||
explicit Editor(GUI::Widget* parent);
|
explicit Editor(GUI::Widget* parent);
|
||||||
|
|
||||||
RefPtr<GUI::Window> m_documentation_tooltip_window;
|
RefPtr<GUI::Window> m_documentation_tooltip_window;
|
||||||
RefPtr<HtmlView> m_documentation_html_view;
|
RefPtr<HtmlView> m_documentation_html_view;
|
||||||
String m_last_parsed_token;
|
String m_last_parsed_token;
|
||||||
|
|
||||||
struct BuddySpan {
|
|
||||||
int index { -1 };
|
|
||||||
GUI::TextDocumentSpan span_backup;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool m_has_brace_buddies { false };
|
|
||||||
BuddySpan m_brace_buddies[2];
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,6 @@ OBJS = \
|
||||||
ProcessStateWidget.o \
|
ProcessStateWidget.o \
|
||||||
FormEditorWidget.o \
|
FormEditorWidget.o \
|
||||||
FormWidget.o \
|
FormWidget.o \
|
||||||
CppLexer.o \
|
|
||||||
Editor.o \
|
Editor.o \
|
||||||
EditorWrapper.o \
|
EditorWrapper.o \
|
||||||
Locator.o \
|
Locator.o \
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "CppLexer.h"
|
|
||||||
#include "CursorTool.h"
|
#include "CursorTool.h"
|
||||||
#include "Editor.h"
|
#include "Editor.h"
|
||||||
#include "EditorWrapper.h"
|
#include "EditorWrapper.h"
|
||||||
|
@ -44,6 +43,7 @@
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
|
#include <LibGUI/CppSyntaxHighlighter.h>
|
||||||
#include <LibGUI/FilePicker.h>
|
#include <LibGUI/FilePicker.h>
|
||||||
#include <LibGUI/InputBox.h>
|
#include <LibGUI/InputBox.h>
|
||||||
#include <LibGUI/Label.h>
|
#include <LibGUI/Label.h>
|
||||||
|
@ -548,70 +548,13 @@ void run(TerminalWrapper& wrapper)
|
||||||
wrapper.run_command("make run");
|
wrapper.run_command("make run");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TextStyle {
|
|
||||||
Color color;
|
|
||||||
const Gfx::Font* font { nullptr };
|
|
||||||
};
|
|
||||||
|
|
||||||
static TextStyle style_for_token_type(CppToken::Type type)
|
|
||||||
{
|
|
||||||
switch (type) {
|
|
||||||
case CppToken::Type::Keyword:
|
|
||||||
return { Color::Black, &Gfx::Font::default_bold_fixed_width_font() };
|
|
||||||
case CppToken::Type::KnownType:
|
|
||||||
return { Color::from_rgb(0x929200), &Gfx::Font::default_bold_fixed_width_font() };
|
|
||||||
case CppToken::Type::Identifier:
|
|
||||||
return { Color::from_rgb(0x000092) };
|
|
||||||
case CppToken::Type::DoubleQuotedString:
|
|
||||||
case CppToken::Type::SingleQuotedString:
|
|
||||||
case CppToken::Type::Number:
|
|
||||||
return { Color::from_rgb(0x920000) };
|
|
||||||
case CppToken::Type::PreprocessorStatement:
|
|
||||||
return { Color::from_rgb(0x009292) };
|
|
||||||
case CppToken::Type::Comment:
|
|
||||||
return { Color::from_rgb(0x009200) };
|
|
||||||
default:
|
|
||||||
return { Color::Black };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rehighlight()
|
|
||||||
{
|
|
||||||
auto text = current_editor().text();
|
|
||||||
CppLexer lexer(text);
|
|
||||||
auto tokens = lexer.lex();
|
|
||||||
|
|
||||||
Vector<GUI::TextDocumentSpan> spans;
|
|
||||||
for (auto& token : tokens) {
|
|
||||||
#ifdef DEBUG_SYNTAX_HIGHLIGHTING
|
|
||||||
dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
|
|
||||||
#endif
|
|
||||||
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(token.m_type);
|
|
||||||
span.color = style.color;
|
|
||||||
span.font = style.font;
|
|
||||||
span.is_skippable = token.m_type == CppToken::Type::Whitespace;
|
|
||||||
span.data = (void*)token.m_type;
|
|
||||||
spans.append(span);
|
|
||||||
}
|
|
||||||
current_editor().document().set_spans(spans);
|
|
||||||
static_cast<Editor&>(current_editor()).notify_did_rehighlight();
|
|
||||||
current_editor().update();
|
|
||||||
}
|
|
||||||
|
|
||||||
void open_file(const String& filename)
|
void open_file(const String& filename)
|
||||||
{
|
{
|
||||||
auto file = g_project->get_file(filename);
|
auto file = g_project->get_file(filename);
|
||||||
current_editor().set_document(const_cast<GUI::TextDocument&>(file->document()));
|
current_editor().set_document(const_cast<GUI::TextDocument&>(file->document()));
|
||||||
|
|
||||||
if (filename.ends_with(".cpp") || filename.ends_with(".h")) {
|
if (filename.ends_with(".cpp") || filename.ends_with(".h"))
|
||||||
current_editor().on_change = [] { rehighlight(); };
|
current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
|
||||||
rehighlight();
|
|
||||||
} else {
|
|
||||||
current_editor().on_change = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filename.ends_with(".frm")) {
|
if (filename.ends_with(".frm")) {
|
||||||
set_edit_mode(EditMode::Form);
|
set_edit_mode(EditMode::Form);
|
||||||
|
|
|
@ -29,6 +29,8 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
CppLexer::CppLexer(const StringView& input)
|
CppLexer::CppLexer(const StringView& input)
|
||||||
: m_input(input)
|
: m_input(input)
|
||||||
{
|
{
|
||||||
|
@ -368,3 +370,5 @@ Vector<CppToken> CppLexer::lex()
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -29,6 +29,8 @@
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
#define FOR_EACH_TOKEN_TYPE \
|
#define FOR_EACH_TOKEN_TYPE \
|
||||||
__TOKEN(Unknown) \
|
__TOKEN(Unknown) \
|
||||||
__TOKEN(Whitespace) \
|
__TOKEN(Whitespace) \
|
||||||
|
@ -94,3 +96,5 @@ private:
|
||||||
CppPosition m_previous_position { 0, 0 };
|
CppPosition m_previous_position { 0, 0 };
|
||||||
CppPosition m_position { 0, 0 };
|
CppPosition m_position { 0, 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
147
Libraries/LibGUI/CppSyntaxHighlighter.cpp
Normal file
147
Libraries/LibGUI/CppSyntaxHighlighter.cpp
Normal file
|
@ -0,0 +1,147 @@
|
||||||
|
#include <LibGUI/CppLexer.h>
|
||||||
|
#include <LibGUI/CppSyntaxHighlighter.h>
|
||||||
|
#include <LibGUI/TextEditor.h>
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
|
struct TextStyle {
|
||||||
|
Color color;
|
||||||
|
const Gfx::Font* font { nullptr };
|
||||||
|
};
|
||||||
|
|
||||||
|
static TextStyle style_for_token_type(CppToken::Type type)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case CppToken::Type::Keyword:
|
||||||
|
return { Color::Black, &Gfx::Font::default_bold_fixed_width_font() };
|
||||||
|
case CppToken::Type::KnownType:
|
||||||
|
return { Color::from_rgb(0x929200), &Gfx::Font::default_bold_fixed_width_font() };
|
||||||
|
case CppToken::Type::Identifier:
|
||||||
|
return { Color::from_rgb(0x000092) };
|
||||||
|
case CppToken::Type::DoubleQuotedString:
|
||||||
|
case CppToken::Type::SingleQuotedString:
|
||||||
|
case CppToken::Type::Number:
|
||||||
|
return { Color::from_rgb(0x920000) };
|
||||||
|
case CppToken::Type::PreprocessorStatement:
|
||||||
|
return { Color::from_rgb(0x009292) };
|
||||||
|
case CppToken::Type::Comment:
|
||||||
|
return { Color::from_rgb(0x009200) };
|
||||||
|
default:
|
||||||
|
return { Color::Black };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CppSyntaxHighlighter::rehighlight()
|
||||||
|
{
|
||||||
|
ASSERT(m_editor);
|
||||||
|
auto text = m_editor->text();
|
||||||
|
CppLexer lexer(text);
|
||||||
|
auto tokens = lexer.lex();
|
||||||
|
|
||||||
|
Vector<GUI::TextDocumentSpan> spans;
|
||||||
|
for (auto& token : tokens) {
|
||||||
|
#ifdef DEBUG_SYNTAX_HIGHLIGHTING
|
||||||
|
dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
|
||||||
|
#endif
|
||||||
|
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(token.m_type);
|
||||||
|
span.color = style.color;
|
||||||
|
span.font = style.font;
|
||||||
|
span.is_skippable = token.m_type == CppToken::Type::Whitespace;
|
||||||
|
span.data = (void*)token.m_type;
|
||||||
|
spans.append(span);
|
||||||
|
}
|
||||||
|
m_editor->document().set_spans(spans);
|
||||||
|
|
||||||
|
m_has_brace_buddies = false;
|
||||||
|
highlight_matching_token_pair();
|
||||||
|
|
||||||
|
m_editor->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CppSyntaxHighlighter::highlight_matching_token_pair()
|
||||||
|
{
|
||||||
|
ASSERT(m_editor);
|
||||||
|
auto& document = m_editor->document();
|
||||||
|
|
||||||
|
enum class Direction {
|
||||||
|
Forward,
|
||||||
|
Backward,
|
||||||
|
};
|
||||||
|
|
||||||
|
auto find_span_of_type = [&](int i, CppToken::Type type, CppToken::Type not_type, Direction direction) {
|
||||||
|
int nesting_level = 0;
|
||||||
|
bool forward = direction == Direction::Forward;
|
||||||
|
for (forward ? ++i : --i; forward ? (i < document.spans().size()) : (i >= 0); forward ? ++i : --i) {
|
||||||
|
auto& span = document.spans().at(i);
|
||||||
|
auto span_token_type = (CppToken::Type)((uintptr_t)span.data);
|
||||||
|
if (span_token_type == not_type) {
|
||||||
|
++nesting_level;
|
||||||
|
} else if (span_token_type == type) {
|
||||||
|
if (nesting_level-- <= 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto make_buddies = [&](int index0, int index1) {
|
||||||
|
auto& buddy0 = const_cast<GUI::TextDocumentSpan&>(document.spans()[index0]);
|
||||||
|
auto& buddy1 = const_cast<GUI::TextDocumentSpan&>(document.spans()[index1]);
|
||||||
|
m_has_brace_buddies = true;
|
||||||
|
m_brace_buddies[0].index = index0;
|
||||||
|
m_brace_buddies[1].index = index1;
|
||||||
|
m_brace_buddies[0].span_backup = buddy0;
|
||||||
|
m_brace_buddies[1].span_backup = buddy1;
|
||||||
|
buddy0.background_color = Color::DarkCyan;
|
||||||
|
buddy1.background_color = Color::DarkCyan;
|
||||||
|
buddy0.color = Color::White;
|
||||||
|
buddy1.color = Color::White;
|
||||||
|
m_editor->update();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MatchingTokenPair {
|
||||||
|
CppToken::Type open;
|
||||||
|
CppToken::Type close;
|
||||||
|
};
|
||||||
|
|
||||||
|
MatchingTokenPair pairs[] = {
|
||||||
|
{ CppToken::Type::LeftCurly, CppToken::Type::RightCurly },
|
||||||
|
{ CppToken::Type::LeftParen, CppToken::Type::RightParen },
|
||||||
|
{ CppToken::Type::LeftBracket, CppToken::Type::RightBracket },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < document.spans().size(); ++i) {
|
||||||
|
auto& span = const_cast<GUI::TextDocumentSpan&>(document.spans().at(i));
|
||||||
|
auto token_type = (CppToken::Type)((uintptr_t)span.data);
|
||||||
|
|
||||||
|
for (auto& pair : pairs) {
|
||||||
|
if (token_type == pair.open && span.range.start() == m_editor->cursor()) {
|
||||||
|
auto buddy = find_span_of_type(i, pair.close, pair.open, Direction::Forward);
|
||||||
|
if (buddy != -1)
|
||||||
|
make_buddies(i, buddy);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto right_of_end = span.range.end();
|
||||||
|
right_of_end.set_column(right_of_end.column() + 1);
|
||||||
|
|
||||||
|
for (auto& pair : pairs) {
|
||||||
|
if (token_type == pair.close && right_of_end == m_editor->cursor()) {
|
||||||
|
auto buddy = find_span_of_type(i, pair.open, pair.close, Direction::Backward);
|
||||||
|
if (buddy != -1)
|
||||||
|
make_buddies(i, buddy);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CppSyntaxHighlighter::~CppSyntaxHighlighter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
16
Libraries/LibGUI/CppSyntaxHighlighter.h
Normal file
16
Libraries/LibGUI/CppSyntaxHighlighter.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibGUI/SyntaxHighlighter.h>
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
|
class CppSyntaxHighlighter final : public SyntaxHighlighter {
|
||||||
|
public:
|
||||||
|
CppSyntaxHighlighter() {}
|
||||||
|
|
||||||
|
virtual ~CppSyntaxHighlighter() override;
|
||||||
|
virtual void rehighlight() override;
|
||||||
|
virtual void highlight_matching_token_pair() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -14,6 +14,8 @@ OBJS = \
|
||||||
ColumnsView.o \
|
ColumnsView.o \
|
||||||
ComboBox.o \
|
ComboBox.o \
|
||||||
Command.o \
|
Command.o \
|
||||||
|
CppLexer.o \
|
||||||
|
CppSyntaxHighlighter.o \
|
||||||
Desktop.o \
|
Desktop.o \
|
||||||
Dialog.o \
|
Dialog.o \
|
||||||
DragOperation.o \
|
DragOperation.o \
|
||||||
|
@ -49,6 +51,7 @@ OBJS = \
|
||||||
Splitter.o \
|
Splitter.o \
|
||||||
StackWidget.o \
|
StackWidget.o \
|
||||||
StatusBar.o \
|
StatusBar.o \
|
||||||
|
SyntaxHighlighter.o \
|
||||||
TabWidget.o \
|
TabWidget.o \
|
||||||
TableView.o \
|
TableView.o \
|
||||||
TextBox.o \
|
TextBox.o \
|
||||||
|
|
37
Libraries/LibGUI/SyntaxHighlighter.cpp
Normal file
37
Libraries/LibGUI/SyntaxHighlighter.cpp
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#include <LibGUI/SyntaxHighlighter.h>
|
||||||
|
#include <LibGUI/TextEditor.h>
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
|
SyntaxHighlighter::~SyntaxHighlighter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void SyntaxHighlighter::attach(TextEditor& editor)
|
||||||
|
{
|
||||||
|
ASSERT(!m_editor);
|
||||||
|
m_editor = editor.make_weak_ptr();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SyntaxHighlighter::detach()
|
||||||
|
{
|
||||||
|
ASSERT(m_editor);
|
||||||
|
m_editor = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SyntaxHighlighter::cursor_did_change()
|
||||||
|
{
|
||||||
|
ASSERT(m_editor);
|
||||||
|
auto& document = m_editor->document();
|
||||||
|
if (m_has_brace_buddies) {
|
||||||
|
if (m_brace_buddies[0].index >= 0 && m_brace_buddies[0].index < document.spans().size())
|
||||||
|
document.set_span_at_index(m_brace_buddies[0].index, m_brace_buddies[0].span_backup);
|
||||||
|
if (m_brace_buddies[1].index >= 0 && m_brace_buddies[1].index < document.spans().size())
|
||||||
|
document.set_span_at_index(m_brace_buddies[1].index, m_brace_buddies[1].span_backup);
|
||||||
|
m_has_brace_buddies = false;
|
||||||
|
m_editor->update();
|
||||||
|
}
|
||||||
|
highlight_matching_token_pair();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
Libraries/LibGUI/SyntaxHighlighter.h
Normal file
39
Libraries/LibGUI/SyntaxHighlighter.h
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Noncopyable.h>
|
||||||
|
#include <AK/WeakPtr.h>
|
||||||
|
#include <LibGUI/TextDocument.h>
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
|
class TextEditor;
|
||||||
|
|
||||||
|
class SyntaxHighlighter {
|
||||||
|
AK_MAKE_NONCOPYABLE(SyntaxHighlighter);
|
||||||
|
AK_MAKE_NONMOVABLE(SyntaxHighlighter);
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual ~SyntaxHighlighter();
|
||||||
|
|
||||||
|
virtual void rehighlight() = 0;
|
||||||
|
virtual void highlight_matching_token_pair() = 0;
|
||||||
|
|
||||||
|
void attach(TextEditor& editor);
|
||||||
|
void detach();
|
||||||
|
void cursor_did_change();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
SyntaxHighlighter() {}
|
||||||
|
|
||||||
|
WeakPtr<TextEditor> m_editor;
|
||||||
|
|
||||||
|
struct BuddySpan {
|
||||||
|
int index { -1 };
|
||||||
|
GUI::TextDocumentSpan span_backup;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool m_has_brace_buddies { false };
|
||||||
|
BuddySpan m_brace_buddies[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -27,7 +27,6 @@
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <Kernel/KeyCode.h>
|
#include <Kernel/KeyCode.h>
|
||||||
#include <LibGfx/Palette.h>
|
|
||||||
#include <LibGUI/Action.h>
|
#include <LibGUI/Action.h>
|
||||||
#include <LibGUI/Clipboard.h>
|
#include <LibGUI/Clipboard.h>
|
||||||
#include <LibGUI/FontDatabase.h>
|
#include <LibGUI/FontDatabase.h>
|
||||||
|
@ -35,8 +34,10 @@
|
||||||
#include <LibGUI/Menu.h>
|
#include <LibGUI/Menu.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGUI/ScrollBar.h>
|
#include <LibGUI/ScrollBar.h>
|
||||||
|
#include <LibGUI/SyntaxHighlighter.h>
|
||||||
#include <LibGUI/TextEditor.h>
|
#include <LibGUI/TextEditor.h>
|
||||||
#include <LibGUI/Window.h>
|
#include <LibGUI/Window.h>
|
||||||
|
#include <LibGfx/Palette.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -1030,6 +1031,8 @@ void TextEditor::set_cursor(const TextPosition& a_position)
|
||||||
cursor_did_change();
|
cursor_did_change();
|
||||||
if (on_cursor_change)
|
if (on_cursor_change)
|
||||||
on_cursor_change();
|
on_cursor_change();
|
||||||
|
if (m_highlighter)
|
||||||
|
m_highlighter->cursor_did_change();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditor::focusin_event(Core::Event&)
|
void TextEditor::focusin_event(Core::Event&)
|
||||||
|
@ -1198,6 +1201,8 @@ void TextEditor::did_change()
|
||||||
return;
|
return;
|
||||||
if (on_change)
|
if (on_change)
|
||||||
on_change();
|
on_change();
|
||||||
|
if (m_highlighter)
|
||||||
|
m_highlighter->rehighlight();
|
||||||
m_has_pending_change_notification = false;
|
m_has_pending_change_notification = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1476,7 +1481,20 @@ void TextEditor::flush_pending_change_notification_if_needed()
|
||||||
return;
|
return;
|
||||||
if (on_change)
|
if (on_change)
|
||||||
on_change();
|
on_change();
|
||||||
|
if (m_highlighter)
|
||||||
|
m_highlighter->rehighlight();
|
||||||
m_has_pending_change_notification = false;
|
m_has_pending_change_notification = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
|
||||||
|
{
|
||||||
|
if (m_highlighter)
|
||||||
|
m_highlighter->detach();
|
||||||
|
m_highlighter = move(highlighter);
|
||||||
|
if (m_highlighter) {
|
||||||
|
m_highlighter->attach(*this);
|
||||||
|
m_highlighter->rehighlight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ class Action;
|
||||||
class Menu;
|
class Menu;
|
||||||
class Painter;
|
class Painter;
|
||||||
class ScrollBar;
|
class ScrollBar;
|
||||||
|
class SyntaxHighlighter;
|
||||||
|
|
||||||
class TextEditor
|
class TextEditor
|
||||||
: public ScrollableWidget
|
: public ScrollableWidget
|
||||||
|
@ -133,6 +134,8 @@ public:
|
||||||
void set_cursor(size_t line, size_t column);
|
void set_cursor(size_t line, size_t column);
|
||||||
void set_cursor(const TextPosition&);
|
void set_cursor(const TextPosition&);
|
||||||
|
|
||||||
|
void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit TextEditor(Widget* parent);
|
explicit TextEditor(Widget* parent);
|
||||||
explicit TextEditor(Type, Widget* parent);
|
explicit TextEditor(Type, Widget* parent);
|
||||||
|
@ -248,6 +251,8 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
|
NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
|
||||||
|
|
||||||
|
OwnPtr<SyntaxHighlighter> m_highlighter;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue