aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r--crates/ide_db/src/apply_change.rs1
-rw-r--r--crates/ide_db/src/call_info/tests.rs54
-rw-r--r--crates/ide_db/src/defs.rs35
-rw-r--r--crates/ide_db/src/helpers.rs109
-rw-r--r--crates/ide_db/src/helpers/famous_defs_fixture.rs120
-rw-r--r--crates/ide_db/src/imports_locator.rs23
-rw-r--r--crates/ide_db/src/search.rs164
-rw-r--r--crates/ide_db/src/traits/tests.rs18
8 files changed, 326 insertions, 198 deletions
diff --git a/crates/ide_db/src/apply_change.rs b/crates/ide_db/src/apply_change.rs
index 71c19ed13..c770a236b 100644
--- a/crates/ide_db/src/apply_change.rs
+++ b/crates/ide_db/src/apply_change.rs
@@ -145,6 +145,7 @@ impl RootDatabase {
145 hir::db::MacroDefQuery 145 hir::db::MacroDefQuery
146 hir::db::ParseMacroExpansionQuery 146 hir::db::ParseMacroExpansionQuery
147 hir::db::MacroExpandQuery 147 hir::db::MacroExpandQuery
148 hir::db::HygieneFrameQuery
148 149
149 // DefDatabase 150 // DefDatabase
150 hir::db::ItemTreeQuery 151 hir::db::ItemTreeQuery
diff --git a/crates/ide_db/src/call_info/tests.rs b/crates/ide_db/src/call_info/tests.rs
index 9335aeaa5..c714cf280 100644
--- a/crates/ide_db/src/call_info/tests.rs
+++ b/crates/ide_db/src/call_info/tests.rs
@@ -3,12 +3,12 @@ use base_db::{fixture::ChangeFixture, FilePosition};
3use expect_test::{expect, Expect}; 3use expect_test::{expect, Expect};
4use test_utils::{mark, RangeOrOffset}; 4use test_utils::{mark, RangeOrOffset};
5 5
6/// Creates analysis from a multi-file fixture, returns positions marked with <|>. 6/// Creates analysis from a multi-file fixture, returns positions marked with $0.
7pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) { 7pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
8 let change_fixture = ChangeFixture::parse(ra_fixture); 8 let change_fixture = ChangeFixture::parse(ra_fixture);
9 let mut database = RootDatabase::default(); 9 let mut database = RootDatabase::default();
10 database.apply_change(change_fixture.change); 10 database.apply_change(change_fixture.change);
11 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker (<|>)"); 11 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
12 let offset = match range_or_offset { 12 let offset = match range_or_offset {
13 RangeOrOffset::Range(_) => panic!(), 13 RangeOrOffset::Range(_) => panic!(),
14 RangeOrOffset::Offset(it) => it, 14 RangeOrOffset::Offset(it) => it,
@@ -49,7 +49,7 @@ fn test_fn_signature_two_args() {
49 check( 49 check(
50 r#" 50 r#"
51fn foo(x: u32, y: u32) -> u32 {x + y} 51fn foo(x: u32, y: u32) -> u32 {x + y}
52fn bar() { foo(<|>3, ); } 52fn bar() { foo($03, ); }
53"#, 53"#,
54 expect![[r#" 54 expect![[r#"
55 fn foo(x: u32, y: u32) -> u32 55 fn foo(x: u32, y: u32) -> u32
@@ -59,7 +59,7 @@ fn bar() { foo(<|>3, ); }
59 check( 59 check(
60 r#" 60 r#"
61fn foo(x: u32, y: u32) -> u32 {x + y} 61fn foo(x: u32, y: u32) -> u32 {x + y}
62fn bar() { foo(3<|>, ); } 62fn bar() { foo(3$0, ); }
63"#, 63"#,
64 expect![[r#" 64 expect![[r#"
65 fn foo(x: u32, y: u32) -> u32 65 fn foo(x: u32, y: u32) -> u32
@@ -69,7 +69,7 @@ fn bar() { foo(3<|>, ); }
69 check( 69 check(
70 r#" 70 r#"
71fn foo(x: u32, y: u32) -> u32 {x + y} 71fn foo(x: u32, y: u32) -> u32 {x + y}
72fn bar() { foo(3,<|> ); } 72fn bar() { foo(3,$0 ); }
73"#, 73"#,
74 expect![[r#" 74 expect![[r#"
75 fn foo(x: u32, y: u32) -> u32 75 fn foo(x: u32, y: u32) -> u32
@@ -79,7 +79,7 @@ fn bar() { foo(3,<|> ); }
79 check( 79 check(
80 r#" 80 r#"
81fn foo(x: u32, y: u32) -> u32 {x + y} 81fn foo(x: u32, y: u32) -> u32 {x + y}
82fn bar() { foo(3, <|>); } 82fn bar() { foo(3, $0); }
83"#, 83"#,
84 expect![[r#" 84 expect![[r#"
85 fn foo(x: u32, y: u32) -> u32 85 fn foo(x: u32, y: u32) -> u32
@@ -93,7 +93,7 @@ fn test_fn_signature_two_args_empty() {
93 check( 93 check(
94 r#" 94 r#"
95fn foo(x: u32, y: u32) -> u32 {x + y} 95fn foo(x: u32, y: u32) -> u32 {x + y}
96fn bar() { foo(<|>); } 96fn bar() { foo($0); }
97"#, 97"#,
98 expect![[r#" 98 expect![[r#"
99 fn foo(x: u32, y: u32) -> u32 99 fn foo(x: u32, y: u32) -> u32
@@ -110,7 +110,7 @@ fn foo<T, U: Copy + Display>(x: T, y: U) -> u32
110 where T: Copy + Display, U: Debug 110 where T: Copy + Display, U: Debug
111{ x + y } 111{ x + y }
112 112
113fn bar() { foo(<|>3, ); } 113fn bar() { foo($03, ); }
114"#, 114"#,
115 expect![[r#" 115 expect![[r#"
116 fn foo(x: i32, y: {unknown}) -> u32 116 fn foo(x: i32, y: {unknown}) -> u32
@@ -124,7 +124,7 @@ fn test_fn_signature_no_params() {
124 check( 124 check(
125 r#" 125 r#"
126fn foo<T>() -> T where T: Copy + Display {} 126fn foo<T>() -> T where T: Copy + Display {}
127fn bar() { foo(<|>); } 127fn bar() { foo($0); }
128"#, 128"#,
129 expect![[r#" 129 expect![[r#"
130 fn foo() -> {unknown} 130 fn foo() -> {unknown}
@@ -140,7 +140,7 @@ fn test_fn_signature_for_impl() {
140struct F; 140struct F;
141impl F { pub fn new() { } } 141impl F { pub fn new() { } }
142fn bar() { 142fn bar() {
143 let _ : F = F::new(<|>); 143 let _ : F = F::new($0);
144} 144}
145"#, 145"#,
146 expect![[r#" 146 expect![[r#"
@@ -159,7 +159,7 @@ impl S { pub fn do_it(&self) {} }
159 159
160fn bar() { 160fn bar() {
161 let s: S = S; 161 let s: S = S;
162 s.do_it(<|>); 162 s.do_it($0);
163} 163}
164"#, 164"#,
165 expect![[r#" 165 expect![[r#"
@@ -178,7 +178,7 @@ impl S {
178 fn foo(&self, x: i32) {} 178 fn foo(&self, x: i32) {}
179} 179}
180 180
181fn main() { S.foo(<|>); } 181fn main() { S.foo($0); }
182"#, 182"#,
183 expect![[r#" 183 expect![[r#"
184 fn foo(&self, x: i32) 184 fn foo(&self, x: i32)
@@ -196,7 +196,7 @@ impl S {
196 fn foo(&self, x: i32) {} 196 fn foo(&self, x: i32) {}
197} 197}
198 198
199fn main() { S::foo(<|>); } 199fn main() { S::foo($0); }
200"#, 200"#,
201 expect![[r#" 201 expect![[r#"
202 fn foo(self: &S, x: i32) 202 fn foo(self: &S, x: i32)
@@ -216,7 +216,7 @@ fn foo(j: u32) -> u32 {
216} 216}
217 217
218fn bar() { 218fn bar() {
219 let _ = foo(<|>); 219 let _ = foo($0);
220} 220}
221"#, 221"#,
222 expect![[r#" 222 expect![[r#"
@@ -246,7 +246,7 @@ pub fn add_one(x: i32) -> i32 {
246} 246}
247 247
248pub fn do() { 248pub fn do() {
249 add_one(<|> 249 add_one($0
250}"#, 250}"#,
251 expect![[r##" 251 expect![[r##"
252 Adds one to the number given. 252 Adds one to the number given.
@@ -287,7 +287,7 @@ impl addr {
287 287
288pub fn do_it() { 288pub fn do_it() {
289 addr {}; 289 addr {};
290 addr::add_one(<|>); 290 addr::add_one($0);
291} 291}
292"#, 292"#,
293 expect![[r##" 293 expect![[r##"
@@ -331,7 +331,7 @@ impl<E> WriteHandler<E> {
331} 331}
332 332
333pub fn foo(mut r: WriteHandler<()>) { 333pub fn foo(mut r: WriteHandler<()>) {
334 r.finished(<|>); 334 r.finished($0);
335} 335}
336"#, 336"#,
337 expect![[r#" 337 expect![[r#"
@@ -351,7 +351,7 @@ fn call_info_bad_offset() {
351 check( 351 check(
352 r#" 352 r#"
353fn foo(x: u32, y: u32) -> u32 {x + y} 353fn foo(x: u32, y: u32) -> u32 {x + y}
354fn bar() { foo <|> (3, ); } 354fn bar() { foo $0 (3, ); }
355"#, 355"#,
356 expect![[""]], 356 expect![[""]],
357 ); 357 );
@@ -368,7 +368,7 @@ fn bar(_: u32) { }
368 368
369fn main() { 369fn main() {
370 let foo = Foo; 370 let foo = Foo;
371 std::thread::spawn(move || foo.bar(<|>)); 371 std::thread::spawn(move || foo.bar($0));
372} 372}
373"#, 373"#,
374 expect![[r#" 374 expect![[r#"
@@ -385,7 +385,7 @@ fn works_for_tuple_structs() {
385/// A cool tuple struct 385/// A cool tuple struct
386struct S(u32, i32); 386struct S(u32, i32);
387fn main() { 387fn main() {
388 let s = S(0, <|>); 388 let s = S(0, $0);
389} 389}
390"#, 390"#,
391 expect![[r#" 391 expect![[r#"
@@ -403,7 +403,7 @@ fn generic_struct() {
403 r#" 403 r#"
404struct S<T>(T); 404struct S<T>(T);
405fn main() { 405fn main() {
406 let s = S(<|>); 406 let s = S($0);
407} 407}
408"#, 408"#,
409 expect![[r#" 409 expect![[r#"
@@ -427,7 +427,7 @@ enum E {
427} 427}
428 428
429fn main() { 429fn main() {
430 let a = E::A(<|>); 430 let a = E::A($0);
431} 431}
432"#, 432"#,
433 expect![[r#" 433 expect![[r#"
@@ -445,7 +445,7 @@ fn cant_call_struct_record() {
445 r#" 445 r#"
446struct S { x: u32, y: i32 } 446struct S { x: u32, y: i32 }
447fn main() { 447fn main() {
448 let s = S(<|>); 448 let s = S($0);
449} 449}
450"#, 450"#,
451 expect![[""]], 451 expect![[""]],
@@ -466,7 +466,7 @@ enum E {
466} 466}
467 467
468fn main() { 468fn main() {
469 let a = E::C(<|>); 469 let a = E::C($0);
470} 470}
471"#, 471"#,
472 expect![[""]], 472 expect![[""]],
@@ -480,7 +480,7 @@ fn fn_signature_for_call_in_macro() {
480macro_rules! id { ($($tt:tt)*) => { $($tt)* } } 480macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
481fn foo() { } 481fn foo() { }
482id! { 482id! {
483 fn bar() { foo(<|>); } 483 fn bar() { foo($0); }
484} 484}
485"#, 485"#,
486 expect![[r#" 486 expect![[r#"
@@ -497,7 +497,7 @@ fn call_info_for_lambdas() {
497struct S; 497struct S;
498fn foo(s: S) -> i32 { 92 } 498fn foo(s: S) -> i32 { 92 }
499fn main() { 499fn main() {
500 (|s| foo(s))(<|>) 500 (|s| foo(s))($0)
501} 501}
502 "#, 502 "#,
503 expect![[r#" 503 expect![[r#"
@@ -512,7 +512,7 @@ fn call_info_for_fn_ptr() {
512 check( 512 check(
513 r#" 513 r#"
514fn main(f: fn(i32, f64) -> char) { 514fn main(f: fn(i32, f64) -> char) {
515 f(0, <|>) 515 f(0, $0)
516} 516}
517 "#, 517 "#,
518 expect![[r#" 518 expect![[r#"
diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs
index d33a6cb86..d68fe42b0 100644
--- a/crates/ide_db/src/defs.rs
+++ b/crates/ide_db/src/defs.rs
@@ -6,8 +6,8 @@
6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). 6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
7 7
8use hir::{ 8use hir::{
9 db::HirDatabase, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, 9 db::HirDatabase, Crate, Field, GenericParam, HasVisibility, Impl, Label, Local, MacroDef,
10 Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility, 10 Module, ModuleDef, Name, PathResolution, Semantics, Visibility,
11}; 11};
12use syntax::{ 12use syntax::{
13 ast::{self, AstNode}, 13 ast::{self, AstNode},
@@ -24,8 +24,7 @@ pub enum Definition {
24 ModuleDef(ModuleDef), 24 ModuleDef(ModuleDef),
25 SelfType(Impl), 25 SelfType(Impl),
26 Local(Local), 26 Local(Local),
27 TypeParam(TypeParam), 27 GenericParam(GenericParam),
28 LifetimeParam(LifetimeParam),
29 Label(Label), 28 Label(Label),
30} 29}
31 30
@@ -37,8 +36,7 @@ impl Definition {
37 Definition::ModuleDef(it) => it.module(db), 36 Definition::ModuleDef(it) => it.module(db),
38 Definition::SelfType(it) => Some(it.module(db)), 37 Definition::SelfType(it) => Some(it.module(db)),
39 Definition::Local(it) => Some(it.module(db)), 38 Definition::Local(it) => Some(it.module(db)),
40 Definition::TypeParam(it) => Some(it.module(db)), 39 Definition::GenericParam(it) => Some(it.module(db)),
41 Definition::LifetimeParam(it) => Some(it.module(db)),
42 Definition::Label(it) => Some(it.module(db)), 40 Definition::Label(it) => Some(it.module(db)),
43 } 41 }
44 } 42 }
@@ -50,8 +48,7 @@ impl Definition {
50 Definition::ModuleDef(def) => def.definition_visibility(db), 48 Definition::ModuleDef(def) => def.definition_visibility(db),
51 Definition::SelfType(_) => None, 49 Definition::SelfType(_) => None,
52 Definition::Local(_) => None, 50 Definition::Local(_) => None,
53 Definition::TypeParam(_) => None, 51 Definition::GenericParam(_) => None,
54 Definition::LifetimeParam(_) => None,
55 Definition::Label(_) => None, 52 Definition::Label(_) => None,
56 } 53 }
57 } 54 }
@@ -77,8 +74,7 @@ impl Definition {
77 }, 74 },
78 Definition::SelfType(_) => return None, 75 Definition::SelfType(_) => return None,
79 Definition::Local(it) => it.name(db)?, 76 Definition::Local(it) => it.name(db)?,
80 Definition::TypeParam(it) => it.name(db), 77 Definition::GenericParam(it) => it.name(db),
81 Definition::LifetimeParam(it) => it.name(db),
82 Definition::Label(it) => it.name(db), 78 Definition::Label(it) => it.name(db),
83 }; 79 };
84 Some(name) 80 Some(name)
@@ -231,7 +227,11 @@ impl NameClass {
231 }, 227 },
232 ast::TypeParam(it) => { 228 ast::TypeParam(it) => {
233 let def = sema.to_def(&it)?; 229 let def = sema.to_def(&it)?;
234 Some(NameClass::Definition(Definition::TypeParam(def))) 230 Some(NameClass::Definition(Definition::GenericParam(def.into())))
231 },
232 ast::ConstParam(it) => {
233 let def = sema.to_def(&it)?;
234 Some(NameClass::Definition(Definition::GenericParam(def.into())))
235 }, 235 },
236 _ => None, 236 _ => None,
237 } 237 }
@@ -249,7 +249,7 @@ impl NameClass {
249 match parent { 249 match parent {
250 ast::LifetimeParam(it) => { 250 ast::LifetimeParam(it) => {
251 let def = sema.to_def(&it)?; 251 let def = sema.to_def(&it)?;
252 Some(NameClass::Definition(Definition::LifetimeParam(def))) 252 Some(NameClass::Definition(Definition::GenericParam(def.into())))
253 }, 253 },
254 ast::Label(it) => { 254 ast::Label(it) => {
255 let def = sema.to_def(&it)?; 255 let def = sema.to_def(&it)?;
@@ -350,7 +350,7 @@ impl NameRefClass {
350 if let Some(path) = macro_call.path() { 350 if let Some(path) = macro_call.path() {
351 if path.qualifier().is_none() { 351 if path.qualifier().is_none() {
352 // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment 352 // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment
353 // paths are handled below (allowing `log<|>::info!` to resolve to the log crate). 353 // paths are handled below (allowing `log$0::info!` to resolve to the log crate).
354 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) { 354 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
355 return Some(NameRefClass::Definition(Definition::Macro(macro_def))); 355 return Some(NameRefClass::Definition(Definition::Macro(macro_def)));
356 } 356 }
@@ -385,7 +385,8 @@ impl NameRefClass {
385 | SyntaxKind::WHERE_PRED 385 | SyntaxKind::WHERE_PRED
386 | SyntaxKind::REF_TYPE => sema 386 | SyntaxKind::REF_TYPE => sema
387 .resolve_lifetime_param(lifetime) 387 .resolve_lifetime_param(lifetime)
388 .map(Definition::LifetimeParam) 388 .map(GenericParam::LifetimeParam)
389 .map(Definition::GenericParam)
389 .map(NameRefClass::Definition), 390 .map(NameRefClass::Definition),
390 // lifetime bounds, as in the 'b in 'a: 'b aren't wrapped in TypeBound nodes so we gotta check 391 // lifetime bounds, as in the 'b in 'a: 'b aren't wrapped in TypeBound nodes so we gotta check
391 // if our lifetime is in a LifetimeParam without being the constrained lifetime 392 // if our lifetime is in a LifetimeParam without being the constrained lifetime
@@ -393,7 +394,8 @@ impl NameRefClass {
393 != Some(lifetime) => 394 != Some(lifetime) =>
394 { 395 {
395 sema.resolve_lifetime_param(lifetime) 396 sema.resolve_lifetime_param(lifetime)
396 .map(Definition::LifetimeParam) 397 .map(GenericParam::LifetimeParam)
398 .map(Definition::GenericParam)
397 .map(NameRefClass::Definition) 399 .map(NameRefClass::Definition)
398 } 400 }
399 _ => None, 401 _ => None,
@@ -414,9 +416,10 @@ impl From<PathResolution> for Definition {
414 Definition::ModuleDef(def) 416 Definition::ModuleDef(def)
415 } 417 }
416 PathResolution::Local(local) => Definition::Local(local), 418 PathResolution::Local(local) => Definition::Local(local),
417 PathResolution::TypeParam(par) => Definition::TypeParam(par), 419 PathResolution::TypeParam(par) => Definition::GenericParam(par.into()),
418 PathResolution::Macro(def) => Definition::Macro(def), 420 PathResolution::Macro(def) => Definition::Macro(def),
419 PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def), 421 PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
422 PathResolution::ConstParam(par) => Definition::GenericParam(par.into()),
420 } 423 }
421 } 424 }
422} 425}
diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs
index d988588ff..c6763ae36 100644
--- a/crates/ide_db/src/helpers.rs
+++ b/crates/ide_db/src/helpers.rs
@@ -1,9 +1,10 @@
1//! A module with ide helpers for high-level ide features. 1//! A module with ide helpers for high-level ide features.
2use crate::RootDatabase; 2pub mod insert_use;
3
3use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait}; 4use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
4use syntax::ast::{self, make}; 5use syntax::ast::{self, make};
5 6
6pub mod insert_use; 7use crate::RootDatabase;
7 8
8/// Converts the mod path struct into its ast representation. 9/// Converts the mod path struct into its ast representation.
9pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path { 10pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
@@ -37,94 +38,7 @@ pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Cr
37 38
38#[allow(non_snake_case)] 39#[allow(non_snake_case)]
39impl FamousDefs<'_, '_> { 40impl FamousDefs<'_, '_> {
40 pub const FIXTURE: &'static str = r#"//- /libcore.rs crate:core 41 pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
41pub mod convert {
42 pub trait From<T> {
43 fn from(t: T) -> Self;
44 }
45}
46
47pub mod default {
48 pub trait Default {
49 fn default() -> Self;
50 }
51}
52
53pub mod iter {
54 pub use self::traits::{collect::IntoIterator, iterator::Iterator};
55 mod traits {
56 pub(crate) mod iterator {
57 use crate::option::Option;
58 pub trait Iterator {
59 type Item;
60 fn next(&mut self) -> Option<Self::Item>;
61 fn by_ref(&mut self) -> &mut Self {
62 self
63 }
64 fn take(self, n: usize) -> crate::iter::Take<Self> {
65 crate::iter::Take { inner: self }
66 }
67 }
68
69 impl<I: Iterator> Iterator for &mut I {
70 type Item = I::Item;
71 fn next(&mut self) -> Option<I::Item> {
72 (**self).next()
73 }
74 }
75 }
76 pub(crate) mod collect {
77 pub trait IntoIterator {
78 type Item;
79 }
80 }
81 }
82
83 pub use self::sources::*;
84 pub(crate) mod sources {
85 use super::Iterator;
86 use crate::option::Option::{self, *};
87 pub struct Repeat<A> {
88 element: A,
89 }
90
91 pub fn repeat<T>(elt: T) -> Repeat<T> {
92 Repeat { element: elt }
93 }
94
95 impl<A> Iterator for Repeat<A> {
96 type Item = A;
97
98 fn next(&mut self) -> Option<A> {
99 None
100 }
101 }
102 }
103
104 pub use self::adapters::*;
105 pub(crate) mod adapters {
106 use super::Iterator;
107 use crate::option::Option::{self, *};
108 pub struct Take<I> { pub(crate) inner: I }
109 impl<I> Iterator for Take<I> where I: Iterator {
110 type Item = <I as Iterator>::Item;
111 fn next(&mut self) -> Option<<I as Iterator>::Item> {
112 None
113 }
114 }
115 }
116}
117
118pub mod option {
119 pub enum Option<T> { None, Some(T)}
120}
121
122pub mod prelude {
123 pub use crate::{convert::From, iter::{IntoIterator, Iterator}, option::Option::{self, *}, default::Default};
124}
125#[prelude_import]
126pub use prelude::*;
127"#;
128 42
129 pub fn core(&self) -> Option<Crate> { 43 pub fn core(&self) -> Option<Crate> {
130 self.find_crate("core") 44 self.find_crate("core")
@@ -201,3 +115,18 @@ pub use prelude::*;
201 Some(def) 115 Some(def)
202 } 116 }
203} 117}
118
119#[derive(Clone, Copy, Debug, PartialEq, Eq)]
120pub struct SnippetCap {
121 _private: (),
122}
123
124impl SnippetCap {
125 pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
126 if allow_snippets {
127 Some(SnippetCap { _private: () })
128 } else {
129 None
130 }
131 }
132}
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs
new file mode 100644
index 000000000..5e88de64d
--- /dev/null
+++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs
@@ -0,0 +1,120 @@
1//- /libcore.rs crate:core
2//! Signatures of traits, types and functions from the core lib for use in tests.
3pub mod convert {
4 pub trait From<T> {
5 fn from(t: T) -> Self;
6 }
7}
8
9pub mod default {
10 pub trait Default {
11 fn default() -> Self;
12 }
13}
14
15pub mod iter {
16 pub use self::traits::{collect::IntoIterator, iterator::Iterator};
17 mod traits {
18 pub(crate) mod iterator {
19 use crate::option::Option;
20 pub trait Iterator {
21 type Item;
22 fn next(&mut self) -> Option<Self::Item>;
23 fn by_ref(&mut self) -> &mut Self {
24 self
25 }
26 fn take(self, n: usize) -> crate::iter::Take<Self> {
27 crate::iter::Take { inner: self }
28 }
29 }
30
31 impl<I: Iterator> Iterator for &mut I {
32 type Item = I::Item;
33 fn next(&mut self) -> Option<I::Item> {
34 (**self).next()
35 }
36 }
37 }
38 pub(crate) mod collect {
39 pub trait IntoIterator {
40 type Item;
41 }
42 }
43 }
44
45 pub use self::sources::*;
46 pub(crate) mod sources {
47 use super::Iterator;
48 use crate::option::Option::{self, *};
49 pub struct Repeat<A> {
50 element: A,
51 }
52
53 pub fn repeat<T>(elt: T) -> Repeat<T> {
54 Repeat { element: elt }
55 }
56
57 impl<A> Iterator for Repeat<A> {
58 type Item = A;
59
60 fn next(&mut self) -> Option<A> {
61 None
62 }
63 }
64 }
65
66 pub use self::adapters::*;
67 pub(crate) mod adapters {
68 use super::Iterator;
69 use crate::option::Option::{self, *};
70 pub struct Take<I> {
71 pub(crate) inner: I,
72 }
73 impl<I> Iterator for Take<I>
74 where
75 I: Iterator,
76 {
77 type Item = <I as Iterator>::Item;
78 fn next(&mut self) -> Option<<I as Iterator>::Item> {
79 None
80 }
81 }
82 }
83}
84
85pub mod ops {
86 #[lang = "fn"]
87 pub trait Fn<Args>: FnMut<Args> {
88 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
89 }
90
91 #[lang = "fn_mut"]
92 pub trait FnMut<Args>: FnOnce<Args> {
93 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
94 }
95 #[lang = "fn_once"]
96 pub trait FnOnce<Args> {
97 #[lang = "fn_once_output"]
98 type Output;
99 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
100 }
101}
102
103pub mod option {
104 pub enum Option<T> {
105 None,
106 Some(T),
107 }
108}
109
110pub mod prelude {
111 pub use crate::{
112 convert::From,
113 default::Default,
114 iter::{IntoIterator, Iterator},
115 ops::{Fn, FnMut, FnOnce},
116 option::Option::{self, *},
117 };
118}
119#[prelude_import]
120pub use prelude::*;
diff --git a/crates/ide_db/src/imports_locator.rs b/crates/ide_db/src/imports_locator.rs
index 0f4c2ca47..e9f23adf8 100644
--- a/crates/ide_db/src/imports_locator.rs
+++ b/crates/ide_db/src/imports_locator.rs
@@ -1,7 +1,7 @@
1//! This module contains an import search funcionality that is provided to the assists module. 1//! This module contains an import search functionality that is provided to the assists module.
2//! Later, this should be moved away to a separate crate that is accessible from the assists module. 2//! Later, this should be moved away to a separate crate that is accessible from the assists module.
3 3
4use hir::{import_map, Crate, MacroDef, ModuleDef, Semantics}; 4use hir::{import_map, AsAssocItem, Crate, MacroDef, ModuleDef, Semantics};
5use syntax::{ast, AstNode, SyntaxKind::NAME}; 5use syntax::{ast, AstNode, SyntaxKind::NAME};
6 6
7use crate::{ 7use crate::{
@@ -40,8 +40,9 @@ pub fn find_similar_imports<'a>(
40 krate: Crate, 40 krate: Crate,
41 limit: Option<usize>, 41 limit: Option<usize>,
42 fuzzy_search_string: String, 42 fuzzy_search_string: String,
43 ignore_assoc_items: bool,
43 name_only: bool, 44 name_only: bool,
44) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { 45) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> + 'a {
45 let _p = profile::span("find_similar_imports"); 46 let _p = profile::span("find_similar_imports");
46 47
47 let mut external_query = import_map::Query::new(fuzzy_search_string.clone()) 48 let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
@@ -57,7 +58,21 @@ pub fn find_similar_imports<'a>(
57 external_query = external_query.limit(limit); 58 external_query = external_query.limit(limit);
58 } 59 }
59 60
60 find_imports(sema, krate, local_query, external_query) 61 let db = sema.db;
62 find_imports(sema, krate, local_query, external_query).filter(move |import_candidate| {
63 if ignore_assoc_items {
64 match import_candidate {
65 Either::Left(ModuleDef::Function(function)) => function.as_assoc_item(db).is_none(),
66 Either::Left(ModuleDef::Const(const_)) => const_.as_assoc_item(db).is_none(),
67 Either::Left(ModuleDef::TypeAlias(type_alias)) => {
68 type_alias.as_assoc_item(db).is_none()
69 }
70 _ => true,
71 }
72 } else {
73 true
74 }
75 })
61} 76}
62 77
63fn find_imports<'a>( 78fn find_imports<'a>(
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs
index ff10f71c3..b5fa46642 100644
--- a/crates/ide_db/src/search.rs
+++ b/crates/ide_db/src/search.rs
@@ -18,9 +18,43 @@ use crate::{
18 RootDatabase, 18 RootDatabase,
19}; 19};
20 20
21#[derive(Debug, Default, Clone)]
22pub struct UsageSearchResult {
23 pub references: FxHashMap<FileId, Vec<FileReference>>,
24}
25
26impl UsageSearchResult {
27 pub fn is_empty(&self) -> bool {
28 self.references.is_empty()
29 }
30
31 pub fn len(&self) -> usize {
32 self.references.len()
33 }
34
35 pub fn iter(&self) -> impl Iterator<Item = (&FileId, &Vec<FileReference>)> + '_ {
36 self.references.iter()
37 }
38
39 pub fn file_ranges(&self) -> impl Iterator<Item = FileRange> + '_ {
40 self.references.iter().flat_map(|(&file_id, refs)| {
41 refs.iter().map(move |&FileReference { range, .. }| FileRange { file_id, range })
42 })
43 }
44}
45
46impl IntoIterator for UsageSearchResult {
47 type Item = (FileId, Vec<FileReference>);
48 type IntoIter = <FxHashMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter;
49
50 fn into_iter(self) -> Self::IntoIter {
51 self.references.into_iter()
52 }
53}
54
21#[derive(Debug, Clone)] 55#[derive(Debug, Clone)]
22pub struct Reference { 56pub struct FileReference {
23 pub file_range: FileRange, 57 pub range: TextRange,
24 pub kind: ReferenceKind, 58 pub kind: ReferenceKind,
25 pub access: Option<ReferenceAccess>, 59 pub access: Option<ReferenceAccess>,
26} 60}
@@ -121,31 +155,55 @@ impl Definition {
121 155
122 if let Definition::Local(var) = self { 156 if let Definition::Local(var) = self {
123 let range = match var.parent(db) { 157 let range = match var.parent(db) {
124 DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), 158 DefWithBody::Function(f) => {
125 DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), 159 f.source(db).and_then(|src| Some(src.value.syntax().text_range()))
126 DefWithBody::Static(s) => s.source(db).value.syntax().text_range(), 160 }
161 DefWithBody::Const(c) => {
162 c.source(db).and_then(|src| Some(src.value.syntax().text_range()))
163 }
164 DefWithBody::Static(s) => {
165 s.source(db).and_then(|src| Some(src.value.syntax().text_range()))
166 }
127 }; 167 };
128 let mut res = FxHashMap::default(); 168 let mut res = FxHashMap::default();
129 res.insert(file_id, Some(range)); 169 res.insert(file_id, range);
130 return SearchScope::new(res); 170 return SearchScope::new(res);
131 } 171 }
132 172
133 if let Definition::LifetimeParam(param) = self { 173 if let Definition::GenericParam(hir::GenericParam::LifetimeParam(param)) = self {
134 let range = match param.parent(db) { 174 let range = match param.parent(db) {
135 hir::GenericDef::Function(it) => it.source(db).value.syntax().text_range(), 175 hir::GenericDef::Function(it) => {
176 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
177 }
136 hir::GenericDef::Adt(it) => match it { 178 hir::GenericDef::Adt(it) => match it {
137 hir::Adt::Struct(it) => it.source(db).value.syntax().text_range(), 179 hir::Adt::Struct(it) => {
138 hir::Adt::Union(it) => it.source(db).value.syntax().text_range(), 180 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
139 hir::Adt::Enum(it) => it.source(db).value.syntax().text_range(), 181 }
182 hir::Adt::Union(it) => {
183 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
184 }
185 hir::Adt::Enum(it) => {
186 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
187 }
140 }, 188 },
141 hir::GenericDef::Trait(it) => it.source(db).value.syntax().text_range(), 189 hir::GenericDef::Trait(it) => {
142 hir::GenericDef::TypeAlias(it) => it.source(db).value.syntax().text_range(), 190 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
143 hir::GenericDef::Impl(it) => it.source(db).value.syntax().text_range(), 191 }
144 hir::GenericDef::Variant(it) => it.source(db).value.syntax().text_range(), 192 hir::GenericDef::TypeAlias(it) => {
145 hir::GenericDef::Const(it) => it.source(db).value.syntax().text_range(), 193 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
194 }
195 hir::GenericDef::Impl(it) => {
196 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
197 }
198 hir::GenericDef::Variant(it) => {
199 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
200 }
201 hir::GenericDef::Const(it) => {
202 it.source(db).and_then(|src| Some(src.value.syntax().text_range()))
203 }
146 }; 204 };
147 let mut res = FxHashMap::default(); 205 let mut res = FxHashMap::default();
148 res.insert(file_id, Some(range)); 206 res.insert(file_id, range);
149 return SearchScope::new(res); 207 return SearchScope::new(res);
150 } 208 }
151 209
@@ -228,23 +286,23 @@ impl<'a> FindUsages<'a> {
228 286
229 pub fn at_least_one(self) -> bool { 287 pub fn at_least_one(self) -> bool {
230 let mut found = false; 288 let mut found = false;
231 self.search(&mut |_reference| { 289 self.search(&mut |_, _| {
232 found = true; 290 found = true;
233 true 291 true
234 }); 292 });
235 found 293 found
236 } 294 }
237 295
238 pub fn all(self) -> Vec<Reference> { 296 pub fn all(self) -> UsageSearchResult {
239 let mut res = Vec::new(); 297 let mut res = UsageSearchResult::default();
240 self.search(&mut |reference| { 298 self.search(&mut |file_id, reference| {
241 res.push(reference); 299 res.references.entry(file_id).or_default().push(reference);
242 false 300 false
243 }); 301 });
244 res 302 res
245 } 303 }
246 304
247 fn search(self, sink: &mut dyn FnMut(Reference) -> bool) { 305 fn search(self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
248 let _p = profile::span("FindUsages:search"); 306 let _p = profile::span("FindUsages:search");
249 let sema = self.sema; 307 let sema = self.sema;
250 308
@@ -296,16 +354,14 @@ impl<'a> FindUsages<'a> {
296 fn found_lifetime( 354 fn found_lifetime(
297 &self, 355 &self,
298 lifetime: &ast::Lifetime, 356 lifetime: &ast::Lifetime,
299 sink: &mut dyn FnMut(Reference) -> bool, 357 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
300 ) -> bool { 358 ) -> bool {
301 match NameRefClass::classify_lifetime(self.sema, lifetime) { 359 match NameRefClass::classify_lifetime(self.sema, lifetime) {
302 Some(NameRefClass::Definition(def)) if &def == self.def => { 360 Some(NameRefClass::Definition(def)) if &def == self.def => {
303 let reference = Reference { 361 let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax());
304 file_range: self.sema.original_range(lifetime.syntax()), 362 let reference =
305 kind: ReferenceKind::Lifetime, 363 FileReference { range, kind: ReferenceKind::Lifetime, access: None };
306 access: None, 364 sink(file_id, reference)
307 };
308 sink(reference)
309 } 365 }
310 _ => false, // not a usage 366 _ => false, // not a usage
311 } 367 }
@@ -314,7 +370,7 @@ impl<'a> FindUsages<'a> {
314 fn found_name_ref( 370 fn found_name_ref(
315 &self, 371 &self,
316 name_ref: &ast::NameRef, 372 name_ref: &ast::NameRef,
317 sink: &mut dyn FnMut(Reference) -> bool, 373 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
318 ) -> bool { 374 ) -> bool {
319 match NameRefClass::classify(self.sema, &name_ref) { 375 match NameRefClass::classify(self.sema, &name_ref) {
320 Some(NameRefClass::Definition(def)) if &def == self.def => { 376 Some(NameRefClass::Definition(def)) if &def == self.def => {
@@ -328,46 +384,50 @@ impl<'a> FindUsages<'a> {
328 ReferenceKind::Other 384 ReferenceKind::Other
329 }; 385 };
330 386
331 let reference = Reference { 387 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
332 file_range: self.sema.original_range(name_ref.syntax()), 388 let reference =
333 kind, 389 FileReference { range, kind, access: reference_access(&def, &name_ref) };
334 access: reference_access(&def, &name_ref), 390 sink(file_id, reference)
335 };
336 sink(reference)
337 } 391 }
338 Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { 392 Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => {
393 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
339 let reference = match self.def { 394 let reference = match self.def {
340 Definition::Field(_) if &field == self.def => Reference { 395 Definition::Field(_) if &field == self.def => FileReference {
341 file_range: self.sema.original_range(name_ref.syntax()), 396 range,
342 kind: ReferenceKind::FieldShorthandForField, 397 kind: ReferenceKind::FieldShorthandForField,
343 access: reference_access(&field, &name_ref), 398 access: reference_access(&field, &name_ref),
344 }, 399 },
345 Definition::Local(l) if &local == l => Reference { 400 Definition::Local(l) if &local == l => FileReference {
346 file_range: self.sema.original_range(name_ref.syntax()), 401 range,
347 kind: ReferenceKind::FieldShorthandForLocal, 402 kind: ReferenceKind::FieldShorthandForLocal,
348 access: reference_access(&Definition::Local(local), &name_ref), 403 access: reference_access(&Definition::Local(local), &name_ref),
349 }, 404 },
350 _ => return false, // not a usage 405 _ => return false, // not a usage
351 }; 406 };
352 sink(reference) 407 sink(file_id, reference)
353 } 408 }
354 _ => false, // not a usage 409 _ => false, // not a usage
355 } 410 }
356 } 411 }
357 412
358 fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { 413 fn found_name(
414 &self,
415 name: &ast::Name,
416 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
417 ) -> bool {
359 match NameClass::classify(self.sema, name) { 418 match NameClass::classify(self.sema, name) {
360 Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { 419 Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => {
361 let reference = match self.def { 420 if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) {
362 Definition::Field(_) if &field_ref == self.def => Reference { 421 return false;
363 file_range: self.sema.original_range(name.syntax()), 422 }
364 kind: ReferenceKind::FieldShorthandForField, 423 let FileRange { file_id, range } = self.sema.original_range(name.syntax());
365 // FIXME: mutable patterns should have `Write` access 424 let reference = FileReference {
366 access: Some(ReferenceAccess::Read), 425 range,
367 }, 426 kind: ReferenceKind::FieldShorthandForField,
368 _ => return false, // not a usage 427 // FIXME: mutable patterns should have `Write` access
428 access: Some(ReferenceAccess::Read),
369 }; 429 };
370 sink(reference) 430 sink(file_id, reference)
371 } 431 }
372 _ => false, // not a usage 432 _ => false, // not a usage
373 } 433 }
diff --git a/crates/ide_db/src/traits/tests.rs b/crates/ide_db/src/traits/tests.rs
index 09c7ac3ec..84bb25505 100644
--- a/crates/ide_db/src/traits/tests.rs
+++ b/crates/ide_db/src/traits/tests.rs
@@ -5,12 +5,12 @@ use hir::Semantics;
5use syntax::ast::{self, AstNode}; 5use syntax::ast::{self, AstNode};
6use test_utils::RangeOrOffset; 6use test_utils::RangeOrOffset;
7 7
8/// Creates analysis from a multi-file fixture, returns positions marked with <|>. 8/// Creates analysis from a multi-file fixture, returns positions marked with $0.
9pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) { 9pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
10 let change_fixture = ChangeFixture::parse(ra_fixture); 10 let change_fixture = ChangeFixture::parse(ra_fixture);
11 let mut database = RootDatabase::default(); 11 let mut database = RootDatabase::default();
12 database.apply_change(change_fixture.change); 12 database.apply_change(change_fixture.change);
13 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker (<|>)"); 13 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
14 let offset = match range_or_offset { 14 let offset = match range_or_offset {
15 RangeOrOffset::Range(_) => panic!(), 15 RangeOrOffset::Range(_) => panic!(),
16 RangeOrOffset::Offset(it) => it, 16 RangeOrOffset::Offset(it) => it,
@@ -55,7 +55,7 @@ pub trait Foo {
55 fn bar(); 55 fn bar();
56} 56}
57impl Foo for u8 { 57impl Foo for u8 {
58 <|> 58 $0
59} 59}
60 "#, 60 "#,
61 expect![["Foo"]], 61 expect![["Foo"]],
@@ -68,7 +68,7 @@ pub trait Foo {
68impl Foo for u8 { 68impl Foo for u8 {
69 fn bar() { 69 fn bar() {
70 fn baz() { 70 fn baz() {
71 <|> 71 $0
72 } 72 }
73 baz(); 73 baz();
74 } 74 }
@@ -83,7 +83,7 @@ pub trait Foo {
83} 83}
84pub struct Bar; 84pub struct Bar;
85impl Bar { 85impl Bar {
86 <|> 86 $0
87} 87}
88 "#, 88 "#,
89 expect![[""]], 89 expect![[""]],
@@ -99,7 +99,7 @@ pub trait Foo {
99 fn bar(); 99 fn bar();
100} 100}
101impl Foo for u8 { 101impl Foo for u8 {
102 <|> 102 $0
103}"#, 103}"#,
104 expect![[r#" 104 expect![[r#"
105 FOO 105 FOO
@@ -114,7 +114,7 @@ pub trait Foo {
114} 114}
115impl Foo for u8 { 115impl Foo for u8 {
116 const FOO: u8 = 10; 116 const FOO: u8 = 10;
117 <|> 117 $0
118}"#, 118}"#,
119 expect![[r#" 119 expect![[r#"
120 bar"#]], 120 bar"#]],
@@ -128,7 +128,7 @@ pub trait Foo {
128} 128}
129impl Foo for u8 { 129impl Foo for u8 {
130 const FOO: u8 = 10; 130 const FOO: u8 = 10;
131 fn bar() {<|>} 131 fn bar() {$0}
132}"#, 132}"#,
133 expect![[r#""#]], 133 expect![[r#""#]],
134 ); 134 );
@@ -137,7 +137,7 @@ impl Foo for u8 {
137 r#" 137 r#"
138pub struct Foo; 138pub struct Foo;
139impl Foo { 139impl Foo {
140 fn bar() {<|>} 140 fn bar() {$0}
141}"#, 141}"#,
142 expect![[r#""#]], 142 expect![[r#""#]],
143 ); 143 );