10527: internal: Remove a few snippet completions, replace them with user snippets definitions in VSCode r=Veykril a=Veykril

Closes https://github.com/rust-analyzer/rust-analyzer/issues/9636
cc https://github.com/rust-analyzer/rust-analyzer/issues/7033#issuecomment-939959905

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-12-17 15:44:48 +00:00 committed by GitHub
commit 04b0b19cdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 175 additions and 42 deletions

View File

@ -229,9 +229,6 @@ fn foo(a: A) { a.$0 }
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -255,9 +252,6 @@ fn foo() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)

View File

@ -163,9 +163,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
}
postfix_snippet("box", "Box::new(expr)", &format!("Box::new({})", receiver_text)).add_to(acc);
postfix_snippet("ok", "Ok(expr)", &format!("Ok({})", receiver_text)).add_to(acc);
postfix_snippet("err", "Err(expr)", &format!("Err({})", receiver_text)).add_to(acc);
postfix_snippet("some", "Some(expr)", &format!("Some({})", receiver_text)).add_to(acc);
postfix_snippet("dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
postfix_snippet("dbgr", "dbg!(&expr)", &format!("dbg!(&{})", receiver_text)).add_to(acc);
postfix_snippet("call", "function(expr)", &format!("${{1}}({})", receiver_text)).add_to(acc);
@ -295,9 +292,6 @@ fn main() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -328,9 +322,6 @@ fn main() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -352,9 +343,6 @@ fn main() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -381,9 +369,6 @@ fn main() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)

View File

