Remove db from AssistsContext

This commit is contained in:
Laurențiu Nicola 2020-07-01 10:14:23 +03:00
parent 98ae447fa7
commit 331e6d8f16
11 changed files with 37 additions and 35 deletions

View file

@ -55,7 +55,6 @@ use crate::{
pub(crate) struct AssistContext<'a> { pub(crate) struct AssistContext<'a> {
pub(crate) config: &'a AssistConfig, pub(crate) config: &'a AssistConfig,
pub(crate) sema: Semantics<'a, RootDatabase>, pub(crate) sema: Semantics<'a, RootDatabase>,
pub(crate) db: &'a RootDatabase,
pub(crate) frange: FileRange, pub(crate) frange: FileRange,
source_file: SourceFile, source_file: SourceFile,
} }
@ -67,8 +66,11 @@ impl<'a> AssistContext<'a> {
frange: FileRange, frange: FileRange,
) -> AssistContext<'a> { ) -> AssistContext<'a> {
let source_file = sema.parse(frange.file_id); let source_file = sema.parse(frange.file_id);
let db = sema.db; AssistContext { config, sema, frange, source_file }
AssistContext { config, sema, db, frange, source_file } }
pub(crate) fn db(&self) -> &RootDatabase {
self.sema.db
} }
// NB, this ignores active selection. // NB, this ignores active selection.

View file

