2021-09-13 13:43:13 +03:00
|
|
|
//! See [`complete_fn_param`].
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2020-08-12 18:26:51 +02:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use syntax::{
|
2021-09-27 12:54:24 +02:00
|
|
|
ast::{self, HasModuleItem},
|
2022-01-04 15:03:46 -03:00
|
|
|
match_ast, AstNode, SyntaxKind,
|
2020-04-09 23:02:10 +02:00
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2021-08-14 18:18:18 +02:00
|
|
|
use crate::{
|
2021-08-14 19:06:35 +02:00
|
|
|
context::{ParamKind, PatternContext},
|
2021-10-27 17:18:42 +02:00
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, Completions,
|
2021-08-14 18:18:18 +02:00
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-02-11 17:18:27 +01:00
|
|
|
/// Complete repeated parameters, both name and type. For example, if all
|
2019-01-08 22:33:36 +03:00
|
|
|
/// 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.
|
2021-07-10 15:12:16 +05:30
|
|
|
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
2022-01-04 15:03:46 -03:00
|
|
|
let param_of_fn =
|
|
|
|
matches!(ctx.pattern_ctx, Some(PatternContext { is_param: Some(ParamKind::Function), .. }));
|
|
|
|
|
|
|
|
if !param_of_fn {
|
2021-07-10 15:12:16 +05:30
|
|
|
return None;
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2022-01-04 15:03:46 -03:00
|
|
|
let mut file_params = FxHashMap::default();
|
2020-07-30 11:42:51 +02:00
|
|
|
|
2022-01-04 15:03:46 -03:00
|
|
|
let mut extract_params = |f: ast::Fn| {
|
|
|
|
f.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
|
2021-03-03 21:58:48 +01:00
|
|
|
if let Some(pat) = param.pat() {
|
2021-11-05 12:30:39 +01:00
|
|
|
// FIXME: We should be able to turn these into SmolStr without having to allocate a String
|
2022-01-04 15:03:46 -03:00
|
|
|
let whole_param = param.syntax().text().to_string();
|
|
|
|
let binding = pat.syntax().text().to_string();
|
|
|
|
file_params.entry(whole_param).or_insert(binding);
|
2021-03-03 21:58:48 +01:00
|
|
|
}
|
|
|
|
});
|
2020-07-30 11:42:51 +02:00
|
|
|
};
|
|
|
|
|
2021-01-30 18:19:21 +03:00
|
|
|
for node in ctx.token.ancestors() {
|
2020-07-30 11:42:51 +02:00
|
|
|
match_ast! {
|
2019-10-05 17:03:03 +03:00
|
|
|
match node {
|
2020-07-30 11:42:51 +02:00
|
|
|
ast::SourceFile(it) => it.items().filter_map(|item| match item {
|
2020-07-30 14:51:08 +02:00
|
|
|
ast::Item::Fn(it) => Some(it),
|
2020-07-30 11:42:51 +02:00
|
|
|
_ => None,
|
2022-01-04 15:03:46 -03:00
|
|
|
}).for_each(&mut extract_params),
|
2020-07-30 11:42:51 +02:00
|
|
|
ast::ItemList(it) => it.items().filter_map(|item| match item {
|
2020-07-30 14:51:08 +02:00
|
|
|
ast::Item::Fn(it) => Some(it),
|
2020-07-30 11:42:51 +02:00
|
|
|
_ => None,
|
2022-01-04 15:03:46 -03:00
|
|
|
}).for_each(&mut extract_params),
|
2020-07-30 11:42:51 +02:00
|
|
|
ast::AssocItemList(it) => it.assoc_items().filter_map(|item| match item {
|
2020-07-30 14:51:08 +02:00
|
|
|
ast::AssocItem::Fn(it) => Some(it),
|
2020-07-30 11:42:51 +02:00
|
|
|
_ => None,
|
2022-01-04 15:03:46 -03:00
|
|
|
}).for_each(&mut extract_params),
|
2020-04-09 23:02:10 +02:00
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2020-07-10 16:29:14 +02:00
|
|
|
|
2022-01-04 15:03:46 -03:00
|
|
|
let function = ctx.token.ancestors().find_map(ast::Fn::cast)?;
|
|
|
|
let param_list = function.param_list()?;
|
|
|
|
|
|
|
|
remove_duplicated(&mut file_params, param_list.params())?;
|
|
|
|
|
2021-07-10 15:12:16 +05:30
|
|
|
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
|
2022-01-04 15:03:46 -03:00
|
|
|
if should_add_self_completions(ctx, param_list) {
|
2021-11-05 12:30:39 +01:00
|
|
|
self_completion_items.into_iter().for_each(|self_item| {
|
2021-07-10 15:12:16 +05:30
|
|
|
add_new_item_to_acc(ctx, acc, self_item.to_string(), self_item.to_string())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-04 15:03:46 -03:00
|
|
|
file_params.into_iter().try_for_each(|(whole_param, binding)| {
|
|
|
|
Some(add_new_item_to_acc(ctx, acc, surround_with_commas(ctx, whole_param)?, binding))
|
|
|
|
})?;
|
2021-07-10 15:12:16 +05:30
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-01-04 15:03:46 -03:00
|
|
|
fn remove_duplicated(
|
|
|
|
file_params: &mut FxHashMap<String, String>,
|
|
|
|
mut fn_params: ast::AstChildren<ast::Param>,
|
|
|
|
) -> Option<()> {
|
|
|
|
fn_params.try_for_each(|param| {
|
|
|
|
let whole_param = param.syntax().text().to_string();
|
|
|
|
file_params.remove(&whole_param);
|
|
|
|
|
|
|
|
let binding = param.pat()?.syntax().text().to_string();
|
|
|
|
|
|
|
|
file_params.retain(|_, v| v != &binding);
|
|
|
|
Some(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_add_self_completions(ctx: &CompletionContext, param_list: ast::ParamList) -> bool {
|
|
|
|
let inside_impl = ctx.impl_def.is_some();
|
|
|
|
let no_params = param_list.params().next().is_none() && param_list.self_param().is_none();
|
|
|
|
|
|
|
|
inside_impl && no_params
|
|
|
|
}
|
|
|
|
|
|
|
|
fn surround_with_commas(ctx: &CompletionContext, param: String) -> Option<String> {
|
2022-01-04 15:30:30 -03:00
|
|
|
let next_token = {
|
|
|
|
let t = ctx.token.next_token()?;
|
|
|
|
if !matches!(t.kind(), SyntaxKind::WHITESPACE) {
|
|
|
|
t
|
|
|
|
} else {
|
|
|
|
t.next_token()?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let trailing_comma_missing = matches!(next_token.kind(), SyntaxKind::IDENT);
|
|
|
|
let trailing = if trailing_comma_missing { "," } else { "" };
|
2022-01-04 15:03:46 -03:00
|
|
|
|
|
|
|
let previous_token = if matches!(ctx.token.kind(), SyntaxKind::IDENT | SyntaxKind::WHITESPACE) {
|
|
|
|
ctx.previous_token.as_ref()?
|
|
|
|
} else {
|
|
|
|
&ctx.token
|
|
|
|
};
|
|
|
|
|
|
|
|
let needs_leading = !matches!(previous_token.kind(), SyntaxKind::L_PAREN | SyntaxKind::COMMA);
|
|
|
|
let leading = if needs_leading { ", " } else { "" };
|
|
|
|
|
|
|
|
Some(format!("{}{}{}", leading, param, trailing))
|
|
|
|
}
|
|
|
|
|
2021-07-10 15:12:16 +05:30
|
|
|
fn add_new_item_to_acc(
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
acc: &mut Completions,
|
|
|
|
label: String,
|
|
|
|
lookup: String,
|
|
|
|
) {
|
2021-10-27 17:18:42 +02:00
|
|
|
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label);
|
|
|
|
item.lookup_by(lookup);
|
2021-07-10 15:12:16 +05:30
|
|
|
item.add_to(acc)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|