buffer_manager.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Loads the content of a file into a new or existing buffer.
  20. /// If the file is already open, switches to that buffer.
  21. /// @param path The path to the file to load.
  22. /// @return True if the file was loaded successfully, false otherwise.
  23. bool load_file(const std::filesystem::path& path);
  24. /// @brief Switches the active buffer in the current window to the buffer with the given name.
  25. /// @param name The name of the buffer to switch to.
  26. /// @return True if the buffer was switched successfully, false otherwise.
  27. bool switch_buffer_in_window(const std::string& name);
  28. /// @brief Closes the buffer with the given name.
  29. /// If the buffer is displayed in any window, that window's buffer will be changed.
  30. /// Cannot close the last remaining buffer.
  31. /// @param name The name of the buffer to close.
  32. /// @return True if the buffer was closed successfully, false otherwise.
  33. bool close_buffer(const std::string& name);
  34. /// @brief Returns a shared pointer to the currently active buffer.
  35. /// @return The active buffer.
  36. [[nodiscard]] std::shared_ptr<Buffer> active_buffer();
  37. /// @brief Returns a shared pointer to the buffer with the given name.
  38. /// @param name The name of the buffer to retrieve.
  39. /// @return A shared pointer to the buffer, or nullptr if not found.
  40. [[nodiscard]] std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name) const;
  41. /// @brief Returns a list of names of all currently managed buffers.
  42. [[nodiscard]] std::vector<std::string> get_buffer_names() const;
  43. /// @brief Structure containing summary information about a buffer.
  44. struct BufferInfo {
  45. std::string name;
  46. size_t size;
  47. bool modified;
  48. std::string mode;
  49. std::optional<std::filesystem::path> filepath;
  50. };
  51. /// @brief Get detailed information about all buffers.
  52. [[nodiscard]] std::vector<BufferInfo> get_all_buffer_info() const;
  53. private:
  54. EditorCore& core_;
  55. std::list<std::shared_ptr<Buffer>> buffers_; // Owns the buffers
  56. };
  57. } // namespace lumacs