Extract relevant information into hashmaps

This commit is contained in:
Johann Hemmann 2024-01-04 12:45:44 +01:00
parent c3a00b5468
commit 084acc287a

View File

@ -365,7 +365,10 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Opt
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::HashMap;
use expect_test::{expect, Expect}; use expect_test::{expect, Expect};
use ide_db::source_change::FileSystemEdit;
use stdx::trim_indent; use stdx::trim_indent;
use test_utils::assert_eq_text; use test_utils::assert_eq_text;
use text_edit::TextEdit; use text_edit::TextEdit;
@ -418,7 +421,37 @@ mod tests {
let (analysis, position) = fixture::position(ra_fixture); let (analysis, position) = fixture::position(ra_fixture);
let source_change = let source_change =
analysis.rename(position, new_name).unwrap().expect("Expect returned a RenameError"); analysis.rename(position, new_name).unwrap().expect("Expect returned a RenameError");
expect.assert_debug_eq(&source_change)
// file_id 1:
// source_file_edits:
// - Indel { insert: "foo2", delete: 4..7 }
//
// file_id 2:
// file_system_edits:
// MoveFile AnchoredPathBuf { anchor: FileId(2), path: "foo2.rs", }
let mut source_file_edits = HashMap::new();
for (id, (text_edit, _)) in source_change.source_file_edits {
let indels = text_edit.into_iter().collect::<Vec<_>>();
source_file_edits.insert(id, indels);
}
let mut file_system_edits = HashMap::new();
for a in source_change.file_system_edits {
let id = match &a {
FileSystemEdit::CreateFile { dst, initial_contents } => unreachable!(),
FileSystemEdit::MoveFile { src, dst } => src,
FileSystemEdit::MoveDir { src, src_id, dst } => src_id,
}
.clone();
file_system_edits.insert(id, a);
}
let b = format!(
"source_file_edits: {:#?}\nfile_system_edits: {:#?}",
source_file_edits, file_system_edits
);
expect.assert_eq(&b)
} }
fn check_expect_will_rename_file(new_name: &str, ra_fixture: &str, expect: Expect) { fn check_expect_will_rename_file(new_name: &str, ra_fixture: &str, expect: Expect) {
@ -916,24 +949,20 @@ mod foo$0;
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
1, 1,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "foo2", insert: "foo2",
delete: 4..7, delete: 4..7,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
}, 2,
file_system_edits: [ ): MoveFile {
MoveFile {
src: FileId( src: FileId(
2, 2,
), ),
@ -944,10 +973,7 @@ mod foo$0;
path: "foo2.rs", path: "foo2.rs",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }
@ -968,37 +994,28 @@ pub struct FooContent;
use crate::foo$0::FooContent; use crate::foo$0::FooContent;
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "quux", insert: "quux",
delete: 8..11, delete: 8..11,
}, },
], ],
},
None,
),
FileId( FileId(
2, 2,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "quux", insert: "quux",
delete: 11..14, delete: 11..14,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
}, 1,
file_system_edits: [ ): MoveFile {
MoveFile {
src: FileId( src: FileId(
1, 1,
), ),
@ -1009,10 +1026,7 @@ use crate::foo$0::FooContent;
path: "quux.rs", path: "quux.rs",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }
@ -1027,24 +1041,20 @@ mod fo$0o;
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "foo2", insert: "foo2",
delete: 4..7, delete: 4..7,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
}, 1,
file_system_edits: [ ): MoveDir {
MoveDir {
src: AnchoredPathBuf { src: AnchoredPathBuf {
anchor: FileId( anchor: FileId(
1, 1,
@ -1061,10 +1071,7 @@ mod fo$0o;
path: "../foo2", path: "../foo2",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }
@ -1080,24 +1087,20 @@ mod outer { mod fo$0o; }
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "bar", insert: "bar",
delete: 16..19, delete: 16..19,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
}, 1,
file_system_edits: [ ): MoveFile {
MoveFile {
src: FileId( src: FileId(
1, 1,
), ),
@ -1108,10 +1111,7 @@ mod outer { mod fo$0o; }
path: "bar.rs", path: "bar.rs",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }
@ -1156,37 +1156,28 @@ pub mod foo$0;
// pub fn fun() {} // pub fn fun() {}
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "foo2", insert: "foo2",
delete: 27..30, delete: 27..30,
}, },
], ],
},
None,
),
FileId( FileId(
1, 1,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "foo2", insert: "foo2",
delete: 8..11, delete: 8..11,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
}, 2,
file_system_edits: [ ): MoveFile {
MoveFile {
src: FileId( src: FileId(
2, 2,
), ),
@ -1197,10 +1188,7 @@ pub mod foo$0;
path: "foo2.rs", path: "foo2.rs",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }
@ -1229,35 +1217,20 @@ mod quux;
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "foo2", insert: "foo2",
delete: 4..7, delete: 4..7,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
},
file_system_edits: [
MoveFile {
src: FileId(
1, 1,
), ): MoveDir {
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo2.rs",
},
},
MoveDir {
src: AnchoredPathBuf { src: AnchoredPathBuf {
anchor: FileId( anchor: FileId(
1, 1,
@ -1274,10 +1247,7 @@ mod quux;
path: "foo2", path: "foo2",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
) )
} }
#[test] #[test]
@ -1370,13 +1340,10 @@ pub mod bar;
pub fn baz() {} pub fn baz() {}
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "r#fn", insert: "r#fn",
delete: 4..7, delete: 4..7,
@ -1386,23 +1353,11 @@ pub fn baz() {}
delete: 22..25, delete: 22..25,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
},
file_system_edits: [
MoveFile {
src: FileId(
1, 1,
), ): MoveDir {
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "fn.rs",
},
},
MoveDir {
src: AnchoredPathBuf { src: AnchoredPathBuf {
anchor: FileId( anchor: FileId(
1, 1,
@ -1419,10 +1374,7 @@ pub fn baz() {}
path: "fn", path: "fn",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }
@ -1443,13 +1395,10 @@ pub mod bar;
pub fn baz() {} pub fn baz() {}
"#, "#,
expect![[r#" expect![[r#"
SourceChange {
source_file_edits: { source_file_edits: {
FileId( FileId(
0, 0,
): ( ): [
TextEdit {
indels: [
Indel { Indel {
insert: "foo", insert: "foo",
delete: 4..8, delete: 4..8,
@ -1459,23 +1408,11 @@ pub fn baz() {}
delete: 23..27, delete: 23..27,
}, },
], ],
}, }
None, file_system_edits: {
), FileId(
},
file_system_edits: [
MoveFile {
src: FileId(
1, 1,
), ): MoveDir {
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo.rs",
},
},
MoveDir {
src: AnchoredPathBuf { src: AnchoredPathBuf {
anchor: FileId( anchor: FileId(
1, 1,
@ -1492,10 +1429,7 @@ pub fn baz() {}
path: "foo", path: "foo",
}, },
}, },
], }"#]],
is_snippet: false,
}
"#]],
); );
} }