EditingEngine.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. void EditingEngine::move_selected_lines_up()
  360. {
  361. if (!m_editor->is_editable())
  362. return;
  363. size_t first_line;
  364. size_t last_line;
  365. get_selection_line_boundaries(first_line, last_line);
  366. if (first_line == 0)
  367. return;
  368. auto& lines = m_editor->document().lines();
  369. lines.insert((int)last_line, lines.take((int)first_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::move_selected_lines_down()
  379. {
  380. if (!m_editor->is_editable())
  381. return;
  382. size_t first_line;
  383. size_t last_line;
  384. get_selection_line_boundaries(first_line, last_line);
  385. auto& lines = m_editor->document().lines();
  386. ASSERT(lines.size() != 0);
  387. if (last_line >= lines.size() - 1)
  388. return;
  389. lines.insert((int)first_line, lines.take((int)last_line + 1));
  390. m_editor->set_cursor({ first_line + 1, 0 });
  391. if (m_editor->has_selection()) {
  392. m_editor->selection()->set_start({ first_line + 1, 0 });
  393. m_editor->selection()->set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
  394. }
  395. m_editor->did_change();
  396. m_editor->update();
  397. }
  398. void EditingEngine::delete_char()
  399. {
  400. if (!m_editor->is_editable())
  401. return;
  402. m_editor->do_delete();
  403. };
  404. void EditingEngine::delete_line()
  405. {
  406. if (!m_editor->is_editable())
  407. return;
  408. m_editor->delete_current_line();
  409. };
  410. }