EditingEngine.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 || event.key() == KeyCode::Key_Down) {
  78. auto const direction = key_code_to_vertical_direction(event.key());
  79. bool const condition_for_up = direction == VerticalDirection::Up && m_editor->cursor().line() > 0;
  80. bool const condition_for_down = direction == VerticalDirection::Down && m_editor->cursor().line() < (m_editor->line_count() - 1);
  81. if (condition_for_up || condition_for_down || m_editor->is_wrapping_enabled())
  82. m_editor->update_selection(event.shift());
  83. move_one_helper(event, direction);
  84. }
  85. if (event.key() == KeyCode::Key_Home) {
  86. m_editor->update_selection(event.shift());
  87. if (event.ctrl()) {
  88. move_to_first_line();
  89. } else {
  90. move_to_line_beginning();
  91. }
  92. if (event.shift() && m_editor->selection().start().is_valid()) {
  93. m_editor->selection().set_end(m_editor->cursor());
  94. m_editor->did_update_selection();
  95. }
  96. return true;
  97. }
  98. if (event.key() == KeyCode::Key_End) {
  99. m_editor->update_selection(event.shift());
  100. if (event.ctrl()) {
  101. move_to_last_line();
  102. } else {
  103. move_to_line_end();
  104. }
  105. if (event.shift() && m_editor->selection().start().is_valid()) {
  106. m_editor->selection().set_end(m_editor->cursor());
  107. m_editor->did_update_selection();
  108. }
  109. return true;
  110. }
  111. if (event.key() == KeyCode::Key_PageUp) {
  112. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  113. m_editor->update_selection(event.shift());
  114. }
  115. move_page_up();
  116. if (event.shift() && m_editor->selection().start().is_valid()) {
  117. m_editor->selection().set_end(m_editor->cursor());
  118. m_editor->did_update_selection();
  119. }
  120. return true;
  121. }
  122. if (event.key() == KeyCode::Key_PageDown) {
  123. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  124. m_editor->update_selection(event.shift());
  125. }
  126. move_page_down();
  127. if (event.shift() && m_editor->selection().start().is_valid()) {
  128. m_editor->selection().set_end(m_editor->cursor());
  129. m_editor->did_update_selection();
  130. }
  131. return true;
  132. }
  133. return false;
  134. }
  135. void EditingEngine::move_one_left()
  136. {
  137. if (m_editor->cursor().column() > 0) {
  138. int new_column = m_editor->cursor().column() - 1;
  139. m_editor->set_cursor(m_editor->cursor().line(), new_column);
  140. } else if (m_editor->cursor().line() > 0) {
  141. int new_line = m_editor->cursor().line() - 1;
  142. int new_column = m_editor->lines()[new_line].length();
  143. m_editor->set_cursor(new_line, new_column);
  144. }
  145. }
  146. void EditingEngine::move_one_right()
  147. {
  148. int new_line = m_editor->cursor().line();
  149. int new_column = m_editor->cursor().column();
  150. if (m_editor->cursor().column() < m_editor->current_line().length()) {
  151. new_line = m_editor->cursor().line();
  152. new_column = m_editor->cursor().column() + 1;
  153. } else if (m_editor->cursor().line() != m_editor->line_count() - 1) {
  154. new_line = m_editor->cursor().line() + 1;
  155. new_column = 0;
  156. }
  157. m_editor->set_cursor(new_line, new_column);
  158. }
  159. void EditingEngine::move_to_previous_span()
  160. {
  161. TextPosition new_cursor;
  162. if (m_editor->document().has_spans()) {
  163. auto span = m_editor->document().first_non_skippable_span_before(m_editor->cursor());
  164. if (span.has_value()) {
  165. new_cursor = span.value().range.start();
  166. } else {
  167. // No remaining spans, just use word break calculation
  168. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  169. }
  170. } else {
  171. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  172. }
  173. m_editor->set_cursor(new_cursor);
  174. }
  175. void EditingEngine::move_to_next_span()
  176. {
  177. TextPosition new_cursor;
  178. if (m_editor->document().has_spans()) {
  179. auto span = m_editor->document().first_non_skippable_span_after(m_editor->cursor());
  180. if (span.has_value()) {
  181. new_cursor = span.value().range.start();
  182. } else {
  183. // No remaining spans, just use word break calculation
  184. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  185. }
  186. } else {
  187. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  188. }
  189. m_editor->set_cursor(new_cursor);
  190. }
  191. void EditingEngine::move_to_logical_line_beginning()
  192. {
  193. TextPosition new_cursor;
  194. size_t first_nonspace_column = m_editor->current_line().first_non_whitespace_column();
  195. if (m_editor->cursor().column() == first_nonspace_column) {
  196. new_cursor = { m_editor->cursor().line(), 0 };
  197. } else {
  198. new_cursor = { m_editor->cursor().line(), first_nonspace_column };
  199. }
  200. m_editor->set_cursor(new_cursor);
  201. }
  202. void EditingEngine::move_to_line_beginning()
  203. {
  204. if (m_editor->is_wrapping_enabled()) {
  205. // FIXME: Replicate the first_nonspace_column behavior in wrapping mode.
  206. auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
  207. m_editor->set_cursor(m_editor->text_position_at_content_position(home_position));
  208. } else {
  209. move_to_logical_line_beginning();
  210. }
  211. }
  212. void EditingEngine::move_to_line_end()
  213. {
  214. if (m_editor->is_wrapping_enabled()) {
  215. auto end_position = m_editor->cursor_content_rect().location().translated(m_editor->width(), 0);
  216. m_editor->set_cursor(m_editor->text_position_at_content_position(end_position));
  217. } else {
  218. move_to_logical_line_end();
  219. }
  220. }
  221. void EditingEngine::move_to_logical_line_end()
  222. {
  223. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().length() });
  224. }
  225. void EditingEngine::move_one_helper(KeyEvent const& event, VerticalDirection direction)
  226. {
  227. auto const result = direction == VerticalDirection::Up ? move_one_up(event) : move_one_down(event);
  228. if (result != DidMoveALine::Yes && event.shift() && m_editor->selection().start().is_valid()) {
  229. m_editor->selection().set_end(m_editor->cursor());
  230. m_editor->did_update_selection();
  231. }
  232. }
  233. EditingEngine::DidMoveALine EditingEngine::move_one_up(KeyEvent const& event)
  234. {
  235. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  236. if (event.ctrl() && event.shift()) {
  237. move_selected_lines_up();
  238. return DidMoveALine::Yes;
  239. }
  240. TextPosition new_cursor;
  241. if (m_editor->is_wrapping_enabled()) {
  242. auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
  243. new_cursor = m_editor->text_position_at_content_position(position_above);
  244. } else {
  245. size_t new_line = m_editor->cursor().line() - 1;
  246. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  247. new_cursor = { new_line, new_column };
  248. }
  249. m_editor->set_cursor(new_cursor);
  250. }
  251. return DidMoveALine::No;
  252. };
  253. EditingEngine::DidMoveALine EditingEngine::move_one_down(KeyEvent const& event)
  254. {
  255. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  256. if (event.ctrl() && event.shift()) {
  257. move_selected_lines_down();
  258. return DidMoveALine::Yes;
  259. }
  260. TextPosition new_cursor;
  261. if (m_editor->is_wrapping_enabled()) {
  262. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  263. new_cursor = m_editor->text_position_at_content_position(position_below);
  264. } else {
  265. size_t new_line = m_editor->cursor().line() + 1;
  266. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  267. new_cursor = { new_line, new_column };
  268. }
  269. m_editor->set_cursor(new_cursor);
  270. }
  271. return DidMoveALine::No;
  272. };
  273. void EditingEngine::move_up(double page_height_factor)
  274. {
  275. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  276. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  277. TextPosition new_cursor;
  278. if (m_editor->is_wrapping_enabled()) {
  279. auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
  280. new_cursor = m_editor->text_position_at_content_position(position_above);
  281. } else {
  282. size_t page_step = (size_t)pixels / (size_t)m_editor->line_height();
  283. size_t new_line = m_editor->cursor().line() < page_step ? 0 : m_editor->cursor().line() - page_step;
  284. size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
  285. new_cursor = { new_line, new_column };
  286. }
  287. m_editor->set_cursor(new_cursor);
  288. }
  289. };
  290. void EditingEngine::move_down(double page_height_factor)
  291. {
  292. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  293. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  294. TextPosition new_cursor;
  295. if (m_editor->is_wrapping_enabled()) {
  296. auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
  297. new_cursor = m_editor->text_position_at_content_position(position_below);
  298. } else {
  299. size_t new_line = min(m_editor->line_count() - 1, m_editor->cursor().line() + pixels / m_editor->line_height());
  300. size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
  301. new_cursor = { new_line, new_column };
  302. }
  303. m_editor->set_cursor(new_cursor);
  304. };
  305. }
  306. void EditingEngine::move_page_up()
  307. {
  308. move_up(1);
  309. };
  310. void EditingEngine::move_page_down()
  311. {
  312. move_down(1);
  313. };
  314. void EditingEngine::move_to_first_line()
  315. {
  316. m_editor->set_cursor(0, 0);
  317. };
  318. void EditingEngine::move_to_last_line()
  319. {
  320. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1].length());
  321. };
  322. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  323. {
  324. auto selection = m_editor->normalized_selection();
  325. if (!selection.is_valid()) {
  326. first_line = m_editor->cursor().line();
  327. last_line = m_editor->cursor().line();
  328. return;
  329. }
  330. first_line = selection.start().line();
  331. last_line = selection.end().line();
  332. if (first_line != last_line && selection.end().column() == 0)
  333. last_line -= 1;
  334. }
  335. void EditingEngine::move_selected_lines_up()
  336. {
  337. if (!m_editor->is_editable())
  338. return;
  339. size_t first_line;
  340. size_t last_line;
  341. get_selection_line_boundaries(first_line, last_line);
  342. if (first_line == 0)
  343. return;
  344. auto& lines = m_editor->document().lines();
  345. lines.insert((int)last_line, lines.take((int)first_line - 1));
  346. m_editor->set_cursor({ m_editor->cursor().line() - 1, m_editor->cursor().column() });
  347. if (m_editor->has_selection()) {
  348. m_editor->selection().start().set_line(m_editor->selection().start().line() - 1);
  349. m_editor->selection().end().set_line(m_editor->selection().end().line() - 1);
  350. }
  351. m_editor->did_change();
  352. m_editor->update();
  353. }
  354. void EditingEngine::move_selected_lines_down()
  355. {
  356. if (!m_editor->is_editable())
  357. return;
  358. size_t first_line;
  359. size_t last_line;
  360. get_selection_line_boundaries(first_line, last_line);
  361. auto& lines = m_editor->document().lines();
  362. VERIFY(lines.size() != 0);
  363. if (last_line >= lines.size() - 1)
  364. return;
  365. lines.insert((int)first_line, lines.take((int)last_line + 1));
  366. m_editor->set_cursor({ m_editor->cursor().line() + 1, m_editor->cursor().column() });
  367. if (m_editor->has_selection()) {
  368. m_editor->selection().start().set_line(m_editor->selection().start().line() + 1);
  369. m_editor->selection().end().set_line(m_editor->selection().end().line() + 1);
  370. }
  371. m_editor->did_change();
  372. m_editor->update();
  373. }
  374. void EditingEngine::delete_char()
  375. {
  376. if (!m_editor->is_editable())
  377. return;
  378. m_editor->do_delete();
  379. };
  380. void EditingEngine::delete_line()
  381. {
  382. if (!m_editor->is_editable())
  383. return;
  384. m_editor->delete_current_line();
  385. };
  386. }