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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
use hir::{Crate, ImplDef, Semantics};
use ra_ide_db::RootDatabase;
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo};
// Feature: Go to Implementation
//
// Navigates to the impl block of structs, enums or traits. Also implemented as a code lens.
//
// |===
// | Editor | Shortcut
//
// | VS Code | kbd:[Ctrl+F12]
// |===
pub(crate) fn goto_implementation(
db: &RootDatabase,
position: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let sema = Semantics::new(db);
let source_file = sema.parse(position.file_id);
let syntax = source_file.syntax().clone();
let krate = sema.to_module_def(position.file_id)?.krate();
if let Some(nominal_def) = find_node_at_offset::<ast::AdtDef>(&syntax, position.offset) {
return Some(RangeInfo::new(
nominal_def.syntax().text_range(),
impls_for_def(&sema, &nominal_def, krate)?,
));
} else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) {
return Some(RangeInfo::new(
trait_def.syntax().text_range(),
impls_for_trait(&sema, &trait_def, krate)?,
));
}
None
}
fn impls_for_def(
sema: &Semantics<RootDatabase>,
node: &ast::AdtDef,
krate: Crate,
) -> Option<Vec<NavigationTarget>> {
let ty = match node {
ast::AdtDef::StructDef(def) => sema.to_def(def)?.ty(sema.db),
ast::AdtDef::EnumDef(def) => sema.to_def(def)?.ty(sema.db),
ast::AdtDef::UnionDef(def) => sema.to_def(def)?.ty(sema.db),
};
let impls = ImplDef::all_in_crate(sema.db, krate);
Some(
impls
.into_iter()
.filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db)))
.map(|imp| imp.to_nav(sema.db))
.collect(),
)
}
fn impls_for_trait(
sema: &Semantics<RootDatabase>,
node: &ast::TraitDef,
krate: Crate,
) -> Option<Vec<NavigationTarget>> {
let tr = sema.to_def(node)?;
let impls = ImplDef::for_trait(sema.db, krate, tr);
Some(impls.into_iter().map(|imp| imp.to_nav(sema.db)).collect())
}
#[cfg(test)]
mod tests {
use ra_db::FileRange;
use crate::mock_analysis::MockAnalysis;
fn check(ra_fixture: &str) {
let (mock, position) = MockAnalysis::with_files_and_position(ra_fixture);
let annotations = mock.annotations();
let analysis = mock.analysis();
let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
let key = |frange: &FileRange| (frange.file_id, frange.range.start());
let mut expected = annotations
.into_iter()
.map(|(range, data)| {
assert!(data.is_empty());
range
})
.collect::<Vec<_>>();
expected.sort_by_key(key);
let mut actual = navs
.into_iter()
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
.collect::<Vec<_>>();
actual.sort_by_key(key);
assert_eq!(expected, actual);
}
#[test]
fn goto_implementation_works() {
check(
r#"
struct Foo<|>;
impl Foo {}
//^^^
"#,
);
}
#[test]
fn goto_implementation_works_multiple_blocks() {
check(
r#"
struct Foo<|>;
impl Foo {}
//^^^
impl Foo {}
//^^^
"#,
);
}
#[test]
fn goto_implementation_works_multiple_mods() {
check(
r#"
struct Foo<|>;
mod a {
impl super::Foo {}
//^^^^^^^^^^
}
mod b {
impl super::Foo {}
//^^^^^^^^^^
}
"#,
);
}
#[test]
fn goto_implementation_works_multiple_files() {
check(
r#"
//- /lib.rs
struct Foo<|>;
mod a;
mod b;
//- /a.rs
impl crate::Foo {}
//^^^^^^^^^^
//- /b.rs
impl crate::Foo {}
//^^^^^^^^^^
"#,
);
}
#[test]
fn goto_implementation_for_trait() {
check(
r#"
trait T<|> {}
struct Foo;
impl T for Foo {}
//^^^
"#,
);
}
#[test]
fn goto_implementation_for_trait_multiple_files() {
check(
r#"
//- /lib.rs
trait T<|> {};
struct Foo;
mod a;
mod b;
//- /a.rs
impl crate::T for crate::Foo {}
//^^^^^^^^^^
//- /b.rs
impl crate::T for crate::Foo {}
//^^^^^^^^^^
"#,
);
}
#[test]
fn goto_implementation_all_impls() {
check(
r#"
//- /lib.rs
trait T {}
struct Foo<|>;
impl Foo {}
//^^^
impl T for Foo {}
//^^^
impl T for &Foo {}
//^^^^
"#,
);
}
#[test]
fn goto_implementation_to_builtin_derive() {
check(
r#"
#[derive(Copy)]
//^^^^^^^^^^^^^^^
struct Foo<|>;
mod marker {
trait Copy {}
}
"#,
);
}
}
|