mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
Merge #9548
9548: add: Adding self keyword completion in complete_fn_param r=lnicola a=feniljain Solves #9522 I haven't added Arc<self> for now as there were some conflicting opinions on it Co-authored-by: vi_mi <fenil.jain2018@vitstudent.ac.in>
This commit is contained in:
commit
637dbb26a7
1 changed files with 45 additions and 7 deletions
|
@ -12,9 +12,9 @@ use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKin
|
||||||
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
|
/// 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
|
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
|
||||||
/// suggested.
|
/// suggested.
|
||||||
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
||||||
if !ctx.is_param {
|
if !ctx.is_param {
|
||||||
return;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut params = FxHashMap::default();
|
let mut params = FxHashMap::default();
|
||||||
|
@ -53,11 +53,27 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
params.into_iter().for_each(|(label, lookup)| {
|
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
|
||||||
let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
|
if ctx.impl_def.is_some() && me?.param_list()?.params().next().is_none() {
|
||||||
item.kind(CompletionItemKind::Binding).lookup_by(lookup);
|
self_completion_items.iter().for_each(|self_item| {
|
||||||
item.add_to(acc)
|
add_new_item_to_acc(ctx, acc, self_item.to_string(), self_item.to_string())
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
params.into_iter().for_each(|(label, lookup)| add_new_item_to_acc(ctx, acc, label, lookup));
|
||||||
|
|
||||||
|
Some(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_new_item_to_acc(
|
||||||
|
ctx: &CompletionContext,
|
||||||
|
acc: &mut Completions,
|
||||||
|
label: String,
|
||||||
|
lookup: String,
|
||||||
|
) {
|
||||||
|
let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
|
||||||
|
item.kind(CompletionItemKind::Binding).lookup_by(lookup);
|
||||||
|
item.add_to(acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -143,4 +159,26 @@ fn foo2($0) {}
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_param_completion_self_param() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
struct A {}
|
||||||
|
|
||||||
|
impl A {
|
||||||
|
fn foo(file_id: FileId) {}
|
||||||
|
fn new($0) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
bn self
|
||||||
|
bn &self
|
||||||
|
bn mut self
|
||||||
|
bn &mut self
|
||||||
|
bn file_id: FileId
|
||||||
|
"#]],
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue