Skip to content
Closed
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
22 changes: 20 additions & 2 deletions crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,29 @@ impl Completions {
}

pub(crate) fn add_nameref_keywords_with_colon(&mut self, ctx: &CompletionContext) {
["self::", "super::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
["self::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));

if ctx.depth_from_crate_root > 0 {
self.add_keyword(ctx, "super::");
}
}

pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext) {
["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
["self", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));

if ctx.depth_from_crate_root > 0 {
self.add_keyword(ctx, "super");
}
}

pub(crate) fn add_super_kw_acc_to_mod_depth(
&mut self,
super_chain_len: &usize,
ctx: &CompletionContext,
) {
if *super_chain_len > 0 && *super_chain_len < ctx.depth_from_crate_root {
self.add_keyword(ctx, "super::");
}
}

pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) {
Expand Down
6 changes: 2 additions & 4 deletions crates/ide-completion/src/completions/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
};

match qualifier {
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
if *is_super_chain {
acc.add_keyword(ctx, "super::");
}
Some(PathQualifierCtx { resolution, super_chain_len, .. }) => {
acc.add_super_kw_acc_to_mod_depth(super_chain_len, ctx);

let module = match resolution {
Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
Expand Down
6 changes: 2 additions & 4 deletions crates/ide-completion/src/completions/attribute/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let core = ctx.famous_defs().core();

match qualifier {
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
if *is_super_chain {
acc.add_keyword(ctx, "super::");
}
Some(PathQualifierCtx { resolution, super_chain_len, .. }) => {
acc.add_super_kw_acc_to_mod_depth(super_chain_len, ctx);

let module = match resolution {
Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
Expand Down
6 changes: 2 additions & 4 deletions crates/ide-completion/src/completions/item_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
}

match path_qualifier {
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
Some(PathQualifierCtx { resolution, super_chain_len, .. }) => {
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
if let Some(def) = module_or_fn_macro(ctx.db, def) {
Expand All @@ -59,9 +59,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
}
}

if *is_super_chain {
acc.add_keyword(ctx, "super::");
}
acc.add_super_kw_acc_to_mod_depth(super_chain_len, ctx);
}
None if is_absolute_path => acc.add_crate_roots(ctx),
None if ctx.qualifier_ctx.none() => {
Expand Down
6 changes: 2 additions & 4 deletions crates/ide-completion/src/completions/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ fn pattern_path_completion(
PathCompletionCtx { qualifier, is_absolute_path, .. }: &PathCompletionCtx,
) {
match qualifier {
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
if *is_super_chain {
acc.add_keyword(ctx, "super::");
}
Some(PathQualifierCtx { resolution, super_chain_len, .. }) => {
acc.add_super_kw_acc_to_mod_depth(super_chain_len, ctx);

let resolution = match resolution {
Some(it) => it,
Expand Down
6 changes: 2 additions & 4 deletions crates/ide-completion/src/completions/use_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
};

match qualifier {
Some(PathQualifierCtx { path, resolution, is_super_chain, use_tree_parent, .. }) => {
if *is_super_chain {
acc.add_keyword(ctx, "super::");
}
Some(PathQualifierCtx { path, resolution, super_chain_len, use_tree_parent, .. }) => {
acc.add_super_kw_acc_to_mod_depth(super_chain_len, ctx);
// only show `self` in a new use-tree when the qualifier doesn't end in self
let not_preceded_by_self = *use_tree_parent
&& !matches!(
Expand Down
6 changes: 2 additions & 4 deletions crates/ide-completion/src/completions/vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext)
};

match qualifier {
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
Some(PathQualifierCtx { resolution, super_chain_len, .. }) => {
// Try completing next child module of the path that is still a parent of the current module
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
let next_towards_current = ctx
Expand All @@ -36,9 +36,7 @@ pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext)
}
}

if *is_super_chain {
acc.add_keyword(ctx, "super::");
}
acc.add_super_kw_acc_to_mod_depth(super_chain_len, ctx);
}
None if !is_absolute_path => {
if !has_in_token {
Expand Down
32 changes: 27 additions & 5 deletions crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl PathCompletionCtx {
pub(crate) struct PathQualifierCtx {
pub(crate) path: ast::Path,
pub(crate) resolution: Option<PathResolution>,
/// Whether this path consists solely of `super` segments
pub(crate) is_super_chain: bool,
/// How many `super` segments are present in the path
pub(crate) super_chain_len: usize,
/// Whether the qualifier comes from a use tree parent or not
pub(crate) use_tree_parent: bool,
/// <_>
Expand Down Expand Up @@ -291,6 +291,12 @@ pub(crate) struct CompletionContext<'a> {
pub(super) existing_derives: FxHashSet<hir::Macro>,

pub(super) locals: FxHashMap<Name, Local>,

// - crate-root
// - mod foo
// - mod bar
// Here depth will be 2: {[bar<->foo], [foo<->crate-root]}
pub(super) depth_from_crate_root: usize,
}

impl<'a> CompletionContext<'a> {
Expand Down Expand Up @@ -490,6 +496,20 @@ impl<'a> CompletionContext<'a> {
}
});

let mut mod_ = module;
let mut depth = 0;
loop {
match mod_.parent(db) {
Some(mod_parent) => {
mod_ = mod_parent;
depth += 1;
}
None => {
break;
}
}
}

let mut ctx = CompletionContext {
sema,
scope,
Expand All @@ -513,6 +533,7 @@ impl<'a> CompletionContext<'a> {
qualifier_ctx: Default::default(),
existing_derives: Default::default(),
locals,
depth_from_crate_root: depth,
};
ctx.expand_and_fill(
original_file.syntax().clone(),
Expand Down Expand Up @@ -1285,8 +1306,9 @@ impl<'a> CompletionContext<'a> {
.map(|it| it.parent_path());
path_ctx.qualifier = path.map(|path| {
let res = sema.resolve_path(&path);
let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
.all(|p| p.segment().and_then(|s| s.super_token()).is_some());
let super_chain_len = iter::successors(Some(path.clone()), |p| p.qualifier())
.filter_map(|p| p.segment().and_then(|s| s.super_token()))
.count();

// `<_>::$0`
let is_infer_qualifier = path.qualifier().is_none()
Expand All @@ -1301,7 +1323,7 @@ impl<'a> CompletionContext<'a> {
PathQualifierCtx {
path,
resolution: res,
is_super_chain,
super_chain_len,
use_tree_parent,
is_infer_qualifier,
}
Expand Down
Loading