mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
fix(ide-db): correct single-file module rename
This commit is contained in:
parent
3fe137a5b5
commit
d98c04aac1
@ -39,6 +39,11 @@ impl Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_inline(self, db: &dyn HirDatabase) -> bool {
|
||||||
|
let def_map = self.id.def_map(db.upcast());
|
||||||
|
def_map[self.id.local_id].origin.is_inline()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
||||||
/// `None` for the crate root.
|
/// `None` for the crate root.
|
||||||
pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> {
|
pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> {
|
||||||
|
@ -180,15 +180,33 @@ fn rename_mod(
|
|||||||
let InFile { file_id, value: def_source } = module.definition_source(sema.db);
|
let InFile { file_id, value: def_source } = module.definition_source(sema.db);
|
||||||
if let ModuleSource::SourceFile(..) = def_source {
|
if let ModuleSource::SourceFile(..) = def_source {
|
||||||
let anchor = file_id.original_file(sema.db);
|
let anchor = file_id.original_file(sema.db);
|
||||||
// not mod.rs and doesn't has children, rename file only
|
|
||||||
if !module.is_mod_rs(sema.db) && module.children(sema.db).next().is_none() {
|
let is_mod_rs = module.is_mod_rs(sema.db);
|
||||||
|
let has_detached_child = module.children(sema.db).any(|child| !child.is_inline(sema.db));
|
||||||
|
|
||||||
|
// Module exists in a named file
|
||||||
|
if !is_mod_rs {
|
||||||
let path = format!("{}.rs", new_name);
|
let path = format!("{}.rs", new_name);
|
||||||
let dst = AnchoredPathBuf { anchor, path };
|
let dst = AnchoredPathBuf { anchor, path };
|
||||||
source_change.push_file_system_edit(FileSystemEdit::MoveFile { src: anchor, dst })
|
source_change.push_file_system_edit(FileSystemEdit::MoveFile { src: anchor, dst })
|
||||||
} else if let Some(mod_name) = module.name(sema.db) {
|
}
|
||||||
// is mod.rs or has children, rename dir
|
|
||||||
let src = AnchoredPathBuf { anchor, path: mod_name.to_string() };
|
// Rename the dir if:
|
||||||
let dst = AnchoredPathBuf { anchor, path: new_name.to_string() };
|
// - Module source is in mod.rs
|
||||||
|
// - Module has submodules defined in separate files
|
||||||
|
let dir_paths = match (is_mod_rs, has_detached_child, module.name(sema.db)) {
|
||||||
|
// Go up one level since the anchor is inside the dir we're trying to rename
|
||||||
|
(true, _, Some(mod_name)) => {
|
||||||
|
Some((format!("../{}", mod_name), format!("../{}", new_name)))
|
||||||
|
}
|
||||||
|
// The anchor is on the same level as target dir
|
||||||
|
(false, true, Some(mod_name)) => Some((mod_name.to_string(), new_name.to_string())),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some((src, dst)) = dir_paths {
|
||||||
|
let src = AnchoredPathBuf { anchor, path: src };
|
||||||
|
let dst = AnchoredPathBuf { anchor, path: dst };
|
||||||
source_change.push_file_system_edit(FileSystemEdit::MoveDir {
|
source_change.push_file_system_edit(FileSystemEdit::MoveDir {
|
||||||
src,
|
src,
|
||||||
src_id: anchor,
|
src_id: anchor,
|
||||||
|
@ -973,7 +973,7 @@ mod fo$0o;
|
|||||||
anchor: FileId(
|
anchor: FileId(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
path: "foo",
|
path: "../foo",
|
||||||
},
|
},
|
||||||
src_id: FileId(
|
src_id: FileId(
|
||||||
1,
|
1,
|
||||||
@ -982,7 +982,7 @@ mod fo$0o;
|
|||||||
anchor: FileId(
|
anchor: FileId(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
path: "foo2",
|
path: "../foo2",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -1158,6 +1158,17 @@ mod quux;
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
file_system_edits: [
|
file_system_edits: [
|
||||||
|
MoveFile {
|
||||||
|
src: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
dst: AnchoredPathBuf {
|
||||||
|
anchor: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
path: "foo2.rs",
|
||||||
|
},
|
||||||
|
},
|
||||||
MoveDir {
|
MoveDir {
|
||||||
src: AnchoredPathBuf {
|
src: AnchoredPathBuf {
|
||||||
anchor: FileId(
|
anchor: FileId(
|
||||||
|
Loading…
Reference in New Issue
Block a user