EditingEngine.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (c) 2021-2022, 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. void EditingEngine::attach(TextEditor& editor)
  12. {
  13. VERIFY(!m_editor);
  14. m_editor = editor;
  15. }
  16. void EditingEngine::detach()
  17. {
  18. VERIFY(m_editor);
  19. m_editor = nullptr;
  20. }
  21. bool EditingEngine::on_key(KeyEvent const& event)
  22. {
  23. if (event.key() == KeyCode::Key_Left) {
  24. if (!event.shift() && m_editor->selection().is_valid()) {
  25. m_editor->set_cursor(m_editor->selection().normalized().start());
  26. m_editor->selection().clear();
  27. m_editor->did_update_selection();
  28. if (!event.ctrl()) {
  29. m_editor->update();
  30. return true;
  31. }
  32. }
  33. if (event.ctrl()) {
  34. m_editor->update_selection(event.shift());
  35. move_to_previous_span();
  36. if (event.shift() && m_editor->selection().start().is_valid()) {
  37. m_editor->selection().set_end(m_editor->cursor());
  38. m_editor->did_update_selection();
  39. }
  40. return true;
  41. }
  42. m_editor->update_selection(event.shift());
  43. move_one_left();
  44. if (event.shift() && m_editor->selection().start().is_valid()) {
  45. m_editor->selection().set_end(m_editor->cursor());
  46. m_editor->did_update_selection();
  47. }
  48. return true;
  49. }
  50. if (event.key() == KeyCode::Key_Right) {
  51. if (!event.shift() && m_editor->selection().is_valid()) {
  52. m_editor->set_cursor(m_editor->selection().normalized().end());
  53. m_editor->selection().clear();
  54. m_editor->did_update_selection();
  55. if (!event.ctrl()) {
  56. m_editor->update();
  57. return true;
  58. }
  59. }
  60. if (event.ctrl()) {
  61. m_editor->update_selection(event.shift());
  62. move_to_next_span();
  63. if (event.shift() && m_editor->selection().start().is_valid()) {
  64. m_editor->selection().set_end(m_editor->cursor());
  65. m_editor->did_update_selection();
  66. }
  67. return true;
  68. }
  69. m_editor->update_selection(event.shift());
  70. move_one_right();
  71. if (event.shift() && m_editor->selection().start().is_valid()) {
  72. m_editor->selection().set_end(m_editor->cursor());
  73. m_editor->did_update_selection();
  74. }
  75. return true;
  76. }
  77. if (event.key() == KeyCode::Key_Up) {
  78. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  79. m_editor->update_selection(event.shift());
  80. }
  81. move_one_up(event);
  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_Down) {
  89. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  90. m_editor->update_selection(event.shift());
  91. }
  92. move_one_down(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_Home) {
  100. m_editor->update_selection(event.shift());
  101. if (event.ctrl()) {
  102. move_to_first_line();
  103. } else {
  104. move_to_line_beginning();
  105. }
  106. if (event.shift() && m_editor->selection().start().is_valid()) {
  107. m_editor->selection().set_end(m_editor->cursor());
  108. m_editor->did_update_selection();
  109. }
  110. return true;
  111. }
  112. if (event.key() == KeyCode::Key_End) {
  113. m_editor->update_selection(event.shift());
  114. if (event.ctrl()) {
  115. move_to_last_line();
  116. } else {
  117. move_to_line_end();
  118. }
  119. if (event.shift() && m_editor->selection().start().is_valid()) {
  120. m_editor->selection().set_end(m_editor->cursor());
  121. m_editor->did_update_selection();
  122. }
  123. return true;
  124. }
  125. if (event.key() == KeyCode::Key_PageUp) {
  126. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  127. m_editor->update_selection(event.shift());
  128. }
  129. move_page_up();
  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. return true;
  135. }
  136. if (event.key() == KeyCode::Key_PageDown) {
  137. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  138. m_editor->update_selection(event.shift());
  139. }
  140. move_page_down();
  141. if (event.shift() && m_editor->selection().start().is_valid()) {
  142. m_editor->selection().set_end(m_editor->cursor());
  143. m_editor->did_update_selection();
  144. }
  145. return true;
  146. }
  147. return false;
  148. }
  149. void EditingEngine::move_one_left()
  150. {
  151. if (m_editor->cursor().column() > 0) {
  152. int new_column = m_editor->cursor().column() - 1;
  153. m_editor->set_cursor(m_editor->cursor().line(), new_column);
  154. } else if (m_editor->cursor().line() > 0) {
  155. int new_line = m_editor->cursor().line() - 1;
  156. int new_column = m_editor->lines()[new_line].length();
  157. m_editor->set_cursor(new_line, new_column);
  158. }
  159. }
  160. void EditingEngine::move_one_right()
  161. {
  162. int new_line = m_editor->cursor().line();
  163. int new_column = m_editor->cursor().column();
  164. if (m_editor->cursor().column() < m_editor->current_line().length()) {
  165. new_line = m_editor->cursor().line();
  166. new_column = m_editor->cursor().column() + 1;
  167. } else if (m_editor->cursor().line() != m_editor->line_count() - 1) {
  168. new_line = m_editor->cursor().line() + 1;
  169. new_column = 0;
  170. }
  171. m_editor->set_cursor(new_line, new_column);
  172. }
  173. void EditingEngine::move_to_previous_span()
  174. {
  175. TextPosition new_cursor;
  176. if (m_editor->document().has_spans()) {
  177. auto span = m_editor->document().first_non_skippable_span_before(m_editor->cursor());
  178. if (span.has_value()) {
  179. new_cursor = span.value().range.start();
  180. } else {
  181. // No remaining spans, just use word break calculation
  182. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  183. }
  184. } else {
  185. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  186. }
  187. m_editor->set_cursor(new_cursor);
  188. }
  189. void EditingEngine::move_to_next_span()
  190. {
  191. TextPosition new_cursor;
  192. if (m_editor->document().has_spans()) {
  193. auto span = m_editor->document().first_non_skippable_span_after(m_editor->cursor());
  194. if (span.has_value()) {
  195. new_cursor = span.value().range.start();
  196. } else {
  197. // No remaining spans, just use word break calculation
  198. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  199. }
  200. } else {
  201. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  202. }
  203. m_editor->set_cursor(new_cursor);
  204. }
  205. void EditingEngine::move_to_logical_line_beginning()
  206. {
  207. TextPosition new_cursor;
  208. size_t first_nonspace_column = m_editor->current_line().first_non_whitespace_column();
  209. if (m_editor->cursor().column() == first_nonspace_column) {
  210. new_cursor = { m_editor->cursor().line(), 0 };
  211. } else {
  212. new_cursor = { m_editor->cursor().line(), first_nonspace_column };
  213. }
  214. m_editor->set_cursor(new_cursor);
  215. }
  216. void EditingEngine::move_to_line_beginning()
  217. {
  218. if (m_editor->is_wrapping_enabled()) {
  219. // FIXME: Replicate the first_nonspace_column behavior in wrapping mode.
  220. auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
  221. m_editor->set_cursor(m_editor->text_position_at_content_position(home_position));
  222. } else {
  223. move_to_logical_line_beginning();
  224. }
  225. }
  226. void EditingEngine::move_to_line_end()
  227. {
  228. if (m_editor->is_wrapping_enabled()) {
  229. auto end_position = m_editor->cursor_content_rect().location().translated(m_editor->width(), 0);
  230. m_editor->set_cursor(m_editor->text_position_at_content_position(end_position));
  231. } else {
  232. move_to_logical_line_end();
  233. }
  234. }
  235. void EditingEngine::move_to_logical_line_end()
  236. {
  237. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().length() });
  238. }
  239. void EditingEngine::move_one_up(KeyEvent const& event)
  240. {
  241. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  242. if (event.ctrl() && event.shift()) {
  243. move_selected_lines_up();
  244. return;
  245. }
  246. TextPosition new_cursor;
  247. if (m_editor->is_wrapping_enabled()) {
  248. auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
  249. new_cursor = m_editor->text_position_at_content_position(position_above);
  250. } else {
  251. size_t new_line = m_editor->cursor().line() - 1;
  252. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  253. new_cursor = { new_line, new_column };
  254. }
  255. m_editor->set_cursor(new_cursor);
  256. }
  257. };
  258. void EditingEngine::move_one_down(KeyEvent const& event)
  259. {
  260. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  261. if (event.ctrl() && event.shift()) {
  262. move_selected_lines_down();
  263. return;
  264. }
  265. TextPosition new_cursor;
  266. if (m_editor->is_wrapping_enabled()) {
  267. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  268. new_cursor = m_editor->text_position_at_content_position(position_below);
  269. } else {
  270. size_t new_line = m_editor->cursor().line() + 1;
  271. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  272. new_cursor = { new_line, new_column };
  273. }
  274. m_editor->set_cursor(new_cursor);
  275. }
  276. };
  277. void EditingEngine::move_up(double page_height_factor)
  278. {
  279. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  280. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  281. TextPosition new_cursor;
  282. if (m_editor->is_wrapping_enabled()) {
  283. auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
  284. new_cursor = m_editor->text_position_at_content_position(position_above);
  285. } else {
  286. size_t page_step = (size_t)pixels / (size_t)m_editor->line_height();
  287. size_t new_line = m_editor->cursor().line() < page_step ? 0 : m_editor->cursor().line() - page_step;
  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->set_cursor(new_cursor);
  292. }
  293. };
  294. void EditingEngine::move_down(double page_height_factor)
  295. {
  296. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || 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_below = m_editor->cursor_content_rect().location().translated(0, pixels);
  301. new_cursor = m_editor->text_position_at_content_position(position_below);
  302. } else {
  303. size_t new_line = min(m_editor->line_count() - 1, m_editor->cursor().line() + pixels / m_editor->line_height());
  304. size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
  305. new_cursor = { new_line, new_column };
  306. }
  307. m_editor->set_cursor(new_cursor);
  308. };
  309. }
  310. void EditingEngine::move_page_up()
  311. {
  312. move_up(1);
  313. };
  314. void EditingEngine::move_page_down()
  315. {
  316. move_down(1);
  317. };
  318. void EditingEngine::move_to_first_line()
  319. {
  320. m_editor->set_cursor(0, 0);
  321. };
  322. void EditingEngine::move_to_last_line()
  323. {
  324. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1].length());
  325. };
  326. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  327. {
  328. auto selection = m_editor->normalized_selection();
  329. if (!selection.is_valid()) {
  330. first_line = m_editor->cursor().line();
  331. last_line = m_editor->cursor().line();
  332. return;
  333. }
  334. first_line = selection.start().line();
  335. last_line = selection.end().line();
  336. if (first_line != last_line && selection.end().column() == 0)
  337. last_line -= 1;
  338. }
  339. void EditingEngine::move_selected_lines_up()
  340. {
  341. if (!m_editor->is_editable())
  342. return;
  343. size_t first_line;
  344. size_t last_line;
  345. get_selection_line_boundaries(first_line, last_line);
  346. if (first_line == 0)
  347. return;
  348. auto& lines = m_editor->document().lines();
  349. lines.insert((int)last_line, lines.take((int)first_line - 1));
  350. m_editor->set_cursor({ first_line - 1, 0 });
  351. if (m_editor->has_selection()) {
  352. m_editor->selection().set_start({ first_line - 1, 0 });
  353. m_editor->selection().set_end({ last_line - 1, m_editor->line(last_line - 1).length() });
  354. }
  355. m_editor->did_change();
  356. m_editor->update();
  357. }
  358. void EditingEngine::move_selected_lines_down()
  359. {
  360. if (!m_editor->is_editable())
  361. return;
  362. size_t first_line;
  363. size_t last_line;
  364. get_selection_line_boundaries(first_line, last_line);
  365. auto& lines = m_editor->document().lines();
  366. VERIFY(lines.size() != 0);
  367. if (last_line >= lines.size() - 1)
  368. return;
  369. lines.insert((int)first_line, lines.take((int)last_line + 1));
  370. m_editor->set_cursor({ first_line + 1, 0 });
  371. if (m_editor->has_selection()) {
  372. m_editor->selection().set_start({ first_line + 1, 0 });
  373. m_editor->selection().set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
  374. }
  375. m_editor->did_change();
  376. m_editor->update();
  377. }
  378. void EditingEngine::delete_char()
  379. {
  380. if (!m_editor->is_editable())
  381. return;
  382. m_editor->do_delete();
  383. };
  384. void EditingEngine::delete_line()
  385. {
  386. if (!m_editor->is_editable())
  387. return;
  388. m_editor->delete_current_line();
  389. };
  390. }