perl_parser/incremental/
edit.rs1use crate::incremental::LineIndex;
2
3#[derive(Clone, Debug)]
5pub struct Edit {
6 pub start_byte: usize,
7 pub old_end_byte: usize,
8 pub new_end_byte: usize,
9 pub new_text: String,
10}
11
12impl Edit {
13 pub(crate) fn touched_bytes(&self) -> usize {
15 let replaced_len = self.old_end_byte.saturating_sub(self.start_byte);
16 replaced_len.max(self.new_text.len())
17 }
18}
19
20#[cfg(feature = "lsp-compat")]
21impl Edit {
22 pub fn from_lsp_change(
24 change: &lsp_types::TextDocumentContentChangeEvent,
25 line_index: &LineIndex,
26 old_text: &str,
27 ) -> Option<Self> {
28 if let Some(range) = change.range {
29 let start_byte = line_index
30 .position_to_byte(range.start.line as usize, range.start.character as usize)?;
31 let old_end_byte = line_index
32 .position_to_byte(range.end.line as usize, range.end.character as usize)?;
33 let new_end_byte = start_byte + change.text.len();
34
35 Some(Edit { start_byte, old_end_byte, new_end_byte, new_text: change.text.clone() })
36 } else {
37 Some(Edit {
38 start_byte: 0,
39 old_end_byte: old_text.len(),
40 new_end_byte: change.text.len(),
41 new_text: change.text.clone(),
42 })
43 }
44 }
45}