TextDocument.h 12 KB

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