Skip to content

Commit

Permalink
Auto merge of rust-lang#12773 - Veykril:self-compl, r=Veykril
Browse files Browse the repository at this point in the history
fix: Improve self param completion applicability

Fixes rust-lang/rust-analyzer#9522
  • Loading branch information
bors committed Jul 16, 2022
2 parents d3796ad + b96f8f1 commit 96481b7
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 18 deletions.
26 changes: 17 additions & 9 deletions crates/ide-completion/src/completions/fn_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use ide_db::FxHashMap;
use syntax::{
algo,
ast::{self, HasModuleItem},
match_ast, AstNode, Direction, SyntaxKind, TextRange,
match_ast, AstNode, Direction, SyntaxKind, TextRange, TextSize,
};

use crate::{
context::{ParamKind, PatternContext},
context::{ParamContext, ParamKind, PatternContext},
CompletionContext, CompletionItem, CompletionItemKind, Completions,
};

Expand All @@ -24,7 +24,7 @@ pub(crate) fn complete_fn_param(
ctx: &CompletionContext,
pattern_ctx: &PatternContext,
) -> Option<()> {
let ((param_list, _, param_kind), impl_) = match pattern_ctx {
let (ParamContext { param_list, kind, .. }, impl_) = match pattern_ctx {
PatternContext { param_ctx: Some(kind), impl_, .. } => (kind, impl_),
_ => return None,
};
Expand All @@ -43,7 +43,7 @@ pub(crate) fn complete_fn_param(
item.add_to(acc)
};

match param_kind {
match kind {
ParamKind::Function(function) => {
fill_fn_params(ctx, function, param_list, impl_, add_new_item_to_acc);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ fn fill_fn_params(
}
remove_duplicated(&mut file_params, param_list.params());
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
if should_add_self_completions(param_list, impl_) {
if should_add_self_completions(ctx.token.text_range().start(), param_list, impl_) {
self_completion_items.into_iter().for_each(|self_item| add_new_item_to_acc(self_item));
}

Expand Down Expand Up @@ -156,10 +156,18 @@ fn remove_duplicated(
})
}

fn should_add_self_completions(param_list: &ast::ParamList, impl_: &Option<ast::Impl>) -> bool {
let no_params = param_list.params().next().is_none() && param_list.self_param().is_none();

impl_.is_some() && no_params
fn should_add_self_completions(
cursor: TextSize,
param_list: &ast::ParamList,
impl_: &Option<ast::Impl>,
) -> bool {
if impl_.is_none() || param_list.self_param().is_some() {
return false;
}
match param_list.params().next() {
Some(first) => first.pat().map_or(false, |pat| pat.syntax().text_range().contains(cursor)),
None => true,
}
}

fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange)> {
Expand Down
4 changes: 4 additions & 0 deletions crates/ide-completion/src/completions/item_list/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ impl Test for T {
expect![[r#"
sp Self
st T
bn &mut self
bn &self
bn mut self
bn self
"#]],
);

Expand Down
9 changes: 8 additions & 1 deletion crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub(super) enum Qualified {
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct PatternContext {
pub(super) refutability: PatternRefutability,
pub(super) param_ctx: Option<(ast::ParamList, ast::Param, ParamKind)>,
pub(super) param_ctx: Option<ParamContext>,
pub(super) has_type_ascription: bool,
pub(super) parent_pat: Option<ast::Pat>,
pub(super) ref_token: Option<SyntaxToken>,
Expand All @@ -209,6 +209,13 @@ pub(super) struct PatternContext {
pub(super) impl_: Option<ast::Impl>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ParamContext {
pub(super) param_list: ast::ParamList,
pub(super) param: ast::Param,
pub(super) kind: ParamKind,
}

/// The state of the lifetime we are completing.
#[derive(Debug)]
pub(super) struct LifetimeContext {
Expand Down
15 changes: 9 additions & 6 deletions crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use syntax::{
use crate::context::{
AttrCtx, CompletionAnalysis, CompletionContext, DotAccess, DotAccessKind, ExprCtx,
ItemListKind, LifetimeContext, LifetimeKind, NameContext, NameKind, NameRefContext,
NameRefKind, ParamKind, PathCompletionCtx, PathKind, PatternContext, PatternRefutability,
Qualified, QualifierCtx, TypeAscriptionTarget, TypeLocation, COMPLETION_MARKER,
NameRefKind, ParamContext, ParamKind, PathCompletionCtx, PathKind, PatternContext,
PatternRefutability, Qualified, QualifierCtx, TypeAscriptionTarget, TypeLocation,
COMPLETION_MARKER,
};

impl<'a> CompletionContext<'a> {
Expand Down Expand Up @@ -990,7 +991,7 @@ fn pattern_context_for(
original_file: &SyntaxNode,
pat: ast::Pat,
) -> PatternContext {
let mut is_param = None;
let mut param_ctx = None;
let (refutability, has_type_ascription) =
pat
.syntax()
Expand All @@ -1003,7 +1004,7 @@ fn pattern_context_for(
ast::LetStmt(let_) => return (PatternRefutability::Irrefutable, let_.ty().is_some()),
ast::Param(param) => {
let has_type_ascription = param.ty().is_some();
is_param = (|| {
param_ctx = (|| {
let fake_param_list = param.syntax().parent().and_then(ast::ParamList::cast)?;
let param_list = find_node_in_file_compensated(sema, original_file, &fake_param_list)?;
let param_list_owner = param_list.syntax().parent()?;
Expand All @@ -1014,7 +1015,9 @@ fn pattern_context_for(
_ => return None,
}
};
Some((param_list, param, kind))
Some(ParamContext {
param_list, param, kind
})
})();
return (PatternRefutability::Irrefutable, has_type_ascription)
},
Expand All @@ -1033,7 +1036,7 @@ fn pattern_context_for(

PatternContext {
refutability,
param_ctx: is_param,
param_ctx,
has_type_ascription,
parent_pat: pat.syntax().parent().and_then(ast::Pat::cast),
mut_token,
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-completion/src/render/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;
use syntax::SmolStr;

use crate::{
context::{ParamKind, PatternContext},
context::{ParamContext, ParamKind, PatternContext},
render::{
variant::{format_literal_label, visible_fields},
RenderContext,
Expand Down Expand Up @@ -102,7 +102,7 @@ fn render_pat(
let needs_ascription = matches!(
pattern_ctx,
PatternContext {
param_ctx: Some((.., ParamKind::Function(_))),
param_ctx: Some(ParamContext { kind: ParamKind::Function(_), .. }),
has_type_ascription: false,
..
}
Expand Down
84 changes: 84 additions & 0 deletions crates/ide-completion/src/tests/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,87 @@ fn f(v: u32) {
"#]],
);
}

#[test]
fn in_method_param() {
check_empty(
r#"
struct Ty(u8);
impl Ty {
fn foo($0)
}
"#,
expect![[r#"
sp Self
st Ty
bn &mut self
bn &self
bn Self(…) Self($1): Self$0
bn Ty(…) Ty($1): Ty$0
bn mut self
bn self
kw mut
kw ref
"#]],
);
check_empty(
r#"
struct Ty(u8);
impl Ty {
fn foo(s$0)
}
"#,
expect![[r#"
sp Self
st Ty
bn &mut self
bn &self
bn Self(…) Self($1): Self$0
bn Ty(…) Ty($1): Ty$0
bn mut self
bn self
kw mut
kw ref
"#]],
);
check_empty(
r#"
struct Ty(u8);
impl Ty {
fn foo(s$0, foo: u8)
}
"#,
expect![[r#"
sp Self
st Ty
bn &mut self
bn &self
bn Self(…) Self($1): Self$0
bn Ty(…) Ty($1): Ty$0
bn mut self
bn self
kw mut
kw ref
"#]],
);
check_empty(
r#"
struct Ty(u8);
impl Ty {
fn foo(foo: u8, b$0)
}
"#,
expect![[r#"
sp Self
st Ty
bn Self(…) Self($1): Self$0
bn Ty(…) Ty($1): Ty$0
kw mut
kw ref
"#]],
);
}

0 comments on commit 96481b7

Please sign in to comment.