From 4eabcb2c01209065a231e616ba646ba63f68f00d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 7 Jun 2021 12:29:46 +0200 Subject: Move more things into PathCompletionContext --- crates/ide_completion/src/completions/dot.rs | 2 +- crates/ide_completion/src/completions/flyimport.rs | 6 +- crates/ide_completion/src/completions/keyword.rs | 11 ++-- .../src/completions/qualified_path.rs | 4 +- crates/ide_completion/src/completions/snippet.rs | 4 +- .../src/completions/unqualified_path.rs | 2 +- crates/ide_completion/src/context.rs | 71 +++++++++++++--------- crates/ide_completion/src/render.rs | 8 ++- 8 files changed, 62 insertions(+), 46 deletions(-) (limited to 'crates/ide_completion') diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs index 22844c2ae..8ad57a069 100644 --- a/crates/ide_completion/src/completions/dot.rs +++ b/crates/ide_completion/src/completions/dot.rs @@ -33,7 +33,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.config.enable_self_on_the_fly { return; } - if !ctx.is_trivial_path || ctx.is_path_disallowed() { + if !ctx.is_trivial_path() || ctx.is_path_disallowed() { return; } ctx.scope.process_all_names(&mut |name, def| { diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index d72bf13d3..7bf47bf75 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -161,13 +161,13 @@ pub(crate) fn position_for_import<'a>( ) -> Option<&'a SyntaxNode> { Some(match import_candidate { Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(), - Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(), + Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(), Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(), None => ctx .name_ref_syntax .as_ref() .map(|name_ref| name_ref.syntax()) - .or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax())) + .or_else(|| ctx.path_qual().map(|path| path.syntax())) .or_else(|| ctx.dot_receiver().map(|expr| expr.syntax()))?, }) } @@ -190,7 +190,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option "return $0;", (true, false) => "return;", (false, true) => "return $0", diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index de58ce1cd..c072de7b5 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -10,8 +10,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon if ctx.is_path_disallowed() || ctx.expects_item() { return; } - let path = match &ctx.path_qual { - Some(path) => path.clone(), + let path = match ctx.path_qual() { + Some(path) => path, None => return, }; diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs index 6e6a6eb92..59a338e7b 100644 --- a/crates/ide_completion/src/completions/snippet.rs +++ b/crates/ide_completion/src/completions/snippet.rs @@ -14,7 +14,7 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) } pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { - if !(ctx.is_trivial_path && ctx.function_def.is_some()) { + if !(ctx.is_trivial_path() && ctx.function_def.is_some()) { return; } let cap = match ctx.config.snippet_cap { @@ -22,7 +22,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte None => return, }; - if ctx.can_be_stmt { + if ctx.can_be_stmt() { snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc); snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc); } diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index bd955aa85..f321ed52b 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -5,7 +5,7 @@ use hir::ScopeDef; use crate::{CompletionContext, Completions}; pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_trivial_path { + if !ctx.is_trivial_path() { return; } if ctx.is_path_disallowed() || ctx.expects_item() { diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 6bd67c123..20e033d31 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -33,6 +33,16 @@ pub(crate) enum PatternRefutability { pub(crate) struct PathCompletionContext { /// If this is a call with () already there call_kind: Option, + /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. + pub(super) is_trivial_path: bool, + /// If not a trivial path, the prefix (qualifier). + pub(super) path_qual: Option, + pub(super) is_path_type: bool, + pub(super) has_type_args: bool, + /// `true` if we are a statement or a last expr in the block. + pub(super) can_be_stmt: bool, + /// `true` if we expect an expression at the cursor position. + pub(super) is_expr: bool, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -83,16 +93,6 @@ pub(crate) struct CompletionContext<'a> { pub(super) path_context: Option, /// FIXME: `ActiveParameter` is string-based, which is very very wrong pub(super) active_parameter: Option, - /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. - pub(super) is_trivial_path: bool, - /// If not a trivial path, the prefix (qualifier). - pub(super) path_qual: Option, - /// `true` if we are a statement or a last expr in the block. - pub(super) can_be_stmt: bool, - /// `true` if we expect an expression at the cursor position. - pub(super) is_expr: bool, - pub(super) is_path_type: bool, - pub(super) has_type_args: bool, pub(super) locals: Vec<(String, Local)>, pub(super) previous_token: Option, @@ -156,13 +156,7 @@ impl<'a> CompletionContext<'a> { is_label_ref: false, is_param: false, is_pat_or_const: None, - is_trivial_path: false, - path_qual: None, - can_be_stmt: false, - is_expr: false, path_context: None, - is_path_type: false, - has_type_args: false, previous_token: None, in_loop_body: false, completion_location: None, @@ -280,11 +274,6 @@ impl<'a> CompletionContext<'a> { matches!(self.completion_location, Some(ImmediateLocation::ItemList)) } - // fn expects_value(&self) -> bool { - pub(crate) fn expects_expression(&self) -> bool { - self.is_expr - } - pub(crate) fn has_block_expr_parent(&self) -> bool { matches!(self.completion_location, Some(ImmediateLocation::BlockExpr)) } @@ -321,10 +310,26 @@ impl<'a> CompletionContext<'a> { ) || self.attribute_under_caret.is_some() } + pub(crate) fn expects_expression(&self) -> bool { + self.path_context.as_ref().map_or(false, |it| it.is_expr) + } + pub(crate) fn path_call_kind(&self) -> Option { self.path_context.as_ref().and_then(|it| it.call_kind) } + pub(crate) fn is_trivial_path(&self) -> bool { + self.path_context.as_ref().map_or(false, |it| it.is_trivial_path) + } + + pub(crate) fn path_qual(&self) -> Option<&ast::Path> { + self.path_context.as_ref().and_then(|it| it.path_qual.as_ref()) + } + + pub(crate) fn can_be_stmt(&self) -> bool { + self.path_context.as_ref().map_or(false, |it| it.can_be_stmt) + } + fn fill_impl_def(&mut self) { self.impl_def = self .sema @@ -577,7 +582,15 @@ impl<'a> CompletionContext<'a> { }; if let Some(segment) = ast::PathSegment::cast(parent) { - let mut path_ctx = PathCompletionContext { call_kind: None }; + let path_ctx = self.path_context.get_or_insert(PathCompletionContext { + call_kind: None, + is_trivial_path: false, + path_qual: None, + has_type_args: false, + is_path_type: false, + can_be_stmt: false, + is_expr: false, + }); let path = segment.parent_path(); if let Some(p) = path.syntax().parent() { @@ -590,13 +603,11 @@ impl<'a> CompletionContext<'a> { } }; } - self.path_context = Some(path_ctx); - dbg!(&self.path_context); - self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); - self.has_type_args = segment.generic_arg_list().is_some(); + path_ctx.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); + path_ctx.has_type_args = segment.generic_arg_list().is_some(); if let Some(path) = path_or_use_tree_qualifier(&path) { - self.path_qual = path + path_ctx.path_qual = path .segment() .and_then(|it| { find_node_with_range::( @@ -614,11 +625,11 @@ impl<'a> CompletionContext<'a> { } } - self.is_trivial_path = true; + path_ctx.is_trivial_path = true; // Find either enclosing expr statement (thing with `;`) or a // block. If block, check that we are the last expr. - self.can_be_stmt = name_ref + path_ctx.can_be_stmt = name_ref .syntax() .ancestors() .find_map(|node| { @@ -634,7 +645,7 @@ impl<'a> CompletionContext<'a> { None }) .unwrap_or(false); - self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); + path_ctx.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); } } } diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index a49a60711..750694432 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -275,8 +275,12 @@ impl<'a> Render<'a> { }; // Add `<>` for generic types - if self.ctx.completion.is_path_type - && !self.ctx.completion.has_type_args + if self + .ctx + .completion + .path_context + .as_ref() + .map_or(false, |it| it.is_path_type && !it.has_type_args) && self.ctx.completion.config.add_call_parenthesis { if let Some(cap) = self.ctx.snippet_cap() { -- cgit v1.2.3