minibuffer_manager.hpp 4.5 KB

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