2020-10-18 13:09:00 +03:00
|
|
|
//! This file provides snippet completions, like `pd` => `eprintln!(...)`.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2021-10-05 18:00:27 +02:00
|
|
|
use hir::Documentation;
|
2021-10-04 19:22:41 +02:00
|
|
|
use ide_db::helpers::{insert_use::ImportScope, SnippetCap};
|
2021-06-16 17:56:04 +02:00
|
|
|
use syntax::T;
|
2021-01-06 20:43:46 +03:00
|
|
|
|
2020-10-18 13:09:00 +03:00
|
|
|
use crate::{
|
2021-06-07 20:45:17 +02:00
|
|
|
context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem,
|
2021-10-04 19:22:41 +02:00
|
|
|
CompletionItemKind, CompletionKind, Completions, SnippetScope,
|
2019-07-04 23:05:17 +03:00
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2020-04-24 02:26:38 +02:00
|
|
|
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
|
2021-03-12 12:12:32 +03:00
|
|
|
let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label);
|
|
|
|
item.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet);
|
|
|
|
item
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
2021-06-07 20:45:17 +02:00
|
|
|
if ctx.function_def.is_none() {
|
2019-01-08 22:33:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-06-07 20:45:17 +02:00
|
|
|
|
|
|
|
let can_be_stmt = match ctx.path_context {
|
|
|
|
Some(PathCompletionContext { is_trivial_path: true, can_be_stmt, .. }) => can_be_stmt,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2020-04-24 02:26:38 +02:00
|
|
|
let cap = match ctx.config.snippet_cap {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
2019-01-23 13:21:29 +08:00
|
|
|
|
2021-10-04 19:22:41 +02:00
|
|
|
if !ctx.config.snippets.is_empty() {
|
|
|
|
add_custom_completions(acc, ctx, cap, SnippetScope::Expr);
|
|
|
|
}
|
|
|
|
|
2021-06-07 20:45:17 +02:00
|
|
|
if can_be_stmt {
|
2021-05-25 11:42:16 -03:00
|
|
|
snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
|
|
|
|
snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
2021-06-17 15:43:21 +02:00
|
|
|
if !ctx.expects_item()
|
|
|
|
|| ctx.previous_token_is(T![unsafe])
|
|
|
|
|| ctx.path_qual().is_some()
|
|
|
|
|| ctx.has_impl_or_trait_prev_sibling()
|
|
|
|
{
|
2019-01-08 22:33:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-06-16 17:56:04 +02:00
|
|
|
if ctx.has_visibility_prev_sibling() {
|
|
|
|
return; // technically we could do some of these snippet completions if we were to put the
|
|
|
|
// attributes before the vis node.
|
|
|
|
}
|
2020-04-24 02:26:38 +02:00
|
|
|
let cap = match ctx.config.snippet_cap {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2021-10-04 19:22:41 +02:00
|
|
|
if !ctx.config.snippets.is_empty() {
|
|
|
|
add_custom_completions(acc, ctx, cap, SnippetScope::Item);
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:12:32 +03:00
|
|
|
let mut item = snippet(
|
2020-05-11 18:11:23 +02:00
|
|
|
ctx,
|
|
|
|
cap,
|
2020-08-11 22:33:17 +03:00
|
|
|
"tmod (Test module)",
|
2020-05-11 18:11:23 +02:00
|
|
|
"\
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ${1:test_name}() {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}",
|
2021-03-11 17:46:41 +02:00
|
|
|
);
|
2021-03-12 12:12:32 +03:00
|
|
|
item.lookup_by("tmod");
|
|
|
|
item.add_to(acc);
|
2020-05-11 18:11:23 +02:00
|
|
|
|
2021-03-12 12:12:32 +03:00
|
|
|
let mut item = snippet(
|
2019-01-19 22:02:50 +08:00
|
|
|
ctx,
|
2020-04-24 02:26:38 +02:00
|
|
|
cap,
|
2020-08-11 22:33:17 +03:00
|
|
|
"tfn (Test function)",
|
2019-01-08 22:33:36 +03:00
|
|
|
"\
|
|
|
|
#[test]
|
|
|
|
fn ${1:feature}() {
|
|
|
|
$0
|
|
|
|
}",
|
2021-03-11 17:46:41 +02:00
|
|
|
);
|
2021-03-12 12:12:32 +03:00
|
|
|
item.lookup_by("tfn");
|
|
|
|
item.add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2021-03-12 12:12:32 +03:00
|
|
|
let item = snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}");
|
|
|
|
item.add_to(acc);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2021-10-04 19:22:41 +02:00
|
|
|
|
|
|
|
fn add_custom_completions(
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
cap: SnippetCap,
|
|
|
|
scope: SnippetScope,
|
|
|
|
) -> Option<()> {
|
|
|
|
let import_scope =
|
|
|
|
ImportScope::find_insert_use_container_with_macros(&ctx.token.parent()?, &ctx.sema)?;
|
2021-10-05 17:18:40 +02:00
|
|
|
ctx.config.prefix_snippets().filter(|(_, snip)| snip.scope == scope).for_each(
|
|
|
|
|(trigger, snip)| {
|
|
|
|
let imports = match snip.imports(ctx, &import_scope) {
|
|
|
|
Some(imports) => imports,
|
|
|
|
None => return,
|
|
|
|
};
|
2021-10-05 18:00:27 +02:00
|
|
|
let body = snip.snippet();
|
|
|
|
let mut builder = snippet(ctx, cap, &trigger, &body);
|
|
|
|
builder.documentation(Documentation::new(format!("```rust\n{}\n```", body)));
|
2021-10-05 17:18:40 +02:00
|
|
|
for import in imports.into_iter() {
|
|
|
|
builder.add_import(import);
|
|
|
|
}
|
|
|
|
builder.detail(snip.description.as_deref().unwrap_or_default());
|
|
|
|
builder.add_to(acc);
|
|
|
|
},
|
|
|
|
);
|
2021-10-04 19:22:41 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{
|
|
|
|
tests::{check_edit_with_config, TEST_CONFIG},
|
|
|
|
CompletionConfig, Snippet,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_snippet_completion() {
|
|
|
|
check_edit_with_config(
|
|
|
|
CompletionConfig {
|
|
|
|
snippets: vec![Snippet::new(
|
2021-10-05 17:18:40 +02:00
|
|
|
&["break".into()],
|
2021-10-04 19:22:41 +02:00
|
|
|
&[],
|
2021-10-05 17:18:40 +02:00
|
|
|
&["ControlFlow::Break(())".into()],
|
|
|
|
"",
|
2021-10-04 19:22:41 +02:00
|
|
|
&["core::ops::ControlFlow".into()],
|
2021-10-04 21:44:33 +02:00
|
|
|
crate::SnippetScope::Expr,
|
2021-10-04 19:22:41 +02:00
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
..TEST_CONFIG
|
|
|
|
},
|
|
|
|
"break",
|
|
|
|
r#"
|
|
|
|
//- minicore: try
|
|
|
|
fn main() { $0 }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use core::ops::ControlFlow;
|
|
|
|
|
|
|
|
fn main() { ControlFlow::Break(()) }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|