1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:58:11 +00:00
serenity/Userland/Applications/Spreadsheet/CellSyntaxHighlighter.cpp
Ali Mohammad Pur 71b4433b0d LibWeb+LibSyntax: Implement nested syntax highlighters
And use them to highlight javascript in HTML source.
This commit also changes how TextDocumentSpan::data is interpreted,
as it used to be an opaque pointer, but everyone stuffed an enum value
inside it, which made the values not unique to each highlighter;
that field is now a u64 serial id.
The syntax highlighters don't need to change their ways of stuffing
token types into that field, but a highlighter that calls another
nested highlighter needs to register the nested types for use with
token pairs.
2021-06-07 14:45:49 +04:30

60 lines
1.5 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CellSyntaxHighlighter.h"
#include <LibGUI/TextEditor.h>
#include <LibGfx/Palette.h>
#include <LibJS/Lexer.h>
namespace Spreadsheet {
void CellSyntaxHighlighter::rehighlight(const Palette& palette)
{
auto text = m_client->get_text();
m_client->spans().clear();
if (!text.starts_with('=')) {
m_client->do_update();
return;
}
JS::SyntaxHighlighter::rehighlight(palette);
// Highlight the '='
m_client->spans().empend(
GUI::TextRange { { 0, 0 }, { 0, 1 } },
Gfx::TextAttributes {
palette.syntax_keyword(),
Optional<Color> {},
false,
false,
},
(u64)-1,
false);
if (m_cell && m_cell->exception()) {
auto& traceback = m_cell->exception()->traceback();
auto& range = traceback.first().source_range;
GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column - 1 } };
m_client->spans().prepend(
GUI::TextDocumentSpan {
text_range,
Gfx::TextAttributes {
Color::Black,
Color::Red,
false,
false,
},
(u64)-1,
false });
}
m_client->do_update();
}
CellSyntaxHighlighter::~CellSyntaxHighlighter()
{
}
}