TextDocument.cpp 26 KB

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