mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Split namelike into the corresponding completion contexts
This commit is contained in:
parent
6a045c7029
commit
99fa37d6e3
@ -9,8 +9,15 @@ use crate::{
|
|||||||
|
|
||||||
/// Complete dot accesses, i.e. fields or methods.
|
/// Complete dot accesses, i.e. fields or methods.
|
||||||
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
let dot_receiver = match ctx.dot_receiver() {
|
let (dot_access, dot_receiver) = match &ctx.nameref_ctx {
|
||||||
Some(expr) => expr,
|
Some(NameRefContext {
|
||||||
|
dot_access:
|
||||||
|
Some(
|
||||||
|
access @ (DotAccess::Method { receiver: Some(receiver), .. }
|
||||||
|
| DotAccess::Field { receiver: Some(receiver), .. }),
|
||||||
|
),
|
||||||
|
..
|
||||||
|
}) => (access, receiver),
|
||||||
_ => return complete_undotted_self(acc, ctx),
|
_ => return complete_undotted_self(acc, ctx),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -19,10 +26,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
|||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches!(
|
if let DotAccess::Method { .. } = dot_access {
|
||||||
ctx.nameref_ctx,
|
|
||||||
Some(NameRefContext { dot_access: Some(DotAccess::Method { .. }), .. }),
|
|
||||||
) {
|
|
||||||
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
|
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
|
||||||
} else {
|
} else {
|
||||||
complete_fields(
|
complete_fields(
|
||||||
|
@ -217,10 +217,9 @@ pub(crate) fn position_for_import(
|
|||||||
) -> Option<SyntaxNode> {
|
) -> Option<SyntaxNode> {
|
||||||
Some(
|
Some(
|
||||||
match import_candidate {
|
match import_candidate {
|
||||||
Some(ImportCandidate::Path(_)) => ctx.name_syntax.as_ref()?.syntax(),
|
|
||||||
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
|
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
|
||||||
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
|
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
|
||||||
None => return ctx.original_token.parent(),
|
Some(ImportCandidate::Path(_)) | None => return ctx.original_token.parent(),
|
||||||
}
|
}
|
||||||
.clone(),
|
.clone(),
|
||||||
)
|
)
|
||||||
|
@ -12,17 +12,20 @@ use syntax::{ast, TokenText};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completions::Completions,
|
completions::Completions,
|
||||||
context::{CompletionContext, LifetimeContext},
|
context::{CompletionContext, LifetimeContext, LifetimeKind},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Completes lifetimes.
|
/// Completes lifetimes.
|
||||||
pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
let lp = match &ctx.lifetime_ctx {
|
let (lp, lifetime) = match &ctx.lifetime_ctx {
|
||||||
Some(LifetimeContext::Lifetime) => None,
|
Some(LifetimeContext { kind: LifetimeKind::Lifetime, lifetime }) => (None, lifetime),
|
||||||
Some(LifetimeContext::LifetimeParam { is_decl: false, param }) => Some(param),
|
Some(LifetimeContext {
|
||||||
|
kind: LifetimeKind::LifetimeParam { is_decl: false, param },
|
||||||
|
lifetime,
|
||||||
|
}) => (Some(param), lifetime),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
let param_lifetime = match (ctx.lifetime(), lp.and_then(|lp| lp.lifetime())) {
|
let param_lifetime = match (lifetime, lp.and_then(|lp| lp.lifetime())) {
|
||||||
(Some(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,
|
||||||
@ -46,7 +49,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
|
|||||||
|
|
||||||
/// Completes labels.
|
/// Completes labels.
|
||||||
pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
if !matches!(ctx.lifetime_ctx, Some(LifetimeContext::LabelRef)) {
|
if !matches!(ctx.lifetime_ctx, Some(LifetimeContext { kind: LifetimeKind::LabelRef, .. })) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ctx.process_all_names_raw(&mut |name, res| {
|
ctx.process_all_names_raw(&mut |name, res| {
|
||||||
|
@ -3,21 +3,23 @@
|
|||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use hir::{Module, ModuleSource};
|
use hir::{Module, ModuleSource};
|
||||||
use ide_db::FxHashSet;
|
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{SourceDatabaseExt, VfsPath},
|
base_db::{SourceDatabaseExt, VfsPath},
|
||||||
RootDatabase, SymbolKind,
|
FxHashSet, RootDatabase, SymbolKind,
|
||||||
};
|
};
|
||||||
use syntax::{ast, AstNode, SyntaxKind};
|
use syntax::{ast, AstNode, SyntaxKind};
|
||||||
|
|
||||||
use crate::{context::NameContext, CompletionItem};
|
use crate::{
|
||||||
|
context::{CompletionContext, NameContext, NameKind},
|
||||||
|
CompletionItem, Completions,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{context::CompletionContext, Completions};
|
/// Complete mod declaration, i.e. `mod ;`
|
||||||
|
|
||||||
/// Complete mod declaration, i.e. `mod $0;`
|
|
||||||
pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
||||||
let mod_under_caret = match &ctx.name_ctx {
|
let mod_under_caret = match &ctx.name_ctx {
|
||||||
Some(NameContext::Module(mod_under_caret)) if mod_under_caret.item_list().is_none() => {
|
Some(NameContext { kind: NameKind::Module(mod_under_caret), .. })
|
||||||
|
if mod_under_caret.item_list().is_none() =>
|
||||||
|
{
|
||||||
mod_under_caret
|
mod_under_caret
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
@ -26,7 +28,7 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
|
|||||||
let _p = profile::span("completion::complete_mod");
|
let _p = profile::span("completion::complete_mod");
|
||||||
|
|
||||||
let mut current_module = ctx.module;
|
let mut current_module = ctx.module;
|
||||||
// For `mod $0`, `ctx.module` is its parent, but for `mod f$0`, it's `mod f` itself, but we're
|
// For `mod `, `ctx.module` is its parent, but for `mod f`, it's `mod f` itself, but we're
|
||||||
// interested in its parent.
|
// interested in its parent.
|
||||||
if ctx.original_token.kind() == SyntaxKind::IDENT {
|
if ctx.original_token.kind() == SyntaxKind::IDENT {
|
||||||
if let Some(module) = ctx.original_token.ancestors().nth(1).and_then(ast::Module::cast) {
|
if let Some(module) = ctx.original_token.ancestors().nth(1).and_then(ast::Module::cast) {
|
||||||
|
@ -5,16 +5,19 @@ use ide_db::FxHashSet;
|
|||||||
use syntax::{ast, AstNode};
|
use syntax::{ast, AstNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx},
|
context::{CompletionContext, NameRefContext, PathCompletionCtx, PathKind, PathQualifierCtx},
|
||||||
item::Builder,
|
item::Builder,
|
||||||
CompletionRelevance, Completions,
|
CompletionRelevance, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
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, name_ref) = match &ctx.nameref_ctx {
|
||||||
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }) => {
|
Some(NameRefContext {
|
||||||
(is_absolute_path, qualifier)
|
path_ctx:
|
||||||
}
|
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }),
|
||||||
|
nameref,
|
||||||
|
..
|
||||||
|
}) => (is_absolute_path, qualifier, nameref),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,7 +58,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
|
|||||||
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_ref(),
|
name_ref,
|
||||||
Some(name_ref) if name_ref.syntax().text() == name.to_smol_str().as_str()
|
Some(name_ref) if name_ref.syntax().text() == name.to_smol_str().as_str()
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
@ -117,16 +117,29 @@ pub(super) struct PatternContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(super) enum LifetimeContext {
|
pub(super) struct LifetimeContext {
|
||||||
|
pub(super) lifetime: Option<ast::Lifetime>,
|
||||||
|
pub(super) kind: LifetimeKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum LifetimeKind {
|
||||||
LifetimeParam { is_decl: bool, param: ast::LifetimeParam },
|
LifetimeParam { is_decl: bool, param: ast::LifetimeParam },
|
||||||
Lifetime,
|
Lifetime,
|
||||||
LabelRef,
|
LabelRef,
|
||||||
LabelDef,
|
LabelDef,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct NameContext {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(super) name: Option<ast::Name>,
|
||||||
|
pub(super) kind: NameKind,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(super) enum NameContext {
|
pub(super) enum NameKind {
|
||||||
Const,
|
Const,
|
||||||
ConstParam,
|
ConstParam,
|
||||||
Enum,
|
Enum,
|
||||||
@ -150,6 +163,8 @@ pub(super) enum NameContext {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(super) struct NameRefContext {
|
pub(super) struct NameRefContext {
|
||||||
|
/// NameRef syntax in the original file
|
||||||
|
pub(super) nameref: Option<ast::NameRef>,
|
||||||
pub(super) dot_access: Option<DotAccess>,
|
pub(super) dot_access: Option<DotAccess>,
|
||||||
pub(super) path_ctx: Option<PathCompletionCtx>,
|
pub(super) path_ctx: Option<PathCompletionCtx>,
|
||||||
}
|
}
|
||||||
@ -203,8 +218,6 @@ pub(crate) struct CompletionContext<'a> {
|
|||||||
pub(super) function_def: Option<ast::Fn>,
|
pub(super) function_def: Option<ast::Fn>,
|
||||||
/// The parent impl of the cursor position if it exists.
|
/// The parent impl of the cursor position if it exists.
|
||||||
pub(super) impl_def: Option<ast::Impl>,
|
pub(super) impl_def: Option<ast::Impl>,
|
||||||
/// The NameLike under the cursor in the original file if it exists.
|
|
||||||
pub(super) name_syntax: Option<ast::NameLike>,
|
|
||||||
/// Are we completing inside a let statement with a missing semicolon?
|
/// Are we completing inside a let statement with a missing semicolon?
|
||||||
pub(super) incomplete_let: bool,
|
pub(super) incomplete_let: bool,
|
||||||
|
|
||||||
@ -216,6 +229,7 @@ pub(crate) struct CompletionContext<'a> {
|
|||||||
pub(super) name_ctx: Option<NameContext>,
|
pub(super) name_ctx: Option<NameContext>,
|
||||||
pub(super) lifetime_ctx: Option<LifetimeContext>,
|
pub(super) lifetime_ctx: Option<LifetimeContext>,
|
||||||
pub(super) nameref_ctx: Option<NameRefContext>,
|
pub(super) nameref_ctx: Option<NameRefContext>,
|
||||||
|
|
||||||
pub(super) pattern_ctx: Option<PatternContext>,
|
pub(super) pattern_ctx: Option<PatternContext>,
|
||||||
|
|
||||||
pub(super) existing_derives: FxHashSet<hir::Macro>,
|
pub(super) existing_derives: FxHashSet<hir::Macro>,
|
||||||
@ -240,14 +254,6 @@ 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)
|
||||||
}
|
}
|
||||||
@ -276,7 +282,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expects_variant(&self) -> bool {
|
pub(crate) fn expects_variant(&self) -> bool {
|
||||||
matches!(self.name_ctx, Some(NameContext::Variant))
|
matches!(self.name_ctx, Some(NameContext { kind: NameKind::Variant, .. }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
|
pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
|
||||||
@ -301,7 +307,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
|
|
||||||
pub(crate) fn expect_field(&self) -> bool {
|
pub(crate) fn expect_field(&self) -> bool {
|
||||||
matches!(self.completion_location, Some(ImmediateLocation::TupleField))
|
matches!(self.completion_location, Some(ImmediateLocation::TupleField))
|
||||||
|| matches!(self.name_ctx, Some(NameContext::RecordField))
|
|| matches!(self.name_ctx, Some(NameContext { kind: NameKind::RecordField, .. }))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether the cursor is right after a trait or impl header.
|
/// Whether the cursor is right after a trait or impl header.
|
||||||
@ -338,7 +344,10 @@ impl<'a> CompletionContext<'a> {
|
|||||||
self.completion_location,
|
self.completion_location,
|
||||||
Some(ImmediateLocation::RecordPat(_) | ImmediateLocation::RecordExpr(_))
|
Some(ImmediateLocation::RecordPat(_) | ImmediateLocation::RecordExpr(_))
|
||||||
)
|
)
|
||||||
|| matches!(self.name_ctx, Some(NameContext::Module(_) | NameContext::Rename))
|
|| matches!(
|
||||||
|
self.name_ctx,
|
||||||
|
Some(NameContext { kind: NameKind::Module(_) | NameKind::Rename, .. })
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn path_context(&self) -> Option<&PathCompletionCtx> {
|
pub(crate) fn path_context(&self) -> Option<&PathCompletionCtx> {
|
||||||
@ -518,7 +527,6 @@ impl<'a> CompletionContext<'a> {
|
|||||||
expected_type: None,
|
expected_type: None,
|
||||||
function_def: None,
|
function_def: None,
|
||||||
impl_def: None,
|
impl_def: None,
|
||||||
name_syntax: None,
|
|
||||||
incomplete_let: false,
|
incomplete_let: false,
|
||||||
completion_location: None,
|
completion_location: None,
|
||||||
prev_sibling: None,
|
prev_sibling: None,
|
||||||
@ -862,11 +870,9 @@ impl<'a> CompletionContext<'a> {
|
|||||||
if let Some(ast::NameLike::NameRef(name_ref)) =
|
if let Some(ast::NameLike::NameRef(name_ref)) =
|
||||||
find_node_at_offset(&file_with_fake_ident, offset)
|
find_node_at_offset(&file_with_fake_ident, offset)
|
||||||
{
|
{
|
||||||
self.name_syntax =
|
if let Some(parent) = name_ref.syntax().parent() {
|
||||||
find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
|
let (mut nameref_ctx, _) =
|
||||||
if let Some((mut nameref_ctx, _)) =
|
Self::classify_name_ref(&self.sema, &original_file, name_ref, parent);
|
||||||
Self::classify_name_ref(&self.sema, &original_file, name_ref)
|
|
||||||
{
|
|
||||||
if let Some(path_ctx) = &mut nameref_ctx.path_ctx {
|
if let Some(path_ctx) = &mut nameref_ctx.path_ctx {
|
||||||
path_ctx.kind = PathKind::Derive;
|
path_ctx.kind = PathKind::Derive;
|
||||||
}
|
}
|
||||||
@ -883,8 +889,6 @@ impl<'a> CompletionContext<'a> {
|
|||||||
self.completion_location =
|
self.completion_location =
|
||||||
determine_location(&self.sema, original_file, offset, &name_like);
|
determine_location(&self.sema, original_file, offset, &name_like);
|
||||||
self.prev_sibling = determine_prev_sibling(&name_like);
|
self.prev_sibling = determine_prev_sibling(&name_like);
|
||||||
self.name_syntax =
|
|
||||||
find_node_at_offset(original_file, name_like.syntax().text_range().start());
|
|
||||||
self.impl_def = self
|
self.impl_def = self
|
||||||
.sema
|
.sema
|
||||||
.token_ancestors_with_macros(self.token.clone())
|
.token_ancestors_with_macros(self.token.clone())
|
||||||
@ -901,9 +905,9 @@ impl<'a> CompletionContext<'a> {
|
|||||||
self.lifetime_ctx = Self::classify_lifetime(&self.sema, original_file, lifetime);
|
self.lifetime_ctx = Self::classify_lifetime(&self.sema, original_file, lifetime);
|
||||||
}
|
}
|
||||||
ast::NameLike::NameRef(name_ref) => {
|
ast::NameLike::NameRef(name_ref) => {
|
||||||
if let Some((nameref_ctx, pat_ctx)) =
|
if let Some(parent) = name_ref.syntax().parent() {
|
||||||
Self::classify_name_ref(&self.sema, original_file, name_ref)
|
let (nameref_ctx, pat_ctx) =
|
||||||
{
|
Self::classify_name_ref(&self.sema, &original_file, name_ref, parent);
|
||||||
self.nameref_ctx = Some(nameref_ctx);
|
self.nameref_ctx = Some(nameref_ctx);
|
||||||
self.pattern_ctx = pat_ctx;
|
self.pattern_ctx = pat_ctx;
|
||||||
}
|
}
|
||||||
@ -921,7 +925,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
|
|
||||||
fn classify_lifetime(
|
fn classify_lifetime(
|
||||||
_sema: &Semantics<RootDatabase>,
|
_sema: &Semantics<RootDatabase>,
|
||||||
_original_file: &SyntaxNode,
|
original_file: &SyntaxNode,
|
||||||
lifetime: ast::Lifetime,
|
lifetime: ast::Lifetime,
|
||||||
) -> Option<LifetimeContext> {
|
) -> Option<LifetimeContext> {
|
||||||
let parent = lifetime.syntax().parent()?;
|
let parent = lifetime.syntax().parent()?;
|
||||||
@ -929,18 +933,21 @@ impl<'a> CompletionContext<'a> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(match_ast! {
|
let kind = match_ast! {
|
||||||
match parent {
|
match parent {
|
||||||
ast::LifetimeParam(param) => LifetimeContext::LifetimeParam {
|
ast::LifetimeParam(param) => LifetimeKind::LifetimeParam {
|
||||||
is_decl: param.lifetime().as_ref() == Some(&lifetime),
|
is_decl: param.lifetime().as_ref() == Some(&lifetime),
|
||||||
param
|
param
|
||||||
},
|
},
|
||||||
ast::BreakExpr(_) => LifetimeContext::LabelRef,
|
ast::BreakExpr(_) => LifetimeKind::LabelRef,
|
||||||
ast::ContinueExpr(_) => LifetimeContext::LabelRef,
|
ast::ContinueExpr(_) => LifetimeKind::LabelRef,
|
||||||
ast::Label(_) => LifetimeContext::LabelDef,
|
ast::Label(_) => LifetimeKind::LabelDef,
|
||||||
_ => LifetimeContext::Lifetime,
|
_ => LifetimeKind::Lifetime,
|
||||||
}
|
}
|
||||||
})
|
};
|
||||||
|
let lifetime = find_node_at_offset(&original_file, lifetime.syntax().text_range().start());
|
||||||
|
|
||||||
|
Some(LifetimeContext { lifetime, kind })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_name(
|
fn classify_name(
|
||||||
@ -950,12 +957,12 @@ impl<'a> CompletionContext<'a> {
|
|||||||
) -> Option<(NameContext, Option<PatternContext>)> {
|
) -> Option<(NameContext, Option<PatternContext>)> {
|
||||||
let parent = name.syntax().parent()?;
|
let parent = name.syntax().parent()?;
|
||||||
let mut pat_ctx = None;
|
let mut pat_ctx = None;
|
||||||
let name_ctx = match_ast! {
|
let kind = match_ast! {
|
||||||
match parent {
|
match parent {
|
||||||
ast::Const(_) => NameContext::Const,
|
ast::Const(_) => NameKind::Const,
|
||||||
ast::ConstParam(_) => NameContext::ConstParam,
|
ast::ConstParam(_) => NameKind::ConstParam,
|
||||||
ast::Enum(_) => NameContext::Enum,
|
ast::Enum(_) => NameKind::Enum,
|
||||||
ast::Fn(_) => NameContext::Function,
|
ast::Fn(_) => NameKind::Function,
|
||||||
ast::IdentPat(bind_pat) => {
|
ast::IdentPat(bind_pat) => {
|
||||||
let is_name_in_field_pat = bind_pat
|
let is_name_in_field_pat = bind_pat
|
||||||
.syntax()
|
.syntax()
|
||||||
@ -966,49 +973,38 @@ impl<'a> CompletionContext<'a> {
|
|||||||
pat_ctx = Some(pattern_context_for(original_file, bind_pat.into()));
|
pat_ctx = Some(pattern_context_for(original_file, bind_pat.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
NameContext::IdentPat
|
NameKind::IdentPat
|
||||||
},
|
},
|
||||||
ast::MacroDef(_) => NameContext::MacroDef,
|
ast::MacroDef(_) => NameKind::MacroDef,
|
||||||
ast::MacroRules(_) => NameContext::MacroRules,
|
ast::MacroRules(_) => NameKind::MacroRules,
|
||||||
ast::Module(module) => NameContext::Module(module),
|
ast::Module(module) => NameKind::Module(module),
|
||||||
ast::RecordField(_) => NameContext::RecordField,
|
ast::RecordField(_) => NameKind::RecordField,
|
||||||
ast::Rename(_) => NameContext::Rename,
|
ast::Rename(_) => NameKind::Rename,
|
||||||
ast::SelfParam(_) => NameContext::SelfParam,
|
ast::SelfParam(_) => NameKind::SelfParam,
|
||||||
ast::Static(_) => NameContext::Static,
|
ast::Static(_) => NameKind::Static,
|
||||||
ast::Struct(_) => NameContext::Struct,
|
ast::Struct(_) => NameKind::Struct,
|
||||||
ast::Trait(_) => NameContext::Trait,
|
ast::Trait(_) => NameKind::Trait,
|
||||||
ast::TypeAlias(_) => NameContext::TypeAlias,
|
ast::TypeAlias(_) => NameKind::TypeAlias,
|
||||||
ast::TypeParam(_) => NameContext::TypeParam,
|
ast::TypeParam(_) => NameKind::TypeParam,
|
||||||
ast::Union(_) => NameContext::Union,
|
ast::Union(_) => NameKind::Union,
|
||||||
ast::Variant(_) => NameContext::Variant,
|
ast::Variant(_) => NameKind::Variant,
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Some((name_ctx, pat_ctx))
|
let name = find_node_at_offset(&original_file, name.syntax().text_range().start());
|
||||||
|
Some((NameContext { name, kind }, pat_ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_name_ref(
|
fn classify_name_ref(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
original_file: &SyntaxNode,
|
original_file: &SyntaxNode,
|
||||||
name_ref: ast::NameRef,
|
name_ref: ast::NameRef,
|
||||||
) -> Option<(NameRefContext, Option<PatternContext>)> {
|
parent: SyntaxNode,
|
||||||
let parent = name_ref.syntax().parent()?;
|
) -> (NameRefContext, Option<PatternContext>) {
|
||||||
|
let nameref = find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
|
||||||
|
|
||||||
let mut nameref_ctx = NameRefContext { dot_access: None, path_ctx: None };
|
let mut nameref_ctx = NameRefContext { dot_access: None, path_ctx: None, nameref };
|
||||||
|
|
||||||
fn find_in_original_file<N: AstNode>(
|
|
||||||
x: Option<N>,
|
|
||||||
original_file: &SyntaxNode,
|
|
||||||
) -> Option<N> {
|
|
||||||
fn find_node_with_range<N: AstNode>(
|
|
||||||
syntax: &SyntaxNode,
|
|
||||||
range: TextRange,
|
|
||||||
) -> Option<N> {
|
|
||||||
let range = syntax.text_range().intersect(range)?;
|
|
||||||
syntax.covering_element(range).ancestors().find_map(N::cast)
|
|
||||||
}
|
|
||||||
x.map(|e| e.syntax().text_range()).and_then(|r| find_node_with_range(original_file, r))
|
|
||||||
}
|
|
||||||
let segment = match_ast! {
|
let segment = match_ast! {
|
||||||
match parent {
|
match parent {
|
||||||
ast::PathSegment(segment) => segment,
|
ast::PathSegment(segment) => segment,
|
||||||
@ -1022,7 +1018,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
nameref_ctx.dot_access = Some(DotAccess::Field { receiver, receiver_is_ambiguous_float_literal });
|
nameref_ctx.dot_access = Some(DotAccess::Field { receiver, receiver_is_ambiguous_float_literal });
|
||||||
return Some((nameref_ctx, None));
|
return (nameref_ctx, None);
|
||||||
},
|
},
|
||||||
ast::MethodCallExpr(method) => {
|
ast::MethodCallExpr(method) => {
|
||||||
nameref_ctx.dot_access = Some(
|
nameref_ctx.dot_access = Some(
|
||||||
@ -1031,9 +1027,9 @@ impl<'a> CompletionContext<'a> {
|
|||||||
has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some())
|
has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some())
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return Some((nameref_ctx, None));
|
return (nameref_ctx, None);
|
||||||
},
|
},
|
||||||
_ => return None,
|
_ => return (nameref_ctx, None),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1057,7 +1053,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
};
|
};
|
||||||
|
|
||||||
path_ctx.kind = path.syntax().ancestors().find_map(|it| {
|
let kind = path.syntax().ancestors().find_map(|it| {
|
||||||
// using Option<Option<PathKind>> as extra controlflow
|
// using Option<Option<PathKind>> as extra controlflow
|
||||||
let kind = match_ast! {
|
let kind = match_ast! {
|
||||||
match it {
|
match it {
|
||||||
@ -1138,7 +1134,11 @@ impl<'a> CompletionContext<'a> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
Some(kind)
|
Some(kind)
|
||||||
}).flatten()?;
|
}).flatten();
|
||||||
|
match kind {
|
||||||
|
Some(kind) => path_ctx.kind = kind,
|
||||||
|
None => return (nameref_ctx, pat_ctx),
|
||||||
|
}
|
||||||
path_ctx.has_type_args = segment.generic_arg_list().is_some();
|
path_ctx.has_type_args = segment.generic_arg_list().is_some();
|
||||||
|
|
||||||
if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
|
if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
|
||||||
@ -1180,7 +1180,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
nameref_ctx.path_ctx = Some(path_ctx);
|
nameref_ctx.path_ctx = Some(path_ctx);
|
||||||
Some((nameref_ctx, pat_ctx))
|
(nameref_ctx, pat_ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1235,6 +1235,14 @@ fn pattern_context_for(original_file: &SyntaxNode, pat: ast::Pat) -> PatternCont
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_in_original_file<N: AstNode>(x: Option<N>, original_file: &SyntaxNode) -> Option<N> {
|
||||||
|
fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
|
||||||
|
let range = syntax.text_range().intersect(range)?;
|
||||||
|
syntax.covering_element(range).ancestors().find_map(N::cast)
|
||||||
|
}
|
||||||
|
x.map(|e| e.syntax().text_range()).and_then(|r| find_node_with_range(original_file, r))
|
||||||
|
}
|
||||||
|
|
||||||
/// Attempts to find `node` inside `syntax` via `node`'s text range.
|
/// Attempts to find `node` inside `syntax` via `node`'s text range.
|
||||||
fn find_node_in_file<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
|
fn find_node_in_file<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
|
||||||
let syntax_range = syntax.text_range();
|
let syntax_range = syntax.text_range();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user