Add completion module tailored towards visibility modifiers

This commit is contained in:
Lukas Wirth 2022-02-02 15:14:43 +01:00
parent 661d721e20
commit 136dadac9a
8 changed files with 76 additions and 38 deletions

View file

@ -8,7 +8,6 @@ pub(crate) mod format_string;
pub(crate) mod keyword; pub(crate) mod keyword;
pub(crate) mod lifetime; pub(crate) mod lifetime;
pub(crate) mod mod_; pub(crate) mod mod_;
pub(crate) mod use_;
pub(crate) mod pattern; pub(crate) mod pattern;
pub(crate) mod postfix; pub(crate) mod postfix;
pub(crate) mod qualified_path; pub(crate) mod qualified_path;
@ -16,6 +15,8 @@ pub(crate) mod record;
pub(crate) mod snippet; pub(crate) mod snippet;
pub(crate) mod trait_impl; pub(crate) mod trait_impl;
pub(crate) mod unqualified_path; pub(crate) mod unqualified_path;
pub(crate) mod use_;
pub(crate) mod vis;
use std::iter; use std::iter;

View file

@ -34,11 +34,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
let has_block_expr_parent = ctx.has_block_expr_parent(); let has_block_expr_parent = ctx.has_block_expr_parent();
let expects_item = ctx.expects_item(); let expects_item = ctx.expects_item();
if let Some(PathKind::Vis { has_in_token }) = ctx.path_kind() { if let Some(PathKind::Vis { .. }) = ctx.path_kind() {
if !has_in_token {
cov_mark::hit!(kw_completion_in);
add_keyword("in", "in");
}
return; return;
} }
if ctx.has_impl_or_trait_prev_sibling() { if ctx.has_impl_or_trait_prev_sibling() {

View file

@ -62,26 +62,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
} }
match kind { match kind {
// Complete next child module that comes after the qualified module which is still our parent Some(PathKind::Attr { .. } | PathKind::Vis { .. } | PathKind::Use) => {
Some(PathKind::Vis { .. }) => {
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
if let Some(current_module) = ctx.module {
let next_towards_current = current_module
.path_to_root(ctx.db)
.into_iter()
.take_while(|it| it != module)
.next();
if let Some(next) = next_towards_current {
if let Some(name) = next.name(ctx.db) {
cov_mark::hit!(visibility_qualified);
acc.add_resolution(ctx, name, ScopeDef::ModuleDef(next.into()));
}
}
}
}
return;
}
Some(PathKind::Attr { .. } | PathKind::Use) => {
return; return;
} }
Some(PathKind::Pat) => (), Some(PathKind::Pat) => (),

View file

@ -15,15 +15,15 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() { if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
return; return;
} }
let kind = match ctx.path_context { match ctx.path_context {
Some(PathCompletionContext { is_trivial_path: true, kind, .. }) => kind, Some(PathCompletionContext {
kind: Some(PathKind::Vis { .. } | PathKind::Attr { .. } | PathKind::Use { .. }),
..
}) => return,
Some(PathCompletionContext { is_trivial_path: true, .. }) => (),
_ => return, _ => return,
};
match kind {
Some(PathKind::Vis { .. } | PathKind::Attr { .. } | PathKind::Use { .. }) => return,
_ => (),
} }
["self", "super", "crate"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw)); ["self", "super", "crate"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
match &ctx.completion_location { match &ctx.completion_location {

View file

@ -1,8 +1,4 @@
//! Completion for use trees //! Completion for use trees
//!
//! This module uses a bit of static metadata to provide completions
//! for built-in attributes.
//! Non-built-in attribute (excluding derives attributes) completions are done in [`super::unqualified_path`].
use std::iter; use std::iter;

View file

@ -0,0 +1,56 @@
//! Completion for visibility specifiers.
use std::iter;
use hir::ScopeDef;
use crate::{
context::{CompletionContext, PathCompletionContext, PathKind},
Completions,
};
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
let (is_trivial_path, qualifier, has_in_token) = match ctx.path_context {
Some(PathCompletionContext {
kind: Some(PathKind::Vis { has_in_token }),
is_trivial_path,
ref qualifier,
..
}) => (is_trivial_path, qualifier, has_in_token),
_ => return,
};
match qualifier {
Some((path, qualifier)) => {
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = qualifier {
if let Some(current_module) = ctx.module {
let next_towards_current = current_module
.path_to_root(ctx.db)
.into_iter()
.take_while(|it| it != module)
.next();
if let Some(next) = next_towards_current {
if let Some(name) = next.name(ctx.db) {
cov_mark::hit!(visibility_qualified);
acc.add_resolution(ctx, name, ScopeDef::ModuleDef(next.into()));
}
}
}
}
let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
.all(|p| p.segment().and_then(|s| s.super_token()).is_some());
if is_super_chain {
acc.add_keyword(ctx, "super::");
}
}
None if is_trivial_path => {
if !has_in_token {
cov_mark::hit!(kw_completion_in);
acc.add_keyword(ctx, "in");
}
["self", "super", "crate"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
}
_ => {}
}
}

View file

@ -154,6 +154,7 @@ pub fn completions(
completions::attribute::complete_known_attribute_input(&mut acc, &ctx); completions::attribute::complete_known_attribute_input(&mut acc, &ctx);
completions::attribute::complete_attribute(&mut acc, &ctx); completions::attribute::complete_attribute(&mut acc, &ctx);
completions::use_::complete_use_tree(&mut acc, &ctx); completions::use_::complete_use_tree(&mut acc, &ctx);
completions::vis::complete_vis(&mut acc, &ctx);
completions::fn_param::complete_fn_param(&mut acc, &ctx); completions::fn_param::complete_fn_param(&mut acc, &ctx);
completions::keyword::complete_expr_keyword(&mut acc, &ctx); completions::keyword::complete_expr_keyword(&mut acc, &ctx);
completions::snippet::complete_expr_snippet(&mut acc, &ctx); completions::snippet::complete_expr_snippet(&mut acc, &ctx);

View file

@ -17,6 +17,9 @@ pub($0)
"#, "#,
expect![[r#" expect![[r#"
kw in kw in
kw self
kw super
kw crate
"#]], "#]],
); );
} }
@ -27,7 +30,11 @@ fn after_in_kw() {
r#" r#"
pub(in $0) pub(in $0)
"#, "#,
expect![[r#""#]], expect![[r#"
kw self
kw super
kw crate
"#]],
); );
} }