ladybird/Userland/Libraries/LibSyntax/HighlighterClient.h
Matteo Benetti 3e1626acdc 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
2023-04-14 10:00:52 +02:00

45 lines
1.8 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/Vector.h>
#include <LibGUI/TextDocument.h>
#include <LibGUI/TextPosition.h>
namespace Syntax {
class HighlighterClient {
public:
virtual ~HighlighterClient() = default;
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;
virtual DeprecatedString highlighter_did_request_text() const = 0;
virtual void highlighter_did_request_update() = 0;
virtual GUI::TextDocument& highlighter_did_request_document() = 0;
virtual GUI::TextPosition highlighter_did_request_cursor() const = 0;
virtual void highlighter_did_set_spans(Vector<GUI::TextDocumentSpan>) = 0;
virtual void highlighter_did_set_folding_regions(Vector<GUI::TextDocumentFoldingRegion>) = 0;
void do_set_spans(Vector<GUI::TextDocumentSpan> spans) { highlighter_did_set_spans(move(spans)); }
void do_set_folding_regions(Vector<GUI::TextDocumentFoldingRegion> folding_regions) { highlighter_did_set_folding_regions(move(folding_regions)); }
void do_update() { highlighter_did_request_update(); }
DeprecatedString get_text() const { return highlighter_did_request_text(); }
GUI::TextDocument& get_document() { return highlighter_did_request_document(); }
GUI::TextPosition get_cursor() const { return highlighter_did_request_cursor(); }
static constexpr auto span_collection_index = 0;
};
}