TextDocument.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. VERIFY(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. if (is_empty())
  276. return String("");
  277. auto range = a_range.normalized();
  278. StringBuilder builder;
  279. for (size_t i = range.start().line(); i <= range.end().line(); ++i) {
  280. auto& line = this->line(i);
  281. size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  282. size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  283. builder.append(Utf32View(line.code_points() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line));
  284. if (i != range.end().line())
  285. builder.append('\n');
  286. }
  287. return builder.to_string();
  288. }
  289. u32 TextDocument::code_point_at(const TextPosition& position) const
  290. {
  291. VERIFY(position.line() < line_count());
  292. auto& line = this->line(position.line());
  293. if (position.column() == line.length())
  294. return '\n';
  295. return line.code_points()[position.column()];
  296. }
  297. TextPosition TextDocument::next_position_after(const TextPosition& position, SearchShouldWrap should_wrap) const
  298. {
  299. auto& line = this->line(position.line());
  300. if (position.column() == line.length()) {
  301. if (position.line() == line_count() - 1) {
  302. if (should_wrap == SearchShouldWrap::Yes)
  303. return { 0, 0 };
  304. return {};
  305. }
  306. return { position.line() + 1, 0 };
  307. }
  308. return { position.line(), position.column() + 1 };
  309. }
  310. TextPosition TextDocument::previous_position_before(const TextPosition& position, SearchShouldWrap should_wrap) const
  311. {
  312. if (position.column() == 0) {
  313. if (position.line() == 0) {
  314. if (should_wrap == SearchShouldWrap::Yes) {
  315. auto& last_line = this->line(line_count() - 1);
  316. return { line_count() - 1, last_line.length() };
  317. }
  318. return {};
  319. }
  320. auto& prev_line = this->line(position.line() - 1);
  321. return { position.line() - 1, prev_line.length() };
  322. }
  323. return { position.line(), position.column() - 1 };
  324. }
  325. void TextDocument::update_regex_matches(const StringView& needle)
  326. {
  327. if (m_regex_needs_update || needle != m_regex_needle) {
  328. Regex<PosixExtended> re(needle);
  329. Vector<RegexStringView> views;
  330. for (size_t line = 0; line < m_lines.size(); ++line) {
  331. views.append(m_lines.at(line).view());
  332. }
  333. re.search(views, m_regex_result);
  334. m_regex_needs_update = false;
  335. m_regex_needle = String { needle };
  336. m_regex_result_match_index = -1;
  337. m_regex_result_match_capture_group_index = -1;
  338. }
  339. }
  340. TextRange TextDocument::find_next(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap, bool regmatch, bool match_case)
  341. {
  342. if (needle.is_empty())
  343. return {};
  344. if (regmatch) {
  345. if (!m_regex_result.matches.size())
  346. return {};
  347. regex::Match match;
  348. bool use_whole_match { false };
  349. auto next_match = [&] {
  350. m_regex_result_match_capture_group_index = 0;
  351. if (m_regex_result_match_index == m_regex_result.matches.size() - 1) {
  352. if (should_wrap == SearchShouldWrap::Yes)
  353. m_regex_result_match_index = 0;
  354. else
  355. ++m_regex_result_match_index;
  356. } else
  357. ++m_regex_result_match_index;
  358. };
  359. if (m_regex_result.n_capture_groups) {
  360. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  361. next_match();
  362. else {
  363. // check if last capture group has been reached
  364. if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size()) {
  365. next_match();
  366. } else {
  367. // get to the next capture group item
  368. ++m_regex_result_match_capture_group_index;
  369. }
  370. }
  371. // use whole match, if there is no capture group for current index
  372. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  373. use_whole_match = true;
  374. else if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  375. next_match();
  376. } else {
  377. next_match();
  378. }
  379. if (use_whole_match || !m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  380. match = m_regex_result.matches.at(m_regex_result_match_index);
  381. else
  382. match = m_regex_result.capture_group_matches.at(m_regex_result_match_index).at(m_regex_result_match_capture_group_index);
  383. return TextRange { { match.line, match.column }, { match.line, match.column + match.view.length() } };
  384. }
  385. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  386. TextPosition original_position = position;
  387. TextPosition start_of_potential_match;
  388. size_t needle_index = 0;
  389. do {
  390. auto ch = code_point_at(position);
  391. // FIXME: This is not the right way to use a Unicode needle!
  392. if (match_case ? ch == (u32)needle[needle_index] : tolower(ch) == tolower((u32)needle[needle_index])) {
  393. if (needle_index == 0)
  394. start_of_potential_match = position;
  395. ++needle_index;
  396. if (needle_index >= needle.length())
  397. return { start_of_potential_match, next_position_after(position, should_wrap) };
  398. } else {
  399. if (needle_index > 0)
  400. position = start_of_potential_match;
  401. needle_index = 0;
  402. }
  403. position = next_position_after(position, should_wrap);
  404. } while (position.is_valid() && position != original_position);
  405. return {};
  406. }
  407. TextRange TextDocument::find_previous(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap, bool regmatch, bool match_case)
  408. {
  409. if (needle.is_empty())
  410. return {};
  411. if (regmatch) {
  412. if (!m_regex_result.matches.size())
  413. return {};
  414. regex::Match match;
  415. bool use_whole_match { false };
  416. auto next_match = [&] {
  417. if (m_regex_result_match_index == 0) {
  418. if (should_wrap == SearchShouldWrap::Yes)
  419. m_regex_result_match_index = m_regex_result.matches.size() - 1;
  420. else
  421. --m_regex_result_match_index;
  422. } else
  423. --m_regex_result_match_index;
  424. m_regex_result_match_capture_group_index = m_regex_result.capture_group_matches.at(m_regex_result_match_index).size() - 1;
  425. };
  426. if (m_regex_result.n_capture_groups) {
  427. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  428. next_match();
  429. else {
  430. // check if last capture group has been reached
  431. if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size()) {
  432. next_match();
  433. } else {
  434. // get to the next capture group item
  435. --m_regex_result_match_capture_group_index;
  436. }
  437. }
  438. // use whole match, if there is no capture group for current index
  439. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  440. use_whole_match = true;
  441. else if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  442. next_match();
  443. } else {
  444. next_match();
  445. }
  446. if (use_whole_match || !m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  447. match = m_regex_result.matches.at(m_regex_result_match_index);
  448. else
  449. match = m_regex_result.capture_group_matches.at(m_regex_result_match_index).at(m_regex_result_match_capture_group_index);
  450. return TextRange { { match.line, match.column }, { match.line, match.column + match.view.length() } };
  451. }
  452. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  453. position = previous_position_before(position, should_wrap);
  454. if (position.line() >= line_count())
  455. return {};
  456. TextPosition original_position = position;
  457. TextPosition end_of_potential_match;
  458. size_t needle_index = needle.length() - 1;
  459. do {
  460. auto ch = code_point_at(position);
  461. // FIXME: This is not the right way to use a Unicode needle!
  462. if (match_case ? ch == (u32)needle[needle_index] : tolower(ch) == tolower((u32)needle[needle_index])) {
  463. if (needle_index == needle.length() - 1)
  464. end_of_potential_match = position;
  465. if (needle_index == 0)
  466. return { position, next_position_after(end_of_potential_match, should_wrap) };
  467. --needle_index;
  468. } else {
  469. if (needle_index < needle.length() - 1)
  470. position = end_of_potential_match;
  471. needle_index = needle.length() - 1;
  472. }
  473. position = previous_position_before(position, should_wrap);
  474. } while (position.is_valid() && position != original_position);
  475. return {};
  476. }
  477. Vector<TextRange> TextDocument::find_all(const StringView& needle, bool regmatch)
  478. {
  479. Vector<TextRange> ranges;
  480. TextPosition position;
  481. for (;;) {
  482. auto range = find_next(needle, position, SearchShouldWrap::No, regmatch);
  483. if (!range.is_valid())
  484. break;
  485. ranges.append(range);
  486. position = range.end();
  487. }
  488. return ranges;
  489. }
  490. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_before(const TextPosition& position) const
  491. {
  492. for (int i = m_spans.size() - 1; i >= 0; --i) {
  493. if (!m_spans[i].range.contains(position))
  494. continue;
  495. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  496. --i;
  497. if (i <= 0)
  498. return {};
  499. return m_spans[i - 1];
  500. }
  501. return {};
  502. }
  503. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_after(const TextPosition& position) const
  504. {
  505. for (size_t i = 0; i < m_spans.size(); ++i) {
  506. if (!m_spans[i].range.contains(position))
  507. continue;
  508. while ((i + 1) < m_spans.size() && m_spans[i + 1].is_skippable)
  509. ++i;
  510. if (i >= (m_spans.size() - 1))
  511. return {};
  512. return m_spans[i + 1];
  513. }
  514. return {};
  515. }
  516. TextPosition TextDocument::first_word_break_before(const TextPosition& position, bool start_at_column_before) const
  517. {
  518. if (position.column() == 0) {
  519. if (position.line() == 0) {
  520. return TextPosition(0, 0);
  521. }
  522. auto previous_line = this->line(position.line() - 1);
  523. return TextPosition(position.line() - 1, previous_line.length());
  524. }
  525. auto target = position;
  526. auto line = this->line(target.line());
  527. auto is_start_alphanumeric = isalnum(line.code_points()[target.column() - (start_at_column_before ? 1 : 0)]);
  528. while (target.column() > 0) {
  529. auto prev_code_point = line.code_points()[target.column() - 1];
  530. if ((is_start_alphanumeric && !isalnum(prev_code_point)) || (!is_start_alphanumeric && isalnum(prev_code_point)))
  531. break;
  532. target.set_column(target.column() - 1);
  533. }
  534. return target;
  535. }
  536. TextPosition TextDocument::first_word_break_after(const TextPosition& position) const
  537. {
  538. auto target = position;
  539. auto line = this->line(target.line());
  540. if (position.column() >= line.length()) {
  541. if (position.line() >= this->line_count() - 1) {
  542. return position;
  543. }
  544. return TextPosition(position.line() + 1, 0);
  545. }
  546. auto is_start_alphanumeric = isalnum(line.code_points()[target.column()]);
  547. while (target.column() < line.length()) {
  548. auto next_code_point = line.code_points()[target.column()];
  549. if ((is_start_alphanumeric && !isalnum(next_code_point)) || (!is_start_alphanumeric && isalnum(next_code_point)))
  550. break;
  551. target.set_column(target.column() + 1);
  552. }
  553. return target;
  554. }
  555. void TextDocument::undo()
  556. {
  557. if (!can_undo())
  558. return;
  559. m_undo_stack.undo();
  560. notify_did_change();
  561. }
  562. void TextDocument::redo()
  563. {
  564. if (!can_redo())
  565. return;
  566. m_undo_stack.redo();
  567. notify_did_change();
  568. }
  569. void TextDocument::add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand> undo_command)
  570. {
  571. m_undo_stack.push(move(undo_command));
  572. }
  573. TextDocumentUndoCommand::TextDocumentUndoCommand(TextDocument& document)
  574. : m_document(document)
  575. {
  576. }
  577. TextDocumentUndoCommand::~TextDocumentUndoCommand()
  578. {
  579. }
  580. InsertTextCommand::InsertTextCommand(TextDocument& document, const String& text, const TextPosition& position)
  581. : TextDocumentUndoCommand(document)
  582. , m_text(text)
  583. , m_range({ position, position })
  584. {
  585. }
  586. void InsertTextCommand::perform_formatting(const TextDocument::Client& client)
  587. {
  588. const size_t tab_width = client.soft_tab_width();
  589. const auto& dest_line = m_document.line(m_range.start().line());
  590. const bool should_auto_indent = client.is_automatic_indentation_enabled();
  591. StringBuilder builder;
  592. size_t column = m_range.start().column();
  593. size_t line_indentation = dest_line.leading_spaces();
  594. bool at_start_of_line = line_indentation == column;
  595. for (auto input_char : m_text) {
  596. if (input_char == '\n') {
  597. builder.append('\n');
  598. column = 0;
  599. if (should_auto_indent) {
  600. for (; column < line_indentation; ++column) {
  601. builder.append(' ');
  602. }
  603. }
  604. at_start_of_line = true;
  605. } else if (input_char == '\t') {
  606. size_t next_soft_tab_stop = ((column + tab_width) / tab_width) * tab_width;
  607. size_t spaces_to_insert = next_soft_tab_stop - column;
  608. for (size_t i = 0; i < spaces_to_insert; ++i) {
  609. builder.append(' ');
  610. }
  611. column = next_soft_tab_stop;
  612. if (at_start_of_line) {
  613. line_indentation = column;
  614. }
  615. } else {
  616. if (input_char == ' ') {
  617. if (at_start_of_line) {
  618. ++line_indentation;
  619. }
  620. } else {
  621. at_start_of_line = false;
  622. }
  623. builder.append(input_char);
  624. ++column;
  625. }
  626. }
  627. m_text = builder.build();
  628. }
  629. void InsertTextCommand::redo()
  630. {
  631. auto new_cursor = m_document.insert_at(m_range.start(), m_text, m_client);
  632. // NOTE: We don't know where the range ends until after doing redo().
  633. // This is okay since we always do redo() after adding this to the undo stack.
  634. m_range.set_end(new_cursor);
  635. m_document.set_all_cursors(new_cursor);
  636. }
  637. void InsertTextCommand::undo()
  638. {
  639. m_document.remove(m_range);
  640. m_document.set_all_cursors(m_range.start());
  641. }
  642. RemoveTextCommand::RemoveTextCommand(TextDocument& document, const String& text, const TextRange& range)
  643. : TextDocumentUndoCommand(document)
  644. , m_text(text)
  645. , m_range(range)
  646. {
  647. }
  648. void RemoveTextCommand::redo()
  649. {
  650. m_document.remove(m_range);
  651. m_document.set_all_cursors(m_range.start());
  652. }
  653. void RemoveTextCommand::undo()
  654. {
  655. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  656. m_document.set_all_cursors(new_cursor);
  657. }
  658. void TextDocument::update_undo_timer()
  659. {
  660. m_undo_stack.finalize_current_combo();
  661. }
  662. TextPosition TextDocument::insert_at(const TextPosition& position, const StringView& text, const Client* client)
  663. {
  664. TextPosition cursor = position;
  665. Utf8View utf8_view(text);
  666. for (auto code_point : utf8_view)
  667. cursor = insert_at(cursor, code_point, client);
  668. return cursor;
  669. }
  670. TextPosition TextDocument::insert_at(const TextPosition& position, u32 code_point, const Client*)
  671. {
  672. if (code_point == '\n') {
  673. auto new_line = make<TextDocumentLine>(*this);
  674. new_line->append(*this, line(position.line()).code_points() + position.column(), line(position.line()).length() - position.column());
  675. line(position.line()).truncate(*this, position.column());
  676. insert_line(position.line() + 1, move(new_line));
  677. notify_did_change();
  678. return { position.line() + 1, 0 };
  679. } else {
  680. line(position.line()).insert(*this, position.column(), code_point);
  681. notify_did_change();
  682. return { position.line(), position.column() + 1 };
  683. }
  684. }
  685. void TextDocument::remove(const TextRange& unnormalized_range)
  686. {
  687. if (!unnormalized_range.is_valid())
  688. return;
  689. auto range = unnormalized_range.normalized();
  690. // First delete all the lines in between the first and last one.
  691. for (size_t i = range.start().line() + 1; i < range.end().line();) {
  692. remove_line(i);
  693. range.end().set_line(range.end().line() - 1);
  694. }
  695. if (range.start().line() == range.end().line()) {
  696. // Delete within same line.
  697. auto& line = this->line(range.start().line());
  698. bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length();
  699. if (whole_line_is_selected) {
  700. line.clear(*this);
  701. } else {
  702. line.remove_range(*this, range.start().column(), range.end().column() - range.start().column());
  703. }
  704. } else {
  705. // Delete across a newline, merging lines.
  706. VERIFY(range.start().line() == range.end().line() - 1);
  707. auto& first_line = line(range.start().line());
  708. auto& second_line = line(range.end().line());
  709. Vector<u32> code_points;
  710. code_points.append(first_line.code_points(), range.start().column());
  711. code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
  712. first_line.set_text(*this, move(code_points));
  713. remove_line(range.end().line());
  714. }
  715. if (lines().is_empty()) {
  716. append_line(make<TextDocumentLine>(*this));
  717. }
  718. notify_did_change();
  719. }
  720. bool TextDocument::is_empty() const
  721. {
  722. return line_count() == 1 && line(0).is_empty();
  723. }
  724. TextRange TextDocument::range_for_entire_line(size_t line_index) const
  725. {
  726. if (line_index >= line_count())
  727. return {};
  728. return { { line_index, 0 }, { line_index, line(line_index).length() } };
  729. }
  730. const TextDocumentSpan* TextDocument::span_at(const TextPosition& position) const
  731. {
  732. for (auto& span : m_spans) {
  733. if (span.range.contains(position))
  734. return &span;
  735. }
  736. return nullptr;
  737. }
  738. }