Bring the implementation closer to VSCode snippet definitions

This commit is contained in:
Lukas Wirth 2021-10-05 17:18:40 +02:00
parent 2b17da60db
commit 77cbf4adbc
9 changed files with 163 additions and 182 deletions

View File

@ -98,8 +98,8 @@ pub use ide_assists::{
Assist, AssistConfig, AssistId, AssistKind, AssistResolveStrategy, SingleResolve, Assist, AssistConfig, AssistId, AssistKind, AssistResolveStrategy, SingleResolve,
}; };
pub use ide_completion::{ pub use ide_completion::{
CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit, CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit, Snippet,
PostfixSnippet, PostfixSnippetScope, Snippet, SnippetScope, SnippetScope,
}; };
pub use ide_db::{ pub use ide_db::{
base_db::{ base_db::{

View File

@ -56,7 +56,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
let postfix_snippet = build_postfix_snippet_builder(ctx, cap, &dot_receiver); let postfix_snippet = build_postfix_snippet_builder(ctx, cap, &dot_receiver);
if !ctx.config.postfix_snippets.is_empty() { if !ctx.config.snippets.is_empty() {
add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text); add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text);
} }
@ -230,21 +230,23 @@ fn add_custom_postfix_completions(
) -> Option<()> { ) -> Option<()> {
let import_scope = let import_scope =
ImportScope::find_insert_use_container_with_macros(&ctx.token.parent()?, &ctx.sema)?; ImportScope::find_insert_use_container_with_macros(&ctx.token.parent()?, &ctx.sema)?;
ctx.config.postfix_snippets.iter().for_each(|snippet| { ctx.config.postfix_snippets().filter(|(_, snip)| snip.is_expr()).for_each(
let imports = match snippet.imports(ctx, &import_scope) { |(trigger, snippet)| {
Some(imports) => imports, let imports = match snippet.imports(ctx, &import_scope) {
None => return, Some(imports) => imports,
}; None => return,
let mut builder = postfix_snippet( };
&snippet.label, let mut builder = postfix_snippet(
snippet.description.as_deref().unwrap_or_default(), trigger,
&format!("{}", snippet.snippet(&receiver_text)), snippet.description.as_deref().unwrap_or_default(),
); &snippet.postfix_snippet(&receiver_text),
for import in imports.into_iter() { );
builder.add_import(import); for import in imports.into_iter() {
} builder.add_import(import);
builder.add_to(acc); }
}); builder.add_to(acc);
},
);
None None
} }
@ -254,7 +256,7 @@ mod tests {
use crate::{ use crate::{
tests::{check_edit, check_edit_with_config, filtered_completion_list, TEST_CONFIG}, tests::{check_edit, check_edit_with_config, filtered_completion_list, TEST_CONFIG},
CompletionConfig, CompletionKind, PostfixSnippet, CompletionConfig, CompletionKind, Snippet,
}; };
fn check(ra_fixture: &str, expect: Expect) { fn check(ra_fixture: &str, expect: Expect) {
@ -476,12 +478,13 @@ fn main() {
fn custom_postfix_completion() { fn custom_postfix_completion() {
check_edit_with_config( check_edit_with_config(
CompletionConfig { CompletionConfig {
postfix_snippets: vec![PostfixSnippet::new( snippets: vec![Snippet::new(
"break".into(),
&["ControlFlow::Break($receiver)".into()],
&[], &[],
&["break".into()],
&["ControlFlow::Break($receiver)".into()],
"",
&["core::ops::ControlFlow".into()], &["core::ops::ControlFlow".into()],
crate::PostfixSnippetScope::Expr, crate::SnippetScope::Expr,
) )
.unwrap()], .unwrap()],
..TEST_CONFIG ..TEST_CONFIG

View File

@ -103,18 +103,20 @@ fn add_custom_completions(
) -> Option<()> { ) -> Option<()> {
let import_scope = let import_scope =
ImportScope::find_insert_use_container_with_macros(&ctx.token.parent()?, &ctx.sema)?; ImportScope::find_insert_use_container_with_macros(&ctx.token.parent()?, &ctx.sema)?;
ctx.config.snippets.iter().filter(|snip| snip.scope == scope).for_each(|snip| { ctx.config.prefix_snippets().filter(|(_, snip)| snip.scope == scope).for_each(
let imports = match snip.imports(ctx, &import_scope) { |(trigger, snip)| {
Some(imports) => imports, let imports = match snip.imports(ctx, &import_scope) {
None => return, Some(imports) => imports,
}; None => return,
let mut builder = snippet(ctx, cap, &snip.label, &snip.snippet); };
for import in imports.into_iter() { let mut builder = snippet(ctx, cap, &trigger, &snip.snippet());
builder.add_import(import); for import in imports.into_iter() {
} builder.add_import(import);
builder.detail(snip.description.as_deref().unwrap_or_default()); }
builder.add_to(acc); builder.detail(snip.description.as_deref().unwrap_or_default());
}); builder.add_to(acc);
},
);
None None
} }
@ -130,9 +132,10 @@ mod tests {
check_edit_with_config( check_edit_with_config(
CompletionConfig { CompletionConfig {
snippets: vec![Snippet::new( snippets: vec![Snippet::new(
"break".into(), &["break".into()],
&["ControlFlow::Break(())".into()],
&[], &[],
&["ControlFlow::Break(())".into()],
"",
&["core::ops::ControlFlow".into()], &["core::ops::ControlFlow".into()],
crate::SnippetScope::Expr, crate::SnippetScope::Expr,
) )

View File

@ -6,7 +6,7 @@
use ide_db::helpers::{insert_use::InsertUseConfig, SnippetCap}; use ide_db::helpers::{insert_use::InsertUseConfig, SnippetCap};
use crate::snippet::{PostfixSnippet, Snippet}; use crate::snippet::Snippet;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompletionConfig { pub struct CompletionConfig {
@ -17,6 +17,18 @@ pub struct CompletionConfig {
pub add_call_argument_snippets: bool, pub add_call_argument_snippets: bool,
pub snippet_cap: Option<SnippetCap>, pub snippet_cap: Option<SnippetCap>,
pub insert_use: InsertUseConfig, pub insert_use: InsertUseConfig,
pub postfix_snippets: Vec<PostfixSnippet>,
pub snippets: Vec<Snippet>, pub snippets: Vec<Snippet>,
} }
impl CompletionConfig {
pub fn postfix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
self.snippets.iter().flat_map(|snip| {
snip.postfix_triggers.iter().map(move |trigger| (trigger.as_str(), snip))
})
}
pub fn prefix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
self.snippets.iter().flat_map(|snip| {
snip.prefix_triggers.iter().map(move |trigger| (trigger.as_str(), snip))
})
}
}

View File

@ -29,7 +29,7 @@ use crate::{completions::Completions, context::CompletionContext, item::Completi
pub use crate::{ pub use crate::{
config::CompletionConfig, config::CompletionConfig,
item::{CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit}, item::{CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit},
snippet::{PostfixSnippet, PostfixSnippetScope, Snippet, SnippetScope}, snippet::{Snippet, SnippetScope},
}; };
//FIXME: split the following feature into fine-grained features. //FIXME: split the following feature into fine-grained features.

View File

@ -1,6 +1,57 @@
//! User (postfix)-snippet definitions. //! User (postfix)-snippet definitions.
//! //!
//! Actual logic is implemented in [`crate::completions::postfix`] and [`crate::completions::snippet`]. //! Actual logic is implemented in [`crate::completions::postfix`] and [`crate::completions::snippet`].
// Feature: User Snippet Completions
//
// rust-analyzer allows the user to define custom (postfix)-snippets that may depend on items to be accessible for the current scope to be applicable.
//
// A custom snippet can be defined by adding it to the `rust-analyzer.completion.snippets` object respectively.
//
// [source,json]
// ----
// {
// "rust-analyzer.completion.snippets": {
// "thread spawn": {
// "prefix": ["spawn", "tspawn"],
// "body": [
// "thread::spawn(move || {",
// "\t$0",
// ")};",
// ],
// "description": "Insert a thread::spawn call",
// "requires": "std::thread",
// "scope": "expr",
// }
// }
// }
// ----
//
// In the example above:
//
// * `"thread spawn"` is the name of the snippet.
//
// * `prefix` defines one or more trigger words that will trigger the snippets completion.
// Using `postfix` will instead create a postfix snippet.
//
// * `body` is one or more lines of content joined via newlines for the final output.
//
// * `description` is an optional description of the snippet, if unset the snippet name will be used.
//
// * `requires` is an optional list of item paths that have to be resolvable in the current crate where the completion is rendered.
// On failure of resolution the snippet won't be applicable, otherwise the snippet will insert an import for the items on insertion if
// the items aren't yet in scope.
//
// * `scope` is an optional filter for when the snippet should be applicable. Possible values are:
// ** for Snippet-Scopes: `expr`, `item` (default: `item`)
// ** for Postfix-Snippet-Scopes: `expr`, `type` (default: `expr`)
//
// The `body` field also has access to placeholders as visible in the example as `$0`.
// These placeholders take the form of `$number` or `${number:placeholder_text}` which can be traversed as tabstop in ascending order starting from 1,
// with `$0` being a special case that always comes last.
//
// There is also a special placeholder, `${receiver}`, which will be replaced by the receiver expression for postfix snippets, or nothing in case of normal snippets.
// It does not act as a tabstop.
use ide_db::helpers::{import_assets::LocatedImport, insert_use::ImportScope}; use ide_db::helpers::{import_assets::LocatedImport, insert_use::ImportScope};
use itertools::Itertools; use itertools::Itertools;
use syntax::ast; use syntax::ast;
@ -8,50 +59,40 @@ use syntax::ast;
use crate::{context::CompletionContext, ImportEdit}; use crate::{context::CompletionContext, ImportEdit};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum PostfixSnippetScope { pub enum SnippetScope {
Item,
Expr, Expr,
Type, Type,
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum SnippetScope { pub struct Snippet {
Item, pub postfix_triggers: Box<[String]>,
Expr, pub prefix_triggers: Box<[String]>,
} pub scope: SnippetScope,
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PostfixSnippet {
pub scope: PostfixSnippetScope,
pub label: String,
snippet: String, snippet: String,
pub description: Option<String>, pub description: Option<String>,
pub requires: Box<[String]>, pub requires: Box<[String]>,
} }
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Snippet {
pub scope: SnippetScope,
pub label: String,
pub snippet: String,
pub description: Option<String>,
pub requires: Box<[String]>,
}
impl Snippet { impl Snippet {
pub fn new( pub fn new(
label: String, prefix_triggers: &[String],
postfix_triggers: &[String],
snippet: &[String], snippet: &[String],
description: &[String], description: &str,
requires: &[String], requires: &[String],
scope: SnippetScope, scope: SnippetScope,
) -> Option<Self> { ) -> Option<Self> {
let (snippet, description) = validate_snippet(snippet, description, requires)?; let (snippet, description) = validate_snippet(snippet, description, requires)?;
Some(Snippet { Some(Snippet {
// Box::into doesn't work as that has a Copy bound 😒
postfix_triggers: postfix_triggers.iter().cloned().collect(),
prefix_triggers: prefix_triggers.iter().cloned().collect(),
scope, scope,
label,
snippet, snippet,
description, description,
requires: requires.iter().cloned().collect(), // Box::into doesn't work as that has a Copy bound 😒 requires: requires.iter().cloned().collect(),
}) })
} }
@ -64,6 +105,14 @@ impl Snippet {
import_edits(ctx, import_scope, &self.requires) import_edits(ctx, import_scope, &self.requires)
} }
pub fn snippet(&self) -> String {
self.snippet.replace("${receiver}", "")
}
pub fn postfix_snippet(&self, receiver: &str) -> String {
self.snippet.replace("${receiver}", receiver)
}
pub fn is_item(&self) -> bool { pub fn is_item(&self) -> bool {
self.scope == SnippetScope::Item self.scope == SnippetScope::Item
} }
@ -73,46 +122,6 @@ impl Snippet {
} }
} }
impl PostfixSnippet {
pub fn new(
label: String,
snippet: &[String],
description: &[String],
requires: &[String],
scope: PostfixSnippetScope,
) -> Option<Self> {
let (snippet, description) = validate_snippet(snippet, description, requires)?;
Some(PostfixSnippet {
scope,
label,
snippet,
description,
requires: requires.iter().cloned().collect(), // Box::into doesn't work as that has a Copy bound 😒
})
}
/// Returns None if the required items do not resolve.
pub(crate) fn imports(
&self,
ctx: &CompletionContext,
import_scope: &ImportScope,
) -> Option<Vec<ImportEdit>> {
import_edits(ctx, import_scope, &self.requires)
}
pub fn snippet(&self, receiver: &str) -> String {
self.snippet.replace("$receiver", receiver)
}
pub fn is_item(&self) -> bool {
self.scope == PostfixSnippetScope::Type
}
pub fn is_expr(&self) -> bool {
self.scope == PostfixSnippetScope::Expr
}
}
fn import_edits( fn import_edits(
ctx: &CompletionContext, ctx: &CompletionContext,
import_scope: &ImportScope, import_scope: &ImportScope,
@ -147,7 +156,7 @@ fn import_edits(
fn validate_snippet( fn validate_snippet(
snippet: &[String], snippet: &[String],
description: &[String], description: &str,
requires: &[String], requires: &[String],
) -> Option<(String, Option<String>)> { ) -> Option<(String, Option<String>)> {
// validate that these are indeed simple paths // validate that these are indeed simple paths
@ -162,7 +171,6 @@ fn validate_snippet(
return None; return None;
} }
let snippet = snippet.iter().join("\n"); let snippet = snippet.iter().join("\n");
let description = description.iter().join("\n"); let description = if description.is_empty() { None } else { Some(description.to_owned()) };
let description = if description.is_empty() { None } else { Some(description) };
Some((snippet, description)) Some((snippet, description))
} }

View File

@ -74,7 +74,6 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
group: true, group: true,
skip_glob_imports: true, skip_glob_imports: true,
}, },
postfix_snippets: Vec::new(),
snippets: Vec::new(), snippets: Vec::new(),
}; };

View File

@ -12,8 +12,7 @@ use std::{ffi::OsString, iter, path::PathBuf};
use flycheck::FlycheckConfig; use flycheck::FlycheckConfig;
use ide::{ use ide::{
AssistConfig, CompletionConfig, DiagnosticsConfig, HighlightRelatedConfig, HoverConfig, AssistConfig, CompletionConfig, DiagnosticsConfig, HighlightRelatedConfig, HoverConfig,
HoverDocFormat, InlayHintsConfig, JoinLinesConfig, PostfixSnippet, PostfixSnippetScope, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope,
Snippet, SnippetScope,
}; };
use ide_db::helpers::{ use ide_db::helpers::{
insert_use::{ImportGranularity, InsertUseConfig, PrefixKind}, insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@ -117,8 +116,6 @@ config_data! {
completion_snippets: FxHashMap<String, SnippetDef> = "{}", completion_snippets: FxHashMap<String, SnippetDef> = "{}",
/// Whether to show postfix snippets like `dbg`, `if`, `not`, etc. /// Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
completion_postfix_enable: bool = "true", completion_postfix_enable: bool = "true",
/// Custom postfix completion snippets.
completion_postfix_snippets: FxHashMap<String, PostfixSnippetDef> = "{}",
/// Toggles the additional completions that automatically add imports when completed. /// Toggles the additional completions that automatically add imports when completed.
/// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled. /// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.
completion_autoimport_enable: bool = "true", completion_autoimport_enable: bool = "true",
@ -301,7 +298,6 @@ pub struct Config {
detached_files: Vec<AbsPathBuf>, detached_files: Vec<AbsPathBuf>,
pub discovered_projects: Option<Vec<ProjectManifest>>, pub discovered_projects: Option<Vec<ProjectManifest>>,
pub root_path: AbsPathBuf, pub root_path: AbsPathBuf,
postfix_snippets: Vec<PostfixSnippet>,
snippets: Vec<Snippet>, snippets: Vec<Snippet>,
} }
@ -438,7 +434,6 @@ impl Config {
detached_files: Vec::new(), detached_files: Vec::new(),
discovered_projects: None, discovered_projects: None,
root_path, root_path,
postfix_snippets: Default::default(),
snippets: Default::default(), snippets: Default::default(),
} }
} }
@ -452,40 +447,28 @@ impl Config {
.map(AbsPathBuf::assert) .map(AbsPathBuf::assert)
.collect(); .collect();
self.data = ConfigData::from_json(json); self.data = ConfigData::from_json(json);
self.postfix_snippets = self self.snippets.clear();
.data for (name, def) in self.data.completion_snippets.iter() {
.completion_postfix_snippets if def.prefix.is_empty() && def.postfix.is_empty() {
.iter() continue;
.flat_map(|(label, desc)| { }
PostfixSnippet::new( let scope = match def.scope {
label.clone(), SnippetScopeDef::Expr => SnippetScope::Expr,
&desc.snippet, SnippetScopeDef::Type => SnippetScope::Type,
&desc.description, SnippetScopeDef::Item => SnippetScope::Item,
&desc.requires, };
match desc.scope { match Snippet::new(
PostfixSnippetScopeDef::Expr => PostfixSnippetScope::Expr, &def.prefix,
PostfixSnippetScopeDef::Type => PostfixSnippetScope::Type, &def.postfix,
}, &def.body,
) def.description.as_ref().unwrap_or(name),
}) &def.requires,
.collect(); scope,
self.snippets = self ) {
.data Some(snippet) => self.snippets.push(snippet),
.completion_snippets None => tracing::info!("Invalid snippet {}", name),
.iter() }
.flat_map(|(label, desc)| { }
Snippet::new(
label.clone(),
&desc.snippet,
&desc.description,
&desc.requires,
match desc.scope {
SnippetScopeDef::Expr => SnippetScope::Expr,
SnippetScopeDef::Item => SnippetScope::Item,
},
)
})
.collect();
} }
pub fn json_schema() -> serde_json::Value { pub fn json_schema() -> serde_json::Value {
@ -821,7 +804,6 @@ impl Config {
.snippet_support?, .snippet_support?,
false false
)), )),
postfix_snippets: self.postfix_snippets.clone(),
snippets: self.snippets.clone(), snippets: self.snippets.clone(),
} }
} }
@ -953,24 +935,12 @@ impl Config {
} }
} }
#[derive(Deserialize, Debug, Clone, Copy)]
#[serde(rename_all = "snake_case")]
enum PostfixSnippetScopeDef {
Expr,
Type,
}
impl Default for PostfixSnippetScopeDef {
fn default() -> Self {
PostfixSnippetScopeDef::Expr
}
}
#[derive(Deserialize, Debug, Clone, Copy)] #[derive(Deserialize, Debug, Clone, Copy)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
enum SnippetScopeDef { enum SnippetScopeDef {
Expr, Expr,
Item, Item,
Type,
} }
impl Default for SnippetScopeDef { impl Default for SnippetScopeDef {
@ -979,27 +949,18 @@ impl Default for SnippetScopeDef {
} }
} }
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone, Default)]
struct PostfixSnippetDef { #[serde(default)]
#[serde(deserialize_with = "single_or_array")]
description: Vec<String>,
#[serde(deserialize_with = "single_or_array")]
snippet: Vec<String>,
#[serde(deserialize_with = "single_or_array")]
requires: Vec<String>,
#[serde(default)]
scope: PostfixSnippetScopeDef,
}
#[derive(Deserialize, Debug, Clone)]
struct SnippetDef { struct SnippetDef {
#[serde(deserialize_with = "single_or_array")] #[serde(deserialize_with = "single_or_array")]
description: Vec<String>, prefix: Vec<String>,
#[serde(deserialize_with = "single_or_array")] #[serde(deserialize_with = "single_or_array")]
snippet: Vec<String>, postfix: Vec<String>,
description: Option<String>,
#[serde(deserialize_with = "single_or_array")]
body: Vec<String>,
#[serde(deserialize_with = "single_or_array")] #[serde(deserialize_with = "single_or_array")]
requires: Vec<String>, requires: Vec<String>,
#[serde(default)]
scope: SnippetScopeDef, scope: SnippetScopeDef,
} }
@ -1203,9 +1164,6 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
"items": { "type": "string" }, "items": { "type": "string" },
"uniqueItems": true, "uniqueItems": true,
}, },
"FxHashMap<String, PostfixSnippetDef>" => set! {
"type": "object",
},
"FxHashMap<String, SnippetDef>" => set! { "FxHashMap<String, SnippetDef>" => set! {
"type": "object", "type": "object",
}, },

View File

@ -144,7 +144,6 @@ fn integrated_completion_benchmark() {
group: true, group: true,
skip_glob_imports: true, skip_glob_imports: true,
}, },
postfix_snippets: Vec::new(),
snippets: Vec::new(), snippets: Vec::new(),
}; };
let position = let position =
@ -182,7 +181,6 @@ fn integrated_completion_benchmark() {
group: true, group: true,
skip_glob_imports: true, skip_glob_imports: true,
}, },
postfix_snippets: Vec::new(),
snippets: Vec::new(), snippets: Vec::new(),
}; };
let position = let position =