1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:27:45 +00:00

LibGUI: Validate TextDocument spans when merging them, not when painting

TextDocument::merge_span_collections() automatically makes sure that the
spans are valid, move forwards, are in order, and do not overlap. This
means we don't have to check these things every time TextEditor paints
the document.

merge_span_collections() now does these checks instead. I am not certain
they are still useful, but someone in the past certainly did. I have
modified them to take advantage of the operator overloads and Formatter
that we now have.
This commit is contained in:
Sam Atkins 2023-02-20 16:00:31 +00:00 committed by Andreas Kling
parent 54d45d4ac6
commit 04deb81f71
2 changed files with 22 additions and 25 deletions

View file

@ -1,12 +1,14 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Badge.h>
#include <AK/CharacterTypes.h>
#include <AK/Debug.h>
#include <AK/QuickSort.h>
#include <AK/ScopeGuard.h>
#include <AK/StdLibExtras.h>
@ -1409,7 +1411,27 @@ void TextDocument::merge_span_collections()
}
m_spans.clear();
TextDocumentSpan previous_span { .range = { TextPosition(0, 0), TextPosition(0, 0) }, .attributes = {} };
for (auto span : merged_spans) {
// Validate spans
if (!span.span.range.is_valid()) {
dbgln_if(TEXTEDITOR_DEBUG, "Invalid span {} => ignoring", span.span.range);
continue;
}
if (span.span.range.end() < span.span.range.start()) {
dbgln_if(TEXTEDITOR_DEBUG, "Span {} has negative length => ignoring", span.span.range);
continue;
}
if (span.span.range.end() < previous_span.range.start()) {
dbgln_if(TEXTEDITOR_DEBUG, "Spans not sorted (Span {} ends before previous span {}) => ignoring", span.span.range, previous_span.range);
continue;
}
if (span.span.range.start() < previous_span.range.end()) {
dbgln_if(TEXTEDITOR_DEBUG, "Span {} overlaps previous span {} => ignoring", span.span.range, previous_span.range);
continue;
}
previous_span = span.span;
m_spans.append(move(span.span));
}
}