From 1dc83f5a90248c53fca38eaee7392a5485a375a3 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 7 May 2022 12:59:26 +0200 Subject: [PATCH] minor: Move inferred type completions --- crates/ide-completion/src/completions.rs | 20 +----------------- crates/ide-completion/src/completions/type.rs | 21 +++++++++++++++++-- crates/ide-completion/src/item.rs | 2 +- crates/ide-completion/src/lib.rs | 2 +- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/crates/ide-completion/src/completions.rs b/crates/ide-completion/src/completions.rs index 687a598bac..931b92dec3 100644 --- a/crates/ide-completion/src/completions.rs +++ b/crates/ide-completion/src/completions.rs @@ -22,13 +22,12 @@ pub(crate) mod vis; use std::iter; -use hir::{db::HirDatabase, known, HirDisplay, ScopeDef}; +use hir::{db::HirDatabase, known, ScopeDef}; use ide_db::SymbolKind; use crate::{ context::Visible, item::Builder, - patterns::{ImmediateLocation, TypeAnnotation}, render::{ const_::render_const, function::{render_fn, render_method}, @@ -36,7 +35,6 @@ use crate::{ macro_::render_macro, pattern::{render_struct_pat, render_variant_pat}, render_field, render_resolution, render_resolution_simple, render_tuple_field, - render_type_inference, type_alias::{render_type_alias, render_type_alias_with_eq}, union_literal::render_union_literal, RenderContext, @@ -401,19 +399,3 @@ fn enum_variants_with_paths( } } } - -pub(crate) fn inferred_type(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { - use TypeAnnotation::*; - let pat = match &ctx.completion_location { - Some(ImmediateLocation::TypeAnnotation(t)) => t, - _ => return None, - }; - let x = match pat { - Let(pat) | FnParam(pat) => ctx.sema.type_of_pat(pat.as_ref()?), - Const(exp) | RetType(exp) => ctx.sema.type_of_expr(exp.as_ref()?), - }? - .adjusted(); - let ty_string = x.display_source_code(ctx.db, ctx.module.into()).ok()?; - acc.add(render_type_inference(ty_string, ctx)); - None -} diff --git a/crates/ide-completion/src/completions/type.rs b/crates/ide-completion/src/completions/type.rs index 2dbe81f92a..a8d71fd868 100644 --- a/crates/ide-completion/src/completions/type.rs +++ b/crates/ide-completion/src/completions/type.rs @@ -1,12 +1,13 @@ //! Completion of names from the current scope in type position. -use hir::ScopeDef; +use hir::{HirDisplay, ScopeDef}; use ide_db::FxHashSet; use syntax::{ast, AstNode}; use crate::{ context::{PathCompletionCtx, PathKind, PathQualifierCtx}, - patterns::ImmediateLocation, + patterns::{ImmediateLocation, TypeAnnotation}, + render::render_type_inference, CompletionContext, Completions, }; @@ -184,6 +185,22 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) } } +pub(crate) fn complete_inferred_type(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { + use TypeAnnotation::*; + let pat = match &ctx.completion_location { + Some(ImmediateLocation::TypeAnnotation(t)) => t, + _ => return None, + }; + let x = match pat { + Let(pat) | FnParam(pat) => ctx.sema.type_of_pat(pat.as_ref()?), + Const(exp) | RetType(exp) => ctx.sema.type_of_expr(exp.as_ref()?), + }? + .adjusted(); + let ty_string = x.display_source_code(ctx.db, ctx.module.into()).ok()?; + acc.add(render_type_inference(ty_string, ctx)); + None +} + fn add_assoc_item(acc: &mut Completions, ctx: &CompletionContext, item: hir::AssocItem) { match item { hir::AssocItem::Const(ct) if ctx.expects_generic_arg() => acc.add_const(ctx, ct), diff --git a/crates/ide-completion/src/item.rs b/crates/ide-completion/src/item.rs index 140e5b600b..6e5c2a9386 100644 --- a/crates/ide-completion/src/item.rs +++ b/crates/ide-completion/src/item.rs @@ -149,7 +149,7 @@ pub struct CompletionRelevance { pub is_private_editable: bool, /// Set for postfix snippet item completions pub postfix_match: Option, - /// This is setted for type inference results + /// This is set for type inference results pub is_definite: bool, } diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs index bc18e80516..6695fcdc19 100644 --- a/crates/ide-completion/src/lib.rs +++ b/crates/ide-completion/src/lib.rs @@ -159,7 +159,6 @@ pub fn completions( completions::fn_param::complete_fn_param(acc, ctx); completions::format_string::format_string(acc, ctx); completions::item_list::complete_item_list(acc, ctx); - completions::inferred_type(acc, ctx); completions::keyword::complete_expr_keyword(acc, ctx); completions::lifetime::complete_label(acc, ctx); completions::lifetime::complete_lifetime(acc, ctx); @@ -172,6 +171,7 @@ pub fn completions( completions::snippet::complete_item_snippet(acc, ctx); completions::trait_impl::complete_trait_impl(acc, ctx); completions::r#type::complete_type_path(acc, ctx); + completions::r#type::complete_inferred_type(acc, ctx); completions::use_::complete_use_tree(acc, ctx); completions::vis::complete_vis(acc, ctx); }