EditingEngine.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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_logical_line_beginning()
  224. {
  225. TextPosition new_cursor;
  226. size_t first_nonspace_column = m_editor->current_line().first_non_whitespace_column();
  227. if (m_editor->cursor().column() == first_nonspace_column) {
  228. new_cursor = { m_editor->cursor().line(), 0 };
  229. } else {
  230. new_cursor = { m_editor->cursor().line(), first_nonspace_column };
  231. }
  232. m_editor->set_cursor(new_cursor);
  233. }
  234. void EditingEngine::move_to_line_beginning()
  235. {
  236. if (m_editor->is_wrapping_enabled()) {
  237. // FIXME: Replicate the first_nonspace_column behavior in wrapping mode.
  238. auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
  239. m_editor->set_cursor(m_editor->text_position_at_content_position(home_position));
  240. } else {
  241. move_to_logical_line_beginning();
  242. }
  243. }
  244. void EditingEngine::move_to_line_end()
  245. {
  246. if (m_editor->is_wrapping_enabled()) {
  247. auto end_position = m_editor->cursor_content_rect().location().translated(m_editor->width(), 0);
  248. m_editor->set_cursor(m_editor->text_position_at_content_position(end_position));
  249. } else {
  250. move_to_logical_line_end();
  251. }
  252. }
  253. void EditingEngine::move_to_logical_line_end()
  254. {
  255. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().length() });
  256. }
  257. void EditingEngine::move_one_up(const KeyEvent& event)
  258. {
  259. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  260. if (event.ctrl() && event.shift()) {
  261. move_selected_lines_up();
  262. return;
  263. }
  264. TextPosition new_cursor;
  265. if (m_editor->is_wrapping_enabled()) {
  266. auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
  267. new_cursor = m_editor->text_position_at_content_position(position_above);
  268. } else {
  269. size_t new_line = m_editor->cursor().line() - 1;
  270. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  271. new_cursor = { new_line, new_column };
  272. }
  273. m_editor->set_cursor(new_cursor);
  274. }
  275. };
  276. void EditingEngine::move_one_down(const KeyEvent& event)
  277. {
  278. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  279. if (event.ctrl() && event.shift()) {
  280. move_selected_lines_down();
  281. return;
  282. }
  283. TextPosition new_cursor;
  284. if (m_editor->is_wrapping_enabled()) {
  285. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  286. new_cursor = m_editor->text_position_at_content_position(position_below);
  287. } else {
  288. size_t new_line = m_editor->cursor().line() + 1;
  289. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  290. new_cursor = { new_line, new_column };
  291. }
  292. m_editor->set_cursor(new_cursor);
  293. }
  294. };
  295. void EditingEngine::move_up(double page_height_factor)
  296. {
  297. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  298. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  299. TextPosition new_cursor;
  300. if (m_editor->is_wrapping_enabled()) {
  301. auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
  302. new_cursor = m_editor->text_position_at_content_position(position_above);
  303. } else {
  304. size_t page_step = (size_t)pixels / (size_t)m_editor->line_height();
  305. size_t new_line = m_editor->cursor().line() < page_step ? 0 : m_editor->cursor().line() - page_step;
  306. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  307. new_cursor = { new_line, new_column };
  308. }
  309. m_editor->set_cursor(new_cursor);
  310. }
  311. };
  312. void EditingEngine::move_down(double page_height_factor)
  313. {
  314. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  315. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  316. TextPosition new_cursor;
  317. if (m_editor->is_wrapping_enabled()) {
  318. auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
  319. new_cursor = m_editor->text_position_at_content_position(position_below);
  320. } else {
  321. size_t new_line = min(m_editor->line_count() - 1, m_editor->cursor().line() + pixels / m_editor->line_height());
  322. size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
  323. new_cursor = { new_line, new_column };
  324. }
  325. m_editor->set_cursor(new_cursor);
  326. };
  327. }
  328. void EditingEngine::move_page_up()
  329. {
  330. move_up(1);
  331. };
  332. void EditingEngine::move_page_down()
  333. {
  334. move_down(1);
  335. };
  336. void EditingEngine::move_to_first_line()
  337. {
  338. m_editor->set_cursor(0, 0);
  339. };
  340. void EditingEngine::move_to_last_line()
  341. {
  342. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1].length());
  343. };
  344. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  345. {
  346. auto selection = m_editor->normalized_selection();
  347. if (!selection.is_valid()) {
  348. first_line = m_editor->cursor().line();
  349. last_line = m_editor->cursor().line();
  350. return;
  351. }
  352. first_line = selection.start().line();
  353. last_line = selection.end().line();
  354. if (first_line != last_line && selection.end().column() == 0)
  355. last_line -= 1;
  356. }
  357. TextPosition EditingEngine::find_beginning_of_next_word()
  358. {
  359. /* The rules that have been coded in:
  360. * Jump to the next punct or alnum after any whitespace
  361. * Jump to the next non-consecutive punct regardless of whitespace
  362. * Jump to the next alnum if started on punct regardless of whitespace
  363. * If the end of the input is reached, jump there
  364. */
  365. bool started_on_punct = is_vim_punctuation(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  366. bool has_seen_whitespace = false;
  367. bool is_first_line = true;
  368. auto& lines = m_editor->lines();
  369. auto cursor = m_editor->cursor();
  370. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  371. auto& line = lines.at(line_index);
  372. if (line.is_empty() && !is_first_line) {
  373. return { line_index, 0 };
  374. } else if (line.is_empty()) {
  375. has_seen_whitespace = true;
  376. }
  377. is_first_line = false;
  378. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  379. if (line_index == cursor.line() && column_index < cursor.column())
  380. continue;
  381. const u32* line_chars = line.view().code_points();
  382. const u32 current_char = line_chars[column_index];
  383. if (started_on_punct && is_vim_alphanumeric(current_char)) {
  384. return { line_index, column_index };
  385. }
  386. if (is_vim_punctuation(current_char) && !started_on_punct) {
  387. return { line_index, column_index };
  388. }
  389. if (is_ascii_space(current_char))
  390. has_seen_whitespace = true;
  391. if (has_seen_whitespace && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char))) {
  392. return { line_index, column_index };
  393. }
  394. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  395. return { line_index, column_index };
  396. }
  397. // Implicit newline
  398. if (column_index == line.length() - 1)
  399. has_seen_whitespace = true;
  400. }
  401. }
  402. VERIFY_NOT_REACHED();
  403. }
  404. void EditingEngine::move_to_beginning_of_next_word()
  405. {
  406. m_editor->set_cursor(find_beginning_of_next_word());
  407. }
  408. TextPosition EditingEngine::find_end_of_next_word()
  409. {
  410. /* The rules that have been coded in:
  411. * If the current_char is alnum and the next is whitespace or punct
  412. * If the current_char is punct and the next is whitespace or alnum
  413. * If the end of the input is reached, jump there
  414. */
  415. bool is_first_line = true;
  416. bool is_first_iteration = true;
  417. auto& lines = m_editor->lines();
  418. auto cursor = m_editor->cursor();
  419. if ((lines.at(cursor.line()).length() - cursor.column()) <= 1)
  420. return { cursor.line(), cursor.column() };
  421. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  422. auto& line = lines.at(line_index);
  423. if (line.is_empty() && !is_first_line) {
  424. return { line_index, 0 };
  425. }
  426. is_first_line = false;
  427. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  428. if (line_index == cursor.line() && column_index < cursor.column())
  429. continue;
  430. const u32* line_chars = line.view().code_points();
  431. const u32 current_char = line_chars[column_index];
  432. if (column_index == lines.at(line_index).length() - 1 && !is_first_iteration && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char)))
  433. return { line_index, column_index };
  434. else if (column_index == lines.at(line_index).length() - 1) {
  435. is_first_iteration = false;
  436. continue;
  437. }
  438. const u32 next_char = line_chars[column_index + 1];
  439. if (!is_first_iteration && is_vim_alphanumeric(current_char) && (is_ascii_space(next_char) || is_vim_punctuation(next_char)))
  440. return { line_index, column_index };
  441. if (!is_first_iteration && is_vim_punctuation(current_char) && (is_ascii_space(next_char) || is_vim_alphanumeric(next_char)))
  442. return { line_index, column_index };
  443. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  444. return { line_index, column_index };
  445. }
  446. is_first_iteration = false;
  447. }
  448. }
  449. VERIFY_NOT_REACHED();
  450. }
  451. void EditingEngine::move_to_end_of_next_word()
  452. {
  453. m_editor->set_cursor(find_end_of_next_word());
  454. }
  455. TextPosition EditingEngine::find_end_of_previous_word()
  456. {
  457. bool started_on_punct = is_vim_punctuation(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  458. bool is_first_line = true;
  459. bool has_seen_whitespace = false;
  460. auto& lines = m_editor->lines();
  461. auto cursor = m_editor->cursor();
  462. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  463. auto& line = lines.at(line_index);
  464. if (line.is_empty() && !is_first_line) {
  465. return { line_index, 0 };
  466. } else if (line.is_empty()) {
  467. has_seen_whitespace = true;
  468. }
  469. is_first_line = false;
  470. size_t line_length = lines.at(line_index).length();
  471. for (size_t column_index = line_length - 1; (int)column_index >= 0; column_index--) {
  472. if (line_index == cursor.line() && column_index > cursor.column())
  473. continue;
  474. const u32* line_chars = line.view().code_points();
  475. const u32 current_char = line_chars[column_index];
  476. if (started_on_punct && is_vim_alphanumeric(current_char)) {
  477. return { line_index, column_index };
  478. }
  479. if (is_vim_punctuation(current_char) && !started_on_punct) {
  480. return { line_index, column_index };
  481. }
  482. if (is_ascii_space(current_char)) {
  483. has_seen_whitespace = true;
  484. }
  485. if (has_seen_whitespace && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char))) {
  486. return { line_index, column_index };
  487. }
  488. if (line_index == 0 && column_index == 0) {
  489. return { line_index, column_index };
  490. }
  491. // Implicit newline when wrapping back up to the end of the previous line.
  492. if (column_index == 0)
  493. has_seen_whitespace = true;
  494. }
  495. }
  496. VERIFY_NOT_REACHED();
  497. }
  498. void EditingEngine::move_to_end_of_previous_word()
  499. {
  500. m_editor->set_cursor(find_end_of_previous_word());
  501. }
  502. TextPosition EditingEngine::find_beginning_of_previous_word()
  503. {
  504. bool is_first_iterated_line = true;
  505. bool is_first_iteration = true;
  506. auto& lines = m_editor->lines();
  507. auto cursor = m_editor->cursor();
  508. if ((lines.at(cursor.line()).length() - cursor.column()) <= 1)
  509. return { cursor.line(), cursor.column() };
  510. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  511. auto& line = lines.at(line_index);
  512. if (line.is_empty() && !is_first_iterated_line) {
  513. return { line_index, 0 };
  514. }
  515. is_first_iterated_line = false;
  516. size_t line_length = lines.at(line_index).length();
  517. for (size_t column_index = line_length; (int)column_index >= 0; column_index--) {
  518. if (line_index == cursor.line() && column_index > cursor.column())
  519. continue;
  520. if (column_index == line_length) {
  521. is_first_iteration = false;
  522. continue;
  523. }
  524. const u32* line_chars = line.view().code_points();
  525. const u32 current_char = line_chars[column_index];
  526. if (column_index == 0 && !is_first_iteration && (is_vim_alphanumeric(current_char) || is_vim_punctuation(current_char))) {
  527. return { line_index, column_index };
  528. } else if (line_index == 0 && column_index == 0) {
  529. return { line_index, column_index };
  530. } else if (column_index == 0 && is_first_iteration) {
  531. is_first_iteration = false;
  532. continue;
  533. }
  534. const u32 next_char = line_chars[column_index - 1];
  535. if (!is_first_iteration && is_vim_alphanumeric(current_char) && (is_ascii_space(next_char) || is_vim_punctuation(next_char)))
  536. return { line_index, column_index };
  537. if (!is_first_iteration && is_vim_punctuation(current_char) && (is_ascii_space(next_char) || is_vim_alphanumeric(next_char)))
  538. return { line_index, column_index };
  539. is_first_iteration = false;
  540. }
  541. }
  542. VERIFY_NOT_REACHED();
  543. }
  544. void EditingEngine::move_to_beginning_of_previous_word()
  545. {
  546. m_editor->set_cursor(find_beginning_of_previous_word());
  547. }
  548. void EditingEngine::move_selected_lines_up()
  549. {
  550. if (!m_editor->is_editable())
  551. return;
  552. size_t first_line;
  553. size_t last_line;
  554. get_selection_line_boundaries(first_line, last_line);
  555. if (first_line == 0)
  556. return;
  557. auto& lines = m_editor->document().lines();
  558. lines.insert((int)last_line, lines.take((int)first_line - 1));
  559. m_editor->set_cursor({ first_line - 1, 0 });
  560. if (m_editor->has_selection()) {
  561. m_editor->selection()->set_start({ first_line - 1, 0 });
  562. m_editor->selection()->set_end({ last_line - 1, m_editor->line(last_line - 1).length() });
  563. }
  564. m_editor->did_change();
  565. m_editor->update();
  566. }
  567. void EditingEngine::move_selected_lines_down()
  568. {
  569. if (!m_editor->is_editable())
  570. return;
  571. size_t first_line;
  572. size_t last_line;
  573. get_selection_line_boundaries(first_line, last_line);
  574. auto& lines = m_editor->document().lines();
  575. VERIFY(lines.size() != 0);
  576. if (last_line >= lines.size() - 1)
  577. return;
  578. lines.insert((int)first_line, lines.take((int)last_line + 1));
  579. m_editor->set_cursor({ first_line + 1, 0 });
  580. if (m_editor->has_selection()) {
  581. m_editor->selection()->set_start({ first_line + 1, 0 });
  582. m_editor->selection()->set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
  583. }
  584. m_editor->did_change();
  585. m_editor->update();
  586. }
  587. void EditingEngine::delete_char()
  588. {
  589. if (!m_editor->is_editable())
  590. return;
  591. m_editor->do_delete();
  592. };
  593. void EditingEngine::delete_line()
  594. {
  595. if (!m_editor->is_editable())
  596. return;
  597. m_editor->delete_current_line();
  598. };
  599. }