TextDocument.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/HashTable.h>
  28. #include <AK/NonnullOwnPtrVector.h>
  29. #include <AK/NonnullRefPtr.h>
  30. #include <AK/Optional.h>
  31. #include <AK/RefCounted.h>
  32. #include <AK/Utf32View.h>
  33. #include <LibCore/Forward.h>
  34. #include <LibGUI/Command.h>
  35. #include <LibGUI/Forward.h>
  36. #include <LibGUI/TextRange.h>
  37. #include <LibGUI/UndoStack.h>
  38. #include <LibGfx/Color.h>
  39. #include <LibGfx/Forward.h>
  40. #include <LibRegex/Regex.h>
  41. namespace GUI {
  42. struct TextDocumentSpan {
  43. TextRange range;
  44. Color color;
  45. Optional<Color> background_color;
  46. bool is_skippable { false };
  47. bool is_underlined { false };
  48. const Gfx::Font* font { nullptr };
  49. void* data { nullptr };
  50. };
  51. class TextDocument : public RefCounted<TextDocument> {
  52. public:
  53. enum class SearchShouldWrap {
  54. No = 0,
  55. Yes
  56. };
  57. class Client {
  58. public:
  59. virtual ~Client();
  60. virtual void document_did_append_line() = 0;
  61. virtual void document_did_insert_line(size_t) = 0;
  62. virtual void document_did_remove_line(size_t) = 0;
  63. virtual void document_did_remove_all_lines() = 0;
  64. virtual void document_did_change() = 0;
  65. virtual void document_did_set_text() = 0;
  66. virtual void document_did_set_cursor(const TextPosition&) = 0;
  67. virtual bool is_automatic_indentation_enabled() const = 0;
  68. virtual int soft_tab_width() const = 0;
  69. };
  70. static NonnullRefPtr<TextDocument> create(Client* client = nullptr);
  71. virtual ~TextDocument();
  72. size_t line_count() const { return m_lines.size(); }
  73. const TextDocumentLine& line(size_t line_index) const { return m_lines[line_index]; }
  74. TextDocumentLine& line(size_t line_index) { return m_lines[line_index]; }
  75. void set_spans(const Vector<TextDocumentSpan>& spans) { m_spans = spans; }
  76. void set_text(const StringView&);
  77. const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return m_lines; }
  78. NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
  79. bool has_spans() const { return !m_spans.is_empty(); }
  80. Vector<TextDocumentSpan>& spans() { return m_spans; }
  81. const Vector<TextDocumentSpan>& spans() const { return m_spans; }
  82. void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[index] = move(span); }
  83. const TextDocumentSpan* span_at(const TextPosition&) const;
  84. void append_line(NonnullOwnPtr<TextDocumentLine>);
  85. void remove_line(size_t line_index);
  86. void remove_all_lines();
  87. void insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine>);
  88. void register_client(Client&);
  89. void unregister_client(Client&);
  90. void update_views(Badge<TextDocumentLine>);
  91. String text() const;
  92. String text_in_range(const TextRange&) const;
  93. Vector<TextRange> find_all(const StringView& needle, bool regmatch = false);
  94. void update_regex_matches(const StringView&);
  95. TextRange find_next(const StringView&, const TextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes, bool regmatch = false);
  96. TextRange find_previous(const StringView&, const TextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes, bool regmatch = false);
  97. TextPosition next_position_after(const TextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
  98. TextPosition previous_position_before(const TextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
  99. u32 code_point_at(const TextPosition&) const;
  100. TextRange range_for_entire_line(size_t line_index) const;
  101. Optional<TextDocumentSpan> first_non_skippable_span_before(const TextPosition&) const;
  102. Optional<TextDocumentSpan> first_non_skippable_span_after(const TextPosition&) const;
  103. TextPosition first_word_break_before(const TextPosition&, bool start_at_column_before) const;
  104. TextPosition first_word_break_after(const TextPosition&) const;
  105. void add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand>);
  106. bool can_undo() const { return m_undo_stack.can_undo(); }
  107. bool can_redo() const { return m_undo_stack.can_redo(); }
  108. void undo();
  109. void redo();
  110. void notify_did_change();
  111. void set_all_cursors(const TextPosition&);
  112. TextPosition insert_at(const TextPosition&, u32, const Client* = nullptr);
  113. TextPosition insert_at(const TextPosition&, const StringView&, const Client* = nullptr);
  114. void remove(const TextRange&);
  115. virtual bool is_code_document() const { return false; }
  116. bool is_empty() const;
  117. protected:
  118. explicit TextDocument(Client* client);
  119. private:
  120. void update_undo_timer();
  121. NonnullOwnPtrVector<TextDocumentLine> m_lines;
  122. Vector<TextDocumentSpan> m_spans;
  123. HashTable<Client*> m_clients;
  124. bool m_client_notifications_enabled { true };
  125. UndoStack m_undo_stack;
  126. RefPtr<Core::Timer> m_undo_timer;
  127. RegexResult m_regex_result;
  128. size_t m_regex_result_match_index { 0 };
  129. size_t m_regex_result_match_capture_group_index { 0 };
  130. bool m_regex_needs_update { true };
  131. String m_regex_needle;
  132. };
  133. class TextDocumentLine {
  134. public:
  135. explicit TextDocumentLine(TextDocument&);
  136. explicit TextDocumentLine(TextDocument&, const StringView&);
  137. String to_utf8() const;
  138. Utf32View view() const { return { code_points(), length() }; }
  139. const u32* code_points() const { return m_text.data(); }
  140. size_t length() const { return m_text.size(); }
  141. void set_text(TextDocument&, const StringView&);
  142. void set_text(TextDocument&, Vector<u32>);
  143. void append(TextDocument&, u32);
  144. void prepend(TextDocument&, u32);
  145. void insert(TextDocument&, size_t index, u32);
  146. void remove(TextDocument&, size_t index);
  147. void append(TextDocument&, const u32*, size_t);
  148. void truncate(TextDocument&, size_t length);
  149. void clear(TextDocument&);
  150. void remove_range(TextDocument&, size_t start, size_t length);
  151. size_t first_non_whitespace_column() const;
  152. Optional<size_t> last_non_whitespace_column() const;
  153. bool ends_in_whitespace() const;
  154. bool is_empty() const { return length() == 0; }
  155. size_t leading_spaces() const;
  156. private:
  157. // NOTE: This vector is null terminated.
  158. Vector<u32> m_text;
  159. };
  160. class TextDocumentUndoCommand : public Command {
  161. public:
  162. TextDocumentUndoCommand(TextDocument&);
  163. virtual ~TextDocumentUndoCommand();
  164. virtual void perform_formatting(const TextDocument::Client&) { }
  165. void execute_from(const TextDocument::Client& client)
  166. {
  167. m_client = &client;
  168. redo();
  169. m_client = nullptr;
  170. }
  171. protected:
  172. TextDocument& m_document;
  173. const TextDocument::Client* m_client { nullptr };
  174. };
  175. class InsertTextCommand : public TextDocumentUndoCommand {
  176. public:
  177. InsertTextCommand(TextDocument&, const String&, const TextPosition&);
  178. virtual void perform_formatting(const TextDocument::Client&) override;
  179. virtual void undo() override;
  180. virtual void redo() override;
  181. virtual bool is_insert_text() const override { return true; }
  182. const String& text() const { return m_text; }
  183. const TextRange& range() const { return m_range; }
  184. private:
  185. String m_text;
  186. TextRange m_range;
  187. };
  188. class RemoveTextCommand : public TextDocumentUndoCommand {
  189. public:
  190. RemoveTextCommand(TextDocument&, const String&, const TextRange&);
  191. virtual void undo() override;
  192. virtual void redo() override;
  193. virtual bool is_remove_text() const override { return true; }
  194. const TextRange& range() const { return m_range; }
  195. private:
  196. String m_text;
  197. TextRange m_range;
  198. };
  199. }