Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Lift out IdentContext from CompletionContext #12594

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 58 additions & 15 deletions crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub(crate) mod vis;
use std::iter;

use hir::{known, ScopeDef};
use ide_db::SymbolKind;
use ide_db::{imports::import_assets::LocatedImport, SymbolKind};
use syntax::ast;

use crate::{
context::{
ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind, PathKind, PatternContext,
TypeLocation, Visible,
DotAccess, ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind,
PathCompletionCtx, PathKind, PatternContext, TypeLocation, Visible,
},
item::Builder,
render::{
Expand All @@ -38,7 +38,7 @@ use crate::{
literal::{render_struct_literal, render_variant_lit},
macro_::render_macro,
pattern::{render_struct_pat, render_variant_pat},
render_field, render_resolution, render_resolution_simple, render_tuple_field,
render_field, render_path_resolution, render_resolution_simple, render_tuple_field,
type_alias::{render_type_alias, render_type_alias_with_eq},
union_literal::render_union_literal,
RenderContext,
Expand Down Expand Up @@ -137,23 +137,27 @@ impl Completions {
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
ctx.process_all_names(&mut |name, res| match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
self.add_resolution(ctx, name, res);
self.add_module(ctx, m, name);
}
_ => (),
});
}

pub(crate) fn add_resolution(
pub(crate) fn add_path_resolution(
&mut self,
ctx: &CompletionContext,
path_ctx: &PathCompletionCtx,
local_name: hir::Name,
resolution: hir::ScopeDef,
) {
if ctx.is_scope_def_hidden(resolution) {
cov_mark::hit!(qualified_path_doc_hidden);
return;
}
self.add(render_resolution(RenderContext::new(ctx), local_name, resolution).build());
self.add(
render_path_resolution(RenderContext::new(ctx), path_ctx, local_name, resolution)
.build(),
);
}

pub(crate) fn add_resolution_simple(
Expand All @@ -174,12 +178,13 @@ impl Completions {
module: hir::Module,
local_name: hir::Name,
) {
self.add_resolution(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
self.add_resolution_simple(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
}

pub(crate) fn add_macro(
&mut self,
ctx: &CompletionContext,
path_ctx: &PathCompletionCtx,
mac: hir::Macro,
local_name: hir::Name,
) {
Expand All @@ -191,6 +196,7 @@ impl Completions {
self.add(
render_macro(
RenderContext::new(ctx).private_editable(is_private_editable),
path_ctx,
local_name,
mac,
)
Expand All @@ -201,6 +207,7 @@ impl Completions {
pub(crate) fn add_function(
&mut self,
ctx: &CompletionContext,
path_ctx: &PathCompletionCtx,
func: hir::Function,
local_name: Option<hir::Name>,
) {
Expand All @@ -212,6 +219,7 @@ impl Completions {
self.add(
render_fn(
RenderContext::new(ctx).private_editable(is_private_editable),
path_ctx,
local_name,
func,
)
Expand All @@ -222,6 +230,7 @@ impl Completions {
pub(crate) fn add_method(
&mut self,
ctx: &CompletionContext,
dot_access: &DotAccess,
func: hir::Function,
receiver: Option<hir::Name>,
local_name: Option<hir::Name>,
Expand All @@ -234,6 +243,7 @@ impl Completions {
self.add(
render_method(
RenderContext::new(ctx).private_editable(is_private_editable),
dot_access,
receiver,
local_name,
func,
Expand All @@ -242,6 +252,32 @@ impl Completions {
);
}

pub(crate) fn add_method_with_import(
&mut self,
ctx: &CompletionContext,
dot_access: &DotAccess,
func: hir::Function,
import: LocatedImport,
) {
let is_private_editable = match ctx.is_visible(&func) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
self.add(
render_method(
RenderContext::new(ctx)
.private_editable(is_private_editable)
.import_to_add(Some(import)),
dot_access,
None,
None,
func,
)
.build(),
);
}

pub(crate) fn add_const(&mut self, ctx: &CompletionContext, konst: hir::Const) {
let is_private_editable = match ctx.is_visible(&konst) {
Visible::Yes => false,
Expand Down Expand Up @@ -277,11 +313,12 @@ impl Completions {
pub(crate) fn add_qualified_enum_variant(
&mut self,
ctx: &CompletionContext,
path_ctx: &PathCompletionCtx,
variant: hir::Variant,
path: hir::ModPath,
) {
if let Some(builder) =
render_variant_lit(RenderContext::new(ctx), None, variant, Some(path))
render_variant_lit(RenderContext::new(ctx), path_ctx, None, variant, Some(path))
{
self.add(builder.build());
}
Expand All @@ -290,11 +327,12 @@ impl Completions {
pub(crate) fn add_enum_variant(
&mut self,
ctx: &CompletionContext,
path_ctx: &PathCompletionCtx,
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
if let Some(builder) =
render_variant_lit(RenderContext::new(ctx), local_name, variant, None)
render_variant_lit(RenderContext::new(ctx), path_ctx, local_name, variant, None)
{
self.add(builder.build());
}
Expand Down Expand Up @@ -324,12 +362,13 @@ impl Completions {
pub(crate) fn add_struct_literal(
&mut self,
ctx: &CompletionContext,
path_ctx: &PathCompletionCtx,
strukt: hir::Struct,
path: Option<hir::ModPath>,
local_name: Option<hir::Name>,
) {
if let Some(builder) =
render_struct_literal(RenderContext::new(ctx), strukt, path, local_name)
render_struct_literal(RenderContext::new(ctx), path_ctx, strukt, path, local_name)
{
self.add(builder.build());
}
Expand Down Expand Up @@ -369,11 +408,13 @@ impl Completions {
pub(crate) fn add_variant_pat(
&mut self,
ctx: &CompletionContext,
pattern_ctx: &PatternContext,
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
self.add_opt(render_variant_pat(
RenderContext::new(ctx),
pattern_ctx,
variant,
local_name.clone(),
None,
Expand All @@ -383,20 +424,22 @@ impl Completions {
pub(crate) fn add_qualified_variant_pat(
&mut self,
ctx: &CompletionContext,
pattern_ctx: &PatternContext,
variant: hir::Variant,
path: hir::ModPath,
) {
let path = Some(&path);
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, path));
self.add_opt(render_variant_pat(RenderContext::new(ctx), pattern_ctx, variant, None, path));
}

pub(crate) fn add_struct_pat(
&mut self,
ctx: &CompletionContext,
pattern_ctx: &PatternContext,
strukt: hir::Struct,
local_name: Option<hir::Name>,
) {
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
self.add_opt(render_struct_pat(RenderContext::new(ctx), pattern_ctx, strukt, local_name));
}
}

Expand Down Expand Up @@ -541,8 +584,8 @@ pub(super) fn complete_name_ref(
NameRefKind::Keyword(item) => {
keyword::complete_for_and_where(acc, ctx, item);
}
NameRefKind::RecordExpr(record_expr) => {
record::complete_record_expr_fields(acc, ctx, record_expr);
NameRefKind::RecordExpr { dot_prefix, expr } => {
record::complete_record_expr_fields(acc, ctx, expr, dot_prefix);
}
NameRefKind::Pattern(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
}
Expand Down
13 changes: 8 additions & 5 deletions crates/ide-completion/src/completions/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) use self::derive::complete_derive_path;
pub(crate) fn complete_known_attribute_input(
acc: &mut Completions,
ctx: &CompletionContext,
&colon_prefix: &bool,
fake_attribute_under_caret: &ast::Attr,
) -> Option<()> {
let attribute = fake_attribute_under_caret;
Expand All @@ -47,7 +48,9 @@ pub(crate) fn complete_known_attribute_input(

match path.text().as_str() {
"repr" => repr::complete_repr(acc, ctx, tt),
"feature" => lint::complete_lint(acc, ctx, &parse_tt_as_comma_sep_paths(tt)?, FEATURES),
"feature" => {
lint::complete_lint(acc, ctx, colon_prefix, &parse_tt_as_comma_sep_paths(tt)?, FEATURES)
}
"allow" | "warn" | "deny" | "forbid" => {
let existing_lints = parse_tt_as_comma_sep_paths(tt)?;

Expand All @@ -60,7 +63,7 @@ pub(crate) fn complete_known_attribute_input(
.cloned()
.collect();

lint::complete_lint(acc, ctx, &existing_lints, &lints);
lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
}
"cfg" => cfg::complete_cfg(acc, ctx),
_ => (),
Expand All @@ -71,7 +74,7 @@ pub(crate) fn complete_known_attribute_input(
pub(crate) fn complete_attribute_path(
acc: &mut Completions,
ctx: &CompletionContext,
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
&AttrCtx { kind, annotated_item_kind }: &AttrCtx,
) {
let is_inner = kind == AttrKind::Inner;
Expand All @@ -89,7 +92,7 @@ pub(crate) fn complete_attribute_path(
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
match def {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
acc.add_macro(ctx, m, name)
acc.add_macro(ctx, path_ctx, m, name)
}
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
acc.add_module(ctx, m, name)
Expand All @@ -105,7 +108,7 @@ pub(crate) fn complete_attribute_path(
Qualified::No => {
ctx.process_all_names(&mut |name, def| match def {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
acc.add_macro(ctx, m, name)
acc.add_macro(ctx, path_ctx, m, name)
}
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
_ => (),
Expand Down
8 changes: 4 additions & 4 deletions crates/ide-completion/src/completions/attribute/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
pub(crate) fn complete_derive_path(
acc: &mut Completions,
ctx: &CompletionContext,
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
existing_derives: &ExistingDerives,
) {
let core = ctx.famous_defs().core();
Expand All @@ -33,7 +33,7 @@ pub(crate) fn complete_derive_path(
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac))
if !existing_derives.contains(&mac) && mac.is_derive(ctx.db) =>
{
acc.add_macro(ctx, mac, name)
acc.add_macro(ctx, path_ctx, mac, name)
}
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
_ => (),
Expand All @@ -59,7 +59,7 @@ pub(crate) fn complete_derive_path(
match (core, mac.module(ctx.db).krate()) {
// show derive dependencies for `core`/`std` derives
(Some(core), mac_krate) if core == mac_krate => {}
_ => return acc.add_macro(ctx, mac, name),
_ => return acc.add_macro(ctx, path_ctx, mac, name),
};

let name_ = name.to_smol_str();
Expand Down Expand Up @@ -92,7 +92,7 @@ pub(crate) fn complete_derive_path(
item.lookup_by(lookup);
item.add_to(acc);
}
None => acc.add_macro(ctx, mac, name),
None => acc.add_macro(ctx, path_ctx, mac, name),
}
});
acc.add_nameref_keywords_with_colon(ctx);
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-completion/src/completions/attribute/lint.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! Completion for lints
use ide_db::{generated::lints::Lint, SymbolKind};
use syntax::{ast, T};
use syntax::ast;

use crate::{context::CompletionContext, item::CompletionItem, Completions};

pub(super) fn complete_lint(
acc: &mut Completions,
ctx: &CompletionContext,
is_qualified: bool,
existing_lints: &[ast::Path],
lints_completions: &[Lint],
) {
let is_qualified = ctx.previous_token_is(T![:]);
for &Lint { label, description } in lints_completions {
let (qual, name) = {
// FIXME: change `Lint`'s label to not store a path in it but split the prefix off instead?
Expand Down
14 changes: 12 additions & 2 deletions crates/ide-completion/src/completions/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext, dot_a
|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
);
}
complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, func, None, None));
complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None));
}

pub(crate) fn complete_undotted_self(
Expand Down Expand Up @@ -68,7 +68,17 @@ pub(crate) fn complete_undotted_self(
|acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
);
complete_methods(ctx, &ty, |func| {
acc.add_method(ctx, func, Some(hir::known::SELF_PARAM), None)
acc.add_method(
ctx,
&DotAccess {
receiver: None,
receiver_ty: None,
kind: DotAccessKind::Method { has_parens: false },
},
func,
Some(hir::known::SELF_PARAM),
None,
)
});
}

Expand Down
Loading