GTextDocument.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include <AK/StringBuilder.h>
  2. #include <LibGUI/GTextDocument.h>
  3. #include <ctype.h>
  4. GTextDocument::GTextDocument(Client* client)
  5. {
  6. if (client)
  7. m_clients.set(client);
  8. append_line(make<GTextDocumentLine>(*this));
  9. }
  10. void GTextDocument::set_text(const StringView& text)
  11. {
  12. m_client_notifications_enabled = false;
  13. m_spans.clear();
  14. remove_all_lines();
  15. int start_of_current_line = 0;
  16. auto add_line = [&](int current_position) {
  17. int line_length = current_position - start_of_current_line;
  18. auto line = make<GTextDocumentLine>(*this);
  19. if (line_length)
  20. line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line));
  21. append_line(move(line));
  22. start_of_current_line = current_position + 1;
  23. };
  24. int i = 0;
  25. for (i = 0; i < text.length(); ++i) {
  26. if (text[i] == '\n')
  27. add_line(i);
  28. }
  29. add_line(i);
  30. m_client_notifications_enabled = true;
  31. for (auto* client : m_clients)
  32. client->document_did_set_text();
  33. }
  34. int GTextDocumentLine::first_non_whitespace_column() const
  35. {
  36. for (int i = 0; i < length(); ++i) {
  37. if (!isspace(m_text[i]))
  38. return i;
  39. }
  40. return length();
  41. }
  42. GTextDocumentLine::GTextDocumentLine(GTextDocument& document)
  43. {
  44. clear(document);
  45. }
  46. GTextDocumentLine::GTextDocumentLine(GTextDocument& document, const StringView& text)
  47. {
  48. set_text(document, text);
  49. }
  50. void GTextDocumentLine::clear(GTextDocument& document)
  51. {
  52. m_text.clear();
  53. m_text.append(0);
  54. document.update_views({});
  55. }
  56. void GTextDocumentLine::set_text(GTextDocument& document, const StringView& text)
  57. {
  58. if (text.length() == length() && !memcmp(text.characters_without_null_termination(), characters(), length()))
  59. return;
  60. if (text.is_empty()) {
  61. clear(document);
  62. return;
  63. }
  64. m_text.resize(text.length() + 1);
  65. memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
  66. document.update_views({});
  67. }
  68. void GTextDocumentLine::append(GTextDocument& document, const char* characters, int length)
  69. {
  70. int old_length = m_text.size() - 1;
  71. m_text.resize(m_text.size() + length);
  72. memcpy(m_text.data() + old_length, characters, length);
  73. m_text.last() = 0;
  74. document.update_views({});
  75. }
  76. void GTextDocumentLine::append(GTextDocument& document, char ch)
  77. {
  78. insert(document, length(), ch);
  79. }
  80. void GTextDocumentLine::prepend(GTextDocument& document, char ch)
  81. {
  82. insert(document, 0, ch);
  83. }
  84. void GTextDocumentLine::insert(GTextDocument& document, int index, char ch)
  85. {
  86. if (index == length()) {
  87. m_text.last() = ch;
  88. m_text.append(0);
  89. } else {
  90. m_text.insert(index, move(ch));
  91. }
  92. document.update_views({});
  93. }
  94. void GTextDocumentLine::remove(GTextDocument& document, int index)
  95. {
  96. if (index == length()) {
  97. m_text.take_last();
  98. m_text.last() = 0;
  99. } else {
  100. m_text.remove(index);
  101. }
  102. document.update_views({});
  103. }
  104. void GTextDocumentLine::truncate(GTextDocument& document, int length)
  105. {
  106. m_text.resize(length + 1);
  107. m_text.last() = 0;
  108. document.update_views({});
  109. }
  110. void GTextDocument::append_line(NonnullOwnPtr<GTextDocumentLine> line)
  111. {
  112. lines().append(move(line));
  113. if (m_client_notifications_enabled) {
  114. for (auto* client : m_clients)
  115. client->document_did_append_line();
  116. }
  117. }
  118. void GTextDocument::insert_line(int line_index, NonnullOwnPtr<GTextDocumentLine> line)
  119. {
  120. lines().insert(line_index, move(line));
  121. if (m_client_notifications_enabled) {
  122. for (auto* client : m_clients)
  123. client->document_did_insert_line(line_index);
  124. }
  125. }
  126. void GTextDocument::remove_line(int line_index)
  127. {
  128. lines().remove(line_index);
  129. if (m_client_notifications_enabled) {
  130. for (auto* client : m_clients)
  131. client->document_did_remove_line(line_index);
  132. }
  133. }
  134. void GTextDocument::remove_all_lines()
  135. {
  136. lines().clear();
  137. if (m_client_notifications_enabled) {
  138. for (auto* client : m_clients)
  139. client->document_did_remove_all_lines();
  140. }
  141. }
  142. GTextDocument::Client::~Client()
  143. {
  144. }
  145. void GTextDocument::register_client(Client& client)
  146. {
  147. m_clients.set(&client);
  148. }
  149. void GTextDocument::unregister_client(Client& client)
  150. {
  151. m_clients.remove(&client);
  152. }
  153. void GTextDocument::update_views(Badge<GTextDocumentLine>)
  154. {
  155. if (m_client_notifications_enabled) {
  156. for (auto* client : m_clients)
  157. client->document_did_change();
  158. }
  159. }
  160. String GTextDocument::text_in_range(const GTextRange& a_range) const
  161. {
  162. auto range = a_range.normalized();
  163. StringBuilder builder;
  164. for (int i = range.start().line(); i <= range.end().line(); ++i) {
  165. auto& line = lines()[i];
  166. int selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  167. int selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  168. builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
  169. if (i != range.end().line())
  170. builder.append('\n');
  171. }
  172. return builder.to_string();
  173. }
  174. char GTextDocument::character_at(const GTextPosition& position) const
  175. {
  176. ASSERT(position.line() < line_count());
  177. auto& line = lines()[position.line()];
  178. if (position.column() == line.length())
  179. return '\n';
  180. return line.characters()[position.column()];
  181. }
  182. GTextPosition GTextDocument::next_position_after(const GTextPosition& position, SearchShouldWrap should_wrap) const
  183. {
  184. auto& line = lines()[position.line()];
  185. if (position.column() == line.length()) {
  186. if (position.line() == line_count() - 1) {
  187. if (should_wrap == SearchShouldWrap::Yes)
  188. return { 0, 0 };
  189. return {};
  190. }
  191. return { position.line() + 1, 0 };
  192. }
  193. return { position.line(), position.column() + 1 };
  194. }
  195. GTextPosition GTextDocument::previous_position_before(const GTextPosition& position, SearchShouldWrap should_wrap) const
  196. {
  197. if (position.column() == 0) {
  198. if (position.line() == 0) {
  199. if (should_wrap == SearchShouldWrap::Yes) {
  200. auto& last_line = lines()[line_count() - 1];
  201. return { line_count() - 1, last_line.length() };
  202. }
  203. return {};
  204. }
  205. auto& prev_line = lines()[position.line() - 1];
  206. return { position.line() - 1, prev_line.length() };
  207. }
  208. return { position.line(), position.column() - 1 };
  209. }
  210. GTextRange GTextDocument::find_next(const StringView& needle, const GTextPosition& start, SearchShouldWrap should_wrap) const
  211. {
  212. if (needle.is_empty())
  213. return {};
  214. GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
  215. GTextPosition original_position = position;
  216. GTextPosition start_of_potential_match;
  217. int needle_index = 0;
  218. do {
  219. auto ch = character_at(position);
  220. if (ch == needle[needle_index]) {
  221. if (needle_index == 0)
  222. start_of_potential_match = position;
  223. ++needle_index;
  224. if (needle_index >= needle.length())
  225. return { start_of_potential_match, next_position_after(position, should_wrap) };
  226. } else {
  227. if (needle_index > 0)
  228. position = start_of_potential_match;
  229. needle_index = 0;
  230. }
  231. position = next_position_after(position, should_wrap);
  232. } while (position.is_valid() && position != original_position);
  233. return {};
  234. }
  235. GTextRange GTextDocument::find_previous(const StringView& needle, const GTextPosition& start, SearchShouldWrap should_wrap) const
  236. {
  237. if (needle.is_empty())
  238. return {};
  239. GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
  240. position = previous_position_before(position, should_wrap);
  241. GTextPosition original_position = position;
  242. GTextPosition end_of_potential_match;
  243. int needle_index = needle.length() - 1;
  244. do {
  245. auto ch = character_at(position);
  246. if (ch == needle[needle_index]) {
  247. if (needle_index == needle.length() - 1)
  248. end_of_potential_match = position;
  249. --needle_index;
  250. if (needle_index < 0)
  251. return { position, next_position_after(end_of_potential_match, should_wrap) };
  252. } else {
  253. if (needle_index < needle.length() - 1)
  254. position = end_of_potential_match;
  255. needle_index = needle.length() - 1;
  256. }
  257. position = previous_position_before(position, should_wrap);
  258. } while (position.is_valid() && position != original_position);
  259. return {};
  260. }
  261. Vector<GTextRange> GTextDocument::find_all(const StringView& needle) const
  262. {
  263. Vector<GTextRange> ranges;
  264. GTextPosition position;
  265. for (;;) {
  266. auto range = find_next(needle, position, SearchShouldWrap::No);
  267. if (!range.is_valid())
  268. break;
  269. ranges.append(range);
  270. position = range.end();
  271. }
  272. return ranges;
  273. }
  274. Optional<GTextDocumentSpan> GTextDocument::first_non_skippable_span_before(const GTextPosition& position) const
  275. {
  276. for (int i = m_spans.size() - 1; i >= 0; --i) {
  277. if (!m_spans[i].range.contains(position))
  278. continue;
  279. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  280. --i;
  281. if (i <= 0)
  282. return {};
  283. return m_spans[i - 1];
  284. }
  285. return {};
  286. }
  287. Optional<GTextDocumentSpan> GTextDocument::first_non_skippable_span_after(const GTextPosition& position) const
  288. {
  289. for (int i = 0; i < m_spans.size(); ++i) {
  290. if (!m_spans[i].range.contains(position))
  291. continue;
  292. while ((i + 1) < m_spans.size() && m_spans[i + 1].is_skippable)
  293. ++i;
  294. if (i >= (m_spans.size() - 1))
  295. return {};
  296. return m_spans[i + 1];
  297. }
  298. return {};
  299. }