ide/
goto_type_definition.rs

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use hir::GenericParam;
use ide_db::{base_db::Upcast, defs::Definition, helpers::pick_best_token, RootDatabase};
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, T};

use crate::{FilePosition, NavigationTarget, RangeInfo, TryToNav};

// Feature: Go to Type Definition
//
// Navigates to the type of an identifier.
//
// |===
// | Editor  | Action Name
//
// | VS Code | **Go to Type Definition**
// |===
//
// image::https://user-images.githubusercontent.com/48062697/113020657-b560f500-917a-11eb-9007-0f809733a338.gif[]
pub(crate) fn goto_type_definition(
    db: &RootDatabase,
    FilePosition { file_id, offset }: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
    let sema = hir::Semantics::new(db);

    let file: ast::SourceFile = sema.parse_guess_edition(file_id);
    let token: SyntaxToken =
        pick_best_token(file.syntax().token_at_offset(offset), |kind| match kind {
            IDENT | INT_NUMBER | T![self] => 2,
            kind if kind.is_trivia() => 0,
            _ => 1,
        })?;

    let mut res = Vec::new();
    let mut push = |def: Definition| {
        if let Some(navs) = def.try_to_nav(db) {
            for nav in navs {
                if !res.contains(&nav) {
                    res.push(nav);
                }
            }
        }
    };
    let mut process_ty = |ty: hir::Type| {
        // collect from each `ty` into the `res` result vec
        let ty = ty.strip_references();
        ty.walk(db, |t| {
            if let Some(adt) = t.as_adt() {
                push(adt.into());
            } else if let Some(trait_) = t.as_dyn_trait() {
                push(trait_.into());
            } else if let Some(traits) = t.as_impl_traits(db) {
                traits.for_each(|it| push(it.into()));
            } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
                push(trait_.into());
            }
        });
    };
    if let Some((range, resolution)) = sema.check_for_format_args_template(token.clone(), offset) {
        if let Some(ty) = resolution.and_then(|res| match Definition::from(res) {
            Definition::Const(it) => Some(it.ty(db)),
            Definition::Static(it) => Some(it.ty(db)),
            Definition::GenericParam(GenericParam::ConstParam(it)) => Some(it.ty(db)),
            Definition::Local(it) => Some(it.ty(db)),
            Definition::Adt(hir::Adt::Struct(it)) => Some(it.ty(db)),
            _ => None,
        }) {
            process_ty(ty);
        }
        return Some(RangeInfo::new(range, res));
    }

    let range = token.text_range();
    sema.descend_into_macros_no_opaque(token)
        .into_iter()
        .filter_map(|token| {
            let ty = sema
                .token_ancestors_with_macros(token)
                // When `token` is within a macro call, we can't determine its type. Don't continue
                // this traversal because otherwise we'll end up returning the type of *that* macro
                // call, which is not what we want in general.
                //
                // Macro calls always wrap `TokenTree`s, so it's sufficient and efficient to test
                // if the current node is a `TokenTree`.
                .take_while(|node| !ast::TokenTree::can_cast(node.kind()))
                .find_map(|node| {
                    let ty = match_ast! {
                        match node {
                            ast::Expr(it) => sema.type_of_expr(&it)?.original,
                            ast::Pat(it) => sema.type_of_pat(&it)?.original,
                            ast::SelfParam(it) => sema.type_of_self(&it)?,
                            ast::Type(it) => sema.resolve_type(&it)?,
                            ast::RecordField(it) => sema.to_def(&it)?.ty(db.upcast()),
                            // can't match on RecordExprField directly as `ast::Expr` will match an iteration too early otherwise
                            ast::NameRef(it) => {
                                if let Some(record_field) = ast::RecordExprField::for_name_ref(&it) {
                                    let (_, _, ty) = sema.resolve_record_field(&record_field)?;
                                    ty
                                } else {
                                    let record_field = ast::RecordPatField::for_field_name_ref(&it)?;
                                    sema.resolve_record_pat_field(&record_field)?.1
                                }
                            },
                            _ => return None,
                        }
                    };

                    Some(ty)
                });
            ty
        })
        .for_each(process_ty);
    Some(RangeInfo::new(range, res))
}

#[cfg(test)]
mod tests {
    use ide_db::FileRange;
    use itertools::Itertools;

    use crate::fixture;

