EditingEngine.cpp 24 KB

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