Document.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/Utf32View.h>
  9. #include <LibGfx/TextAttributes.h>
  10. #include <LibSyntax/Forward.h>
  11. #include <LibSyntax/TextRange.h>
  12. namespace Syntax {
  13. struct TextDocumentSpan {
  14. TextRange range;
  15. Gfx::TextAttributes attributes;
  16. u64 data { 0 };
  17. bool is_skippable { false };
  18. };
  19. struct TextDocumentFoldingRegion {
  20. TextRange range;
  21. bool is_folded { false };
  22. // This pointer is only used to identify that two TDFRs are the same.
  23. RawPtr<class TextDocumentLine> line_ptr;
  24. };
  25. class TextDocumentLine {
  26. public:
  27. explicit TextDocumentLine(Document&);
  28. explicit TextDocumentLine(Document&, StringView);
  29. ByteString to_utf8() const;
  30. Utf32View view() const { return { code_points(), length() }; }
  31. u32 const* code_points() const { return m_text.data(); }
  32. size_t length() const { return m_text.size(); }
  33. bool set_text(Document&, StringView);
  34. void set_text(Document&, Vector<u32>);
  35. void append(Document&, u32);
  36. void prepend(Document&, u32);
  37. void insert(Document&, size_t index, u32);
  38. void remove(Document&, size_t index);
  39. void append(Document&, u32 const*, size_t);
  40. void truncate(Document&, size_t length);
  41. void clear(Document&);
  42. void remove_range(Document&, size_t start, size_t length);
  43. void keep_range(Document&, size_t start_index, size_t end_index);
  44. size_t first_non_whitespace_column() const;
  45. Optional<size_t> last_non_whitespace_column() const;
  46. bool ends_in_whitespace() const;
  47. bool can_select() const;
  48. bool is_empty() const { return length() == 0; }
  49. size_t leading_spaces() const;
  50. private:
  51. // NOTE: This vector is null terminated.
  52. Vector<u32> m_text;
  53. };
  54. class Document : public RefCounted<Document> {
  55. public:
  56. Document() = default;
  57. virtual ~Document() = default;
  58. void set_spans(u32 span_collection_index, Vector<TextDocumentSpan> spans);
  59. bool has_spans() const { return !m_spans.is_empty(); }
  60. Vector<TextDocumentSpan>& spans() { return m_spans; }
  61. Vector<TextDocumentSpan> const& spans() const { return m_spans; }
  62. void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[index] = move(span); }
  63. TextDocumentSpan const* span_at(TextPosition const&) const;
  64. void set_folding_regions(Vector<TextDocumentFoldingRegion>);
  65. bool has_folding_regions() const { return !m_folding_regions.is_empty(); }
  66. Vector<TextDocumentFoldingRegion>& folding_regions() { return m_folding_regions; }
  67. Vector<TextDocumentFoldingRegion> const& folding_regions() const { return m_folding_regions; }
  68. Optional<TextDocumentFoldingRegion&> folding_region_starting_on_line(size_t line);
  69. // Returns all folded FoldingRegions that are not contained inside another folded region.
  70. Vector<TextDocumentFoldingRegion const&> currently_folded_regions() const;
  71. // Returns true if any part of the line is currently visible. (Not inside a folded FoldingRegion.)
  72. bool line_is_visible(size_t line) const;
  73. virtual TextDocumentLine const& line(size_t line_index) const = 0;
  74. virtual TextDocumentLine& line(size_t line_index) = 0;
  75. virtual void update_views(Badge<TextDocumentLine>) = 0;
  76. protected:
  77. HashMap<u32, Vector<TextDocumentSpan>> m_span_collections;
  78. Vector<TextDocumentSpan> m_spans;
  79. Vector<TextDocumentFoldingRegion> m_folding_regions;
  80. private:
  81. void merge_span_collections();
  82. };
  83. }