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), Some(LifetimeContext::LifetimeParam { is_decl: false, param }) => Some(param),
_ => return, _ => return,
}; };
let param_lifetime = match (&ctx.name_syntax, lp.and_then(|lp| lp.lifetime())) { let param_lifetime = match (ctx.lifetime(), lp.and_then(|lp| lp.lifetime())) {
(Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return, (Some(lt), Some(lp)) if lp == lt.clone() => return,
(Some(_), Some(lp)) => Some(lp), (Some(_), Some(lp)) => Some(lp),
_ => None, _ => None,
}; };

View File

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

View File

@ -8,11 +8,11 @@ use crate::{
}; };
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) { 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 { Some(PathCompletionCtx {
kind: Some(PathKind::Vis { has_in_token }), kind: Some(PathKind::Vis { has_in_token }),
is_absolute_path, is_absolute_path,
ref qualifier, qualifier,
.. ..
}) => (is_absolute_path, qualifier, has_in_token), }) => (is_absolute_path, qualifier, has_in_token),
_ => return, _ => return,

View File

@ -50,6 +50,7 @@ pub(super) enum PathKind {
Type, Type,
Attr { kind: AttrKind, annotated_item_kind: Option<SyntaxKind> }, Attr { kind: AttrKind, annotated_item_kind: Option<SyntaxKind> },
Derive, Derive,
// This should be removed in favor of `has_macro_bang` in PathCompletionContext
Mac, Mac,
Pat, Pat,
Vis { has_in_token: bool }, Vis { has_in_token: bool },
@ -196,6 +197,14 @@ impl<'a> CompletionContext<'a> {
} }
} }
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 { pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind) self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
} }

View File

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