EditingEngine.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/EditingEngine.h>
  27. #include <LibGUI/Event.h>
  28. #include <LibGUI/TextEditor.h>
  29. namespace GUI {
  30. EditingEngine::~EditingEngine()
  31. {
  32. }
  33. void EditingEngine::attach(TextEditor& editor)
  34. {
  35. VERIFY(!m_editor);
  36. m_editor = editor;
  37. }
  38. void EditingEngine::detach()
  39. {
  40. VERIFY(m_editor);
  41. m_editor = nullptr;
  42. }
  43. bool EditingEngine::on_key(const KeyEvent& event)
  44. {
  45. if (event.key() == KeyCode::Key_Left) {
  46. if (!event.shift() && m_editor->selection()->is_valid()) {
  47. m_editor->set_cursor(m_editor->selection()->normalized().start());
  48. m_editor->selection()->clear();
  49. m_editor->did_update_selection();
  50. if (!event.ctrl()) {
  51. m_editor->update();
  52. return true;
  53. }
  54. }
  55. if (event.ctrl()) {
  56. move_to_previous_span(event);
  57. if (event.shift() && m_editor->selection()->start().is_valid()) {
  58. m_editor->selection()->set_end(m_editor->cursor());
  59. m_editor->did_update_selection();
  60. }
  61. return true;
  62. }
  63. move_one_left(event);
  64. if (event.shift() && m_editor->selection()->start().is_valid()) {
  65. m_editor->selection()->set_end(m_editor->cursor());
  66. m_editor->did_update_selection();
  67. }
  68. return true;
  69. }
  70. if (event.key() == KeyCode::Key_Right) {
  71. if (!event.shift() && m_editor->selection()->is_valid()) {
  72. m_editor->set_cursor(m_editor->selection()->normalized().end());
  73. m_editor->selection()->clear();
  74. m_editor->did_update_selection();
  75. if (!event.ctrl()) {
  76. m_editor->update();
  77. return true;
  78. }
  79. }
  80. if (event.ctrl()) {
  81. move_to_next_span(event);
  82. return true;
  83. }
  84. move_one_right(event);
  85. if (event.shift() && m_editor->selection()->start().is_valid()) {
  86. m_editor->selection()->set_end(m_editor->cursor());
  87. m_editor->did_update_selection();
  88. }
  89. return true;
  90. }
  91. if (event.key() == KeyCode::Key_Up) {
  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. 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. if (event.ctrl()) {
  109. m_editor->toggle_selection_if_needed_for_event(event.shift());
  110. move_to_first_line();
  111. if (event.shift() && m_editor->selection()->start().is_valid()) {
  112. m_editor->selection()->set_end(m_editor->cursor());
  113. m_editor->did_update_selection();
  114. }
  115. } else {
  116. move_to_line_beginning(event);
  117. if (event.shift() && m_editor->selection()->start().is_valid()) {
  118. m_editor->selection()->set_end(m_editor->cursor());
  119. m_editor->did_update_selection();
  120. }
  121. }
  122. return true;
  123. }
  124. if (event.key() == KeyCode::Key_End) {
  125. if (event.ctrl()) {
  126. m_editor->toggle_selection_if_needed_for_event(event.shift());
  127. move_to_last_line();
  128. if (event.shift() && m_editor->selection()->start().is_valid()) {
  129. m_editor->selection()->set_end(m_editor->cursor());
  130. m_editor->did_update_selection();
  131. }
  132. } else {
  133. move_to_line_end(event);
  134. if (event.shift() && m_editor->selection()->start().is_valid()) {
  135. m_editor->selection()->set_end(m_editor->cursor());
  136. m_editor->did_update_selection();
  137. }
  138. }
  139. return true;
  140. }
  141. if (event.key() == KeyCode::Key_PageUp) {
  142. move_page_up(event);
  143. if (event.shift() && m_editor->selection()->start().is_valid()) {
  144. m_editor->selection()->set_end(m_editor->cursor());
  145. m_editor->did_update_selection();
  146. }
  147. return true;
  148. }
  149. if (event.key() == KeyCode::Key_PageDown) {
  150. move_page_down(event);
  151. if (event.shift() && m_editor->selection()->start().is_valid()) {
  152. m_editor->selection()->set_end(m_editor->cursor());
  153. m_editor->did_update_selection();
  154. }
  155. return true;
  156. }
  157. return false;
  158. }
  159. void EditingEngine::move_one_left(const KeyEvent& event)
  160. {
  161. if (m_editor->cursor().column() > 0) {
  162. int new_column = m_editor->cursor().column() - 1;
  163. m_editor->toggle_selection_if_needed_for_event(event.shift());
  164. m_editor->set_cursor(m_editor->cursor().line(), new_column);
  165. } else if (m_editor->cursor().line() > 0) {
  166. int new_line = m_editor->cursor().line() - 1;
  167. int new_column = m_editor->lines()[new_line].length();
  168. m_editor->toggle_selection_if_needed_for_event(event.shift());
  169. m_editor->set_cursor(new_line, new_column);
  170. }
  171. }
  172. void EditingEngine::move_one_right(const KeyEvent& event)
  173. {
  174. int new_line = m_editor->cursor().line();
  175. int new_column = m_editor->cursor().column();
  176. if (m_editor->cursor().column() < m_editor->current_line().length()) {
  177. new_line = m_editor->cursor().line();
  178. new_column = m_editor->cursor().column() + 1;
  179. } else if (m_editor->cursor().line() != m_editor->line_count() - 1) {
  180. new_line = m_editor->cursor().line() + 1;
  181. new_column = 0;
  182. }
  183. m_editor->toggle_selection_if_needed_for_event(event.shift());
  184. m_editor->set_cursor(new_line, new_column);
  185. }
  186. void EditingEngine::move_to_previous_span(const KeyEvent& event)
  187. {
  188. TextPosition new_cursor;
  189. if (m_editor->document().has_spans()) {
  190. auto span = m_editor->document().first_non_skippable_span_before(m_editor->cursor());
  191. if (span.has_value()) {
  192. new_cursor = span.value().range.start();
  193. } else {
  194. // No remaining spans, just use word break calculation
  195. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  196. }
  197. } else {
  198. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  199. }
  200. m_editor->toggle_selection_if_needed_for_event(event.shift());
  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->toggle_selection_if_needed_for_event(event.shift());
  218. m_editor->set_cursor(new_cursor);
  219. if (event.shift() && m_editor->selection()->start().is_valid()) {
  220. m_editor->selection()->set_end(m_editor->cursor());
  221. m_editor->did_update_selection();
  222. }
  223. }
  224. void EditingEngine::move_to_line_beginning(const KeyEvent& event)
  225. {
  226. TextPosition new_cursor;
  227. m_editor->toggle_selection_if_needed_for_event(event.shift());
  228. if (m_editor->is_wrapping_enabled()) {
  229. // FIXME: Replicate the first_nonspace_column behavior in wrapping mode.
  230. auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
  231. new_cursor = m_editor->text_position_at_content_position(home_position);
  232. } else {
  233. size_t first_nonspace_column = m_editor->current_line().first_non_whitespace_column();
  234. if (m_editor->cursor().column() == first_nonspace_column) {
  235. new_cursor = { m_editor->cursor().line(), 0 };
  236. } else {
  237. new_cursor = { m_editor->cursor().line(), first_nonspace_column };
  238. }
  239. }
  240. m_editor->set_cursor(new_cursor);
  241. }
  242. void EditingEngine::move_to_line_end(const KeyEvent& event)
  243. {
  244. TextPosition new_cursor;
  245. if (m_editor->is_wrapping_enabled()) {
  246. auto end_position = m_editor->cursor_content_rect().location().translated(m_editor->width(), 0);
  247. new_cursor = m_editor->text_position_at_content_position(end_position);
  248. } else {
  249. new_cursor = { m_editor->cursor().line(), m_editor->current_line().length() };
  250. }
  251. m_editor->toggle_selection_if_needed_for_event(event.shift());
  252. m_editor->set_cursor(new_cursor);
  253. }
  254. void EditingEngine::move_one_up(const KeyEvent& event)
  255. {
  256. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  257. if (event.ctrl() && event.shift()) {
  258. move_selected_lines_up();
  259. return;
  260. }
  261. TextPosition new_cursor;
  262. if (m_editor->is_wrapping_enabled()) {
  263. auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
  264. new_cursor = m_editor->text_position_at_content_position(position_above);
  265. } else {
  266. size_t new_line = m_editor->cursor().line() - 1;
  267. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  268. new_cursor = { new_line, new_column };
  269. }
  270. m_editor->toggle_selection_if_needed_for_event(event.shift());
  271. m_editor->set_cursor(new_cursor);
  272. }
  273. };
  274. void EditingEngine::move_one_down(const KeyEvent& event)
  275. {
  276. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  277. if (event.ctrl() && event.shift()) {
  278. move_selected_lines_down();
  279. return;
  280. }
  281. TextPosition new_cursor;
  282. if (m_editor->is_wrapping_enabled()) {
  283. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  284. new_cursor = m_editor->text_position_at_content_position(position_below);
  285. } else {
  286. size_t new_line = m_editor->cursor().line() + 1;
  287. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  288. new_cursor = { new_line, new_column };
  289. }
  290. m_editor->toggle_selection_if_needed_for_event(event.shift());
  291. m_editor->set_cursor(new_cursor);
  292. }
  293. };
  294. void EditingEngine::move_up(const KeyEvent& event, double page_height_factor)
  295. {
  296. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  297. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  298. TextPosition new_cursor;
  299. if (m_editor->is_wrapping_enabled()) {
  300. auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
  301. new_cursor = m_editor->text_position_at_content_position(position_above);
  302. } else {
  303. size_t page_step = (size_t)pixels / (size_t)m_editor->line_height();
  304. size_t new_line = m_editor->cursor().line() < page_step ? 0 : m_editor->cursor().line() - page_step;
  305. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  306. new_cursor = { new_line, new_column };
  307. }
  308. m_editor->toggle_selection_if_needed_for_event(event.shift());
  309. m_editor->set_cursor(new_cursor);
  310. }
  311. };
  312. void EditingEngine::move_down(const KeyEvent& event, 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->toggle_selection_if_needed_for_event(event.shift());
  326. m_editor->set_cursor(new_cursor);
  327. };
  328. }
  329. void EditingEngine::move_page_up(const KeyEvent& event)
  330. {
  331. move_up(event, 1);
  332. };
  333. void EditingEngine::move_page_down(const KeyEvent& event)
  334. {
  335. move_down(event, 1);
  336. };
  337. void EditingEngine::move_to_first_line()
  338. {
  339. m_editor->set_cursor(0, 0);
  340. };
  341. void EditingEngine::move_to_last_line()
  342. {
  343. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1].length());
  344. };
  345. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  346. {
  347. auto selection = m_editor->normalized_selection();
  348. if (!selection.is_valid()) {
  349. first_line = m_editor->cursor().line();
  350. last_line = m_editor->cursor().line();
  351. return;
  352. }
  353. first_line = selection.start().line();
  354. last_line = selection.end().line();
  355. if (first_line != last_line && selection.end().column() == 0)
  356. last_line -= 1;
  357. }
  358. TextPosition EditingEngine::find_beginning_of_next_word()
  359. {
  360. /* The rules that have been coded in:
  361. * Jump to the next punct or alnum after any whitespace
  362. * Jump to the next non-consecutive punct regardless of whitespace
  363. * Jump to the next alnum if started on punct regardless of whitespace
  364. * If the end of the input is reached, jump there
  365. */
  366. auto vim_isalnum = [](int c) {
  367. return c == '_' || isalnum(c);
  368. };
  369. auto vim_ispunct = [](int c) {
  370. return c != '_' && ispunct(c);
  371. };
  372. bool started_on_punct = vim_ispunct(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  373. bool has_seen_whitespace = false;
  374. bool is_first_line = true;
  375. auto& lines = m_editor->lines();
  376. auto cursor = m_editor->cursor();
  377. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  378. auto& line = lines.at(line_index);
  379. if (line.is_empty() && !is_first_line) {
  380. return { line_index, 0 };
  381. } else if (line.is_empty()) {
  382. has_seen_whitespace = true;
  383. }
  384. is_first_line = false;
  385. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  386. if (line_index == cursor.line() && column_index < cursor.column())
  387. continue;
  388. const u32* line_chars = line.view().code_points();
  389. const u32 current_char = line_chars[column_index];
  390. if (started_on_punct && vim_isalnum(current_char)) {
  391. return { line_index, column_index };
  392. }
  393. if (vim_ispunct(current_char) && !started_on_punct) {
  394. return { line_index, column_index };
  395. }
  396. if (isspace(current_char))
  397. has_seen_whitespace = true;
  398. if (has_seen_whitespace && (vim_isalnum(current_char) || vim_ispunct(current_char))) {
  399. return { line_index, column_index };
  400. }
  401. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  402. return { line_index, column_index };
  403. }
  404. // Implicit newline
  405. if (column_index == line.length() - 1)
  406. has_seen_whitespace = true;
  407. }
  408. }
  409. VERIFY_NOT_REACHED();
  410. }
  411. void EditingEngine::move_to_beginning_of_next_word()
  412. {
  413. m_editor->set_cursor(find_beginning_of_next_word());
  414. }
  415. TextPosition EditingEngine::find_end_of_next_word()
  416. {
  417. /* The rules that have been coded in:
  418. * If the current_char is alnum and the next is whitespace or punct
  419. * If the current_char is punct and the next is whitespace or alnum
  420. * If the end of the input is reached, jump there
  421. */
  422. auto vim_isalnum = [](int c) {
  423. return c == '_' || isalnum(c);
  424. };
  425. auto vim_ispunct = [](int c) {
  426. return c != '_' && ispunct(c);
  427. };
  428. bool is_first_line = true;
  429. bool is_first_iteration = true;
  430. auto& lines = m_editor->lines();
  431. auto cursor = m_editor->cursor();
  432. if ((lines.at(cursor.line()).length() - cursor.column()) <= 1)
  433. return { cursor.line(), cursor.column() };
  434. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  435. auto& line = lines.at(line_index);
  436. if (line.is_empty() && !is_first_line) {
  437. return { line_index, 0 };
  438. }
  439. is_first_line = false;
  440. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  441. if (line_index == cursor.line() && column_index < cursor.column())
  442. continue;
  443. const u32* line_chars = line.view().code_points();
  444. const u32 current_char = line_chars[column_index];
  445. if (column_index == lines.at(line_index).length() - 1 && !is_first_iteration && (vim_isalnum(current_char) || vim_ispunct(current_char)))
  446. return { line_index, column_index };
  447. else if (column_index == lines.at(line_index).length() - 1) {
  448. is_first_iteration = false;
  449. continue;
  450. }
  451. const u32 next_char = line_chars[column_index + 1];
  452. if (!is_first_iteration && vim_isalnum(current_char) && (isspace(next_char) || vim_ispunct(next_char)))
  453. return { line_index, column_index };
  454. if (!is_first_iteration && vim_ispunct(current_char) && (isspace(next_char) || vim_isalnum(next_char)))
  455. return { line_index, column_index };
  456. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  457. return { line_index, column_index };
  458. }
  459. is_first_iteration = false;
  460. }
  461. }
  462. VERIFY_NOT_REACHED();
  463. }
  464. void EditingEngine::move_to_end_of_next_word()
  465. {
  466. m_editor->set_cursor(find_end_of_next_word());
  467. }
  468. TextPosition EditingEngine::find_end_of_previous_word()
  469. {
  470. auto vim_isalnum = [](int c) {
  471. return c == '_' || isalnum(c);
  472. };
  473. auto vim_ispunct = [](int c) {
  474. return c != '_' && ispunct(c);
  475. };
  476. bool started_on_punct = vim_ispunct(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  477. bool is_first_line = true;
  478. bool has_seen_whitespace = false;
  479. auto& lines = m_editor->lines();
  480. auto cursor = m_editor->cursor();
  481. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  482. auto& line = lines.at(line_index);
  483. if (line.is_empty() && !is_first_line) {
  484. return { line_index, 0 };
  485. } else if (line.is_empty()) {
  486. has_seen_whitespace = true;
  487. }
  488. is_first_line = false;
  489. size_t line_length = lines.at(line_index).length();
  490. for (size_t column_index = line_length - 1; (int)column_index >= 0; column_index--) {
  491. if (line_index == cursor.line() && column_index > cursor.column())
  492. continue;
  493. const u32* line_chars = line.view().code_points();
  494. const u32 current_char = line_chars[column_index];
  495. if (started_on_punct && vim_isalnum(current_char)) {
  496. return { line_index, column_index };
  497. }
  498. if (vim_ispunct(current_char) && !started_on_punct) {
  499. return { line_index, column_index };
  500. }
  501. if (isspace(current_char)) {
  502. has_seen_whitespace = true;
  503. }
  504. if (has_seen_whitespace && (vim_isalnum(current_char) || vim_ispunct(current_char))) {
  505. return { line_index, column_index };
  506. }
  507. if (line_index == 0 && column_index == 0) {
  508. return { line_index, column_index };
  509. }
  510. // Implicit newline when wrapping back up to the end of the previous line.
  511. if (column_index == 0)
  512. has_seen_whitespace = true;
  513. }
  514. }
  515. VERIFY_NOT_REACHED();
  516. }
  517. void EditingEngine::move_to_end_of_previous_word()
  518. {
  519. m_editor->set_cursor(find_end_of_previous_word());
  520. }
  521. TextPosition EditingEngine::find_beginning_of_previous_word()
  522. {
  523. auto vim_isalnum = [](int c) {
  524. return c == '_' || isalnum(c);
  525. };
  526. auto vim_ispunct = [](int c) {
  527. return c != '_' && ispunct(c);
  528. };
  529. bool is_first_iterated_line = true;
  530. bool is_first_iteration = true;
  531. auto& lines = m_editor->lines();
  532. auto cursor = m_editor->cursor();
  533. if ((lines.at(cursor.line()).length() - cursor.column()) <= 1)
  534. return { cursor.line(), cursor.column() };
  535. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  536. auto& line = lines.at(line_index);
  537. if (line.is_empty() && !is_first_iterated_line) {
  538. return { line_index, 0 };
  539. }
  540. is_first_iterated_line = false;
  541. size_t line_length = lines.at(line_index).length();
  542. for (size_t column_index = line_length; (int)column_index >= 0; column_index--) {
  543. if (line_index == cursor.line() && column_index > cursor.column())
  544. continue;
  545. if (column_index == line_length) {
  546. is_first_iteration = false;
  547. continue;
  548. }
  549. const u32* line_chars = line.view().code_points();
  550. const u32 current_char = line_chars[column_index];
  551. if (column_index == 0 && !is_first_iteration && (vim_isalnum(current_char) || vim_ispunct(current_char))) {
  552. return { line_index, column_index };
  553. } else if (line_index == 0 && column_index == 0) {
  554. return { line_index, column_index };
  555. } else if (column_index == 0 && is_first_iteration) {
  556. is_first_iteration = false;
  557. continue;
  558. }
  559. const u32 next_char = line_chars[column_index - 1];
  560. if (!is_first_iteration && vim_isalnum(current_char) && (isspace(next_char) || vim_ispunct(next_char)))
  561. return { line_index, column_index };
  562. if (!is_first_iteration && vim_ispunct(current_char) && (isspace(next_char) || vim_isalnum(next_char)))
  563. return { line_index, column_index };
  564. is_first_iteration = false;
  565. }
  566. }
  567. VERIFY_NOT_REACHED();
  568. }
  569. void EditingEngine::move_to_beginning_of_previous_word()
  570. {
  571. m_editor->set_cursor(find_beginning_of_previous_word());
  572. }
  573. void EditingEngine::move_selected_lines_up()
  574. {
  575. if (!m_editor->is_editable())
  576. return;
  577. size_t first_line;
  578. size_t last_line;
  579. get_selection_line_boundaries(first_line, last_line);
  580. if (first_line == 0)
  581. return;
  582. auto& lines = m_editor->document().lines();
  583. lines.insert((int)last_line, lines.take((int)first_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::move_selected_lines_down()
  593. {
  594. if (!m_editor->is_editable())
  595. return;
  596. size_t first_line;
  597. size_t last_line;
  598. get_selection_line_boundaries(first_line, last_line);
  599. auto& lines = m_editor->document().lines();
  600. VERIFY(lines.size() != 0);
  601. if (last_line >= lines.size() - 1)
  602. return;
  603. lines.insert((int)first_line, lines.take((int)last_line + 1));
  604. m_editor->set_cursor({ first_line + 1, 0 });
  605. if (m_editor->has_selection()) {
  606. m_editor->selection()->set_start({ first_line + 1, 0 });
  607. m_editor->selection()->set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
  608. }
  609. m_editor->did_change();
  610. m_editor->update();
  611. }
  612. void EditingEngine::delete_char()
  613. {
  614. if (!m_editor->is_editable())
  615. return;
  616. m_editor->do_delete();
  617. };
  618. void EditingEngine::delete_line()
  619. {
  620. if (!m_editor->is_editable())
  621. return;
  622. m_editor->delete_current_line();
  623. };
  624. }