TextDocument.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/HashTable.h>
  29. #include <AK/NonnullOwnPtrVector.h>
  30. #include <AK/NonnullRefPtr.h>
  31. #include <AK/RefCounted.h>
  32. #include <LibCore/Timer.h>
  33. #include <LibGfx/Color.h>
  34. #include <LibGfx/Font.h>
  35. #include <LibGUI/TextRange.h>
  36. #include <LibGUI/UndoStack.h>
  37. namespace GUI {
  38. class TextDocument;
  39. class TextDocumentLine;
  40. class TextDocumentUndoCommand;
  41. class TextEditor;
  42. struct TextDocumentSpan {
  43. TextRange range;
  44. Color color;
  45. Optional<Color> background_color;
  46. bool is_skippable { false };
  47. const Gfx::Font* font { nullptr };
  48. void* data { nullptr };
  49. };
  50. class TextDocument : public RefCounted<TextDocument> {
  51. public:
  52. enum class SearchShouldWrap {
  53. No = 0,
  54. Yes
  55. };
  56. class Client {
  57. public:
  58. virtual ~Client();
  59. virtual void document_did_append_line() = 0;
  60. virtual void document_did_insert_line(size_t) = 0;
  61. virtual void document_did_remove_line(size_t) = 0;
  62. virtual void document_did_remove_all_lines() = 0;
  63. virtual void document_did_change() = 0;
  64. virtual void document_did_set_text() = 0;
  65. virtual void document_did_set_cursor(const TextPosition&) = 0;
  66. virtual bool is_automatic_indentation_enabled() const = 0;
  67. virtual int soft_tab_width() const = 0;
  68. };
  69. static NonnullRefPtr<TextDocument> create(Client* client = nullptr)
  70. {
  71. return adopt(*new TextDocument(client));
  72. }
  73. size_t line_count() const { return (size_t)m_lines.size(); }
  74. const TextDocumentLine& line(size_t line_index) const { return m_lines[(int)line_index]; }
  75. TextDocumentLine& line(size_t line_index) { return m_lines[(int)line_index]; }
  76. void set_spans(const Vector<TextDocumentSpan>& spans) { m_spans = spans; }
  77. void set_text(const StringView&);
  78. const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return m_lines; }
  79. NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
  80. bool has_spans() const { return !m_spans.is_empty(); }
  81. const Vector<TextDocumentSpan>& spans() const { return m_spans; }
  82. void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[(int)index] = move(span); }
  83. void append_line(NonnullOwnPtr<TextDocumentLine>);
  84. void remove_line(size_t line_index);
  85. void remove_all_lines();
  86. void insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine>);
  87. void register_client(Client&);
  88. void unregister_client(Client&);
  89. void update_views(Badge<TextDocumentLine>);
  90. String text_in_range(const TextRange&) const;
  91. Vector<TextRange> find_all(const StringView& needle) const;
  92. TextRange find_next(const StringView&, const TextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes) const;
  93. TextRange find_previous(const StringView&, const TextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes) const;
  94. TextPosition next_position_after(const TextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
  95. TextPosition previous_position_before(const TextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
  96. char character_at(const TextPosition&) const;
  97. TextRange range_for_entire_line(size_t line_index) const;
  98. Optional<TextDocumentSpan> first_non_skippable_span_before(const TextPosition&) const;
  99. Optional<TextDocumentSpan> first_non_skippable_span_after(const TextPosition&) const;
  100. void add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand>);
  101. bool can_undo() const { return m_undo_stack.can_undo(); }
  102. bool can_redo() const { return m_undo_stack.can_redo(); }
  103. void undo();
  104. void redo();
  105. void notify_did_change();
  106. void set_all_cursors(const TextPosition&);
  107. TextPosition insert_at(const TextPosition&, char, const Client* = nullptr);
  108. TextPosition insert_at(const TextPosition&, const StringView&, const Client* = nullptr);
  109. void remove(const TextRange&);
  110. private:
  111. explicit TextDocument(Client* client);
  112. void update_undo_timer();
  113. NonnullOwnPtrVector<TextDocumentLine> m_lines;
  114. Vector<TextDocumentSpan> m_spans;
  115. HashTable<Client*> m_clients;
  116. bool m_client_notifications_enabled { true };
  117. UndoStack m_undo_stack;
  118. RefPtr<Core::Timer> m_undo_timer;
  119. };
  120. class TextDocumentLine {
  121. friend class GTextEditor;
  122. friend class TextDocument;
  123. public:
  124. explicit TextDocumentLine(TextDocument&);
  125. explicit TextDocumentLine(TextDocument&, const StringView&);
  126. StringView view() const { return { characters(), (size_t)length() }; }
  127. const char* characters() const { return m_text.data(); }
  128. size_t length() const { return (size_t)m_text.size() - 1; }
  129. void set_text(TextDocument&, const StringView&);
  130. void append(TextDocument&, char);
  131. void prepend(TextDocument&, char);
  132. void insert(TextDocument&, size_t index, char);
  133. void remove(TextDocument&, size_t index);
  134. void append(TextDocument&, const char*, size_t);
  135. void truncate(TextDocument&, size_t length);
  136. void clear(TextDocument&);
  137. size_t first_non_whitespace_column() const;
  138. private:
  139. // NOTE: This vector is null terminated.
  140. Vector<char> m_text;
  141. };
  142. class TextDocumentUndoCommand : public Command {
  143. public:
  144. TextDocumentUndoCommand(TextDocument&);
  145. virtual ~TextDocumentUndoCommand();
  146. void execute_from(const TextDocument::Client& client)
  147. {
  148. m_client = &client;
  149. redo();
  150. m_client = nullptr;
  151. }
  152. protected:
  153. TextDocument& m_document;
  154. const TextDocument::Client* m_client { nullptr };
  155. };
  156. class InsertTextCommand : public TextDocumentUndoCommand {
  157. public:
  158. InsertTextCommand(TextDocument&, const String&, const TextPosition&);
  159. virtual void undo() override;
  160. virtual void redo() override;
  161. private:
  162. String m_text;
  163. TextRange m_range;
  164. };
  165. class RemoveTextCommand : public TextDocumentUndoCommand {
  166. public:
  167. RemoveTextCommand(TextDocument&, const String&, const TextRange&);
  168. virtual void undo() override;
  169. virtual void redo() override;
  170. private:
  171. String m_text;
  172. TextRange m_range;
  173. };
  174. }