Skip to content

Commit

Permalink
Auto merge of rust-lang#12173 - Veykril:completion-rev, r=Veykril
Browse files Browse the repository at this point in the history
internal: completion PathKind is not optional
  • Loading branch information
bors committed May 6, 2022
2 parents dd3f5e0 + 57a9915 commit 616796a
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 33 deletions.
2 changes: 1 addition & 1 deletion crates/ide-completion/src/completions/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn complete_known_attribute_input(
pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) {
let (is_absolute_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Attr { kind, annotated_item_kind }),
kind: PathKind::Attr { kind, annotated_item_kind },
is_absolute_path,
ref qualifier,
..
Expand Down
5 changes: 1 addition & 4 deletions crates/ide-completion/src/completions/attribute/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use crate::{
pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let (qualifier, is_absolute_path) = match ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Derive),
ref qualifier,
is_absolute_path,
..
kind: PathKind::Derive, ref qualifier, is_absolute_path, ..
}) => (qualifier, is_absolute_path),
_ => return,
};
Expand Down
17 changes: 14 additions & 3 deletions crates/ide-completion/src/completions/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
use ide_db::FxHashSet;

use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
use crate::{
context::{CompletionContext, PathCompletionCtx, PathKind},
patterns::ImmediateLocation,
Completions,
};

/// Complete dot accesses, i.e. fields or methods.
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
Expand Down Expand Up @@ -34,9 +38,16 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.config.enable_self_on_the_fly {
return;
}
if ctx.is_non_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
return;
match ctx.path_context {
Some(PathCompletionCtx {
is_absolute_path: false,
qualifier: None,
kind: PathKind::Expr,
..
}) if !ctx.is_path_disallowed() => {}
_ => return,
}

if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
if let Some(self_) = func.self_param(ctx.db) {
let ty = self_.ty(ctx.db);
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
}

let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Expr), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Expr, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

Expand Down
6 changes: 3 additions & 3 deletions crates/ide-completion/src/completions/item_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
}

let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Item), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Item, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

Expand Down
6 changes: 3 additions & 3 deletions crates/ide-completion/src/completions/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
}

let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Type), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Type, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

Expand Down
6 changes: 3 additions & 3 deletions crates/ide-completion/src/completions/use_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::{

pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Use), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/completions/vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
let (&is_absolute_path, qualifier, &has_in_token) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Vis { has_in_token }),
kind: PathKind::Vis { has_in_token },
is_absolute_path,
qualifier,
..
Expand Down
15 changes: 7 additions & 8 deletions crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ pub(crate) struct PathCompletionCtx {
// FIXME: use this
/// The parent of the path we are completing.
pub(super) parent: Option<ast::Path>,
// FIXME: This should be PathKind, the none case should never occur
pub(super) kind: Option<PathKind>,
pub(super) kind: PathKind,
/// Whether the path segment has type args or not.
pub(super) has_type_args: bool,
/// `true` if we are a statement or a last expr in the block.
Expand Down Expand Up @@ -315,11 +314,11 @@ impl<'a> CompletionContext<'a> {
}

pub(crate) fn expects_expression(&self) -> bool {
matches!(self.path_context, Some(PathCompletionCtx { kind: Some(PathKind::Expr), .. }))
matches!(self.path_context, Some(PathCompletionCtx { kind: PathKind::Expr, .. }))
}

pub(crate) fn expects_type(&self) -> bool {
matches!(self.path_context, Some(PathCompletionCtx { kind: Some(PathKind::Type), .. }))
matches!(self.path_context, Some(PathCompletionCtx { kind: PathKind::Type, .. }))
}

pub(crate) fn path_is_call(&self) -> bool {
Expand All @@ -341,7 +340,7 @@ impl<'a> CompletionContext<'a> {
}

pub(crate) fn path_kind(&self) -> Option<PathKind> {
self.path_context.as_ref().and_then(|it| it.kind)
self.path_context.as_ref().map(|it| it.kind)
}

pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
Expand Down Expand Up @@ -837,7 +836,7 @@ impl<'a> CompletionContext<'a> {
Self::classify_name_ref(&self.sema, &original_file, name_ref)
{
self.path_context =
Some(PathCompletionCtx { kind: Some(PathKind::Derive), ..path_ctx });
Some(PathCompletionCtx { kind: PathKind::Derive, ..path_ctx });
}
}
return;
Expand Down Expand Up @@ -969,7 +968,7 @@ impl<'a> CompletionContext<'a> {
is_absolute_path: false,
qualifier: None,
parent: path.parent_path(),
kind: None,
kind: PathKind::Item,
has_type_args: false,
can_be_stmt: false,
in_loop_body: false,
Expand Down Expand Up @@ -1041,7 +1040,7 @@ impl<'a> CompletionContext<'a> {
}
};
Some(kind)
}).flatten();
}).flatten()?;
path_ctx.has_type_args = segment.generic_arg_list().is_some();

if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn render_resolution_simple_(
// Add `<>` for generic types
let type_path_no_ty_args = matches!(
ctx.completion.path_context,
Some(PathCompletionCtx { kind: Some(PathKind::Type), has_type_args: false, .. })
Some(PathCompletionCtx { kind: PathKind::Type, has_type_args: false, .. })
) && ctx.completion.config.add_call_parenthesis;
if type_path_no_ty_args {
if let Some(cap) = ctx.snippet_cap() {
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
}

match ctx.path_context {
Some(PathCompletionCtx { kind: Some(PathKind::Expr), has_call_parens: true, .. }) => {
Some(PathCompletionCtx { kind: PathKind::Expr, has_call_parens: true, .. }) => {
return false
}
Some(PathCompletionCtx { kind: Some(PathKind::Use | PathKind::Type), .. }) => {
Some(PathCompletionCtx { kind: PathKind::Use | PathKind::Type, .. }) => {
cov_mark::hit!(no_parens_in_use_item);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/render/macro_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn render(

let needs_bang = match completion.path_context {
Some(PathCompletionCtx { kind, has_macro_bang, .. }) => {
is_fn_like && kind != Some(PathKind::Use) && !has_macro_bang
is_fn_like && kind != PathKind::Use && !has_macro_bang
}
_ => is_fn_like,
};
Expand Down

0 comments on commit 616796a

Please sign in to comment.