From 66b701ed3e9980c4441f963467e49c687a4afca5 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 16 Jun 2021 15:46:58 +0200 Subject: Simplify --- crates/ide_completion/src/completions.rs | 18 ++----- crates/ide_completion/src/render.rs | 78 +++++++++++------------------- crates/ide_completion/src/render/macro_.rs | 4 +- 3 files changed, 34 insertions(+), 66 deletions(-) (limited to 'crates') diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index aef70f26a..783305005 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs @@ -131,9 +131,6 @@ impl Completions { func: hir::Function, local_name: Option, ) { - if ctx.expects_type() { - return; - } self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func)); } @@ -175,9 +172,6 @@ impl Completions { } pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { - if ctx.expects_type() { - return; - } self.add_opt(render_const(RenderContext::new(ctx), constant)); } @@ -209,32 +203,30 @@ impl Completions { variant: hir::Variant, local_name: Option, ) { - if ctx.expects_type() { - return; - } let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None); self.add(item); } } +/// Calls the callback for each variant of the provided enum with the path to the variant. fn complete_enum_variants( acc: &mut Completions, ctx: &CompletionContext, - enum_data: hir::Enum, + enum_: hir::Enum, cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath), ) { - let variants = enum_data.variants(ctx.db); + let variants = enum_.variants(ctx.db); let module = if let Some(module) = ctx.scope.module() { // Compute path from the completion site if available. module } else { // Otherwise fall back to the enum's definition site. - enum_data.module(ctx.db) + enum_.module(ctx.db) }; if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) { - if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_data)) { + if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) { for &variant in &variants { let self_path = hir::ModPath::from_segments( hir::PathKind::Plain, diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 70bf26cf4..2bd2c44d0 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -23,50 +23,6 @@ use crate::{ render::{enum_variant::render_variant, function::render_fn, macro_::render_macro}, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, }; - -pub(crate) fn render_field( - ctx: RenderContext<'_>, - receiver: Option, - field: hir::Field, - ty: &hir::Type, -) -> CompletionItem { - render_field_(ctx, receiver, field, ty) -} - -pub(crate) fn render_tuple_field( - ctx: RenderContext<'_>, - receiver: Option, - field: usize, - ty: &hir::Type, -) -> CompletionItem { - render_tuple_field_(ctx, receiver, field, ty) -} - -pub(crate) fn render_resolution( - ctx: RenderContext<'_>, - local_name: hir::Name, - resolution: &hir::ScopeDef, -) -> Option { - render_resolution_(ctx, local_name, None, resolution) -} - -pub(crate) fn render_resolution_with_import( - ctx: RenderContext<'_>, - import_edit: ImportEdit, -) -> Option { - let resolution = hir::ScopeDef::from(import_edit.import.original_item); - let local_name = match resolution { - hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db), - hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?, - hir::ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db), - _ => item_name(ctx.db(), import_edit.import.original_item)?, - }; - render_resolution_(ctx, local_name, Some(import_edit), &resolution).map(|mut item| { - item.completion_kind = CompletionKind::Magic; - item - }) -} - /// Interface for data and methods required for items rendering. #[derive(Debug)] pub(crate) struct RenderContext<'a> { @@ -119,7 +75,7 @@ impl<'a> RenderContext<'a> { } } -fn render_field_( +pub(crate) fn render_field( ctx: RenderContext<'_>, receiver: Option, field: hir::Field, @@ -132,7 +88,6 @@ fn render_field_( ctx.source_range(), receiver.map_or_else(|| name.clone(), |receiver| format!("{}.{}", receiver, name)), ); - item.set_relevance(CompletionRelevance { type_match: compute_type_match(ctx.completion, ty), exact_name_match: compute_exact_name_match(ctx.completion, &name), @@ -143,17 +98,15 @@ fn render_field_( .set_documentation(field.docs(ctx.db())) .set_deprecated(is_deprecated) .lookup_by(name); - if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) { // FIXME // For now we don't properly calculate the edits for ref match // completions on struct fields, so we've disabled them. See #8058. } - item.build() } -fn render_tuple_field_( +pub(crate) fn render_tuple_field( ctx: RenderContext<'_>, receiver: Option, field: usize, @@ -164,14 +117,37 @@ fn render_tuple_field_( ctx.source_range(), receiver.map_or_else(|| field.to_string(), |receiver| format!("{}.{}", receiver, field)), ); - item.kind(SymbolKind::Field) .detail(ty.display(ctx.db()).to_string()) .lookup_by(field.to_string()); - item.build() } +pub(crate) fn render_resolution( + ctx: RenderContext<'_>, + local_name: hir::Name, + resolution: &hir::ScopeDef, +) -> Option { + render_resolution_(ctx, local_name, None, resolution) +} + +pub(crate) fn render_resolution_with_import( + ctx: RenderContext<'_>, + import_edit: ImportEdit, +) -> Option { + let resolution = hir::ScopeDef::from(import_edit.import.original_item); + let local_name = match resolution { + hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db), + hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?, + hir::ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db), + _ => item_name(ctx.db(), import_edit.import.original_item)?, + }; + render_resolution_(ctx, local_name, Some(import_edit), &resolution).map(|mut item| { + item.completion_kind = CompletionKind::Magic; + item + }) +} + fn render_resolution_( ctx: RenderContext<'_>, local_name: hir::Name, diff --git a/crates/ide_completion/src/render/macro_.rs b/crates/ide_completion/src/render/macro_.rs index 429d937c8..3a7238bb8 100644 --- a/crates/ide_completion/src/render/macro_.rs +++ b/crates/ide_completion/src/render/macro_.rs @@ -180,7 +180,7 @@ fn main() { frobnicate!(); } /// ``` macro_rules! vec { () => {} } -fn fn main() { v$0 } +fn main() { v$0 } "#, r#" /// Creates a [`Vec`] containing the arguments. @@ -193,7 +193,7 @@ fn fn main() { v$0 } /// ``` macro_rules! vec { () => {} } -fn fn main() { vec![$0] } +fn main() { vec![$0] } "#, ); -- cgit v1.2.3