buffer_manager.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. #include <memory>
  5. #include <list>
  6. #include <filesystem>
  7. #include <optional>
  8. #include "lumacs/buffer.hpp" // Include the Buffer class definition
  9. #include "lumacs/window.hpp" // For shared_ptr<Window>
  10. #include "lumacs/i_editor_notifier.hpp"
  11. #include "lumacs/i_window_manager.hpp"
  12. namespace lumacs {
  13. // class EditorCore; // Removed as we depend on interfaces now
  14. /// @brief Manages the creation, loading, and manipulation of editor buffers.
  15. class BufferManager {
  16. public:
  17. BufferManager(IEditorNotifier& notifier, IWindowManager& window_manager);
  18. /// @brief Creates a new scratch buffer and makes it active in the current window.
  19. /// @param name The name of the new buffer (defaults to "*scratch*").
  20. void new_buffer(std::string name = "*scratch*");
  21. /// @brief Creates a buffer but does not attach it to any window.
  22. /// Useful for initialization or background buffer creation.
  23. std::shared_ptr<Buffer> create_buffer_no_window(std::string name);
  24. /// @brief Loads the content of a file into a new or existing buffer.
  25. /// If the file is already open, switches to that buffer.
  26. /// @param path The path to the file to load.
  27. /// @return True if the file was loaded successfully, false otherwise.
  28. bool load_file(const std::filesystem::path& path);
  29. /// @brief Switches the active buffer in the current window to the buffer with the given name.
  30. /// @param name The name of the buffer to switch to.
  31. /// @return True if the buffer was switched successfully, false otherwise.
  32. bool switch_buffer_in_window(const std::string& name);
  33. /// @brief Closes the buffer with the given name.
  34. /// If the buffer is displayed in any window, that window's buffer will be changed.
  35. /// Cannot close the last remaining buffer.
  36. /// @param name The name of the buffer to close.
  37. /// @return True if the buffer was closed successfully, false otherwise.
  38. bool close_buffer(const std::string& name);
  39. /// @brief Returns a shared pointer to the currently active buffer.
  40. /// @return The active buffer.
  41. [[nodiscard]] std::shared_ptr<Buffer> active_buffer();
  42. /// @brief Returns a shared pointer to the buffer with the given name.
  43. /// @param name The name of the buffer to retrieve.
  44. /// @return A shared pointer to the buffer, or nullptr if not found.
  45. [[nodiscard]] std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name) const;
  46. /// @brief Returns a list of names of all currently managed buffers.
  47. [[nodiscard]] std::vector<std::string> get_buffer_names() const;
  48. /// @brief Structure containing summary information about a buffer.
  49. struct BufferInfo {
  50. std::string name;
  51. size_t size;
  52. bool modified;
  53. std::string mode;
  54. std::optional<std::filesystem::path> filepath;
  55. };
  56. /// @brief Get detailed information about all buffers.
  57. [[nodiscard]] std::vector<BufferInfo> get_all_buffer_info() const;
  58. private:
  59. IEditorNotifier& notifier_;
  60. IWindowManager& window_manager_;
  61. std::list<std::shared_ptr<Buffer>> buffers_; // Owns the buffers
  62. };
  63. } // namespace lumacs