From a2242dcf1b81f96330aed7d7219d8b51c552cdb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20G=C5=82uszak?= Date: Wed, 20 Oct 2021 21:35:35 +0200 Subject: [PATCH] Fixes --- .../src/handlers/generate_function.rs | 12 ++++------ .../src/handlers/generate_getter.rs | 11 ++++----- crates/ide_assists/src/utils.rs | 23 +++++++------------ 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs index bc8cb2a3377..edc4697f2ca 100644 --- a/crates/ide_assists/src/handlers/generate_function.rs +++ b/crates/ide_assists/src/handlers/generate_function.rs @@ -1,6 +1,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use hir::{HasSource, HirDisplay, Module, ModuleDef, Semantics, TypeInfo}; +use ide_db::helpers::FamousDefs; use ide_db::{ base_db::FileId, defs::{Definition, NameRefClass}, @@ -512,13 +513,10 @@ fn maybe_displayed_type( } if ty.is_reference() || ty.is_mutable_reference() { - convert_reference_type( - ty.strip_references(), - ctx, - ctx.sema.scope(fn_arg.syntax()).krate(), - ) - .map(|conversion| conversion.convert_type(ctx.db())) - .or_else(|| ty.display_source_code(ctx.db(), target_module.into()).ok()) + let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(fn_arg.syntax()).krate()); + convert_reference_type(ty.strip_references(), ctx.db(), famous_defs) + .map(|conversion| conversion.convert_type(ctx.db())) + .or_else(|| ty.display_source_code(ctx.db(), target_module.into()).ok()) } else { ty.display_source_code(ctx.db(), target_module.into()).ok() } diff --git a/crates/ide_assists/src/handlers/generate_getter.rs b/crates/ide_assists/src/handlers/generate_getter.rs index ba0bc7fdd94..81cf72dd713 100644 --- a/crates/ide_assists/src/handlers/generate_getter.rs +++ b/crates/ide_assists/src/handlers/generate_getter.rs @@ -1,3 +1,4 @@ +use ide_db::helpers::FamousDefs; use stdx::{format_to, to_lower_snake_case}; use syntax::ast::{self, AstNode, HasName, HasVisibility}; @@ -112,16 +113,12 @@ pub(crate) fn generate_getter_impl( let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v)); let (ty, body) = if mutable { - ( - format!("&mut {}", field_ty.to_string()), - format!("&mut self.{}", field_name.to_string()), - ) + (format!("&mut {}", field_ty), format!("&mut self.{}", field_name)) } else { + let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(field_ty.syntax()).krate()); ctx.sema .resolve_type(&field_ty) - .and_then(|ty| { - convert_reference_type(ty, ctx, ctx.sema.scope(field_ty.syntax()).krate()) - }) + .and_then(|ty| convert_reference_type(ty, ctx.db(), famous_defs)) .map(|conversion| { cov_mark::hit!(convert_reference_type); ( diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index e58f1fd50f5..8f0e76f7b8d 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -5,10 +5,10 @@ use itertools::Itertools; pub(crate) use gen_trait_fn_body::gen_trait_fn_body; -use hir::db::HirDatabase; -use hir::{Crate, HasSource, HirDisplay}; -use ide_db::helpers::FamousDefs; -use ide_db::{helpers::SnippetCap, path_transform::PathTransform, RootDatabase}; +use hir::{db::HirDatabase, HasSource, HirDisplay}; +use ide_db::{ + helpers::FamousDefs, helpers::SnippetCap, path_transform::PathTransform, RootDatabase, +}; use stdx::format_to; use syntax::{ ast::{ @@ -577,19 +577,13 @@ pub(crate) fn getter(&self, field_name: String) -> String { // FIXME: It should return a new hir::Type, but currently constructing new types is too cumbersome // and all users of this function operate on string type names, so they can do the conversion // itself themselves. -// Another problem is that it probably shouldn't take AssistContext as a parameter, as -// it should be usable not only in assists. pub(crate) fn convert_reference_type( ty: hir::Type, - ctx: &AssistContext, - krate: Option, + db: &RootDatabase, + famous_defs: &FamousDefs, ) -> Option { - let sema = &ctx.sema; - let db = sema.db; - let famous_defs = &FamousDefs(sema, krate); - handle_copy(&ty, db) - .or_else(|| handle_as_ref_str(&ty, db, famous_defs, ctx)) + .or_else(|| handle_as_ref_str(&ty, db, famous_defs)) .or_else(|| handle_as_ref_slice(&ty, db, famous_defs)) .or_else(|| handle_dereferenced(&ty, db, famous_defs)) .or_else(|| handle_option_as_ref(&ty, db, famous_defs)) @@ -605,9 +599,8 @@ fn handle_as_ref_str( ty: &hir::Type, db: &dyn HirDatabase, famous_defs: &FamousDefs, - ctx: &AssistContext, ) -> Option { - let module = ctx.sema.to_module_def(ctx.file_id())?; + let module = famous_defs.1?.root_module(db); let str_type = hir::BuiltinType::str().ty(db, module); ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type])