buffer_manager.hpp 2.9 KB

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