    fn check(ra_fixture: &str) {
        let (analysis, position, expected) = fixture::annotations(ra_fixture);
        let navs = analysis.goto_type_definition(position).unwrap().unwrap().info;
        assert!(!navs.is_empty(), "navigation is empty");

        let cmp = |&FileRange { file_id, range }: &_| (file_id, range.start());
        let navs = navs
            .into_iter()
            .map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
            .sorted_by_key(cmp)
            .collect::<Vec<_>>();
        let expected = expected
            .into_iter()
            .map(|(file_range, _)| file_range)
            .sorted_by_key(cmp)
            .collect::<Vec<_>>();
        assert_eq!(expected, navs);
    }

    #[test]
    fn goto_type_definition_works_simple() {
        check(
            r#"
struct Foo;
     //^^^
fn foo() {
    let f: Foo; f$0
}
"#,
        );
    }

    #[test]
    fn goto_type_definition_record_expr_field() {
        check(
            r#"
struct Bar;
    // ^^^
struct Foo { foo: Bar }
fn foo() {
    Foo { foo$0 }
}
"#,
        );
        check(
            r#"
struct Bar;
    // ^^^
struct Foo { foo: Bar }
fn foo() {
    Foo { foo$0: Bar }
}
"#,
        );
    }

    #[test]
    fn goto_type_definition_record_pat_field() {
        check(
            r#"
struct Bar;
    // ^^^
struct Foo { foo: Bar }
fn foo() {
    let Foo { foo$0 };
}
"#,
        );
        check(
            r#"
struct Bar;
    // ^^^
struct Foo { foo: Bar }
fn foo() {
    let Foo { foo$0: bar };
}
"#,
        );
    }

    #[test]
    fn goto_type_definition_works_simple_ref() {
        check(
            r#"
struct Foo;
     //^^^
fn foo() {
    let f: &Foo; f$0
}
"#,
        );
    }

    #[test]
    fn goto_type_definition_works_through_macro() {
        check(
            r#"
macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
struct Foo {}
     //^^^
id! {
    fn bar() { let f$0 = Foo {}; }
}
"#,
        );
    }

    #[test]
    fn dont_collect_type_from_token_in_macro_call() {
        check(
            r#"
struct DontCollectMe;
struct S;
     //^

macro_rules! inner {
    ($t:tt) => { DontCollectMe }
}
macro_rules! m {
    ($t:ident) => {
        match $t {
            _ => inner!($t);
        }
    }
}

fn test() {
    m!($0S);
}
"#,
        );
    }

    #[test]
    fn goto_type_definition_for_param() {
        check(
            r#"
struct Foo;
     //^^^
fn foo($0f: Foo) {}
"#,
        );
    }

    #[test]
    fn goto_type_definition_for_tuple_field() {
        check(
            r#"
struct Foo;
     //^^^
struct Bar(Foo);
fn foo() {
    let bar = Bar(Foo);
    bar.$00;
}
"#,
        );
    }

    #[test]
    fn goto_def_for_self_param() {
        check(
            r#"
struct Foo;
     //^^^
impl Foo {
    fn f(&self$0) {}
}
"#,
        )
    }

    #[test]
    fn goto_def_for_type_fallback() {
        check(
            r#"
struct Foo;
     //^^^
impl Foo$0 {}
"#,
        )
    }

    #[test]
    fn goto_def_for_struct_field() {
        check(
            r#"
struct Bar;
     //^^^

struct Foo {
    bar$0: Bar,
}
"#,
        );
    }

    #[test]
    fn goto_def_for_enum_struct_field() {
        check(
            r#"
struct Bar;
     //^^^

enum Foo {
    Bar {
        bar$0: Bar
    },
}
"#,
        );
    }

    #[test]
    fn goto_def_considers_generics() {
        check(
            r#"
struct Foo;
     //^^^
struct Bar<T, U>(T, U);
     //^^^
struct Baz<T>(T);
     //^^^

fn foo(x$0: Bar<Baz<Foo>, Baz<usize>) {}
"#,
        );
    }

    #[test]
    fn implicit_format_args() {
        check(
            r#"
//- minicore: fmt
struct Bar;
    // ^^^
    fn test() {
    let a = Bar;
    format_args!("hello {a$0}");
}
"#,
        );
        check(
            r#"
//- minicore: fmt
struct Bar;
    // ^^^
    fn test() {
    format_args!("hello {Bar$0}");
}
"#,
        );
        check(
            r#"
//- minicore: fmt
struct Bar;
    // ^^^
const BAR: Bar = Bar;
fn test() {
    format_args!("hello {BAR$0}");
}
"#,
        );
    }
}