2020-10-18 05:09:00 -05:00
//! `completions` crate provides utilities for generating completions of user input.
2021-06-16 10:37:23 -05:00
mod completions ;
2020-10-25 02:59:15 -05:00
mod config ;
mod context ;
2021-06-16 10:37:23 -05:00
mod item ;
2020-07-04 11:53:30 -05:00
mod patterns ;
2020-11-01 03:35:04 -06:00
mod render ;
2019-01-08 13:33:36 -06:00
2021-06-16 10:37:23 -05:00
#[ cfg(test) ]
mod tests ;
2019-01-08 13:33:36 -06:00
2021-01-16 18:52:36 -06:00
use completions ::flyimport ::position_for_import ;
2020-12-04 08:03:22 -06:00
use ide_db ::{
2021-02-24 17:06:31 -06:00
base_db ::FilePosition ,
2021-03-20 08:02:52 -05:00
helpers ::{
import_assets ::{ LocatedImport , NameToImport } ,
insert_use ::ImportScope ,
} ,
2021-03-02 17:26:53 -06:00
items_locator , RootDatabase ,
2020-12-04 08:03:22 -06:00
} ;
use text_edit ::TextEdit ;
2019-01-08 13:33:36 -06:00
2020-10-25 03:26:38 -05:00
use crate ::{ completions ::Completions , context ::CompletionContext , item ::CompletionKind } ;
2019-01-08 13:33:36 -06:00
2020-10-18 05:09:00 -05:00
pub use crate ::{
2021-01-06 11:23:53 -06:00
config ::CompletionConfig ,
2021-03-11 21:13:27 -06:00
item ::{ CompletionItem , CompletionItemKind , CompletionRelevance , ImportEdit , InsertTextFormat } ,
2019-07-04 15:05:17 -05:00
} ;
2019-01-08 13:33:36 -06:00
2020-05-31 04:29:19 -05:00
//FIXME: split the following feature into fine-grained features.
// Feature: Magic Completions
//
// In addition to usual reference completion, rust-analyzer provides some ✨magic✨
// completions as well:
//
// Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor
// is placed at the appropriate position. Even though `if` is easy to type, you
// still want to complete it, to get ` { }` for free! `return` is inserted with a
// space or `;` depending on the return type of the function.
//
// When completing a function call, `()` are automatically inserted. If a function
// takes arguments, the cursor is positioned inside the parenthesis.
//
// There are postfix completions, which can be triggered by typing something like
// `foo().if`. The word after `.` determines postfix completion. Possible variants are:
//
// - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result`
// - `expr.match` -> `match expr {}`
// - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result`
// - `expr.ref` -> `&expr`
// - `expr.refm` -> `&mut expr`
2021-01-06 14:15:48 -06:00
// - `expr.let` -> `let $0 = expr;`
// - `expr.letm` -> `let mut $0 = expr;`
2020-05-31 04:29:19 -05:00
// - `expr.not` -> `!expr`
// - `expr.dbg` -> `dbg!(expr)`
2020-10-14 08:23:51 -05:00
// - `expr.dbgr` -> `dbg!(&expr)`
// - `expr.call` -> `(expr)`
2020-05-31 04:29:19 -05:00
//
// There also snippet completions:
//
// .Expressions
2020-07-19 11:25:19 -05:00
// - `pd` -> `eprintln!(" = {:?}", );`
2020-07-01 22:06:00 -05:00
// - `ppd` -> `eprintln!(" = {:#?}", );`
2020-05-31 04:29:19 -05:00
//
// .Items
2020-07-01 22:06:00 -05:00
// - `tfn` -> `#[test] fn feature(){}`
2020-05-31 04:29:19 -05:00
// - `tmod` ->
// ```rust
// #[cfg(test)]
// mod tests {
// use super::*;
//
// #[test]
2020-07-01 22:06:00 -05:00
// fn test_name() {}
2020-05-31 04:29:19 -05:00
// }
// ```
2020-11-24 16:42:58 -06:00
//
2020-12-08 06:27:18 -06:00
// And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities.
// Those are the additional completion options with automatic `use` import and options from all project importable items,
2021-05-10 13:05:32 -05:00
// fuzzy matched against the completion input.
2021-03-30 18:08:10 -05:00
//
// image::https://user-images.githubusercontent.com/48062697/113020667-b72ab880-917a-11eb-8778-716cf26a0eb3.gif[]
2020-05-31 04:29:19 -05:00
2019-01-08 13:33:36 -06:00
/// Main entry point for completion. We run completion as a two-phase process.
///
/// First, we look at the position and collect a so-called `CompletionContext.
/// This is a somewhat messy process, because, during completion, syntax tree is
/// incomplete and can look really weird.
///
/// Once the context is collected, we run a series of completion routines which
2019-02-11 10:18:27 -06:00
/// look at the context and produce completion items. One subtlety about this
2019-01-08 13:33:36 -06:00
/// phase is that completion engine should not filter by the substring which is
/// already present, it should give all possible variants for the identifier at
/// the caret. In other words, for
///
2020-08-22 14:03:02 -05:00
/// ```no_run
2019-01-08 13:33:36 -06:00
/// fn f() {
/// let foo = 92;
2021-01-06 14:15:48 -06:00
/// let _ = bar$0
2019-01-08 13:33:36 -06:00
/// }
/// ```
///
/// `foo` *should* be present among the completion variants. Filtering by
/// identifier prefix/fuzzy match should be done higher in the stack, together
/// with ordering of completions (currently this is done by the client).
2021-04-05 08:27:45 -05:00
///
2021-05-24 14:21:25 -05:00
/// # Speculative Completion Problem
2021-04-05 08:27:45 -05:00
///
/// There's a curious unsolved problem in the current implementation. Often, you
/// want to compute completions on a *slightly different* text document.
///
/// In the simplest case, when the code looks like `let x = `, you want to
/// insert a fake identifier to get a better syntax tree: `let x = complete_me`.
///
/// We do this in `CompletionContext`, and it works OK-enough for *syntax*
/// analysis. However, we might want to, eg, ask for the type of `complete_me`
/// variable, and that's where our current infrastructure breaks down. salsa
/// doesn't allow such "phantom" inputs.
///
/// Another case where this would be instrumental is macro expansion. We want to
2021-05-24 14:21:25 -05:00
/// insert a fake ident and re-expand code. There's `expand_speculative` as a
2021-04-05 08:27:45 -05:00
/// work-around for this.
///
/// A different use-case is completion of injection (examples and links in doc
/// comments). When computing completion for a path in a doc-comment, you want
/// to inject a fake path expression into the item being documented and complete
/// that.
///
/// IntelliJ has CodeFragment/Context infrastructure for that. You can create a
/// temporary PSI node, and say that the context ("parent") of this node is some
/// existing node. Asking for, eg, type of this `CodeFragment` node works
/// correctly, as the underlying infrastructure makes use of contexts to do
/// analysis.
2020-10-18 05:09:00 -05:00
pub fn completions (
2020-03-10 12:39:17 -05:00
db : & RootDatabase ,
2020-03-31 09:02:55 -05:00
config : & CompletionConfig ,
2020-05-17 05:09:53 -05:00
position : FilePosition ,
2020-03-10 12:39:17 -05:00
) -> Option < Completions > {
2020-03-31 09:02:55 -05:00
let ctx = CompletionContext ::new ( db , position , config ) ? ;
2019-01-08 13:33:36 -06:00
2020-10-12 02:59:15 -05:00
if ctx . no_completion_required ( ) {
2021-06-16 10:37:23 -05:00
cov_mark ::hit! ( no_completion_required ) ;
2020-10-12 02:59:15 -05:00
// No work required here.
return None ;
}
2019-01-08 13:33:36 -06:00
let mut acc = Completions ::default ( ) ;
2020-10-25 02:59:15 -05:00
completions ::attribute ::complete_attribute ( & mut acc , & ctx ) ;
completions ::fn_param ::complete_fn_param ( & mut acc , & ctx ) ;
completions ::keyword ::complete_expr_keyword ( & mut acc , & ctx ) ;
completions ::keyword ::complete_use_tree_keyword ( & mut acc , & ctx ) ;
completions ::snippet ::complete_expr_snippet ( & mut acc , & ctx ) ;
completions ::snippet ::complete_item_snippet ( & mut acc , & ctx ) ;
completions ::qualified_path ::complete_qualified_path ( & mut acc , & ctx ) ;
completions ::unqualified_path ::complete_unqualified_path ( & mut acc , & ctx ) ;
completions ::dot ::complete_dot ( & mut acc , & ctx ) ;
completions ::record ::complete_record ( & mut acc , & ctx ) ;
completions ::pattern ::complete_pattern ( & mut acc , & ctx ) ;
completions ::postfix ::complete_postfix ( & mut acc , & ctx ) ;
completions ::trait_impl ::complete_trait_impl ( & mut acc , & ctx ) ;
completions ::mod_ ::complete_mod ( & mut acc , & ctx ) ;
2021-01-16 11:33:36 -06:00
completions ::flyimport ::import_on_the_fly ( & mut acc , & ctx ) ;
2021-03-20 16:43:42 -05:00
completions ::lifetime ::complete_lifetime ( & mut acc , & ctx ) ;
2021-03-20 19:00:09 -05:00
completions ::lifetime ::complete_label ( & mut acc , & ctx ) ;
2020-02-09 12:24:34 -06:00
2019-01-15 12:09:51 -06:00
Some ( acc )
2019-01-08 13:33:36 -06:00
}
2020-05-31 10:33:48 -05:00
2020-12-04 08:03:22 -06:00
/// Resolves additional completion data at the position given.
pub fn resolve_completion_edits (
db : & RootDatabase ,
config : & CompletionConfig ,
position : FilePosition ,
full_import_path : & str ,
2020-12-29 06:35:49 -06:00
imported_name : String ,
2020-12-06 15:58:15 -06:00
) -> Option < Vec < TextEdit > > {
2020-12-04 08:03:22 -06:00
let ctx = CompletionContext ::new ( db , position , config ) ? ;
2021-01-16 18:52:36 -06:00
let position_for_import = position_for_import ( & ctx , None ) ? ;
2021-04-20 12:28:18 -05:00
let scope = ImportScope ::find_insert_use_container_with_macros ( position_for_import , & ctx . sema ) ? ;
2020-12-04 08:03:22 -06:00
2021-01-16 18:52:36 -06:00
let current_module = ctx . sema . scope ( position_for_import ) . module ( ) ? ;
2020-12-04 08:03:22 -06:00
let current_crate = current_module . krate ( ) ;
2021-03-20 15:54:04 -05:00
let ( import_path , item_to_import ) = items_locator ::items_with_name (
2021-03-20 08:02:52 -05:00
& ctx . sema ,
current_crate ,
NameToImport ::Exact ( imported_name ) ,
items_locator ::AssocItemSearch ::Include ,
2021-03-20 15:54:04 -05:00
Some ( items_locator ::DEFAULT_QUERY_SEARCH_LIMIT ) ,
2021-03-20 08:02:52 -05:00
)
. filter_map ( | candidate | {
current_module
. find_use_path_prefixed ( db , candidate , config . insert_use . prefix_kind )
. zip ( Some ( candidate ) )
} )
. find ( | ( mod_path , _ ) | mod_path . to_string ( ) = = full_import_path ) ? ;
2021-03-03 15:55:21 -06:00
let import =
LocatedImport ::new ( import_path . clone ( ) , item_to_import , item_to_import , Some ( import_path ) ) ;
2020-12-04 08:03:22 -06:00
2021-03-03 15:55:21 -06:00
ImportEdit { import , scope } . to_text_edit ( config . insert_use ) . map ( | edit | vec! [ edit ] )
2020-12-04 08:03:22 -06:00
}