aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-09 15:30:09 +0000
committerGitHub <[email protected]>2021-02-09 15:30:09 +0000
commit12c7b66a7c6963d42ab5f33a9ac3f0b30e351b69 (patch)
tree952bfc7c6274568d90192c847485a22e8c16fb37 /crates/ide/src
parent4ae7d39f77995ad455c7a21c7fdfc1deb588e782 (diff)
parent9ea2c96ddd0ad8c8898f1c65667a57a78ba2218c (diff)
Merge #7611
7611: Cleanups r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/display/navigation_target.rs12
-rw-r--r--crates/ide/src/fixture.rs16
-rw-r--r--crates/ide/src/goto_definition.rs13
-rw-r--r--crates/ide/src/parent_module.rs71
4 files changed, 63 insertions, 49 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 {
85 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 85 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
86 if let Some(src) = module.declaration_source(db) { 86 if let Some(src) = module.declaration_source(db) {
87 let node = src.as_ref().map(|it| it.syntax()); 87 let node = src.as_ref().map(|it| it.syntax());
88 let frange = node.original_file_range(db); 88 let full_range = node.original_file_range(db);
89 let focus_range = src
90 .value
91 .name()
92 .map(|name| src.with_value(name.syntax()).original_file_range(db).range);
89 let mut res = NavigationTarget::from_syntax( 93 let mut res = NavigationTarget::from_syntax(
90 frange.file_id, 94 full_range.file_id,
91 name, 95 name,
92 None, 96 focus_range,
93 frange.range, 97 full_range.range,
94 SymbolKind::Module, 98 SymbolKind::Module,
95 ); 99 );
96 res.docs = module.attrs(db).docs(); 100 res.docs = module.attrs(db).docs();
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 @@
1//! Utilities for creating `Analysis` instances for tests. 1//! Utilities for creating `Analysis` instances for tests.
2use ide_db::base_db::fixture::ChangeFixture; 2use ide_db::base_db::fixture::ChangeFixture;
3use syntax::{TextRange, TextSize};
3use test_utils::{extract_annotations, RangeOrOffset}; 4use test_utils::{extract_annotations, RangeOrOffset};
4 5
5use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange}; 6use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
@@ -68,3 +69,18 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil
68 .collect(); 69 .collect();
69 (host.analysis(), FilePosition { file_id, offset }, annotations) 70 (host.analysis(), FilePosition { file_id, offset }, annotations)
70} 71}
72
73pub(crate) fn nav_target_annotation(ra_fixture: &str) -> (Analysis, FilePosition, FileRange) {
74 let (analysis, position, mut annotations) = annotations(ra_fixture);
75 let (mut expected, data) = annotations.pop().unwrap();
76 assert!(annotations.is_empty());
77 match data.as_str() {
78 "" => (),
79 "file" => {
80 expected.range =
81 TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
82 }
83 data => panic!("bad data: {}", data),
84 }
85 (analysis, position, expected)
86}
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs
index 1a997fa40..e86ae2a18 100644
--- a/crates/ide/src/goto_definition.rs
+++ b/crates/ide/src/goto_definition.rs
@@ -131,22 +131,11 @@ pub(crate) fn reference_definition(
131#[cfg(test)] 131#[cfg(test)]
132mod tests { 132mod tests {
133 use ide_db::base_db::FileRange; 133 use ide_db::base_db::FileRange;
134 use syntax::{TextRange, TextSize};
135 134
136 use crate::fixture; 135 use crate::fixture;
137 136
138 fn check(ra_fixture: &str) { 137 fn check(ra_fixture: &str) {
139 let (analysis, position, mut annotations) = fixture::annotations(ra_fixture); 138 let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture);
140 let (mut expected, data) = annotations.pop().unwrap();
141 match data.as_str() {
142 "" => (),
143 "file" => {
144 expected.range =
145 TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
146 }
147 data => panic!("bad data: {}", data),
148 }
149
150 let mut navs = 139 let mut navs =
151 analysis.goto_definition(position).unwrap().expect("no definition found").info; 140 analysis.goto_definition(position).unwrap().expect("no definition found").info;
152 if navs.len() == 0 { 141 if navs.len() == 0 {
diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs
index d343638fb..ddbaf22b7 100644
--- a/crates/ide/src/parent_module.rs
+++ b/crates/ide/src/parent_module.rs
@@ -63,57 +63,62 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
63 63
64#[cfg(test)] 64#[cfg(test)]
65mod tests { 65mod tests {
66 use ide_db::base_db::FileRange;
66 use test_utils::mark; 67 use test_utils::mark;
67 68
68 use crate::fixture::{self}; 69 use crate::fixture;
70
71 fn check(ra_fixture: &str) {
72 let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture);
73 let mut navs = analysis.parent_module(position).unwrap();
74 assert_eq!(navs.len(), 1);
75 let nav = navs.pop().unwrap();
76 assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
77 }
69 78
70 #[test] 79 #[test]
71 fn test_resolve_parent_module() { 80 fn test_resolve_parent_module() {
72 let (analysis, pos) = fixture::position( 81 check(
73 " 82 r#"
74 //- /lib.rs 83//- /lib.rs
75 mod foo; 84mod foo;
76 //- /foo.rs 85 //^^^
77 $0// empty 86
78 ", 87//- /foo.rs
88$0// empty
89"#,
79 ); 90 );
80 let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
81 nav.assert_match("foo Module FileId(0) 0..8");
82 } 91 }
83 92
84 #[test] 93 #[test]
85 fn test_resolve_parent_module_on_module_decl() { 94 fn test_resolve_parent_module_on_module_decl() {
86 mark::check!(test_resolve_parent_module_on_module_decl); 95 mark::check!(test_resolve_parent_module_on_module_decl);
87 let (analysis, pos) = fixture::position( 96 check(
88 " 97 r#"
89 //- /lib.rs 98//- /lib.rs
90 mod foo; 99mod foo;
91 100 //^^^
92 //- /foo.rs 101//- /foo.rs
93 mod $0bar; 102mod $0bar;
94 103
95 //- /foo/bar.rs 104//- /foo/bar.rs
96 // empty 105// empty
97 ", 106"#,
98 ); 107 );
99 let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
100 nav.assert_match("foo Module FileId(0) 0..8");
101 } 108 }
102 109
103 #[test] 110 #[test]
104 fn test_resolve_parent_module_for_inline() { 111 fn test_resolve_parent_module_for_inline() {
105 let (analysis, pos) = fixture::position( 112 check(
106 " 113 r#"
107 //- /lib.rs 114//- /lib.rs
108 mod foo { 115mod foo {
109 mod bar { 116 mod bar {
110 mod baz { $0 } 117 mod baz { $0 }
111 } 118 } //^^^
112 } 119}
113 ", 120"#,
114 ); 121 );
115 let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
116 nav.assert_match("baz Module FileId(0) 32..44");
117 } 122 }
118 123
119 #[test] 124 #[test]