1 //! See `CompletionContext` structure.
5 use base_db::SourceDatabaseExt;
7 HasAttrs, Local, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo,
10 active_parameter::ActiveParameter,
11 base_db::{FilePosition, SourceDatabase},
12 famous_defs::FamousDefs,
13 FxHashMap, FxHashSet, RootDatabase,
16 algo::{find_node_at_offset, non_trivia_sibling},
17 ast::{self, AttrKind, HasArgList, HasName, NameOrNameRef},
18 match_ast, AstNode, NodeOrToken,
19 SyntaxKind::{self, *},
20 SyntaxNode, SyntaxToken, TextRange, TextSize, T,
26 determine_location, determine_prev_sibling, is_in_loop_body, is_in_token_of_for_loop,
27 previous_token, ImmediateLocation, ImmediatePrevSibling,
32 const COMPLETION_MARKER: &str = "intellijRulezz";
34 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
35 pub(crate) enum PatternRefutability {
40 pub(crate) enum Visible {
46 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
47 pub(super) enum PathKind {
55 annotated_item_kind: Option<SyntaxKind>,
58 /// Path in item position, that is inside an (Assoc)ItemList
69 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
70 pub(super) enum ItemListKind {
79 pub(crate) struct PathCompletionCtx {
80 /// If this is a call with () already there (or {} in case of record patterns)
81 pub(super) has_call_parens: bool,
82 /// If this has a macro call bang !
83 pub(super) has_macro_bang: bool,
84 /// Whether this path stars with a `::`.
85 pub(super) is_absolute_path: bool,
86 /// The qualifier of the current path if it exists.
87 pub(super) qualifier: Option<PathQualifierCtx>,
90 /// The parent of the path we are completing.
91 pub(super) parent: Option<ast::Path>,
92 pub(super) kind: PathKind,
93 /// Whether the path segment has type args or not.
94 pub(super) has_type_args: bool,
98 pub(crate) struct PathQualifierCtx {
99 pub(crate) path: ast::Path,
100 pub(crate) resolution: Option<PathResolution>,
101 /// Whether this path consists solely of `super` segments
102 pub(crate) is_super_chain: bool,
103 /// Whether the qualifier comes from a use tree parent or not
104 pub(crate) use_tree_parent: bool,
106 pub(crate) is_infer_qualifier: bool,
110 pub(super) struct PatternContext {
111 pub(super) refutability: PatternRefutability,
112 pub(super) param_ctx: Option<(ast::ParamList, ast::Param, ParamKind)>,
113 pub(super) has_type_ascription: bool,
114 pub(super) parent_pat: Option<ast::Pat>,
115 pub(super) ref_token: Option<SyntaxToken>,
116 pub(super) mut_token: Option<SyntaxToken>,
120 pub(super) struct LifetimeContext {
121 pub(super) lifetime: Option<ast::Lifetime>,
122 pub(super) kind: LifetimeKind,
126 pub enum LifetimeKind {
127 LifetimeParam { is_decl: bool, param: ast::LifetimeParam },
134 pub struct NameContext {
136 pub(super) name: Option<ast::Name>,
137 pub(super) kind: NameKind,
142 pub(super) enum NameKind {
165 pub(super) struct NameRefContext {
166 /// NameRef syntax in the original file
167 pub(super) nameref: Option<ast::NameRef>,
168 pub(super) dot_access: Option<DotAccess>,
169 pub(super) path_ctx: Option<PathCompletionCtx>,
173 pub(super) enum DotAccess {
175 receiver: Option<ast::Expr>,
176 /// True if the receiver is an integer and there is no ident in the original file after it yet
178 receiver_is_ambiguous_float_literal: bool,
181 receiver: Option<ast::Expr>,
186 #[derive(Clone, Debug, PartialEq, Eq)]
187 pub(crate) enum ParamKind {
189 Closure(ast::ClosureExpr),
192 /// `CompletionContext` is created early during completion to figure out, where
193 /// exactly is the cursor, syntax-wise.
195 pub(crate) struct CompletionContext<'a> {
196 pub(super) sema: Semantics<'a, RootDatabase>,
197 pub(super) scope: SemanticsScope<'a>,
198 pub(super) db: &'a RootDatabase,
199 pub(super) config: &'a CompletionConfig,
200 pub(super) position: FilePosition,
202 /// The token before the cursor, in the original file.
203 pub(super) original_token: SyntaxToken,
204 /// The token before the cursor, in the macro-expanded file.
205 pub(super) token: SyntaxToken,
206 /// The crate of the current file.
207 pub(super) krate: hir::Crate,
208 /// The module of the `scope`.
209 pub(super) module: hir::Module,
211 /// The expected name of what we are completing.
212 /// This is usually the parameter name of the function argument we are completing.
213 pub(super) expected_name: Option<NameOrNameRef>,
214 /// The expected type of what we are completing.
215 pub(super) expected_type: Option<Type>,
217 /// The parent function of the cursor position if it exists.
218 pub(super) function_def: Option<ast::Fn>,
219 /// The parent impl of the cursor position if it exists.
220 pub(super) impl_def: Option<ast::Impl>,
221 /// Are we completing inside a let statement with a missing semicolon?
222 pub(super) incomplete_let: bool,
224 pub(super) completion_location: Option<ImmediateLocation>,
225 pub(super) prev_sibling: Option<ImmediatePrevSibling>,
226 pub(super) fake_attribute_under_caret: Option<ast::Attr>,
227 pub(super) previous_token: Option<SyntaxToken>,
229 pub(super) name_ctx: Option<NameContext>,
230 pub(super) lifetime_ctx: Option<LifetimeContext>,
231 pub(super) nameref_ctx: Option<NameRefContext>,
233 pub(super) pattern_ctx: Option<PatternContext>,
235 pub(super) existing_derives: FxHashSet<hir::Macro>,
237 pub(super) locals: FxHashMap<Name, Local>,
240 impl<'a> CompletionContext<'a> {
241 /// The range of the identifier that is being completed.
242 pub(crate) fn source_range(&self) -> TextRange {
243 // check kind of macro-expanded token, but use range of original token
244 let kind = self.token.kind();
247 // assume we are completing a lifetime but the user has only typed the '
248 cov_mark::hit!(completes_if_lifetime_without_idents);
249 TextRange::at(self.original_token.text_range().start(), TextSize::from(1))
251 IDENT | LIFETIME_IDENT | UNDERSCORE => self.original_token.text_range(),
252 _ if kind.is_keyword() => self.original_token.text_range(),
253 _ => TextRange::empty(self.position.offset),
257 pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
258 self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
261 pub(crate) fn famous_defs(&self) -> FamousDefs {
262 FamousDefs(&self.sema, self.krate)
265 pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
266 match &self.nameref_ctx {
267 Some(NameRefContext {
269 Some(DotAccess::Method { receiver, .. } | DotAccess::Field { receiver, .. }),
271 }) => receiver.as_ref(),
276 pub(crate) fn has_dot_receiver(&self) -> bool {
277 self.dot_receiver().is_some()
280 pub(crate) fn expects_assoc_item(&self) -> bool {
281 matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
284 pub(crate) fn expects_variant(&self) -> bool {
285 matches!(self.name_ctx, Some(NameContext { kind: NameKind::Variant, .. }))
288 pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
289 matches!(self.completion_location, Some(ImmediateLocation::Impl))
292 pub(crate) fn expects_item(&self) -> bool {
293 matches!(self.completion_location, Some(ImmediateLocation::ItemList))
296 pub(crate) fn expects_generic_arg(&self) -> bool {
297 matches!(self.completion_location, Some(ImmediateLocation::GenericArgList(_)))
300 pub(crate) fn has_block_expr_parent(&self) -> bool {
301 matches!(self.completion_location, Some(ImmediateLocation::StmtList))
304 pub(crate) fn expects_ident_ref_expr(&self) -> bool {
305 matches!(self.completion_location, Some(ImmediateLocation::RefExpr))
308 pub(crate) fn expect_field(&self) -> bool {
309 matches!(self.completion_location, Some(ImmediateLocation::TupleField))
310 || matches!(self.name_ctx, Some(NameContext { kind: NameKind::RecordField, .. }))
313 /// Whether the cursor is right after a trait or impl header.
314 /// trait Foo ident$0
315 // FIXME: This probably shouldn't exist
316 pub(crate) fn has_unfinished_impl_or_trait_prev_sibling(&self) -> bool {
319 Some(ImmediatePrevSibling::ImplDefType | ImmediatePrevSibling::TraitDefName)
323 // FIXME: This probably shouldn't exist
324 pub(crate) fn has_impl_prev_sibling(&self) -> bool {
325 matches!(self.prev_sibling, Some(ImmediatePrevSibling::ImplDefType))
328 pub(crate) fn has_visibility_prev_sibling(&self) -> bool {
329 matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility))
332 pub(crate) fn after_if(&self) -> bool {
333 matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
336 // FIXME: This shouldn't exist
337 pub(crate) fn is_path_disallowed(&self) -> bool {
338 self.previous_token_is(T![unsafe])
341 Some(ImmediatePrevSibling::Attribute | ImmediatePrevSibling::Visibility)
344 self.completion_location,
345 Some(ImmediateLocation::RecordPat(_) | ImmediateLocation::RecordExpr(_))
349 Some(NameContext { kind: NameKind::Module(_) | NameKind::Rename, .. })
353 pub(crate) fn path_context(&self) -> Option<&PathCompletionCtx> {
354 self.nameref_ctx.as_ref().and_then(|ctx| ctx.path_ctx.as_ref())
357 pub(crate) fn expects_expression(&self) -> bool {
358 matches!(self.path_context(), Some(PathCompletionCtx { kind: PathKind::Expr { .. }, .. }))
361 pub(crate) fn expects_type(&self) -> bool {
362 matches!(self.path_context(), Some(PathCompletionCtx { kind: PathKind::Type, .. }))
365 pub(crate) fn path_is_call(&self) -> bool {
366 self.path_context().map_or(false, |it| it.has_call_parens)
369 pub(crate) fn is_non_trivial_path(&self) -> bool {
373 PathCompletionCtx { is_absolute_path: true, .. }
374 | PathCompletionCtx { qualifier: Some(_), .. }
379 pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
380 self.path_context().and_then(|it| it.qualifier.as_ref().map(|it| &it.path))
383 pub(crate) fn path_kind(&self) -> Option<PathKind> {
384 self.path_context().map(|it| it.kind)
387 pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
388 self.token.kind() == BANG && self.token.parent().map_or(false, |it| it.kind() == MACRO_CALL)
391 /// Checks if an item is visible and not `doc(hidden)` at the completion site.
392 pub(crate) fn is_visible<I>(&self, item: &I) -> Visible
394 I: hir::HasVisibility + hir::HasAttrs + hir::HasCrate + Copy,
396 self.is_visible_impl(&item.visibility(self.db), &item.attrs(self.db), item.krate(self.db))
399 pub(crate) fn is_scope_def_hidden(&self, scope_def: ScopeDef) -> bool {
400 if let (Some(attrs), Some(krate)) = (scope_def.attrs(self.db), scope_def.krate(self.db)) {
401 return self.is_doc_hidden(&attrs, krate);
407 /// Check if an item is `#[doc(hidden)]`.
408 pub(crate) fn is_item_hidden(&self, item: &hir::ItemInNs) -> bool {
409 let attrs = item.attrs(self.db);
410 let krate = item.krate(self.db);
411 match (attrs, krate) {
412 (Some(attrs), Some(krate)) => self.is_doc_hidden(&attrs, krate),
416 /// Whether the given trait is an operator trait or not.
417 pub(crate) fn is_ops_trait(&self, trait_: hir::Trait) -> bool {
418 match trait_.attrs(self.db).lang() {
419 Some(lang) => OP_TRAIT_LANG_NAMES.contains(&lang.as_str()),
424 /// Returns the traits in scope, with the [`Drop`] trait removed.
425 pub(crate) fn traits_in_scope(&self) -> hir::VisibleTraits {
426 let mut traits_in_scope = self.scope.visible_traits();
427 if let Some(drop) = self.famous_defs().core_ops_Drop() {
428 traits_in_scope.0.remove(&drop.into());
433 /// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
434 pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
435 let _p = profile::span("CompletionContext::process_all_names");
436 self.scope.process_all_names(&mut |name, def| {
437 if self.is_scope_def_hidden(def) {
445 pub(crate) fn process_all_names_raw(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
446 let _p = profile::span("CompletionContext::process_all_names_raw");
447 self.scope.process_all_names(&mut |name, def| f(name, def));
452 vis: &hir::Visibility,
454 defining_crate: hir::Crate,
456 if !vis.is_visible_from(self.db, self.module.into()) {
457 if !self.config.enable_private_editable {
460 // If the definition location is editable, also show private items
461 let root_file = defining_crate.root_file(self.db);
462 let source_root_id = self.db.file_source_root(root_file);
463 let is_editable = !self.db.source_root(source_root_id).is_library;
464 return if is_editable { Visible::Editable } else { Visible::No };
467 if self.is_doc_hidden(attrs, defining_crate) {
474 fn is_doc_hidden(&self, attrs: &hir::Attrs, defining_crate: hir::Crate) -> bool {
475 // `doc(hidden)` items are only completed within the defining crate.
476 self.krate != defining_crate && attrs.has_doc_hidden()
480 // CompletionContext construction
481 impl<'a> CompletionContext<'a> {
483 db: &'a RootDatabase,
484 position @ FilePosition { file_id, offset }: FilePosition,
485 config: &'a CompletionConfig,
486 ) -> Option<CompletionContext<'a>> {
487 let _p = profile::span("CompletionContext::new");
488 let sema = Semantics::new(db);
490 let original_file = sema.parse(file_id);
492 // Insert a fake ident to get a valid parse tree. We will use this file
493 // to determine context, though the original_file will be used for
494 // actual completion.
495 let file_with_fake_ident = {
496 let parse = db.parse(file_id);
497 let edit = Indel::insert(offset, COMPLETION_MARKER.to_string());
498 parse.reparse(&edit).tree()
500 let fake_ident_token =
501 file_with_fake_ident.syntax().token_at_offset(offset).right_biased()?;
503 let original_token = original_file.syntax().token_at_offset(offset).left_biased()?;
504 let token = sema.descend_into_macros_single(original_token.clone());
505 let scope = sema.scope_at_offset(&token.parent()?, offset)?;
506 let krate = scope.krate();
507 let module = scope.module();
509 let mut locals = FxHashMap::default();
510 scope.process_all_names(&mut |name, scope| {
511 if let ScopeDef::Local(local) = scope {
512 locals.insert(name, local);
516 let mut ctx = CompletionContext {
530 incomplete_let: false,
531 completion_location: None,
533 fake_attribute_under_caret: None,
534 previous_token: None,
539 existing_derives: Default::default(),
543 original_file.syntax().clone(),
544 file_with_fake_ident.syntax().clone(),
551 /// Expand attributes and macro calls at the current cursor position for both the original file
552 /// and fake file repeatedly. As soon as one of the two expansions fail we stop so the original
553 /// and speculative states stay in sync.
556 mut original_file: SyntaxNode,
557 mut speculative_file: SyntaxNode,
558 mut offset: TextSize,
559 mut fake_ident_token: SyntaxToken,
561 let _p = profile::span("CompletionContext::expand_and_fill");
562 let mut derive_ctx = None;
566 |item: &ast::Item| item.syntax().ancestors().skip(1).find_map(ast::Item::cast);
567 let ancestor_items = iter::successors(
569 find_node_at_offset::<ast::Item>(&original_file, offset),
570 find_node_at_offset::<ast::Item>(&speculative_file, offset),
572 |(a, b)| parent_item(a).zip(parent_item(b)),
575 // first try to expand attributes as these are always the outermost macro calls
576 'ancestors: for (actual_item, item_with_fake_ident) in ancestor_items {
578 self.sema.expand_attr_macro(&actual_item),
579 self.sema.speculative_expand_attr_macro(
581 &item_with_fake_ident,
582 fake_ident_token.clone(),
585 // maybe parent items have attributes, so continue walking the ancestors
586 (None, None) => continue 'ancestors,
587 // successful expansions
588 (Some(actual_expansion), Some((fake_expansion, fake_mapped_token))) => {
589 let new_offset = fake_mapped_token.text_range().start();
590 if new_offset > actual_expansion.text_range().end() {
591 // offset outside of bounds from the original expansion,
592 // stop here to prevent problems from happening
595 original_file = actual_expansion;
596 speculative_file = fake_expansion;
597 fake_ident_token = fake_mapped_token;
601 // exactly one expansion failed, inconsistent state so stop expanding completely
602 _ => break 'expansion,
606 // No attributes have been expanded, so look for macro_call! token trees or derive token trees
607 let orig_tt = match find_node_at_offset::<ast::TokenTree>(&original_file, offset) {
609 None => break 'expansion,
611 let spec_tt = match find_node_at_offset::<ast::TokenTree>(&speculative_file, offset) {
613 None => break 'expansion,
616 // Expand pseudo-derive expansion
617 if let (Some(orig_attr), Some(spec_attr)) = (
618 orig_tt.syntax().parent().and_then(ast::Meta::cast).and_then(|it| it.parent_attr()),
619 spec_tt.syntax().parent().and_then(ast::Meta::cast).and_then(|it| it.parent_attr()),
621 if let (Some(actual_expansion), Some((fake_expansion, fake_mapped_token))) = (
622 self.sema.expand_derive_as_pseudo_attr_macro(&orig_attr),
623 self.sema.speculative_expand_derive_as_pseudo_attr_macro(
626 fake_ident_token.clone(),
632 fake_mapped_token.text_range().start(),
636 // at this point we won't have any more successful expansions, so stop
640 // Expand fn-like macro calls
641 if let (Some(actual_macro_call), Some(macro_call_with_fake_ident)) = (
642 orig_tt.syntax().ancestors().find_map(ast::MacroCall::cast),
643 spec_tt.syntax().ancestors().find_map(ast::MacroCall::cast),
645 let mac_call_path0 = actual_macro_call.path().as_ref().map(|s| s.syntax().text());
647 macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text());
649 // inconsistent state, stop expanding
650 if mac_call_path0 != mac_call_path1 {
653 let speculative_args = match macro_call_with_fake_ident.token_tree() {
655 None => break 'expansion,
659 self.sema.expand(&actual_macro_call),
660 self.sema.speculative_expand(
663 fake_ident_token.clone(),
666 // successful expansions
667 (Some(actual_expansion), Some((fake_expansion, fake_mapped_token))) => {
668 let new_offset = fake_mapped_token.text_range().start();
669 if new_offset > actual_expansion.text_range().end() {
670 // offset outside of bounds from the original expansion,
671 // stop here to prevent problems from happening
674 original_file = actual_expansion;
675 speculative_file = fake_expansion;
676 fake_ident_token = fake_mapped_token;
680 // at least on expansion failed, we won't have anything to expand from this point
681 // onwards so break out
682 _ => break 'expansion,
686 // none of our states have changed so stop the loop
690 self.fill(&original_file, speculative_file, offset, derive_ctx);
693 /// Calculate the expected type and name of the cursor position.
694 fn expected_type_and_name(&self) -> (Option<Type>, Option<NameOrNameRef>) {
695 let mut node = match self.token.parent() {
697 None => return (None, None),
702 ast::LetStmt(it) => {
703 cov_mark::hit!(expected_type_let_with_leading_char);
704 cov_mark::hit!(expected_type_let_without_leading_char);
706 .and_then(|pat| self.sema.type_of_pat(&pat))
707 .or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it)))
708 .map(TypeInfo::original);
709 let name = match it.pat() {
710 Some(ast::Pat::IdentPat(ident)) => ident.name().map(NameOrNameRef::Name),
711 Some(_) | None => None,
716 ast::LetExpr(it) => {
717 cov_mark::hit!(expected_type_if_let_without_leading_char);
719 .and_then(|pat| self.sema.type_of_pat(&pat))
720 .or_else(|| it.expr().and_then(|it| self.sema.type_of_expr(&it)))
721 .map(TypeInfo::original);
725 cov_mark::hit!(expected_type_fn_param);
726 ActiveParameter::at_token(
730 let name = ap.ident().map(NameOrNameRef::Name);
731 let ty = if has_ref(&self.token) {
732 cov_mark::hit!(expected_type_fn_param_ref);
739 .unwrap_or((None, None))
741 ast::RecordExprFieldList(it) => {
742 // wouldn't try {} be nice...
744 if self.token.kind() == T![..]
745 || self.token.prev_token().map(|t| t.kind()) == Some(T![..])
747 cov_mark::hit!(expected_type_struct_func_update);
748 let record_expr = it.syntax().parent().and_then(ast::RecordExpr::cast)?;
749 let ty = self.sema.type_of_expr(&record_expr.into())?;
755 cov_mark::hit!(expected_type_struct_field_without_leading_char);
756 let expr_field = self.token.prev_sibling_or_token()?
758 .and_then(ast::RecordExprField::cast)?;
759 let (_, _, ty) = self.sema.resolve_record_field(&expr_field)?;
762 expr_field.field_name().map(NameOrNameRef::NameRef),
765 })().unwrap_or((None, None))
767 ast::RecordExprField(it) => {
768 if let Some(expr) = it.expr() {
769 cov_mark::hit!(expected_type_struct_field_with_leading_char);
771 self.sema.type_of_expr(&expr).map(TypeInfo::original),
772 it.field_name().map(NameOrNameRef::NameRef),
775 cov_mark::hit!(expected_type_struct_field_followed_by_comma);
776 let ty = self.sema.resolve_record_field(&it)
777 .map(|(_, _, ty)| ty);
780 it.field_name().map(NameOrNameRef::NameRef),
784 ast::MatchExpr(it) => {
785 cov_mark::hit!(expected_type_match_arm_without_leading_char);
786 let ty = it.expr().and_then(|e| self.sema.type_of_expr(&e)).map(TypeInfo::original);
790 let ty = it.condition()
791 .and_then(|e| self.sema.type_of_expr(&e))
792 .map(TypeInfo::original);
795 ast::IdentPat(it) => {
796 cov_mark::hit!(expected_type_if_let_with_leading_char);
797 cov_mark::hit!(expected_type_match_arm_with_leading_char);
798 let ty = self.sema.type_of_pat(&ast::Pat::from(it)).map(TypeInfo::original);
802 cov_mark::hit!(expected_type_fn_ret_with_leading_char);
803 cov_mark::hit!(expected_type_fn_ret_without_leading_char);
804 let def = self.sema.to_def(&it);
805 (def.map(|def| def.ret_type(self.db)), None)
807 ast::ClosureExpr(it) => {
808 let ty = self.sema.type_of_expr(&it.into());
809 ty.and_then(|ty| ty.original.as_callable(self.db))
810 .map(|c| (Some(c.return_type()), None))
811 .unwrap_or((None, None))
813 ast::ParamList(_) => (None, None),
814 ast::Stmt(_) => (None, None),
815 ast::Item(_) => (None, None),
817 match node.parent() {
822 None => (None, None),
830 /// Fill the completion context, this is what does semantic reasoning about the surrounding context
831 /// of the completion location.
834 original_file: &SyntaxNode,
835 file_with_fake_ident: SyntaxNode,
837 derive_ctx: Option<(SyntaxNode, SyntaxNode, TextSize, ast::Attr)>,
839 let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
840 let syntax_element = NodeOrToken::Token(fake_ident_token);
841 if is_in_token_of_for_loop(syntax_element.clone()) {
843 // there is nothing to complete here except `in` keyword
844 // don't bother populating the context
845 // FIXME: the completion calculations should end up good enough
846 // such that this special case becomes unnecessary
850 self.previous_token = previous_token(syntax_element.clone());
851 self.fake_attribute_under_caret = syntax_element.ancestors().find_map(ast::Attr::cast);
853 self.incomplete_let =
854 syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| {
855 it.syntax().text_range().end() == syntax_element.text_range().end()
858 (self.expected_type, self.expected_name) = self.expected_type_and_name();
860 // Overwrite the path kind for derives
861 if let Some((original_file, file_with_fake_ident, offset, origin_attr)) = derive_ctx {
862 self.existing_derives = self
864 .resolve_derive_macro(&origin_attr)
870 if let Some(ast::NameLike::NameRef(name_ref)) =
871 find_node_at_offset(&file_with_fake_ident, offset)
873 if let Some(parent) = name_ref.syntax().parent() {
874 let (mut nameref_ctx, _) =
875 Self::classify_name_ref(&self.sema, &original_file, name_ref, parent);
876 if let Some(path_ctx) = &mut nameref_ctx.path_ctx {
877 path_ctx.kind = PathKind::Derive;
879 self.nameref_ctx = Some(nameref_ctx);
885 let name_like = match find_node_at_offset(&file_with_fake_ident, offset) {
889 self.completion_location =
890 determine_location(&self.sema, original_file, offset, &name_like);
891 self.prev_sibling = determine_prev_sibling(&name_like);
894 .token_ancestors_with_macros(self.token.clone())
895 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
896 .find_map(ast::Impl::cast);
897 self.function_def = self
899 .token_ancestors_with_macros(self.token.clone())
900 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
901 .find_map(ast::Fn::cast);
904 ast::NameLike::Lifetime(lifetime) => {
905 self.lifetime_ctx = Self::classify_lifetime(&self.sema, original_file, lifetime);
907 ast::NameLike::NameRef(name_ref) => {
908 if let Some(parent) = name_ref.syntax().parent() {
909 let (nameref_ctx, pat_ctx) =
910 Self::classify_name_ref(&self.sema, &original_file, name_ref, parent);
911 self.nameref_ctx = Some(nameref_ctx);
912 self.pattern_ctx = pat_ctx;
915 ast::NameLike::Name(name) => {
916 if let Some((name_ctx, pat_ctx)) =
917 Self::classify_name(&self.sema, original_file, name)
919 self.pattern_ctx = pat_ctx;
920 self.name_ctx = Some(name_ctx);
926 fn classify_lifetime(
927 _sema: &Semantics<RootDatabase>,
928 original_file: &SyntaxNode,
929 lifetime: ast::Lifetime,
930 ) -> Option<LifetimeContext> {
931 let parent = lifetime.syntax().parent()?;
932 if parent.kind() == ERROR {
936 let kind = match_ast! {
938 ast::LifetimeParam(param) => LifetimeKind::LifetimeParam {
939 is_decl: param.lifetime().as_ref() == Some(&lifetime),
942 ast::BreakExpr(_) => LifetimeKind::LabelRef,
943 ast::ContinueExpr(_) => LifetimeKind::LabelRef,
944 ast::Label(_) => LifetimeKind::LabelDef,
945 _ => LifetimeKind::Lifetime,
948 let lifetime = find_node_at_offset(&original_file, lifetime.syntax().text_range().start());
950 Some(LifetimeContext { lifetime, kind })
954 _sema: &Semantics<RootDatabase>,
955 original_file: &SyntaxNode,
957 ) -> Option<(NameContext, Option<PatternContext>)> {
958 let parent = name.syntax().parent()?;
959 let mut pat_ctx = None;
960 let kind = match_ast! {
962 ast::Const(_) => NameKind::Const,
963 ast::ConstParam(_) => NameKind::ConstParam,
964 ast::Enum(_) => NameKind::Enum,
965 ast::Fn(_) => NameKind::Function,
966 ast::IdentPat(bind_pat) => {
967 let is_name_in_field_pat = bind_pat
970 .and_then(ast::RecordPatField::cast)
971 .map_or(false, |pat_field| pat_field.name_ref().is_none());
972 if !is_name_in_field_pat {
973 pat_ctx = Some(pattern_context_for(original_file, bind_pat.into()));
978 ast::MacroDef(_) => NameKind::MacroDef,
979 ast::MacroRules(_) => NameKind::MacroRules,
980 ast::Module(module) => NameKind::Module(module),
981 ast::RecordField(_) => NameKind::RecordField,
982 ast::Rename(_) => NameKind::Rename,
983 ast::SelfParam(_) => NameKind::SelfParam,
984 ast::Static(_) => NameKind::Static,
985 ast::Struct(_) => NameKind::Struct,
986 ast::Trait(_) => NameKind::Trait,
987 ast::TypeAlias(_) => NameKind::TypeAlias,
988 ast::TypeParam(_) => NameKind::TypeParam,
989 ast::Union(_) => NameKind::Union,
990 ast::Variant(_) => NameKind::Variant,
994 let name = find_node_at_offset(&original_file, name.syntax().text_range().start());
995 Some((NameContext { name, kind }, pat_ctx))
998 fn classify_name_ref(
999 sema: &Semantics<RootDatabase>,
1000 original_file: &SyntaxNode,
1001 name_ref: ast::NameRef,
1003 ) -> (NameRefContext, Option<PatternContext>) {
1004 let nameref = find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
1006 let mut nameref_ctx = NameRefContext { dot_access: None, path_ctx: None, nameref };
1008 let segment = match_ast! {
1010 ast::PathSegment(segment) => segment,
1011 ast::FieldExpr(field) => {
1012 let receiver = find_in_original_file(field.expr(), original_file);
1013 let receiver_is_ambiguous_float_literal = match &receiver {
1014 Some(ast::Expr::Literal(l)) => matches! {
1016 ast::LiteralKind::FloatNumber { .. } if l.syntax().last_token().map_or(false, |it| it.kind() == T![.])
1020 nameref_ctx.dot_access = Some(DotAccess::Field { receiver, receiver_is_ambiguous_float_literal });
1021 return (nameref_ctx, None);
1023 ast::MethodCallExpr(method) => {
1024 nameref_ctx.dot_access = Some(
1026 receiver: find_in_original_file(method.receiver(), original_file),
1027 has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some())
1030 return (nameref_ctx, None);
1032 _ => return (nameref_ctx, None),
1036 let path = segment.parent_path();
1037 let mut path_ctx = PathCompletionCtx {
1038 has_call_parens: false,
1039 has_macro_bang: false,
1040 is_absolute_path: false,
1042 parent: path.parent_path(),
1043 kind: PathKind::Item { kind: ItemListKind::SourceFile },
1044 has_type_args: false,
1046 let mut pat_ctx = None;
1048 let is_in_block = |it: &SyntaxNode| {
1051 ast::ExprStmt::can_cast(node.kind()) || ast::StmtList::can_cast(node.kind())
1056 let kind = path.syntax().ancestors().find_map(|it| {
1057 // using Option<Option<PathKind>> as extra controlflow
1058 let kind = match_ast! {
1060 ast::PathType(_) => Some(PathKind::Type),
1061 ast::PathExpr(it) => {
1062 path_ctx.has_call_parens = it.syntax().parent().map_or(false, |it| ast::CallExpr::can_cast(it.kind()));
1063 let in_block_expr = is_in_block(it.syntax());
1064 let in_loop_body = is_in_loop_body(it.syntax());
1066 Some(PathKind::Expr { in_block_expr, in_loop_body })
1068 ast::TupleStructPat(it) => {
1069 path_ctx.has_call_parens = true;
1070 pat_ctx = Some(pattern_context_for(original_file, it.into()));
1073 ast::RecordPat(it) => {
1074 path_ctx.has_call_parens = true;
1075 pat_ctx = Some(pattern_context_for(original_file, it.into()));
1078 ast::PathPat(it) => {
1079 pat_ctx = Some(pattern_context_for(original_file, it.into()));
1082 ast::MacroCall(it) => {
1083 path_ctx.has_macro_bang = it.excl_token().is_some();
1084 let parent = it.syntax().parent();
1085 match parent.as_ref().map(|it| it.kind()) {
1086 Some(SyntaxKind::MACRO_PAT) => Some(PathKind::Pat),
1087 Some(SyntaxKind::MACRO_TYPE) => Some(PathKind::Type),
1088 Some(SyntaxKind::ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::Module }),
1089 Some(SyntaxKind::ASSOC_ITEM_LIST) => Some(PathKind::Item { kind: match parent.and_then(|it| it.parent()).map(|it| it.kind()) {
1090 Some(SyntaxKind::TRAIT) => ItemListKind::Trait,
1091 Some(SyntaxKind::IMPL) => ItemListKind::Impl,
1092 _ => return Some(None),
1094 Some(SyntaxKind::EXTERN_ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::ExternBlock }),
1095 Some(SyntaxKind::SOURCE_FILE) => Some(PathKind::Item { kind: ItemListKind::SourceFile }),
1097 return Some(parent.and_then(ast::MacroExpr::cast).map(|it| {
1098 let in_loop_body = is_in_loop_body(it.syntax());
1099 let in_block_expr = is_in_block(it.syntax());
1100 PathKind::Expr { in_block_expr, in_loop_body }
1105 ast::Meta(meta) => (|| {
1106 let attr = meta.parent_attr()?;
1107 let kind = attr.kind();
1108 let attached = attr.syntax().parent()?;
1109 let is_trailing_outer_attr = kind != AttrKind::Inner
1110 && non_trivia_sibling(attr.syntax().clone().into(), syntax::Direction::Next).is_none();
1111 let annotated_item_kind = if is_trailing_outer_attr {
1114 Some(attached.kind())
1116 Some(PathKind::Attr {
1118 annotated_item_kind,
1121 ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }),
1122 ast::UseTree(_) => Some(PathKind::Use),
1123 ast::ItemList(_) => Some(PathKind::Item { kind: ItemListKind::Module }),
1124 ast::AssocItemList(it) => Some(PathKind::Item { kind: {
1125 match it.syntax().parent()?.kind() {
1126 SyntaxKind::TRAIT => ItemListKind::Trait,
1127 SyntaxKind::IMPL => ItemListKind::Impl,
1131 ast::ExternItemList(_) => Some(PathKind::Item { kind: ItemListKind::ExternBlock }),
1132 ast::SourceFile(_) => Some(PathKind::Item { kind: ItemListKind::SourceFile }),
1139 Some(kind) => path_ctx.kind = kind,
1140 None => return (nameref_ctx, pat_ctx),
1142 path_ctx.has_type_args = segment.generic_arg_list().is_some();
1144 if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
1145 if !use_tree_parent {
1146 path_ctx.is_absolute_path =
1147 path.top_path().segment().map_or(false, |it| it.coloncolon_token().is_some());
1152 .and_then(|it| find_node_in_file(original_file, &it))
1153 .map(|it| it.parent_path());
1154 path_ctx.qualifier = path.map(|path| {
1155 let res = sema.resolve_path(&path);
1156 let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
1157 .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
1160 let is_infer_qualifier = path.qualifier().is_none()
1162 path.segment().and_then(|it| it.kind()),
1163 Some(ast::PathSegmentKind::Type {
1164 type_ref: Some(ast::Type::InferType(_)),
1177 } else if let Some(segment) = path.segment() {
1178 if segment.coloncolon_token().is_some() {
1179 path_ctx.is_absolute_path = true;
1182 nameref_ctx.path_ctx = Some(path_ctx);
1183 (nameref_ctx, pat_ctx)
1187 fn pattern_context_for(original_file: &SyntaxNode, pat: ast::Pat) -> PatternContext {
1188 let mut is_param = None;
1189 let (refutability, has_type_ascription) =
1193 .skip_while(|it| ast::Pat::can_cast(it.kind()))
1195 .map_or((PatternRefutability::Irrefutable, false), |node| {
1196 let refutability = match_ast! {
1198 ast::LetStmt(let_) => return (PatternRefutability::Irrefutable, let_.ty().is_some()),
1199 ast::Param(param) => {
1200 let has_type_ascription = param.ty().is_some();
1202 let fake_param_list = param.syntax().parent().and_then(ast::ParamList::cast)?;
1203 let param_list = find_node_in_file_compensated(original_file, &fake_param_list)?;
1204 let param_list_owner = param_list.syntax().parent()?;
1205 let kind = match_ast! {
1206 match param_list_owner {
1207 ast::ClosureExpr(closure) => ParamKind::Closure(closure),
1208 ast::Fn(fn_) => ParamKind::Function(fn_),
1212 Some((param_list, param, kind))
1214 return (PatternRefutability::Irrefutable, has_type_ascription)
1216 ast::MatchArm(_) => PatternRefutability::Refutable,
1217 ast::LetExpr(_) => PatternRefutability::Refutable,
1218 ast::ForExpr(_) => PatternRefutability::Irrefutable,
1219 _ => PatternRefutability::Irrefutable,
1222 (refutability, false)
1224 let (ref_token, mut_token) = match &pat {
1225 ast::Pat::IdentPat(it) => (it.ref_token(), it.mut_token()),
1230 param_ctx: is_param,
1231 has_type_ascription,
1232 parent_pat: pat.syntax().parent().and_then(ast::Pat::cast),
1238 fn find_in_original_file<N: AstNode>(x: Option<N>, original_file: &SyntaxNode) -> Option<N> {
1239 fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
1240 let range = syntax.text_range().intersect(range)?;
1241 syntax.covering_element(range).ancestors().find_map(N::cast)
1243 x.map(|e| e.syntax().text_range()).and_then(|r| find_node_with_range(original_file, r))
1246 /// Attempts to find `node` inside `syntax` via `node`'s text range.
1247 fn find_node_in_file<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
1248 let syntax_range = syntax.text_range();
1249 let range = node.syntax().text_range();
1250 let intersection = range.intersect(syntax_range)?;
1251 syntax.covering_element(intersection).ancestors().find_map(N::cast)
1254 /// Attempts to find `node` inside `syntax` via `node`'s text range while compensating
1255 /// for the offset introduced by the fake ident.
1256 /// This is wrong if `node` comes before the insertion point! Use `find_node_in_file` instead.
1257 fn find_node_in_file_compensated<N: AstNode>(syntax: &SyntaxNode, node: &N) -> Option<N> {
1258 let syntax_range = syntax.text_range();
1259 let range = node.syntax().text_range();
1260 let end = range.end().checked_sub(TextSize::try_from(COMPLETION_MARKER.len()).ok()?)?;
1261 if end < range.start() {
1264 let range = TextRange::new(range.start(), end);
1265 // our inserted ident could cause `range` to be go outside of the original syntax, so cap it
1266 let intersection = range.intersect(syntax_range)?;
1267 syntax.covering_element(intersection).ancestors().find_map(N::cast)
1270 fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
1271 if let Some(qual) = path.qualifier() {
1272 return Some((qual, false));
1274 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
1275 let use_tree = use_tree_list.syntax().parent().and_then(ast::UseTree::cast)?;
1276 Some((use_tree.path()?, true))
1279 fn has_ref(token: &SyntaxToken) -> bool {
1280 let mut token = token.clone();
1281 for skip in [IDENT, WHITESPACE, T![mut]] {
1282 if token.kind() == skip {
1283 token = match token.prev_token() {
1285 None => return false,
1289 token.kind() == T![&]
1292 const OP_TRAIT_LANG_NAMES: &[&str] = &[
1327 use expect_test::{expect, Expect};
1328 use hir::HirDisplay;
1330 use crate::tests::{position, TEST_CONFIG};
1332 use super::CompletionContext;
1334 fn check_expected_type_and_name(ra_fixture: &str, expect: Expect) {
1335 let (db, pos) = position(ra_fixture);
1336 let config = TEST_CONFIG;
1337 let completion_context = CompletionContext::new(&db, pos, &config).unwrap();
1339 let ty = completion_context
1341 .map(|t| t.display_test(&db).to_string())
1342 .unwrap_or("?".to_owned());
1344 let name = completion_context
1346 .map_or_else(|| "?".to_owned(), |name| name.to_string());
1348 expect.assert_eq(&format!("ty: {}, name: {}", ty, name));
1352 fn expected_type_let_without_leading_char() {
1353 cov_mark::check!(expected_type_let_without_leading_char);
1354 check_expected_type_and_name(
1360 expect![[r#"ty: u32, name: x"#]],
1365 fn expected_type_let_with_leading_char() {
1366 cov_mark::check!(expected_type_let_with_leading_char);
1367 check_expected_type_and_name(
1373 expect![[r#"ty: u32, name: x"#]],
1378 fn expected_type_let_pat() {
1379 check_expected_type_and_name(
1385 expect![[r#"ty: u32, name: ?"#]],
1387 check_expected_type_and_name(
1393 expect![[r#"ty: u32, name: ?"#]],
1398 fn expected_type_fn_param() {
1399 cov_mark::check!(expected_type_fn_param);
1400 check_expected_type_and_name(
1402 fn foo() { bar($0); }
1405 expect![[r#"ty: u32, name: x"#]],
1407 check_expected_type_and_name(
1409 fn foo() { bar(c$0); }
1412 expect![[r#"ty: u32, name: x"#]],
1417 fn expected_type_fn_param_ref() {
1418 cov_mark::check!(expected_type_fn_param_ref);
1419 check_expected_type_and_name(
1421 fn foo() { bar(&$0); }
1424 expect![[r#"ty: u32, name: x"#]],
1426 check_expected_type_and_name(
1428 fn foo() { bar(&mut $0); }
1429 fn bar(x: &mut u32) {}
1431 expect![[r#"ty: u32, name: x"#]],
1433 check_expected_type_and_name(
1435 fn foo() { bar(& c$0); }
1438 expect![[r#"ty: u32, name: x"#]],
1440 check_expected_type_and_name(
1442 fn foo() { bar(&mut c$0); }
1443 fn bar(x: &mut u32) {}
1445 expect![[r#"ty: u32, name: x"#]],
1447 check_expected_type_and_name(
1449 fn foo() { bar(&c$0); }
1452 expect![[r#"ty: u32, name: x"#]],
1457 fn expected_type_struct_field_without_leading_char() {
1458 cov_mark::check!(expected_type_struct_field_without_leading_char);
1459 check_expected_type_and_name(
1461 struct Foo { a: u32 }
1466 expect![[r#"ty: u32, name: a"#]],
1471 fn expected_type_struct_field_followed_by_comma() {
1472 cov_mark::check!(expected_type_struct_field_followed_by_comma);
1473 check_expected_type_and_name(
1475 struct Foo { a: u32 }
1480 expect![[r#"ty: u32, name: a"#]],
1485 fn expected_type_generic_struct_field() {
1486 check_expected_type_and_name(
1488 struct Foo<T> { a: T }
1489 fn foo() -> Foo<u32> {
1493 expect![[r#"ty: u32, name: a"#]],
1498 fn expected_type_struct_field_with_leading_char() {
1499 cov_mark::check!(expected_type_struct_field_with_leading_char);
1500 check_expected_type_and_name(
1502 struct Foo { a: u32 }
1507 expect![[r#"ty: u32, name: a"#]],
1512 fn expected_type_match_arm_without_leading_char() {
1513 cov_mark::check!(expected_type_match_arm_without_leading_char);
1514 check_expected_type_and_name(
1521 expect![[r#"ty: E, name: ?"#]],
1526 fn expected_type_match_arm_with_leading_char() {
1527 cov_mark::check!(expected_type_match_arm_with_leading_char);
1528 check_expected_type_and_name(
1535 expect![[r#"ty: E, name: ?"#]],
1540 fn expected_type_if_let_without_leading_char() {
1541 cov_mark::check!(expected_type_if_let_without_leading_char);
1542 check_expected_type_and_name(
1544 enum Foo { Bar, Baz, Quux }
1551 expect![[r#"ty: Foo, name: ?"#]],
1556 fn expected_type_if_let_with_leading_char() {
1557 cov_mark::check!(expected_type_if_let_with_leading_char);
1558 check_expected_type_and_name(
1560 enum Foo { Bar, Baz, Quux }
1567 expect![[r#"ty: Foo, name: ?"#]],
1572 fn expected_type_fn_ret_without_leading_char() {
1573 cov_mark::check!(expected_type_fn_ret_without_leading_char);
1574 check_expected_type_and_name(
1580 expect![[r#"ty: u32, name: ?"#]],
1585 fn expected_type_fn_ret_with_leading_char() {
1586 cov_mark::check!(expected_type_fn_ret_with_leading_char);
1587 check_expected_type_and_name(
1593 expect![[r#"ty: u32, name: ?"#]],
1598 fn expected_type_fn_ret_fn_ref_fully_typed() {
1599 check_expected_type_and_name(
1605 expect![[r#"ty: u32, name: ?"#]],
1610 fn expected_type_closure_param_return() {
1611 // FIXME: make this work with `|| $0`
1612 check_expected_type_and_name(
1619 fn bar(f: impl FnOnce() -> u32) {}
1621 expect![[r#"ty: u32, name: ?"#]],
1626 fn expected_type_generic_function() {
1627 check_expected_type_and_name(
1635 expect![[r#"ty: u32, name: t"#]],
1640 fn expected_type_generic_method() {
1641 check_expected_type_and_name(
1649 fn bar(self, t: T) {}
1652 expect![[r#"ty: u32, name: t"#]],
1657 fn expected_type_functional_update() {
1658 cov_mark::check!(expected_type_struct_func_update);
1659 check_expected_type_and_name(
1661 struct Foo { field: u32 }
1668 expect![[r#"ty: Foo, name: ?"#]],
1673 fn expected_type_param_pat() {
1674 check_expected_type_and_name(
1676 struct Foo { field: u32 }
1679 expect![[r#"ty: Foo, name: ?"#]],
1681 check_expected_type_and_name(
1683 struct Foo { field: u32 }
1686 // FIXME make this work, currently fails due to pattern recovery eating the `:`
1687 expect![[r#"ty: ?, name: ?"#]],