This commit is contained in:
Lukas Wirth 2022-04-23 02:21:27 +02:00
parent f8c32df7cd
commit ea45e54458
5 changed files with 29 additions and 21 deletions

View File

@ -22,8 +22,8 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
Some(LifetimeContext::LifetimeParam { is_decl: false, param }) => Some(param),
_ => return,
};
let param_lifetime = match (&ctx.name_syntax, lp.and_then(|lp| lp.lifetime())) {
(Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return,
let param_lifetime = match (ctx.lifetime(), lp.and_then(|lp| lp.lifetime())) {
(Some(lt), Some(lp)) if lp == lt.clone() => return,
(Some(_), Some(lp)) => Some(lp),
_ => None,
};

View File

@ -11,12 +11,9 @@
};
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
let (is_absolute_path, qualifier) = match ctx.path_context {
let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Use),
is_absolute_path,
ref qualifier,
..
kind: Some(PathKind::Use), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
_ => return,
};
@ -45,31 +42,27 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
if let Some(list) = ctx.token.ancestors().find_map(ast::UseTreeList::cast) {
let use_tree = list.parent_use_tree();
if use_tree.path().as_ref() == Some(path) {
for tree in list.use_trees() {
if tree.is_simple_path() {
if let Some(name) =
tree.path().and_then(|path| path.as_single_name_ref())
{
for tree in list.use_trees().filter(|tree| tree.is_simple_path()) {
if let Some(name) = tree.path().and_then(|path| path.as_single_name_ref()) {
already_imported_names.insert(name.to_string());
}
}
}
}
}
match resolution {
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
let module_scope = module.scope(ctx.db, Some(ctx.module));
let unknown_is_current = |name: &hir::Name| {
matches!(
ctx.name_syntax.as_ref(),
Some(ast::NameLike::NameRef(name_ref))
if name_ref.syntax().text() == name.to_smol_str().as_str()
ctx.name_ref(),
Some(name_ref) if name_ref.syntax().text() == name.to_smol_str().as_str()
)
};
for (name, def) in module_scope {
let is_name_already_imported =
already_imported_names.contains(name.as_text().unwrap().as_str());
let is_name_already_imported = name
.as_text()
.map_or(false, |text| already_imported_names.contains(text.as_str()));
let add_resolution = match def {
ScopeDef::Unknown if unknown_is_current(&name) => {

View File

@ -8,11 +8,11 @@
};
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
let (is_absolute_path, qualifier, has_in_token) = match ctx.path_context {
let (&is_absolute_path, qualifier, &has_in_token) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Vis { has_in_token }),
is_absolute_path,
ref qualifier,
qualifier,
..
}) => (is_absolute_path, qualifier, has_in_token),
_ => return,

View File

@ -50,6 +50,7 @@ pub(super) enum PathKind {
Type,
Attr { kind: AttrKind, annotated_item_kind: Option<SyntaxKind> },
Derive,
// This should be removed in favor of `has_macro_bang` in PathCompletionContext
Mac,
Pat,
Vis { has_in_token: bool },
@ -196,6 +197,14 @@ pub(crate) fn source_range(&self) -> TextRange {
}
}
pub(crate) fn name_ref(&self) -> Option<&ast::NameRef> {
self.name_syntax.as_ref().and_then(ast::NameLike::as_name_ref)
}
pub(crate) fn lifetime(&self) -> Option<&ast::Lifetime> {
self.name_syntax.as_ref().and_then(ast::NameLike::as_lifetime)
}
pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
}

View File

@ -426,6 +426,12 @@ pub fn as_name_ref(&self) -> Option<&ast::NameRef> {
_ => None,
}
}
pub fn as_lifetime(&self) -> Option<&ast::Lifetime> {
match self {
NameLike::Lifetime(lifetime) => Some(lifetime),
_ => None,
}
}
}
impl ast::AstNode for NameLike {