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

LibGUI: Add folding regions to TextDocument

A `TextDocumentFoldingRegion` represents a region of the document which
the user can collapse/expand to make code easier to navigate.
This commit is contained in:
Sam Atkins 2023-02-23 15:25:29 +00:00 committed by Andreas Kling
parent 96cf9355b2
commit 3d25b4eb34
2 changed files with 100 additions and 0 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2021, 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
*/
@ -35,6 +36,13 @@ struct TextDocumentSpan {
bool is_skippable { false };
};
struct TextDocumentFoldingRegion {
TextRange range;
bool is_folded { false };
// This pointer is only used to identify that two TDFRs are the same.
RawPtr<class TextDocumentLine> line_ptr;
};
class TextDocument : public RefCounted<TextDocument> {
public:
enum class SearchShouldWrap {
@ -79,6 +87,16 @@ public:
TextDocumentSpan const* span_at(TextPosition const&) const;
void set_folding_regions(Vector<TextDocumentFoldingRegion>);
bool has_folding_regions() const { return !m_folding_regions.is_empty(); }
Vector<TextDocumentFoldingRegion>& folding_regions() { return m_folding_regions; }
Vector<TextDocumentFoldingRegion> const& folding_regions() const { return m_folding_regions; }
Optional<TextDocumentFoldingRegion&> folding_region_starting_on_line(size_t line);
// Returns all folded FoldingRegions that are not contained inside another folded region.
Vector<TextDocumentFoldingRegion const&> currently_folded_regions() const;
// Returns true if any part of the line is currently visible. (Not inside a folded FoldingRegion.)
bool line_is_visible(size_t line) const;
void append_line(NonnullOwnPtr<TextDocumentLine>);
NonnullOwnPtr<TextDocumentLine> take_line(size_t line_index);
void remove_line(size_t line_index);
@ -148,6 +166,7 @@ private:
NonnullOwnPtrVector<TextDocumentLine> m_lines;
HashMap<u32, Vector<TextDocumentSpan>> m_span_collections;
Vector<TextDocumentSpan> m_spans;
Vector<TextDocumentFoldingRegion> m_folding_regions;
HashTable<Client*> m_clients;
bool m_client_notifications_enabled { true };