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 target_type_name(impl_def: &ast::Impl) -> Option<String> {
#[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 @@ fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
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,38 +949,31 @@ fn test_rename_mod() {
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 1,
1, ): [
): ( Indel {
TextEdit { insert: "foo2",
indels: [ delete: 4..7,
Indel {
insert: "foo2",
delete: 4..7,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
2,
),
dst: AnchoredPathBuf {
anchor: FileId(
2,
),
path: "foo2.rs",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
2,
): MoveFile {
src: FileId(
2,
),
dst: AnchoredPathBuf {
anchor: FileId(
2,
),
path: "foo2.rs",
},
},
}"#]],
); );
} }
@ -968,51 +994,39 @@ fn main() {}
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, ): [
): ( Indel {
TextEdit { insert: "quux",
indels: [ delete: 8..11,
Indel { },
insert: "quux", ],
delete: 8..11, FileId(
}, 2,
], ): [
}, Indel {
None, insert: "quux",
), delete: 11..14,
FileId(
2,
): (
TextEdit {
indels: [
Indel {
insert: "quux",
delete: 11..14,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "quux.rs",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
1,
): MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "quux.rs",
},
},
}"#]],
); );
} }
@ -1027,44 +1041,37 @@ fn test_rename_mod_in_dir() {
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 0,
0, ): [
): ( Indel {
TextEdit { insert: "foo2",
indels: [ delete: 4..7,
Indel {
insert: "foo2",
delete: 4..7,
},
],
},
None,
),
},
file_system_edits: [
MoveDir {
src: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "../foo",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "../foo2",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
1,
): MoveDir {
src: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "../foo",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "../foo2",
},
},
}"#]],
); );
} }
@ -1080,38 +1087,31 @@ fn test_rename_unusually_nested_mod() {
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 0,
0, ): [
): ( Indel {
TextEdit { insert: "bar",
indels: [ delete: 16..19,
Indel {
insert: "bar",
delete: 16..19,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "bar.rs",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
1,
): MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "bar.rs",
},
},
}"#]],
); );
} }
@ -1156,51 +1156,39 @@ fn f() {
// pub fn fun() {} // pub fn fun() {}
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 0,
0, ): [
): ( Indel {
TextEdit { insert: "foo2",
indels: [ delete: 27..30,
Indel { },
insert: "foo2", ],
delete: 27..30, FileId(
}, 1,
], ): [
}, Indel {
None, insert: "foo2",
), delete: 8..11,
FileId(
1,
): (
TextEdit {
indels: [
Indel {
insert: "foo2",
delete: 8..11,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
2,
),
dst: AnchoredPathBuf {
anchor: FileId(
2,
),
path: "foo2.rs",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
2,
): MoveFile {
src: FileId(
2,
),
dst: AnchoredPathBuf {
anchor: FileId(
2,
),
path: "foo2.rs",
},
},
}"#]],
); );
} }
@ -1229,55 +1217,37 @@ fn test_rename_mod_recursive() {
// empty // empty
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 0,
0, ): [
): ( Indel {
TextEdit { insert: "foo2",
indels: [ delete: 4..7,
Indel {
insert: "foo2",
delete: 4..7,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo2.rs",
},
},
MoveDir {
src: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo2",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
1,
): MoveDir {
src: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo2",
},
},
}"#]],
) )
} }
#[test] #[test]
@ -1370,59 +1340,41 @@ fn test_rename_mod_to_raw_ident() {
pub fn baz() {} pub fn baz() {}
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 0,
0, ): [
): ( Indel {
TextEdit { insert: "r#fn",
indels: [ delete: 4..7,
Indel {
insert: "r#fn",
delete: 4..7,
},
Indel {
insert: "r#fn",
delete: 22..25,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "fn.rs",
},
}, },
MoveDir { Indel {
src: AnchoredPathBuf { insert: "r#fn",
anchor: FileId( delete: 22..25,
1,
),
path: "foo",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "fn",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
1,
): MoveDir {
src: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "fn",
},
},
}"#]],
); );
} }
@ -1443,59 +1395,41 @@ fn test_rename_mod_from_raw_ident() {
pub fn baz() {} pub fn baz() {}
"#, "#,
expect![[r#" expect![[r#"
SourceChange { source_file_edits: {
source_file_edits: { FileId(
FileId( 0,
0, ): [
): ( Indel {
TextEdit { insert: "foo",
indels: [ delete: 4..8,
Indel {
insert: "foo",
delete: 4..8,
},
Indel {
insert: "foo",
delete: 23..27,
},
],
},
None,
),
},
file_system_edits: [
MoveFile {
src: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo.rs",
},
}, },
MoveDir { Indel {
src: AnchoredPathBuf { insert: "foo",
anchor: FileId( delete: 23..27,
1,
),
path: "fn",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo",
},
}, },
], ],
is_snippet: false,
} }
"#]], file_system_edits: {
FileId(
1,
): MoveDir {
src: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "fn",
},
src_id: FileId(
1,
),
dst: AnchoredPathBuf {
anchor: FileId(
1,
),
path: "foo",
},
},
}"#]],
); );
} }