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

View file

@ -4,7 +4,8 @@ use ide_db::FxHashSet;
use crate::{
context::{
CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind, Qualified,
CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, PathKind,
Qualified,
},
CompletionItem, CompletionItemKind, Completions,
};
@ -49,7 +50,7 @@ pub(crate) fn complete_undotted_self(
let self_param = match path_ctx {
PathCompletionCtx {
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,
_ => return,

View file

@ -4,7 +4,7 @@ use hir::ScopeDef;
use ide_db::FxHashSet;
use crate::{
context::{PathCompletionCtx, PathKind, Qualified},
context::{ExprCtx, PathCompletionCtx, PathKind, Qualified},
CompletionContext, Completions,
};
@ -19,47 +19,28 @@ pub(crate) fn complete_expr_path(
}
let (
qualified,
in_block_expr,
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,
&ExprCtx {
in_block_expr,
in_loop_body,
is_func_update.is_some(),
after_if_expr,
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false),
in_condition,
innermost_ret_ty,
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,
};
let wants_mut_token =
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false);
let scope_def_applicable = |def| {
use hir::{GenericParam::*, ModuleDef::*};
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 =
|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(
"return",
match (in_block_expr, ty.is_unit()) {

View file

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

View file

@ -6,7 +6,7 @@ use syntax::{
};
use crate::{
context::{PathCompletionCtx, PathKind, PatternContext, Qualified},
context::{ExprCtx, PathCompletionCtx, PathKind, PatternContext, Qualified},
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
CompletionRelevancePostfixMatch, Completions,
};
@ -87,7 +87,7 @@ pub(crate) fn complete_record_expr_func_update(
path_ctx: &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,
..
} = path_ctx

View file

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

View file

@ -88,24 +88,13 @@ impl PathCompletionCtx {
#[derive(Debug, PartialEq, Eq)]
pub(super) enum PathKind {
Expr {
in_block_expr: bool,
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>,
expr_ctx: ExprCtx,
},
Type {
location: TypeLocation,
},
Attr {
kind: AttrKind,
annotated_item_kind: Option<SyntaxKind>,
attr_ctx: AttrCtx,
},
Derive {
existing_derives: FxHashSet<hir::Macro>,
@ -122,6 +111,26 @@ pub(super) enum PathKind {
},
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
#[derive(Clone, Debug, PartialEq, Eq)]

View file

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

View file

@ -165,6 +165,13 @@ pub fn 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 {
IdentContext::Name(NameContext { name, kind }) => match kind {
NameKind::Const => {
@ -173,12 +180,7 @@ pub fn completions(
NameKind::Function => {
completions::item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
}
NameKind::IdentPat(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::IdentPat(pattern_ctx) => complete_patterns(pattern_ctx),
NameKind::Module(mod_under_caret) => {
completions::mod_::complete_mod(acc, ctx, mod_under_caret);
}
@ -239,12 +241,7 @@ pub fn completions(
record_expr,
);
}
NameRefKind::Pattern(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);
}
NameRefKind::Pattern(pattern_ctx) => complete_patterns(pattern_ctx),
},
IdentContext::Lifetime(lifetime_ctx) => {
completions::lifetime::complete_label(acc, ctx, lifetime_ctx);