TextDocument.h 12 KB

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