Lift out PathKind variant fields into structs

This commit is contained in:
Lukas Wirth 2022-06-20 13:29:13 +02:00
parent 5c69df93df
commit ce5859e387
9 changed files with 86 additions and 91 deletions

View file

@ -18,7 +18,7 @@ use syntax::{
use crate::{ use crate::{
completions::module_or_attr, completions::module_or_attr,
context::{CompletionContext, PathCompletionCtx, PathKind, Qualified}, context::{AttrCtx, CompletionContext, PathCompletionCtx, PathKind, Qualified},
item::CompletionItem, item::CompletionItem,
Completions, Completions,
}; };
@ -76,7 +76,7 @@ pub(crate) fn complete_attribute(
) { ) {
let (qualified, is_inner, annotated_item_kind) = match path_ctx { let (qualified, is_inner, annotated_item_kind) = match path_ctx {
&PathCompletionCtx { &PathCompletionCtx {
kind: PathKind::Attr { kind, annotated_item_kind }, kind: PathKind::Attr { attr_ctx: AttrCtx { kind, annotated_item_kind } },
ref qualified, ref qualified,
.. ..
} => (qualified, kind == AttrKind::Inner, annotated_item_kind), } => (qualified, kind == AttrKind::Inner, annotated_item_kind),

View file

@ -4,7 +4,8 @@ use ide_db::FxHashSet;
use crate::{ use crate::{
context::{ context::{
CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind, Qualified, CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, PathKind,
Qualified,
}, },
CompletionItem, CompletionItemKind, Completions, CompletionItem, CompletionItemKind, Completions,
}; };
@ -49,7 +50,7 @@ pub(crate) fn complete_undotted_self(
let self_param = match path_ctx { let self_param = match path_ctx {
PathCompletionCtx { PathCompletionCtx {
qualified: Qualified::No, qualified: Qualified::No,
kind: PathKind::Expr { self_param: Some(self_param), .. }, kind: PathKind::Expr { expr_ctx: ExprCtx { self_param: Some(self_param), .. } },
.. ..
} if path_ctx.is_trivial_path() && ctx.qualifier_ctx.none() => self_param, } if path_ctx.is_trivial_path() && ctx.qualifier_ctx.none() => self_param,
_ => return, _ => return,

View file

@ -4,7 +4,7 @@ use hir::ScopeDef;
use ide_db::FxHashSet; use ide_db::FxHashSet;
use crate::{ use crate::{
context::{PathCompletionCtx, PathKind, Qualified}, context::{ExprCtx, PathCompletionCtx, PathKind, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -19,47 +19,28 @@ pub(crate) fn complete_expr_path(
} }
let ( let (
qualified, qualified,
in_block_expr, &ExprCtx {
in_loop_body,
is_func_update,
after_if_expr,
wants_mut_token,
in_condition,
ty,
incomplete_let,
impl_,
) = match path_ctx {
&PathCompletionCtx {
kind:
PathKind::Expr {
in_block_expr,
in_loop_body,
after_if_expr,
in_condition,
incomplete_let,
ref ref_expr_parent,
ref is_func_update,
ref innermost_ret_ty,
ref impl_,
..
},
ref qualified,
..
} => (
qualified,
in_block_expr, in_block_expr,
in_loop_body, in_loop_body,
is_func_update.is_some(),
after_if_expr, after_if_expr,
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false),
in_condition, in_condition,
innermost_ret_ty,
incomplete_let, incomplete_let,
impl_, ref ref_expr_parent,
), ref is_func_update,
ref innermost_ret_ty,
ref impl_,
..
},
) = match path_ctx {
PathCompletionCtx { kind: PathKind::Expr { expr_ctx }, qualified, .. } => {
(qualified, expr_ctx)
}
_ => return, _ => return,
}; };
let wants_mut_token =
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false);
let scope_def_applicable = |def| { let scope_def_applicable = |def| {
use hir::{GenericParam::*, ModuleDef::*}; use hir::{GenericParam::*, ModuleDef::*};
match def { match def {
@ -230,7 +211,7 @@ pub(crate) fn complete_expr_path(
} }
}); });
if !is_func_update { if is_func_update.is_none() {
let mut add_keyword = let mut add_keyword =
|kw, snippet| acc.add_keyword_snippet_expr(ctx, kw, snippet, incomplete_let); |kw, snippet| acc.add_keyword_snippet_expr(ctx, kw, snippet, incomplete_let);
@ -270,7 +251,7 @@ pub(crate) fn complete_expr_path(
} }
} }
if let Some(ty) = ty { if let Some(ty) = innermost_ret_ty {
add_keyword( add_keyword(
"return", "return",
match (in_block_expr, ty.is_unit()) { match (in_block_expr, ty.is_unit()) {

View file

@ -2,7 +2,7 @@
use crate::{ use crate::{
completions::module_or_fn_macro, completions::module_or_fn_macro,
context::{ItemListKind, PathCompletionCtx, PathKind, Qualified}, context::{ExprCtx, ItemListKind, PathCompletionCtx, PathKind, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -21,9 +21,10 @@ pub(crate) fn complete_item_list(
} }
qualified qualified
} }
PathCompletionCtx { kind: PathKind::Expr { in_block_expr: true, .. }, .. } PathCompletionCtx {
if path_ctx.is_trivial_path() => kind: PathKind::Expr { expr_ctx: ExprCtx { in_block_expr: true, .. } },
{ ..
} if path_ctx.is_trivial_path() => {
add_keywords(acc, ctx, None); add_keywords(acc, ctx, None);
return; return;
} }

View file

@ -6,7 +6,7 @@ use syntax::{
}; };
use crate::{ use crate::{
context::{PathCompletionCtx, PathKind, PatternContext, Qualified}, context::{ExprCtx, PathCompletionCtx, PathKind, PatternContext, Qualified},
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance, CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
CompletionRelevancePostfixMatch, Completions, CompletionRelevancePostfixMatch, Completions,
}; };
@ -87,7 +87,7 @@ pub(crate) fn complete_record_expr_func_update(
path_ctx: &PathCompletionCtx, path_ctx: &PathCompletionCtx,
) { ) {
if let PathCompletionCtx { if let PathCompletionCtx {
kind: PathKind::Expr { is_func_update: Some(record_expr), .. }, kind: PathKind::Expr { expr_ctx: ExprCtx { is_func_update: Some(record_expr), .. } },
qualified: Qualified::No, qualified: Qualified::No,
.. ..
} = path_ctx } = path_ctx

View file

@ -4,7 +4,7 @@ use hir::Documentation;
use ide_db::{imports::insert_use::ImportScope, SnippetCap}; use ide_db::{imports::insert_use::ImportScope, SnippetCap};
use crate::{ use crate::{
context::{ItemListKind, PathCompletionCtx, PathKind, Qualified}, context::{ExprCtx, ItemListKind, PathCompletionCtx, PathKind, Qualified},
item::Builder, item::Builder,
CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope, CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope,
}; };
@ -23,7 +23,7 @@ pub(crate) fn complete_expr_snippet(
let &can_be_stmt = match path_ctx { let &can_be_stmt = match path_ctx {
PathCompletionCtx { PathCompletionCtx {
qualified: Qualified::No, qualified: Qualified::No,
kind: PathKind::Expr { in_block_expr, .. }, kind: PathKind::Expr { expr_ctx: ExprCtx { in_block_expr, .. } },
.. ..
} => in_block_expr, } => in_block_expr,
_ => return, _ => return,
@ -52,7 +52,9 @@ pub(crate) fn complete_item_snippet(
let path_kind = match path_ctx { let path_kind = match path_ctx {
PathCompletionCtx { PathCompletionCtx {
qualified: Qualified::No, qualified: Qualified::No,
kind: kind @ (PathKind::Item { .. } | PathKind::Expr { in_block_expr: true, .. }), kind:
kind @ (PathKind::Item { .. }
| PathKind::Expr { expr_ctx: ExprCtx { in_block_expr: true, .. }, .. }),
.. ..
} => kind, } => kind,
_ => return, _ => return,

View file

@ -88,24 +88,13 @@ impl PathCompletionCtx {
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub(super) enum PathKind { pub(super) enum PathKind {
Expr { Expr {
in_block_expr: bool, expr_ctx: ExprCtx,
in_loop_body: bool,
after_if_expr: bool,
/// Whether this expression is the direct condition of an if or while expression
in_condition: bool,
incomplete_let: bool,
ref_expr_parent: Option<ast::RefExpr>,
is_func_update: Option<ast::RecordExpr>,
self_param: Option<hir::SelfParam>,
innermost_ret_ty: Option<hir::Type>,
impl_: Option<ast::Impl>,
}, },
Type { Type {
location: TypeLocation, location: TypeLocation,
}, },
Attr { Attr {
kind: AttrKind, attr_ctx: AttrCtx,
annotated_item_kind: Option<SyntaxKind>,
}, },
Derive { Derive {
existing_derives: FxHashSet<hir::Macro>, existing_derives: FxHashSet<hir::Macro>,
@ -122,6 +111,26 @@ pub(super) enum PathKind {
}, },
Use, Use,
} }
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct AttrCtx {
pub(crate) kind: AttrKind,
pub(crate) annotated_item_kind: Option<SyntaxKind>,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct ExprCtx {
pub(crate) in_block_expr: bool,
pub(crate) in_loop_body: bool,
pub(crate) after_if_expr: bool,
/// Whether this expression is the direct condition of an if or while expression
pub(crate) in_condition: bool,
pub(crate) incomplete_let: bool,
pub(crate) ref_expr_parent: Option<ast::RefExpr>,
pub(crate) is_func_update: Option<ast::RecordExpr>,
pub(crate) self_param: Option<hir::SelfParam>,
pub(crate) innermost_ret_ty: Option<hir::Type>,
pub(crate) impl_: Option<ast::Impl>,
}
/// Original file ast nodes /// Original file ast nodes
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]

View file

@ -11,10 +11,10 @@ use syntax::{
}; };
use crate::context::{ use crate::context::{
CompletionContext, DotAccess, DotAccessKind, IdentContext, ItemListKind, LifetimeContext, AttrCtx, CompletionContext, DotAccess, DotAccessKind, ExprCtx, IdentContext, ItemListKind,
LifetimeKind, NameContext, NameKind, NameRefContext, NameRefKind, ParamKind, PathCompletionCtx, LifetimeContext, LifetimeKind, NameContext, NameKind, NameRefContext, NameRefKind, ParamKind,
PathKind, PatternContext, PatternRefutability, Qualified, QualifierCtx, TypeAscriptionTarget, PathCompletionCtx, PathKind, PatternContext, PatternRefutability, Qualified, QualifierCtx,
TypeLocation, COMPLETION_MARKER, TypeAscriptionTarget, TypeLocation, COMPLETION_MARKER,
}; };
impl<'a> CompletionContext<'a> { impl<'a> CompletionContext<'a> {
@ -765,16 +765,18 @@ impl<'a> CompletionContext<'a> {
let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax()); let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax());
PathKind::Expr { PathKind::Expr {
in_block_expr, expr_ctx: ExprCtx {
in_loop_body, in_block_expr,
after_if_expr, in_loop_body,
in_condition, after_if_expr,
ref_expr_parent, in_condition,
is_func_update, ref_expr_parent,
innermost_ret_ty, is_func_update,
self_param, innermost_ret_ty,
incomplete_let, self_param,
impl_, incomplete_let,
impl_,
},
} }
}; };
let make_path_kind_type = |ty: ast::Type| { let make_path_kind_type = |ty: ast::Type| {
@ -858,8 +860,10 @@ impl<'a> CompletionContext<'a> {
Some(attached.kind()) Some(attached.kind())
}; };
PathKind::Attr { PathKind::Attr {
kind, attr_ctx: AttrCtx {
annotated_item_kind, kind,
annotated_item_kind,
}
} }
}, },
ast::Visibility(it) => PathKind::Vis { has_in_token: it.in_token().is_some() }, ast::Visibility(it) => PathKind::Vis { has_in_token: it.in_token().is_some() },
@ -914,7 +918,7 @@ impl<'a> CompletionContext<'a> {
if path_ctx.is_trivial_path() { if path_ctx.is_trivial_path() {
// fetch the full expression that may have qualifiers attached to it // fetch the full expression that may have qualifiers attached to it
let top_node = match path_ctx.kind { let top_node = match path_ctx.kind {
PathKind::Expr { in_block_expr: true, .. } => { PathKind::Expr { expr_ctx: ExprCtx { in_block_expr: true, .. } } => {
parent.ancestors().find(|it| ast::PathExpr::can_cast(it.kind())).and_then(|p| { parent.ancestors().find(|it| ast::PathExpr::can_cast(it.kind())).and_then(|p| {
let parent = p.parent()?; let parent = p.parent()?;
if ast::StmtList::can_cast(parent.kind()) { if ast::StmtList::can_cast(parent.kind()) {

View file

@ -165,6 +165,13 @@ pub fn completions(
{ {
let acc = &mut completions; let acc = &mut completions;
let mut complete_patterns = |pattern_ctx| {
completions::flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
completions::fn_param::complete_fn_param(acc, ctx, pattern_ctx);
completions::pattern::complete_pattern(acc, ctx, pattern_ctx);
completions::record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
};
match &ctx.ident_ctx { match &ctx.ident_ctx {
IdentContext::Name(NameContext { name, kind }) => match kind { IdentContext::Name(NameContext { name, kind }) => match kind {
NameKind::Const => { NameKind::Const => {
@ -173,12 +180,7 @@ pub fn completions(
NameKind::Function => { NameKind::Function => {
completions::item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name); completions::item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
} }
NameKind::IdentPat(pattern_ctx) => { NameKind::IdentPat(pattern_ctx) => complete_patterns(pattern_ctx),
completions::flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
completions::fn_param::complete_fn_param(acc, ctx, pattern_ctx);
completions::pattern::complete_pattern(acc, ctx, pattern_ctx);
completions::record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
}
NameKind::Module(mod_under_caret) => { NameKind::Module(mod_under_caret) => {
completions::mod_::complete_mod(acc, ctx, mod_under_caret); completions::mod_::complete_mod(acc, ctx, mod_under_caret);
} }
@ -239,12 +241,7 @@ pub fn completions(
record_expr, record_expr,
); );
} }
NameRefKind::Pattern(pattern_ctx) => { NameRefKind::Pattern(pattern_ctx) => complete_patterns(pattern_ctx),
completions::flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
completions::fn_param::complete_fn_param(acc, ctx, pattern_ctx);
completions::pattern::complete_pattern(acc, ctx, pattern_ctx);
completions::record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
}
}, },
IdentContext::Lifetime(lifetime_ctx) => { IdentContext::Lifetime(lifetime_ctx) => {
completions::lifetime::complete_label(acc, ctx, lifetime_ctx); completions::lifetime::complete_label(acc, ctx, lifetime_ctx);