mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Merge #3840
3840: Add parens for enums r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
6207ac90da
7 changed files with 236 additions and 127 deletions
|
@ -61,7 +61,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T
|
||||||
&& ctx.scope().module().map_or(true, |m| func.is_visible_from(ctx.db, m))
|
&& ctx.scope().module().map_or(true, |m| func.is_visible_from(ctx.db, m))
|
||||||
&& seen_methods.insert(func.name(ctx.db))
|
&& seen_methods.insert(func.name(ctx.db))
|
||||||
{
|
{
|
||||||
acc.add_function(ctx, func);
|
acc.add_function(ctx, func, None);
|
||||||
}
|
}
|
||||||
None::<()>
|
None::<()>
|
||||||
});
|
});
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => {
|
hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => {
|
||||||
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
|
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
|
||||||
for variant in e.variants(ctx.db) {
|
for variant in e.variants(ctx.db) {
|
||||||
acc.add_enum_variant(ctx, variant);
|
acc.add_enum_variant(ctx, variant, None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let ty = match def {
|
let ty = match def {
|
||||||
|
@ -58,7 +58,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
match item {
|
match item {
|
||||||
hir::AssocItem::Function(func) => {
|
hir::AssocItem::Function(func) => {
|
||||||
if !func.has_self_param(ctx.db) {
|
if !func.has_self_param(ctx.db) {
|
||||||
acc.add_function(ctx, func);
|
acc.add_function(ctx, func, None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
|
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
|
||||||
|
@ -87,7 +87,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
match item {
|
match item {
|
||||||
hir::AssocItem::Function(func) => {
|
hir::AssocItem::Function(func) => {
|
||||||
if !func.has_self_param(ctx.db) {
|
if !func.has_self_param(ctx.db) {
|
||||||
acc.add_function(ctx, func);
|
acc.add_function(ctx, func, None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
|
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
|
||||||
|
@ -355,15 +355,17 @@ mod tests {
|
||||||
@r###"
|
@r###"
|
||||||
[
|
[
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "Bar",
|
label: "Bar(…)",
|
||||||
source_range: [116; 116),
|
source_range: [116; 116),
|
||||||
delete: [116; 116),
|
delete: [116; 116),
|
||||||
insert: "Bar",
|
insert: "Bar($0)",
|
||||||
kind: EnumVariant,
|
kind: EnumVariant,
|
||||||
|
lookup: "Bar",
|
||||||
detail: "(i32)",
|
detail: "(i32)",
|
||||||
documentation: Documentation(
|
documentation: Documentation(
|
||||||
"Bar Variant with i32",
|
"Bar Variant with i32",
|
||||||
),
|
),
|
||||||
|
trigger_call_info: true,
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "Foo",
|
label: "Foo",
|
||||||
|
@ -403,15 +405,17 @@ mod tests {
|
||||||
@r###"
|
@r###"
|
||||||
[
|
[
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "Bar",
|
label: "Bar(…)",
|
||||||
source_range: [180; 180),
|
source_range: [180; 180),
|
||||||
delete: [180; 180),
|
delete: [180; 180),
|
||||||
insert: "Bar",
|
insert: "Bar($0)",
|
||||||
kind: EnumVariant,
|
kind: EnumVariant,
|
||||||
|
lookup: "Bar",
|
||||||
detail: "(i32, u32)",
|
detail: "(i32, u32)",
|
||||||
documentation: Documentation(
|
documentation: Documentation(
|
||||||
"Bar Variant with i32 and u32",
|
"Bar Variant with i32 and u32",
|
||||||
),
|
),
|
||||||
|
trigger_call_info: true,
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "Foo",
|
label: "Foo",
|
||||||
|
@ -425,15 +429,17 @@ mod tests {
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "S",
|
label: "S(…)",
|
||||||
source_range: [180; 180),
|
source_range: [180; 180),
|
||||||
delete: [180; 180),
|
delete: [180; 180),
|
||||||
insert: "S",
|
insert: "S($0)",
|
||||||
kind: EnumVariant,
|
kind: EnumVariant,
|
||||||
|
lookup: "S",
|
||||||
detail: "(S)",
|
detail: "(S)",
|
||||||
documentation: Documentation(
|
documentation: Documentation(
|
||||||
"",
|
"",
|
||||||
),
|
),
|
||||||
|
trigger_call_info: true,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
|
|
|
@ -4,23 +4,25 @@ use crate::completion::{CompletionContext, Completions};
|
||||||
|
|
||||||
/// Completes constats and paths in patterns.
|
/// Completes constats and paths in patterns.
|
||||||
pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
if !ctx.is_pat_binding {
|
if !ctx.is_pat_binding_or_const {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// FIXME: ideally, we should look at the type we are matching against and
|
// FIXME: ideally, we should look at the type we are matching against and
|
||||||
// suggest variants + auto-imports
|
// suggest variants + auto-imports
|
||||||
ctx.scope().process_all_names(&mut |name, res| {
|
ctx.scope().process_all_names(&mut |name, res| {
|
||||||
let def = match &res {
|
match &res {
|
||||||
hir::ScopeDef::ModuleDef(def) => def,
|
hir::ScopeDef::ModuleDef(def) => match def {
|
||||||
|
hir::ModuleDef::Adt(hir::Adt::Enum(..))
|
||||||
|
| hir::ModuleDef::Adt(hir::Adt::Struct(..))
|
||||||
|
| hir::ModuleDef::EnumVariant(..)
|
||||||
|
| hir::ModuleDef::Const(..)
|
||||||
|
| hir::ModuleDef::Module(..) => (),
|
||||||
|
_ => return,
|
||||||
|
},
|
||||||
|
hir::ScopeDef::MacroDef(_) => (),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
match def {
|
|
||||||
hir::ModuleDef::Adt(hir::Adt::Enum(..))
|
|
||||||
| hir::ModuleDef::EnumVariant(..)
|
|
||||||
| hir::ModuleDef::Const(..)
|
|
||||||
| hir::ModuleDef::Module(..) => (),
|
|
||||||
_ => return,
|
|
||||||
}
|
|
||||||
acc.add_resolution(ctx, name.to_string(), &res)
|
acc.add_resolution(ctx, name.to_string(), &res)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -69,26 +71,13 @@ mod tests {
|
||||||
insert: "E",
|
insert: "E",
|
||||||
kind: Enum,
|
kind: Enum,
|
||||||
},
|
},
|
||||||
CompletionItem {
|
|
||||||
label: "E",
|
|
||||||
source_range: [246; 246),
|
|
||||||
delete: [246; 246),
|
|
||||||
insert: "E",
|
|
||||||
kind: Enum,
|
|
||||||
},
|
|
||||||
CompletionItem {
|
|
||||||
label: "X",
|
|
||||||
source_range: [246; 246),
|
|
||||||
delete: [246; 246),
|
|
||||||
insert: "X",
|
|
||||||
kind: EnumVariant,
|
|
||||||
},
|
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "X",
|
label: "X",
|
||||||
source_range: [246; 246),
|
source_range: [246; 246),
|
||||||
delete: [246; 246),
|
delete: [246; 246),
|
||||||
insert: "X",
|
insert: "X",
|
||||||
kind: EnumVariant,
|
kind: EnumVariant,
|
||||||
|
detail: "()",
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "Z",
|
label: "Z",
|
||||||
|
@ -97,20 +86,6 @@ mod tests {
|
||||||
insert: "Z",
|
insert: "Z",
|
||||||
kind: Const,
|
kind: Const,
|
||||||
},
|
},
|
||||||
CompletionItem {
|
|
||||||
label: "Z",
|
|
||||||
source_range: [246; 246),
|
|
||||||
delete: [246; 246),
|
|
||||||
insert: "Z",
|
|
||||||
kind: Const,
|
|
||||||
},
|
|
||||||
CompletionItem {
|
|
||||||
label: "m",
|
|
||||||
source_range: [246; 246),
|
|
||||||
delete: [246; 246),
|
|
||||||
insert: "m",
|
|
||||||
kind: Module,
|
|
||||||
},
|
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "m",
|
label: "m",
|
||||||
source_range: [246; 246),
|
source_range: [246; 246),
|
||||||
|
@ -138,13 +113,6 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_debug_snapshot!(completions, @r###"
|
assert_debug_snapshot!(completions, @r###"
|
||||||
[
|
[
|
||||||
CompletionItem {
|
|
||||||
label: "E",
|
|
||||||
source_range: [151; 151),
|
|
||||||
delete: [151; 151),
|
|
||||||
insert: "E",
|
|
||||||
kind: Enum,
|
|
||||||
},
|
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "E",
|
label: "E",
|
||||||
source_range: [151; 151),
|
source_range: [151; 151),
|
||||||
|
|
|
@ -1,19 +1,13 @@
|
||||||
//! Completion of names from the current scope, e.g. locals and imported items.
|
//! Completion of names from the current scope, e.g. locals and imported items.
|
||||||
|
|
||||||
use crate::completion::{CompletionContext, Completions};
|
use crate::completion::{CompletionContext, Completions};
|
||||||
use hir::{ModuleDef, ScopeDef};
|
|
||||||
|
|
||||||
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
if !ctx.is_trivial_path && !ctx.is_pat_binding_and_path {
|
if !(ctx.is_trivial_path && !ctx.is_pat_binding_or_const) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.scope().process_all_names(&mut |name, res| match (ctx.is_pat_binding_and_path, &res) {
|
ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res));
|
||||||
(true, ScopeDef::ModuleDef(ModuleDef::Function(..))) => (),
|
|
||||||
(true, ScopeDef::ModuleDef(ModuleDef::Static(..))) => (),
|
|
||||||
(true, ScopeDef::Local(..)) => (),
|
|
||||||
_ => acc.add_resolution(ctx, name.to_string(), &res),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -35,10 +35,7 @@ pub(crate) struct CompletionContext<'a> {
|
||||||
pub(super) is_param: bool,
|
pub(super) is_param: bool,
|
||||||
/// If a name-binding or reference to a const in a pattern.
|
/// If a name-binding or reference to a const in a pattern.
|
||||||
/// Irrefutable patterns (like let) are excluded.
|
/// Irrefutable patterns (like let) are excluded.
|
||||||
pub(super) is_pat_binding: bool,
|
pub(super) is_pat_binding_or_const: bool,
|
||||||
// A bind battern which may also be part of a path.
|
|
||||||
// if let Some(En<|>) = Some(Enum::A)
|
|
||||||
pub(super) is_pat_binding_and_path: bool,
|
|
||||||
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
|
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
|
||||||
pub(super) is_trivial_path: bool,
|
pub(super) is_trivial_path: bool,
|
||||||
/// If not a trivial path, the prefix (qualifier).
|
/// If not a trivial path, the prefix (qualifier).
|
||||||
|
@ -97,8 +94,7 @@ impl<'a> CompletionContext<'a> {
|
||||||
record_lit_pat: None,
|
record_lit_pat: None,
|
||||||
impl_def: None,
|
impl_def: None,
|
||||||
is_param: false,
|
is_param: false,
|
||||||
is_pat_binding: false,
|
is_pat_binding_or_const: false,
|
||||||
is_pat_binding_and_path: false,
|
|
||||||
is_trivial_path: false,
|
is_trivial_path: false,
|
||||||
path_prefix: None,
|
path_prefix: None,
|
||||||
after_if: false,
|
after_if: false,
|
||||||
|
@ -190,18 +186,19 @@ impl<'a> CompletionContext<'a> {
|
||||||
// suggest declaration names, see `CompletionKind::Magic`.
|
// suggest declaration names, see `CompletionKind::Magic`.
|
||||||
if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) {
|
if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) {
|
||||||
if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
|
if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
|
||||||
let parent = bind_pat.syntax().parent();
|
self.is_pat_binding_or_const = true;
|
||||||
if parent.clone().and_then(ast::MatchArm::cast).is_some()
|
if bind_pat.has_at() || bind_pat.is_ref() || bind_pat.is_mutable() {
|
||||||
|| parent.clone().and_then(ast::Condition::cast).is_some()
|
self.is_pat_binding_or_const = false;
|
||||||
{
|
|
||||||
self.is_pat_binding = true;
|
|
||||||
}
|
}
|
||||||
|
if bind_pat.syntax().parent().and_then(ast::RecordFieldPatList::cast).is_some() {
|
||||||
if parent.and_then(ast::RecordFieldPatList::cast).is_none()
|
self.is_pat_binding_or_const = false;
|
||||||
&& bind_pat.pat().is_none()
|
}
|
||||||
&& !bind_pat.is_ref()
|
if let Some(let_stmt) = bind_pat.syntax().ancestors().find_map(ast::LetStmt::cast) {
|
||||||
{
|
if let Some(pat) = let_stmt.pat() {
|
||||||
self.is_pat_binding_and_path = true;
|
if bind_pat.syntax().text_range().is_subrange(&pat.syntax().text_range()) {
|
||||||
|
self.is_pat_binding_or_const = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if is_node::<ast::Param>(name.syntax()) {
|
if is_node::<ast::Param>(name.syntax()) {
|
||||||
|
|
|
@ -7,7 +7,8 @@ use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completion::{
|
completion::{
|
||||||
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
|
||||||
|
CompletionKind, Completions,
|
||||||
},
|
},
|
||||||
display::{const_label, macro_label, type_label, FunctionSignature},
|
display::{const_label, macro_label, type_label, FunctionSignature},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
|
@ -56,14 +57,16 @@ impl Completions {
|
||||||
let kind = match resolution {
|
let kind = match resolution {
|
||||||
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
|
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
|
||||||
ScopeDef::ModuleDef(Function(func)) => {
|
ScopeDef::ModuleDef(Function(func)) => {
|
||||||
return self.add_function_with_name(ctx, Some(local_name), *func);
|
return self.add_function(ctx, *func, Some(local_name));
|
||||||
}
|
}
|
||||||
ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
|
ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
|
||||||
// FIXME: add CompletionItemKind::Union
|
// FIXME: add CompletionItemKind::Union
|
||||||
ScopeDef::ModuleDef(Adt(hir::Adt::Union(_))) => CompletionItemKind::Struct,
|
ScopeDef::ModuleDef(Adt(hir::Adt::Union(_))) => CompletionItemKind::Struct,
|
||||||
ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
|
ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
|
||||||
|
|
||||||
ScopeDef::ModuleDef(EnumVariant(..)) => CompletionItemKind::EnumVariant,
|
ScopeDef::ModuleDef(EnumVariant(var)) => {
|
||||||
|
return self.add_enum_variant(ctx, *var, Some(local_name));
|
||||||
|
}
|
||||||
ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
|
ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
|
||||||
ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
|
ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
|
||||||
ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::Trait,
|
ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::Trait,
|
||||||
|
@ -124,10 +127,6 @@ impl Completions {
|
||||||
completion_item.kind(kind).set_documentation(docs).add_to(self)
|
completion_item.kind(kind).set_documentation(docs).add_to(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) {
|
|
||||||
self.add_function_with_name(ctx, None, func)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn guess_macro_braces(&self, macro_name: &str, docs: &str) -> &'static str {
|
fn guess_macro_braces(&self, macro_name: &str, docs: &str) -> &'static str {
|
||||||
let mut votes = [0, 0, 0];
|
let mut votes = [0, 0, 0];
|
||||||
for (idx, s) in docs.match_indices(¯o_name) {
|
for (idx, s) in docs.match_indices(¯o_name) {
|
||||||
|
@ -186,16 +185,15 @@ impl Completions {
|
||||||
self.add(builder);
|
self.add(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_function_with_name(
|
pub(crate) fn add_function(
|
||||||
&mut self,
|
&mut self,
|
||||||
ctx: &CompletionContext,
|
ctx: &CompletionContext,
|
||||||
name: Option<String>,
|
|
||||||
func: hir::Function,
|
func: hir::Function,
|
||||||
|
local_name: Option<String>,
|
||||||
) {
|
) {
|
||||||
let has_self_param = func.has_self_param(ctx.db);
|
let has_self_param = func.has_self_param(ctx.db);
|
||||||
let params = func.params(ctx.db);
|
|
||||||
|
|
||||||
let name = name.unwrap_or_else(|| func.name(ctx.db).to_string());
|
let name = local_name.unwrap_or_else(|| func.name(ctx.db).to_string());
|
||||||
let ast_node = func.source(ctx.db).value;
|
let ast_node = func.source(ctx.db).value;
|
||||||
let function_signature = FunctionSignature::from(&ast_node);
|
let function_signature = FunctionSignature::from(&ast_node);
|
||||||
|
|
||||||
|
@ -210,32 +208,14 @@ impl Completions {
|
||||||
.set_deprecated(is_deprecated(func, ctx.db))
|
.set_deprecated(is_deprecated(func, ctx.db))
|
||||||
.detail(function_signature.to_string());
|
.detail(function_signature.to_string());
|
||||||
|
|
||||||
// If not an import, add parenthesis automatically.
|
let params = function_signature
|
||||||
if ctx.use_item_syntax.is_none() && !ctx.is_call && ctx.config.add_call_parenthesis {
|
.parameter_names
|
||||||
tested_by!(inserts_parens_for_function_calls);
|
.iter()
|
||||||
|
.skip(if function_signature.has_self_param { 1 } else { 0 })
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
|
builder = builder.add_call_parens(ctx, name, Params::Named(params));
|
||||||
(format!("{}()$0", name), format!("{}()", name))
|
|
||||||
} else {
|
|
||||||
builder = builder.trigger_call_info();
|
|
||||||
let snippet = if ctx.config.add_call_argument_snippets {
|
|
||||||
let to_skip = if has_self_param { 1 } else { 0 };
|
|
||||||
let function_params_snippet = function_signature
|
|
||||||
.parameter_names
|
|
||||||
.iter()
|
|
||||||
.skip(to_skip)
|
|
||||||
.enumerate()
|
|
||||||
.map(|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name))
|
|
||||||
.sep_by(", ");
|
|
||||||
format!("{}({})$0", name, function_params_snippet)
|
|
||||||
} else {
|
|
||||||
format!("{}($0)", name)
|
|
||||||
};
|
|
||||||
|
|
||||||
(snippet, format!("{}(…)", name))
|
|
||||||
};
|
|
||||||
builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.add(builder)
|
self.add(builder)
|
||||||
}
|
}
|
||||||
|
@ -272,14 +252,20 @@ impl Completions {
|
||||||
.add_to(self);
|
.add_to(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) {
|
pub(crate) fn add_enum_variant(
|
||||||
|
&mut self,
|
||||||
|
ctx: &CompletionContext,
|
||||||
|
variant: hir::EnumVariant,
|
||||||
|
local_name: Option<String>,
|
||||||
|
) {
|
||||||
let is_deprecated = is_deprecated(variant, ctx.db);
|
let is_deprecated = is_deprecated(variant, ctx.db);
|
||||||
let name = variant.name(ctx.db);
|
let name = local_name.unwrap_or_else(|| variant.name(ctx.db).to_string());
|
||||||
let detail_types = variant
|
let detail_types = variant
|
||||||
.fields(ctx.db)
|
.fields(ctx.db)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|field| (field.name(ctx.db), field.signature_ty(ctx.db)));
|
.map(|field| (field.name(ctx.db), field.signature_ty(ctx.db)));
|
||||||
let detail = match variant.kind(ctx.db) {
|
let variant_kind = variant.kind(ctx.db);
|
||||||
|
let detail = match variant_kind {
|
||||||
StructKind::Tuple | StructKind::Unit => detail_types
|
StructKind::Tuple | StructKind::Unit => detail_types
|
||||||
.map(|(_, t)| t.display(ctx.db).to_string())
|
.map(|(_, t)| t.display(ctx.db).to_string())
|
||||||
.sep_by(", ")
|
.sep_by(", ")
|
||||||
|
@ -291,12 +277,70 @@ impl Completions {
|
||||||
.surround_with("{ ", " }")
|
.surround_with("{ ", " }")
|
||||||
.to_string(),
|
.to_string(),
|
||||||
};
|
};
|
||||||
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
|
let mut res =
|
||||||
.kind(CompletionItemKind::EnumVariant)
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone())
|
||||||
.set_documentation(variant.docs(ctx.db))
|
.kind(CompletionItemKind::EnumVariant)
|
||||||
.set_deprecated(is_deprecated)
|
.set_documentation(variant.docs(ctx.db))
|
||||||
.detail(detail)
|
.set_deprecated(is_deprecated)
|
||||||
.add_to(self);
|
.detail(detail);
|
||||||
|
|
||||||
|
if variant_kind == StructKind::Tuple {
|
||||||
|
let params = Params::Anonymous(variant.fields(ctx.db).len());
|
||||||
|
res = res.add_call_parens(ctx, name, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.add_to(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Params {
|
||||||
|
Named(Vec<String>),
|
||||||
|
Anonymous(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Params {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
match self {
|
||||||
|
Params::Named(xs) => xs.len(),
|
||||||
|
Params::Anonymous(len) => *len,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
self.len() == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Builder {
|
||||||
|
fn add_call_parens(mut self, ctx: &CompletionContext, name: String, params: Params) -> Builder {
|
||||||
|
if !ctx.config.add_call_parenthesis {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
if ctx.use_item_syntax.is_some() || ctx.is_call {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
// If not an import, add parenthesis automatically.
|
||||||
|
tested_by!(inserts_parens_for_function_calls);
|
||||||
|
|
||||||
|
let (snippet, label) = if params.is_empty() {
|
||||||
|
(format!("{}()$0", name), format!("{}()", name))
|
||||||
|
} else {
|
||||||
|
self = self.trigger_call_info();
|
||||||
|
let snippet = match (ctx.config.add_call_argument_snippets, params) {
|
||||||
|
(true, Params::Named(params)) => {
|
||||||
|
let function_params_snippet = params
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name))
|
||||||
|
.sep_by(", ");
|
||||||
|
format!("{}({})$0", name, function_params_snippet)
|
||||||
|
}
|
||||||
|
_ => format!("{}($0)", name),
|
||||||
|
};
|
||||||
|
|
||||||
|
(snippet, format!("{}(…)", name))
|
||||||
|
};
|
||||||
|
self.lookup_by(name).label(label).insert_snippet(snippet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,12 +410,14 @@ mod tests {
|
||||||
@r###"
|
@r###"
|
||||||
[
|
[
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
label: "Foo",
|
label: "Foo(…)",
|
||||||
source_range: [115; 117),
|
source_range: [115; 117),
|
||||||
delete: [115; 117),
|
delete: [115; 117),
|
||||||
insert: "Foo",
|
insert: "Foo($0)",
|
||||||
kind: EnumVariant,
|
kind: EnumVariant,
|
||||||
|
lookup: "Foo",
|
||||||
detail: "(i32, i32)",
|
detail: "(i32, i32)",
|
||||||
|
trigger_call_info: true,
|
||||||
},
|
},
|
||||||
]"###
|
]"###
|
||||||
);
|
);
|
||||||
|
@ -545,6 +591,101 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inserts_parens_for_tuple_enums() {
|
||||||
|
assert_debug_snapshot!(
|
||||||
|
do_reference_completion(
|
||||||
|
r"
|
||||||
|
enum Option<T> { Some(T), None }
|
||||||
|
use Option::*;
|
||||||
|
fn main() -> Option<i32> {
|
||||||
|
Som<|>
|
||||||
|
}
|
||||||
|
"
|
||||||
|
),
|
||||||
|
@r###"
|
||||||
|
[
|
||||||
|
CompletionItem {
|
||||||
|
label: "None",
|
||||||
|
source_range: [144; 147),
|
||||||
|
delete: [144; 147),
|
||||||
|
insert: "None",
|
||||||
|
kind: EnumVariant,
|
||||||
|
detail: "()",
|
||||||
|
},
|
||||||
|
CompletionItem {
|
||||||
|
label: "Option",
|
||||||
|
source_range: [144; 147),
|
||||||
|
delete: [144; 147),
|
||||||
|
insert: "Option",
|
||||||
|
kind: Enum,
|
||||||
|
},
|
||||||
|
CompletionItem {
|
||||||
|
label: "Some(…)",
|
||||||
|
source_range: [144; 147),
|
||||||
|
delete: [144; 147),
|
||||||
|
insert: "Some($0)",
|
||||||
|
kind: EnumVariant,
|
||||||
|
lookup: "Some",
|
||||||
|
detail: "(T)",
|
||||||
|
trigger_call_info: true,
|
||||||
|
},
|
||||||
|
CompletionItem {
|
||||||
|
label: "main()",
|
||||||
|
source_range: [144; 147),
|
||||||
|
delete: [144; 147),
|
||||||
|
insert: "main()$0",
|
||||||
|
kind: Function,
|
||||||
|
lookup: "main",
|
||||||
|
detail: "fn main() -> Option<i32>",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
assert_debug_snapshot!(
|
||||||
|
do_reference_completion(
|
||||||
|
r"
|
||||||
|
enum Option<T> { Some(T), None }
|
||||||
|
use Option::*;
|
||||||
|
fn main(value: Option<i32>) {
|
||||||
|
match value {
|
||||||
|
Som<|>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"
|
||||||
|
),
|
||||||
|
@r###"
|
||||||
|
[
|
||||||
|
CompletionItem {
|
||||||
|
label: "None",
|
||||||
|
source_range: [185; 188),
|
||||||
|
delete: [185; 188),
|
||||||
|
insert: "None",
|
||||||
|
kind: EnumVariant,
|
||||||
|
detail: "()",
|
||||||
|
},
|
||||||
|
CompletionItem {
|
||||||
|
label: "Option",
|
||||||
|
source_range: [185; 188),
|
||||||
|
delete: [185; 188),
|
||||||
|
insert: "Option",
|
||||||
|
kind: Enum,
|
||||||
|
},
|
||||||
|
CompletionItem {
|
||||||
|
label: "Some(…)",
|
||||||
|
source_range: [185; 188),
|
||||||
|
delete: [185; 188),
|
||||||
|
insert: "Some($0)",
|
||||||
|
kind: EnumVariant,
|
||||||
|
lookup: "Some",
|
||||||
|
detail: "(T)",
|
||||||
|
trigger_call_info: true,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn arg_snippets_for_method_call() {
|
fn arg_snippets_for_method_call() {
|
||||||
assert_debug_snapshot!(
|
assert_debug_snapshot!(
|
||||||
|
|
|
@ -325,6 +325,9 @@ impl ast::BindPat {
|
||||||
pub fn is_ref(&self) -> bool {
|
pub fn is_ref(&self) -> bool {
|
||||||
self.syntax().children_with_tokens().any(|n| n.kind() == T![ref])
|
self.syntax().children_with_tokens().any(|n| n.kind() == T![ref])
|
||||||
}
|
}
|
||||||
|
pub fn has_at(&self) -> bool {
|
||||||
|
self.syntax().children_with_tokens().any(|it| it.kind() == T![@])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SlicePatComponents {
|
pub struct SlicePatComponents {
|
||||||
|
|
Loading…
Reference in a new issue