editor_core.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. #include "lumacs/buffer.hpp"
  3. #include "lumacs/window.hpp"
  4. #include "lumacs/kill_ring.hpp"
  5. #include "lumacs/theme.hpp"
  6. #include <memory>
  7. #include <functional>
  8. #include <vector>
  9. #include <list>
  10. namespace lumacs {
  11. /// Editor state change events
  12. enum class EditorEvent {
  13. BufferModified,
  14. CursorMoved,
  15. ViewportChanged,
  16. WindowLayoutChanged, // New event
  17. WindowFocused, // New event: a different window gained focus
  18. Message, // New event
  19. CommandMode, // Trigger command mode (minibuffer)
  20. BufferSwitchMode, // Trigger buffer switch mode
  21. KillBufferMode, // Trigger kill buffer mode
  22. ISearchMode, // Trigger incremental search mode
  23. ISearchBackwardMode, // Trigger incremental search mode (backward)
  24. Quit
  25. };
  26. struct LayoutNode;
  27. /// Core editor logic, independent of UI framework
  28. class EditorCore {
  29. public:
  30. EditorCore();
  31. ~EditorCore() = default;
  32. // Disable copy, allow move
  33. EditorCore(const EditorCore&) = delete;
  34. EditorCore& operator=(const EditorCore&) = delete;
  35. EditorCore(EditorCore&&) noexcept = default;
  36. EditorCore& operator=(EditorCore&&) noexcept = default;
  37. // === Message System ===
  38. void set_message(std::string msg) {
  39. last_message_ = std::move(msg);
  40. emit_event(EditorEvent::Message);
  41. }
  42. const std::string& last_message() const { return last_message_; }
  43. // === Actions ===
  44. void enter_command_mode() {
  45. emit_event(EditorEvent::CommandMode);
  46. }
  47. void enter_buffer_switch_mode() {
  48. emit_event(EditorEvent::BufferSwitchMode);
  49. }
  50. void enter_kill_buffer_mode() {
  51. emit_event(EditorEvent::KillBufferMode);
  52. }
  53. void enter_isearch_mode() {
  54. emit_event(EditorEvent::ISearchMode);
  55. }
  56. void enter_isearch_backward_mode() {
  57. emit_event(EditorEvent::ISearchBackwardMode);
  58. }
  59. // === Buffer Management ===
  60. /// Get the current buffer (of the active window)
  61. [[nodiscard]] const Buffer& buffer() const noexcept;
  62. [[nodiscard]] Buffer& buffer() noexcept;
  63. /// Load a file into the current window
  64. bool load_file(const std::filesystem::path& path);
  65. /// Create a new empty buffer in current window
  66. void new_buffer(std::string name = "*scratch*");
  67. /// Get list of all buffer names
  68. [[nodiscard]] std::vector<std::string> get_buffer_names() const;
  69. /// Get buffer by name (returns nullptr if not found)
  70. [[nodiscard]] std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name);
  71. /// Switch active window to buffer by name
  72. bool switch_buffer_in_window(const std::string& name);
  73. /// Close buffer by name (returns false if buffer is displayed or doesn't exist)
  74. bool close_buffer(const std::string& name);
  75. /// Buffer information for list display
  76. struct BufferInfo {
  77. std::string name;
  78. size_t size;
  79. bool modified;
  80. std::string mode;
  81. std::optional<std::filesystem::path> filepath;
  82. };
  83. /// Get information about all buffers
  84. [[nodiscard]] std::vector<BufferInfo> get_all_buffer_info() const;
  85. // === Window Management ===
  86. /// Split the current window horizontally (active window becomes top, new one bottom)
  87. void split_horizontally();
  88. /// Split the current window vertically (active window becomes left, new one right)
  89. void split_vertically();
  90. /// Close the current window
  91. void close_active_window();
  92. /// Move focus to the next window
  93. void next_window();
  94. /// Get the active window
  95. std::shared_ptr<Window> active_window() const { return active_window_; }
  96. /// Get the root of the layout tree (for rendering)
  97. std::shared_ptr<LayoutNode> root_layout() const { return root_node_; }
  98. // === Cursor Management (Proxies to active window) ===
  99. [[nodiscard]] Position cursor() const noexcept;
  100. void set_cursor(Position pos);
  101. // === Cursor Movement (Proxies to active window) ===
  102. void move_up();
  103. void move_down();
  104. void move_left();
  105. void move_right();
  106. void move_to_line_start();
  107. void move_to_line_end();
  108. void move_forward_word();
  109. void move_backward_word();
  110. void page_up();
  111. void page_down();
  112. void goto_beginning();
  113. void goto_end();
  114. void goto_line(size_t line);
  115. // === Viewport Management (Proxies to active window) ===
  116. const Viewport& viewport() const noexcept;
  117. void set_viewport_size(int width, int height);
  118. void adjust_scroll();
  119. std::pair<size_t, size_t> visible_line_range() const;
  120. // === Event Callbacks ===
  121. using EventCallback = std::function<void(EditorEvent)>;
  122. void on_event(EventCallback callback) {
  123. event_callbacks_.push_back(std::move(callback));
  124. }
  125. // === Actions ===
  126. void request_quit() {
  127. emit_event(EditorEvent::Quit);
  128. }
  129. // === Undo/Redo ===
  130. bool undo();
  131. bool redo();
  132. bool can_undo() const;
  133. bool can_redo() const;
  134. // === Kill Ring ===
  135. /// Get the kill ring
  136. [[nodiscard]] KillRing& kill_ring() noexcept { return kill_ring_; }
  137. [[nodiscard]] const KillRing& kill_ring() const noexcept { return kill_ring_; }
  138. /// Kill (cut) text from position to end of line
  139. void kill_line();
  140. /// Kill (cut) the active region
  141. void kill_region();
  142. /// Copy the active region to kill ring (without deleting)
  143. void copy_region_as_kill();
  144. /// Yank (paste) from kill ring
  145. void yank();
  146. /// Yank and pop to previous kill ring entry
  147. void yank_pop();
  148. /// Kill word forward
  149. void kill_word();
  150. /// Kill word backward
  151. void backward_kill_word();
  152. // === Theme Management ===
  153. /// Get the theme manager
  154. [[nodiscard]] ThemeManager& theme_manager() noexcept { return theme_manager_; }
  155. [[nodiscard]] const ThemeManager& theme_manager() const noexcept { return theme_manager_; }
  156. /// Set active theme
  157. void set_theme(const std::string& name) { theme_manager_.set_active_theme(name); }
  158. /// Get active theme
  159. [[nodiscard]] std::shared_ptr<Theme> active_theme() const { return theme_manager_.active_theme(); }
  160. private:
  161. // All open buffers
  162. std::list<std::shared_ptr<Buffer>> buffers_;
  163. // Word movement helpers
  164. Position calculate_forward_word_pos(Position start_pos);
  165. Position calculate_backward_word_pos(Position start_pos);
  166. // Window layout
  167. std::shared_ptr<LayoutNode> root_node_;
  168. std::shared_ptr<Window> active_window_;
  169. std::string last_message_;
  170. std::vector<EventCallback> event_callbacks_;
  171. // Kill ring for cut/copy/paste
  172. KillRing kill_ring_;
  173. // Last yank position (for yank-pop)
  174. std::optional<Position> last_yank_start_;
  175. std::optional<Position> last_yank_end_;
  176. // Theme manager
  177. ThemeManager theme_manager_;
  178. void emit_event(EditorEvent event);
  179. // Helper to find a node containing the active window
  180. LayoutNode* find_parent_node(LayoutNode* root, std::shared_ptr<Window> target);
  181. // Helper to collect all windows in traversal order
  182. void collect_windows(LayoutNode* node, std::vector<std::shared_ptr<Window>>& windows);
  183. };
  184. struct LayoutNode {
  185. enum class Type { Leaf, HorizontalSplit, VerticalSplit };
  186. Type type;
  187. // If Leaf
  188. std::shared_ptr<Window> window;
  189. // If Split
  190. std::shared_ptr<LayoutNode> child1;
  191. std::shared_ptr<LayoutNode> child2;
  192. float ratio = 0.5f; // For future resizing
  193. LayoutNode(std::shared_ptr<Window> w) : type(Type::Leaf), window(w) {}
  194. LayoutNode(Type t, std::shared_ptr<LayoutNode> c1, std::shared_ptr<LayoutNode> c2)
  195. : type(t), child1(c1), child2(c2) {}
  196. };
  197. } // namespace lumacs