Serenity Operating System
1/*
2 * Copyright (c) 2020-2022, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "CellSyntaxHighlighter.h"
8#include <LibGUI/TextEditor.h>
9#include <LibGfx/Palette.h>
10#include <LibJS/Lexer.h>
11
12namespace Spreadsheet {
13
14void CellSyntaxHighlighter::rehighlight(Palette const& palette)
15{
16 auto text = m_client->get_text();
17 m_client->spans().clear();
18 if (!text.starts_with('=')) {
19 m_client->do_update();
20 return;
21 }
22
23 JS::SyntaxHighlighter::rehighlight(palette);
24
25 // Highlight the '='
26 m_client->spans().empend(
27 GUI::TextRange { { 0, 0 }, { 0, 1 } },
28 Gfx::TextAttributes {
29 palette.syntax_keyword(),
30 Optional<Color> {},
31 false,
32 false,
33 },
34 (u64)-1,
35 false);
36
37 if (m_cell && m_cell->thrown_value().has_value()) {
38 if (auto value = m_cell->thrown_value().value(); value.is_object() && is<JS::Error>(value.as_object())) {
39 auto& error = static_cast<JS::Error const&>(value.as_object());
40 auto& traceback = error.traceback();
41 auto& range = traceback.first().source_range;
42 GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column } };
43 m_client->spans().prepend(
44 GUI::TextDocumentSpan {
45 text_range,
46 Gfx::TextAttributes {
47 Color::Black,
48 Color::Red,
49 false,
50 false,
51 },
52 (u64)-1,
53 false });
54 }
55 }
56 m_client->do_update();
57}
58}