mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-14 17:07:26 +00:00
Auto merge of #12060 - Veykril:completion-ctx, r=Veykril
minor: Simplify bors r+
This commit is contained in:
commit
c606229241
5 changed files with 29 additions and 21 deletions
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -11,12 +11,9 @@ use crate::{
|
|||
};
|
||||
|
||||
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,13 +42,9 @@ 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())
|
||||
{
|
||||
already_imported_names.insert(name.to_string());
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,14 +55,14 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
|
|||
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) => {
|
||||
|
|
|
@ -8,11 +8,11 @@ use crate::{
|
|||
};
|
||||
|
||||
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,
|
||||
|
|
|
@ -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 @@ 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 {
|
||||
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
|
||||
}
|
||||
|
|
|
@ -426,6 +426,12 @@ impl NameLike {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn as_lifetime(&self) -> Option<&ast::Lifetime> {
|
||||
match self {
|
||||
NameLike::Lifetime(lifetime) => Some(lifetime),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::AstNode for NameLike {
|
||||
|
|
Loading…
Reference in a new issue