@ -1437,9 +1437,6 @@ fn main() {
sn refm []
sn match []
sn box []
sn ok []
sn err []
sn some []
sn dbg []
sn dbgr []
sn call []

View File

@ -52,8 +52,56 @@ use std::ops::Deref;
// 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.
// There is also a special placeholder, `${receiver}`, which will be replaced by the receiver expression for postfix snippets, or a `$0` tabstop in case of normal snippets.
// This replacement for normal snippets allows you to reuse a snippet for both post- and prefix in a single definition.
//
// For the VSCode editor, rust-analyzer also ships with a small set of defaults which can be removed
// by overwriting the settings object mentioned above, the defaults are:
// [source,json]
// ----
// {
// "Arc::new": {
// "postfix": "arc",
// "body": "Arc::new(${receiver})",
// "requires": "std::sync::Arc",
// "description": "Put the expression into an `Arc`",
// "scope": "expr"
// },
// "Rc::new": {
// "postfix": "rc",
// "body": "Rc::new(${receiver})",
// "requires": "std::rc::Rc",
// "description": "Put the expression into an `Rc`",
// "scope": "expr"
// },
// "Box::pin": {
// "postfix": "pinbox",
// "body": "Box::pin(${receiver})",
// "requires": "std::boxed::Box",
// "description": "Put the expression into a pinned `Box`",
// "scope": "expr"
// },
// "Ok": {
// "postfix": "ok",
// "body": "Ok(${receiver})",
// "description": "Wrap the expression in a `Result::Ok`",
// "scope": "expr"
// },
// "Err": {
// "postfix": "err",
// "body": "Err(${receiver})",
// "description": "Wrap the expression in a `Result::Err`",
// "scope": "expr"
// },
// "Some": {
// "postfix": "some",
// "body": "Some(${receiver})",
// "description": "Wrap the expression in an `Option::Some`",
// "scope": "expr"
// }
// }
// ----
use ide_db::helpers::{import_assets::LocatedImport, insert_use::ImportScope};
use itertools::Itertools;
use syntax::{ast, AstNode, GreenNode, SyntaxNode};
@ -117,7 +165,7 @@ impl Snippet {
}
pub fn snippet(&self) -> String {
self.snippet.replace("${receiver}", "")
self.snippet.replace("${receiver}", "$0")
}
pub fn postfix_snippet(&self, receiver: &str) -> String {

View File

@ -29,9 +29,6 @@ fn main() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -62,9 +59,6 @@ fn main() {
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -97,9 +91,6 @@ fn main() {}
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)
@ -132,9 +123,6 @@ fn main() {}
sn refm &mut expr
sn match match expr {}
sn box Box::new(expr)
sn ok Ok(expr)
sn err Err(expr)
sn some Some(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn call function(expr)

View File

@ -116,7 +116,48 @@ config_data! {
/// Whether to add parenthesis when completing functions.
completion_addCallParenthesis: bool = "true",
/// Custom completion snippets.
completion_snippets: FxHashMap<String, SnippetDef> = "{}",
// NOTE: Keep this list in sync with the feature docs of user snippets.
completion_snippets: FxHashMap<String, SnippetDef> = r#"{
"Arc::new": {
"postfix": "arc",
"body": "Arc::new(${receiver})",
"requires": "std::sync::Arc",
"description": "Put the expression into an `Arc`",
"scope": "expr"
},
"Rc::new": {
"postfix": "rc",
"body": "Rc::new(${receiver})",
"requires": "std::rc::Rc",
"description": "Put the expression into an `Rc`",
"scope": "expr"
},
"Box::pin": {
"postfix": "pinbox",
"body": "Box::pin(${receiver})",
"requires": "std::boxed::Box",
"description": "Put the expression into a pinned `Box`",
"scope": "expr"
},
"Ok": {
"postfix": "ok",
"body": "Ok(${receiver})",
"description": "Wrap the expression in a `Result::Ok`",
"scope": "expr"
},
"Err": {
"postfix": "err",
"body": "Err(${receiver})",
"description": "Wrap the expression in a `Result::Err`",
"scope": "expr"
},
"Some": {
"postfix": "some",
"body": "Some(${receiver})",
"description": "Wrap the expression in an `Option::Some`",
"scope": "expr"
}
}"#,
/// Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
completion_postfix_enable: bool = "true",
/// Toggles the additional completions that automatically add imports when completed.

View File

@ -141,7 +141,47 @@ Only applies when `#rust-analyzer.completion.addCallParenthesis#` is set.
--
Whether to add parenthesis when completing functions.
--
[[rust-analyzer.completion.snippets]]rust-analyzer.completion.snippets (default: `{}`)::
[[rust-analyzer.completion.snippets]]rust-analyzer.completion.snippets (default: `{
"Arc::new": {
"postfix": "arc",
"body": "Arc::new(${receiver})",
"requires": "std::sync::Arc",
"description": "Put the expression into an `Arc`",
"scope": "expr"
},
"Rc::new": {
"postfix": "rc",
"body": "Rc::new(${receiver})",
"requires": "std::rc::Rc",
"description": "Put the expression into an `Rc`",
"scope": "expr"
},
"Box::pin": {
"postfix": "pinbox",
"body": "Box::pin(${receiver})",
"requires": "std::boxed::Box",
"description": "Put the expression into a pinned `Box`",
"scope": "expr"
},
"Ok": {
"postfix": "ok",
"body": "Ok(${receiver})",
"description": "Wrap the expression in a `Result::Ok`",
"scope": "expr"
},
"Err": {
"postfix": "err",
"body": "Err(${receiver})",
"description": "Wrap the expression in a `Result::Err`",
"scope": "expr"
},
"Some": {
"postfix": "some",
"body": "Some(${receiver})",
"description": "Wrap the expression in an `Option::Some`",
"scope": "expr"
}
}`)::
+
--
Custom completion snippets.

View File

@ -592,7 +592,47 @@
},
"rust-analyzer.completion.snippets": {
"markdownDescription": "Custom completion snippets.",
"default": {},
"default": {
"Arc::new": {
"postfix": "arc",
"body": "Arc::new(${receiver})",
"requires": "std::sync::Arc",
"description": "Put the expression into an `Arc`",
"scope": "expr"
},
"Rc::new": {
"postfix": "rc",
"body": "Rc::new(${receiver})",
"requires": "std::rc::Rc",
"description": "Put the expression into an `Rc`",
"scope": "expr"
},
"Box::pin": {
"postfix": "pinbox",
"body": "Box::pin(${receiver})",
"requires": "std::boxed::Box",
"description": "Put the expression into a pinned `Box`",
"scope": "expr"
},
"Ok": {
"postfix": "ok",
"body": "Ok(${receiver})",
"description": "Wrap the expression in a `Result::Ok`",
"scope": "expr"
},
"Err": {
"postfix": "err",
"body": "Err(${receiver})",
"description": "Wrap the expression in a `Result::Err`",
"scope": "expr"
},
"Some": {
"postfix": "some",
"body": "Some(${receiver})",
"description": "Wrap the expression in an `Option::Some`",
"scope": "expr"
}
},
"type": "object"
},
"rust-analyzer.completion.postfix.enable": {