Skip to content

Commit

Permalink
changed InputEdit old_end_position
Browse files Browse the repository at this point in the history
  • Loading branch information
uros-5 committed Mar 20, 2024
1 parent f513de5 commit e930f20
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
68 changes: 48 additions & 20 deletions jinja-lsp-queries/src/to_input_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,65 @@ use crate::tree_builder::JinjaVariable;
// use crate::lsp_files::JinjaVariable;

pub trait ToInputEdit {
fn to_point(&self) -> (Point, Point);
fn to_byte(&self, rope: &Rope) -> (usize, usize);
fn to_input_edit(&self, rope: &Rope) -> InputEdit;
fn to_point(&self, position: Position) -> Point;
fn to_byte(&self, position: Position) -> usize;
fn to_position(&self, offset: usize) -> Position;
fn to_input_edit(&self, range: Range, text: &str) -> InputEdit;
}

impl ToInputEdit for Range {
fn to_point(&self) -> (Point, Point) {
let start = Point::new(self.start.line as usize, self.start.character as usize);
let end = Point::new(self.end.line as usize, self.end.character as usize);
(start, end)
impl ToInputEdit for Rope {
fn to_point(&self, position: Position) -> Point {
Point::new(position.line as usize, position.character as usize)
}

fn to_byte(&self, rope: &Rope) -> (usize, usize) {
let start_line = rope.line_to_byte(self.start.line as usize);
let start_offset = start_line + self.start.character as usize;

let end_line = rope.line_to_byte(self.end.line as usize);
let end_offset = end_line + self.end.character as usize;
fn to_byte(&self, position: Position) -> usize {
let start_line = self.line_to_byte(position.line as usize);
start_line + position.character as usize
}

(start_offset, end_offset)
fn to_position(&self, mut offset: usize) -> Position {
offset = offset.min(self.len_bytes());
let mut low = 0usize;
let mut high = self.len_lines();
if high == 0 {
return Position {
line: 0,
character: offset as u32,
};
}
while low < high {
let mid = low + (high - low) / 2;
if self.line_to_byte(mid) > offset {
high = mid;
} else {
low = mid + 1;
}
}
let line = low - 1;
let character = offset - self.line_to_byte(line);
Position::new(line as u32, character as u32)
}

fn to_input_edit(&self, rope: &Rope) -> InputEdit {
let (start_position, new_end_position) = self.to_point();
let (start_byte, new_end_byte) = self.to_byte(rope);
fn to_input_edit(&self, range: Range, text: &str) -> InputEdit {
let start = range.start;
let end = range.end;

let start_byte = self.to_byte(start);
let start_position = self.to_point(start);

let new_end_byte = start_byte + text.len();
let new_end_position = self.to_position(new_end_byte);
let new_end_position = self.to_point(new_end_position);

let old_end_byte = self.to_byte(end);
let old_end_position = self.to_point(end);

InputEdit {
start_byte,
old_end_byte: start_byte,
old_end_byte,
new_end_byte,
start_position,
old_end_position: start_position,
old_end_position,
new_end_position,
}
}
Expand Down
12 changes: 8 additions & 4 deletions jinja-lsp-queries/src/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ impl JinjaKeyword {
if from.is_empty() {
*from = from2;
Some(())
} else if name.is_empty() {
*name = String::from(identifier);
Some(())
} else {
}
//else if name.is_empty() {
else {
None
}
// *name = String::from(identifier);
// Some(())
// } else {
// None
// }
}
JinjaKeyword::With { name } => {
if name.is_empty() {
Expand Down
15 changes: 10 additions & 5 deletions jinja-lsp/src/lsp_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,18 @@ impl LspFiles {
let lang_type = self.config.file_ext(&Path::new(&uri));
let mut changes = vec![];
for change in params.content_changes {
let range = &change.range?;
let input_edit = range.to_input_edit(rope);
let range = change.range?;
let input_edit = rope.to_input_edit(range, &change.text);
if change.text.is_empty() {
let (start, end) = range.to_byte(rope);
rope.remove(start..end);
let start = rope.to_byte(range.start);
let end = rope.to_byte(range.end);
if start <= end {
rope.remove(start..end);
} else {
rope.remove(end..start);
}
} else {
let (start, _) = range.to_byte(rope);
let start = rope.to_byte(range.start);
rope.insert(start, &change.text);
}
let mut w = FileWriter::default();
Expand Down

0 comments on commit e930f20

Please sign in to comment.