Remove some usages of Completions::add_resolution

This commit is contained in:
Lukas Wirth 2022-06-20 14:47:30 +02:00
parent 7685245282
commit 90483321ee
6 changed files with 62 additions and 54 deletions

View file

@ -22,7 +22,7 @@ pub(crate) mod vis;
use std::iter; use std::iter;
use hir::{db::HirDatabase, known, ScopeDef}; use hir::{known, ScopeDef};
use ide_db::SymbolKind; use ide_db::SymbolKind;
use syntax::ast; use syntax::ast;
@ -46,22 +46,6 @@ use crate::{
CompletionContext, CompletionItem, CompletionItemKind, CompletionContext, CompletionItem, CompletionItemKind,
}; };
fn module_or_attr(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
match def {
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(db) => Some(def),
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
_ => None,
}
}
fn module_or_fn_macro(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
match def {
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(db) => Some(def),
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
_ => None,
}
}
/// Represents an in-progress set of completions being built. /// Represents an in-progress set of completions being built.
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Completions { pub struct Completions {
@ -184,6 +168,15 @@ impl Completions {
self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution).build()); self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution).build());
} }
pub(crate) fn add_module(
&mut self,
ctx: &CompletionContext,
module: hir::Module,
local_name: hir::Name,
) {
self.add_resolution(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
}
pub(crate) fn add_macro( pub(crate) fn add_macro(
&mut self, &mut self,
ctx: &CompletionContext, ctx: &CompletionContext,
@ -486,16 +479,19 @@ pub(super) fn complete_name_ref(
match kind { match kind {
NameRefKind::Path(path_ctx) => { NameRefKind::Path(path_ctx) => {
flyimport::import_on_the_fly_path(acc, ctx, path_ctx); flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
match &path_ctx.kind { match &path_ctx.kind {
PathKind::Expr { expr_ctx } => { PathKind::Expr { expr_ctx } => {
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx); expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx);
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx); item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
record::complete_record_expr_func_update(acc, ctx, path_ctx, expr_ctx); record::complete_record_expr_func_update(acc, ctx, path_ctx, expr_ctx);
snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx); snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
} }
PathKind::Type { location } => { PathKind::Type { location } => {
r#type::complete_type_path(acc, ctx, path_ctx, location); r#type::complete_type_path(acc, ctx, path_ctx, location);
match location { match location {
TypeLocation::TupleField => { TypeLocation::TupleField => {
field::complete_field_list_tuple_variant(acc, ctx, path_ctx); field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
@ -511,13 +507,14 @@ pub(super) fn complete_name_ref(
} }
} }
PathKind::Attr { attr_ctx } => { PathKind::Attr { attr_ctx } => {
attribute::complete_attribute(acc, ctx, path_ctx, attr_ctx); attribute::complete_attribute_path(acc, ctx, path_ctx, attr_ctx);
} }
PathKind::Derive { existing_derives } => { PathKind::Derive { existing_derives } => {
attribute::complete_derive(acc, ctx, path_ctx, existing_derives); attribute::complete_derive_path(acc, ctx, path_ctx, existing_derives);
} }
PathKind::Item { kind } => { PathKind::Item { kind } => {
item_list::complete_item_list(acc, ctx, path_ctx, kind); item_list::complete_item_list(acc, ctx, path_ctx, kind);
snippet::complete_item_snippet(acc, ctx, path_ctx, kind); snippet::complete_item_snippet(acc, ctx, path_ctx, kind);
if let ItemListKind::TraitImpl(impl_) = kind { if let ItemListKind::TraitImpl(impl_) = kind {
item_list::trait_impl::complete_trait_impl_item_by_name( item_list::trait_impl::complete_trait_impl_item_by_name(
@ -532,7 +529,7 @@ pub(super) fn complete_name_ref(
vis::complete_vis_path(acc, ctx, path_ctx, has_in_token); vis::complete_vis_path(acc, ctx, path_ctx, has_in_token);
} }
PathKind::Use => { PathKind::Use => {
use_::complete_use_tree(acc, ctx, path_ctx, nameref); use_::complete_use_path(acc, ctx, path_ctx, nameref);
} }
} }
} }

View file

@ -17,7 +17,6 @@ use syntax::{
}; };
use crate::{ use crate::{
completions::module_or_attr,
context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified}, context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
item::CompletionItem, item::CompletionItem,
Completions, Completions,
@ -28,7 +27,7 @@ mod derive;
mod lint; mod lint;
mod repr; mod repr;
pub(crate) use self::derive::complete_derive; pub(crate) use self::derive::complete_derive_path;
/// Complete inputs to known builtin attributes as well as derive attributes /// Complete inputs to known builtin attributes as well as derive attributes
pub(crate) fn complete_known_attribute_input( pub(crate) fn complete_known_attribute_input(
@ -69,7 +68,7 @@ pub(crate) fn complete_known_attribute_input(
Some(()) Some(())
} }
pub(crate) fn complete_attribute( pub(crate) fn complete_attribute_path(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext, ctx: &CompletionContext,
PathCompletionCtx { qualified, .. }: &PathCompletionCtx, PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
@ -88,8 +87,14 @@ pub(crate) fn complete_attribute(
} }
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
if let Some(def) = module_or_attr(ctx.db, def) { match def {
acc.add_resolution(ctx, name, def); hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
acc.add_macro(ctx, m, name)
}
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
acc.add_module(ctx, m, name)
}
_ => (),
} }
} }
return; return;
@ -98,10 +103,12 @@ pub(crate) fn complete_attribute(
Qualified::Absolute => acc.add_crate_roots(ctx), Qualified::Absolute => acc.add_crate_roots(ctx),
// only show modules in a fresh UseTree // only show modules in a fresh UseTree
Qualified::No => { Qualified::No => {
ctx.process_all_names(&mut |name, def| { ctx.process_all_names(&mut |name, def| match def {
if let Some(def) = module_or_attr(ctx.db, def) { hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
acc.add_resolution(ctx, name, def); acc.add_macro(ctx, m, name)
} }
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
_ => (),
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }

View file

@ -10,7 +10,7 @@ use crate::{
Completions, Completions,
}; };
pub(crate) fn complete_derive( pub(crate) fn complete_derive_path(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext, ctx: &CompletionContext,
PathCompletionCtx { qualified, .. }: &PathCompletionCtx, PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
@ -29,15 +29,14 @@ pub(crate) fn complete_derive(
} }
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
let add_def = match def { match def {
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => { ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac))
!existing_derives.contains(&mac) && mac.is_derive(ctx.db) if !existing_derives.contains(&mac) && mac.is_derive(ctx.db) =>
{
acc.add_macro(ctx, mac, name)
} }
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true, ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
_ => false, _ => (),
};
if add_def {
acc.add_resolution(ctx, name, def);
} }
} }
} }
@ -51,8 +50,8 @@ pub(crate) fn complete_derive(
{ {
mac mac
} }
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => { ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
return acc.add_resolution(ctx, name, def); return acc.add_module(ctx, m, name);
} }
_ => return, _ => return,
}; };
@ -60,7 +59,7 @@ pub(crate) fn complete_derive(
match (core, mac.module(ctx.db).krate()) { match (core, mac.module(ctx.db).krate()) {
// show derive dependencies for `core`/`std` derives // show derive dependencies for `core`/`std` derives
(Some(core), mac_krate) if core == mac_krate => {} (Some(core), mac_krate) if core == mac_krate => {}
_ => return acc.add_resolution(ctx, name, def), _ => return acc.add_macro(ctx, mac, name),
}; };
let name_ = name.to_smol_str(); let name_ = name.to_smol_str();
@ -93,7 +92,7 @@ pub(crate) fn complete_derive(
item.lookup_by(lookup); item.lookup_by(lookup);
item.add_to(acc); item.add_to(acc);
} }
None => acc.add_resolution(ctx, name, def), None => acc.add_macro(ctx, mac, name),
} }
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);

View file

@ -1,7 +1,6 @@
//! Completion of paths and keywords at item list position. //! Completion of paths and keywords at item list position.
use crate::{ use crate::{
completions::module_or_fn_macro,
context::{ExprCtx, ItemListKind, PathCompletionCtx, Qualified}, context::{ExprCtx, ItemListKind, PathCompletionCtx, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -41,8 +40,14 @@ pub(crate) fn complete_item_list(
.. ..
} => { } => {
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
if let Some(def) = module_or_fn_macro(ctx.db, def) { match def {
acc.add_resolution(ctx, name, def); hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(ctx.db) => {
acc.add_macro(ctx, m, name)
}
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
acc.add_module(ctx, m, name)
}
_ => (),
} }
} }
@ -52,10 +57,12 @@ pub(crate) fn complete_item_list(
} }
Qualified::Absolute => acc.add_crate_roots(ctx), Qualified::Absolute => acc.add_crate_roots(ctx),
Qualified::No if ctx.qualifier_ctx.none() => { Qualified::No if ctx.qualifier_ctx.none() => {
ctx.process_all_names(&mut |name, def| { ctx.process_all_names(&mut |name, def| match def {
if let Some(def) = module_or_fn_macro(ctx.db, def) { hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(ctx.db) => {
acc.add_resolution(ctx, name, def); acc.add_macro(ctx, m, name)
} }
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
_ => (),
}); });
acc.add_nameref_keywords_with_colon(ctx); acc.add_nameref_keywords_with_colon(ctx);
} }

View file

@ -10,7 +10,7 @@ use crate::{
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
}; };
pub(crate) fn complete_use_tree( pub(crate) fn complete_use_path(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext, ctx: &CompletionContext,
PathCompletionCtx { qualified, use_tree_parent, .. }: &PathCompletionCtx, PathCompletionCtx { qualified, use_tree_parent, .. }: &PathCompletionCtx,
@ -96,8 +96,8 @@ pub(crate) fn complete_use_tree(
cov_mark::hit!(unqualified_path_selected_only); cov_mark::hit!(unqualified_path_selected_only);
ctx.process_all_names(&mut |name, res| { ctx.process_all_names(&mut |name, res| {
match res { match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => { ScopeDef::ModuleDef(hir::ModuleDef::Module(module)) => {
acc.add_resolution(ctx, name, res); acc.add_module(ctx, module, name);
} }
ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => { ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
// exclude prelude enum // exclude prelude enum

View file

@ -1,7 +1,5 @@
//! Completion for visibility specifiers. //! Completion for visibility specifiers.
use hir::ScopeDef;
use crate::{ use crate::{
context::{CompletionContext, PathCompletionCtx, Qualified}, context::{CompletionContext, PathCompletionCtx, Qualified},
Completions, Completions,
@ -25,7 +23,7 @@ pub(crate) fn complete_vis_path(
if let Some(next) = next_towards_current { if let Some(next) = next_towards_current {
if let Some(name) = next.name(ctx.db) { if let Some(name) = next.name(ctx.db) {
cov_mark::hit!(visibility_qualified); cov_mark::hit!(visibility_qualified);
acc.add_resolution(ctx, name, ScopeDef::ModuleDef(next.into())); acc.add_module(ctx, next, name);
} }
} }