buffer.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <optional>
  5. #include <filesystem>
  6. #include <memory>
  7. namespace lumacs {
  8. /// Represents a cursor position in a buffer
  9. struct Position {
  10. size_t line;
  11. size_t column;
  12. auto operator<=>(const Position&) const = default;
  13. };
  14. /// Represents a range in the buffer
  15. struct Range {
  16. Position start;
  17. Position end;
  18. auto operator<=>(const Range&) const = default;
  19. };
  20. /// A text buffer that manages the content of a file or scratch buffer
  21. class Buffer {
  22. public:
  23. /// Create an empty buffer
  24. Buffer();
  25. /// Create a buffer with a name (for scratch buffers)
  26. explicit Buffer(std::string name);
  27. /// Create a buffer from a file
  28. static std::optional<Buffer> from_file(const std::filesystem::path& path);
  29. // Disable copy, allow move
  30. Buffer(const Buffer&) = delete;
  31. Buffer& operator=(const Buffer&) = delete;
  32. Buffer(Buffer&&) noexcept = default;
  33. Buffer& operator=(Buffer&&) noexcept = default;
  34. ~Buffer() = default;
  35. // === Content Access ===
  36. /// Get the number of lines in the buffer
  37. [[nodiscard]] size_t line_count() const noexcept;
  38. /// Get a line by index (0-based)
  39. [[nodiscard]] const std::string& line(size_t index) const;
  40. /// Get all lines
  41. [[nodiscard]] const std::vector<std::string>& lines() const noexcept;
  42. /// Get the entire buffer content as a single string
  43. [[nodiscard]] std::string content() const;
  44. // === Modification ===
  45. /// Insert text at a position
  46. void insert(Position pos, std::string_view text);
  47. /// Insert a character at a position
  48. void insert_char(Position pos, char c);
  49. /// Insert a newline at a position
  50. void insert_newline(Position pos);
  51. /// Delete a range of text
  52. void erase(Range range);
  53. /// Delete a character at a position (backspace)
  54. void erase_char(Position pos);
  55. /// Replace text in a range
  56. void replace(Range range, std::string_view text);
  57. /// Clear the entire buffer
  58. void clear();
  59. // === File Operations ===
  60. /// Save the buffer to its associated file
  61. bool save();
  62. /// Save the buffer to a specific file
  63. bool save_as(const std::filesystem::path& path);
  64. /// Check if the buffer has been modified since last save
  65. [[nodiscard]] bool is_modified() const noexcept;
  66. /// Get the file path associated with this buffer (if any)
  67. [[nodiscard]] std::optional<std::filesystem::path> file_path() const noexcept;
  68. // === Buffer Properties ===
  69. /// Get the buffer name
  70. [[nodiscard]] const std::string& name() const noexcept;
  71. /// Set the buffer name
  72. void set_name(std::string name);
  73. /// Check if position is valid
  74. [[nodiscard]] bool is_valid_position(Position pos) const noexcept;
  75. /// Clamp a position to valid bounds
  76. [[nodiscard]] Position clamp_position(Position pos) const noexcept;
  77. private:
  78. std::string name_;
  79. std::vector<std::string> lines_;
  80. std::optional<std::filesystem::path> file_path_;
  81. bool modified_;
  82. /// Ensure the buffer has at least one line
  83. void ensure_min_lines();
  84. /// Mark the buffer as modified
  85. void mark_modified();
  86. };
  87. } // namespace lumacs