EditingEngine.cpp 24 KB

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