EditingEngine.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Noncopyable.h>
  8. #include <LibGUI/Event.h>
  9. #include <LibGUI/TextDocument.h>
  10. namespace GUI {
  11. enum CursorWidth {
  12. NARROW,
  13. WIDE
  14. };
  15. enum EngineType {
  16. Regular,
  17. Vim,
  18. };
  19. class MoveLineUpOrDownCommand;
  20. class EditingEngine {
  21. AK_MAKE_NONCOPYABLE(EditingEngine);
  22. AK_MAKE_NONMOVABLE(EditingEngine);
  23. public:
  24. virtual ~EditingEngine() = default;
  25. virtual CursorWidth cursor_width() const { return NARROW; }
  26. void attach(TextEditor& editor);
  27. void detach();
  28. TextEditor& editor()
  29. {
  30. VERIFY(!m_editor.is_null());
  31. return *m_editor.unsafe_ptr();
  32. }
  33. virtual bool on_key(KeyEvent const& event);
  34. bool is_regular() const { return engine_type() == EngineType::Regular; }
  35. bool is_vim() const { return engine_type() == EngineType::Vim; }
  36. void get_selection_line_boundaries(Badge<MoveLineUpOrDownCommand>, size_t& first_line, size_t& last_line);
  37. protected:
  38. EditingEngine() = default;
  39. WeakPtr<TextEditor> m_editor;
  40. enum class DidMoveALine {
  41. No,
  42. Yes,
  43. };
  44. void move_one_left();
  45. void move_one_right();
  46. void move_one_helper(KeyEvent const& event, VerticalDirection direction);
  47. DidMoveALine move_one_up(KeyEvent const& event);
  48. DidMoveALine move_one_down(KeyEvent const& event);
  49. void move_to_previous_span();
  50. void move_to_next_span();
  51. void move_to_logical_line_beginning();
  52. void move_to_logical_line_end();
  53. void move_to_line_beginning();
  54. void move_to_line_end();
  55. void move_page_up();
  56. void move_page_down();
  57. void move_to_first_line();
  58. void move_to_last_line();
  59. void move_up(double page_height_factor);
  60. void move_down(double page_height_factor);
  61. void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
  62. void delete_line();
  63. void delete_char();
  64. virtual EngineType engine_type() const = 0;
  65. };
  66. class MoveLineUpOrDownCommand : public TextDocumentUndoCommand {
  67. public:
  68. MoveLineUpOrDownCommand(TextDocument&, KeyEvent event, EditingEngine&);
  69. virtual void undo() override;
  70. virtual void redo() override;
  71. bool merge_with(GUI::Command const&) override;
  72. String action_text() const override;
  73. static bool valid_operation(EditingEngine& engine, VerticalDirection direction);
  74. private:
  75. void move_lines(VerticalDirection);
  76. TextRange retrieve_selection(VerticalDirection);
  77. KeyEvent m_event;
  78. VerticalDirection m_direction;
  79. EditingEngine& m_engine;
  80. TextRange m_selection;
  81. TextPosition m_cursor;
  82. };
  83. }