EditingEngine.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. ASSERT(!m_editor);
  36. m_editor = editor;
  37. }
  38. void EditingEngine::detach()
  39. {
  40. ASSERT(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_line_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_line_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_line_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_line_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_line_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_line_wrapping_enabled()) {
  283. new_cursor = m_editor->text_position_at_content_position(m_editor->cursor_content_rect().location().translated(0, m_editor->line_height()));
  284. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  285. new_cursor = m_editor->text_position_at_content_position(position_below);
  286. } else {
  287. size_t new_line = m_editor->cursor().line() + 1;
  288. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  289. new_cursor = { new_line, new_column };
  290. }
  291. m_editor->toggle_selection_if_needed_for_event(event.shift());
  292. m_editor->set_cursor(new_cursor);
  293. }
  294. };
  295. void EditingEngine::move_up(const KeyEvent& event, double page_height_factor)
  296. {
  297. if (m_editor->cursor().line() > 0 || m_editor->is_line_wrapping_enabled()) {
  298. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  299. TextPosition new_cursor;
  300. if (m_editor->is_line_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->toggle_selection_if_needed_for_event(event.shift());
  310. m_editor->set_cursor(new_cursor);
  311. }
  312. };
  313. void EditingEngine::move_down(const KeyEvent& event, double page_height_factor)
  314. {
  315. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_line_wrapping_enabled()) {
  316. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  317. TextPosition new_cursor;
  318. if (m_editor->is_line_wrapping_enabled()) {
  319. auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
  320. new_cursor = m_editor->text_position_at_content_position(position_below);
  321. } else {
  322. size_t new_line = min(m_editor->line_count() - 1, m_editor->cursor().line() + pixels / m_editor->line_height());
  323. size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
  324. new_cursor = { new_line, new_column };
  325. }
  326. m_editor->toggle_selection_if_needed_for_event(event.shift());
  327. m_editor->set_cursor(new_cursor);
  328. };
  329. }
  330. void EditingEngine::move_page_up(const KeyEvent& event)
  331. {
  332. move_up(event, 1);
  333. };
  334. void EditingEngine::move_page_down(const KeyEvent& event)
  335. {
  336. move_down(event, 1);
  337. };
  338. void EditingEngine::move_to_first_line()
  339. {
  340. m_editor->set_cursor(0, 0);
  341. };
  342. void EditingEngine::move_to_last_line()
  343. {
  344. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1].length());
  345. };
  346. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  347. {
  348. auto selection = m_editor->normalized_selection();
  349. if (!selection.is_valid()) {
  350. first_line = m_editor->cursor().line();
  351. last_line = m_editor->cursor().line();
  352. return;
  353. }
  354. first_line = selection.start().line();
  355. last_line = selection.end().line();
  356. if (first_line != last_line && selection.end().column() == 0)
  357. last_line -= 1;
  358. }
  359. TextPosition EditingEngine::find_beginning_of_next_word()
  360. {
  361. /* The rules that have been coded in:
  362. * Jump to the next punct or alnum after any whitespace
  363. * Jump to the next non-consecutive punct regardless of whitespace
  364. * Jump to the next alnum if started on punct regardless of whitespace
  365. * If the end of the input is reached, jump there
  366. */
  367. auto vim_isalnum = [](int c) {
  368. return c == '_' || isalnum(c);
  369. };
  370. auto vim_ispunct = [](int c) {
  371. return c != '_' && ispunct(c);
  372. };
  373. bool started_on_punct = vim_ispunct(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  374. bool has_seen_whitespace = false;
  375. bool is_first_line = true;
  376. auto& lines = m_editor->lines();
  377. auto cursor = m_editor->cursor();
  378. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  379. auto& line = lines.at(line_index);
  380. if (line.is_empty() && !is_first_line) {
  381. return { line_index, 0 };
  382. } else if (line.is_empty()) {
  383. has_seen_whitespace = true;
  384. }
  385. is_first_line = false;
  386. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  387. if (line_index == cursor.line() && column_index < cursor.column())
  388. continue;
  389. const u32* line_chars = line.view().code_points();
  390. const u32 current_char = line_chars[column_index];
  391. if (started_on_punct && vim_isalnum(current_char)) {
  392. return { line_index, column_index };
  393. }
  394. if (vim_ispunct(current_char) && !started_on_punct) {
  395. return { line_index, column_index };
  396. }
  397. if (isspace(current_char))
  398. has_seen_whitespace = true;
  399. if (has_seen_whitespace && (vim_isalnum(current_char) || vim_ispunct(current_char))) {
  400. return { line_index, column_index };
  401. }
  402. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  403. return { line_index, column_index };
  404. }
  405. // Implicit newline
  406. if (column_index == line.length() - 1)
  407. has_seen_whitespace = true;
  408. }
  409. }
  410. ASSERT_NOT_REACHED();
  411. }
  412. void EditingEngine::move_to_beginning_of_next_word()
  413. {
  414. m_editor->set_cursor(find_beginning_of_next_word());
  415. }
  416. TextPosition EditingEngine::find_end_of_next_word()
  417. {
  418. /* The rules that have been coded in:
  419. * If the current_char is alnum and the next is whitespace or punct
  420. * If the current_char is punct and the next is whitespace or alnum
  421. * If the end of the input is reached, jump there
  422. */
  423. auto vim_isalnum = [](int c) {
  424. return c == '_' || isalnum(c);
  425. };
  426. auto vim_ispunct = [](int c) {
  427. return c != '_' && ispunct(c);
  428. };
  429. bool is_first_line = true;
  430. bool is_first_iteration = true;
  431. auto& lines = m_editor->lines();
  432. auto cursor = m_editor->cursor();
  433. for (size_t line_index = cursor.line(); line_index < lines.size(); line_index++) {
  434. auto& line = lines.at(line_index);
  435. if (line.is_empty() && !is_first_line) {
  436. return { line_index, 0 };
  437. }
  438. is_first_line = false;
  439. for (size_t column_index = 0; column_index < lines.at(line_index).length(); column_index++) {
  440. if (line_index == cursor.line() && column_index < cursor.column())
  441. continue;
  442. const u32* line_chars = line.view().code_points();
  443. const u32 current_char = line_chars[column_index];
  444. if (column_index == lines.at(line_index).length() - 1 && !is_first_iteration && (vim_isalnum(current_char) || vim_ispunct(current_char)))
  445. return { line_index, column_index };
  446. else if (column_index == lines.at(line_index).length() - 1) {
  447. is_first_iteration = false;
  448. continue;
  449. }
  450. const u32 next_char = line_chars[column_index + 1];
  451. if (!is_first_iteration && vim_isalnum(current_char) && (isspace(next_char) || vim_ispunct(next_char)))
  452. return { line_index, column_index };
  453. if (!is_first_iteration && vim_ispunct(current_char) && (isspace(next_char) || vim_isalnum(next_char)))
  454. return { line_index, column_index };
  455. if (line_index == lines.size() - 1 && column_index == line.length() - 1) {
  456. return { line_index, column_index };
  457. }
  458. is_first_iteration = false;
  459. }
  460. }
  461. ASSERT_NOT_REACHED();
  462. }
  463. void EditingEngine::move_to_end_of_next_word()
  464. {
  465. m_editor->set_cursor(find_end_of_next_word());
  466. }
  467. TextPosition EditingEngine::find_end_of_previous_word()
  468. {
  469. auto vim_isalnum = [](int c) {
  470. return c == '_' || isalnum(c);
  471. };
  472. auto vim_ispunct = [](int c) {
  473. return c != '_' && ispunct(c);
  474. };
  475. bool started_on_punct = vim_ispunct(m_editor->current_line().to_utf8().characters()[m_editor->cursor().column()]);
  476. bool is_first_line = true;
  477. bool has_seen_whitespace = false;
  478. auto& lines = m_editor->lines();
  479. auto cursor = m_editor->cursor();
  480. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  481. auto& line = lines.at(line_index);
  482. if (line.is_empty() && !is_first_line) {
  483. return { line_index, 0 };
  484. } else if (line.is_empty()) {
  485. has_seen_whitespace = true;
  486. }
  487. is_first_line = false;
  488. size_t line_length = lines.at(line_index).length();
  489. for (size_t column_index = line_length - 1; (int)column_index >= 0; column_index--) {
  490. if (line_index == cursor.line() && column_index > cursor.column())
  491. continue;
  492. const u32* line_chars = line.view().code_points();
  493. const u32 current_char = line_chars[column_index];
  494. if (started_on_punct && vim_isalnum(current_char)) {
  495. return { line_index, column_index };
  496. }
  497. if (vim_ispunct(current_char) && !started_on_punct) {
  498. return { line_index, column_index };
  499. }
  500. if (isspace(current_char)) {
  501. has_seen_whitespace = true;
  502. }
  503. if (has_seen_whitespace && (vim_isalnum(current_char) || vim_ispunct(current_char))) {
  504. return { line_index, column_index };
  505. }
  506. if (line_index == 0 && column_index == 0) {
  507. return { line_index, column_index };
  508. }
  509. // Implicit newline when wrapping back up to the end of the previous line.
  510. if (column_index == 0)
  511. has_seen_whitespace = true;
  512. }
  513. }
  514. ASSERT_NOT_REACHED();
  515. }
  516. void EditingEngine::move_to_end_of_previous_word()
  517. {
  518. m_editor->set_cursor(find_end_of_previous_word());
  519. }
  520. TextPosition EditingEngine::find_beginning_of_previous_word()
  521. {
  522. auto vim_isalnum = [](int c) {
  523. return c == '_' || isalnum(c);
  524. };
  525. auto vim_ispunct = [](int c) {
  526. return c != '_' && ispunct(c);
  527. };
  528. bool is_first_iterated_line = true;
  529. bool is_first_iteration = true;
  530. auto& lines = m_editor->lines();
  531. auto cursor = m_editor->cursor();
  532. for (size_t line_index = cursor.line(); (int)line_index >= 0; line_index--) {
  533. auto& line = lines.at(line_index);
  534. if (line.is_empty() && !is_first_iterated_line) {
  535. return { line_index, 0 };
  536. }
  537. is_first_iterated_line = false;
  538. size_t line_length = lines.at(line_index).length();
  539. for (size_t column_index = line_length; (int)column_index >= 0; column_index--) {
  540. if (line_index == cursor.line() && column_index > cursor.column())
  541. continue;
  542. if (column_index == line_length) {
  543. is_first_iteration = false;
  544. continue;
  545. }
  546. const u32* line_chars = line.view().code_points();
  547. const u32 current_char = line_chars[column_index];
  548. if (column_index == 0 && !is_first_iteration && (vim_isalnum(current_char) || vim_ispunct(current_char))) {
  549. return { line_index, column_index };
  550. } else if (line_index == 0 && column_index == 0) {
  551. return { line_index, column_index };
  552. } else if (column_index == 0 && is_first_iteration) {
  553. is_first_iteration = false;
  554. continue;
  555. }
  556. const u32 next_char = line_chars[column_index - 1];
  557. if (!is_first_iteration && vim_isalnum(current_char) && (isspace(next_char) || vim_ispunct(next_char)))
  558. return { line_index, column_index };
  559. if (!is_first_iteration && vim_ispunct(current_char) && (isspace(next_char) || vim_isalnum(next_char)))
  560. return { line_index, column_index };
  561. is_first_iteration = false;
  562. }
  563. }
  564. ASSERT_NOT_REACHED();
  565. }
  566. void EditingEngine::move_to_beginning_of_previous_word()
  567. {
  568. m_editor->set_cursor(find_beginning_of_previous_word());
  569. }
  570. void EditingEngine::move_selected_lines_up()
  571. {
  572. if (!m_editor->is_editable())
  573. return;
  574. size_t first_line;
  575. size_t last_line;
  576. get_selection_line_boundaries(first_line, last_line);
  577. if (first_line == 0)
  578. return;
  579. auto& lines = m_editor->document().lines();
  580. lines.insert((int)last_line, lines.take((int)first_line - 1));
  581. m_editor->set_cursor({ first_line - 1, 0 });
  582. if (m_editor->has_selection()) {
  583. m_editor->selection()->set_start({ first_line - 1, 0 });
  584. m_editor->selection()->set_end({ last_line - 1, m_editor->line(last_line - 1).length() });
  585. }
  586. m_editor->did_change();
  587. m_editor->update();
  588. }
  589. void EditingEngine::move_selected_lines_down()
  590. {
  591. if (!m_editor->is_editable())
  592. return;
  593. size_t first_line;
  594. size_t last_line;
  595. get_selection_line_boundaries(first_line, last_line);
  596. auto& lines = m_editor->document().lines();
  597. ASSERT(lines.size() != 0);
  598. if (last_line >= lines.size() - 1)
  599. return;
  600. lines.insert((int)first_line, lines.take((int)last_line + 1));
  601. m_editor->set_cursor({ first_line + 1, 0 });
  602. if (m_editor->has_selection()) {
  603. m_editor->selection()->set_start({ first_line + 1, 0 });
  604. m_editor->selection()->set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
  605. }
  606. m_editor->did_change();
  607. m_editor->update();
  608. }
  609. void EditingEngine::delete_char()
  610. {
  611. if (!m_editor->is_editable())
  612. return;
  613. m_editor->do_delete();
  614. };
  615. void EditingEngine::delete_line()
  616. {
  617. if (!m_editor->is_editable())
  618. return;
  619. m_editor->delete_current_line();
  620. };
  621. }