aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_fn_param.rs
blob: 0887ef1f687f5eb276753061e3f91c7ba2ee3b15 (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
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
use ra_syntax::{
    algo::visit::{visitor_ctx, VisitorCtx},
    ast, AstNode,
};
use rustc_hash::FxHashMap;

use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};

/// Complete repeated parameters, both name and type. For example, if all
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
/// suggested.
pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
    if !ctx.is_param {
        return;
    }

    let mut params = FxHashMap::default();
    for node in ctx.token.parent().ancestors() {
        let _ = visitor_ctx(&mut params)
            .visit::<ast::SourceFile, _>(process)
            .visit::<ast::ItemList, _>(process)
            .accept(&node);
    }
    params
        .into_iter()
        .filter_map(|(label, (count, param))| {
            let lookup = param.pat()?.syntax().text().to_string();
            if count < 2 {
                None
            } else {
                Some((label, lookup))
            }
        })
        .for_each(|(label, lookup)| {
            CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
                .lookup_by(lookup)
                .add_to(acc)
        });

    fn process<N: ast::FnDefOwner>(node: N, params: &mut FxHashMap<String, (u32, ast::Param)>) {
        node.functions().filter_map(|it| it.param_list()).flat_map(|it| it.params()).for_each(
            |param| {
                let text = param.syntax().text().to_string();
                params.entry(text).or_insert((0, param)).0 += 1;
            },
        )
    }
}

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

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

    #[test]
    fn test_param_completion_last_param() {
        assert_debug_snapshot_matches!(
        do_magic_completion(
                r"
                fn foo(file_id: FileId) {}
                fn bar(file_id: FileId) {}
                fn baz(file<|>) {}
                ",
        ),
            @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "file_id: FileId",
       ⋮        source_range: [110; 114),
       ⋮        delete: [110; 114),
       ⋮        insert: "file_id: FileId",
       ⋮        lookup: "file_id",
       ⋮    },
       ⋮]
        "###
        );
    }

    #[test]
    fn test_param_completion_nth_param() {
        assert_debug_snapshot_matches!(
        do_magic_completion(
                r"
                fn foo(file_id: FileId) {}
                fn bar(file_id: FileId) {}
                fn baz(file<|>, x: i32) {}
                ",
        ),
            @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "file_id: FileId",
       ⋮        source_range: [110; 114),
       ⋮        delete: [110; 114),
       ⋮        insert: "file_id: FileId",
       ⋮        lookup: "file_id",
       ⋮    },
       ⋮]
        "###
        );
    }

    #[test]
    fn test_param_completion_trait_param() {
        assert_debug_snapshot_matches!(
        do_magic_completion(
                r"
                pub(crate) trait SourceRoot {
                    pub fn contains(&self, file_id: FileId) -> bool;
                    pub fn module_map(&self) -> &ModuleMap;
                    pub fn lines(&self, file_id: FileId) -> &LineIndex;
                    pub fn syntax(&self, file<|>)
                }
                ",
        ),
            @r###"
       ⋮[
       ⋮    CompletionItem {
       ⋮        label: "file_id: FileId",
       ⋮        source_range: [289; 293),
       ⋮        delete: [289; 293),
       ⋮        insert: "file_id: FileId",
       ⋮        lookup: "file_id",
       ⋮    },
       ⋮]
        "###
        );
    }
}