TextDocument.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashTable.h>
  9. #include <AK/NonnullOwnPtrVector.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <AK/Optional.h>
  12. #include <AK/RefCounted.h>
  13. #include <AK/Time.h>
  14. #include <AK/Utf32View.h>
  15. #include <LibCore/Forward.h>
  16. #include <LibGUI/Command.h>
  17. #include <LibGUI/Forward.h>
  18. #include <LibGUI/TextRange.h>
  19. #include <LibGUI/UndoStack.h>
  20. #include <LibGUI/Widget.h>
  21. #include <LibGfx/Color.h>
  22. #include <LibGfx/TextAttributes.h>
  23. #include <LibRegex/Regex.h>
  24. namespace GUI {
  25. constexpr Time COMMAND_COMMIT_TIME = Time::from_milliseconds(400);
  26. struct TextDocumentSpan {
  27. TextRange range;
  28. Gfx::TextAttributes attributes;
  29. u64 data { 0 };
  30. bool is_skippable { false };
  31. };
  32. class TextDocument : public RefCounted<TextDocument> {
  33. public:
  34. enum class SearchShouldWrap {
  35. No = 0,
  36. Yes
  37. };
  38. class Client {
  39. public:
  40. virtual ~Client() = default;
  41. virtual void document_did_append_line() = 0;
  42. virtual void document_did_insert_line(size_t) = 0;
  43. virtual void document_did_remove_line(size_t) = 0;
  44. virtual void document_did_remove_all_lines() = 0;
  45. virtual void document_did_change(AllowCallback = AllowCallback::Yes) = 0;
  46. virtual void document_did_set_text(AllowCallback = AllowCallback::Yes) = 0;
  47. virtual void document_did_set_cursor(TextPosition const&) = 0;
  48. virtual void document_did_update_undo_stack() = 0;
  49. virtual bool is_automatic_indentation_enabled() const = 0;
  50. virtual int soft_tab_width() const = 0;
  51. };
  52. static NonnullRefPtr<TextDocument> create(Client* client = nullptr);
  53. virtual ~TextDocument() = default;
  54. size_t line_count() const { return m_lines.size(); }
  55. TextDocumentLine const& line(size_t line_index) const { return m_lines[line_index]; }
  56. TextDocumentLine& line(size_t line_index) { return m_lines[line_index]; }
  57. void set_spans(u32 span_collection_index, Vector<TextDocumentSpan> spans);
  58. bool set_text(StringView, AllowCallback = AllowCallback::Yes);
  59. NonnullOwnPtrVector<TextDocumentLine> const& lines() const { return m_lines; }
  60. NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
  61. bool has_spans() const { return !m_spans.is_empty(); }
  62. Vector<TextDocumentSpan>& spans() { return m_spans; }
  63. Vector<TextDocumentSpan> const& spans() const { return m_spans; }
  64. void set_span_at_index(size_t index, TextDocumentSpan span) { m_spans[index] = move(span); }
  65. TextDocumentSpan const* span_at(TextPosition const&) const;
  66. void append_line(NonnullOwnPtr<TextDocumentLine>);
  67. NonnullOwnPtr<TextDocumentLine> take_line(size_t line_index);
  68. void remove_line(size_t line_index);
  69. void remove_all_lines();
  70. void insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine>);
  71. void register_client(Client&);
  72. void unregister_client(Client&);
  73. void update_views(Badge<TextDocumentLine>);
  74. DeprecatedString text() const;
  75. DeprecatedString text_in_range(TextRange const&) const;
  76. Vector<TextRange> find_all(StringView needle, bool regmatch = false, bool match_case = true);
  77. void update_regex_matches(StringView);
  78. TextRange find_next(StringView, TextPosition const& start = {}, SearchShouldWrap = SearchShouldWrap::Yes, bool regmatch = false, bool match_case = true);
  79. TextRange find_previous(StringView, TextPosition const& start = {}, SearchShouldWrap = SearchShouldWrap::Yes, bool regmatch = false, bool match_case = true);
  80. TextPosition next_position_after(TextPosition const&, SearchShouldWrap = SearchShouldWrap::Yes) const;
  81. TextPosition previous_position_before(TextPosition const&, SearchShouldWrap = SearchShouldWrap::Yes) const;
  82. size_t get_next_grapheme_cluster_boundary(TextPosition const& cursor) const;
  83. size_t get_previous_grapheme_cluster_boundary(TextPosition const& cursor) const;
  84. u32 code_point_at(TextPosition const&) const;
  85. TextRange range_for_entire_line(size_t line_index) const;
  86. Optional<TextDocumentSpan> first_non_skippable_span_before(TextPosition const&) const;
  87. Optional<TextDocumentSpan> first_non_skippable_span_after(TextPosition const&) const;
  88. TextPosition first_word_break_before(TextPosition const&, bool start_at_column_before) const;
  89. TextPosition first_word_break_after(TextPosition const&) const;
  90. TextPosition first_word_before(TextPosition const&, bool start_at_column_before) const;
  91. void add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand>);
  92. bool can_undo() const { return m_undo_stack.can_undo(); }
  93. bool can_redo() const { return m_undo_stack.can_redo(); }
  94. void undo();
  95. void redo();
  96. UndoStack const& undo_stack() const { return m_undo_stack; }
  97. void notify_did_change();
  98. void set_all_cursors(TextPosition const&);
  99. TextPosition insert_at(TextPosition const&, u32, Client const* = nullptr);
  100. TextPosition insert_at(TextPosition const&, StringView, Client const* = nullptr);
  101. void remove(TextRange const&);
  102. virtual bool is_code_document() const { return false; }
  103. bool is_empty() const;
  104. bool is_modified() const { return m_undo_stack.is_current_modified(); }
  105. void set_unmodified();
  106. protected:
  107. explicit TextDocument(Client* client);
  108. private:
  109. void merge_span_collections();
  110. NonnullOwnPtrVector<TextDocumentLine> m_lines;
  111. HashMap<u32, Vector<TextDocumentSpan>> m_span_collections;
  112. Vector<TextDocumentSpan> m_spans;
  113. HashTable<Client*> m_clients;
  114. bool m_client_notifications_enabled { true };
  115. UndoStack m_undo_stack;
  116. RegexResult m_regex_result;
  117. size_t m_regex_result_match_index { 0 };
  118. size_t m_regex_result_match_capture_group_index { 0 };
  119. bool m_regex_needs_update { true };
  120. DeprecatedString m_regex_needle;
  121. };
  122. class TextDocumentLine {
  123. public:
  124. explicit TextDocumentLine(TextDocument&);
  125. explicit TextDocumentLine(TextDocument&, StringView);
  126. DeprecatedString to_utf8() const;
  127. Utf32View view() const { return { code_points(), length() }; }
  128. u32 const* code_points() const { return m_text.data(); }
  129. size_t length() const { return m_text.size(); }
  130. bool set_text(TextDocument&, StringView);
  131. void set_text(TextDocument&, Vector<u32>);
  132. void append(TextDocument&, u32);
  133. void prepend(TextDocument&, u32);
  134. void insert(TextDocument&, size_t index, u32);
  135. void remove(TextDocument&, size_t index);
  136. void append(TextDocument&, u32 const*, size_t);
  137. void truncate(TextDocument&, size_t length);
  138. void clear(TextDocument&);
  139. void remove_range(TextDocument&, size_t start, size_t length);
  140. void keep_range(TextDocument&, size_t start_index, size_t end_index);
  141. size_t first_non_whitespace_column() const;
  142. Optional<size_t> last_non_whitespace_column() const;
  143. bool ends_in_whitespace() const;
  144. bool can_select() const;
  145. bool is_empty() const { return length() == 0; }
  146. size_t leading_spaces() const;
  147. private:
  148. // NOTE: This vector is null terminated.
  149. Vector<u32> m_text;
  150. };
  151. class TextDocumentUndoCommand : public Command {
  152. public:
  153. TextDocumentUndoCommand(TextDocument&);
  154. virtual ~TextDocumentUndoCommand() = default;
  155. virtual void perform_formatting(TextDocument::Client const&) { }
  156. void execute_from(TextDocument::Client const& client)
  157. {
  158. m_client = &client;
  159. redo();
  160. m_client = nullptr;
  161. }
  162. protected:
  163. bool commit_time_expired() const { return Time::now_monotonic() - m_timestamp >= COMMAND_COMMIT_TIME; }
  164. Time m_timestamp = Time::now_monotonic();
  165. TextDocument& m_document;
  166. TextDocument::Client const* m_client { nullptr };
  167. };
  168. class InsertTextCommand : public TextDocumentUndoCommand {
  169. public:
  170. InsertTextCommand(TextDocument&, DeprecatedString const&, TextPosition const&);
  171. virtual ~InsertTextCommand() = default;
  172. virtual void perform_formatting(TextDocument::Client const&) override;
  173. virtual void undo() override;
  174. virtual void redo() override;
  175. virtual bool merge_with(GUI::Command const&) override;
  176. virtual DeprecatedString action_text() const override;
  177. DeprecatedString const& text() const { return m_text; }
  178. TextRange const& range() const { return m_range; }
  179. private:
  180. DeprecatedString m_text;
  181. TextRange m_range;
  182. };
  183. class RemoveTextCommand : public TextDocumentUndoCommand {
  184. public:
  185. RemoveTextCommand(TextDocument&, DeprecatedString const&, TextRange const&);
  186. virtual ~RemoveTextCommand() = default;
  187. virtual void undo() override;
  188. virtual void redo() override;
  189. TextRange const& range() const { return m_range; }
  190. virtual bool merge_with(GUI::Command const&) override;
  191. virtual DeprecatedString action_text() const override;
  192. private:
  193. DeprecatedString m_text;
  194. TextRange m_range;
  195. };
  196. class InsertLineCommand : public TextDocumentUndoCommand {
  197. public:
  198. enum class InsertPosition {
  199. Above,
  200. Below,
  201. };
  202. InsertLineCommand(TextDocument&, TextPosition, DeprecatedString&&, InsertPosition);
  203. virtual ~InsertLineCommand() = default;
  204. virtual void undo() override;
  205. virtual void redo() override;
  206. virtual DeprecatedString action_text() const override;
  207. private:
  208. size_t compute_line_number() const;
  209. TextPosition m_cursor;
  210. DeprecatedString m_text;
  211. InsertPosition m_pos;
  212. };
  213. class ReplaceAllTextCommand final : public GUI::TextDocumentUndoCommand {
  214. public:
  215. ReplaceAllTextCommand(GUI::TextDocument& document, DeprecatedString const& new_text, GUI::TextRange const& range, DeprecatedString const& action_text);
  216. virtual ~ReplaceAllTextCommand() = default;
  217. void redo() override;
  218. void undo() override;
  219. bool merge_with(GUI::Command const&) override;
  220. DeprecatedString action_text() const override;
  221. DeprecatedString const& text() const { return m_new_text; }
  222. TextRange const& range() const { return m_range; }
  223. private:
  224. DeprecatedString m_original_text;
  225. DeprecatedString m_new_text;
  226. GUI::TextRange m_range;
  227. DeprecatedString m_action_text;
  228. };
  229. class IndentSelection : public TextDocumentUndoCommand {
  230. public:
  231. IndentSelection(TextDocument&, size_t tab_width, TextRange const&);
  232. virtual void undo() override;
  233. virtual void redo() override;
  234. TextRange const& range() const { return m_range; }
  235. private:
  236. size_t m_tab_width { 0 };
  237. TextRange m_range;
  238. };
  239. class UnindentSelection : public TextDocumentUndoCommand {
  240. public:
  241. UnindentSelection(TextDocument&, size_t tab_width, TextRange const&);
  242. virtual void undo() override;
  243. virtual void redo() override;
  244. TextRange const& range() const { return m_range; }
  245. private:
  246. size_t m_tab_width { 0 };
  247. TextRange m_range;
  248. };
  249. class CommentSelection : public TextDocumentUndoCommand {
  250. public:
  251. CommentSelection(TextDocument&, StringView, StringView, TextRange const&);
  252. virtual void undo() override;
  253. virtual void redo() override;
  254. TextRange const& range() const { return m_range; }
  255. private:
  256. StringView m_prefix;
  257. StringView m_suffix;
  258. TextRange m_range;
  259. };
  260. class UncommentSelection : public TextDocumentUndoCommand {
  261. public:
  262. UncommentSelection(TextDocument&, StringView, StringView, TextRange const&);
  263. virtual void undo() override;
  264. virtual void redo() override;
  265. TextRange const& range() const { return m_range; }
  266. private:
  267. StringView m_prefix;
  268. StringView m_suffix;
  269. TextRange m_range;
  270. };
  271. }