]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/snippet.rs
Merge #11391
[rust.git] / crates / ide_completion / src / snippet.rs
1 //! User (postfix)-snippet definitions.
2 //!
3 //! Actual logic is implemented in [`crate::completions::postfix`] and [`crate::completions::snippet`] respectively.
4
5 use std::ops::Deref;
6
7 // Feature: User Snippet Completions
8 //
9 // 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.
10 //
11 // A custom snippet can be defined by adding it to the `rust-analyzer.completion.snippets` object respectively.
12 //
13 // [source,json]
14 // ----
15 // {
16 //   "rust-analyzer.completion.snippets": {
17 //     "thread spawn": {
18 //       "prefix": ["spawn", "tspawn"],
19 //       "body": [
20 //         "thread::spawn(move || {",
21 //         "\t$0",
22 //         ")};",
23 //       ],
24 //       "description": "Insert a thread::spawn call",
25 //       "requires": "std::thread",
26 //       "scope": "expr",
27 //     }
28 //   }
29 // }
30 // ----
31 //
32 // In the example above:
33 //
34 // * `"thread spawn"` is the name of the snippet.
35 //
36 // * `prefix` defines one or more trigger words that will trigger the snippets completion.
37 // Using `postfix` will instead create a postfix snippet.
38 //
39 // * `body` is one or more lines of content joined via newlines for the final output.
40 //
41 // * `description` is an optional description of the snippet, if unset the snippet name will be used.
42 //
43 // * `requires` is an optional list of item paths that have to be resolvable in the current crate where the completion is rendered.
44 // On failure of resolution the snippet won't be applicable, otherwise the snippet will insert an import for the items on insertion if
45 // the items aren't yet in scope.
46 //
47 // * `scope` is an optional filter for when the snippet should be applicable. Possible values are:
48 // ** for Snippet-Scopes: `expr`, `item` (default: `item`)
49 // ** for Postfix-Snippet-Scopes: `expr`, `type` (default: `expr`)
50 //
51 // The `body` field also has access to placeholders as visible in the example as `$0`.
52 // These placeholders take the form of `$number` or `${number:placeholder_text}` which can be traversed as tabstop in ascending order starting from 1,
53 // with `$0` being a special case that always comes last.
54 //
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
105 use ide_db::helpers::{import_assets::LocatedImport, insert_use::ImportScope};
106 use itertools::Itertools;
107 use syntax::{ast, AstNode, GreenNode, SyntaxNode};
108
109 use crate::{context::CompletionContext, ImportEdit};
110
111 /// A snippet scope describing where a snippet may apply to.
112 /// These may differ slightly in meaning depending on the snippet trigger.
113 #[derive(Clone, Debug, PartialEq, Eq)]
114 pub enum SnippetScope {
115     Item,
116     Expr,
117     Type,
118 }
119
120 /// A user supplied snippet.
121 #[derive(Clone, Debug, PartialEq, Eq)]
122 pub struct Snippet {
123     pub postfix_triggers: Box<[Box<str>]>,
124     pub prefix_triggers: Box<[Box<str>]>,
125     pub scope: SnippetScope,
126     pub description: Option<Box<str>>,
127     snippet: String,
128     // These are `ast::Path`'s but due to SyntaxNodes not being Send we store these
129     // and reconstruct them on demand instead. This is cheaper than reparsing them
130     // from strings
131     requires: Box<[GreenNode]>,
132 }
133
134 impl Snippet {
135     pub fn new(
136         prefix_triggers: &[String],
137         postfix_triggers: &[String],
138         snippet: &[String],
139         description: &str,
140         requires: &[String],
141         scope: SnippetScope,
142     ) -> Option<Self> {
143         if prefix_triggers.is_empty() && postfix_triggers.is_empty() {
144             return None;
145         }
146         let (requires, snippet, description) = validate_snippet(snippet, description, requires)?;
147         Some(Snippet {
148             // Box::into doesn't work as that has a Copy bound ðŸ˜’
149             postfix_triggers: postfix_triggers.iter().map(Deref::deref).map(Into::into).collect(),
150             prefix_triggers: prefix_triggers.iter().map(Deref::deref).map(Into::into).collect(),
151             scope,
152             snippet,
153             description,
154             requires,
155         })
156     }
157
158     /// Returns [`None`] if the required items do not resolve.
159     pub(crate) fn imports(
160         &self,
161         ctx: &CompletionContext,
162         import_scope: &ImportScope,
163     ) -> Option<Vec<ImportEdit>> {
164         import_edits(ctx, import_scope, &self.requires)
165     }
166
167     pub fn snippet(&self) -> String {
168         self.snippet.replace("${receiver}", "$0")
169     }
170
171     pub fn postfix_snippet(&self, receiver: &str) -> String {
172         self.snippet.replace("${receiver}", receiver)
173     }
174 }
175
176 fn import_edits(
177     ctx: &CompletionContext,
178     import_scope: &ImportScope,
179     requires: &[GreenNode],
180 ) -> Option<Vec<ImportEdit>> {
181     let resolve = |import: &GreenNode| {
182         let path = ast::Path::cast(SyntaxNode::new_root(import.clone()))?;
183         let item = match ctx.scope.speculative_resolve(&path)? {
184             hir::PathResolution::Macro(mac) => mac.into(),
185             hir::PathResolution::Def(def) => def.into(),
186             _ => return None,
187         };
188         let path = ctx.scope.module()?.find_use_path_prefixed(
189             ctx.db,
190             item,
191             ctx.config.insert_use.prefix_kind,
192         )?;
193         Some((path.len() > 1).then(|| ImportEdit {
194             import: LocatedImport::new(path.clone(), item, item, None),
195             scope: import_scope.clone(),
196         }))
197     };
198     let mut res = Vec::with_capacity(requires.len());
199     for import in requires {
200         match resolve(import) {
201             Some(first) => res.extend(first),
202             None => return None,
203         }
204     }
205     Some(res)
206 }
207
208 fn validate_snippet(
209     snippet: &[String],
210     description: &str,
211     requires: &[String],
212 ) -> Option<(Box<[GreenNode]>, String, Option<Box<str>>)> {
213     let mut imports = Vec::with_capacity(requires.len());
214     for path in requires.iter() {
215         let use_path = ast::SourceFile::parse(&format!("use {};", path))
216             .syntax_node()
217             .descendants()
218             .find_map(ast::Path::cast)?;
219         if use_path.syntax().text() != path.as_str() {
220             return None;
221         }
222         let green = use_path.syntax().green().into_owned();
223         imports.push(green);
224     }
225     let snippet = snippet.iter().join("\n");
226     let description = (!description.is_empty())
227         .then(|| description.split_once('\n').map_or(description, |(it, _)| it))
228         .map(ToOwned::to_owned)
229         .map(Into::into);
230     Some((imports.into_boxed_slice(), snippet, description))
231 }