GTextDocument.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. #include <AK/StringBuilder.h>
  2. #include <LibCore/CTimer.h>
  3. #include <LibGUI/GTextDocument.h>
  4. #include <ctype.h>
  5. GTextDocument::GTextDocument(Client* client)
  6. {
  7. if (client)
  8. m_clients.set(client);
  9. append_line(make<GTextDocumentLine>(*this));
  10. // TODO: Instead of a repating timer, this we should call a delayed 2 sec timer when the user types.
  11. m_undo_timer = CTimer::construct(
  12. 2000, [this] {
  13. update_undo_timer();
  14. });
  15. }
  16. void GTextDocument::set_text(const StringView& text)
  17. {
  18. m_client_notifications_enabled = false;
  19. m_spans.clear();
  20. remove_all_lines();
  21. int start_of_current_line = 0;
  22. auto add_line = [&](int current_position) {
  23. int line_length = current_position - start_of_current_line;
  24. auto line = make<GTextDocumentLine>(*this);
  25. if (line_length)
  26. line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line));
  27. append_line(move(line));
  28. start_of_current_line = current_position + 1;
  29. };
  30. int i = 0;
  31. for (i = 0; i < text.length(); ++i) {
  32. if (text[i] == '\n')
  33. add_line(i);
  34. }
  35. add_line(i);
  36. m_client_notifications_enabled = true;
  37. for (auto* client : m_clients)
  38. client->document_did_set_text();
  39. }
  40. int GTextDocumentLine::first_non_whitespace_column() const
  41. {
  42. for (int i = 0; i < length(); ++i) {
  43. if (!isspace(m_text[i]))
  44. return i;
  45. }
  46. return length();
  47. }
  48. GTextDocumentLine::GTextDocumentLine(GTextDocument& document)
  49. {
  50. clear(document);
  51. }
  52. GTextDocumentLine::GTextDocumentLine(GTextDocument& document, const StringView& text)
  53. {
  54. set_text(document, text);
  55. }
  56. void GTextDocumentLine::clear(GTextDocument& document)
  57. {
  58. m_text.clear();
  59. m_text.append(0);
  60. document.update_views({});
  61. }
  62. void GTextDocumentLine::set_text(GTextDocument& document, const StringView& text)
  63. {
  64. if (text.length() == length() && !memcmp(text.characters_without_null_termination(), characters(), length()))
  65. return;
  66. if (text.is_empty()) {
  67. clear(document);
  68. return;
  69. }
  70. m_text.resize(text.length() + 1);
  71. memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
  72. document.update_views({});
  73. }
  74. void GTextDocumentLine::append(GTextDocument& document, const char* characters, int length)
  75. {
  76. int old_length = m_text.size() - 1;
  77. m_text.resize(m_text.size() + length);
  78. memcpy(m_text.data() + old_length, characters, length);
  79. m_text.last() = 0;
  80. document.update_views({});
  81. }
  82. void GTextDocumentLine::append(GTextDocument& document, char ch)
  83. {
  84. insert(document, length(), ch);
  85. }
  86. void GTextDocumentLine::prepend(GTextDocument& document, char ch)
  87. {
  88. insert(document, 0, ch);
  89. }
  90. void GTextDocumentLine::insert(GTextDocument& document, int index, char ch)
  91. {
  92. if (index == length()) {
  93. m_text.last() = ch;
  94. m_text.append(0);
  95. } else {
  96. m_text.insert(index, move(ch));
  97. }
  98. document.update_views({});
  99. }
  100. void GTextDocumentLine::remove(GTextDocument& document, int index)
  101. {
  102. if (index == length()) {
  103. m_text.take_last();
  104. m_text.last() = 0;
  105. } else {
  106. m_text.remove(index);
  107. }
  108. document.update_views({});
  109. }
  110. void GTextDocumentLine::truncate(GTextDocument& document, int length)
  111. {
  112. m_text.resize(length + 1);
  113. m_text.last() = 0;
  114. document.update_views({});
  115. }
  116. void GTextDocument::append_line(NonnullOwnPtr<GTextDocumentLine> line)
  117. {
  118. lines().append(move(line));
  119. if (m_client_notifications_enabled) {
  120. for (auto* client : m_clients)
  121. client->document_did_append_line();
  122. }
  123. }
  124. void GTextDocument::insert_line(int line_index, NonnullOwnPtr<GTextDocumentLine> line)
  125. {
  126. lines().insert(line_index, move(line));
  127. if (m_client_notifications_enabled) {
  128. for (auto* client : m_clients)
  129. client->document_did_insert_line(line_index);
  130. }
  131. }
  132. void GTextDocument::remove_line(int line_index)
  133. {
  134. lines().remove(line_index);
  135. if (m_client_notifications_enabled) {
  136. for (auto* client : m_clients)
  137. client->document_did_remove_line(line_index);
  138. }
  139. }
  140. void GTextDocument::remove_all_lines()
  141. {
  142. lines().clear();
  143. if (m_client_notifications_enabled) {
  144. for (auto* client : m_clients)
  145. client->document_did_remove_all_lines();
  146. }
  147. }
  148. GTextDocument::Client::~Client()
  149. {
  150. }
  151. void GTextDocument::register_client(Client& client)
  152. {
  153. m_clients.set(&client);
  154. }
  155. void GTextDocument::unregister_client(Client& client)
  156. {
  157. m_clients.remove(&client);
  158. }
  159. void GTextDocument::update_views(Badge<GTextDocumentLine>)
  160. {
  161. notify_did_change();
  162. }
  163. void GTextDocument::notify_did_change()
  164. {
  165. if (m_client_notifications_enabled) {
  166. for (auto* client : m_clients)
  167. client->document_did_change();
  168. }
  169. }
  170. void GTextDocument::set_all_cursors(const GTextPosition& position)
  171. {
  172. if (m_client_notifications_enabled) {
  173. for (auto* client : m_clients)
  174. client->document_did_set_cursor(position);
  175. }
  176. }
  177. String GTextDocument::text_in_range(const GTextRange& a_range) const
  178. {
  179. auto range = a_range.normalized();
  180. StringBuilder builder;
  181. for (int i = range.start().line(); i <= range.end().line(); ++i) {
  182. auto& line = lines()[i];
  183. int selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  184. int selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  185. builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
  186. if (i != range.end().line())
  187. builder.append('\n');
  188. }
  189. return builder.to_string();
  190. }
  191. char GTextDocument::character_at(const GTextPosition& position) const
  192. {
  193. ASSERT(position.line() < line_count());
  194. auto& line = lines()[position.line()];
  195. if (position.column() == line.length())
  196. return '\n';
  197. return line.characters()[position.column()];
  198. }
  199. GTextPosition GTextDocument::next_position_after(const GTextPosition& position, SearchShouldWrap should_wrap) const
  200. {
  201. auto& line = lines()[position.line()];
  202. if (position.column() == line.length()) {
  203. if (position.line() == line_count() - 1) {
  204. if (should_wrap == SearchShouldWrap::Yes)
  205. return { 0, 0 };
  206. return {};
  207. }
  208. return { position.line() + 1, 0 };
  209. }
  210. return { position.line(), position.column() + 1 };
  211. }
  212. GTextPosition GTextDocument::previous_position_before(const GTextPosition& position, SearchShouldWrap should_wrap) const
  213. {
  214. if (position.column() == 0) {
  215. if (position.line() == 0) {
  216. if (should_wrap == SearchShouldWrap::Yes) {
  217. auto& last_line = lines()[line_count() - 1];
  218. return { line_count() - 1, last_line.length() };
  219. }
  220. return {};
  221. }
  222. auto& prev_line = lines()[position.line() - 1];
  223. return { position.line() - 1, prev_line.length() };
  224. }
  225. return { position.line(), position.column() - 1 };
  226. }
  227. GTextRange GTextDocument::find_next(const StringView& needle, const GTextPosition& start, SearchShouldWrap should_wrap) const
  228. {
  229. if (needle.is_empty())
  230. return {};
  231. GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
  232. GTextPosition original_position = position;
  233. GTextPosition start_of_potential_match;
  234. int needle_index = 0;
  235. do {
  236. auto ch = character_at(position);
  237. if (ch == needle[needle_index]) {
  238. if (needle_index == 0)
  239. start_of_potential_match = position;
  240. ++needle_index;
  241. if (needle_index >= needle.length())
  242. return { start_of_potential_match, next_position_after(position, should_wrap) };
  243. } else {
  244. if (needle_index > 0)
  245. position = start_of_potential_match;
  246. needle_index = 0;
  247. }
  248. position = next_position_after(position, should_wrap);
  249. } while (position.is_valid() && position != original_position);
  250. return {};
  251. }
  252. GTextRange GTextDocument::find_previous(const StringView& needle, const GTextPosition& start, SearchShouldWrap should_wrap) const
  253. {
  254. if (needle.is_empty())
  255. return {};
  256. GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
  257. position = previous_position_before(position, should_wrap);
  258. GTextPosition original_position = position;
  259. GTextPosition end_of_potential_match;
  260. int needle_index = needle.length() - 1;
  261. do {
  262. auto ch = character_at(position);
  263. if (ch == needle[needle_index]) {
  264. if (needle_index == needle.length() - 1)
  265. end_of_potential_match = position;
  266. --needle_index;
  267. if (needle_index < 0)
  268. return { position, next_position_after(end_of_potential_match, should_wrap) };
  269. } else {
  270. if (needle_index < needle.length() - 1)
  271. position = end_of_potential_match;
  272. needle_index = needle.length() - 1;
  273. }
  274. position = previous_position_before(position, should_wrap);
  275. } while (position.is_valid() && position != original_position);
  276. return {};
  277. }
  278. Vector<GTextRange> GTextDocument::find_all(const StringView& needle) const
  279. {
  280. Vector<GTextRange> ranges;
  281. GTextPosition position;
  282. for (;;) {
  283. auto range = find_next(needle, position, SearchShouldWrap::No);
  284. if (!range.is_valid())
  285. break;
  286. ranges.append(range);
  287. position = range.end();
  288. }
  289. return ranges;
  290. }
  291. Optional<GTextDocumentSpan> GTextDocument::first_non_skippable_span_before(const GTextPosition& position) const
  292. {
  293. for (int i = m_spans.size() - 1; i >= 0; --i) {
  294. if (!m_spans[i].range.contains(position))
  295. continue;
  296. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  297. --i;
  298. if (i <= 0)
  299. return {};
  300. return m_spans[i - 1];
  301. }
  302. return {};
  303. }
  304. Optional<GTextDocumentSpan> GTextDocument::first_non_skippable_span_after(const GTextPosition& position) const
  305. {
  306. for (int i = 0; i < m_spans.size(); ++i) {
  307. if (!m_spans[i].range.contains(position))
  308. continue;
  309. while ((i + 1) < m_spans.size() && m_spans[i + 1].is_skippable)
  310. ++i;
  311. if (i >= (m_spans.size() - 1))
  312. return {};
  313. return m_spans[i + 1];
  314. }
  315. return {};
  316. }
  317. void GTextDocument::undo()
  318. {
  319. if (!can_undo())
  320. return;
  321. m_undo_stack.undo();
  322. notify_did_change();
  323. }
  324. void GTextDocument::redo()
  325. {
  326. if (!can_redo())
  327. return;
  328. m_undo_stack.redo();
  329. notify_did_change();
  330. }
  331. void GTextDocument::add_to_undo_stack(NonnullOwnPtr<GTextDocumentUndoCommand> undo_command)
  332. {
  333. m_undo_stack.push(move(undo_command));
  334. }
  335. GTextDocumentUndoCommand::GTextDocumentUndoCommand(GTextDocument& document)
  336. : m_document(document)
  337. {
  338. }
  339. GTextDocumentUndoCommand::~GTextDocumentUndoCommand()
  340. {
  341. }
  342. InsertTextCommand::InsertTextCommand(GTextDocument& document, const String& text, const GTextPosition& position)
  343. : GTextDocumentUndoCommand(document)
  344. , m_text(text)
  345. , m_range({ position, position })
  346. {
  347. }
  348. void InsertTextCommand::redo()
  349. {
  350. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  351. // NOTE: We don't know where the range ends until after doing redo().
  352. // This is okay since we always do redo() after adding this to the undo stack.
  353. m_range.set_end(new_cursor);
  354. m_document.set_all_cursors(new_cursor);
  355. }
  356. void InsertTextCommand::undo()
  357. {
  358. m_document.remove(m_range);
  359. m_document.set_all_cursors(m_range.start());
  360. }
  361. RemoveTextCommand::RemoveTextCommand(GTextDocument& document, const String& text, const GTextRange& range)
  362. : GTextDocumentUndoCommand(document)
  363. , m_text(text)
  364. , m_range(range)
  365. {
  366. }
  367. void RemoveTextCommand::redo()
  368. {
  369. m_document.remove(m_range);
  370. m_document.set_all_cursors(m_range.start());
  371. }
  372. void RemoveTextCommand::undo()
  373. {
  374. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  375. m_document.set_all_cursors(new_cursor);
  376. }
  377. void GTextDocument::update_undo_timer()
  378. {
  379. m_undo_stack.finalize_current_combo();
  380. }
  381. GTextPosition GTextDocument::insert_at(const GTextPosition& position, const StringView& text)
  382. {
  383. GTextPosition cursor = position;
  384. for (int i = 0; i < text.length(); ++i) {
  385. cursor = insert_at(cursor, text[i]);
  386. }
  387. return cursor;
  388. }
  389. GTextPosition GTextDocument::insert_at(const GTextPosition& position, char ch)
  390. {
  391. // FIXME: We need these from GTextEditor!
  392. bool m_automatic_indentation_enabled = true;
  393. int m_soft_tab_width = 4;
  394. bool at_head = position.column() == 0;
  395. bool at_tail = position.column() == line(position.line()).length();
  396. if (ch == '\n') {
  397. if (at_tail || at_head) {
  398. String new_line_contents;
  399. if (m_automatic_indentation_enabled && at_tail) {
  400. int leading_spaces = 0;
  401. auto& old_line = lines()[position.line()];
  402. for (int i = 0; i < old_line.length(); ++i) {
  403. if (old_line.characters()[i] == ' ')
  404. ++leading_spaces;
  405. else
  406. break;
  407. }
  408. if (leading_spaces)
  409. new_line_contents = String::repeated(' ', leading_spaces);
  410. }
  411. int row = position.line();
  412. Vector<char> line_content;
  413. for (int i = position.column(); i < line(row).length(); i++)
  414. line_content.append(line(row).characters()[i]);
  415. insert_line(position.line() + (at_tail ? 1 : 0), make<GTextDocumentLine>(*this, new_line_contents));
  416. notify_did_change();
  417. return { position.line() + 1, line(position.line() + 1).length() };
  418. }
  419. auto new_line = make<GTextDocumentLine>(*this);
  420. new_line->append(*this, line(position.line()).characters() + position.column(), line(position.line()).length() - position.column());
  421. Vector<char> line_content;
  422. for (int i = 0; i < new_line->length(); i++)
  423. line_content.append(new_line->characters()[i]);
  424. line(position.line()).truncate(*this, position.column());
  425. insert_line(position.line() + 1, move(new_line));
  426. notify_did_change();
  427. return { position.line() + 1, 0 };
  428. }
  429. if (ch == '\t') {
  430. int next_soft_tab_stop = ((position.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  431. int spaces_to_insert = next_soft_tab_stop - position.column();
  432. for (int i = 0; i < spaces_to_insert; ++i) {
  433. line(position.line()).insert(*this, position.column(), ' ');
  434. }
  435. notify_did_change();
  436. return { position.line(), next_soft_tab_stop };
  437. }
  438. line(position.line()).insert(*this, position.column(), ch);
  439. notify_did_change();
  440. return { position.line(), position.column() + 1 };
  441. }
  442. void GTextDocument::remove(const GTextRange& unnormalized_range)
  443. {
  444. if (!unnormalized_range.is_valid())
  445. return;
  446. auto range = unnormalized_range.normalized();
  447. // First delete all the lines in between the first and last one.
  448. for (int i = range.start().line() + 1; i < range.end().line();) {
  449. remove_line(i);
  450. range.end().set_line(range.end().line() - 1);
  451. }
  452. if (range.start().line() == range.end().line()) {
  453. // Delete within same line.
  454. auto& line = lines()[range.start().line()];
  455. bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length();
  456. if (whole_line_is_selected) {
  457. line.clear(*this);
  458. } else {
  459. auto before_selection = String(line.characters(), line.length()).substring(0, range.start().column());
  460. auto after_selection = String(line.characters(), line.length()).substring(range.end().column(), line.length() - range.end().column());
  461. StringBuilder builder(before_selection.length() + after_selection.length());
  462. builder.append(before_selection);
  463. builder.append(after_selection);
  464. line.set_text(*this, builder.to_string());
  465. }
  466. } else {
  467. // Delete across a newline, merging lines.
  468. ASSERT(range.start().line() == range.end().line() - 1);
  469. auto& first_line = lines()[range.start().line()];
  470. auto& second_line = lines()[range.end().line()];
  471. auto before_selection = String(first_line.characters(), first_line.length()).substring(0, range.start().column());
  472. auto after_selection = String(second_line.characters(), second_line.length()).substring(range.end().column(), second_line.length() - range.end().column());
  473. StringBuilder builder(before_selection.length() + after_selection.length());
  474. builder.append(before_selection);
  475. builder.append(after_selection);
  476. first_line.set_text(*this, builder.to_string());
  477. remove_line(range.end().line());
  478. }
  479. if (lines().is_empty()) {
  480. append_line(make<GTextDocumentLine>(*this));
  481. }
  482. notify_did_change();
  483. }