TextDocument.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 <ctype.h>
  33. namespace GUI {
  34. NonnullRefPtr<TextDocument> TextDocument::create(Client* client)
  35. {
  36. return adopt(*new TextDocument(client));
  37. }
  38. TextDocument::TextDocument(Client* client)
  39. {
  40. if (client)
  41. m_clients.set(client);
  42. append_line(make<TextDocumentLine>(*this));
  43. // TODO: Instead of a repating timer, this we should call a delayed 2 sec timer when the user types.
  44. m_undo_timer = Core::Timer::construct(
  45. 2000, [this] {
  46. update_undo_timer();
  47. });
  48. }
  49. TextDocument::~TextDocument()
  50. {
  51. }
  52. void TextDocument::set_text(const StringView& text)
  53. {
  54. m_client_notifications_enabled = false;
  55. m_spans.clear();
  56. remove_all_lines();
  57. size_t start_of_current_line = 0;
  58. auto add_line = [&](size_t current_position) {
  59. size_t line_length = current_position - start_of_current_line;
  60. auto line = make<TextDocumentLine>(*this);
  61. if (line_length)
  62. line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line));
  63. append_line(move(line));
  64. start_of_current_line = current_position + 1;
  65. };
  66. size_t i = 0;
  67. for (i = 0; i < text.length(); ++i) {
  68. if (text[i] == '\n')
  69. add_line(i);
  70. }
  71. add_line(i);
  72. m_client_notifications_enabled = true;
  73. for (auto* client : m_clients)
  74. client->document_did_set_text();
  75. }
  76. size_t TextDocumentLine::first_non_whitespace_column() const
  77. {
  78. for (size_t i = 0; i < length(); ++i) {
  79. auto code_point = code_points()[i];
  80. if (!isspace(code_point))
  81. return i;
  82. }
  83. return length();
  84. }
  85. Optional<size_t> TextDocumentLine::last_non_whitespace_column() const
  86. {
  87. for (ssize_t i = length() - 1; i >= 0; --i) {
  88. auto code_point = code_points()[i];
  89. if (!isspace(code_point))
  90. return i;
  91. }
  92. return {};
  93. }
  94. bool TextDocumentLine::ends_in_whitespace() const
  95. {
  96. if (!length())
  97. return false;
  98. return isspace(code_points()[length() - 1]);
  99. }
  100. String TextDocumentLine::to_utf8() const
  101. {
  102. StringBuilder builder;
  103. builder.append(view());
  104. return builder.to_string();
  105. }
  106. TextDocumentLine::TextDocumentLine(TextDocument& document)
  107. {
  108. clear(document);
  109. }
  110. TextDocumentLine::TextDocumentLine(TextDocument& document, const StringView& text)
  111. {
  112. set_text(document, text);
  113. }
  114. void TextDocumentLine::clear(TextDocument& document)
  115. {
  116. m_text.clear();
  117. document.update_views({});
  118. }
  119. void TextDocumentLine::set_text(TextDocument& document, const Vector<u32> text)
  120. {
  121. m_text = move(text);
  122. document.update_views({});
  123. }
  124. void TextDocumentLine::set_text(TextDocument& document, const StringView& text)
  125. {
  126. if (text.is_empty()) {
  127. clear(document);
  128. return;
  129. }
  130. m_text.clear();
  131. Utf8View utf8_view(text);
  132. for (auto code_point : utf8_view)
  133. m_text.append(code_point);
  134. document.update_views({});
  135. }
  136. void TextDocumentLine::append(TextDocument& document, const u32* code_points, size_t length)
  137. {
  138. if (length == 0)
  139. return;
  140. m_text.append(code_points, length);
  141. document.update_views({});
  142. }
  143. void TextDocumentLine::append(TextDocument& document, u32 code_point)
  144. {
  145. insert(document, length(), code_point);
  146. }
  147. void TextDocumentLine::prepend(TextDocument& document, u32 code_point)
  148. {
  149. insert(document, 0, code_point);
  150. }
  151. void TextDocumentLine::insert(TextDocument& document, size_t index, u32 code_point)
  152. {
  153. if (index == length()) {
  154. m_text.append(code_point);
  155. } else {
  156. m_text.insert(index, code_point);
  157. }
  158. document.update_views({});
  159. }
  160. void TextDocumentLine::remove(TextDocument& document, size_t index)
  161. {
  162. if (index == length()) {
  163. m_text.take_last();
  164. } else {
  165. m_text.remove(index);
  166. }
  167. document.update_views({});
  168. }
  169. void TextDocumentLine::remove_range(TextDocument& document, size_t start, size_t length)
  170. {
  171. ASSERT(length <= m_text.size());
  172. Vector<u32> new_data;
  173. new_data.ensure_capacity(m_text.size() - length);
  174. for (size_t i = 0; i < start; ++i)
  175. new_data.append(m_text[i]);
  176. for (size_t i = (start + length); i < m_text.size(); ++i)
  177. new_data.append(m_text[i]);
  178. m_text = move(new_data);
  179. document.update_views({});
  180. }
  181. void TextDocumentLine::truncate(TextDocument& document, size_t length)
  182. {
  183. m_text.resize(length);
  184. document.update_views({});
  185. }
  186. void TextDocument::append_line(NonnullOwnPtr<TextDocumentLine> line)
  187. {
  188. lines().append(move(line));
  189. if (m_client_notifications_enabled) {
  190. for (auto* client : m_clients)
  191. client->document_did_append_line();
  192. }
  193. }
  194. void TextDocument::insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine> line)
  195. {
  196. lines().insert((int)line_index, move(line));
  197. if (m_client_notifications_enabled) {
  198. for (auto* client : m_clients)
  199. client->document_did_insert_line(line_index);
  200. }
  201. }
  202. void TextDocument::remove_line(size_t line_index)
  203. {
  204. lines().remove((int)line_index);
  205. if (m_client_notifications_enabled) {
  206. for (auto* client : m_clients)
  207. client->document_did_remove_line(line_index);
  208. }
  209. }
  210. void TextDocument::remove_all_lines()
  211. {
  212. lines().clear();
  213. if (m_client_notifications_enabled) {
  214. for (auto* client : m_clients)
  215. client->document_did_remove_all_lines();
  216. }
  217. }
  218. TextDocument::Client::~Client()
  219. {
  220. }
  221. void TextDocument::register_client(Client& client)
  222. {
  223. m_clients.set(&client);
  224. }
  225. void TextDocument::unregister_client(Client& client)
  226. {
  227. m_clients.remove(&client);
  228. }
  229. void TextDocument::update_views(Badge<TextDocumentLine>)
  230. {
  231. notify_did_change();
  232. }
  233. void TextDocument::notify_did_change()
  234. {
  235. if (m_client_notifications_enabled) {
  236. for (auto* client : m_clients)
  237. client->document_did_change();
  238. }
  239. }
  240. void TextDocument::set_all_cursors(const TextPosition& position)
  241. {
  242. if (m_client_notifications_enabled) {
  243. for (auto* client : m_clients)
  244. client->document_did_set_cursor(position);
  245. }
  246. }
  247. String TextDocument::text() const
  248. {
  249. StringBuilder builder;
  250. for (size_t i = 0; i < line_count(); ++i) {
  251. auto& line = this->line(i);
  252. builder.append(line.view());
  253. if (i != line_count() - 1)
  254. builder.append('\n');
  255. }
  256. return builder.to_string();
  257. }
  258. String TextDocument::text_in_range(const TextRange& a_range) const
  259. {
  260. auto range = a_range.normalized();
  261. StringBuilder builder;
  262. for (size_t i = range.start().line(); i <= range.end().line(); ++i) {
  263. auto& line = this->line(i);
  264. size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  265. size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  266. builder.append(Utf32View(line.code_points() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line));
  267. if (i != range.end().line())
  268. builder.append('\n');
  269. }
  270. return builder.to_string();
  271. }
  272. u32 TextDocument::code_point_at(const TextPosition& position) const
  273. {
  274. ASSERT(position.line() < line_count());
  275. auto& line = this->line(position.line());
  276. if (position.column() == line.length())
  277. return '\n';
  278. return line.code_points()[position.column()];
  279. }
  280. TextPosition TextDocument::next_position_after(const TextPosition& position, SearchShouldWrap should_wrap) const
  281. {
  282. auto& line = this->line(position.line());
  283. if (position.column() == line.length()) {
  284. if (position.line() == line_count() - 1) {
  285. if (should_wrap == SearchShouldWrap::Yes)
  286. return { 0, 0 };
  287. return {};
  288. }
  289. return { position.line() + 1, 0 };
  290. }
  291. return { position.line(), position.column() + 1 };
  292. }
  293. TextPosition TextDocument::previous_position_before(const TextPosition& position, SearchShouldWrap should_wrap) const
  294. {
  295. if (position.column() == 0) {
  296. if (position.line() == 0) {
  297. if (should_wrap == SearchShouldWrap::Yes) {
  298. auto& last_line = this->line(line_count() - 1);
  299. return { line_count() - 1, last_line.length() };
  300. }
  301. return {};
  302. }
  303. auto& prev_line = this->line(position.line() - 1);
  304. return { position.line() - 1, prev_line.length() };
  305. }
  306. return { position.line(), position.column() - 1 };
  307. }
  308. TextRange TextDocument::find_next(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap) const
  309. {
  310. if (needle.is_empty())
  311. return {};
  312. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  313. TextPosition original_position = position;
  314. TextPosition start_of_potential_match;
  315. size_t needle_index = 0;
  316. do {
  317. auto ch = code_point_at(position);
  318. // FIXME: This is not the right way to use a Unicode needle!
  319. if (ch == (u32)needle[needle_index]) {
  320. if (needle_index == 0)
  321. start_of_potential_match = position;
  322. ++needle_index;
  323. if (needle_index >= needle.length())
  324. return { start_of_potential_match, next_position_after(position, should_wrap) };
  325. } else {
  326. if (needle_index > 0)
  327. position = start_of_potential_match;
  328. needle_index = 0;
  329. }
  330. position = next_position_after(position, should_wrap);
  331. } while (position.is_valid() && position != original_position);
  332. return {};
  333. }
  334. TextRange TextDocument::find_previous(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap) const
  335. {
  336. if (needle.is_empty())
  337. return {};
  338. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  339. position = previous_position_before(position, should_wrap);
  340. TextPosition original_position = position;
  341. TextPosition end_of_potential_match;
  342. size_t needle_index = needle.length() - 1;
  343. do {
  344. auto ch = code_point_at(position);
  345. // FIXME: This is not the right way to use a Unicode needle!
  346. if (ch == (u32)needle[needle_index]) {
  347. if (needle_index == needle.length() - 1)
  348. end_of_potential_match = position;
  349. if (needle_index == 0)
  350. return { position, next_position_after(end_of_potential_match, should_wrap) };
  351. --needle_index;
  352. } else {
  353. if (needle_index < needle.length() - 1)
  354. position = end_of_potential_match;
  355. needle_index = needle.length() - 1;
  356. }
  357. position = previous_position_before(position, should_wrap);
  358. } while (position.is_valid() && position != original_position);
  359. return {};
  360. }
  361. Vector<TextRange> TextDocument::find_all(const StringView& needle) const
  362. {
  363. Vector<TextRange> ranges;
  364. TextPosition position;
  365. for (;;) {
  366. auto range = find_next(needle, position, SearchShouldWrap::No);
  367. if (!range.is_valid())
  368. break;
  369. ranges.append(range);
  370. position = range.end();
  371. }
  372. return ranges;
  373. }
  374. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_before(const TextPosition& position) const
  375. {
  376. for (int i = m_spans.size() - 1; i >= 0; --i) {
  377. if (!m_spans[i].range.contains(position))
  378. continue;
  379. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  380. --i;
  381. if (i <= 0)
  382. return {};
  383. return m_spans[i - 1];
  384. }
  385. return {};
  386. }
  387. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_after(const TextPosition& position) const
  388. {
  389. for (size_t i = 0; i < m_spans.size(); ++i) {
  390. if (!m_spans[i].range.contains(position))
  391. continue;
  392. while ((i + 1) < m_spans.size() && m_spans[i + 1].is_skippable)
  393. ++i;
  394. if (i >= (m_spans.size() - 1))
  395. return {};
  396. return m_spans[i + 1];
  397. }
  398. return {};
  399. }
  400. TextPosition TextDocument::first_word_break_before(const TextPosition& position, bool start_at_column_before) const
  401. {
  402. if (position.column() == 0) {
  403. if (position.line() == 0) {
  404. return TextPosition(0, 0);
  405. }
  406. auto previous_line = this->line(position.line() - 1);
  407. return TextPosition(position.line() - 1, previous_line.length());
  408. }
  409. auto target = position;
  410. auto line = this->line(target.line());
  411. auto is_start_alphanumeric = isalnum(line.code_points()[target.column() - (start_at_column_before ? 1 : 0)]);
  412. while (target.column() > 0) {
  413. auto prev_code_point = line.code_points()[target.column() - 1];
  414. if ((is_start_alphanumeric && !isalnum(prev_code_point)) || (!is_start_alphanumeric && isalnum(prev_code_point)))
  415. break;
  416. target.set_column(target.column() - 1);
  417. }
  418. return target;
  419. }
  420. TextPosition TextDocument::first_word_break_after(const TextPosition& position) const
  421. {
  422. auto target = position;
  423. auto line = this->line(target.line());
  424. if (position.column() >= line.length()) {
  425. if (position.line() >= this->line_count() - 1) {
  426. return position;
  427. }
  428. return TextPosition(position.line() + 1, 0);
  429. }
  430. auto is_start_alphanumeric = isalnum(line.code_points()[target.column()]);
  431. while (target.column() < line.length()) {
  432. auto next_code_point = line.code_points()[target.column()];
  433. if ((is_start_alphanumeric && !isalnum(next_code_point)) || (!is_start_alphanumeric && isalnum(next_code_point)))
  434. break;
  435. target.set_column(target.column() + 1);
  436. }
  437. return target;
  438. }
  439. void TextDocument::undo()
  440. {
  441. if (!can_undo())
  442. return;
  443. m_undo_stack.undo();
  444. notify_did_change();
  445. }
  446. void TextDocument::redo()
  447. {
  448. if (!can_redo())
  449. return;
  450. m_undo_stack.redo();
  451. notify_did_change();
  452. }
  453. void TextDocument::add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand> undo_command)
  454. {
  455. m_undo_stack.push(move(undo_command));
  456. }
  457. TextDocumentUndoCommand::TextDocumentUndoCommand(TextDocument& document)
  458. : m_document(document)
  459. {
  460. }
  461. TextDocumentUndoCommand::~TextDocumentUndoCommand()
  462. {
  463. }
  464. InsertTextCommand::InsertTextCommand(TextDocument& document, const String& text, const TextPosition& position)
  465. : TextDocumentUndoCommand(document)
  466. , m_text(text)
  467. , m_range({ position, position })
  468. {
  469. }
  470. void InsertTextCommand::redo()
  471. {
  472. auto new_cursor = m_document.insert_at(m_range.start(), m_text, m_client);
  473. // NOTE: We don't know where the range ends until after doing redo().
  474. // This is okay since we always do redo() after adding this to the undo stack.
  475. m_range.set_end(new_cursor);
  476. m_document.set_all_cursors(new_cursor);
  477. }
  478. void InsertTextCommand::undo()
  479. {
  480. m_document.remove(m_range);
  481. m_document.set_all_cursors(m_range.start());
  482. }
  483. RemoveTextCommand::RemoveTextCommand(TextDocument& document, const String& text, const TextRange& range)
  484. : TextDocumentUndoCommand(document)
  485. , m_text(text)
  486. , m_range(range)
  487. {
  488. }
  489. void RemoveTextCommand::redo()
  490. {
  491. m_document.remove(m_range);
  492. m_document.set_all_cursors(m_range.start());
  493. }
  494. void RemoveTextCommand::undo()
  495. {
  496. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  497. m_document.set_all_cursors(new_cursor);
  498. }
  499. void TextDocument::update_undo_timer()
  500. {
  501. m_undo_stack.finalize_current_combo();
  502. }
  503. TextPosition TextDocument::insert_at(const TextPosition& position, const StringView& text, const Client* client)
  504. {
  505. TextPosition cursor = position;
  506. Utf8View utf8_view(text);
  507. for (auto code_point : utf8_view)
  508. cursor = insert_at(cursor, code_point, client);
  509. return cursor;
  510. }
  511. TextPosition TextDocument::insert_at(const TextPosition& position, u32 code_point, const Client* client)
  512. {
  513. bool automatic_indentation_enabled = client ? client->is_automatic_indentation_enabled() : false;
  514. size_t m_soft_tab_width = client ? client->soft_tab_width() : 4;
  515. bool at_head = position.column() == 0;
  516. bool at_tail = position.column() == line(position.line()).length();
  517. if (code_point == '\n') {
  518. if (at_tail || at_head) {
  519. String new_line_contents;
  520. if (automatic_indentation_enabled && at_tail) {
  521. size_t leading_spaces = 0;
  522. auto& old_line = lines()[position.line()];
  523. for (size_t i = 0; i < old_line.length(); ++i) {
  524. if (old_line.code_points()[i] == ' ')
  525. ++leading_spaces;
  526. else
  527. break;
  528. }
  529. if (leading_spaces)
  530. new_line_contents = String::repeated(' ', leading_spaces);
  531. }
  532. size_t row = position.line();
  533. Vector<u32> line_content;
  534. for (size_t i = position.column(); i < line(row).length(); i++)
  535. line_content.append(line(row).code_points()[i]);
  536. insert_line(position.line() + (at_tail ? 1 : 0), make<TextDocumentLine>(*this, new_line_contents));
  537. notify_did_change();
  538. return { position.line() + 1, line(position.line() + 1).length() };
  539. }
  540. auto new_line = make<TextDocumentLine>(*this);
  541. new_line->append(*this, line(position.line()).code_points() + position.column(), line(position.line()).length() - position.column());
  542. Vector<u32> line_content;
  543. for (size_t i = 0; i < new_line->length(); i++)
  544. line_content.append(new_line->code_points()[i]);
  545. line(position.line()).truncate(*this, position.column());
  546. insert_line(position.line() + 1, move(new_line));
  547. notify_did_change();
  548. return { position.line() + 1, 0 };
  549. }
  550. if (code_point == '\t') {
  551. size_t next_soft_tab_stop = ((position.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  552. size_t spaces_to_insert = next_soft_tab_stop - position.column();
  553. for (size_t i = 0; i < spaces_to_insert; ++i) {
  554. line(position.line()).insert(*this, position.column(), ' ');
  555. }
  556. notify_did_change();
  557. return { position.line(), next_soft_tab_stop };
  558. }
  559. line(position.line()).insert(*this, position.column(), code_point);
  560. notify_did_change();
  561. return { position.line(), position.column() + 1 };
  562. }
  563. void TextDocument::remove(const TextRange& unnormalized_range)
  564. {
  565. if (!unnormalized_range.is_valid())
  566. return;
  567. auto range = unnormalized_range.normalized();
  568. // First delete all the lines in between the first and last one.
  569. for (size_t i = range.start().line() + 1; i < range.end().line();) {
  570. remove_line(i);
  571. range.end().set_line(range.end().line() - 1);
  572. }
  573. if (range.start().line() == range.end().line()) {
  574. // Delete within same line.
  575. auto& line = this->line(range.start().line());
  576. bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length();
  577. if (whole_line_is_selected) {
  578. line.clear(*this);
  579. } else {
  580. line.remove_range(*this, range.start().column(), range.end().column() - range.start().column());
  581. }
  582. } else {
  583. // Delete across a newline, merging lines.
  584. ASSERT(range.start().line() == range.end().line() - 1);
  585. auto& first_line = line(range.start().line());
  586. auto& second_line = line(range.end().line());
  587. Vector<u32> code_points;
  588. code_points.append(first_line.code_points(), range.start().column());
  589. code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
  590. first_line.set_text(*this, move(code_points));
  591. remove_line(range.end().line());
  592. }
  593. if (lines().is_empty()) {
  594. append_line(make<TextDocumentLine>(*this));
  595. }
  596. notify_did_change();
  597. }
  598. bool TextDocument::is_empty() const
  599. {
  600. return line_count() == 1 && line(0).is_empty();
  601. }
  602. TextRange TextDocument::range_for_entire_line(size_t line_index) const
  603. {
  604. if (line_index >= line_count())
  605. return {};
  606. return { { line_index, 0 }, { line_index, line(line_index).length() } };
  607. }
  608. }