Skip to main content

ass_editor/core/document/
position_api.rs

1//! Fluent position-anchored editing API
2//!
3//! Defines `DocumentPosition`, a short-lived handle returned by
4//! `EditorDocument::at` that offers ergonomic insert/delete/replace
5//! operations relative to a fixed position.
6
7use super::EditorDocument;
8use crate::core::errors::Result;
9use crate::core::position::{Position, Range};
10
11/// Fluent position API for editor operations
12pub struct DocumentPosition<'a> {
13    document: &'a mut EditorDocument,
14    position: Position,
15}
16
17impl<'a> DocumentPosition<'a> {
18    /// Insert text at this position
19    pub fn insert_text(self, text: &str) -> Result<()> {
20        self.document.insert(self.position, text)
21    }
22
23    /// Delete text range starting from this position
24    pub fn delete_range(self, len: usize) -> Result<()> {
25        let end_pos = Position::new(self.position.offset + len);
26        let range = Range::new(self.position, end_pos);
27        self.document.delete(range)
28    }
29
30    /// Replace text at this position
31    pub fn replace_text(self, len: usize, new_text: &str) -> Result<()> {
32        let end_pos = Position::new(self.position.offset + len);
33        let range = Range::new(self.position, end_pos);
34        self.document.replace(range, new_text)
35    }
36}
37
38impl EditorDocument {
39    /// Get fluent API for position-based operations
40    pub fn at(&mut self, pos: Position) -> DocumentPosition<'_> {
41        DocumentPosition {
42            document: self,
43            position: pos,
44        }
45    }
46}