ass_editor/core/document/
position_api.rs1use super::EditorDocument;
8use crate::core::errors::Result;
9use crate::core::position::{Position, Range};
10
11pub struct DocumentPosition<'a> {
13 document: &'a mut EditorDocument,
14 position: Position,
15}
16
17impl<'a> DocumentPosition<'a> {
18 pub fn insert_text(self, text: &str) -> Result<()> {
20 self.document.insert(self.position, text)
21 }
22
23 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 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 pub fn at(&mut self, pos: Position) -> DocumentPosition<'_> {
41 DocumentPosition {
42 document: self,
43 position: pos,
44 }
45 }
46}