@ -57,7 +57,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
return None; return None;
} }
let inferred_type = ty.display_source_code(ctx.db, module.into()).ok()?; let inferred_type = ty.display_source_code(ctx.db(), module.into()).ok()?;
acc.add( acc.add(
AssistId("add_explicit_type"), AssistId("add_explicit_type"),
format!("Insert explicit type `{}`", inferred_type), format!("Insert explicit type `{}`", inferred_type),

View file

@ -117,7 +117,7 @@ impl FunctionBuilder {
let mut file = ctx.frange.file_id; let mut file = ctx.frange.file_id;
let target = match &target_module { let target = match &target_module {
Some(target_module) => { Some(target_module) => {
let module_source = target_module.definition_source(ctx.db); let module_source = target_module.definition_source(ctx.db());
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, &module_source)?; let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, &module_source)?;
file = in_file; file = in_file;
target target
@ -269,7 +269,7 @@ fn fn_arg_type(
return None; return None;
} }
if let Ok(rendered) = ty.display_source_code(ctx.db, target_module.into()) { if let Ok(rendered) = ty.display_source_code(ctx.db(), target_module.into()) {
Some(rendered) Some(rendered)
} else { } else {
None None

View file

@ -128,9 +128,9 @@ fn add_missing_impl_members_inner(
let missing_items = get_missing_assoc_items(&ctx.sema, &impl_def) let missing_items = get_missing_assoc_items(&ctx.sema, &impl_def)
.iter() .iter()
.map(|i| match i { .map(|i| match i {
hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value), hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db()).value),
hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAliasDef(i.source(ctx.db).value), hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAliasDef(i.source(ctx.db()).value),
hir::AssocItem::Const(i) => ast::AssocItem::ConstDef(i.source(ctx.db).value), hir::AssocItem::Const(i) => ast::AssocItem::ConstDef(i.source(ctx.db()).value),
}) })
.filter(|t| def_name(&t).is_some()) .filter(|t| def_name(&t).is_some())
.filter(|t| match t { .filter(|t| match t {

View file

@ -122,7 +122,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
// FIXME: change the new fn checking to a more semantic approach when that's more // FIXME: change the new fn checking to a more semantic approach when that's more
// viable (e.g. we process proc macros, etc) // viable (e.g. we process proc macros, etc)
fn find_struct_impl(ctx: &AssistContext, strukt: &ast::StructDef) -> Option<Option<ast::ImplDef>> { fn find_struct_impl(ctx: &AssistContext, strukt: &ast::StructDef) -> Option<Option<ast::ImplDef>> {
let db = ctx.db; let db = ctx.db();
let module = strukt.syntax().ancestors().find(|node| { let module = strukt.syntax().ancestors().find(|node| {
ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind()) ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
})?; })?;

View file

@ -36,7 +36,7 @@ use crate::{utils::insert_use_statement, AssistContext, AssistId, Assists, Group
// ``` // ```
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let auto_import_assets = AutoImportAssets::new(&ctx)?; let auto_import_assets = AutoImportAssets::new(&ctx)?;
let proposed_imports = auto_import_assets.search_for_imports(ctx.db); let proposed_imports = auto_import_assets.search_for_imports(ctx.db());
if proposed_imports.is_empty() { if proposed_imports.is_empty() {
return None; return None;
} }

View file

@ -37,15 +37,15 @@ pub(crate) fn extract_struct_from_enum_variant(
}; };
let variant_name = variant.name()?.to_string(); let variant_name = variant.name()?.to_string();
let variant_hir = ctx.sema.to_def(&variant)?; let variant_hir = ctx.sema.to_def(&variant)?;
if existing_struct_def(ctx.db, &variant_name, &variant_hir) { if existing_struct_def(ctx.db(), &variant_name, &variant_hir) {
return None; return None;
} }
let enum_ast = variant.parent_enum(); let enum_ast = variant.parent_enum();
let visibility = enum_ast.visibility(); let visibility = enum_ast.visibility();
let enum_hir = ctx.sema.to_def(&enum_ast)?; let enum_hir = ctx.sema.to_def(&enum_ast)?;
let variant_hir_name = variant_hir.name(ctx.db); let variant_hir_name = variant_hir.name(ctx.db());
let enum_module_def = ModuleDef::from(enum_hir); let enum_module_def = ModuleDef::from(enum_hir);
let current_module = enum_hir.module(ctx.db); let current_module = enum_hir.module(ctx.db());
let target = variant.syntax().text_range(); let target = variant.syntax().text_range();
acc.add( acc.add(
AssistId("extract_struct_from_enum_variant"), AssistId("extract_struct_from_enum_variant"),
@ -53,7 +53,7 @@ pub(crate) fn extract_struct_from_enum_variant(
target, target,
|builder| { |builder| {
let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir)); let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir));
let res = definition.find_usages(&ctx.db, None); let res = definition.find_usages(&ctx.db(), None);
let start_offset = variant.parent_enum().syntax().text_range().start(); let start_offset = variant.parent_enum().syntax().text_range().start();
let mut visited_modules_set = FxHashSet::default(); let mut visited_modules_set = FxHashSet::default();
visited_modules_set.insert(current_module); visited_modules_set.insert(current_module);
@ -101,7 +101,7 @@ fn insert_import(
enum_module_def: &ModuleDef, enum_module_def: &ModuleDef,
variant_hir_name: &Name, variant_hir_name: &Name,
) -> Option<()> { ) -> Option<()> {
let db = ctx.db; let db = ctx.db();
let mod_path = module.find_use_path(db, enum_module_def.clone()); let mod_path = module.find_use_path(db, enum_module_def.clone());
if let Some(mut mod_path) = mod_path { if let Some(mut mod_path) = mod_path {
mod_path.segments.pop(); mod_path.segments.pop();

View file

@ -51,11 +51,11 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
let module = ctx.sema.scope(expr.syntax()).module()?; let module = ctx.sema.scope(expr.syntax()).module()?;
let missing_arms: Vec<MatchArm> = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) { let missing_arms: Vec<MatchArm> = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) {
let variants = enum_def.variants(ctx.db); let variants = enum_def.variants(ctx.db());
let mut variants = variants let mut variants = variants
.into_iter() .into_iter()
.filter_map(|variant| build_pat(ctx.db, module, variant)) .filter_map(|variant| build_pat(ctx.db(), module, variant))
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat)) .filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))
.map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block())) .map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -84,11 +84,11 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
// where each tuple represents a proposed match arm. // where each tuple represents a proposed match arm.
enum_defs enum_defs
.into_iter() .into_iter()
.map(|enum_def| enum_def.variants(ctx.db)) .map(|enum_def| enum_def.variants(ctx.db()))
.multi_cartesian_product() .multi_cartesian_product()
.map(|variants| { .map(|variants| {
let patterns = let patterns =
variants.into_iter().filter_map(|variant| build_pat(ctx.db, module, variant)); variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant));
ast::Pat::from(make::tuple_pat(patterns)) ast::Pat::from(make::tuple_pat(patterns))
}) })
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat)) .filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))

View file

@ -41,14 +41,14 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
}; };
let current_module = ctx.sema.scope(&path.syntax()).module()?; let current_module = ctx.sema.scope(&path.syntax()).module()?;
let target_module = def.module(ctx.db)?; let target_module = def.module(ctx.db())?;
let vis = target_module.visibility_of(ctx.db, &def)?; let vis = target_module.visibility_of(ctx.db(), &def)?;
if vis.is_visible_from(ctx.db, current_module.into()) { if vis.is_visible_from(ctx.db(), current_module.into()) {
return None; return None;
}; };
let (offset, target, target_file, target_name) = target_data_for_def(ctx.db, def)?; let (offset, target, target_file, target_name) = target_data_for_def(ctx.db(), def)?;
let missing_visibility = let missing_visibility =
if current_module.krate() == target_module.krate() { "pub(crate)" } else { "pub" }; if current_module.krate() == target_module.krate() { "pub(crate)" } else { "pub" };
@ -72,16 +72,16 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
let (record_field_def, _) = ctx.sema.resolve_record_field(&record_field)?; let (record_field_def, _) = ctx.sema.resolve_record_field(&record_field)?;
let current_module = ctx.sema.scope(record_field.syntax()).module()?; let current_module = ctx.sema.scope(record_field.syntax()).module()?;
let visibility = record_field_def.visibility(ctx.db); let visibility = record_field_def.visibility(ctx.db());
if visibility.is_visible_from(ctx.db, current_module.into()) { if visibility.is_visible_from(ctx.db(), current_module.into()) {
return None; return None;
} }
let parent = record_field_def.parent_def(ctx.db); let parent = record_field_def.parent_def(ctx.db());
let parent_name = parent.name(ctx.db); let parent_name = parent.name(ctx.db());
let target_module = parent.module(ctx.db); let target_module = parent.module(ctx.db());
let in_file_source = record_field_def.source(ctx.db); let in_file_source = record_field_def.source(ctx.db());
let (offset, target) = match in_file_source.value { let (offset, target) = match in_file_source.value {
hir::FieldSource::Named(it) => { hir::FieldSource::Named(it) => {
let s = it.syntax(); let s = it.syntax();
@ -95,9 +95,9 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
let missing_visibility = let missing_visibility =
if current_module.krate() == target_module.krate() { "pub(crate)" } else { "pub" }; if current_module.krate() == target_module.krate() { "pub(crate)" } else { "pub" };
let target_file = in_file_source.file_id.original_file(ctx.db); let target_file = in_file_source.file_id.original_file(ctx.db());
let target_name = record_field_def.name(ctx.db); let target_name = record_field_def.name(ctx.db());
let assist_label = let assist_label =
format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility); format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility);

View file

@ -44,7 +44,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
let def = ctx.sema.to_def(&bind_pat)?; let def = ctx.sema.to_def(&bind_pat)?;
let def = Definition::Local(def); let def = Definition::Local(def);
let refs = def.find_usages(ctx.db, None); let refs = def.find_usages(ctx.db(), None);
if refs.is_empty() { if refs.is_empty() {
mark::hit!(test_not_applicable_if_variable_unused); mark::hit!(test_not_applicable_if_variable_unused);
return None; return None;

View file

@ -90,10 +90,10 @@ fn struct_definition(path: &ast::Path, sema: &Semantics<RootDatabase>) -> Option
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> { fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
Some( Some(
struct_definition(path, &ctx.sema)? struct_definition(path, &ctx.sema)?
.fields(ctx.db) .fields(ctx.db())
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, field)| (field.name(ctx.db).to_string(), idx)) .map(|(idx, field)| (field.name(ctx.db()).to_string(), idx))
.collect(), .collect(),
) )
} }