Skip to content

Commit 83280ea

Browse files
bors[bot]Veykril
andauthored
Merge #7958
7958: Avoid double text edits when renaming mod declaration r=matklad a=Veykril Closes #7916 See microsoft/vscode-languageserver-node#752 for context Co-authored-by: Lukas Wirth <[email protected]>
2 parents 9dc1340 + 3af69b5 commit 83280ea

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

crates/hir/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ impl ModuleDef {
305305
ModuleDef::Module(it) => it.name(db),
306306
ModuleDef::Const(it) => it.name(db),
307307
ModuleDef::Static(it) => it.name(db),
308-
309308
ModuleDef::BuiltinType(it) => Some(it.name()),
310309
}
311310
}

crates/ide/src/references/rename.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub(crate) fn rename_with_semantics(
9494
}
9595
}
9696

97+
/// Called by the client when it is about to rename a file.
9798
pub(crate) fn will_rename_file(
9899
db: &RootDatabase,
99100
file_id: FileId,

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ impl Config {
395395
pub fn work_done_progress(&self) -> bool {
396396
try_or!(self.caps.window.as_ref()?.work_done_progress?, false)
397397
}
398+
pub fn will_rename(&self) -> bool {
399+
try_or!(self.caps.workspace.as_ref()?.file_operations.as_ref()?.will_rename?, false)
400+
}
398401
pub fn code_action_resolve(&self) -> bool {
399402
try_or!(
400403
self.caps

crates/rust-analyzer/src/handlers.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,18 @@ pub(crate) fn handle_rename(
799799
let _p = profile::span("handle_rename");
800800
let position = from_proto::file_position(&snap, params.text_document_position)?;
801801

802-
let change =
802+
let mut change =
803803
snap.analysis.rename(position, &*params.new_name)?.map_err(to_proto::rename_error)?;
804+
805+
// this is kind of a hack to prevent double edits from happening when moving files
806+
// When a module gets renamed by renaming the mod declaration this causes the file to move
807+
// which in turn will trigger a WillRenameFiles request to the server for which we reply with a
808+
// a second identical set of renames, the client will then apply both edits causing incorrect edits
809+
// with this we only emit source_file_edits in the WillRenameFiles response which will do the rename instead
810+
// See https://github.com/microsoft/vscode-languageserver-node/issues/752 for more info
811+
if !change.file_system_edits.is_empty() && snap.config.will_rename() {
812+
change.source_file_edits.clear();
813+
}
804814
let workspace_edit = to_proto::workspace_edit(&snap, change)?;
805815
Ok(Some(workspace_edit))
806816
}

0 commit comments

Comments
 (0)