EditingEngine.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibGUI/EditingEngine.h>
  8. #include <LibGUI/Event.h>
  9. #include <LibGUI/TextEditor.h>
  10. namespace GUI {
  11. constexpr bool is_vim_alphanumeric(u32 code_point)
  12. {
  13. return is_ascii_alphanumeric(code_point) || code_point == '_';
  14. }
  15. constexpr bool is_vim_punctuation(u32 code_point)
  16. {
  17. return is_ascii_punctuation(code_point) && code_point != '_';
  18. }
  19. EditingEngine::~EditingEngine()
  20. {
  21. }
  22. void EditingEngine::attach(TextEditor& editor)
  23. {
  24. VERIFY(!m_editor);
  25. m_editor = editor;
  26. }
  27. void EditingEngine::detach()
  28. {
  29. VERIFY(m_editor);
  30. m_editor = nullptr;
  31. }
  32. bool EditingEngine::on_key(const KeyEvent& event)
  33. {
  34. if (event.key() == KeyCode::Key_Left) {
  35. if (!event.shift() && m_editor->selection().is_valid()) {
  36. m_editor->set_cursor(m_editor->selection().normalized().start());
  37. m_editor->selection().clear();
  38. m_editor->did_update_selection();
  39. if (!event.ctrl()) {
  40. m_editor->update();
  41. return true;
  42. }
  43. }
  44. if (event.ctrl()) {
  45. m_editor->update_selection(event.shift());
  46. move_to_previous_span();
  47. if (event.shift() && m_editor->selection().start().is_valid()) {
  48. m_editor->selection().set_end(m_editor->cursor());
  49. m_editor->did_update_selection();
  50. }
  51. return true;
  52. }
  53. m_editor->update_selection(event.shift());
  54. move_one_left();
  55. if (event.shift() && m_editor->selection().start().is_valid()) {
  56. m_editor->selection().set_end(m_editor->cursor());
  57. m_editor->did_update_selection();
  58. }
  59. return true;
  60. }
  61. if (event.key() == KeyCode::Key_Right) {
  62. if (!event.shift() && m_editor->selection().is_valid()) {
  63. m_editor->set_cursor(m_editor->selection().normalized().end());
  64. m_editor->selection().clear();
  65. m_editor->did_update_selection();
  66. if (!event.ctrl()) {
  67. m_editor->update();
  68. return true;
  69. }
  70. }
  71. if (event.ctrl()) {
  72. m_editor->update_selection(event.shift());
  73. move_to_next_span();
  74. if (event.shift() && m_editor->selection().start().is_valid()) {
  75. m_editor->selection().set_end(m_editor->cursor());
  76. m_editor->did_update_selection();
  77. }
  78. return true;
  79. }
  80. m_editor->update_selection(event.shift());
  81. move_one_right();
  82. if (event.shift() && m_editor->selection().start().is_valid()) {
  83. m_editor->selection().set_end(m_editor->cursor());
  84. m_editor->did_update_selection();
  85. }
  86. return true;
  87. }
  88. if (event.key() == KeyCode::Key_Up) {
  89. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  90. m_editor->update_selection(event.shift());
  91. }
  92. move_one_up(event);
  93. if (event.shift() && m_editor->selection().start().is_valid()) {
  94. m_editor->selection().set_end(m_editor->cursor());
  95. m_editor->did_update_selection();
  96. }
  97. return true;
  98. }
  99. if (event.key() == KeyCode::Key_Down) {
  100. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  101. m_editor->update_selection(event.shift());
  102. }
  103. move_one_down(event);
  104. if (event.shift() && m_editor->selection().start().is_valid()) {
  105. m_editor->selection().set_end(m_editor->cursor());
  106. m_editor->did_update_selection();
  107. }
  108. return true;
  109. }
  110. if (event.key() == KeyCode::Key_Home) {
  111. m_editor->update_selection(event.shift());
  112. if (event.ctrl()) {
  113. move_to_first_line();
  114. } else {
  115. move_to_line_beginning();
  116. }
  117. if (event.shift() && m_editor->selection().start().is_valid()) {
  118. m_editor->selection().set_end(m_editor->cursor());
  119. m_editor->did_update_selection();
  120. }
  121. return true;
  122. }
  123. if (event.key() == KeyCode::Key_End) {
  124. m_editor->update_selection(event.shift());
  125. if (event.ctrl()) {
  126. move_to_last_line();
  127. } else {
  128. move_to_line_end();
  129. }
  130. if (event.shift() && m_editor->selection().start().is_valid()) {
  131. m_editor->selection().set_end(m_editor->cursor());
  132. m_editor->did_update_selection();
  133. }
  134. return true;
  135. }
  136. if (event.key() == KeyCode::Key_PageUp) {
  137. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  138. m_editor->update_selection(event.shift());
  139. }
  140. move_page_up();
  141. if (event.shift() && m_editor->selection().start().is_valid()) {
  142. m_editor->selection().set_end(m_editor->cursor());
  143. m_editor->did_update_selection();
  144. }
  145. return true;
  146. }
  147. if (event.key() == KeyCode::Key_PageDown) {
  148. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  149. m_editor->update_selection(event.shift());
  150. }
  151. move_page_down();
  152. if (event.shift() && m_editor->selection().start().is_valid()) {
  153. m_editor->selection().set_end(m_editor->cursor());
  154. m_editor->did_update_selection();
  155. }
  156. return true;
  157. }
  158. return false;
  159. }
  160. void EditingEngine::move_one_left()
  161. {
  162. if (m_editor->cursor().column() > 0) {
  163. int new_column = m_editor->cursor().column() - 1;
  164. m_editor->set_cursor(m_editor->cursor().line(), new_column);
  165. } else if (m_editor->cursor().line() > 0) {
  166. int new_line = m_editor->cursor().line() - 1;
  167. int new_column = m_editor->lines()[new_line].length();
  168. m_editor->set_cursor(new_line, new_column);
  169. }
  170. }
  171. void EditingEngine::move_one_right()
  172. {
  173. int new_line = m_editor->cursor().line();
  174. int new_column = m_editor->cursor().column();
  175. if (m_editor->cursor().column() < m_editor->current_line().length()) {
  176. new_line = m_editor->cursor().line();
  177. new_column = m_editor->cursor().column() + 1;
  178. } else if (m_editor->cursor().line() != m_editor->line_count() - 1) {
  179. new_line = m_editor->cursor().line() + 1;
  180. new_column = 0;
  181. }
  182. m_editor->set_cursor(new_line, new_column);
  183. }
  184. void EditingEngine::move_to_previous_span()
  185. {
  186. TextPosition new_cursor;
  187. if (m_editor->document().has_spans()) {
  188. auto span = m_editor->document().first_non_skippable_span_before(m_editor->cursor());
  189. if (span.has_value()) {
  190. new_cursor = span.value().range.start();
  191. } else {
  192. // No remaining spans, just use word break calculation
  193. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  194. }
  195. } else {
  196. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  197. }
  198. m_editor->set_cursor(new_cursor);
  199. }
  200. void EditingEngine::move_to_next_span()
  201. {
  202. TextPosition new_cursor;
  203. if (m_editor->document().has_spans()) {
  204. auto span = m_editor->document().first_non_skippable_span_after(m_editor->cursor());
  205. if (span.has_value()) {
  206. new_cursor = span.value().range.start();
  207. } else {
  208. // No remaining spans, just use word break calculation
  209. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  210. }
  211. } else {
  212. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  213. }
  214. m_editor->set_cursor(new_cursor);
  215. }
  216. void EditingEngine::move_to_logical_line_beginning()
  217. {
  218. TextPosition new_cursor;
  219. size_t first_nonspace_column = m_editor->current_line().first_non_whitespace_column();
  220. if (m_editor->cursor().column() == first_nonspace_column) {
  221. new_cursor = { m_editor->cursor().line(), 0 };
  222. } else {
  223. new_cursor = { m_editor->cursor().line(), first_nonspace_column };
  224. }
  225. m_editor->set_cursor(new_cursor);
  226. }
  227. void EditingEngine::move_to_line_beginning()
  228. {
  229. if (m_editor->is_wrapping_enabled()) {
  230. // FIXME: Replicate the first_nonspace_column behavior in wrapping mode.
  231. auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
  232. m_editor->set_cursor(m_editor->text_position_at_content_position(home_position));
  233. } else {
  234. move_to_logical_line_beginning();
  235. }
  236. }
  237. void EditingEngine::move_to_line_end()
  238. {
  239. if (m_editor->is_wrapping_enabled()) {
  240. auto end_position = m_editor->cursor_content_rect().location().translated(m_editor->width(), 0);
  241. m_editor->set_cursor(m_editor->text_position_at_content_position(end_position));
  242. } else {
  243. move_to_logical_line_end();
  244. }
  245. }
  246. void EditingEngine::move_to_logical_line_end()
  247. {
  248. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().length() });
  249. }
  250. void EditingEngine::move_one_up(const KeyEvent& event)
  251. {
  252. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  253. if (event.ctrl() && event.shift()) {
  254. move_selected_lines_up();
  255. return;
  256. }
  257. TextPosition new_cursor;
  258. if (m_editor->is_wrapping_enabled()) {
  259. auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
  260. new_cursor = m_editor->text_position_at_content_position(position_above);
  261. } else {
  262. size_t new_line = m_editor->cursor().line() - 1;
  263. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  264. new_cursor = { new_line, new_column };
  265. }
  266. m_editor->set_cursor(new_cursor);
  267. }
  268. };
  269. void EditingEngine::move_one_down(const KeyEvent& event)
  270. {
  271. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  272. if (event.ctrl() && event.shift()) {
  273. move_selected_lines_down();
  274. return;
  275. }
  276. TextPosition new_cursor;
  277. if (m_editor->is_wrapping_enabled()) {
  278. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  279. new_cursor = m_editor->text_position_at_content_position(position_below);
  280. } else {
  281. size_t new_line = m_editor->cursor().line() + 1;
  282. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  283. new_cursor = { new_line, new_column };
  284. }
  285. m_editor->set_cursor(new_cursor);
  286. }
  287. };
  288. void EditingEngine::move_up(double page_height_factor)
  289. {
  290. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  291. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  292. TextPosition new_cursor;
  293. if (m_editor->is_wrapping_enabled()) {
  294. auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
  295. new_cursor = m_editor->text_position_at_content_position(position_above);
  296. } else {
  297. size_t page_step = (size_t)pixels / (size_t)m_editor->line_height();
  298. size_t new_line = m_editor->cursor().line() < page_step ? 0 : m_editor->cursor().line() - page_step;
  299. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  300. new_cursor = { new_line, new_column };
  301. }
  302. m_editor->set_cursor(new_cursor);
  303. }
  304. };
  305. void EditingEngine::move_down(double page_height_factor)
  306. {
  307. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  308. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  309. TextPosition new_cursor;
  310. if (m_editor->is_wrapping_enabled()) {
  311. auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
  312. new_cursor = m_editor->text_position_at_content_position(position_below);
  313. } else {
  314. size_t new_line = min(m_editor->line_count() - 1, m_editor->cursor().line() + pixels / m_editor->line_height());
  315. size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
  316. new_cursor = { new_line, new_column };
  317. }
  318. m_editor->set_cursor(new_cursor);
  319. };
  320. }
  321. void EditingEngine::move_page_up()
  322. {
  323. move_up(1);
  324. };
  325. void EditingEngine::move_page_down()
  326. {
  327. move_down(1);
  328. };
  329. void EditingEngine::move_to_first_line()
  330. {
  331. m_editor->set_cursor(0, 0);
  332. };
  333. void EditingEngine::move_to_last_line()
  334. {
  335. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1].length());
  336. };
  337. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  338. {
  339. auto selection = m_editor->normalized_selection();
  340. if (!selection.is_valid()) {
  341. first_line = m_editor->cursor().line();
  342. last_line = m_editor->cursor().line();
  343. return;
  344. }
  345. first_line = selection.start().line();
  346. last_line = selection.end().line();
  347. if (first_line != last_line && selection.end().column() == 0)
  348. last_line -= 1;
  349. }
  350. TextPosition EditingEngine::find_beginning_of_next_word()
  351. {
  352. /* The rules that have been coded in:
  353. * Jump to the next punct or alnum after any whitespace
  354. * Jump to the next non-consecutive punct regardless of whitespace
  355. * Jump to the next alnum if started on punct regardless of whitespace
  356. * If the end of the input is reached, jump there
  357. */
  358. bool started_on_punct = is_vim_punctuation(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  359. bool has_seen_whitespace = false;
  360. bool is_first_line = true;
  361. auto& lines = m_editor->lines();
  362. auto cursor = m_editor->cursor();
  363. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  364. auto& line = lines.at(line_index);
  365. if (line.is_empty() && !is_first_line) {
  366. return { line_index, 0 };
  367. } else if (line.is_empty()) {
  368. has_seen_whitespace = true;
  369. }
  370. is_first_line = false;
  371. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  372. if (line_index == cursor.line() && column_index < cursor.column())
  373. continue;
  374. const u32* line_chars = line.view().code_points();
  375. const u32 current_char = line_chars[column_index];
  376. if (started_on_punct && is_vim_alphanumeric(current_char)) {
  377. return { line_index, column_index };
  378. }
  379. if (is_vim_punctuation(current_char) && !started_on_punct) {
  380. return { line_index, column_index };
  381. }
  382. if (is_ascii_space(current_char))
  383. has_seen_whitespace = true;
  384. if (has_seen_whitespace && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char))) {
  385. return { line_index, column_index };
  386. }
  387. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  388. return { line_index, column_index };
  389. }
  390. // Implicit newline
  391. if (column_index == line.length() - 1)
  392. has_seen_whitespace = true;
  393. }
  394. }
  395. VERIFY_NOT_REACHED();
  396. }
  397. void EditingEngine::move_to_beginning_of_next_word()
  398. {
  399. m_editor->set_cursor(find_beginning_of_next_word());
  400. }
  401. TextPosition EditingEngine::find_end_of_next_word()
  402. {
  403. /* The rules that have been coded in:
  404. * If the current_char is alnum and the next is whitespace or punct
  405. * If the current_char is punct and the next is whitespace or alnum
  406. * If the end of the input is reached, jump there
  407. */
  408. bool is_first_line = true;
  409. bool is_first_iteration = true;
  410. auto& lines = m_editor->lines();
  411. auto cursor = m_editor->cursor();
  412. if ((lines.at(cursor.line()).length() - cursor.column()) <= 1)
  413. return { cursor.line(), cursor.column() };
  414. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  415. auto& line = lines.at(line_index);
  416. if (line.is_empty() && !is_first_line) {
  417. return { line_index, 0 };
  418. }
  419. is_first_line = false;
  420. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  421. if (line_index == cursor.line() && column_index < cursor.column())
  422. continue;
  423. const u32* line_chars = line.view().code_points();
  424. const u32 current_char = line_chars[column_index];
  425. if (column_index == lines.at(line_index).length() - 1 && !is_first_iteration && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char)))
  426. return { line_index, column_index };
  427. else if (column_index == lines.at(line_index).length() - 1) {
  428. is_first_iteration = false;
  429. continue;
  430. }
  431. const u32 next_char = line_chars[column_index + 1];
  432. if (!is_first_iteration && is_vim_alphanumeric(current_char) && (is_ascii_space(next_char) || is_vim_punctuation(next_char)))
  433. return { line_index, column_index };
  434. if (!is_first_iteration && is_vim_punctuation(current_char) && (is_ascii_space(next_char) || is_vim_alphanumeric(next_char)))
  435. return { line_index, column_index };
  436. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  437. return { line_index, column_index };
  438. }
  439. is_first_iteration = false;
  440. }
  441. }
  442. VERIFY_NOT_REACHED();
  443. }
  444. void EditingEngine::move_to_end_of_next_word()
  445. {
  446. m_editor->set_cursor(find_end_of_next_word());
  447. }
  448. TextPosition EditingEngine::find_end_of_previous_word()
  449. {
  450. bool started_on_punct = is_vim_punctuation(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  451. bool is_first_line = true;
  452. bool has_seen_whitespace = false;
  453. auto& lines = m_editor->lines();
  454. auto cursor = m_editor->cursor();
  455. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  456. auto& line = lines.at(line_index);
  457. if (line.is_empty() && !is_first_line) {
  458. return { line_index, 0 };
  459. } else if (line.is_empty()) {
  460. has_seen_whitespace = true;
  461. }
  462. is_first_line = false;
  463. size_t line_length = lines.at(line_index).length();
  464. for (size_t column_index = line_length - 1; (int)column_index >= 0; column_index--) {
  465. if (line_index == cursor.line() && column_index > cursor.column())
  466. continue;
  467. const u32* line_chars = line.view().code_points();
  468. const u32 current_char = line_chars[column_index];
  469. if (started_on_punct && is_vim_alphanumeric(current_char)) {
  470. return { line_index, column_index };
  471. }
  472. if (is_vim_punctuation(current_char) && !started_on_punct) {
  473. return { line_index, column_index };
  474. }
  475. if (is_ascii_space(current_char)) {
  476. has_seen_whitespace = true;
  477. }
  478. if (has_seen_whitespace && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char))) {
  479. return { line_index, column_index };
  480. }
  481. if (line_index == 0 && column_index == 0) {
  482. return { line_index, column_index };
  483. }
  484. // Implicit newline when wrapping back up to the end of the previous line.
  485. if (column_index == 0)
  486. has_seen_whitespace = true;
  487. }
  488. }
  489. VERIFY_NOT_REACHED();
  490. }
  491. void EditingEngine::move_to_end_of_previous_word()
  492. {
  493. m_editor->set_cursor(find_end_of_previous_word());
  494. }
  495. TextPosition EditingEngine::find_beginning_of_previous_word()
  496. {
  497. bool is_first_iterated_line = true;
  498. bool is_first_iteration = true;
  499. auto& lines = m_editor->lines();
  500. auto cursor = m_editor->cursor();
  501. if ((lines.at(cursor.line()).length() - cursor.column()) <= 1)
  502. return { cursor.line(), cursor.column() };
  503. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  504. auto& line = lines.at(line_index);
  505. if (line.is_empty() && !is_first_iterated_line) {
  506. return { line_index, 0 };
  507. }
  508. is_first_iterated_line = false;
  509. size_t line_length = lines.at(line_index).length();
  510. for (size_t column_index = line_length; (int)column_index >= 0; column_index--) {
  511. if (line_index == cursor.line() && column_index > cursor.column())
  512. continue;
  513. if (column_index == line_length) {
  514. is_first_iteration = false;
  515. continue;
  516. }
  517. const u32* line_chars = line.view().code_points();
  518. const u32 current_char = line_chars[column_index];
  519. if (column_index == 0 && !is_first_iteration && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char))) {
  520. return { line_index, column_index };
  521. } else if (line_index == 0 && column_index == 0) {
  522. return { line_index, column_index };
  523. } else if (column_index == 0 && is_first_iteration) {
  524. is_first_iteration = false;
  525. continue;
  526. }
  527. const u32 next_char = line_chars[column_index - 1];
  528. if (!is_first_iteration && is_vim_alphanumeric(current_char) && (is_ascii_space(next_char) || is_vim_punctuation(next_char)))
  529. return { line_index, column_index };
  530. if (!is_first_iteration && is_vim_punctuation(current_char) && (is_ascii_space(next_char) || is_vim_alphanumeric(next_char)))
  531. return { line_index, column_index };
  532. is_first_iteration = false;
  533. }
  534. }
  535. VERIFY_NOT_REACHED();
  536. }
  537. void EditingEngine::move_to_beginning_of_previous_word()
  538. {
  539. m_editor->set_cursor(find_beginning_of_previous_word());
  540. }
  541. void EditingEngine::move_selected_lines_up()
  542. {
  543. if (!m_editor->is_editable())
  544. return;
  545. size_t first_line;
  546. size_t last_line;
  547. get_selection_line_boundaries(first_line, last_line);
  548. if (first_line == 0)
  549. return;
  550. auto& lines = m_editor->document().lines();
  551. lines.insert((int)last_line, lines.take((int)first_line - 1));
  552. m_editor->set_cursor({ first_line - 1, 0 });
  553. if (m_editor->has_selection()) {
  554. m_editor->selection().set_start({ first_line - 1, 0 });
  555. m_editor->selection().set_end({ last_line - 1, m_editor->line(last_line - 1).length() });
  556. }
  557. m_editor->did_change();
  558. m_editor->update();
  559. }
  560. void EditingEngine::move_selected_lines_down()
  561. {
  562. if (!m_editor->is_editable())
  563. return;
  564. size_t first_line;
  565. size_t last_line;
  566. get_selection_line_boundaries(first_line, last_line);
  567. auto& lines = m_editor->document().lines();
  568. VERIFY(lines.size() != 0);
  569. if (last_line >= lines.size() - 1)
  570. return;
  571. lines.insert((int)first_line, lines.take((int)last_line + 1));
  572. m_editor->set_cursor({ first_line + 1, 0 });
  573. if (m_editor->has_selection()) {
  574. m_editor->selection().set_start({ first_line + 1, 0 });
  575. m_editor->selection().set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
  576. }
  577. m_editor->did_change();
  578. m_editor->update();
  579. }
  580. void EditingEngine::delete_char()
  581. {
  582. if (!m_editor->is_editable())
  583. return;
  584. m_editor->do_delete();
  585. };
  586. void EditingEngine::delete_line()
  587. {
  588. if (!m_editor->is_editable())
  589. return;
  590. m_editor->delete_current_line();
  591. };
  592. }