Skip to content

Commit

Permalink
fix: reword rename to use one text document edit (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribru17 authored Dec 2, 2024
1 parent 291c482 commit 3d281f7
Showing 1 changed file with 24 additions and 45 deletions.
69 changes: 24 additions & 45 deletions src/handlers/rename.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::cmp::Ordering;

use log::warn;
use regex::Regex;
use tower_lsp::{
Expand Down Expand Up @@ -49,7 +47,7 @@ pub async fn rename(backend: &Backend, params: RenameParams) -> Result<Option<Wo
"New name is not a valid identifier",
));
}
let mut text_document_edits: Vec<TextDocumentEdit> = vec![];
let mut text_document_edits = vec![];
let provider = TextProviderRope(&rope);
get_references(
&tree.root_node(),
Expand All @@ -63,35 +61,21 @@ pub async fn rename(backend: &Backend, params: RenameParams) -> Result<Option<Wo
.for_each(|mut elem| {
// Don't include the preceding `@`
elem.range.start.character += 1;
text_document_edits.push(TextDocumentEdit {
text_document_edits.push(OneOf::Left(TextEdit {
range: elem.range,
new_text: new_name.to_owned(),
}));
});

Ok(Some(WorkspaceEdit {
document_changes: Some(DocumentChanges::Edits(vec![TextDocumentEdit {
text_document: OptionalVersionedTextDocumentIdentifier {
uri: elem.uri,
uri: uri.clone(),
// TODO: Support versioned edits
version: None,
},
edits: vec![OneOf::Left(TextEdit {
range: elem.range,
new_text: new_name.to_owned(),
})],
});
});
// Apply edits from end to start, to prevent offset inaccuracies
text_document_edits.sort_by(|a, b| {
if let OneOf::Left(a) = &a.edits[0] {
if let OneOf::Left(b) = &b.edits[0] {
let range_a = a.range;
let range_b = b.range;
range_b.start.cmp(&range_a.start)
} else {
Ordering::Equal
}
} else {
Ordering::Equal
}
});

Ok(Some(WorkspaceEdit {
document_changes: Some(DocumentChanges::Edits(text_document_edits)),
edits: text_document_edits,
}])),
changes: None,
change_annotations: None,
}))
Expand All @@ -118,20 +102,20 @@ mod test {
&SIMPLE_FILE,
Position { line: 1, character: 12, },
&[
TestEdit::new("superlongnamehere", (1, 21), (1, 29)),
TestEdit::new("superlongnamehere", (1, 11), (1, 19)),
TestEdit::new("superlongnamehere", (0, 15), (0, 23)),
TestEdit::new("superlongnamehere", (1, 11), (1, 19)),
TestEdit::new("superlongnamehere", (1, 21), (1, 29)),
],
"superlongnamehere",
)]
#[case(
&COMPLEX_FILE,
Position { line: 8, character: 24 },
&[
TestEdit::new("invariant", (18, 17), (18, 34)),
TestEdit::new("invariant", (12, 13), (12, 30)),
TestEdit::new("invariant", (9, 23), (9, 40)),
TestEdit::new("invariant", (8, 25), (8, 42)),
TestEdit::new("invariant", (9, 23), (9, 40)),
TestEdit::new("invariant", (12, 13), (12, 30)),
TestEdit::new("invariant", (18, 17), (18, 34)),
],
"invariant"
)]
Expand Down Expand Up @@ -181,18 +165,13 @@ mod test {
None
} else {
Some(WorkspaceEdit {
document_changes: Some(DocumentChanges::Edits(
edits
.iter()
.map(|e| TextDocumentEdit {
text_document: OptionalVersionedTextDocumentIdentifier {
uri: TEST_URI.clone(),
version: None,
},
edits: vec![OneOf::Left(e.into())],
})
.collect(),
)),
document_changes: Some(DocumentChanges::Edits(vec![TextDocumentEdit {
text_document: OptionalVersionedTextDocumentIdentifier {
uri: TEST_URI.clone(),
version: None,
},
edits: edits.iter().map(|e| OneOf::Left(e.into())).collect(),
}])),
..Default::default()
})
};
Expand Down

0 comments on commit 3d281f7

Please sign in to comment.