diff options
Diffstat (limited to 'crates/ide_completion/src/context.rs')
-rw-r--r-- | crates/ide_completion/src/context.rs | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index cb4f08e53..6bd67c123 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -29,6 +29,18 @@ pub(crate) enum PatternRefutability { | |||
29 | Irrefutable, | 29 | Irrefutable, |
30 | } | 30 | } |
31 | 31 | ||
32 | #[derive(Debug)] | ||
33 | pub(crate) struct PathCompletionContext { | ||
34 | /// If this is a call with () already there | ||
35 | call_kind: Option<CallKind>, | ||
36 | } | ||
37 | |||
38 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
39 | pub(crate) enum CallKind { | ||
40 | Pat, | ||
41 | Mac, | ||
42 | Expr, | ||
43 | } | ||
32 | /// `CompletionContext` is created early during completion to figure out, where | 44 | /// `CompletionContext` is created early during completion to figure out, where |
33 | /// exactly is the cursor, syntax-wise. | 45 | /// exactly is the cursor, syntax-wise. |
34 | #[derive(Debug)] | 46 | #[derive(Debug)] |
@@ -68,6 +80,7 @@ pub(crate) struct CompletionContext<'a> { | |||
68 | pub(super) prev_sibling: Option<ImmediatePrevSibling>, | 80 | pub(super) prev_sibling: Option<ImmediatePrevSibling>, |
69 | pub(super) attribute_under_caret: Option<ast::Attr>, | 81 | pub(super) attribute_under_caret: Option<ast::Attr>, |
70 | 82 | ||
83 | pub(super) path_context: Option<PathCompletionContext>, | ||
71 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong | 84 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong |
72 | pub(super) active_parameter: Option<ActiveParameter>, | 85 | pub(super) active_parameter: Option<ActiveParameter>, |
73 | /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. | 86 | /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. |
@@ -78,12 +91,6 @@ pub(crate) struct CompletionContext<'a> { | |||
78 | pub(super) can_be_stmt: bool, | 91 | pub(super) can_be_stmt: bool, |
79 | /// `true` if we expect an expression at the cursor position. | 92 | /// `true` if we expect an expression at the cursor position. |
80 | pub(super) is_expr: bool, | 93 | pub(super) is_expr: bool, |
81 | /// If this is a call (method or function) in particular, i.e. the () are already there. | ||
82 | pub(super) is_call: bool, | ||
83 | /// Like `is_call`, but for tuple patterns. | ||
84 | pub(super) is_pattern_call: bool, | ||
85 | /// If this is a macro call, i.e. the () are already there. | ||
86 | pub(super) is_macro_call: bool, | ||
87 | pub(super) is_path_type: bool, | 94 | pub(super) is_path_type: bool, |
88 | pub(super) has_type_args: bool, | 95 | pub(super) has_type_args: bool, |
89 | pub(super) locals: Vec<(String, Local)>, | 96 | pub(super) locals: Vec<(String, Local)>, |
@@ -153,9 +160,7 @@ impl<'a> CompletionContext<'a> { | |||
153 | path_qual: None, | 160 | path_qual: None, |
154 | can_be_stmt: false, | 161 | can_be_stmt: false, |
155 | is_expr: false, | 162 | is_expr: false, |
156 | is_call: false, | 163 | path_context: None, |
157 | is_pattern_call: false, | ||
158 | is_macro_call: false, | ||
159 | is_path_type: false, | 164 | is_path_type: false, |
160 | has_type_args: false, | 165 | has_type_args: false, |
161 | previous_token: None, | 166 | previous_token: None, |
@@ -250,14 +255,14 @@ impl<'a> CompletionContext<'a> { | |||
250 | pub(crate) fn has_dot_receiver(&self) -> bool { | 255 | pub(crate) fn has_dot_receiver(&self) -> bool { |
251 | matches!( | 256 | matches!( |
252 | &self.completion_location, | 257 | &self.completion_location, |
253 | Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver }) | 258 | Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver,.. }) |
254 | if receiver.is_some() | 259 | if receiver.is_some() |
255 | ) | 260 | ) |
256 | } | 261 | } |
257 | 262 | ||
258 | pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> { | 263 | pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> { |
259 | match &self.completion_location { | 264 | match &self.completion_location { |
260 | Some(ImmediateLocation::MethodCall { receiver }) | 265 | Some(ImmediateLocation::MethodCall { receiver, .. }) |
261 | | Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(), | 266 | | Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(), |
262 | _ => None, | 267 | _ => None, |
263 | } | 268 | } |
@@ -316,6 +321,10 @@ impl<'a> CompletionContext<'a> { | |||
316 | ) || self.attribute_under_caret.is_some() | 321 | ) || self.attribute_under_caret.is_some() |
317 | } | 322 | } |
318 | 323 | ||
324 | pub(crate) fn path_call_kind(&self) -> Option<CallKind> { | ||
325 | self.path_context.as_ref().and_then(|it| it.call_kind) | ||
326 | } | ||
327 | |||
319 | fn fill_impl_def(&mut self) { | 328 | fn fill_impl_def(&mut self) { |
320 | self.impl_def = self | 329 | self.impl_def = self |
321 | .sema | 330 | .sema |
@@ -568,17 +577,21 @@ impl<'a> CompletionContext<'a> { | |||
568 | }; | 577 | }; |
569 | 578 | ||
570 | if let Some(segment) = ast::PathSegment::cast(parent) { | 579 | if let Some(segment) = ast::PathSegment::cast(parent) { |
580 | let mut path_ctx = PathCompletionContext { call_kind: None }; | ||
571 | let path = segment.parent_path(); | 581 | let path = segment.parent_path(); |
572 | self.is_call = path | ||
573 | .syntax() | ||
574 | .parent() | ||
575 | .and_then(ast::PathExpr::cast) | ||
576 | .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) | ||
577 | .is_some(); | ||
578 | self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some(); | ||
579 | self.is_pattern_call = | ||
580 | path.syntax().parent().and_then(ast::TupleStructPat::cast).is_some(); | ||
581 | 582 | ||
583 | if let Some(p) = path.syntax().parent() { | ||
584 | path_ctx.call_kind = match_ast! { | ||
585 | match p { | ||
586 | ast::PathExpr(it) => it.syntax().parent().and_then(ast::CallExpr::cast).map(|_| CallKind::Expr), | ||
587 | ast::MacroCall(_it) => Some(CallKind::Mac), | ||
588 | ast::TupleStructPat(_it) => Some(CallKind::Pat), | ||
589 | _ => None | ||
590 | } | ||
591 | }; | ||
592 | } | ||
593 | self.path_context = Some(path_ctx); | ||
594 | dbg!(&self.path_context); | ||
582 | self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); | 595 | self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); |
583 | self.has_type_args = segment.generic_arg_list().is_some(); | 596 | self.has_type_args = segment.generic_arg_list().is_some(); |
584 | 597 | ||
@@ -623,8 +636,6 @@ impl<'a> CompletionContext<'a> { | |||
623 | .unwrap_or(false); | 636 | .unwrap_or(false); |
624 | self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); | 637 | self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); |
625 | } | 638 | } |
626 | self.is_call |= | ||
627 | matches!(self.completion_location, Some(ImmediateLocation::MethodCall { .. })); | ||
628 | } | 639 | } |
629 | } | 640 | } |
630 | 641 | ||