aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_impl_fn.rs
blob: 6d464cc1f282b9c13c6ea8a7fd107ef867d54600 (plain)
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
use crate::completion::{CompletionContext, Completions};

use hir::{ self, db::HirDatabase, HasSource };

use ra_syntax::{ ast, ast::AstNode };

pub(crate) fn complete_impl_fn(acc: &mut Completions, ctx: &CompletionContext) {
    let impl_trait = ast::ItemList::cast(ctx.token.parent())
        .and_then(|item_list| item_list.syntax().parent())
        .and_then(|item_list_parent| ast::ImplBlock::cast(item_list_parent))
        .and_then(|impl_block| resolve_target_trait(ctx.db, &ctx.analyzer, &impl_block));

    if let Some(x) = &impl_trait {
        for trait_item in x.0.items(ctx.db) {
            match trait_item {
                hir::AssocItem::Function(f) => acc.add_function_impl(ctx, f),
                _ => {}
            }
        }
    }
}

fn resolve_target_trait(
    db: &impl HirDatabase,
    analyzer: &hir::SourceAnalyzer,
    impl_block: &ast::ImplBlock
) -> Option<(hir::Trait, ast::TraitDef)> {
    let ast_path = impl_block
        .target_trait()
        .map(|it| it.syntax().clone())
        .and_then(ast::PathType::cast)?
        .path()?;

    match analyzer.resolve_path(db, &ast_path) {
        Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => {
            Some((def, def.source(db).value))
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use crate::completion::{do_completion, CompletionItem, CompletionKind};
    use insta::assert_debug_snapshot;

    fn complete(code: &str) -> Vec<CompletionItem> {
        do_completion(code, CompletionKind::Reference)
    }

    #[test]
    fn single_function() {
        let completions = complete(
            r"
            trait Test {
                fn foo();
            }

            struct T1;

            impl Test for T1 {
                <|>
            }
            ",
        );
        assert_debug_snapshot!(completions, @r###"
        [
            CompletionItem {
                label: "fn foo()",
                source_range: [138; 138),
                delete: [138; 138),
                insert: "fn foo() { $0 }",
                kind: Function,
                lookup: "foo",
            },
        ]
        "###);
    }
}