From e201e7834d623f954e06925b9de0f14cd5ff90c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 15:55:59 +0200 Subject: Ascribe more correct types --- crates/ra_assists/src/handlers/add_explicit_type.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index 55409e501..146cc75df 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs @@ -23,6 +23,7 @@ use crate::{AssistContext, AssistId, Assists}; // ``` pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let stmt = ctx.find_node_at_offset::()?; + let module = ctx.sema.scope(stmt.syntax()).module()?; let expr = stmt.initializer()?; let pat = stmt.pat()?; // Must be a binding @@ -57,17 +58,17 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio return None; } - let db = ctx.db; - let new_type_string = ty.display_truncated(db, None).to_string(); + let inferred_type = ty.display_source_code(ctx.db, module.into()).ok()?; acc.add( AssistId("add_explicit_type"), - format!("Insert explicit type '{}'", new_type_string), + format!("Insert explicit type '{}'", inferred_type), pat_range, - |edit| { - if let Some(ascribed_ty) = ascribed_ty { - edit.replace(ascribed_ty.syntax().text_range(), new_type_string); - } else { - edit.insert(name_range.end(), format!(": {}", new_type_string)); + |builder| match ascribed_ty { + Some(ascribed_ty) => { + builder.replace(ascribed_ty.syntax().text_range(), inferred_type); + } + None => { + builder.insert(name_range.end(), format!(": {}", inferred_type)); } }, ) @@ -208,7 +209,7 @@ struct Test { } fn main() { - let test<|>: Test = Test { t: 23, k: 33 }; + let test<|>: Test = Test { t: 23, k: 33 }; }"#, ); } -- cgit v1.2.3