2020-10-18 05:09:00 -05:00
|
|
|
//! `completions` crate provides utilities for generating completions of user input.
|
|
|
|
|
2020-04-23 19:01:23 -05:00
|
|
|
mod completion_config;
|
2019-01-08 13:33:36 -06:00
|
|
|
mod completion_item;
|
|
|
|
mod completion_context;
|
2019-02-24 09:51:38 -06:00
|
|
|
mod presentation;
|
2020-07-04 11:53:30 -05:00
|
|
|
mod patterns;
|
2020-10-20 14:29:31 -05:00
|
|
|
mod generated_lint_completions;
|
2020-07-04 11:53:30 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_utils;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-04-23 11:22:33 -05:00
|
|
|
mod complete_attribute;
|
2019-01-08 13:33:36 -06:00
|
|
|
mod complete_dot;
|
2020-03-31 16:50:45 -05:00
|
|
|
mod complete_record;
|
2019-02-24 14:49:47 -06:00
|
|
|
mod complete_pattern;
|
2019-01-08 13:33:36 -06:00
|
|
|
mod complete_fn_param;
|
|
|
|
mod complete_keyword;
|
|
|
|
mod complete_snippet;
|
2020-04-07 06:19:57 -05:00
|
|
|
mod complete_qualified_path;
|
|
|
|
mod complete_unqualified_path;
|
2019-01-20 23:19:51 -06:00
|
|
|
mod complete_postfix;
|
2019-09-11 09:44:44 -05:00
|
|
|
mod complete_macro_in_item_position;
|
2020-01-22 22:38:03 -06:00
|
|
|
mod complete_trait_impl;
|
2020-09-07 11:21:39 -05:00
|
|
|
mod complete_mod;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-10-24 03:39:57 -05:00
|
|
|
use ide_db::base_db::FilePosition;
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::RootDatabase;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
|
|
|
use crate::{
|
2020-10-18 05:09:00 -05:00
|
|
|
completion_context::CompletionContext,
|
|
|
|
completion_item::{CompletionKind, Completions},
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
|
|
|
|
2020-10-18 05:09:00 -05:00
|
|
|
pub use crate::{
|
2020-04-23 19:01:23 -05:00
|
|
|
completion_config::CompletionConfig,
|
|
|
|
completion_item::{CompletionItem, CompletionItemKind, CompletionScore, 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`
|
|
|
|
// - `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
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
|
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;
|
|
|
|
/// let _ = bar<|>
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// `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).
|
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() {
|
|
|
|
// No work required here.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
let mut acc = Completions::default();
|
2020-05-04 08:07:09 -05:00
|
|
|
complete_attribute::complete_attribute(&mut acc, &ctx);
|
|
|
|
complete_fn_param::complete_fn_param(&mut acc, &ctx);
|
|
|
|
complete_keyword::complete_expr_keyword(&mut acc, &ctx);
|
|
|
|
complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
|
|
|
|
complete_snippet::complete_expr_snippet(&mut acc, &ctx);
|
|
|
|
complete_snippet::complete_item_snippet(&mut acc, &ctx);
|
|
|
|
complete_qualified_path::complete_qualified_path(&mut acc, &ctx);
|
|
|
|
complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx);
|
|
|
|
complete_dot::complete_dot(&mut acc, &ctx);
|
|
|
|
complete_record::complete_record(&mut acc, &ctx);
|
|
|
|
complete_pattern::complete_pattern(&mut acc, &ctx);
|
|
|
|
complete_postfix::complete_postfix(&mut acc, &ctx);
|
|
|
|
complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
|
|
|
|
complete_trait_impl::complete_trait_impl(&mut acc, &ctx);
|
2020-09-07 11:21:39 -05:00
|
|
|
complete_mod::complete_mod(&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
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-18 05:09:00 -05:00
|
|
|
use crate::completion_config::CompletionConfig;
|
|
|
|
use crate::test_utils;
|
2020-05-31 10:33:48 -05:00
|
|
|
|
|
|
|
struct DetailAndDocumentation<'a> {
|
|
|
|
detail: &'a str,
|
|
|
|
documentation: &'a str,
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:08:45 -05:00
|
|
|
fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) {
|
2020-10-18 05:09:00 -05:00
|
|
|
let (db, position) = test_utils::position(ra_fixture);
|
2020-05-31 10:33:48 -05:00
|
|
|
let config = CompletionConfig::default();
|
2020-10-18 05:09:00 -05:00
|
|
|
let completions: Vec<_> = crate::completions(&db, &config, position).unwrap().into();
|
2020-05-31 10:33:48 -05:00
|
|
|
for item in completions {
|
|
|
|
if item.detail() == Some(expected.detail) {
|
|
|
|
let opt = item.documentation();
|
|
|
|
let doc = opt.as_ref().map(|it| it.as_str());
|
|
|
|
assert_eq!(doc, Some(expected.documentation));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("completion detail not found: {}", expected.detail)
|
|
|
|
}
|
|
|
|
|
2020-10-12 02:59:15 -05:00
|
|
|
fn check_no_completion(ra_fixture: &str) {
|
2020-10-18 05:09:00 -05:00
|
|
|
let (db, position) = test_utils::position(ra_fixture);
|
2020-10-12 02:59:15 -05:00
|
|
|
let config = CompletionConfig::default();
|
|
|
|
|
2020-10-18 05:09:00 -05:00
|
|
|
let completions: Option<Vec<String>> = crate::completions(&db, &config, position)
|
|
|
|
.and_then(|completions| {
|
|
|
|
let completions: Vec<_> = completions.into();
|
|
|
|
if completions.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(completions)
|
|
|
|
}
|
|
|
|
})
|
2020-10-12 02:59:15 -05:00
|
|
|
.map(|completions| {
|
|
|
|
completions.into_iter().map(|completion| format!("{:?}", completion)).collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
// `assert_eq` instead of `assert!(completions.is_none())` to get the list of completions if test will panic.
|
|
|
|
assert_eq!(completions, None, "Completions were generated, but weren't expected");
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:33:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
|
|
|
|
check_detail_and_documentation(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! bar {
|
|
|
|
() => {
|
|
|
|
struct Bar;
|
|
|
|
impl Bar {
|
|
|
|
#[doc = "Do the foo"]
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bar!();
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let bar = Bar;
|
|
|
|
bar.fo<|>;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
|
|
|
|
check_detail_and_documentation(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! bar {
|
|
|
|
() => {
|
|
|
|
struct Bar;
|
|
|
|
impl Bar {
|
|
|
|
/// Do the foo
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bar!();
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let bar = Bar;
|
|
|
|
bar.fo<|>;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" },
|
|
|
|
);
|
|
|
|
}
|
2020-10-12 02:59:15 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_completions_required() {
|
2020-10-17 02:47:35 -05:00
|
|
|
// There must be no hint for 'in' keyword.
|
2020-10-12 02:59:15 -05:00
|
|
|
check_no_completion(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
for i i<|>
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-17 02:47:35 -05:00
|
|
|
);
|
|
|
|
// After 'in' keyword hints may be spawned.
|
|
|
|
check_detail_and_documentation(
|
|
|
|
r#"
|
|
|
|
/// Do the foo
|
|
|
|
fn foo() -> &'static str { "foo" }
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
for c in fo<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
DetailAndDocumentation {
|
|
|
|
detail: "fn foo() -> &'static str",
|
|
|
|
documentation: "Do the foo",
|
|
|
|
},
|
|
|
|
);
|
2020-10-12 02:59:15 -05:00
|
|
|
}
|
2020-05-31 10:33:48 -05:00
|
|
|
}
|