Skip to content

Commit 9b0daf2

Browse files
committed
fix: don't include r# prefix in filesystem changes
1 parent 92fdfb5 commit 9b0daf2

File tree

2 files changed

+150
-4
lines changed

2 files changed

+150
-4
lines changed

crates/ide-db/src/rename.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ fn rename_mod(
190190

191191
let InFile { file_id, value: def_source } = module.definition_source(sema.db);
192192
if let ModuleSource::SourceFile(..) = def_source {
193+
let new_name = new_name.trim_start_matches("r#");
193194
let anchor = file_id.original_file(sema.db);
194195

195196
let is_mod_rs = module.is_mod_rs(sema.db);
@@ -207,9 +208,13 @@ fn rename_mod(
207208
// - Module has submodules defined in separate files
208209
let dir_paths = match (is_mod_rs, has_detached_child, module.name(sema.db)) {
209210
// Go up one level since the anchor is inside the dir we're trying to rename
210-
(true, _, Some(mod_name)) => Some((format!("../{mod_name}"), format!("../{new_name}"))),
211+
(true, _, Some(mod_name)) => {
212+
Some((format!("../{}", mod_name.unescaped()), format!("../{new_name}")))
213+
}
211214
// The anchor is on the same level as target dir
212-
(false, true, Some(mod_name)) => Some((mod_name.to_string(), new_name.to_string())),
215+
(false, true, Some(mod_name)) => {
216+
Some((mod_name.unescaped().to_string(), new_name.to_string()))
217+
}
213218
_ => None,
214219
};
215220

crates/ide/src/rename.rs

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ide_db::{
1313
};
1414
use itertools::Itertools;
1515
use stdx::{always, never};
16-
use syntax::{ast, AstNode, SyntaxNode, TextRange, TextSize};
16+
use syntax::{ast, utils::is_raw_identifier, AstNode, SmolStr, SyntaxNode, TextRange, TextSize};
1717

1818
use text_edit::TextEdit;
1919

@@ -122,7 +122,11 @@ pub(crate) fn will_rename_file(
122122
let sema = Semantics::new(db);
123123
let module = sema.to_module_def(file_id)?;
124124
let def = Definition::Module(module);
125-
let mut change = def.rename(&sema, new_name_stem).ok()?;
125+
let mut change = if is_raw_identifier(new_name_stem) {
126+
def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem])).ok()?
127+
} else {
128+
def.rename(&sema, new_name_stem).ok()?
129+
};
126130
change.file_system_edits.clear();
127131
Some(change)
128132
}
@@ -1286,6 +1290,143 @@ mod bar$0;
12861290
)
12871291
}
12881292

1293+
#[test]
1294+
fn test_rename_mod_to_raw_ident() {
1295+
check_expect(
1296+
"r#fn",
1297+
r#"
1298+
//- /lib.rs
1299+
mod foo$0;
1300+
1301+
fn main() { foo::bar::baz(); }
1302+
1303+
//- /foo.rs
1304+
pub mod bar;
1305+
1306+
//- /foo/bar.rs
1307+
pub fn baz() {}
1308+
"#,
1309+
expect![[r#"
1310+
SourceChange {
1311+
source_file_edits: {
1312+
FileId(
1313+
0,
1314+
): TextEdit {
1315+
indels: [
1316+
Indel {
1317+
insert: "r#fn",
1318+
delete: 4..7,
1319+
},
1320+
Indel {
1321+
insert: "r#fn",
1322+
delete: 22..25,
1323+
},
1324+
],
1325+
},
1326+
},
1327+
file_system_edits: [
1328+
MoveFile {
1329+
src: FileId(
1330+
1,
1331+
),
1332+
dst: AnchoredPathBuf {
1333+
anchor: FileId(
1334+
1,
1335+
),
1336+
path: "fn.rs",
1337+
},
1338+
},
1339+
MoveDir {
1340+
src: AnchoredPathBuf {
1341+
anchor: FileId(
1342+
1,
1343+
),
1344+
path: "foo",
1345+
},
1346+
src_id: FileId(
1347+
1,
1348+
),
1349+
dst: AnchoredPathBuf {
1350+
anchor: FileId(
1351+
1,
1352+
),
1353+
path: "fn",
1354+
},
1355+
},
1356+
],
1357+
is_snippet: false,
1358+
}
1359+
"#]],
1360+
);
1361+
}
1362+
1363+
#[test]
1364+
fn test_rename_mod_from_raw_ident() {
1365+
// FIXME: `r#fn` in path expression is not renamed.
1366+
check_expect(
1367+
"foo",
1368+
r#"
1369+
//- /lib.rs
1370+
mod r#fn$0;
1371+
1372+
fn main() { r#fn::bar::baz(); }
1373+
1374+
//- /fn.rs
1375+
pub mod bar;
1376+
1377+
//- /fn/bar.rs
1378+
pub fn baz() {}
1379+
"#,
1380+
expect![[r#"
1381+
SourceChange {
1382+
source_file_edits: {
1383+
FileId(
1384+
0,
1385+
): TextEdit {
1386+
indels: [
1387+
Indel {
1388+
insert: "foo",
1389+
delete: 4..8,
1390+
},
1391+
],
1392+
},
1393+
},
1394+
file_system_edits: [
1395+
MoveFile {
1396+
src: FileId(
1397+
1,
1398+
),
1399+
dst: AnchoredPathBuf {
1400+
anchor: FileId(
1401+
1,
1402+
),
1403+
path: "foo.rs",
1404+
},
1405+
},
1406+
MoveDir {
1407+
src: AnchoredPathBuf {
1408+
anchor: FileId(
1409+
1,
1410+
),
1411+
path: "fn",
1412+
},
1413+
src_id: FileId(
1414+
1,
1415+
),
1416+
dst: AnchoredPathBuf {
1417+
anchor: FileId(
1418+
1,
1419+
),
1420+
path: "foo",
1421+
},
1422+
},
1423+
],
1424+
is_snippet: false,
1425+
}
1426+
"#]],
1427+
);
1428+
}
1429+
12891430
#[test]
12901431
fn test_enum_variant_from_module_1() {
12911432
cov_mark::check!(rename_non_local);

0 commit comments

Comments
 (0)