From 2ac20b05f18895654ced1d243e99092441a94f86 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Feb 2021 18:13:11 +0300 Subject: Cleanup tests --- crates/ide/src/fixture.rs | 16 ++++++++ crates/ide/src/goto_definition.rs | 12 +----- crates/ide/src/parent_module.rs | 80 ++++++++++++++++++--------------------- 3 files changed, 53 insertions(+), 55 deletions(-) diff --git a/crates/ide/src/fixture.rs b/crates/ide/src/fixture.rs index cc8218885..cc6641ba1 100644 --- a/crates/ide/src/fixture.rs +++ b/crates/ide/src/fixture.rs @@ -1,5 +1,6 @@ //! Utilities for creating `Analysis` instances for tests. use ide_db::base_db::fixture::ChangeFixture; +use syntax::{TextRange, TextSize}; use test_utils::{extract_annotations, RangeOrOffset}; use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange}; @@ -68,3 +69,18 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil .collect(); (host.analysis(), FilePosition { file_id, offset }, annotations) } + +pub(crate) fn nav_target_annotation(ra_fixture: &str) -> (Analysis, FilePosition, FileRange) { + let (analysis, position, mut annotations) = annotations(ra_fixture); + let (mut expected, data) = annotations.pop().unwrap(); + assert!(annotations.is_empty()); + match data.as_str() { + "" => (), + "file" => { + expected.range = + TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap())) + } + data => panic!("bad data: {}", data), + } + (analysis, position, expected) +} diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index 1a997fa40..c91eb1283 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -136,17 +136,7 @@ mod tests { use crate::fixture; fn check(ra_fixture: &str) { - let (analysis, position, mut annotations) = fixture::annotations(ra_fixture); - let (mut expected, data) = annotations.pop().unwrap(); - match data.as_str() { - "" => (), - "file" => { - expected.range = - TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap())) - } - data => panic!("bad data: {}", data), - } - + let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture); let mut navs = analysis.goto_definition(position).unwrap().expect("no definition found").info; if navs.len() == 0 { diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs index d343638fb..e5515ef2c 100644 --- a/crates/ide/src/parent_module.rs +++ b/crates/ide/src/parent_module.rs @@ -63,69 +63,61 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec { #[cfg(test)] mod tests { + use ide_db::base_db::FileRange; use test_utils::mark; - use crate::fixture::{self}; + use crate::fixture; + + fn check(ra_fixture: &str) { + let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture); + let mut navs = analysis.parent_module(position).unwrap(); + assert_eq!(navs.len(), 1); + let nav = navs.pop().unwrap(); + assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() }); + } #[test] fn test_resolve_parent_module() { - let (analysis, pos) = fixture::position( - " - //- /lib.rs - mod foo; - //- /foo.rs - $0// empty - ", + check( + r#" +//- /lib.rs + mod foo; +//^^^^^^^^ + +//- /foo.rs +$0// empty +"#, ); - let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); - nav.assert_match("foo Module FileId(0) 0..8"); } #[test] fn test_resolve_parent_module_on_module_decl() { mark::check!(test_resolve_parent_module_on_module_decl); - let (analysis, pos) = fixture::position( - " - //- /lib.rs - mod foo; - - //- /foo.rs - mod $0bar; + check( + r#" +//- /lib.rs + mod foo; +//^^^^^^^^ +//- /foo.rs +mod $0bar; - //- /foo/bar.rs - // empty - ", +//- /foo/bar.rs +// empty +"#, ); - let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); - nav.assert_match("foo Module FileId(0) 0..8"); } #[test] fn test_resolve_parent_module_for_inline() { - let (analysis, pos) = fixture::position( - " - //- /lib.rs - mod foo { - mod bar { - mod baz { $0 } - } - } - ", - ); - let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); - nav.assert_match("baz Module FileId(0) 32..44"); - } - - #[test] - fn test_resolve_crate_root() { - let (analysis, file_id) = fixture::file( + check( r#" -//- /main.rs -mod foo; -//- /foo.rs -$0 +//- /lib.rs +mod foo { + mod bar { + mod baz { $0 } + } //^^^^^^^^^^^^ +} "#, ); - assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1); } } -- cgit v1.2.3 From ef8f38efc658270d55f438fc1a23c355ee0f4481 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Feb 2021 18:21:09 +0300 Subject: More precise navigation to parent --- crates/ide/src/display/navigation_target.rs | 12 ++++++++---- crates/ide/src/goto_definition.rs | 1 - crates/ide/src/parent_module.rs | 10 +++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 23d885218..198243466 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -85,12 +85,16 @@ impl NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); if let Some(src) = module.declaration_source(db) { let node = src.as_ref().map(|it| it.syntax()); - let frange = node.original_file_range(db); + let full_range = node.original_file_range(db); + let focus_range = src + .value + .name() + .map(|name| src.with_value(name.syntax()).original_file_range(db).range); let mut res = NavigationTarget::from_syntax( - frange.file_id, + full_range.file_id, name, - None, - frange.range, + focus_range, + full_range.range, SymbolKind::Module, ); res.docs = module.attrs(db).docs(); diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index c91eb1283..e86ae2a18 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -131,7 +131,6 @@ pub(crate) fn reference_definition( #[cfg(test)] mod tests { use ide_db::base_db::FileRange; - use syntax::{TextRange, TextSize}; use crate::fixture; diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs index e5515ef2c..7b08bc27b 100644 --- a/crates/ide/src/parent_module.rs +++ b/crates/ide/src/parent_module.rs @@ -81,8 +81,8 @@ mod tests { check( r#" //- /lib.rs - mod foo; -//^^^^^^^^ +mod foo; + //^^^ //- /foo.rs $0// empty @@ -96,8 +96,8 @@ $0// empty check( r#" //- /lib.rs - mod foo; -//^^^^^^^^ +mod foo; + //^^^ //- /foo.rs mod $0bar; @@ -115,7 +115,7 @@ mod $0bar; mod foo { mod bar { mod baz { $0 } - } //^^^^^^^^^^^^ + } //^^^ } "#, ); -- cgit v1.2.3 From 9ea2c96ddd0ad8c8898f1c65667a57a78ba2218c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Feb 2021 18:29:15 +0300 Subject: restore accidentally deleted test --- crates/ide/src/parent_module.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs index 7b08bc27b..ddbaf22b7 100644 --- a/crates/ide/src/parent_module.rs +++ b/crates/ide/src/parent_module.rs @@ -120,4 +120,17 @@ mod foo { "#, ); } + + #[test] + fn test_resolve_crate_root() { + let (analysis, file_id) = fixture::file( + r#" +//- /main.rs +mod foo; +//- /foo.rs +$0 +"#, + ); + assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1); + } } -- cgit v1.2.3