TextDocument.cpp 26 KB

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