Skip to content

Commit 9fe0f0d

Browse files
committed
Add a few default snippets for VSCode
1 parent f79f3db commit 9fe0f0d

File tree

5 files changed

+175
-9
lines changed

5 files changed

+175
-9
lines changed

crates/ide_completion/src/completions/postfix.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
163163
}
164164

165165
postfix_snippet("box", "Box::new(expr)", &format!("Box::new({})", receiver_text)).add_to(acc);
166-
postfix_snippet("ok", "Ok(expr)", &format!("Ok({})", receiver_text)).add_to(acc);
167-
postfix_snippet("err", "Err(expr)", &format!("Err({})", receiver_text)).add_to(acc);
168-
postfix_snippet("some", "Some(expr)", &format!("Some({})", receiver_text)).add_to(acc);
169166
postfix_snippet("dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
170167
postfix_snippet("dbgr", "dbg!(&expr)", &format!("dbg!(&{})", receiver_text)).add_to(acc);
171168
postfix_snippet("call", "function(expr)", &format!("${{1}}({})", receiver_text)).add_to(acc);

crates/ide_completion/src/snippet.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,56 @@ use std::ops::Deref;
5252
// These placeholders take the form of `$number` or `${number:placeholder_text}` which can be traversed as tabstop in ascending order starting from 1,
5353
// with `$0` being a special case that always comes last.
5454
//
55-
// 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.
56-
// It does not act as a tabstop.
55+
// 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.
56+
// This replacement for normal snippets allows you to reuse a snippet for both post- and prefix in a single definition.
57+
//
58+
// For the VSCode editor, rust-analyzer also ships with a small set of defaults which can be removed
59+
// by overwriting the settings object mentioned above, the defaults are:
60+
// [source,json]
61+
// ----
62+
// {
63+
// "Arc::new": {
64+
// "postfix": "arc",
65+
// "body": "Arc::new(${receiver})",
66+
// "requires": "std::sync::Arc",
67+
// "description": "Put the expression into an `Arc`",
68+
// "scope": "expr"
69+
// },
70+
// "Rc::new": {
71+
// "postfix": "rc",
72+
// "body": "Rc::new(${receiver})",
73+
// "requires": "std::rc::Rc",
74+
// "description": "Put the expression into an `Rc`",
75+
// "scope": "expr"
76+
// },
77+
// "Box::pin": {
78+
// "postfix": "pinbox",
79+
// "body": "Box::pin(${receiver})",
80+
// "requires": "std::boxed::Box",
81+
// "description": "Put the expression into a pinned `Box`",
82+
// "scope": "expr"
83+
// },
84+
// "Ok": {
85+
// "postfix": "ok",
86+
// "body": "Ok(${receiver})",
87+
// "description": "Wrap the expression in a `Result::Ok`",
88+
// "scope": "expr"
89+
// },
90+
// "Err": {
91+
// "postfix": "err",
92+
// "body": "Err(${receiver})",
93+
// "description": "Wrap the expression in a `Result::Err`",
94+
// "scope": "expr"
95+
// },
96+
// "Some": {
97+
// "postfix": "some",
98+
// "body": "Some(${receiver})",
99+
// "description": "Wrap the expression in an `Option::Some`",
100+
// "scope": "expr"
101+
// }
102+
// }
103+
// ----
104+
57105
use ide_db::helpers::{import_assets::LocatedImport, insert_use::ImportScope};
58106
use itertools::Itertools;
59107
use syntax::{ast, AstNode, GreenNode, SyntaxNode};
@@ -117,7 +165,7 @@ impl Snippet {
117165
}
118166

119167
pub fn snippet(&self) -> String {
120-
self.snippet.replace("${receiver}", "")
168+
self.snippet.replace("${receiver}", "$0")
121169
}
122170

123171
pub fn postfix_snippet(&self, receiver: &str) -> String {

crates/rust-analyzer/src/config.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,48 @@ config_data! {
116116
/// Whether to add parenthesis when completing functions.
117117
completion_addCallParenthesis: bool = "true",
118118
/// Custom completion snippets.
119-
completion_snippets: FxHashMap<String, SnippetDef> = "{}",
119+
// NOTE: Keep this list in sync with the feature docs of user snippets.
120+
completion_snippets: FxHashMap<String, SnippetDef> = r#"{
121+
"Arc::new": {
122+
"postfix": "arc",
123+
"body": "Arc::new(${receiver})",
124+
"requires": "std::sync::Arc",
125+
"description": "Put the expression into an `Arc`",
126+
"scope": "expr"
127+
},
128+
"Rc::new": {
129+
"postfix": "rc",
130+
"body": "Rc::new(${receiver})",
131+
"requires": "std::rc::Rc",
132+
"description": "Put the expression into an `Rc`",
133+
"scope": "expr"
134+
},
135+
"Box::pin": {
136+
"postfix": "pinbox",
137+
"body": "Box::pin(${receiver})",
138+
"requires": "std::boxed::Box",
139+
"description": "Put the expression into a pinned `Box`",
140+
"scope": "expr"
141+
},
142+
"Ok": {
143+
"postfix": "ok",
144+
"body": "Ok(${receiver})",
145+
"description": "Wrap the expression in a `Result::Ok`",
146+
"scope": "expr"
147+
},
148+
"Err": {
149+
"postfix": "err",
150+
"body": "Err(${receiver})",
151+
"description": "Wrap the expression in a `Result::Err`",
152+
"scope": "expr"
153+
},
154+
"Some": {
155+
"postfix": "some",
156+
"body": "Some(${receiver})",
157+
"description": "Wrap the expression in an `Option::Some`",
158+
"scope": "expr"
159+
}
160+
}"#,
120161
/// Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
121162
completion_postfix_enable: bool = "true",
122163
/// Toggles the additional completions that automatically add imports when completed.

docs/user/generated_config.adoc

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,47 @@ Only applies when `#rust-analyzer.completion.addCallParenthesis#` is set.
141141
--
142142
Whether to add parenthesis when completing functions.
143143
--
144-
[[rust-analyzer.completion.snippets]]rust-analyzer.completion.snippets (default: `{}`)::
144+
[[rust-analyzer.completion.snippets]]rust-analyzer.completion.snippets (default: `{
145+
"Arc::new": {
146+
"postfix": "arc",
147+
"body": "Arc::new(${receiver})",
148+
"requires": "std::sync::Arc",
149+
"description": "Put the expression into an `Arc`",
150+
"scope": "expr"
151+
},
152+
"Rc::new": {
153+
"postfix": "rc",
154+
"body": "Rc::new(${receiver})",
155+
"requires": "std::rc::Rc",
156+
"description": "Put the expression into an `Rc`",
157+
"scope": "expr"
158+
},
159+
"Box::pin": {
160+
"postfix": "pinbox",
161+
"body": "Box::pin(${receiver})",
162+
"requires": "std::boxed::Box",
163+
"description": "Put the expression into a pinned `Box`",
164+
"scope": "expr"
165+
},
166+
"Ok": {
167+
"postfix": "ok",
168+
"body": "Ok(${receiver})",
169+
"description": "Wrap the expression in a `Result::Ok`",
170+
"scope": "expr"
171+
},
172+
"Err": {
173+
"postfix": "err",
174+
"body": "Err(${receiver})",
175+
"description": "Wrap the expression in a `Result::Err`",
176+
"scope": "expr"
177+
},
178+
"Some": {
179+
"postfix": "some",
180+
"body": "Some(${receiver})",
181+
"description": "Wrap the expression in an `Option::Some`",
182+
"scope": "expr"
183+
}
184+
}`)::
145185
+
146186
--
147187
Custom completion snippets.

editors/code/package.json

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,47 @@
592592
},
593593
"rust-analyzer.completion.snippets": {
594594
"markdownDescription": "Custom completion snippets.",
595-
"default": {},
595+
"default": {
596+
"Arc::new": {
597+
"postfix": "arc",
598+
"body": "Arc::new(${receiver})",
599+
"requires": "std::sync::Arc",
600+
"description": "Put the expression into an `Arc`",
601+
"scope": "expr"
602+
},
603+
"Rc::new": {
604+
"postfix": "rc",
605+
"body": "Rc::new(${receiver})",
606+
"requires": "std::rc::Rc",
607+
"description": "Put the expression into an `Rc`",
608+
"scope": "expr"
609+
},
610+
"Box::pin": {
611+
"postfix": "pinbox",
612+
"body": "Box::pin(${receiver})",
613+
"requires": "std::boxed::Box",
614+
"description": "Put the expression into a pinned `Box`",
615+
"scope": "expr"
616+
},
617+
"Ok": {
618+
"postfix": "ok",
619+
"body": "Ok(${receiver})",
620+
"description": "Wrap the expression in a `Result::Ok`",
621+
"scope": "expr"
622+
},
623+
"Err": {
624+
"postfix": "err",
625+
"body": "Err(${receiver})",
626+
"description": "Wrap the expression in a `Result::Err`",
627+
"scope": "expr"
628+
},
629+
"Some": {
630+
"postfix": "some",
631+
"body": "Some(${receiver})",
632+
"description": "Wrap the expression in an `Option::Some`",
633+
"scope": "expr"
634+
}
635+
},
596636
"type": "object"
597637
},
598638
"rust-analyzer.completion.postfix.enable": {

0 commit comments

Comments
 (0)