364: Parens r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2018-12-30 13:30:43 +00:00
commit 12d4c069bb
4 changed files with 39 additions and 17 deletions

View File

@ -17,7 +17,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
let module_scope = module.scope(ctx.db)?; let module_scope = module.scope(ctx.db)?;
module_scope.entries().for_each(|(name, res)| { module_scope.entries().for_each(|(name, res)| {
CompletionItem::new(CompletionKind::Reference, name.to_string()) CompletionItem::new(CompletionKind::Reference, name.to_string())
.from_resolution(ctx.db, res) .from_resolution(ctx, res)
.add_to(acc) .add_to(acc)
}); });
} }
@ -113,4 +113,16 @@ fn foo() { let _ = E::<|> }
"Foo;Bar", "Foo;Bar",
); );
} }
#[test]
fn dont_render_function_parens_in_use_item() {
check_reference_completion(
"
//- /lib.rs
mod m { pub fn foo() {} }
use crate::m::f<|>;
",
"foo",
)
}
} }

View File

@ -34,7 +34,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
}) })
.for_each(|(name, res)| { .for_each(|(name, res)| {
CompletionItem::new(CompletionKind::Reference, name.to_string()) CompletionItem::new(CompletionKind::Reference, name.to_string())
.from_resolution(ctx.db, res) .from_resolution(ctx, res)
.add_to(acc) .add_to(acc)
}); });
Ok(()) Ok(())

View File

@ -24,6 +24,7 @@ pub(super) struct CompletionContext<'a> {
pub(super) module: Option<hir::Module>, pub(super) module: Option<hir::Module>,
pub(super) function: Option<hir::Function>, pub(super) function: Option<hir::Function>,
pub(super) function_syntax: Option<ast::FnDef<'a>>, pub(super) function_syntax: Option<ast::FnDef<'a>>,
pub(super) use_item_syntax: Option<ast::UseItem<'a>>,
pub(super) is_param: bool, pub(super) is_param: bool,
/// A single-indent path, like `foo`. /// A single-indent path, like `foo`.
pub(super) is_trivial_path: bool, pub(super) is_trivial_path: bool,
@ -55,6 +56,7 @@ pub(super) fn new(
module, module,
function: None, function: None,
function_syntax: None, function_syntax: None,
use_item_syntax: None,
is_param: false, is_param: false,
is_trivial_path: false, is_trivial_path: false,
path_prefix: None, path_prefix: None,
@ -114,6 +116,8 @@ fn classify_name_ref(&mut self, original_file: &'a SourceFileNode, name_ref: ast
_ => (), _ => (),
} }
self.use_item_syntax = self.leaf.ancestors().find_map(ast::UseItem::cast);
self.function_syntax = self self.function_syntax = self
.leaf .leaf
.ancestors() .ancestors()

View File

@ -1,7 +1,7 @@
use crate::db;
use hir::PerNs; use hir::PerNs;
use crate::completion::CompletionContext;
/// `CompletionItem` describes a single completion variant in the editor pop-up. /// `CompletionItem` describes a single completion variant in the editor pop-up.
/// It is basically a POD with various properties. To construct a /// It is basically a POD with various properties. To construct a
/// `CompletionItem`, use `new` method and the `Builder` struct. /// `CompletionItem`, use `new` method and the `Builder` struct.
@ -118,12 +118,12 @@ pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
self.kind = Some(kind); self.kind = Some(kind);
self self
} }
pub(crate) fn from_resolution( pub(super) fn from_resolution(
mut self, mut self,
db: &db::RootDatabase, ctx: &CompletionContext,
resolution: &hir::Resolution, resolution: &hir::Resolution,
) -> Builder { ) -> Builder {
let resolved = resolution.def_id.and_then(|d| d.resolve(db).ok()); let resolved = resolution.def_id.and_then(|d| d.resolve(ctx.db).ok());
let kind = match resolved { let kind = match resolved {
PerNs { PerNs {
types: Some(hir::Def::Module(..)), types: Some(hir::Def::Module(..)),
@ -140,21 +140,27 @@ pub(crate) fn from_resolution(
PerNs { PerNs {
values: Some(hir::Def::Function(function)), values: Some(hir::Def::Function(function)),
.. ..
} => { } => return self.from_function(ctx, function),
if let Some(sig_info) = function.signature_info(db) {
if sig_info.params.is_empty() {
self.snippet = Some(format!("{}()$0", self.label));
} else {
self.snippet = Some(format!("{}($0)", self.label));
}
}
CompletionItemKind::Function
}
_ => return self, _ => return self,
}; };
self.kind = Some(kind); self.kind = Some(kind);
self self
} }
fn from_function(mut self, ctx: &CompletionContext, function: hir::Function) -> Builder {
// If not an import, add parenthesis automatically.
if ctx.use_item_syntax.is_none() {
if let Some(sig_info) = function.signature_info(ctx.db) {
if sig_info.params.is_empty() {
self.snippet = Some(format!("{}()$0", self.label));
} else {
self.snippet = Some(format!("{}($0)", self.label));
}
}
}
self.kind = Some(CompletionItemKind::Function);
self
}
} }
impl Into<CompletionItem> for Builder { impl Into<CompletionItem> for Builder {