Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/tests.rs')
| -rw-r--r-- | crates/ide-assists/src/tests.rs | 135 |
1 files changed, 86 insertions, 49 deletions
diff --git a/crates/ide-assists/src/tests.rs b/crates/ide-assists/src/tests.rs index 7d7012c462..0593e6930d 100644 --- a/crates/ide-assists/src/tests.rs +++ b/crates/ide-assists/src/tests.rs @@ -1,12 +1,12 @@ mod generated; use expect_test::expect; -use hir::{FileRange, Semantics}; +use hir::Semantics; use ide_db::{ - base_db::{SourceDatabase, SourceRootDatabase}, + EditionedFileId, FileRange, RootDatabase, SnippetCap, + base_db::SourceDatabase, imports::insert_use::{ImportGranularity, InsertUseConfig}, source_change::FileSystemEdit, - EditionedFileId, RootDatabase, SnippetCap, }; use stdx::{format_to, trim_indent}; use syntax::TextRange; @@ -14,8 +14,8 @@ use test_fixture::WithFixture; use test_utils::{assert_eq_text, extract_offset}; use crate::{ - assists, handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, - AssistResolveStrategy, Assists, SingleResolve, + Assist, AssistConfig, AssistContext, AssistKind, AssistResolveStrategy, Assists, SingleResolve, + assists, handlers::Handler, }; pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { @@ -222,17 +222,17 @@ pub(crate) fn check_assist_unresolved( fn check_doc_test(assist_id: &str, before: &str, after: &str) { let after = trim_indent(after); let (db, file_id, selection) = RootDatabase::with_range_or_offset(before); - let before = db.file_text(file_id.file_id()).to_string(); - let frange = FileRange { file_id, range: selection.into() }; + let before = db.file_text(file_id.file_id(&db)).text(&db).to_string(); + let frange = ide_db::FileRange { file_id: file_id.file_id(&db), range: selection.into() }; - let assist = assists(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange.into()) + let assist = assists(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange) .into_iter() .find(|assist| assist.id.0 == assist_id) .unwrap_or_else(|| { panic!( "\n\nAssist is not applicable: {}\nAvailable assists: {}", assist_id, - assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange.into()) + assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange) .into_iter() .map(|assist| assist.id.0) .collect::<Vec<_>>() @@ -247,7 +247,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { .expect("Assist did not contain any source changes"); let mut actual = before; if let Some((source_file_edit, snippet_edit)) = - source_change.get_source_and_snippet_edit(file_id.file_id()) + source_change.get_source_and_snippet_edit(file_id.file_id(&db)) { source_file_edit.apply(&mut actual); if let Some(snippet_edit) = snippet_edit { @@ -281,9 +281,9 @@ fn check_with_config( ) { let (mut db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); db.enable_proc_attr_macros(); - let text_without_caret = db.file_text(file_with_caret_id.into()).to_string(); + let text_without_caret = db.file_text(file_with_caret_id.file_id(&db)).text(&db).to_string(); - let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; + let frange = hir::FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; let sema = Semantics::new(&db); let ctx = AssistContext::new(sema, &config, frange); @@ -297,7 +297,9 @@ fn check_with_config( let assist = match assist_label { Some(label) => res.into_iter().find(|resolved| resolved.label == label), - None => res.pop(), + None if res.is_empty() => None, + // Pick the first as that is the one with the highest priority + None => Some(res.swap_remove(0)), }; match (assist, expected) { @@ -311,14 +313,14 @@ fn check_with_config( let mut buf = String::new(); for (file_id, (edit, snippet_edit)) in source_change.source_file_edits { - let mut text = db.file_text(file_id).as_ref().to_owned(); + let mut text = db.file_text(file_id).text(&db).as_ref().to_owned(); edit.apply(&mut text); if let Some(snippet_edit) = snippet_edit { snippet_edit.apply(&mut text); } if !skip_header { - let sr = db.file_source_root(file_id); - let sr = db.source_root(sr); + let source_root_id = db.file_source_root(file_id).source_root_id(&db); + let sr = db.source_root(source_root_id).source_root(&db); let path = sr.path_for_file(&file_id).unwrap(); format_to!(buf, "//- {}\n", path) } @@ -329,15 +331,16 @@ fn check_with_config( let (dst, contents) = match file_system_edit { FileSystemEdit::CreateFile { dst, initial_contents } => (dst, initial_contents), FileSystemEdit::MoveFile { src, dst } => { - (dst, db.file_text(src).as_ref().to_owned()) + (dst, db.file_text(src).text(&db).as_ref().to_owned()) } FileSystemEdit::MoveDir { src, src_id, dst } => { // temporary placeholder for MoveDir since we are not using MoveDir in ide assists yet. (dst, format!("{src_id:?}\n{src:?}")) } }; - let sr = db.file_source_root(dst.anchor); - let sr = db.source_root(sr); + + let source_root_id = db.file_source_root(dst.anchor).source_root_id(&db); + let sr = db.source_root(source_root_id).source_root(&db); let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); base.pop(); let created_file_path = base.join(&dst.path).unwrap(); @@ -387,8 +390,9 @@ fn assist_order_field_struct() { let before = "struct Foo { $0bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); let (db, file_id) = with_single_file(&before); - let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; - let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange.into()); + let frange = + FileRange { file_id: file_id.file_id(&db), range: TextRange::empty(before_cursor_pos) }; + let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange); let mut assists = assists.iter(); assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); @@ -414,7 +418,12 @@ pub fn test_some_range(a: int) -> bool { "#, ); - let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange.into()); + let assists = assists( + &db, + &TEST_CONFIG, + AssistResolveStrategy::None, + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, + ); let expected = labels(&assists); expect![[r#" @@ -442,7 +451,12 @@ pub fn test_some_range(a: int) -> bool { let mut cfg = TEST_CONFIG; cfg.allowed = Some(vec![AssistKind::Refactor]); - let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange.into()); + let assists = assists( + &db, + &cfg, + AssistResolveStrategy::None, + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, + ); let expected = labels(&assists); expect![[r#" @@ -456,7 +470,12 @@ pub fn test_some_range(a: int) -> bool { { let mut cfg = TEST_CONFIG; cfg.allowed = Some(vec![AssistKind::RefactorExtract]); - let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange.into()); + let assists = assists( + &db, + &cfg, + AssistResolveStrategy::None, + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, + ); let expected = labels(&assists); expect![[r#" @@ -468,7 +487,12 @@ pub fn test_some_range(a: int) -> bool { { let mut cfg = TEST_CONFIG; cfg.allowed = Some(vec![AssistKind::QuickFix]); - let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange.into()); + let assists = assists( + &db, + &cfg, + AssistResolveStrategy::None, + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, + ); let expected = labels(&assists); expect![[r#""#]].assert_eq(&expected); @@ -493,7 +517,12 @@ pub fn test_some_range(a: int) -> bool { cfg.allowed = Some(vec![AssistKind::RefactorExtract]); { - let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange.into()); + let assists = assists( + &db, + &cfg, + AssistResolveStrategy::None, + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, + ); assert_eq!(4, assists.len()); let mut assists = assists.into_iter(); @@ -503,6 +532,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_variable", RefactorExtract, + None, ), label: "Extract into variable", group: Some( @@ -523,6 +553,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_constant", RefactorExtract, + None, ), label: "Extract into constant", group: Some( @@ -543,6 +574,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_static", RefactorExtract, + None, ), label: "Extract into static", group: Some( @@ -563,6 +595,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_function", RefactorExtract, + None, ), label: "Extract into function", group: Some( @@ -585,8 +618,9 @@ pub fn test_some_range(a: int) -> bool { AssistResolveStrategy::Single(SingleResolve { assist_id: "SOMETHING_MISMATCHING".to_owned(), assist_kind: AssistKind::RefactorExtract, + assist_subtype: None, }), - frange.into(), + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, ); assert_eq!(4, assists.len()); let mut assists = assists.into_iter(); @@ -597,6 +631,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_variable", RefactorExtract, + None, ), label: "Extract into variable", group: Some( @@ -617,6 +652,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_constant", RefactorExtract, + None, ), label: "Extract into constant", group: Some( @@ -637,6 +673,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_static", RefactorExtract, + None, ), label: "Extract into static", group: Some( @@ -657,6 +694,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_function", RefactorExtract, + None, ), label: "Extract into function", group: Some( @@ -679,8 +717,9 @@ pub fn test_some_range(a: int) -> bool { AssistResolveStrategy::Single(SingleResolve { assist_id: "extract_variable".to_owned(), assist_kind: AssistKind::RefactorExtract, + assist_subtype: None, }), - frange.into(), + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, ); assert_eq!(4, assists.len()); let mut assists = assists.into_iter(); @@ -691,6 +730,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_variable", RefactorExtract, + None, ), label: "Extract into variable", group: Some( @@ -710,24 +750,21 @@ pub fn test_some_range(a: int) -> bool { Indel { insert: "let", delete: 45..47, - annotation: None, }, Indel { insert: "var_name", delete: 48..60, - annotation: None, }, Indel { insert: "=", delete: 61..81, - annotation: None, }, Indel { insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }", delete: 82..108, - annotation: None, }, ], + annotation: None, }, Some( SnippetEdit( @@ -760,6 +797,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_constant", RefactorExtract, + None, ), label: "Extract into constant", group: Some( @@ -780,6 +818,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_static", RefactorExtract, + None, ), label: "Extract into static", group: Some( @@ -800,6 +839,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_function", RefactorExtract, + None, ), label: "Extract into function", group: Some( @@ -816,7 +856,12 @@ pub fn test_some_range(a: int) -> bool { } { - let assists = assists(&db, &cfg, AssistResolveStrategy::All, frange.into()); + let assists = assists( + &db, + &cfg, + AssistResolveStrategy::All, + FileRange { file_id: frange.file_id.file_id(&db), range: frange.range }, + ); assert_eq!(4, assists.len()); let mut assists = assists.into_iter(); @@ -826,6 +871,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_variable", RefactorExtract, + None, ), label: "Extract into variable", group: Some( @@ -845,24 +891,21 @@ pub fn test_some_range(a: int) -> bool { Indel { insert: "let", delete: 45..47, - annotation: None, }, Indel { insert: "var_name", delete: 48..60, - annotation: None, }, Indel { insert: "=", delete: 61..81, - annotation: None, }, Indel { insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }", delete: 82..108, - annotation: None, }, ], + annotation: None, }, Some( SnippetEdit( @@ -895,6 +938,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_constant", RefactorExtract, + None, ), label: "Extract into constant", group: Some( @@ -914,29 +958,25 @@ pub fn test_some_range(a: int) -> bool { Indel { insert: "const", delete: 45..47, - annotation: None, }, Indel { insert: "VAR_NAME:", delete: 48..60, - annotation: None, }, Indel { insert: "i32", delete: 61..81, - annotation: None, }, Indel { insert: "=", delete: 82..86, - annotation: None, }, Indel { insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }", delete: 87..108, - annotation: None, }, ], + annotation: None, }, Some( SnippetEdit( @@ -969,6 +1009,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_static", RefactorExtract, + None, ), label: "Extract into static", group: Some( @@ -988,29 +1029,25 @@ pub fn test_some_range(a: int) -> bool { Indel { insert: "static", delete: 45..47, - annotation: None, }, Indel { insert: "VAR_NAME:", delete: 48..60, - annotation: None, }, Indel { insert: "i32", delete: 61..81, - annotation: None, }, Indel { insert: "=", delete: 82..86, - annotation: None, }, Indel { insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }", delete: 87..108, - annotation: None, }, ], + annotation: None, }, Some( SnippetEdit( @@ -1043,6 +1080,7 @@ pub fn test_some_range(a: int) -> bool { id: AssistId( "extract_function", RefactorExtract, + None, ), label: "Extract into function", group: Some( @@ -1062,14 +1100,13 @@ pub fn test_some_range(a: int) -> bool { Indel { insert: "fun_name()", delete: 59..60, - annotation: None, }, Indel { insert: "\n\nfn fun_name() -> i32 {\n 5\n}", delete: 110..110, - annotation: None, }, ], + annotation: None, }, Some( SnippetEdit( |