ide_completion/
snippet.rs

1//! User (postfix)-snippet definitions.
2//!
3//! Actual logic is implemented in [`crate::completions::postfix`] and [`crate::completions::snippet`] respectively.
4
5// Feature: User Snippet Completions
6//
7// 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.
8//
9// A custom snippet can be defined by adding it to the `rust-analyzer.completion.snippets.custom` object respectively.
10//
11// ```json
12// {
13//   "rust-analyzer.completion.snippets.custom": {
14//     "thread spawn": {
15//       "prefix": ["spawn", "tspawn"],
16//       "body": [
17//         "thread::spawn(move || {",
18//         "\t$0",
19//         "});",
20//       ],
21//       "description": "Insert a thread::spawn call",
22//       "requires": "std::thread",
23//       "scope": "expr",
24//     }
25//   }
26// }
27// ```
28//
29// In the example above:
30//
31// * `"thread spawn"` is the name of the snippet.
32//
33// * `prefix` defines one or more trigger words that will trigger the snippets completion.
34// Using `postfix` will instead create a postfix snippet.
35//
36// * `body` is one or more lines of content joined via newlines for the final output.
37//
38// * `description` is an optional description of the snippet, if unset the snippet name will be used.
39//
40// * `requires` is an optional list of item paths that have to be resolvable in the current crate where the completion is rendered.
41
42// On failure of resolution the snippet won't be applicable, otherwise the snippet will insert an import for the items on insertion if
43// the items aren't yet in scope.
44//
45// * `scope` is an optional filter for when the snippet should be applicable. Possible values are:
46// ** for Snippet-Scopes: `expr`, `item` (default: `item`)
47// ** for Postfix-Snippet-Scopes: `expr`, `type` (default: `expr`)
48//
49// The `body` field also has access to placeholders as visible in the example as `$0`.
50// These placeholders take the form of `$number` or `${number:placeholder_text}` which can be traversed as tabstop in ascending order starting from 1,
51// with `$0` being a special case that always comes last.
52//
53// 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.
54// This replacement for normal snippets allows you to reuse a snippet for both post- and prefix in a single definition.
55//
56// For the VSCode editor, rust-analyzer also ships with a small set of defaults which can be removed
57// by overwriting the settings object mentioned above, the defaults are:
58//
59// ```json
60// {
61//     "Arc::new": {
62//         "postfix": "arc",
63//         "body": "Arc::new(${receiver})",
64//         "requires": "std::sync::Arc",
65//         "description": "Put the expression into an `Arc`",
66//         "scope": "expr"
67//     },
68//     "Rc::new": {
69//         "postfix": "rc",
70//         "body": "Rc::new(${receiver})",
71//         "requires": "std::rc::Rc",
72//         "description": "Put the expression into an `Rc`",
73//         "scope": "expr"
74//     },
75//     "Box::pin": {
76//         "postfix": "pinbox",
77//         "body": "Box::pin(${receiver})",
78//         "requires": "std::boxed::Box",
79//         "description": "Put the expression into a pinned `Box`",
80//         "scope": "expr"
81//     },
82//     "Ok": {
83//         "postfix": "ok",
84//         "body": "Ok(${receiver})",
85//         "description": "Wrap the expression in a `Result::Ok`",
86//         "scope": "expr"
87//     },
88//     "Err": {
89//         "postfix": "err",
90//         "body": "Err(${receiver})",
91//         "description": "Wrap the expression in a `Result::Err`",
92//         "scope": "expr"
93//     },
94//     "Some": {
95//         "postfix": "some",
96//         "body": "Some(${receiver})",
97//         "description": "Wrap the expression in an `Option::Some`",
98//         "scope": "expr"
99//     }
100// }
101// ````
102
103use hir::{ModPath, Name, Symbol};
104use ide_db::imports::import_assets::LocatedImport;
105use itertools::Itertools;
106
107use crate::context::CompletionContext;
108
109/// A snippet scope describing where a snippet may apply to.
110/// These may differ slightly in meaning depending on the snippet trigger.
111#[derive(Clone, Debug, PartialEq, Eq)]
112pub enum SnippetScope {
113    Item,
114    Expr,
115    Type,
116}
117
118/// A user supplied snippet.
119#[derive(Clone, Debug, PartialEq, Eq)]
120pub struct Snippet {
121    pub postfix_triggers: Box<[Box<str>]>,
122    pub prefix_triggers: Box<[Box<str>]>,
123    pub scope: SnippetScope,
124    pub description: Option<Box<str>>,
125    snippet: String,
126    requires: Box<[ModPath]>,
127}
128
129impl Snippet {
130    pub fn new(
131        prefix_triggers: &[String],
132        postfix_triggers: &[String],
133        snippet: &[String],
134        description: &str,
135        requires: &[String],
136        scope: SnippetScope,
137    ) -> Option<Self> {
138        if prefix_triggers.is_empty() && postfix_triggers.is_empty() {
139            return None;
140        }
141        let (requires, snippet, description) = validate_snippet(snippet, description, requires)?;
142        Some(Snippet {
143            postfix_triggers: postfix_triggers.iter().map(String::as_str).map(Into::into).collect(),
144            prefix_triggers: prefix_triggers.iter().map(String::as_str).map(Into::into).collect(),
145            scope,
146            snippet,
147            description,
148            requires,
149        })
150    }
151
152    /// Returns [`None`] if the required items do not resolve.
153    pub(crate) fn imports(&self, ctx: &CompletionContext<'_>) -> Option<Vec<LocatedImport>> {
154        import_edits(ctx, &self.requires)
155    }
156
157    pub fn snippet(&self) -> String {
158        self.snippet.replace("${receiver}", "$0")
159    }
160
161    pub fn postfix_snippet(&self, receiver: &str) -> String {
162        self.snippet.replace("${receiver}", receiver)
163    }
164}
165
166fn import_edits(ctx: &CompletionContext<'_>, requires: &[ModPath]) -> Option<Vec<LocatedImport>> {
167    let import_cfg = ctx.config.find_path_config(ctx.is_nightly);
168
169    let resolve = |import| {
170        let item = ctx.scope.resolve_mod_path(import).next()?;
171        let path = ctx.module.find_use_path(
172            ctx.db,
173            item,
174            ctx.config.insert_use.prefix_kind,
175            import_cfg,
176        )?;
177        Some((path.len() > 1).then(|| LocatedImport::new_no_completion(path.clone(), item, item)))
178    };
179    let mut res = Vec::with_capacity(requires.len());
180    for import in requires {
181        match resolve(import) {
182            Some(first) => res.extend(first),
183            None => return None,
184        }
185    }
186    Some(res)
187}
188
189fn validate_snippet(
190    snippet: &[String],
191    description: &str,
192    requires: &[String],
193) -> Option<(Box<[ModPath]>, String, Option<Box<str>>)> {
194    let mut imports = Vec::with_capacity(requires.len());
195    for path in requires.iter() {
196        let use_path = ModPath::from_segments(
197            hir::PathKind::Plain,
198            path.split("::").map(Symbol::intern).map(Name::new_symbol_root),
199        );
200        imports.push(use_path);
201    }
202    let snippet = snippet.iter().join("\n");
203    let description = (!description.is_empty())
204        .then(|| description.split_once('\n').map_or(description, |(it, _)| it))
205        .map(ToOwned::to_owned)
206        .map(Into::into);
207    Some((imports.into_boxed_slice(), snippet, description))
208}