TextDocument.cpp 26 KB

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