minor: Move inferred type completions

This commit is contained in:
Lukas Wirth 2022-05-07 12:59:26 +02:00
parent 81b3e6d124
commit 1dc83f5a90
4 changed files with 22 additions and 23 deletions

View file

@ -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
}

View file

@ -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),

View file

@ -149,7 +149,7 @@ pub struct CompletionRelevance {
pub is_private_editable: bool,
/// Set for postfix snippet item completions
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
/// This is setted for type inference results
/// This is set for type inference results
pub is_definite: bool,
}

View file

@ -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);
}