minibuffer_manager.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include <functional>
  3. #include <optional>
  4. #include <memory>
  5. #include <unordered_map>
  6. #include "lumacs/command_system.hpp"
  7. #include "lumacs/history_manager.hpp"
  8. #include "lumacs/minibuffer_mode_hash.hpp"
  9. #include "lumacs/minibuffer_mode.hpp"
  10. #include "lumacs/completion_common.hpp"
  11. #include "lumacs/isearch_manager.hpp"
  12. namespace lumacs {
  13. // Forward declarations to avoid circular dependencies
  14. class EditorCore;
  15. class LuaApi;
  16. class CompletionSystem;
  17. /// @brief Manages the state and logic for the minibuffer, independent of the UI.
  18. class MinibufferManager {
  19. public:
  20. MinibufferManager(EditorCore& core, LuaApi& lua_api, CompletionSystem& completion_system);
  21. /// @brief Sets the minibuffer into a specific mode and displays a prompt.
  22. void activate_minibuffer(MinibufferMode mode, const std::string& prompt,
  23. std::function<void(const std::string&)> on_submit,
  24. std::function<void()> on_cancel = nullptr);
  25. /// @brief Deactivates the minibuffer and clears its state.
  26. void deactivate_minibuffer();
  27. /// @brief Handles a key event when the minibuffer is active.
  28. /// @return True if the key was handled, false otherwise.
  29. bool handle_key_event(const std::string& key_name);
  30. /// @brief Returns the current prompt string for display.
  31. std::string get_prompt() const;
  32. /// @brief Returns the current input buffer content for display.
  33. std::string get_input_buffer() const;
  34. /// @brief Returns the current cursor position within the input buffer.
  35. size_t get_cursor_position() const;
  36. /// @brief Returns whether the minibuffer is currently active.
  37. bool is_active() const { return current_mode_ != MinibufferMode::None; }
  38. /// @brief Returns the current minibuffer mode.
  39. MinibufferMode get_current_mode() const { return current_mode_; }
  40. /// @brief Sets the input buffer content directly.
  41. void set_input_buffer(const std::string& input);
  42. // History navigation
  43. void history_previous();
  44. void history_next();
  45. // Completion related methods
  46. void update_completion_candidates();
  47. std::vector<CompletionCandidate> get_completion_candidates() const;
  48. std::optional<std::string> get_current_completion() const;
  49. void complete(); // For tab completion
  50. // ISearch related methods (delegated to ISearchManager)
  51. bool is_isearch_active() const { return isearch_manager_.is_active(); }
  52. std::optional<Range> get_isearch_match_range() const { return isearch_manager_.match_range(); }
  53. bool is_isearch_failed() const { return isearch_manager_.is_failed(); }
  54. // ISearch control methods
  55. void start_isearch(bool forward);
  56. /// @brief Get access to the ISearch manager for advanced control.
  57. ISearchManager& isearch_manager() { return isearch_manager_; }
  58. /// @brief Parses a command string and executes the corresponding command.
  59. /// @brief Parses a command string and executes the corresponding command.
  60. /// @param command_line The string containing the command name and its arguments.
  61. /// @return The result of the command execution.
  62. CommandResult parse_and_execute_command_string(const std::string& command_line); // Made public
  63. private:
  64. EditorCore& core_;
  65. [[maybe_unused]] LuaApi& lua_api_; // Reserved for future Lua integration
  66. CompletionSystem& completion_system_;
  67. MinibufferMode current_mode_ = MinibufferMode::None;
  68. std::string prompt_text_;
  69. std::string input_buffer_;
  70. std::function<void(const std::string&)> on_submit_callback_;
  71. std::function<void()> on_cancel_callback_;
  72. size_t cursor_position_ = 0; // Tracks the current cursor position within input_buffer_
  73. // History
  74. std::unordered_map<MinibufferMode, HistoryManager> histories_;
  75. HistoryManager* current_history_ = nullptr; // Pointer to the active history manager
  76. // Completion
  77. std::vector<CompletionCandidate> completion_candidates_;
  78. size_t completion_index_ = 0;
  79. // ISearch is managed by ISearchManager
  80. ISearchManager isearch_manager_;
  81. /// @brief Helper to add current input to history if it's new.
  82. void add_to_history();
  83. };
  84. } // namespace lumacs