TextDocument.cpp 26 KB

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