EditingEngine.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. bool const condition_for_up_to_beginning = direction == VerticalDirection::Up && m_editor->cursor().line() == 0;
  82. bool const condition_for_down_to_end = direction == VerticalDirection::Down && m_editor->cursor().line() == (m_editor->line_count() - 1);
  83. if (condition_for_up || condition_for_down || m_editor->is_wrapping_enabled())
  84. m_editor->update_selection(event.shift());
  85. // Shift + Up on the top line (or only line) selects from the cursor to the start of the line.
  86. if (condition_for_up_to_beginning) {
  87. m_editor->update_selection(event.shift());
  88. move_to_line_beginning();
  89. }
  90. // Shift + Down on the bottom line (or only line) selects from the cursor to the end of the line.
  91. if (condition_for_down_to_end) {
  92. m_editor->update_selection(event.shift());
  93. move_to_line_end();
  94. }
  95. move_one_helper(event, direction);
  96. return true;
  97. }
  98. if (event.key() == KeyCode::Key_Home) {
  99. m_editor->update_selection(event.shift());
  100. if (event.ctrl()) {
  101. move_to_first_line();
  102. } else {
  103. move_to_line_beginning();
  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_End) {
  112. m_editor->update_selection(event.shift());
  113. if (event.ctrl()) {
  114. move_to_last_line();
  115. } else {
  116. move_to_line_end();
  117. }
  118. if (event.shift() && m_editor->selection().start().is_valid()) {
  119. m_editor->selection().set_end(m_editor->cursor());
  120. m_editor->did_update_selection();
  121. }
  122. return true;
  123. }
  124. if (event.key() == KeyCode::Key_PageUp) {
  125. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  126. m_editor->update_selection(event.shift());
  127. }
  128. move_page_up();
  129. if (event.shift() && m_editor->selection().start().is_valid()) {
  130. m_editor->selection().set_end(m_editor->cursor());
  131. m_editor->did_update_selection();
  132. }
  133. return true;
  134. }
  135. if (event.key() == KeyCode::Key_PageDown) {
  136. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  137. m_editor->update_selection(event.shift());
  138. }
  139. move_page_down();
  140. if (event.shift() && m_editor->selection().start().is_valid()) {
  141. m_editor->selection().set_end(m_editor->cursor());
  142. m_editor->did_update_selection();
  143. }
  144. return true;
  145. }
  146. return false;
  147. }
  148. void EditingEngine::move_one_left()
  149. {
  150. if (m_editor->cursor().column() > 0) {
  151. auto new_column = m_editor->document().get_previous_grapheme_cluster_boundary(m_editor->cursor());
  152. m_editor->set_cursor(m_editor->cursor().line(), new_column);
  153. } else if (m_editor->cursor().line() > 0) {
  154. auto new_line = m_editor->cursor().line() - 1;
  155. auto new_column = m_editor->lines()[new_line]->length();
  156. m_editor->set_cursor(new_line, new_column);
  157. }
  158. }
  159. void EditingEngine::move_one_right()
  160. {
  161. auto new_line = m_editor->cursor().line();
  162. auto new_column = m_editor->cursor().column();
  163. if (m_editor->cursor().column() < m_editor->current_line().length()) {
  164. new_line = m_editor->cursor().line();
  165. new_column = m_editor->document().get_next_grapheme_cluster_boundary(m_editor->cursor());
  166. } else if (m_editor->cursor().line() != m_editor->line_count() - 1) {
  167. new_line = m_editor->cursor().line() + 1;
  168. new_column = 0;
  169. }
  170. m_editor->set_cursor(new_line, new_column);
  171. }
  172. void EditingEngine::move_to_previous_span()
  173. {
  174. TextPosition new_cursor;
  175. if (m_editor->document().has_spans()) {
  176. auto span = m_editor->document().first_non_skippable_span_before(m_editor->cursor());
  177. if (span.has_value()) {
  178. new_cursor = span.value().range.start();
  179. } else {
  180. // No remaining spans, just use word break calculation
  181. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  182. }
  183. } else {
  184. new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
  185. }
  186. m_editor->set_cursor(new_cursor);
  187. }
  188. void EditingEngine::move_to_next_span()
  189. {
  190. TextPosition new_cursor;
  191. if (m_editor->document().has_spans()) {
  192. auto span = m_editor->document().first_non_skippable_span_after(m_editor->cursor());
  193. if (span.has_value()) {
  194. new_cursor = span.value().range.start();
  195. } else {
  196. // No remaining spans, just use word break calculation
  197. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  198. }
  199. } else {
  200. new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
  201. }
  202. m_editor->set_cursor(new_cursor);
  203. }
  204. void EditingEngine::move_to_logical_line_beginning()
  205. {
  206. TextPosition new_cursor;
  207. size_t first_nonspace_column = m_editor->current_line().first_non_whitespace_column();
  208. if (m_editor->cursor().column() == first_nonspace_column) {
  209. new_cursor = { m_editor->cursor().line(), 0 };
  210. } else {
  211. new_cursor = { m_editor->cursor().line(), first_nonspace_column };
  212. }
  213. m_editor->set_cursor(new_cursor);
  214. }
  215. void EditingEngine::move_to_line_beginning()
  216. {
  217. if (m_editor->is_wrapping_enabled()) {
  218. TextPosition new_cursor;
  219. auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
  220. auto start_of_visual_line = m_editor->text_position_at_content_position(home_position);
  221. auto first_non_space_column = m_editor->current_line().first_non_whitespace_column();
  222. // Subsequent "move_to_line_beginning()" calls move us in the following way:
  223. // 1. To the start of the current visual line
  224. // 2. To the first non-whitespace character on the logical line
  225. // 3. To the first character on the logical line
  226. // ...and then repeat 2 and 3.
  227. if (m_editor->cursor() == start_of_visual_line) {
  228. // Already at 1 so go to 2
  229. new_cursor = { m_editor->cursor().line(), first_non_space_column };
  230. } else if (m_editor->cursor().column() == first_non_space_column) {
  231. // At 2 so go to 3
  232. new_cursor = { m_editor->cursor().line(), 0 };
  233. } else {
  234. // Anything else, so go to 1
  235. new_cursor = start_of_visual_line;
  236. }
  237. m_editor->set_cursor(new_cursor);
  238. } else {
  239. move_to_logical_line_beginning();
  240. }
  241. }
  242. void EditingEngine::move_to_line_end()
  243. {
  244. if (m_editor->is_wrapping_enabled())
  245. m_editor->set_cursor_to_end_of_visual_line();
  246. else
  247. move_to_logical_line_end();
  248. }
  249. void EditingEngine::move_to_logical_line_end()
  250. {
  251. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().length() });
  252. }
  253. void EditingEngine::move_one_helper(KeyEvent const& event, VerticalDirection direction)
  254. {
  255. auto const result = direction == VerticalDirection::Up ? move_one_up(event) : move_one_down(event);
  256. if (result != DidMoveALine::Yes && event.shift() && m_editor->selection().start().is_valid()) {
  257. m_editor->selection().set_end(m_editor->cursor());
  258. m_editor->did_update_selection();
  259. }
  260. }
  261. EditingEngine::DidMoveALine EditingEngine::move_one_up(KeyEvent const& event)
  262. {
  263. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  264. if (event.ctrl() && event.shift()) {
  265. if (MoveLineUpOrDownCommand::valid_operation(*this, VerticalDirection::Up)) {
  266. m_editor->execute<MoveLineUpOrDownCommand>(Badge<EditingEngine> {}, event, *this);
  267. return DidMoveALine::Yes;
  268. }
  269. return DidMoveALine::No;
  270. }
  271. auto position_above = m_editor->cursor_content_rect().location().translated(0, -m_editor->line_height());
  272. TextPosition new_cursor = m_editor->text_position_at_content_position(position_above);
  273. m_editor->set_cursor(new_cursor);
  274. }
  275. return DidMoveALine::No;
  276. };
  277. EditingEngine::DidMoveALine EditingEngine::move_one_down(KeyEvent const& event)
  278. {
  279. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  280. if (event.ctrl() && event.shift()) {
  281. if (MoveLineUpOrDownCommand::valid_operation(*this, VerticalDirection::Down)) {
  282. m_editor->execute<MoveLineUpOrDownCommand>(Badge<EditingEngine> {}, event, *this);
  283. return DidMoveALine::Yes;
  284. }
  285. return DidMoveALine::No;
  286. }
  287. auto position_below = m_editor->cursor_content_rect().location().translated(0, m_editor->line_height());
  288. TextPosition new_cursor = m_editor->text_position_at_content_position(position_below);
  289. m_editor->set_cursor(new_cursor);
  290. }
  291. return DidMoveALine::No;
  292. };
  293. void EditingEngine::move_up(double page_height_factor)
  294. {
  295. if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
  296. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  297. auto position_above = m_editor->cursor_content_rect().location().translated(0, -pixels);
  298. TextPosition new_cursor = m_editor->text_position_at_content_position(position_above);
  299. m_editor->set_cursor(new_cursor);
  300. }
  301. };
  302. void EditingEngine::move_down(double page_height_factor)
  303. {
  304. if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
  305. int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
  306. auto position_below = m_editor->cursor_content_rect().location().translated(0, pixels);
  307. TextPosition new_cursor = m_editor->text_position_at_content_position(position_below);
  308. m_editor->set_cursor(new_cursor);
  309. };
  310. }
  311. void EditingEngine::move_page_up()
  312. {
  313. move_up(1);
  314. };
  315. void EditingEngine::move_page_down()
  316. {
  317. move_down(1);
  318. };
  319. void EditingEngine::move_to_first_line()
  320. {
  321. m_editor->set_cursor(0, 0);
  322. };
  323. void EditingEngine::move_to_last_line()
  324. {
  325. m_editor->set_cursor(m_editor->line_count() - 1, m_editor->lines()[m_editor->line_count() - 1]->length());
  326. };
  327. void EditingEngine::get_selection_line_boundaries(Badge<MoveLineUpOrDownCommand>, size_t& first_line, size_t& last_line)
  328. {
  329. get_selection_line_boundaries(first_line, last_line);
  330. }
  331. void EditingEngine::get_selection_line_boundaries(size_t& first_line, size_t& last_line)
  332. {
  333. auto selection = m_editor->normalized_selection();
  334. if (!selection.is_valid()) {
  335. first_line = m_editor->cursor().line();
  336. last_line = m_editor->cursor().line();
  337. return;
  338. }
  339. first_line = selection.start().line();
  340. last_line = selection.end().line();
  341. if (first_line != last_line && selection.end().column() == 0)
  342. last_line -= 1;
  343. }
  344. void EditingEngine::delete_char()
  345. {
  346. if (!m_editor->is_editable())
  347. return;
  348. m_editor->do_delete();
  349. };
  350. void EditingEngine::delete_line()
  351. {
  352. if (!m_editor->is_editable())
  353. return;
  354. m_editor->delete_current_line();
  355. };
  356. MoveLineUpOrDownCommand::MoveLineUpOrDownCommand(TextDocument& document, KeyEvent event, EditingEngine& engine)
  357. : TextDocumentUndoCommand(document)
  358. , m_event(move(event))
  359. , m_direction(key_code_to_vertical_direction(m_event.key()))
  360. , m_engine(engine)
  361. , m_selection(m_engine.editor().selection())
  362. , m_cursor(m_engine.editor().cursor())
  363. {
  364. }
  365. void MoveLineUpOrDownCommand::redo()
  366. {
  367. move_lines(m_direction);
  368. }
  369. void MoveLineUpOrDownCommand::undo()
  370. {
  371. move_lines(!m_direction);
  372. }
  373. bool MoveLineUpOrDownCommand::merge_with(GUI::Command const&)
  374. {
  375. return false;
  376. }
  377. DeprecatedString MoveLineUpOrDownCommand::action_text() const
  378. {
  379. return "Move a line";
  380. }
  381. bool MoveLineUpOrDownCommand::valid_operation(EditingEngine& engine, VerticalDirection direction)
  382. {
  383. VERIFY(engine.editor().line_count() != 0);
  384. auto const& selection = engine.editor().selection().normalized();
  385. if (selection.is_valid()) {
  386. if ((direction == VerticalDirection::Up && selection.start().line() == 0) || (direction == VerticalDirection::Down && selection.end().line() >= engine.editor().line_count() - 1))
  387. return false;
  388. } else {
  389. size_t first_line;
  390. size_t last_line;
  391. engine.get_selection_line_boundaries(Badge<MoveLineUpOrDownCommand> {}, first_line, last_line);
  392. if ((direction == VerticalDirection::Up && first_line == 0) || (direction == VerticalDirection::Down && last_line >= engine.editor().line_count() - 1))
  393. return false;
  394. }
  395. return true;
  396. }
  397. TextRange MoveLineUpOrDownCommand::retrieve_selection(VerticalDirection direction)
  398. {
  399. if (direction == m_direction)
  400. return m_selection;
  401. auto const offset_selection = [this](auto const offset) {
  402. auto tmp = m_selection;
  403. tmp.start().set_line(tmp.start().line() + offset);
  404. tmp.end().set_line(tmp.end().line() + offset);
  405. return tmp;
  406. };
  407. if (direction == VerticalDirection::Up)
  408. return offset_selection(1);
  409. if (direction == VerticalDirection::Down)
  410. return offset_selection(-1);
  411. VERIFY_NOT_REACHED();
  412. }
  413. void MoveLineUpOrDownCommand::move_lines(VerticalDirection direction)
  414. {
  415. if (m_event.shift() && m_selection.is_valid()) {
  416. m_engine.editor().set_selection(retrieve_selection(direction));
  417. m_engine.editor().did_update_selection();
  418. }
  419. if (!m_engine.editor().is_editable())
  420. return;
  421. size_t first_line;
  422. size_t last_line;
  423. m_engine.get_selection_line_boundaries(Badge<MoveLineUpOrDownCommand> {}, first_line, last_line);
  424. auto const offset = direction == VerticalDirection::Up ? -1 : 1;
  425. auto const insertion_index = direction == VerticalDirection::Up ? last_line : first_line;
  426. auto const moved_line_index = offset + (direction != VerticalDirection::Up ? last_line : first_line);
  427. auto moved_line = m_document.take_line(moved_line_index);
  428. m_document.insert_line(insertion_index, move(moved_line));
  429. m_engine.editor().set_cursor({ m_engine.editor().cursor().line() + offset, m_engine.editor().cursor().column() });
  430. if (m_engine.editor().has_selection()) {
  431. m_engine.editor().selection().start().set_line(m_engine.editor().selection().start().line() + offset);
  432. m_engine.editor().selection().end().set_line(m_engine.editor().selection().end().line() + offset);
  433. }
  434. m_engine.editor().did_change();
  435. m_engine.editor().update();
  436. }
  437. }