TextDocument.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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_in_range(const TextRange& a_range) const
  248. {
  249. auto range = a_range.normalized();
  250. StringBuilder builder;
  251. for (size_t i = range.start().line(); i <= range.end().line(); ++i) {
  252. auto& line = this->line(i);
  253. size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  254. size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  255. builder.append(Utf32View(line.code_points() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line));
  256. if (i != range.end().line())
  257. builder.append('\n');
  258. }
  259. return builder.to_string();
  260. }
  261. u32 TextDocument::code_point_at(const TextPosition& position) const
  262. {
  263. ASSERT(position.line() < line_count());
  264. auto& line = this->line(position.line());
  265. if (position.column() == line.length())
  266. return '\n';
  267. return line.code_points()[position.column()];
  268. }
  269. TextPosition TextDocument::next_position_after(const TextPosition& position, SearchShouldWrap should_wrap) const
  270. {
  271. auto& line = this->line(position.line());
  272. if (position.column() == line.length()) {
  273. if (position.line() == line_count() - 1) {
  274. if (should_wrap == SearchShouldWrap::Yes)
  275. return { 0, 0 };
  276. return {};
  277. }
  278. return { position.line() + 1, 0 };
  279. }
  280. return { position.line(), position.column() + 1 };
  281. }
  282. TextPosition TextDocument::previous_position_before(const TextPosition& position, SearchShouldWrap should_wrap) const
  283. {
  284. if (position.column() == 0) {
  285. if (position.line() == 0) {
  286. if (should_wrap == SearchShouldWrap::Yes) {
  287. auto& last_line = this->line(line_count() - 1);
  288. return { line_count() - 1, last_line.length() };
  289. }
  290. return {};
  291. }
  292. auto& prev_line = this->line(position.line() - 1);
  293. return { position.line() - 1, prev_line.length() };
  294. }
  295. return { position.line(), position.column() - 1 };
  296. }
  297. TextRange TextDocument::find_next(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap) const
  298. {
  299. if (needle.is_empty())
  300. return {};
  301. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  302. TextPosition original_position = position;
  303. TextPosition start_of_potential_match;
  304. size_t needle_index = 0;
  305. do {
  306. auto ch = code_point_at(position);
  307. // FIXME: This is not the right way to use a Unicode needle!
  308. if (ch == (u32)needle[needle_index]) {
  309. if (needle_index == 0)
  310. start_of_potential_match = position;
  311. ++needle_index;
  312. if (needle_index >= needle.length())
  313. return { start_of_potential_match, next_position_after(position, should_wrap) };
  314. } else {
  315. if (needle_index > 0)
  316. position = start_of_potential_match;
  317. needle_index = 0;
  318. }
  319. position = next_position_after(position, should_wrap);
  320. } while (position.is_valid() && position != original_position);
  321. return {};
  322. }
  323. TextRange TextDocument::find_previous(const StringView& needle, const TextPosition& start, SearchShouldWrap should_wrap) const
  324. {
  325. if (needle.is_empty())
  326. return {};
  327. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  328. position = previous_position_before(position, should_wrap);
  329. TextPosition original_position = position;
  330. TextPosition end_of_potential_match;
  331. size_t needle_index = needle.length() - 1;
  332. do {
  333. auto ch = code_point_at(position);
  334. // FIXME: This is not the right way to use a Unicode needle!
  335. if (ch == (u32)needle[needle_index]) {
  336. if (needle_index == needle.length() - 1)
  337. end_of_potential_match = position;
  338. if (needle_index == 0)
  339. return { position, next_position_after(end_of_potential_match, should_wrap) };
  340. --needle_index;
  341. } else {
  342. if (needle_index < needle.length() - 1)
  343. position = end_of_potential_match;
  344. needle_index = needle.length() - 1;
  345. }
  346. position = previous_position_before(position, should_wrap);
  347. } while (position.is_valid() && position != original_position);
  348. return {};
  349. }
  350. Vector<TextRange> TextDocument::find_all(const StringView& needle) const
  351. {
  352. Vector<TextRange> ranges;
  353. TextPosition position;
  354. for (;;) {
  355. auto range = find_next(needle, position, SearchShouldWrap::No);
  356. if (!range.is_valid())
  357. break;
  358. ranges.append(range);
  359. position = range.end();
  360. }
  361. return ranges;
  362. }
  363. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_before(const TextPosition& position) const
  364. {
  365. for (int i = m_spans.size() - 1; i >= 0; --i) {
  366. if (!m_spans[i].range.contains(position))
  367. continue;
  368. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  369. --i;
  370. if (i <= 0)
  371. return {};
  372. return m_spans[i - 1];
  373. }
  374. return {};
  375. }
  376. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_after(const TextPosition& position) const
  377. {
  378. for (size_t i = 0; i < m_spans.size(); ++i) {
  379. if (!m_spans[i].range.contains(position))
  380. continue;
  381. while ((i + 1) < m_spans.size() && m_spans[i + 1].is_skippable)
  382. ++i;
  383. if (i >= (m_spans.size() - 1))
  384. return {};
  385. return m_spans[i + 1];
  386. }
  387. return {};
  388. }
  389. TextPosition TextDocument::first_word_break_before(const TextPosition& position, bool start_at_column_before) const
  390. {
  391. if (position.column() == 0) {
  392. if (position.line() == 0) {
  393. return TextPosition(0, 0);
  394. }
  395. auto previous_line = this->line(position.line() - 1);
  396. return TextPosition(position.line() - 1, previous_line.length());
  397. }
  398. auto target = position;
  399. auto line = this->line(target.line());
  400. auto is_start_alphanumeric = isalnum(line.code_points()[target.column() - (start_at_column_before ? 1 : 0)]);
  401. while (target.column() > 0) {
  402. auto prev_code_point = line.code_points()[target.column() - 1];
  403. if ((is_start_alphanumeric && !isalnum(prev_code_point)) || (!is_start_alphanumeric && isalnum(prev_code_point)))
  404. break;
  405. target.set_column(target.column() - 1);
  406. }
  407. return target;
  408. }
  409. TextPosition TextDocument::first_word_break_after(const TextPosition& position) const
  410. {
  411. auto target = position;
  412. auto line = this->line(target.line());
  413. if (position.column() >= line.length()) {
  414. if (position.line() >= this->line_count() - 1) {
  415. return position;
  416. }
  417. return TextPosition(position.line() + 1, 0);
  418. }
  419. auto is_start_alphanumeric = isalnum(line.code_points()[target.column()]);
  420. while (target.column() < line.length()) {
  421. auto next_code_point = line.code_points()[target.column()];
  422. if ((is_start_alphanumeric && !isalnum(next_code_point)) || (!is_start_alphanumeric && isalnum(next_code_point)))
  423. break;
  424. target.set_column(target.column() + 1);
  425. }
  426. return target;
  427. }
  428. void TextDocument::undo()
  429. {
  430. if (!can_undo())
  431. return;
  432. m_undo_stack.undo();
  433. notify_did_change();
  434. }
  435. void TextDocument::redo()
  436. {
  437. if (!can_redo())
  438. return;
  439. m_undo_stack.redo();
  440. notify_did_change();
  441. }
  442. void TextDocument::add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand> undo_command)
  443. {
  444. m_undo_stack.push(move(undo_command));
  445. }
  446. TextDocumentUndoCommand::TextDocumentUndoCommand(TextDocument& document)
  447. : m_document(document)
  448. {
  449. }
  450. TextDocumentUndoCommand::~TextDocumentUndoCommand()
  451. {
  452. }
  453. InsertTextCommand::InsertTextCommand(TextDocument& document, const String& text, const TextPosition& position)
  454. : TextDocumentUndoCommand(document)
  455. , m_text(text)
  456. , m_range({ position, position })
  457. {
  458. }
  459. void InsertTextCommand::redo()
  460. {
  461. auto new_cursor = m_document.insert_at(m_range.start(), m_text, m_client);
  462. // NOTE: We don't know where the range ends until after doing redo().
  463. // This is okay since we always do redo() after adding this to the undo stack.
  464. m_range.set_end(new_cursor);
  465. m_document.set_all_cursors(new_cursor);
  466. }
  467. void InsertTextCommand::undo()
  468. {
  469. m_document.remove(m_range);
  470. m_document.set_all_cursors(m_range.start());
  471. }
  472. RemoveTextCommand::RemoveTextCommand(TextDocument& document, const String& text, const TextRange& range)
  473. : TextDocumentUndoCommand(document)
  474. , m_text(text)
  475. , m_range(range)
  476. {
  477. }
  478. void RemoveTextCommand::redo()
  479. {
  480. m_document.remove(m_range);
  481. m_document.set_all_cursors(m_range.start());
  482. }
  483. void RemoveTextCommand::undo()
  484. {
  485. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  486. m_document.set_all_cursors(new_cursor);
  487. }
  488. void TextDocument::update_undo_timer()
  489. {
  490. m_undo_stack.finalize_current_combo();
  491. }
  492. TextPosition TextDocument::insert_at(const TextPosition& position, const StringView& text, const Client* client)
  493. {
  494. TextPosition cursor = position;
  495. Utf8View utf8_view(text);
  496. for (auto code_point : utf8_view)
  497. cursor = insert_at(cursor, code_point, client);
  498. return cursor;
  499. }
  500. TextPosition TextDocument::insert_at(const TextPosition& position, u32 code_point, const Client* client)
  501. {
  502. bool automatic_indentation_enabled = client ? client->is_automatic_indentation_enabled() : false;
  503. size_t m_soft_tab_width = client ? client->soft_tab_width() : 4;
  504. bool at_head = position.column() == 0;
  505. bool at_tail = position.column() == line(position.line()).length();
  506. if (code_point == '\n') {
  507. if (at_tail || at_head) {
  508. String new_line_contents;
  509. if (automatic_indentation_enabled && at_tail) {
  510. size_t leading_spaces = 0;
  511. auto& old_line = lines()[position.line()];
  512. for (size_t i = 0; i < old_line.length(); ++i) {
  513. if (old_line.code_points()[i] == ' ')
  514. ++leading_spaces;
  515. else
  516. break;
  517. }
  518. if (leading_spaces)
  519. new_line_contents = String::repeated(' ', leading_spaces);
  520. }
  521. size_t row = position.line();
  522. Vector<u32> line_content;
  523. for (size_t i = position.column(); i < line(row).length(); i++)
  524. line_content.append(line(row).code_points()[i]);
  525. insert_line(position.line() + (at_tail ? 1 : 0), make<TextDocumentLine>(*this, new_line_contents));
  526. notify_did_change();
  527. return { position.line() + 1, line(position.line() + 1).length() };
  528. }
  529. auto new_line = make<TextDocumentLine>(*this);
  530. new_line->append(*this, line(position.line()).code_points() + position.column(), line(position.line()).length() - position.column());
  531. Vector<u32> line_content;
  532. for (size_t i = 0; i < new_line->length(); i++)
  533. line_content.append(new_line->code_points()[i]);
  534. line(position.line()).truncate(*this, position.column());
  535. insert_line(position.line() + 1, move(new_line));
  536. notify_did_change();
  537. return { position.line() + 1, 0 };
  538. }
  539. if (code_point == '\t') {
  540. size_t next_soft_tab_stop = ((position.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  541. size_t spaces_to_insert = next_soft_tab_stop - position.column();
  542. for (size_t i = 0; i < spaces_to_insert; ++i) {
  543. line(position.line()).insert(*this, position.column(), ' ');
  544. }
  545. notify_did_change();
  546. return { position.line(), next_soft_tab_stop };
  547. }
  548. line(position.line()).insert(*this, position.column(), code_point);
  549. notify_did_change();
  550. return { position.line(), position.column() + 1 };
  551. }
  552. void TextDocument::remove(const TextRange& unnormalized_range)
  553. {
  554. if (!unnormalized_range.is_valid())
  555. return;
  556. auto range = unnormalized_range.normalized();
  557. // First delete all the lines in between the first and last one.
  558. for (size_t i = range.start().line() + 1; i < range.end().line();) {
  559. remove_line(i);
  560. range.end().set_line(range.end().line() - 1);
  561. }
  562. if (range.start().line() == range.end().line()) {
  563. // Delete within same line.
  564. auto& line = this->line(range.start().line());
  565. bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length();
  566. if (whole_line_is_selected) {
  567. line.clear(*this);
  568. } else {
  569. line.remove_range(*this, range.start().column(), range.end().column() - range.start().column());
  570. }
  571. } else {
  572. // Delete across a newline, merging lines.
  573. ASSERT(range.start().line() == range.end().line() - 1);
  574. auto& first_line = line(range.start().line());
  575. auto& second_line = line(range.end().line());
  576. Vector<u32> code_points;
  577. code_points.append(first_line.code_points(), range.start().column());
  578. code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
  579. first_line.set_text(*this, move(code_points));
  580. remove_line(range.end().line());
  581. }
  582. if (lines().is_empty()) {
  583. append_line(make<TextDocumentLine>(*this));
  584. }
  585. notify_did_change();
  586. }
  587. TextRange TextDocument::range_for_entire_line(size_t line_index) const
  588. {
  589. if (line_index >= line_count())
  590. return {};
  591. return { { line_index, 0 }, { line_index, line(line_index).length() } };
  592. }
  593. }