Skip to content

Commit 0647b64

Browse files
committed
ensure renames happen after edit
1 parent 7059ae2 commit 0647b64

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Conversion of rust-analyzer specific types to lsp_types equivalents.
22
use std::{
33
iter::once,
4-
path,
4+
mem, path,
55
sync::atomic::{AtomicU32, Ordering},
66
};
77

@@ -1123,13 +1123,20 @@ pub(crate) fn snippet_text_document_ops(
11231123

11241124
pub(crate) fn snippet_workspace_edit(
11251125
snap: &GlobalStateSnapshot,
1126-
source_change: SourceChange,
1126+
mut source_change: SourceChange,
11271127
) -> Cancellable<lsp_ext::SnippetWorkspaceEdit> {
11281128
let mut document_changes: Vec<lsp_ext::SnippetDocumentChangeOperation> = Vec::new();
11291129

1130-
for op in source_change.file_system_edits {
1131-
let ops = snippet_text_document_ops(snap, op)?;
1132-
document_changes.extend_from_slice(&ops);
1130+
for op in &mut source_change.file_system_edits {
1131+
if let FileSystemEdit::CreateFile { dst, initial_contents } = op {
1132+
// replace with a placeholder to avoid cloneing the edit
1133+
let op = FileSystemEdit::CreateFile {
1134+
dst: dst.clone(),
1135+
initial_contents: mem::take(initial_contents),
1136+
};
1137+
let ops = snippet_text_document_ops(snap, op)?;
1138+
document_changes.extend_from_slice(&ops);
1139+
}
11331140
}
11341141
for (file_id, (edit, snippet_edit)) in source_change.source_file_edits {
11351142
let edit = snippet_text_document_edit(
@@ -1141,6 +1148,12 @@ pub(crate) fn snippet_workspace_edit(
11411148
)?;
11421149
document_changes.push(lsp_ext::SnippetDocumentChangeOperation::Edit(edit));
11431150
}
1151+
for op in source_change.file_system_edits {
1152+
if !matches!(op, FileSystemEdit::CreateFile { .. }) {
1153+
let ops = snippet_text_document_ops(snap, op)?;
1154+
document_changes.extend_from_slice(&ops);
1155+
}
1156+
}
11441157
let mut workspace_edit = lsp_ext::SnippetWorkspaceEdit {
11451158
changes: None,
11461159
document_changes: Some(document_changes),

crates/rust-analyzer/tests/slow-tests/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,11 @@ fn main() {}
984984
//- /src/old_file.rs
985985
986986
//- /src/old_folder/mod.rs
987+
mod nested;
988+
989+
//- /src/old_folder/nested.rs
990+
struct foo;
991+
use crate::old_folder::nested::foo as bar;
987992
988993
//- /src/from_mod/mod.rs
989994
@@ -1080,6 +1085,27 @@ fn main() {}
10801085
"newText": "new_folder"
10811086
}
10821087
]
1088+
},
1089+
{
1090+
"textDocument": {
1091+
"uri": format!("file://{}", tmp_dir_path.join("src").join("old_folder").join("nested.rs").to_str().unwrap().to_string().replace("C:\\", "/c:/").replace('\\', "/")),
1092+
"version": null
1093+
},
1094+
"edits": [
1095+
{
1096+
"range": {
1097+
"start": {
1098+
"line": 1,
1099+
"character": 11
1100+
},
1101+
"end": {
1102+
"line": 1,
1103+
"character": 21
1104+
}
1105+
},
1106+
"newText": "new_folder"
1107+
}
1108+
]
10831109
}
10841110
]
10851111
}),

0 commit comments

Comments
 (0)