TextDocument.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibCore/Timer.h>
  11. #include <LibGUI/TextDocument.h>
  12. #include <LibGUI/TextEditor.h>
  13. #include <LibRegex/Regex.h>
  14. #include <ctype.h>
  15. namespace GUI {
  16. NonnullRefPtr<TextDocument> TextDocument::create(Client* client)
  17. {
  18. return adopt_ref(*new TextDocument(client));
  19. }
  20. TextDocument::TextDocument(Client* client)
  21. {
  22. if (client)
  23. m_clients.set(client);
  24. append_line(make<TextDocumentLine>(*this));
  25. set_unmodified();
  26. m_undo_stack.on_state_change = [this] {
  27. if (m_client_notifications_enabled) {
  28. for (auto* client : m_clients)
  29. client->document_did_update_undo_stack();
  30. }
  31. };
  32. m_undo_timer = Core::Timer::create_single_shot(
  33. 2000, [this] {
  34. update_undo();
  35. });
  36. }
  37. TextDocument::~TextDocument()
  38. {
  39. }
  40. bool TextDocument::set_text(const StringView& text)
  41. {
  42. m_client_notifications_enabled = false;
  43. m_undo_stack.clear();
  44. m_spans.clear();
  45. remove_all_lines();
  46. ArmedScopeGuard clear_text_guard([this]() {
  47. set_text({});
  48. });
  49. size_t start_of_current_line = 0;
  50. auto add_line = [&](size_t current_position) -> bool {
  51. size_t line_length = current_position - start_of_current_line;
  52. auto line = make<TextDocumentLine>(*this);
  53. bool success = true;
  54. if (line_length)
  55. success = line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line));
  56. if (!success)
  57. return false;
  58. append_line(move(line));
  59. start_of_current_line = current_position + 1;
  60. return true;
  61. };
  62. size_t i = 0;
  63. for (i = 0; i < text.length(); ++i) {
  64. if (text[i] != '\n')
  65. continue;
  66. auto success = add_line(i);
  67. if (!success)
  68. return false;
  69. }
  70. auto success = add_line(i);
  71. if (!success)
  72. return false;
  73. // Don't show the file's trailing newline as an actual new line.
  74. if (line_count() > 1 && line(line_count() - 1).is_empty())
  75. m_lines.take_last();
  76. m_client_notifications_enabled = true;
  77. for (auto* client : m_clients)
  78. client->document_did_set_text();
  79. clear_text_guard.disarm();
  80. // FIXME: Should the modified state be cleared on some of the earlier returns as well?
  81. set_unmodified();
  82. return true;
  83. }
  84. size_t TextDocumentLine::first_non_whitespace_column() const
  85. {
  86. for (size_t i = 0; i < length(); ++i) {
  87. auto code_point = code_points()[i];
  88. if (!isspace(code_point))
  89. return i;
  90. }
  91. return length();
  92. }
  93. Optional<size_t> TextDocumentLine::last_non_whitespace_column() const
  94. {
  95. for (ssize_t i = length() - 1; i >= 0; --i) {
  96. auto code_point = code_points()[i];
  97. if (!isspace(code_point))
  98. return i;
  99. }
  100. return {};
  101. }
  102. bool TextDocumentLine::ends_in_whitespace() const
  103. {
  104. if (!length())
  105. return false;
  106. return isspace(code_points()[length() - 1]);
  107. }
  108. bool TextDocumentLine::can_select() const
  109. {
  110. if (is_empty())
  111. return false;
  112. for (size_t i = 0; i < length(); ++i) {
  113. auto code_point = code_points()[i];
  114. if (code_point != '\n' && code_point != '\r' && code_point != '\f' && code_point != '\v')
  115. return true;
  116. }
  117. return false;
  118. }
  119. size_t TextDocumentLine::leading_spaces() const
  120. {
  121. size_t count = 0;
  122. for (; count < m_text.size(); ++count) {
  123. if (m_text[count] != ' ') {
  124. break;
  125. }
  126. }
  127. return count;
  128. }
  129. String TextDocumentLine::to_utf8() const
  130. {
  131. StringBuilder builder;
  132. builder.append(view());
  133. return builder.to_string();
  134. }
  135. TextDocumentLine::TextDocumentLine(TextDocument& document)
  136. {
  137. clear(document);
  138. }
  139. TextDocumentLine::TextDocumentLine(TextDocument& document, const StringView& text)
  140. {
  141. set_text(document, text);
  142. }
  143. void TextDocumentLine::clear(TextDocument& document)
  144. {
  145. m_text.clear();
  146. document.update_views({});
  147. }
  148. void TextDocumentLine::set_text(TextDocument& document, const Vector<u32> text)
  149. {
  150. m_text = move(text);
  151. document.update_views({});
  152. }
  153. bool TextDocumentLine::set_text(TextDocument& document, const StringView& text)
  154. {
  155. if (text.is_empty()) {
  156. clear(document);
  157. return true;
  158. }
  159. m_text.clear();
  160. Utf8View utf8_view(text);
  161. if (!utf8_view.validate()) {
  162. return false;
  163. }
  164. for (auto code_point : utf8_view)
  165. m_text.append(code_point);
  166. document.update_views({});
  167. return true;
  168. }
  169. void TextDocumentLine::append(TextDocument& document, const u32* code_points, size_t length)
  170. {
  171. if (length == 0)
  172. return;
  173. m_text.append(code_points, length);
  174. document.update_views({});
  175. }
  176. void TextDocumentLine::append(TextDocument& document, u32 code_point)
  177. {
  178. insert(document, length(), code_point);
  179. }
  180. void TextDocumentLine::prepend(TextDocument& document, u32 code_point)
  181. {
  182. insert(document, 0, code_point);
  183. }
  184. void TextDocumentLine::insert(TextDocument& document, size_t index, u32 code_point)
  185. {
  186. if (index == length()) {
  187. m_text.append(code_point);
  188. } else {
  189. m_text.insert(index, code_point);
  190. }
  191. document.update_views({});
  192. }
  193. void TextDocumentLine::remove(TextDocument& document, size_t index)
  194. {
  195. if (index == length()) {
  196. m_text.take_last();
  197. } else {
  198. m_text.remove(index);
  199. }
  200. document.update_views({});
  201. }
  202. void TextDocumentLine::remove_range(TextDocument& document, size_t start, size_t length)
  203. {
  204. VERIFY(length <= m_text.size());
  205. Vector<u32> new_data;
  206. new_data.ensure_capacity(m_text.size() - length);
  207. for (size_t i = 0; i < start; ++i)
  208. new_data.append(m_text[i]);
  209. for (size_t i = (start + length); i < m_text.size(); ++i)
  210. new_data.append(m_text[i]);
  211. m_text = move(new_data);
  212. document.update_views({});
  213. }
  214. void TextDocumentLine::truncate(TextDocument& document, size_t length)
  215. {
  216. m_text.resize(length);
  217. document.update_views({});
  218. }
  219. void TextDocument::append_line(NonnullOwnPtr<TextDocumentLine> line)
  220. {
  221. lines().append(move(line));
  222. if (m_client_notifications_enabled) {
  223. for (auto* client : m_clients)
  224. client->document_did_append_line();
  225. }
  226. }
  227. void TextDocument::insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine> line)
  228. {
  229. lines().insert((int)line_index, move(line));
  230. if (m_client_notifications_enabled) {
  231. for (auto* client : m_clients)
  232. client->document_did_insert_line(line_index);
  233. }
  234. }
  235. void TextDocument::remove_line(size_t line_index)
  236. {
  237. lines().remove((int)line_index);
  238. if (m_client_notifications_enabled) {
  239. for (auto* client : m_clients)
  240. client->document_did_remove_line(line_index);
  241. }
  242. }
  243. void TextDocument::remove_all_lines()
  244. {
  245. lines().clear();
  246. if (m_client_notifications_enabled) {
  247. for (auto* client : m_clients)
  248. client->document_did_remove_all_lines();
  249. }
  250. }
  251. TextDocument::Client::~Client()
  252. {
  253. }
  254. void TextDocument::register_client(Client& client)
  255. {
  256. m_clients.set(&client);
  257. }
  258. void TextDocument::unregister_client(Client& client)
  259. {
  260. m_clients.remove(&client);
  261. }
  262. void TextDocument::update_views(Badge<TextDocumentLine>)
  263. {
  264. notify_did_change();
  265. }
  266. void TextDocument::notify_did_change()
  267. {
  268. if (m_undo_timer)
  269. m_undo_timer->restart();
  270. if (m_client_notifications_enabled) {
  271. for (auto* client : m_clients)
  272. client->document_did_change();
  273. }
  274. m_regex_needs_update = true;
  275. }
  276. void TextDocument::set_all_cursors(const TextPosition& position)
  277. {
  278. if (m_client_notifications_enabled) {
  279. for (auto* client : m_clients)
  280. client->document_did_set_cursor(position);
  281. }
  282. }
  283. String TextDocument::text() const
  284. {
  285. StringBuilder builder;
  286. for (size_t i = 0; i < line_count(); ++i) {
  287. auto& line = this->line(i);
  288. builder.append(line.view());
  289. if (i != line_count() - 1)
  290. builder.append('\n');
  291. }
  292. return builder.to_string();
  293. }
  294. String TextDocument::text_in_range(const TextRange& a_range) const
  295. {
  296. auto range = a_range.normalized();
  297. if (is_empty() || line_count() < range.end().line() - range.start().line())
  298. return String("");
  299. StringBuilder builder;
  300. for (size_t i = range.start().line(); i <= range.end().line(); ++i) {
  301. auto& line = this->line(i);
  302. size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  303. size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  304. builder.append(Utf32View(line.code_points() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line));
  305. if (i != range.end().line())
  306. builder.append('\n');
  307. }
  308. return builder.to_string();
  309. }
  310. u32 TextDocument::code_point_at(const TextPosition& position) const
  311. {
  312. VERIFY(position.line() < line_count());
  313. auto& line = this->line(position.line());
  314. if (position.column() == line.length())
  315. return '\n';
  316. return line.code_points()[position.column()];
  317. }
  318. TextPosition TextDocument::next_position_after(const TextPosition& position, SearchShouldWrap should_wrap) const
  319. {
  320. auto& line = this->line(position.line());
  321. if (position.column() == line.length()) {
  322. if (position.line() == line_count() - 1) {
  323. if (should_wrap == SearchShouldWrap::Yes)
  324. return { 0, 0 };
  325. return {};
  326. }
  327. return { position.line() + 1, 0 };
  328. }
  329. return { position.line(), position.column() + 1 };
  330. }
  331. TextPosition TextDocument::previous_position_before(const TextPosition& position, SearchShouldWrap should_wrap) const
  332. {
  333. if (position.column() == 0) {
  334. if (position.line() == 0) {
  335. if (should_wrap == SearchShouldWrap::Yes) {
  336. auto& last_line = this->line(line_count() - 1);
  337. return { line_count() - 1, last_line.length() };
  338. }
  339. return {};
  340. }
  341. auto& prev_line = this->line(position.line() - 1);
  342. return { position.line() - 1, prev_line.length() };
  343. }
  344. return { position.line(), position.column() - 1 };
  345. }
  346. void TextDocument::update_regex_matches(const StringView& needle)
  347. {
  348. if (m_regex_needs_update || needle != m_regex_needle) {
  349. Regex<PosixExtended> re(needle);
  350. Vector<RegexStringView> views;
  351. for (size_t line = 0; line < m_lines.size(); ++line) {
  352. views.append(m_lines.at(line).view());
  353. }
  354. re.search(views, m_regex_result);
  355. m_regex_needs_update = false;
  356. m_regex_needle = String { needle };
  357. m_regex_result_match_index = -1;
  358. m_regex_result_match_capture_group_index = -1;
  359. }
  360. }
  361. TextRange TextDocument::find_next(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap, bool regmatch, bool match_case)
  362. {
  363. if (needle.is_empty())
  364. return {};
  365. if (regmatch) {
  366. if (!m_regex_result.matches.size())
  367. return {};
  368. regex::Match match;
  369. bool use_whole_match { false };
  370. auto next_match = [&] {
  371. m_regex_result_match_capture_group_index = 0;
  372. if (m_regex_result_match_index == m_regex_result.matches.size() - 1) {
  373. if (should_wrap == SearchShouldWrap::Yes)
  374. m_regex_result_match_index = 0;
  375. else
  376. ++m_regex_result_match_index;
  377. } else
  378. ++m_regex_result_match_index;
  379. };
  380. if (m_regex_result.n_capture_groups) {
  381. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  382. next_match();
  383. else {
  384. // check if last capture group has been reached
  385. if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size()) {
  386. next_match();
  387. } else {
  388. // get to the next capture group item
  389. ++m_regex_result_match_capture_group_index;
  390. }
  391. }
  392. // use whole match, if there is no capture group for current index
  393. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  394. use_whole_match = true;
  395. else if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  396. next_match();
  397. } else {
  398. next_match();
  399. }
  400. if (use_whole_match || !m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  401. match = m_regex_result.matches.at(m_regex_result_match_index);
  402. else
  403. match = m_regex_result.capture_group_matches.at(m_regex_result_match_index).at(m_regex_result_match_capture_group_index);
  404. return TextRange { { match.line, match.column }, { match.line, match.column + match.view.length() } };
  405. }
  406. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  407. TextPosition original_position = position;
  408. TextPosition start_of_potential_match;
  409. size_t needle_index = 0;
  410. do {
  411. auto ch = code_point_at(position);
  412. // FIXME: This is not the right way to use a Unicode needle!
  413. if (match_case ? ch == (u32)needle[needle_index] : tolower(ch) == tolower((u32)needle[needle_index])) {
  414. if (needle_index == 0)
  415. start_of_potential_match = position;
  416. ++needle_index;
  417. if (needle_index >= needle.length())
  418. return { start_of_potential_match, next_position_after(position, should_wrap) };
  419. } else {
  420. if (needle_index > 0)
  421. position = start_of_potential_match;
  422. needle_index = 0;
  423. }
  424. position = next_position_after(position, should_wrap);
  425. } while (position.is_valid() && position != original_position);
  426. return {};
  427. }
  428. TextRange TextDocument::find_previous(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap, bool regmatch, bool match_case)
  429. {
  430. if (needle.is_empty())
  431. return {};
  432. if (regmatch) {
  433. if (!m_regex_result.matches.size())
  434. return {};
  435. regex::Match match;
  436. bool use_whole_match { false };
  437. auto next_match = [&] {
  438. if (m_regex_result_match_index == 0) {
  439. if (should_wrap == SearchShouldWrap::Yes)
  440. m_regex_result_match_index = m_regex_result.matches.size() - 1;
  441. else
  442. --m_regex_result_match_index;
  443. } else
  444. --m_regex_result_match_index;
  445. m_regex_result_match_capture_group_index = m_regex_result.capture_group_matches.at(m_regex_result_match_index).size() - 1;
  446. };
  447. if (m_regex_result.n_capture_groups) {
  448. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  449. next_match();
  450. else {
  451. // check if last capture group has been reached
  452. if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size()) {
  453. next_match();
  454. } else {
  455. // get to the next capture group item
  456. --m_regex_result_match_capture_group_index;
  457. }
  458. }
  459. // use whole match, if there is no capture group for current index
  460. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  461. use_whole_match = true;
  462. else if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  463. next_match();
  464. } else {
  465. next_match();
  466. }
  467. if (use_whole_match || !m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  468. match = m_regex_result.matches.at(m_regex_result_match_index);
  469. else
  470. match = m_regex_result.capture_group_matches.at(m_regex_result_match_index).at(m_regex_result_match_capture_group_index);
  471. return TextRange { { match.line, match.column }, { match.line, match.column + match.view.length() } };
  472. }
  473. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  474. position = previous_position_before(position, should_wrap);
  475. if (position.line() >= line_count())
  476. return {};
  477. TextPosition original_position = position;
  478. TextPosition end_of_potential_match;
  479. size_t needle_index = needle.length() - 1;
  480. do {
  481. auto ch = code_point_at(position);
  482. // FIXME: This is not the right way to use a Unicode needle!
  483. if (match_case ? ch == (u32)needle[needle_index] : tolower(ch) == tolower((u32)needle[needle_index])) {
  484. if (needle_index == needle.length() - 1)
  485. end_of_potential_match = position;
  486. if (needle_index == 0)
  487. return { position, next_position_after(end_of_potential_match, should_wrap) };
  488. --needle_index;
  489. } else {
  490. if (needle_index < needle.length() - 1)
  491. position = end_of_potential_match;
  492. needle_index = needle.length() - 1;
  493. }
  494. position = previous_position_before(position, should_wrap);
  495. } while (position.is_valid() && position != original_position);
  496. return {};
  497. }
  498. Vector<TextRange> TextDocument::find_all(const StringView& needle, bool regmatch)
  499. {
  500. Vector<TextRange> ranges;
  501. TextPosition position;
  502. for (;;) {
  503. auto range = find_next(needle, position, SearchShouldWrap::No, regmatch);
  504. if (!range.is_valid())
  505. break;
  506. ranges.append(range);
  507. position = range.end();
  508. }
  509. return ranges;
  510. }
  511. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_before(const TextPosition& position) const
  512. {
  513. for (int i = m_spans.size() - 1; i >= 0; --i) {
  514. if (!m_spans[i].range.contains(position))
  515. continue;
  516. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  517. --i;
  518. if (i <= 0)
  519. return {};
  520. return m_spans[i - 1];
  521. }
  522. return {};
  523. }
  524. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_after(const TextPosition& position) const
  525. {
  526. for (size_t i = 0; i < m_spans.size(); ++i) {
  527. if (!m_spans[i].range.contains(position))
  528. continue;
  529. while ((i + 1) < m_spans.size() && m_spans[i + 1].is_skippable)
  530. ++i;
  531. if (i >= (m_spans.size() - 1))
  532. return {};
  533. return m_spans[i + 1];
  534. }
  535. return {};
  536. }
  537. TextPosition TextDocument::first_word_break_before(const TextPosition& position, bool start_at_column_before) const
  538. {
  539. if (position.column() == 0) {
  540. if (position.line() == 0) {
  541. return TextPosition(0, 0);
  542. }
  543. auto previous_line = this->line(position.line() - 1);
  544. return TextPosition(position.line() - 1, previous_line.length());
  545. }
  546. auto target = position;
  547. auto line = this->line(target.line());
  548. auto modifier = start_at_column_before ? 1 : 0;
  549. if (target.column() == line.length())
  550. modifier = 1;
  551. auto is_start_alphanumeric = isalnum(line.code_points()[target.column() - modifier]);
  552. while (target.column() > 0) {
  553. auto prev_code_point = line.code_points()[target.column() - 1];
  554. if ((is_start_alphanumeric && !isalnum(prev_code_point)) || (!is_start_alphanumeric && isalnum(prev_code_point)))
  555. break;
  556. target.set_column(target.column() - 1);
  557. }
  558. return target;
  559. }
  560. TextPosition TextDocument::first_word_break_after(const TextPosition& position) const
  561. {
  562. auto target = position;
  563. auto line = this->line(target.line());
  564. if (position.column() >= line.length()) {
  565. if (position.line() >= this->line_count() - 1) {
  566. return position;
  567. }
  568. return TextPosition(position.line() + 1, 0);
  569. }
  570. auto is_start_alphanumeric = isalnum(line.code_points()[target.column()]);
  571. while (target.column() < line.length()) {
  572. auto next_code_point = line.code_points()[target.column()];
  573. if ((is_start_alphanumeric && !isalnum(next_code_point)) || (!is_start_alphanumeric && isalnum(next_code_point)))
  574. break;
  575. target.set_column(target.column() + 1);
  576. }
  577. return target;
  578. }
  579. void TextDocument::undo()
  580. {
  581. if (!can_undo())
  582. return;
  583. m_undo_stack.undo();
  584. notify_did_change();
  585. }
  586. void TextDocument::redo()
  587. {
  588. if (!can_redo())
  589. return;
  590. m_undo_stack.redo();
  591. notify_did_change();
  592. }
  593. void TextDocument::add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand> undo_command)
  594. {
  595. m_undo_stack.push(move(undo_command));
  596. notify_did_change();
  597. }
  598. TextDocumentUndoCommand::TextDocumentUndoCommand(TextDocument& document)
  599. : m_document(document)
  600. {
  601. }
  602. TextDocumentUndoCommand::~TextDocumentUndoCommand()
  603. {
  604. }
  605. InsertTextCommand::InsertTextCommand(TextDocument& document, const String& text, const TextPosition& position)
  606. : TextDocumentUndoCommand(document)
  607. , m_text(text)
  608. , m_range({ position, position })
  609. {
  610. }
  611. void InsertTextCommand::perform_formatting(const TextDocument::Client& client)
  612. {
  613. const size_t tab_width = client.soft_tab_width();
  614. const auto& dest_line = m_document.line(m_range.start().line());
  615. const bool should_auto_indent = client.is_automatic_indentation_enabled();
  616. StringBuilder builder;
  617. size_t column = m_range.start().column();
  618. size_t line_indentation = dest_line.leading_spaces();
  619. bool at_start_of_line = line_indentation == column;
  620. for (auto input_char : m_text) {
  621. if (input_char == '\n') {
  622. builder.append('\n');
  623. column = 0;
  624. if (should_auto_indent) {
  625. for (; column < line_indentation; ++column) {
  626. builder.append(' ');
  627. }
  628. }
  629. at_start_of_line = true;
  630. } else if (input_char == '\t') {
  631. size_t next_soft_tab_stop = ((column + tab_width) / tab_width) * tab_width;
  632. size_t spaces_to_insert = next_soft_tab_stop - column;
  633. for (size_t i = 0; i < spaces_to_insert; ++i) {
  634. builder.append(' ');
  635. }
  636. column = next_soft_tab_stop;
  637. if (at_start_of_line) {
  638. line_indentation = column;
  639. }
  640. } else {
  641. if (input_char == ' ') {
  642. if (at_start_of_line) {
  643. ++line_indentation;
  644. }
  645. } else {
  646. at_start_of_line = false;
  647. }
  648. builder.append(input_char);
  649. ++column;
  650. }
  651. }
  652. m_text = builder.build();
  653. }
  654. void InsertTextCommand::redo()
  655. {
  656. auto new_cursor = m_document.insert_at(m_range.start(), m_text, m_client);
  657. // NOTE: We don't know where the range ends until after doing redo().
  658. // This is okay since we always do redo() after adding this to the undo stack.
  659. m_range.set_end(new_cursor);
  660. m_document.set_all_cursors(new_cursor);
  661. }
  662. void InsertTextCommand::undo()
  663. {
  664. m_document.remove(m_range);
  665. m_document.set_all_cursors(m_range.start());
  666. }
  667. RemoveTextCommand::RemoveTextCommand(TextDocument& document, const String& text, const TextRange& range)
  668. : TextDocumentUndoCommand(document)
  669. , m_text(text)
  670. , m_range(range)
  671. {
  672. }
  673. void RemoveTextCommand::redo()
  674. {
  675. m_document.remove(m_range);
  676. m_document.set_all_cursors(m_range.start());
  677. }
  678. void RemoveTextCommand::undo()
  679. {
  680. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  681. m_document.set_all_cursors(new_cursor);
  682. }
  683. void TextDocument::update_undo()
  684. {
  685. m_undo_stack.finalize_current_combo();
  686. }
  687. TextPosition TextDocument::insert_at(const TextPosition& position, const StringView& text, const Client* client)
  688. {
  689. TextPosition cursor = position;
  690. Utf8View utf8_view(text);
  691. for (auto code_point : utf8_view)
  692. cursor = insert_at(cursor, code_point, client);
  693. return cursor;
  694. }
  695. TextPosition TextDocument::insert_at(const TextPosition& position, u32 code_point, const Client*)
  696. {
  697. if (code_point == '\n') {
  698. auto new_line = make<TextDocumentLine>(*this);
  699. new_line->append(*this, line(position.line()).code_points() + position.column(), line(position.line()).length() - position.column());
  700. line(position.line()).truncate(*this, position.column());
  701. insert_line(position.line() + 1, move(new_line));
  702. notify_did_change();
  703. return { position.line() + 1, 0 };
  704. } else {
  705. line(position.line()).insert(*this, position.column(), code_point);
  706. notify_did_change();
  707. return { position.line(), position.column() + 1 };
  708. }
  709. }
  710. void TextDocument::remove(const TextRange& unnormalized_range)
  711. {
  712. if (!unnormalized_range.is_valid())
  713. return;
  714. auto range = unnormalized_range.normalized();
  715. // First delete all the lines in between the first and last one.
  716. for (size_t i = range.start().line() + 1; i < range.end().line();) {
  717. remove_line(i);
  718. range.end().set_line(range.end().line() - 1);
  719. }
  720. if (range.start().line() == range.end().line()) {
  721. // Delete within same line.
  722. auto& line = this->line(range.start().line());
  723. bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length();
  724. if (whole_line_is_selected) {
  725. line.clear(*this);
  726. } else {
  727. line.remove_range(*this, range.start().column(), range.end().column() - range.start().column());
  728. }
  729. } else {
  730. // Delete across a newline, merging lines.
  731. VERIFY(range.start().line() == range.end().line() - 1);
  732. auto& first_line = line(range.start().line());
  733. auto& second_line = line(range.end().line());
  734. Vector<u32> code_points;
  735. code_points.append(first_line.code_points(), range.start().column());
  736. code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
  737. first_line.set_text(*this, move(code_points));
  738. remove_line(range.end().line());
  739. }
  740. if (lines().is_empty()) {
  741. append_line(make<TextDocumentLine>(*this));
  742. }
  743. notify_did_change();
  744. }
  745. bool TextDocument::is_empty() const
  746. {
  747. return line_count() == 1 && line(0).is_empty();
  748. }
  749. TextRange TextDocument::range_for_entire_line(size_t line_index) const
  750. {
  751. if (line_index >= line_count())
  752. return {};
  753. return { { line_index, 0 }, { line_index, line(line_index).length() } };
  754. }
  755. const TextDocumentSpan* TextDocument::span_at(const TextPosition& position) const
  756. {
  757. for (auto& span : m_spans) {
  758. if (span.range.contains(position))
  759. return &span;
  760. }
  761. return nullptr;
  762. }
  763. void TextDocument::set_unmodified()
  764. {
  765. m_undo_stack.set_current_unmodified();
  766. }
  767. }