buffer.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <functional>
  5. #include <optional>
  6. #include <filesystem>
  7. #include <memory>
  8. namespace lumacs {
  9. /// Represents a cursor position in a buffer
  10. struct Position {
  11. size_t line;
  12. size_t column;
  13. auto operator<=>(const Position&) const = default;
  14. };
  15. /// Represents a range in the buffer
  16. struct Range {
  17. Position start;
  18. Position end;
  19. auto operator<=>(const Range&) const = default;
  20. };
  21. /// Buffer events for hooks/callbacks
  22. enum class BufferEvent {
  23. // Lifecycle events
  24. Created, // Buffer was created
  25. Loaded, // File was loaded into buffer
  26. Closed, // Buffer is being closed
  27. // Modification events
  28. BeforeChange, // About to modify buffer (can be used to save state for undo)
  29. AfterChange, // Buffer content was modified
  30. LineChanged, // Specific line was modified
  31. // File operations
  32. BeforeSave, // About to save
  33. AfterSave, // File was saved
  34. // Language/mode
  35. LanguageChanged // Buffer language/mode changed
  36. };
  37. /// Event data passed to callbacks
  38. struct BufferEventData {
  39. BufferEvent event;
  40. size_t line = 0; // Line number for LineChanged events
  41. std::string language = ""; // For LanguageChanged events
  42. };
  43. /// Text styling attributes for syntax highlighting
  44. struct TextAttribute {
  45. enum class Style {
  46. Normal = 0,
  47. Bold = 1,
  48. Italic = 2,
  49. Underline = 4,
  50. // Can combine with bitwise OR
  51. };
  52. // Common semantic colors for syntax highlighting
  53. enum class ColorType {
  54. Default,
  55. Keyword, // if, for, while, etc.
  56. String, // String literals
  57. Comment, // Comments
  58. Function, // Function names
  59. Type, // Type names, classes
  60. Number, // Numeric literals
  61. Operator, // +, -, *, etc.
  62. Variable, // Variable names
  63. Constant, // Constants, enums
  64. Error, // Error highlighting
  65. };
  66. ColorType color = ColorType::Default;
  67. int style_flags = 0; // Combination of Style flags
  68. TextAttribute() = default;
  69. TextAttribute(ColorType c, int s = 0) : color(c), style_flags(s) {}
  70. };
  71. /// A range with associated styling
  72. struct StyledRange {
  73. Range range;
  74. TextAttribute attr;
  75. StyledRange() = default;
  76. StyledRange(Range r, TextAttribute a) : range(r), attr(a) {}
  77. };
  78. /// Undo/Redo state snapshot
  79. struct UndoState {
  80. std::vector<std::string> lines;
  81. Position cursor;
  82. UndoState() = default;
  83. UndoState(const std::vector<std::string>& l, Position c) : lines(l), cursor(c) {}
  84. };
  85. /// A text buffer that manages the content of a file or scratch buffer
  86. class Buffer {
  87. public:
  88. /// Create an empty buffer
  89. Buffer();
  90. /// Create a buffer with a name (for scratch buffers)
  91. explicit Buffer(std::string name);
  92. /// Create a buffer from a file
  93. static std::optional<Buffer> from_file(const std::filesystem::path& path);
  94. // Disable copy, allow move
  95. Buffer(const Buffer&) = delete;
  96. Buffer& operator=(const Buffer&) = delete;
  97. Buffer(Buffer&&) noexcept = default;
  98. Buffer& operator=(Buffer&&) noexcept = default;
  99. ~Buffer() = default;
  100. // === Content Access ===
  101. /// Get the number of lines in the buffer
  102. [[nodiscard]] size_t line_count() const noexcept;
  103. /// Get a line by index (0-based)
  104. [[nodiscard]] const std::string& line(size_t index) const;
  105. /// Get all lines
  106. [[nodiscard]] const std::vector<std::string>& lines() const noexcept;
  107. /// Get the entire buffer content as a single string
  108. [[nodiscard]] std::string content() const;
  109. // === Modification ===
  110. /// Insert text at a position
  111. void insert(Position pos, std::string_view text);
  112. /// Insert a character at a position
  113. void insert_char(Position pos, char c);
  114. /// Insert a newline at a position
  115. void insert_newline(Position pos);
  116. /// Delete a range of text
  117. void erase(Range range);
  118. /// Delete a character at a position (backspace)
  119. void erase_char(Position pos);
  120. /// Replace text in a range
  121. void replace(Range range, std::string_view text);
  122. /// Find text starting from a position
  123. [[nodiscard]] std::optional<Range> find(const std::string& query, Position start_pos) const;
  124. /// Clear the entire buffer
  125. void clear();
  126. // === File Operations ===
  127. /// Save the buffer to its associated file
  128. bool save();
  129. /// Save the buffer to a specific file
  130. bool save_as(const std::filesystem::path& path);
  131. /// Check if the buffer has been modified since last save
  132. [[nodiscard]] bool is_modified() const noexcept;
  133. /// Get the file path associated with this buffer (if any)
  134. [[nodiscard]] std::optional<std::filesystem::path> file_path() const noexcept;
  135. // === Buffer Properties ===
  136. /// Get the buffer name
  137. [[nodiscard]] const std::string& name() const noexcept;
  138. /// Set the buffer name
  139. void set_name(std::string name);
  140. /// Check if position is valid
  141. [[nodiscard]] bool is_valid_position(Position pos) const noexcept;
  142. /// Clamp a position to valid bounds
  143. [[nodiscard]] Position clamp_position(Position pos) const noexcept;
  144. // === Syntax Highlighting / Styling ===
  145. /// Set styling for a range of text
  146. void set_style(Range range, TextAttribute attr);
  147. /// Get all styled ranges for a specific line
  148. [[nodiscard]] const std::vector<StyledRange>& get_line_styles(size_t line) const;
  149. /// Clear all styling information
  150. void clear_styles();
  151. /// Clear styling for a specific line
  152. void clear_line_styles(size_t line);
  153. // === Events & Hooks ===
  154. using BufferEventCallback = std::function<void(const BufferEventData&)>;
  155. /// Register a callback for buffer events
  156. void on_buffer_event(BufferEventCallback callback);
  157. /// Get the language/file type of this buffer
  158. [[nodiscard]] const std::string& language() const noexcept { return language_; }
  159. /// Set the language/file type (triggers LanguageChanged event)
  160. void set_language(std::string lang);
  161. /// Auto-detect language from file path
  162. static std::string detect_language(const std::filesystem::path& path);
  163. // === Undo/Redo ===
  164. /// Undo the last change
  165. bool undo(Position& out_cursor);
  166. /// Redo the last undone change
  167. bool redo(Position& out_cursor);
  168. /// Check if undo is available
  169. [[nodiscard]] bool can_undo() const noexcept { return !undo_stack_.empty(); }
  170. /// Check if redo is available
  171. [[nodiscard]] bool can_redo() const noexcept { return !redo_stack_.empty(); }
  172. /// Save current state to undo stack (called automatically before changes)
  173. void save_undo_state(Position cursor);
  174. /// Clear redo stack (called when new change happens)
  175. void clear_redo_stack();
  176. private:
  177. std::string name_;
  178. std::vector<std::string> lines_;
  179. std::optional<std::filesystem::path> file_path_;
  180. bool modified_;
  181. std::string language_;
  182. // Styling information: one vector of styled ranges per line
  183. std::vector<std::vector<StyledRange>> line_styles_;
  184. // Event callbacks
  185. std::vector<BufferEventCallback> event_callbacks_;
  186. // Undo/Redo stacks
  187. std::vector<UndoState> undo_stack_;
  188. std::vector<UndoState> redo_stack_;
  189. static constexpr size_t MAX_UNDO_LEVELS = 100;
  190. bool in_undo_redo_ = false; // Prevent saving state during undo/redo
  191. /// Ensure the buffer has at least one line
  192. void ensure_min_lines();
  193. /// Mark the buffer as modified
  194. void mark_modified();
  195. /// Ensure styles vector matches lines vector size
  196. void ensure_styles_size();
  197. /// Emit a buffer event to all registered callbacks
  198. void emit_event(BufferEvent event, size_t line = 0);
  199. };
  200. } // namespace lumacs