ide_completion/completions/
fn_param.rs

1//! See [`complete_fn_param`].
2
3use hir::HirDisplay;
4use ide_db::FxHashMap;
5use itertools::Either;
6use syntax::{
7    AstNode, Direction, SmolStr, SyntaxKind, TextRange, TextSize, ToSmolStr, algo,
8    ast::{self, HasModuleItem},
9    format_smolstr, match_ast,
10};
11
12use crate::{
13    CompletionContext, CompletionItem, CompletionItemKind, Completions,
14    context::{ParamContext, ParamKind, PatternContext},
15};
16
17// FIXME: Make this a submodule of [`pattern`]
18/// Complete repeated parameters, both name and type. For example, if all
19/// functions in a file have a `spam: &mut Spam` parameter, a completion with
20/// `spam: &mut Spam` insert text/label will be suggested.
21///
22/// Also complete parameters for closure or local functions from the surrounding defined locals.
23pub(crate) fn complete_fn_param(
24    acc: &mut Completions,
25    ctx: &CompletionContext<'_>,
26    pattern_ctx: &PatternContext,
27) -> Option<()> {
28    let (ParamContext { param_list, kind, param, .. }, impl_or_trait) = match pattern_ctx {
29        PatternContext { param_ctx: Some(kind), impl_or_trait, .. } => (kind, impl_or_trait),
30        _ => return None,
31    };
32
33    let comma_wrapper = comma_wrapper(ctx);
34    let mut add_new_item_to_acc = |label: &str| {
35        let mk_item = |label: &str, range: TextRange| {
36            CompletionItem::new(CompletionItemKind::Binding, range, label, ctx.edition)
37        };
38        let item = match &comma_wrapper {
39            Some((fmt, range)) => mk_item(&fmt(label), *range),
40            None => mk_item(label, ctx.source_range()),
41        };
42        // Completion lookup is omitted intentionally here.
43        // See the full discussion: https://github.com/rust-lang/rust-analyzer/issues/12073
44        item.add_to(acc, ctx.db)
45    };
46
47    match kind {
48        ParamKind::Function(function) => {
49            fill_fn_params(ctx, function, param_list, param, impl_or_trait, add_new_item_to_acc);
50        }
51        ParamKind::Closure(closure) => {
52            if is_simple_param(param) {
53                let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?;
54                params_from_stmt_list_scope(ctx, stmt_list, |name, ty| {
55                    add_new_item_to_acc(&format_smolstr!(
56                        "{}: {ty}",
57                        name.display(ctx.db, ctx.edition)
58                    ));
59                });
60            }
61        }
62    }
63
64    Some(())
65}
66
67fn fill_fn_params(
68    ctx: &CompletionContext<'_>,
69    function: &ast::Fn,
70    param_list: &ast::ParamList,
71    current_param: &ast::Param,
72    impl_or_trait: &Option<Either<ast::Impl, ast::Trait>>,
73    mut add_new_item_to_acc: impl FnMut(&str),
74) {
75    let mut file_params = FxHashMap::default();
76
77    let mut extract_params = |f: ast::Fn| {
78        if !is_simple_param(current_param) {
79            return;
80        }
81        f.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
82            if let Some(pat) = param.pat() {
83                let whole_param = param.to_smolstr();
84                let binding = pat.to_smolstr();
85                file_params.entry(whole_param).or_insert(binding);
86            }
87        });
88    };
89
90    for node in ctx.token.parent_ancestors() {
91        match_ast! {
92            match node {
93                ast::SourceFile(it) => it.items().filter_map(|item| match item {
94                    ast::Item::Fn(it) => Some(it),
95                    _ => None,
96                }).for_each(&mut extract_params),
97                ast::ItemList(it) => it.items().filter_map(|item| match item {
98                    ast::Item::Fn(it) => Some(it),
99                    _ => None,
100                }).for_each(&mut extract_params),
101                ast::AssocItemList(it) => it.assoc_items().filter_map(|item| match item {
102                    ast::AssocItem::Fn(it) => Some(it),
103                    _ => None,
104                }).for_each(&mut extract_params),
105                _ => continue,
106            }
107        };
108    }
109
110    if let Some(stmt_list) = function.syntax().parent().and_then(ast::StmtList::cast)
111        && is_simple_param(current_param)
112    {
113        params_from_stmt_list_scope(ctx, stmt_list, |name, ty| {
114            file_params
115                .entry(format_smolstr!("{}: {ty}", name.display(ctx.db, ctx.edition)))
116                .or_insert(name.display(ctx.db, ctx.edition).to_smolstr());
117        });
118    }
119    remove_duplicated(&mut file_params, param_list.params());
120    let self_completion_items = ["self", "&self", "mut self", "&mut self"];
121    if should_add_self_completions(ctx.token.text_range().start(), param_list, impl_or_trait) {
122        self_completion_items.into_iter().for_each(&mut add_new_item_to_acc);
123    }
124
125    file_params.keys().for_each(|whole_param| add_new_item_to_acc(whole_param));
126}
127
128fn params_from_stmt_list_scope(
129    ctx: &CompletionContext<'_>,
130    stmt_list: ast::StmtList,
131    mut cb: impl FnMut(hir::Name, String),
132) {
133    let syntax_node = match stmt_list.syntax().last_child() {
134        Some(it) => it,
135        None => return,
136    };
137    if let Some(scope) =
138        ctx.sema.scope_at_offset(stmt_list.syntax(), syntax_node.text_range().end())
139    {
140        let module = scope.module().into();
141        scope.process_all_names(&mut |name, def| {
142            if let hir::ScopeDef::Local(local) = def
143                && let Ok(ty) = local.ty(ctx.db).display_source_code(ctx.db, module, true)
144            {
145                cb(name, ty);
146            }
147        });
148    }
149}
150
151fn remove_duplicated(
152    file_params: &mut FxHashMap<SmolStr, SmolStr>,
153    fn_params: ast::AstChildren<ast::Param>,
154) {
155    fn_params.for_each(|param| {
156        let whole_param = param.to_smolstr();
157        file_params.remove(&whole_param);
158
159        match param.pat() {
160            // remove suggestions for patterns that already exist
161            // if the type is missing we are checking the current param to be completed
162            // in which case this would find itself removing the suggestions due to itself
163            Some(pattern) if param.ty().is_some() => {
164                let binding = pattern.to_smolstr();
165                file_params.retain(|_, v| v != &binding);
166            }
167            _ => (),
168        }
169    })
170}
171
172fn should_add_self_completions(
173    cursor: TextSize,
174    param_list: &ast::ParamList,
175    impl_or_trait: &Option<Either<ast::Impl, ast::Trait>>,
176) -> bool {
177    if impl_or_trait.is_none() || param_list.self_param().is_some() {
178        return false;
179    }
180    match param_list.params().next() {
181        Some(first) => first.pat().is_some_and(|pat| pat.syntax().text_range().contains(cursor)),
182        None => true,
183    }
184}
185
186fn comma_wrapper(ctx: &CompletionContext<'_>) -> Option<(impl Fn(&str) -> SmolStr, TextRange)> {
187    let param =
188        ctx.original_token.parent_ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
189
190    let next_token_kind = {
191        let t = param.last_token()?.next_token()?;
192        let t = algo::skip_whitespace_token(t, Direction::Next)?;
193        t.kind()
194    };
195    let prev_token_kind = {
196        let t = param.first_token()?.prev_token()?;
197        let t = algo::skip_whitespace_token(t, Direction::Prev)?;
198        t.kind()
199    };
200
201    let has_trailing_comma =
202        matches!(next_token_kind, SyntaxKind::COMMA | SyntaxKind::R_PAREN | SyntaxKind::PIPE);
203    let trailing = if has_trailing_comma { "" } else { "," };
204
205    let has_leading_comma =
206        matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
207    let leading = if has_leading_comma { "" } else { ", " };
208
209    Some((move |label: &_| format_smolstr!("{leading}{label}{trailing}"), param.text_range()))
210}
211
212fn is_simple_param(param: &ast::Param) -> bool {
213    param
214        .pat()
215        .is_none_or(|pat| matches!(pat, ast::Pat::IdentPat(ident_pat) if ident_pat.pat().is_none()))
216}