mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #6688
6688: Place cursor correctly when completing assoc fns with self r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
75e037fcf7
6 changed files with 87 additions and 43 deletions
|
@ -353,10 +353,10 @@ impl S {
|
||||||
fn foo() { let _ = S::<|> }
|
fn foo() { let _ = S::<|> }
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
ct C const C: i32 = 42;
|
ct C const C: i32 = 42;
|
||||||
ta T type T = i32;
|
ta T type T = i32;
|
||||||
fn a() fn a()
|
fn a() fn a()
|
||||||
me b() fn b(&self)
|
me b(…) fn b(&self)
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -503,14 +503,14 @@ trait Sub: Super {
|
||||||
fn foo<T: Sub>() { T::<|> }
|
fn foo<T: Sub>() { T::<|> }
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
ct C2 const C2: ();
|
ct C2 const C2: ();
|
||||||
ct CONST const CONST: u8;
|
ct CONST const CONST: u8;
|
||||||
ta SubTy type SubTy;
|
ta SubTy type SubTy;
|
||||||
ta Ty type Ty;
|
ta Ty type Ty;
|
||||||
fn func() fn func()
|
fn func() fn func()
|
||||||
me method() fn method(&self)
|
me method(…) fn method(&self)
|
||||||
fn subfunc() fn subfunc()
|
fn subfunc() fn subfunc()
|
||||||
me submethod() fn submethod(&self)
|
me submethod(…) fn submethod(&self)
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -543,14 +543,14 @@ impl<T> Sub for Wrap<T> {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
ct C2 const C2: () = ();
|
ct C2 const C2: () = ();
|
||||||
ct CONST const CONST: u8 = 0;
|
ct CONST const CONST: u8 = 0;
|
||||||
ta SubTy type SubTy;
|
ta SubTy type SubTy;
|
||||||
ta Ty type Ty;
|
ta Ty type Ty;
|
||||||
fn func() fn func()
|
fn func() fn func()
|
||||||
me method() fn method(&self)
|
me method(…) fn method(&self)
|
||||||
fn subfunc() fn subfunc()
|
fn subfunc() fn subfunc()
|
||||||
me submethod() fn submethod(&self)
|
me submethod(…) fn submethod(&self)
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,7 @@ fn add_function_impl(
|
||||||
) {
|
) {
|
||||||
let fn_name = func.name(ctx.db).to_string();
|
let fn_name = func.name(ctx.db).to_string();
|
||||||
|
|
||||||
let label = if func.params(ctx.db).is_empty() {
|
let label = if func.assoc_fn_params(ctx.db).is_empty() {
|
||||||
format!("fn {}()", fn_name)
|
format!("fn {}()", fn_name)
|
||||||
} else {
|
} else {
|
||||||
format!("fn {}(..)", fn_name)
|
format!("fn {}(..)", fn_name)
|
||||||
|
|
|
@ -5,6 +5,7 @@ use test_utils::mark;
|
||||||
|
|
||||||
use crate::{item::Builder, CompletionContext};
|
use crate::{item::Builder, CompletionContext};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub(super) enum Params {
|
pub(super) enum Params {
|
||||||
Named(Vec<String>),
|
Named(Vec<String>),
|
||||||
Anonymous(usize),
|
Anonymous(usize),
|
||||||
|
@ -24,7 +25,7 @@ impl Params {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
pub(super) fn should_add_parems(&self, ctx: &CompletionContext) -> bool {
|
fn should_add_parens(&self, ctx: &CompletionContext) -> bool {
|
||||||
if !ctx.config.add_call_parenthesis {
|
if !ctx.config.add_call_parenthesis {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -58,7 +59,7 @@ impl Builder {
|
||||||
name: String,
|
name: String,
|
||||||
params: Params,
|
params: Params,
|
||||||
) -> Builder {
|
) -> Builder {
|
||||||
if !self.should_add_parems(ctx) {
|
if !self.should_add_parens(ctx) {
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use hir::{HasSource, Type};
|
use hir::{HasSource, Type};
|
||||||
use syntax::{ast::Fn, display::function_declaration};
|
use syntax::{ast::Fn, display::function_declaration};
|
||||||
|
use test_utils::mark;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
|
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
|
||||||
|
@ -22,7 +23,7 @@ pub(crate) fn render_fn<'a>(
|
||||||
struct FunctionRender<'a> {
|
struct FunctionRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
name: String,
|
name: String,
|
||||||
fn_: hir::Function,
|
func: hir::Function,
|
||||||
ast_node: Fn,
|
ast_node: Fn,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,15 +36,15 @@ impl<'a> FunctionRender<'a> {
|
||||||
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
|
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
|
||||||
let ast_node = fn_.source(ctx.db()).value;
|
let ast_node = fn_.source(ctx.db()).value;
|
||||||
|
|
||||||
FunctionRender { ctx, name, fn_, ast_node }
|
FunctionRender { ctx, name, func: fn_, ast_node }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
|
fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
|
||||||
let params = self.params();
|
let params = self.params();
|
||||||
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
|
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
|
||||||
.kind(self.kind())
|
.kind(self.kind())
|
||||||
.set_documentation(self.ctx.docs(self.fn_))
|
.set_documentation(self.ctx.docs(self.func))
|
||||||
.set_deprecated(self.ctx.is_deprecated(self.fn_))
|
.set_deprecated(self.ctx.is_deprecated(self.func))
|
||||||
.detail(self.detail())
|
.detail(self.detail())
|
||||||
.add_call_parens(self.ctx.completion, self.name, params)
|
.add_call_parens(self.ctx.completion, self.name, params)
|
||||||
.add_import(import_to_add)
|
.add_import(import_to_add)
|
||||||
|
@ -67,27 +68,39 @@ impl<'a> FunctionRender<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn params(&self) -> Params {
|
fn params(&self) -> Params {
|
||||||
let params_ty = self.fn_.params(self.ctx.db());
|
let ast_params = match self.ast_node.param_list() {
|
||||||
let params = self
|
Some(it) => it,
|
||||||
.ast_node
|
None => return Params::Named(Vec::new()),
|
||||||
.param_list()
|
};
|
||||||
|
|
||||||
|
let mut params_pats = Vec::new();
|
||||||
|
let params_ty = if self.ctx.completion.dot_receiver.is_some() {
|
||||||
|
self.func.method_params(self.ctx.db()).unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
if let Some(s) = ast_params.self_param() {
|
||||||
|
mark::hit!(parens_for_method_call_as_assoc_fn);
|
||||||
|
params_pats.push(Some(s.to_string()));
|
||||||
|
}
|
||||||
|
self.func.assoc_fn_params(self.ctx.db())
|
||||||
|
};
|
||||||
|
params_pats
|
||||||
|
.extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string())));
|
||||||
|
|
||||||
|
let params = params_pats
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|it| it.params())
|
|
||||||
.zip(params_ty)
|
.zip(params_ty)
|
||||||
.flat_map(|(it, param_ty)| {
|
.flat_map(|(pat, param_ty)| {
|
||||||
if let Some(pat) = it.pat() {
|
let pat = pat?;
|
||||||
let name = pat.to_string();
|
let name = pat.to_string();
|
||||||
let arg = name.trim_start_matches("mut ").trim_start_matches('_');
|
let arg = name.trim_start_matches("mut ").trim_start_matches('_');
|
||||||
return Some(self.add_arg(arg, param_ty.ty()));
|
Some(self.add_arg(arg, param_ty.ty()))
|
||||||
}
|
|
||||||
None
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Params::Named(params)
|
Params::Named(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kind(&self) -> CompletionItemKind {
|
fn kind(&self) -> CompletionItemKind {
|
||||||
if self.fn_.self_param(self.ctx.db()).is_some() {
|
if self.func.self_param(self.ctx.db()).is_some() {
|
||||||
CompletionItemKind::Method
|
CompletionItemKind::Method
|
||||||
} else {
|
} else {
|
||||||
CompletionItemKind::Function
|
CompletionItemKind::Function
|
||||||
|
@ -172,6 +185,28 @@ fn bar(s: &S) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parens_for_method_call_as_assoc_fn() {
|
||||||
|
mark::check!(parens_for_method_call_as_assoc_fn);
|
||||||
|
check_edit(
|
||||||
|
"foo",
|
||||||
|
r#"
|
||||||
|
struct S;
|
||||||
|
impl S {
|
||||||
|
fn foo(&self) {}
|
||||||
|
}
|
||||||
|
fn main() { S::f<|> }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct S;
|
||||||
|
impl S {
|
||||||
|
fn foo(&self) {}
|
||||||
|
}
|
||||||
|
fn main() { S::foo(${1:&self})$0 }
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn suppress_arg_snippets() {
|
fn suppress_arg_snippets() {
|
||||||
mark::check!(suppress_arg_snippets);
|
mark::check!(suppress_arg_snippets);
|
||||||
|
|
|
@ -744,14 +744,13 @@ impl Function {
|
||||||
Some(SelfParam { func: self.id })
|
Some(SelfParam { func: self.id })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn params(self, db: &dyn HirDatabase) -> Vec<Param> {
|
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {
|
||||||
let resolver = self.id.resolver(db.upcast());
|
let resolver = self.id.resolver(db.upcast());
|
||||||
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
|
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
|
||||||
let environment = TraitEnvironment::lower(db, &resolver);
|
let environment = TraitEnvironment::lower(db, &resolver);
|
||||||
db.function_data(self.id)
|
db.function_data(self.id)
|
||||||
.params
|
.params
|
||||||
.iter()
|
.iter()
|
||||||
.skip(if self.self_param(db).is_some() { 1 } else { 0 })
|
|
||||||
.map(|type_ref| {
|
.map(|type_ref| {
|
||||||
let ty = Type {
|
let ty = Type {
|
||||||
krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
|
krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
|
||||||
|
@ -764,6 +763,14 @@ impl Function {
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> {
|
||||||
|
if self.self_param(db).is_none() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let mut res = self.assoc_fn_params(db);
|
||||||
|
res.remove(0);
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
|
pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
|
||||||
db.function_data(self.id).is_unsafe
|
db.function_data(self.id).is_unsafe
|
||||||
|
@ -799,6 +806,7 @@ impl From<Mutability> for Access {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Param {
|
pub struct Param {
|
||||||
ty: Type,
|
ty: Type,
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,7 +241,7 @@ fn rename_to_self(
|
||||||
return Err(RenameError("Method already has a self parameter".to_string()));
|
return Err(RenameError("Method already has a self parameter".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let params = fn_def.params(sema.db);
|
let params = fn_def.assoc_fn_params(sema.db);
|
||||||
let first_param =
|
let first_param =
|
||||||
params.first().ok_or_else(|| RenameError("Method has no parameters".into()))?;
|
params.first().ok_or_else(|| RenameError("Method has no parameters".into()))?;
|
||||||
let first_param_ty = first_param.ty();
|
let first_param_ty = first_param.ty();
|
||||||
|
|
Loading…
Reference in a new issue