TextDocument.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Badge.h>
  8. #include <AK/CharacterTypes.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/ScopeGuard.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <AK/StringBuilder.h>
  13. #include <AK/Utf8View.h>
  14. #include <LibCore/Timer.h>
  15. #include <LibGUI/TextDocument.h>
  16. #include <LibRegex/Regex.h>
  17. #include <LibUnicode/CharacterTypes.h>
  18. #include <LibUnicode/Segmentation.h>
  19. namespace GUI {
  20. NonnullRefPtr<TextDocument> TextDocument::create(Client* client)
  21. {
  22. return adopt_ref(*new TextDocument(client));
  23. }
  24. TextDocument::TextDocument(Client* client)
  25. {
  26. if (client)
  27. m_clients.set(client);
  28. append_line(make<TextDocumentLine>(*this));
  29. set_unmodified();
  30. m_undo_stack.on_state_change = [this] {
  31. if (m_client_notifications_enabled) {
  32. for (auto* client : m_clients)
  33. client->document_did_update_undo_stack();
  34. }
  35. };
  36. }
  37. bool TextDocument::set_text(StringView text, AllowCallback allow_callback)
  38. {
  39. m_client_notifications_enabled = false;
  40. m_undo_stack.clear();
  41. m_spans.clear();
  42. remove_all_lines();
  43. ArmedScopeGuard clear_text_guard([this]() {
  44. set_text({});
  45. });
  46. size_t start_of_current_line = 0;
  47. auto add_line = [&](size_t current_position) -> bool {
  48. size_t line_length = current_position - start_of_current_line;
  49. auto line = make<TextDocumentLine>(*this);
  50. bool success = true;
  51. if (line_length)
  52. success = line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line));
  53. if (!success)
  54. return false;
  55. append_line(move(line));
  56. start_of_current_line = current_position + 1;
  57. return true;
  58. };
  59. size_t i = 0;
  60. for (i = 0; i < text.length(); ++i) {
  61. if (text[i] != '\n')
  62. continue;
  63. auto success = add_line(i);
  64. if (!success)
  65. return false;
  66. }
  67. auto success = add_line(i);
  68. if (!success)
  69. return false;
  70. // Don't show the file's trailing newline as an actual new line.
  71. if (line_count() > 1 && line(line_count() - 1).is_empty())
  72. (void)m_lines.take_last();
  73. m_client_notifications_enabled = true;
  74. for (auto* client : m_clients)
  75. client->document_did_set_text(allow_callback);
  76. clear_text_guard.disarm();
  77. // FIXME: Should the modified state be cleared on some of the earlier returns as well?
  78. set_unmodified();
  79. return true;
  80. }
  81. size_t TextDocumentLine::first_non_whitespace_column() const
  82. {
  83. for (size_t i = 0; i < length(); ++i) {
  84. auto code_point = code_points()[i];
  85. if (!is_ascii_space(code_point))
  86. return i;
  87. }
  88. return length();
  89. }
  90. Optional<size_t> TextDocumentLine::last_non_whitespace_column() const
  91. {
  92. for (ssize_t i = length() - 1; i >= 0; --i) {
  93. auto code_point = code_points()[i];
  94. if (!is_ascii_space(code_point))
  95. return i;
  96. }
  97. return {};
  98. }
  99. bool TextDocumentLine::ends_in_whitespace() const
  100. {
  101. if (!length())
  102. return false;
  103. return is_ascii_space(code_points()[length() - 1]);
  104. }
  105. bool TextDocumentLine::can_select() const
  106. {
  107. if (is_empty())
  108. return false;
  109. for (size_t i = 0; i < length(); ++i) {
  110. auto code_point = code_points()[i];
  111. if (code_point != '\n' && code_point != '\r' && code_point != '\f' && code_point != '\v')
  112. return true;
  113. }
  114. return false;
  115. }
  116. size_t TextDocumentLine::leading_spaces() const
  117. {
  118. size_t count = 0;
  119. for (; count < m_text.size(); ++count) {
  120. if (m_text[count] != ' ') {
  121. break;
  122. }
  123. }
  124. return count;
  125. }
  126. DeprecatedString TextDocumentLine::to_utf8() const
  127. {
  128. StringBuilder builder;
  129. builder.append(view());
  130. return builder.to_deprecated_string();
  131. }
  132. TextDocumentLine::TextDocumentLine(TextDocument& document)
  133. {
  134. clear(document);
  135. }
  136. TextDocumentLine::TextDocumentLine(TextDocument& document, StringView text)
  137. {
  138. set_text(document, text);
  139. }
  140. void TextDocumentLine::clear(TextDocument& document)
  141. {
  142. m_text.clear();
  143. document.update_views({});
  144. }
  145. void TextDocumentLine::set_text(TextDocument& document, Vector<u32> const text)
  146. {
  147. m_text = move(text);
  148. document.update_views({});
  149. }
  150. bool TextDocumentLine::set_text(TextDocument& document, StringView text)
  151. {
  152. if (text.is_empty()) {
  153. clear(document);
  154. return true;
  155. }
  156. m_text.clear();
  157. Utf8View utf8_view(text);
  158. if (!utf8_view.validate()) {
  159. return false;
  160. }
  161. for (auto code_point : utf8_view)
  162. m_text.append(code_point);
  163. document.update_views({});
  164. return true;
  165. }
  166. void TextDocumentLine::append(TextDocument& document, u32 const* code_points, size_t length)
  167. {
  168. if (length == 0)
  169. return;
  170. m_text.append(code_points, length);
  171. document.update_views({});
  172. }
  173. void TextDocumentLine::append(TextDocument& document, u32 code_point)
  174. {
  175. insert(document, length(), code_point);
  176. }
  177. void TextDocumentLine::prepend(TextDocument& document, u32 code_point)
  178. {
  179. insert(document, 0, code_point);
  180. }
  181. void TextDocumentLine::insert(TextDocument& document, size_t index, u32 code_point)
  182. {
  183. if (index == length()) {
  184. m_text.append(code_point);
  185. } else {
  186. m_text.insert(index, code_point);
  187. }
  188. document.update_views({});
  189. }
  190. void TextDocumentLine::remove(TextDocument& document, size_t index)
  191. {
  192. if (index == length()) {
  193. m_text.take_last();
  194. } else {
  195. m_text.remove(index);
  196. }
  197. document.update_views({});
  198. }
  199. void TextDocumentLine::remove_range(TextDocument& document, size_t start, size_t length)
  200. {
  201. VERIFY(length <= m_text.size());
  202. Vector<u32> new_data;
  203. new_data.ensure_capacity(m_text.size() - length);
  204. for (size_t i = 0; i < start; ++i)
  205. new_data.append(m_text[i]);
  206. for (size_t i = (start + length); i < m_text.size(); ++i)
  207. new_data.append(m_text[i]);
  208. m_text = move(new_data);
  209. document.update_views({});
  210. }
  211. void TextDocumentLine::keep_range(TextDocument& document, size_t start_index, size_t length)
  212. {
  213. VERIFY(start_index + length < m_text.size());
  214. Vector<u32> new_data;
  215. new_data.ensure_capacity(m_text.size());
  216. for (size_t i = start_index; i <= (start_index + length); i++)
  217. new_data.append(m_text[i]);
  218. m_text = move(new_data);
  219. document.update_views({});
  220. }
  221. void TextDocumentLine::truncate(TextDocument& document, size_t length)
  222. {
  223. m_text.resize(length);
  224. document.update_views({});
  225. }
  226. void TextDocument::append_line(NonnullOwnPtr<TextDocumentLine> line)
  227. {
  228. lines().append(move(line));
  229. if (m_client_notifications_enabled) {
  230. for (auto* client : m_clients)
  231. client->document_did_append_line();
  232. }
  233. }
  234. void TextDocument::insert_line(size_t line_index, NonnullOwnPtr<TextDocumentLine> line)
  235. {
  236. lines().insert(line_index, move(line));
  237. if (m_client_notifications_enabled) {
  238. for (auto* client : m_clients)
  239. client->document_did_insert_line(line_index);
  240. }
  241. }
  242. NonnullOwnPtr<TextDocumentLine> TextDocument::take_line(size_t line_index)
  243. {
  244. auto line = lines().take(line_index);
  245. if (m_client_notifications_enabled) {
  246. for (auto* client : m_clients)
  247. client->document_did_remove_line(line_index);
  248. }
  249. return line;
  250. }
  251. void TextDocument::remove_line(size_t line_index)
  252. {
  253. lines().remove(line_index);
  254. if (m_client_notifications_enabled) {
  255. for (auto* client : m_clients)
  256. client->document_did_remove_line(line_index);
  257. }
  258. }
  259. void TextDocument::remove_all_lines()
  260. {
  261. lines().clear();
  262. if (m_client_notifications_enabled) {
  263. for (auto* client : m_clients)
  264. client->document_did_remove_all_lines();
  265. }
  266. }
  267. void TextDocument::register_client(Client& client)
  268. {
  269. m_clients.set(&client);
  270. }
  271. void TextDocument::unregister_client(Client& client)
  272. {
  273. m_clients.remove(&client);
  274. }
  275. void TextDocument::update_views(Badge<TextDocumentLine>)
  276. {
  277. notify_did_change();
  278. }
  279. void TextDocument::notify_did_change()
  280. {
  281. if (m_client_notifications_enabled) {
  282. for (auto* client : m_clients)
  283. client->document_did_change();
  284. }
  285. m_regex_needs_update = true;
  286. }
  287. void TextDocument::set_all_cursors(TextPosition const& position)
  288. {
  289. if (m_client_notifications_enabled) {
  290. for (auto* client : m_clients)
  291. client->document_did_set_cursor(position);
  292. }
  293. }
  294. DeprecatedString TextDocument::text() const
  295. {
  296. StringBuilder builder;
  297. for (size_t i = 0; i < line_count(); ++i) {
  298. auto& line = this->line(i);
  299. builder.append(line.view());
  300. if (i != line_count() - 1)
  301. builder.append('\n');
  302. }
  303. return builder.to_deprecated_string();
  304. }
  305. DeprecatedString TextDocument::text_in_range(TextRange const& a_range) const
  306. {
  307. auto range = a_range.normalized();
  308. if (is_empty() || line_count() < range.end().line() - range.start().line())
  309. return DeprecatedString("");
  310. StringBuilder builder;
  311. for (size_t i = range.start().line(); i <= range.end().line(); ++i) {
  312. auto& line = this->line(i);
  313. size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0;
  314. size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length();
  315. if (!line.is_empty()) {
  316. builder.append(
  317. Utf32View(
  318. line.code_points() + selection_start_column_on_line,
  319. selection_end_column_on_line - selection_start_column_on_line));
  320. }
  321. if (i != range.end().line())
  322. builder.append('\n');
  323. }
  324. return builder.to_deprecated_string();
  325. }
  326. // This function will return the position of the previous grapheme cluster
  327. // break, relative to the cursor, for "correct looking" parsing of unicode based
  328. // on grapheme cluster boundary algorithm.
  329. size_t TextDocument::get_previous_grapheme_cluster_boundary(TextPosition const& cursor) const
  330. {
  331. if (!cursor.is_valid())
  332. return 0;
  333. auto const& line = this->line(cursor.line());
  334. auto index = Unicode::previous_grapheme_segmentation_boundary(line.view(), cursor.column());
  335. return index.value_or(cursor.column() - 1);
  336. }
  337. // This function will return the position of the next grapheme cluster break,
  338. // relative to the cursor, for "correct looking" parsing of unicode based on
  339. // grapheme cluster boundary algorithm.
  340. size_t TextDocument::get_next_grapheme_cluster_boundary(TextPosition const& cursor) const
  341. {
  342. if (!cursor.is_valid())
  343. return 0;
  344. auto const& line = this->line(cursor.line());
  345. auto index = Unicode::next_grapheme_segmentation_boundary(line.view(), cursor.column());
  346. return index.value_or(cursor.column() + 1);
  347. }
  348. u32 TextDocument::code_point_at(TextPosition const& position) const
  349. {
  350. VERIFY(position.line() < line_count());
  351. auto& line = this->line(position.line());
  352. if (position.column() == line.length())
  353. return '\n';
  354. return line.code_points()[position.column()];
  355. }
  356. TextPosition TextDocument::next_position_after(TextPosition const& position, SearchShouldWrap should_wrap) const
  357. {
  358. auto& line = this->line(position.line());
  359. if (position.column() == line.length()) {
  360. if (position.line() == line_count() - 1) {
  361. if (should_wrap == SearchShouldWrap::Yes)
  362. return { 0, 0 };
  363. return {};
  364. }
  365. return { position.line() + 1, 0 };
  366. }
  367. return { position.line(), position.column() + 1 };
  368. }
  369. TextPosition TextDocument::previous_position_before(TextPosition const& position, SearchShouldWrap should_wrap) const
  370. {
  371. if (position.column() == 0) {
  372. if (position.line() == 0) {
  373. if (should_wrap == SearchShouldWrap::Yes) {
  374. auto& last_line = this->line(line_count() - 1);
  375. return { line_count() - 1, last_line.length() };
  376. }
  377. return {};
  378. }
  379. auto& prev_line = this->line(position.line() - 1);
  380. return { position.line() - 1, prev_line.length() };
  381. }
  382. return { position.line(), position.column() - 1 };
  383. }
  384. void TextDocument::update_regex_matches(StringView needle)
  385. {
  386. if (m_regex_needs_update || needle != m_regex_needle) {
  387. Regex<PosixExtended> re(needle);
  388. Vector<RegexStringView> views;
  389. for (size_t line = 0; line < m_lines.size(); ++line) {
  390. views.append(m_lines.at(line).view());
  391. }
  392. re.search(views, m_regex_result);
  393. m_regex_needs_update = false;
  394. m_regex_needle = DeprecatedString { needle };
  395. m_regex_result_match_index = -1;
  396. m_regex_result_match_capture_group_index = -1;
  397. }
  398. }
  399. TextRange TextDocument::find_next(StringView needle, TextPosition const& start, SearchShouldWrap should_wrap, bool regmatch, bool match_case)
  400. {
  401. if (needle.is_empty())
  402. return {};
  403. if (regmatch) {
  404. if (!m_regex_result.matches.size())
  405. return {};
  406. regex::Match match;
  407. bool use_whole_match { false };
  408. auto next_match = [&] {
  409. m_regex_result_match_capture_group_index = 0;
  410. if (m_regex_result_match_index == m_regex_result.matches.size() - 1) {
  411. if (should_wrap == SearchShouldWrap::Yes)
  412. m_regex_result_match_index = 0;
  413. else
  414. ++m_regex_result_match_index;
  415. } else
  416. ++m_regex_result_match_index;
  417. };
  418. if (m_regex_result.n_capture_groups) {
  419. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  420. next_match();
  421. else {
  422. // check if last capture group has been reached
  423. if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size()) {
  424. next_match();
  425. } else {
  426. // get to the next capture group item
  427. ++m_regex_result_match_capture_group_index;
  428. }
  429. }
  430. // use whole match, if there is no capture group for current index
  431. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  432. use_whole_match = true;
  433. else if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  434. next_match();
  435. } else {
  436. next_match();
  437. }
  438. if (use_whole_match || !m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  439. match = m_regex_result.matches.at(m_regex_result_match_index);
  440. else
  441. match = m_regex_result.capture_group_matches.at(m_regex_result_match_index).at(m_regex_result_match_capture_group_index);
  442. return TextRange { { match.line, match.column }, { match.line, match.column + match.view.length() } };
  443. }
  444. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  445. TextPosition original_position = position;
  446. TextPosition start_of_potential_match;
  447. size_t needle_index = 0;
  448. Utf8View unicode_needle(needle);
  449. Vector<u32> needle_code_points;
  450. for (u32 code_point : unicode_needle)
  451. needle_code_points.append(code_point);
  452. do {
  453. auto ch = code_point_at(position);
  454. bool code_point_matches = false;
  455. if (needle_index >= needle_code_points.size())
  456. code_point_matches = false;
  457. else if (match_case)
  458. code_point_matches = ch == needle_code_points[needle_index];
  459. else
  460. code_point_matches = Unicode::to_unicode_lowercase(ch) == Unicode::to_unicode_lowercase(needle_code_points[needle_index]);
  461. if (code_point_matches) {
  462. if (needle_index == 0)
  463. start_of_potential_match = position;
  464. ++needle_index;
  465. if (needle_index >= needle_code_points.size())
  466. return { start_of_potential_match, next_position_after(position, should_wrap) };
  467. } else {
  468. if (needle_index > 0)
  469. position = start_of_potential_match;
  470. needle_index = 0;
  471. }
  472. position = next_position_after(position, should_wrap);
  473. } while (position.is_valid() && position != original_position);
  474. return {};
  475. }
  476. TextRange TextDocument::find_previous(StringView needle, TextPosition const& start, SearchShouldWrap should_wrap, bool regmatch, bool match_case)
  477. {
  478. if (needle.is_empty())
  479. return {};
  480. if (regmatch) {
  481. if (!m_regex_result.matches.size())
  482. return {};
  483. regex::Match match;
  484. bool use_whole_match { false };
  485. auto next_match = [&] {
  486. if (m_regex_result_match_index == 0) {
  487. if (should_wrap == SearchShouldWrap::Yes)
  488. m_regex_result_match_index = m_regex_result.matches.size() - 1;
  489. else
  490. --m_regex_result_match_index;
  491. } else
  492. --m_regex_result_match_index;
  493. m_regex_result_match_capture_group_index = m_regex_result.capture_group_matches.at(m_regex_result_match_index).size() - 1;
  494. };
  495. if (m_regex_result.n_capture_groups) {
  496. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  497. next_match();
  498. else {
  499. // check if last capture group has been reached
  500. if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size()) {
  501. next_match();
  502. } else {
  503. // get to the next capture group item
  504. --m_regex_result_match_capture_group_index;
  505. }
  506. }
  507. // use whole match, if there is no capture group for current index
  508. if (m_regex_result_match_index >= m_regex_result.capture_group_matches.size())
  509. use_whole_match = true;
  510. else if (m_regex_result_match_capture_group_index >= m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  511. next_match();
  512. } else {
  513. next_match();
  514. }
  515. if (use_whole_match || !m_regex_result.capture_group_matches.at(m_regex_result_match_index).size())
  516. match = m_regex_result.matches.at(m_regex_result_match_index);
  517. else
  518. match = m_regex_result.capture_group_matches.at(m_regex_result_match_index).at(m_regex_result_match_capture_group_index);
  519. return TextRange { { match.line, match.column }, { match.line, match.column + match.view.length() } };
  520. }
  521. TextPosition position = start.is_valid() ? start : TextPosition(0, 0);
  522. position = previous_position_before(position, should_wrap);
  523. if (position.line() >= line_count())
  524. return {};
  525. TextPosition original_position = position;
  526. Utf8View unicode_needle(needle);
  527. Vector<u32> needle_code_points;
  528. for (u32 code_point : unicode_needle)
  529. needle_code_points.append(code_point);
  530. TextPosition end_of_potential_match;
  531. size_t needle_index = needle_code_points.size() - 1;
  532. do {
  533. auto ch = code_point_at(position);
  534. bool code_point_matches = false;
  535. if (needle_index >= needle_code_points.size())
  536. code_point_matches = false;
  537. else if (match_case)
  538. code_point_matches = ch == needle_code_points[needle_index];
  539. else
  540. code_point_matches = Unicode::to_unicode_lowercase(ch) == Unicode::to_unicode_lowercase(needle_code_points[needle_index]);
  541. if (code_point_matches) {
  542. if (needle_index == needle_code_points.size() - 1)
  543. end_of_potential_match = position;
  544. if (needle_index == 0)
  545. return { position, next_position_after(end_of_potential_match, should_wrap) };
  546. --needle_index;
  547. } else {
  548. if (needle_index < needle_code_points.size() - 1)
  549. position = end_of_potential_match;
  550. needle_index = needle_code_points.size() - 1;
  551. }
  552. position = previous_position_before(position, should_wrap);
  553. } while (position.is_valid() && position != original_position);
  554. return {};
  555. }
  556. Vector<TextRange> TextDocument::find_all(StringView needle, bool regmatch, bool match_case)
  557. {
  558. Vector<TextRange> ranges;
  559. TextPosition position;
  560. for (;;) {
  561. auto range = find_next(needle, position, SearchShouldWrap::No, regmatch, match_case);
  562. if (!range.is_valid())
  563. break;
  564. ranges.append(range);
  565. position = range.end();
  566. }
  567. return ranges;
  568. }
  569. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_before(TextPosition const& position) const
  570. {
  571. for (int i = m_spans.size() - 1; i >= 0; --i) {
  572. if (!m_spans[i].range.contains(position))
  573. continue;
  574. while ((i - 1) >= 0 && m_spans[i - 1].is_skippable)
  575. --i;
  576. if (i <= 0)
  577. return {};
  578. return m_spans[i - 1];
  579. }
  580. return {};
  581. }
  582. Optional<TextDocumentSpan> TextDocument::first_non_skippable_span_after(TextPosition const& position) const
  583. {
  584. size_t i = 0;
  585. // Find the first span containing the cursor
  586. for (; i < m_spans.size(); ++i) {
  587. if (m_spans[i].range.contains(position))
  588. break;
  589. }
  590. // Find the first span *after* the cursor
  591. // TODO: For a large number of spans, binary search would be faster.
  592. for (; i < m_spans.size(); ++i) {
  593. if (!m_spans[i].range.contains(position))
  594. break;
  595. }
  596. // Skip skippable spans
  597. for (; i < m_spans.size(); ++i) {
  598. if (!m_spans[i].is_skippable)
  599. break;
  600. }
  601. if (i < m_spans.size())
  602. return m_spans[i];
  603. return {};
  604. }
  605. static bool should_continue_beyond_word(Utf32View const& view)
  606. {
  607. static auto punctuation = Unicode::general_category_from_string("Punctuation"sv);
  608. static auto separator = Unicode::general_category_from_string("Separator"sv);
  609. if (!punctuation.has_value() || !separator.has_value())
  610. return false;
  611. auto has_any_gc = [&](auto code_point, auto&&... categories) {
  612. return (Unicode::code_point_has_general_category(code_point, *categories) || ...);
  613. };
  614. for (auto code_point : view) {
  615. if (!has_any_gc(code_point, punctuation, separator))
  616. return false;
  617. }
  618. return true;
  619. }
  620. TextPosition TextDocument::first_word_break_before(TextPosition const& position, bool start_at_column_before) const
  621. {
  622. if (position.column() == 0) {
  623. if (position.line() == 0) {
  624. return TextPosition(0, 0);
  625. }
  626. auto previous_line = this->line(position.line() - 1);
  627. return TextPosition(position.line() - 1, previous_line.length());
  628. }
  629. auto target = position;
  630. auto const& line = this->line(target.line());
  631. auto modifier = start_at_column_before ? 1 : 0;
  632. if (target.column() == line.length())
  633. modifier = 1;
  634. target.set_column(target.column() - modifier);
  635. while (target.column() < line.length()) {
  636. if (auto index = Unicode::previous_word_segmentation_boundary(line.view(), target.column()); index.has_value()) {
  637. auto view_between_target_and_index = line.view().substring_view(*index, target.column() - *index);
  638. if (should_continue_beyond_word(view_between_target_and_index)) {
  639. target.set_column(*index - 1);
  640. continue;
  641. }
  642. target.set_column(*index);
  643. break;
  644. }
  645. }
  646. return target;
  647. }
  648. TextPosition TextDocument::first_word_break_after(TextPosition const& position) const
  649. {
  650. auto target = position;
  651. auto const& line = this->line(target.line());
  652. if (position.column() >= line.length()) {
  653. if (position.line() >= this->line_count() - 1) {
  654. return position;
  655. }
  656. return TextPosition(position.line() + 1, 0);
  657. }
  658. while (target.column() < line.length()) {
  659. if (auto index = Unicode::next_word_segmentation_boundary(line.view(), target.column()); index.has_value()) {
  660. auto view_between_target_and_index = line.view().substring_view(target.column(), *index - target.column());
  661. if (should_continue_beyond_word(view_between_target_and_index)) {
  662. target.set_column(*index + 1);
  663. continue;
  664. }
  665. target.set_column(*index);
  666. break;
  667. }
  668. }
  669. return target;
  670. }
  671. TextPosition TextDocument::first_word_before(TextPosition const& position, bool start_at_column_before) const
  672. {
  673. if (position.column() == 0) {
  674. if (position.line() == 0) {
  675. return TextPosition(0, 0);
  676. }
  677. auto previous_line = this->line(position.line() - 1);
  678. return TextPosition(position.line() - 1, previous_line.length());
  679. }
  680. auto target = position;
  681. auto line = this->line(target.line());
  682. if (target.column() == line.length())
  683. start_at_column_before = 1;
  684. auto nonblank_passed = !is_ascii_blank(line.code_points()[target.column() - start_at_column_before]);
  685. while (target.column() > 0) {
  686. auto prev_code_point = line.code_points()[target.column() - 1];
  687. nonblank_passed |= !is_ascii_blank(prev_code_point);
  688. if (nonblank_passed && is_ascii_blank(prev_code_point)) {
  689. break;
  690. } else if (is_ascii_punctuation(prev_code_point)) {
  691. target.set_column(target.column() - 1);
  692. break;
  693. }
  694. target.set_column(target.column() - 1);
  695. }
  696. return target;
  697. }
  698. void TextDocument::undo()
  699. {
  700. if (!can_undo())
  701. return;
  702. m_undo_stack.undo();
  703. notify_did_change();
  704. }
  705. void TextDocument::redo()
  706. {
  707. if (!can_redo())
  708. return;
  709. m_undo_stack.redo();
  710. notify_did_change();
  711. }
  712. void TextDocument::add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand> undo_command)
  713. {
  714. m_undo_stack.push(move(undo_command));
  715. }
  716. TextDocumentUndoCommand::TextDocumentUndoCommand(TextDocument& document)
  717. : m_document(document)
  718. {
  719. }
  720. InsertTextCommand::InsertTextCommand(TextDocument& document, DeprecatedString const& text, TextPosition const& position)
  721. : TextDocumentUndoCommand(document)
  722. , m_text(text)
  723. , m_range({ position, position })
  724. {
  725. }
  726. DeprecatedString InsertTextCommand::action_text() const
  727. {
  728. return "Insert Text";
  729. }
  730. bool InsertTextCommand::merge_with(GUI::Command const& other)
  731. {
  732. if (!is<InsertTextCommand>(other) || commit_time_expired())
  733. return false;
  734. auto const& typed_other = static_cast<InsertTextCommand const&>(other);
  735. if (typed_other.m_text.is_whitespace() && !m_text.is_whitespace())
  736. return false; // Skip if other is whitespace while this is not
  737. if (m_range.end() != typed_other.m_range.start())
  738. return false;
  739. if (m_range.start().line() != m_range.end().line())
  740. return false;
  741. StringBuilder builder(m_text.length() + typed_other.m_text.length());
  742. builder.append(m_text);
  743. builder.append(typed_other.m_text);
  744. m_text = builder.to_deprecated_string();
  745. m_range.set_end(typed_other.m_range.end());
  746. m_timestamp = Time::now_monotonic();
  747. return true;
  748. }
  749. void InsertTextCommand::perform_formatting(TextDocument::Client const& client)
  750. {
  751. const size_t tab_width = client.soft_tab_width();
  752. auto const& dest_line = m_document.line(m_range.start().line());
  753. bool const should_auto_indent = client.is_automatic_indentation_enabled();
  754. StringBuilder builder;
  755. size_t column = m_range.start().column();
  756. size_t line_indentation = dest_line.leading_spaces();
  757. bool at_start_of_line = line_indentation == column;
  758. for (auto input_char : m_text) {
  759. if (input_char == '\n') {
  760. size_t spaces_at_end = 0;
  761. if (column < line_indentation)
  762. spaces_at_end = line_indentation - column;
  763. line_indentation -= spaces_at_end;
  764. builder.append('\n');
  765. column = 0;
  766. if (should_auto_indent) {
  767. for (; column < line_indentation; ++column) {
  768. builder.append(' ');
  769. }
  770. }
  771. at_start_of_line = true;
  772. } else if (input_char == '\t') {
  773. size_t next_soft_tab_stop = ((column + tab_width) / tab_width) * tab_width;
  774. size_t spaces_to_insert = next_soft_tab_stop - column;
  775. for (size_t i = 0; i < spaces_to_insert; ++i) {
  776. builder.append(' ');
  777. }
  778. column = next_soft_tab_stop;
  779. if (at_start_of_line) {
  780. line_indentation = column;
  781. }
  782. } else {
  783. if (input_char == ' ') {
  784. if (at_start_of_line) {
  785. ++line_indentation;
  786. }
  787. } else {
  788. at_start_of_line = false;
  789. }
  790. builder.append(input_char);
  791. ++column;
  792. }
  793. }
  794. m_text = builder.to_deprecated_string();
  795. }
  796. void InsertTextCommand::redo()
  797. {
  798. auto new_cursor = m_document.insert_at(m_range.start(), m_text, m_client);
  799. // NOTE: We don't know where the range ends until after doing redo().
  800. // This is okay since we always do redo() after adding this to the undo stack.
  801. m_range.set_end(new_cursor);
  802. m_document.set_all_cursors(new_cursor);
  803. }
  804. void InsertTextCommand::undo()
  805. {
  806. m_document.remove(m_range);
  807. m_document.set_all_cursors(m_range.start());
  808. }
  809. RemoveTextCommand::RemoveTextCommand(TextDocument& document, DeprecatedString const& text, TextRange const& range)
  810. : TextDocumentUndoCommand(document)
  811. , m_text(text)
  812. , m_range(range)
  813. {
  814. }
  815. DeprecatedString RemoveTextCommand::action_text() const
  816. {
  817. return "Remove Text";
  818. }
  819. bool RemoveTextCommand::merge_with(GUI::Command const& other)
  820. {
  821. if (!is<RemoveTextCommand>(other) || commit_time_expired())
  822. return false;
  823. auto const& typed_other = static_cast<RemoveTextCommand const&>(other);
  824. if (m_range.start() != typed_other.m_range.end())
  825. return false;
  826. if (m_range.start().line() != m_range.end().line())
  827. return false;
  828. // Merge backspaces
  829. StringBuilder builder(m_text.length() + typed_other.m_text.length());
  830. builder.append(typed_other.m_text);
  831. builder.append(m_text);
  832. m_text = builder.to_deprecated_string();
  833. m_range.set_start(typed_other.m_range.start());
  834. m_timestamp = Time::now_monotonic();
  835. return true;
  836. }
  837. void RemoveTextCommand::redo()
  838. {
  839. m_document.remove(m_range);
  840. m_document.set_all_cursors(m_range.start());
  841. }
  842. void RemoveTextCommand::undo()
  843. {
  844. auto new_cursor = m_document.insert_at(m_range.start(), m_text);
  845. m_document.set_all_cursors(new_cursor);
  846. }
  847. InsertLineCommand::InsertLineCommand(TextDocument& document, TextPosition cursor, DeprecatedString&& text, InsertPosition pos)
  848. : TextDocumentUndoCommand(document)
  849. , m_cursor(cursor)
  850. , m_text(move(text))
  851. , m_pos(pos)
  852. {
  853. }
  854. void InsertLineCommand::redo()
  855. {
  856. size_t line_number = compute_line_number();
  857. m_document.insert_line(line_number, make<TextDocumentLine>(m_document, m_text));
  858. m_document.set_all_cursors(TextPosition { line_number, m_document.line(line_number).length() });
  859. }
  860. void InsertLineCommand::undo()
  861. {
  862. size_t line_number = compute_line_number();
  863. m_document.remove_line(line_number);
  864. m_document.set_all_cursors(m_cursor);
  865. }
  866. size_t InsertLineCommand::compute_line_number() const
  867. {
  868. if (m_pos == InsertPosition::Above)
  869. return m_cursor.line();
  870. if (m_pos == InsertPosition::Below)
  871. return m_cursor.line() + 1;
  872. VERIFY_NOT_REACHED();
  873. }
  874. DeprecatedString InsertLineCommand::action_text() const
  875. {
  876. StringBuilder action_text_builder;
  877. action_text_builder.append("Insert Line"sv);
  878. if (m_pos == InsertPosition::Above) {
  879. action_text_builder.append(" (Above)"sv);
  880. } else if (m_pos == InsertPosition::Below) {
  881. action_text_builder.append(" (Below)"sv);
  882. } else {
  883. VERIFY_NOT_REACHED();
  884. }
  885. return action_text_builder.to_deprecated_string();
  886. }
  887. ReplaceAllTextCommand::ReplaceAllTextCommand(GUI::TextDocument& document, DeprecatedString const& text, GUI::TextRange const& range, DeprecatedString const& action_text)
  888. : TextDocumentUndoCommand(document)
  889. , m_original_text(document.text())
  890. , m_new_text(text)
  891. , m_range(range)
  892. , m_action_text(action_text)
  893. {
  894. }
  895. void ReplaceAllTextCommand::redo()
  896. {
  897. m_document.remove(m_range);
  898. m_document.set_all_cursors(m_range.start());
  899. auto new_cursor = m_document.insert_at(m_range.start(), m_new_text, m_client);
  900. m_range.set_end(new_cursor);
  901. m_document.set_all_cursors(new_cursor);
  902. }
  903. void ReplaceAllTextCommand::undo()
  904. {
  905. m_document.remove(m_range);
  906. m_document.set_all_cursors(m_range.start());
  907. auto new_cursor = m_document.insert_at(m_range.start(), m_original_text, m_client);
  908. m_range.set_end(new_cursor);
  909. m_document.set_all_cursors(new_cursor);
  910. }
  911. bool ReplaceAllTextCommand::merge_with(GUI::Command const&)
  912. {
  913. return false;
  914. }
  915. DeprecatedString ReplaceAllTextCommand::action_text() const
  916. {
  917. return m_action_text;
  918. }
  919. IndentSelection::IndentSelection(TextDocument& document, size_t tab_width, TextRange const& range)
  920. : TextDocumentUndoCommand(document)
  921. , m_tab_width(tab_width)
  922. , m_range(range)
  923. {
  924. }
  925. void IndentSelection::redo()
  926. {
  927. auto const tab = DeprecatedString::repeated(' ', m_tab_width);
  928. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  929. m_document.insert_at({ i, 0 }, tab, m_client);
  930. }
  931. m_document.set_all_cursors(m_range.start());
  932. }
  933. void IndentSelection::undo()
  934. {
  935. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  936. m_document.remove({ { i, 0 }, { i, m_tab_width } });
  937. }
  938. m_document.set_all_cursors(m_range.start());
  939. }
  940. UnindentSelection::UnindentSelection(TextDocument& document, size_t tab_width, TextRange const& range)
  941. : TextDocumentUndoCommand(document)
  942. , m_tab_width(tab_width)
  943. , m_range(range)
  944. {
  945. }
  946. void UnindentSelection::redo()
  947. {
  948. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  949. if (m_document.line(i).leading_spaces() >= m_tab_width)
  950. m_document.remove({ { i, 0 }, { i, m_tab_width } });
  951. else
  952. m_document.remove({ { i, 0 }, { i, m_document.line(i).leading_spaces() } });
  953. }
  954. m_document.set_all_cursors(m_range.start());
  955. }
  956. void UnindentSelection::undo()
  957. {
  958. auto const tab = DeprecatedString::repeated(' ', m_tab_width);
  959. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++)
  960. m_document.insert_at({ i, 0 }, tab, m_client);
  961. m_document.set_all_cursors(m_range.start());
  962. }
  963. CommentSelection::CommentSelection(TextDocument& document, StringView prefix, StringView suffix, TextRange const& range)
  964. : TextDocumentUndoCommand(document)
  965. , m_prefix(prefix)
  966. , m_suffix(suffix)
  967. , m_range(range)
  968. {
  969. }
  970. void CommentSelection::undo()
  971. {
  972. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  973. if (m_document.line(i).is_empty())
  974. continue;
  975. auto line = m_document.line(i).to_utf8();
  976. auto prefix_start = line.find(m_prefix).value_or(0);
  977. m_document.line(i).keep_range(
  978. m_document,
  979. prefix_start + m_prefix.length(),
  980. m_document.line(i).last_non_whitespace_column().value_or(line.length()) - prefix_start - m_prefix.length() - m_suffix.length());
  981. }
  982. m_document.set_all_cursors(m_range.start());
  983. }
  984. void CommentSelection::redo()
  985. {
  986. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  987. if (m_document.line(i).is_empty())
  988. continue;
  989. m_document.insert_at({ i, 0 }, m_prefix, m_client);
  990. for (auto const& b : m_suffix.bytes()) {
  991. m_document.line(i).append(m_document, b);
  992. }
  993. }
  994. m_document.set_all_cursors(m_range.start());
  995. }
  996. UncommentSelection::UncommentSelection(TextDocument& document, StringView prefix, StringView suffix, TextRange const& range)
  997. : TextDocumentUndoCommand(document)
  998. , m_prefix(prefix)
  999. , m_suffix(suffix)
  1000. , m_range(range)
  1001. {
  1002. }
  1003. void UncommentSelection::undo()
  1004. {
  1005. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  1006. if (m_document.line(i).is_empty())
  1007. continue;
  1008. m_document.insert_at({ i, 0 }, m_prefix, m_client);
  1009. for (auto const& b : m_suffix.bytes()) {
  1010. m_document.line(i).append(m_document, b);
  1011. }
  1012. }
  1013. m_document.set_all_cursors(m_range.start());
  1014. }
  1015. void UncommentSelection::redo()
  1016. {
  1017. for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
  1018. if (m_document.line(i).is_empty())
  1019. continue;
  1020. auto line = m_document.line(i).to_utf8();
  1021. auto prefix_start = line.find(m_prefix).value_or(0);
  1022. m_document.line(i).keep_range(
  1023. m_document,
  1024. prefix_start + m_prefix.length(),
  1025. m_document.line(i).last_non_whitespace_column().value_or(line.length()) - prefix_start - m_prefix.length() - m_suffix.length());
  1026. }
  1027. m_document.set_all_cursors(m_range.start());
  1028. }
  1029. TextPosition TextDocument::insert_at(TextPosition const& position, StringView text, Client const* client)
  1030. {
  1031. TextPosition cursor = position;
  1032. Utf8View utf8_view(text);
  1033. for (auto code_point : utf8_view)
  1034. cursor = insert_at(cursor, code_point, client);
  1035. return cursor;
  1036. }
  1037. TextPosition TextDocument::insert_at(TextPosition const& position, u32 code_point, Client const*)
  1038. {
  1039. if (code_point == '\n') {
  1040. auto new_line = make<TextDocumentLine>(*this);
  1041. new_line->append(*this, line(position.line()).code_points() + position.column(), line(position.line()).length() - position.column());
  1042. line(position.line()).truncate(*this, position.column());
  1043. insert_line(position.line() + 1, move(new_line));
  1044. notify_did_change();
  1045. return { position.line() + 1, 0 };
  1046. } else {
  1047. line(position.line()).insert(*this, position.column(), code_point);
  1048. notify_did_change();
  1049. return { position.line(), position.column() + 1 };
  1050. }
  1051. }
  1052. void TextDocument::remove(TextRange const& unnormalized_range)
  1053. {
  1054. if (!unnormalized_range.is_valid())
  1055. return;
  1056. auto range = unnormalized_range.normalized();
  1057. // First delete all the lines in between the first and last one.
  1058. for (size_t i = range.start().line() + 1; i < range.end().line();) {
  1059. remove_line(i);
  1060. range.end().set_line(range.end().line() - 1);
  1061. }
  1062. if (range.start().line() == range.end().line()) {
  1063. // Delete within same line.
  1064. auto& line = this->line(range.start().line());
  1065. if (line.length() == 0)
  1066. return;
  1067. bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length();
  1068. if (whole_line_is_selected) {
  1069. line.clear(*this);
  1070. } else {
  1071. line.remove_range(*this, range.start().column(), range.end().column() - range.start().column());
  1072. }
  1073. } else {
  1074. // Delete across a newline, merging lines.
  1075. VERIFY(range.start().line() == range.end().line() - 1);
  1076. auto& first_line = line(range.start().line());
  1077. auto& second_line = line(range.end().line());
  1078. Vector<u32> code_points;
  1079. code_points.append(first_line.code_points(), range.start().column());
  1080. if (!second_line.is_empty())
  1081. code_points.append(second_line.code_points() + range.end().column(), second_line.length() - range.end().column());
  1082. first_line.set_text(*this, move(code_points));
  1083. remove_line(range.end().line());
  1084. }
  1085. if (lines().is_empty()) {
  1086. append_line(make<TextDocumentLine>(*this));
  1087. }
  1088. notify_did_change();
  1089. }
  1090. bool TextDocument::is_empty() const
  1091. {
  1092. return line_count() == 1 && line(0).is_empty();
  1093. }
  1094. TextRange TextDocument::range_for_entire_line(size_t line_index) const
  1095. {
  1096. if (line_index >= line_count())
  1097. return {};
  1098. return { { line_index, 0 }, { line_index, line(line_index).length() } };
  1099. }
  1100. TextDocumentSpan const* TextDocument::span_at(TextPosition const& position) const
  1101. {
  1102. for (auto& span : m_spans) {
  1103. if (span.range.contains(position))
  1104. return &span;
  1105. }
  1106. return nullptr;
  1107. }
  1108. void TextDocument::set_unmodified()
  1109. {
  1110. m_undo_stack.set_current_unmodified();
  1111. }
  1112. void TextDocument::set_spans(u32 span_collection_index, Vector<TextDocumentSpan> spans)
  1113. {
  1114. m_span_collections.set(span_collection_index, move(spans));
  1115. merge_span_collections();
  1116. }
  1117. struct SpanAndCollectionIndex {
  1118. TextDocumentSpan span;
  1119. u32 collection_index { 0 };
  1120. };
  1121. void TextDocument::merge_span_collections()
  1122. {
  1123. Vector<SpanAndCollectionIndex> sorted_spans;
  1124. auto collection_indices = m_span_collections.keys();
  1125. quick_sort(collection_indices);
  1126. for (auto collection_index : collection_indices) {
  1127. auto spans = m_span_collections.get(collection_index).value();
  1128. for (auto span : spans) {
  1129. sorted_spans.append({ move(span), collection_index });
  1130. }
  1131. }
  1132. quick_sort(sorted_spans, [](SpanAndCollectionIndex const& a, SpanAndCollectionIndex const& b) {
  1133. if (a.span.range.start() == b.span.range.start()) {
  1134. return a.collection_index < b.collection_index;
  1135. }
  1136. return a.span.range.start() < b.span.range.start();
  1137. });
  1138. // The end of the TextRanges of spans are non-inclusive, i.e span range = [X,y).
  1139. // This transforms the span's range to be inclusive, i.e [X,Y].
  1140. auto adjust_end = [](GUI::TextDocumentSpan span) -> GUI::TextDocumentSpan {
  1141. span.range.set_end({ span.range.end().line(), span.range.end().column() == 0 ? 0 : span.range.end().column() - 1 });
  1142. return span;
  1143. };
  1144. Vector<SpanAndCollectionIndex> merged_spans;
  1145. for (auto& span_and_collection_index : sorted_spans) {
  1146. if (merged_spans.is_empty()) {
  1147. merged_spans.append(span_and_collection_index);
  1148. continue;
  1149. }
  1150. auto const& span = span_and_collection_index.span;
  1151. auto last_span_and_collection_index = merged_spans.last();
  1152. auto const& last_span = last_span_and_collection_index.span;
  1153. if (adjust_end(span).range.start() > adjust_end(last_span).range.end()) {
  1154. // Current span does not intersect with previous one, can simply append to merged list.
  1155. merged_spans.append(span_and_collection_index);
  1156. continue;
  1157. }
  1158. merged_spans.take_last();
  1159. if (span.range.start() > last_span.range.start()) {
  1160. SpanAndCollectionIndex first_part = last_span_and_collection_index;
  1161. first_part.span.range.set_end(span.range.start());
  1162. merged_spans.append(move(first_part));
  1163. }
  1164. SpanAndCollectionIndex merged_span;
  1165. merged_span.collection_index = span_and_collection_index.collection_index;
  1166. merged_span.span.range = { span.range.start(), min(span.range.end(), last_span.range.end()) };
  1167. merged_span.span.is_skippable = span.is_skippable | last_span.is_skippable;
  1168. merged_span.span.data = span.data ? span.data : last_span.data;
  1169. merged_span.span.attributes.color = span_and_collection_index.collection_index > last_span_and_collection_index.collection_index ? span.attributes.color : last_span.attributes.color;
  1170. merged_span.span.attributes.bold = span.attributes.bold | last_span.attributes.bold;
  1171. merged_span.span.attributes.background_color = span.attributes.background_color.has_value() ? span.attributes.background_color.value() : last_span.attributes.background_color;
  1172. merged_span.span.attributes.underline = span.attributes.underline | last_span.attributes.underline;
  1173. merged_span.span.attributes.underline_color = span.attributes.underline_color.has_value() ? span.attributes.underline_color.value() : last_span.attributes.underline_color;
  1174. merged_span.span.attributes.underline_style = span.attributes.underline_style;
  1175. merged_spans.append(move(merged_span));
  1176. if (span.range.end() == last_span.range.end())
  1177. continue;
  1178. if (span.range.end() > last_span.range.end()) {
  1179. SpanAndCollectionIndex last_part = span_and_collection_index;
  1180. last_part.span.range.set_start(last_span.range.end());
  1181. merged_spans.append(move(last_part));
  1182. continue;
  1183. }
  1184. SpanAndCollectionIndex last_part = last_span_and_collection_index;
  1185. last_part.span.range.set_start(span.range.end());
  1186. merged_spans.append(move(last_part));
  1187. }
  1188. m_spans.clear();
  1189. for (auto span : merged_spans) {
  1190. m_spans.append(move(span.span));
  1191. }
  1192. }
  1193. }