1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
use ra_db::{SourceDatabase};
use ra_syntax::{
AstNode, ast,
algo::find_node_at_offset,
};
use hir::{db::HirDatabase, source_binder};
use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
pub(crate) fn goto_implementation(
db: &RootDatabase,
position: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let file = db.parse(position.file_id);
let syntax = file.syntax();
let module = source_binder::module_from_position(db, position)?;
let krate = module.krate(db)?;
let node = find_node_at_offset::<ast::NominalDef>(syntax, position.offset)?;
let ty = match node.kind() {
ast::NominalDefKind::StructDef(def) => {
source_binder::struct_from_module(db, module, &def).ty(db)
}
ast::NominalDefKind::EnumDef(def) => {
source_binder::enum_from_module(db, module, &def).ty(db)
}
};
let impls = db.impls_in_crate(krate);
let navs = impls
.lookup_impl_blocks(db, &ty)
.map(|(module, imp)| NavigationTarget::from_impl_block(db, module, &imp));
Some(RangeInfo::new(node.syntax().range(), navs.collect()))
}
#[cfg(test)]
mod tests {
use crate::mock_analysis::analysis_and_position;
fn check_goto(fixuture: &str, expected: &[&str]) {
let (analysis, pos) = analysis_and_position(fixuture);
let navs = analysis.goto_implementation(pos).unwrap().unwrap().info;
assert_eq!(navs.len(), expected.len());
navs.into_iter()
.enumerate()
.for_each(|(i, nav)| nav.assert_match(expected[i]));
}
#[test]
fn goto_implementation_works() {
check_goto(
"
//- /lib.rs
struct Foo<|>;
impl Foo {}
",
&["impl IMPL_BLOCK FileId(1) [12; 23)"],
);
}
#[test]
fn goto_implementation_works_multiple_blocks() {
check_goto(
"
//- /lib.rs
struct Foo<|>;
impl Foo {}
impl Foo {}
",
&[
"impl IMPL_BLOCK FileId(1) [12; 23)",
"impl IMPL_BLOCK FileId(1) [24; 35)",
],
);
}
#[test]
fn goto_implementation_works_multiple_mods() {
check_goto(
"
//- /lib.rs
struct Foo<|>;
mod a {
impl super::Foo {}
}
mod b {
impl super::Foo {}
}
",
&[
"impl IMPL_BLOCK FileId(1) [24; 42)",
"impl IMPL_BLOCK FileId(1) [57; 75)",
],
);
}
#[test]
fn goto_implementation_works_multiple_files() {
check_goto(
"
//- /lib.rs
struct Foo<|>;
mod a;
mod b;
//- /a.rs
impl crate::Foo {}
//- /b.rs
impl crate::Foo {}
",
&[
"impl IMPL_BLOCK FileId(2) [0; 18)",
"impl IMPL_BLOCK FileId(3) [0; 18)",
],
);
}
}
|