isearch_manager.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <optional>
  3. #include <string>
  4. #include <functional>
  5. #include "lumacs/buffer.hpp" // For Position and Range
  6. namespace lumacs {
  7. // Forward declarations
  8. class EditorCore;
  9. /// @brief Manages incremental search (ISearch) state and logic.
  10. /// Extracted from MinibufferManager to follow single responsibility principle.
  11. class ISearchManager {
  12. public:
  13. explicit ISearchManager(EditorCore& core);
  14. /// @brief Start an incremental search.
  15. /// @param forward True for forward search, false for backward.
  16. void start(bool forward);
  17. /// @brief Stop the incremental search and optionally restore cursor.
  18. /// @param restore_cursor If true, restore cursor to position before search started.
  19. void stop(bool restore_cursor = true);
  20. /// @brief Update the search with a new query string.
  21. /// @param query The search query.
  22. void update(const std::string& query);
  23. /// @brief Move to the next match in the current search direction.
  24. void next_match();
  25. /// @brief Move to the previous match (opposite of current direction).
  26. void previous_match();
  27. /// @brief Check if incremental search is currently active.
  28. [[nodiscard]] bool is_active() const { return active_; }
  29. /// @brief Get the current match range, if any.
  30. [[nodiscard]] std::optional<Range> match_range() const { return match_range_; }
  31. /// @brief Check if the last search failed to find a match.
  32. [[nodiscard]] bool is_failed() const { return failed_; }
  33. /// @brief Get the current search direction.
  34. [[nodiscard]] bool is_forward() const { return direction_forward_; }
  35. /// @brief Get the last search query.
  36. [[nodiscard]] const std::string& last_query() const { return last_query_; }
  37. /// @brief Set the search direction.
  38. void set_direction(bool forward) { direction_forward_ = forward; }
  39. /// @brief Set the prompt text (used for message display).
  40. void set_prompt(const std::string& prompt) { prompt_text_ = prompt; }
  41. private:
  42. EditorCore& core_;
  43. bool active_ = false;
  44. bool direction_forward_ = true;
  45. bool failed_ = false;
  46. std::optional<Range> match_range_;
  47. std::optional<Position> start_cursor_; // Cursor position when search started
  48. std::string last_query_;
  49. std::string prompt_text_;
  50. };
  51. } // namespace lumacs