EditingEngine.cpp 23 KB

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