Spreadsheet+LibSyntax: Never insert spans directly
Function `CellSyntaxHighlighter::rehighlight()` direct inserted spans to TextDocument `m_span` vector missing out important reordering and merging operation carried out by `TextDocument::set_spans()`. This caused overlapping spans for a cell with only a `=` symbol (one for the actual token and one for the highlighting) to miscalculate `start` and `end` value and a `length` value (of `size_t` type) with a `0-1` substraction (result: 18446744073709551615) causing `Utf32View::substring_view()` to fail the `!Checked<size_t>::addition_would_overflow(offset, length)` assertion This remove the possibility to directly alter `TextDocument`'s spans thus forcing the utilization of `HighlighterClient::do_set_spans()` interface function. Proper refactor have been applied to `CellSyntaxHighlighter::rehighlight()` function
This commit is contained in:
parent
fb47b988ca
commit
3e1626acdc
Notes:
sideshowbarker
2024-07-17 07:09:53 +09:00
Author: https://github.com/fernetmatt Commit: https://github.com/SerenityOS/serenity/commit/3e1626acdc Pull-request: https://github.com/SerenityOS/serenity/pull/18345 Issue: https://github.com/SerenityOS/serenity/issues/18164 Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/alimpfard ✅ Reviewed-by: https://github.com/fdellwing ✅
4 changed files with 31 additions and 30 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2022, the SerenityOS developers.
|
||||
* Copyright (c) 2023, Matteo benetti <matteo.benetti@proton.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -13,44 +14,46 @@ namespace Spreadsheet {
|
|||
|
||||
void CellSyntaxHighlighter::rehighlight(Palette const& palette)
|
||||
{
|
||||
auto text = m_client->get_text();
|
||||
m_client->spans().clear();
|
||||
if (!text.starts_with('=')) {
|
||||
m_client->do_update();
|
||||
return;
|
||||
}
|
||||
m_client->clear_spans();
|
||||
|
||||
JS::SyntaxHighlighter::rehighlight(palette);
|
||||
if (m_client->get_text().starts_with('=')) {
|
||||
|
||||
// Highlight the '='
|
||||
m_client->spans().empend(
|
||||
GUI::TextRange { { 0, 0 }, { 0, 1 } },
|
||||
Gfx::TextAttributes {
|
||||
palette.syntax_keyword(),
|
||||
Optional<Color> {},
|
||||
false,
|
||||
},
|
||||
(u64)-1,
|
||||
false);
|
||||
JS::SyntaxHighlighter::rehighlight(palette);
|
||||
|
||||
if (m_cell && m_cell->thrown_value().has_value()) {
|
||||
if (auto value = m_cell->thrown_value().value(); value.is_object() && is<JS::Error>(value.as_object())) {
|
||||
auto& error = static_cast<JS::Error const&>(value.as_object());
|
||||
auto& traceback = error.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 } };
|
||||
m_client->spans().prepend(
|
||||
GUI::TextDocumentSpan {
|
||||
text_range,
|
||||
auto spans = m_client->spans();
|
||||
|
||||
// Highlight the '='
|
||||
spans.empend(
|
||||
GUI::TextRange { { 0, 0 }, { 0, 1 } },
|
||||
Gfx::TextAttributes {
|
||||
palette.syntax_keyword(),
|
||||
Optional<Color> {},
|
||||
false,
|
||||
},
|
||||
(u64)-1,
|
||||
false);
|
||||
|
||||
if (m_cell && m_cell->thrown_value().has_value()) {
|
||||
if (auto value = m_cell->thrown_value().value(); value.is_object() && is<JS::Error>(value.as_object())) {
|
||||
auto& error = static_cast<JS::Error const&>(value.as_object());
|
||||
auto& range = error.traceback().first().source_range;
|
||||
|
||||
spans.prepend({
|
||||
GUI::TextRange { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column } },
|
||||
Gfx::TextAttributes {
|
||||
Color::Black,
|
||||
Color::Red,
|
||||
false,
|
||||
},
|
||||
(u64)-1,
|
||||
false });
|
||||
false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
m_client->do_set_spans(move(spans));
|
||||
}
|
||||
|
||||
m_client->do_update();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -306,7 +306,6 @@ private:
|
|||
virtual void document_did_update_undo_stack() override;
|
||||
|
||||
// ^Syntax::HighlighterClient
|
||||
virtual Vector<TextDocumentSpan>& spans() final { return document().spans(); }
|
||||
virtual Vector<TextDocumentSpan> const& spans() const final { return document().spans(); }
|
||||
virtual void set_span_at_index(size_t index, TextDocumentSpan span) final { document().set_span_at_index(index, move(span)); }
|
||||
virtual Vector<GUI::TextDocumentFoldingRegion>& folding_regions() final { return document().folding_regions(); };
|
||||
|
|
|
@ -125,7 +125,6 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
virtual Vector<GUI::TextDocumentSpan>& spans() override { return m_spans; }
|
||||
virtual Vector<GUI::TextDocumentSpan> const& spans() const override { return m_spans; }
|
||||
virtual void set_span_at_index(size_t index, GUI::TextDocumentSpan span) override { m_spans.at(index) = move(span); }
|
||||
|
||||
|
|
|
@ -17,9 +17,9 @@ class HighlighterClient {
|
|||
public:
|
||||
virtual ~HighlighterClient() = default;
|
||||
|
||||
virtual Vector<GUI::TextDocumentSpan>& spans() = 0;
|
||||
virtual Vector<GUI::TextDocumentSpan> const& spans() const = 0;
|
||||
virtual void set_span_at_index(size_t index, GUI::TextDocumentSpan span) = 0;
|
||||
virtual void clear_spans() { do_set_spans({}); };
|
||||
|
||||
virtual Vector<GUI::TextDocumentFoldingRegion>& folding_regions() = 0;
|
||||
virtual Vector<GUI::TextDocumentFoldingRegion> const& folding_regions() const = 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue