1use anyhow::{Context as _, Result, anyhow};
2use smallvec::SmallVec;
3use std::{collections::BTreeMap, ops::Range};
4
5#[derive(Clone, Debug, Default, PartialEq)]
6pub struct Snippet {
7 pub text: String,
8 pub tabstops: Vec<TabStop>,
9}
10
11#[derive(Clone, Debug, Default, PartialEq)]
12pub struct TabStop {
13 pub ranges: SmallVec<[Range<isize>; 2]>,
14 pub choices: Option<Vec<String>>,
15}
16
17impl Snippet {
18 pub fn parse(source: &str) -> Result<Self> {
19 let mut text = String::with_capacity(source.len());
20 let mut tabstops = BTreeMap::new();
21 parse_snippet(source, false, &mut text, &mut tabstops)
22 .context("failed to parse snippet")?;
23
24 let len = text.len() as isize;
25 let final_tabstop = tabstops.remove(&0);
26 let mut tabstops = tabstops.into_values().collect::<Vec<_>>();
27
28 if let Some(final_tabstop) = final_tabstop {
29 tabstops.push(final_tabstop);
30 } else {
31 let end_tabstop = TabStop {
32 ranges: [len..len].into_iter().collect(),
33 choices: None,
34 };
35
36 if !tabstops.last().map_or(false, |t| *t == end_tabstop) {
37 tabstops.push(end_tabstop);
38 }
39 }
40
41 Ok(Snippet { text, tabstops })
42 }
43}
44
45fn parse_snippet<'a>(
46 mut source: &'a str,
47 nested: bool,
48 text: &mut String,
49 tabstops: &mut BTreeMap<usize, TabStop>,
50) -> Result<&'a str> {
51 loop {
52 match source.chars().next() {
53 None => return Ok(""),
54 Some('$') => {
55 source = parse_tabstop(&source[1..], text, tabstops)?;
56 }
57 Some('\\') => {
58 // As specified in the LSP spec (`Grammar` section),
59 // backslashes can escape some characters:
60 // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#snippet_syntax
61 source = &source[1..];
62 if let Some(c) = source.chars().next() {
63 if c == '$' || c == '\\' || c == '}' {
64 text.push(c);
65 // All escapable characters are 1 byte long:
66 source = &source[1..];
67 } else {
68 text.push('\\');
69 }
70 } else {
71 text.push('\\');
72 }
73 }
74 Some('}') => {
75 if nested {
76 return Ok(source);
77 } else {
78 text.push('}');
79 source = &source[1..];
80 }
81 }
82 Some(_) => {
83 let chunk_end = source.find(['}', '$', '\\']).unwrap_or(source.len());
84 let (chunk, rest) = source.split_at(chunk_end);
85 text.push_str(chunk);
86 source = rest;
87 }
88 }
89 }
90}
91
92fn parse_tabstop<'a>(
93 mut source: &'a str,
94 text: &mut String,
95 tabstops: &mut BTreeMap<usize, TabStop>,
96) -> Result<&'a str> {
97 let tabstop_start = text.len();
98 let tabstop_index;
99 let mut choices = None;
100
101 if source.starts_with('{') {
102 let (index, rest) = parse_int(&source[1..])?;
103 tabstop_index = index;
104 source = rest;
105
106 if source.starts_with("|") {
107 (source, choices) = parse_choices(&source[1..], text)?;
108 }
109
110 if source.starts_with(':') {
111 source = parse_snippet(&source[1..], true, text, tabstops)?;
112 }
113
114 if source.starts_with('}') {
115 source = &source[1..];
116 } else {
117 return Err(anyhow!("expected a closing brace"));
118 }
119 } else {
120 let (index, rest) = parse_int(source)?;
121 tabstop_index = index;
122 source = rest;
123 }
124
125 tabstops
126 .entry(tabstop_index)
127 .or_insert_with(|| TabStop {
128 ranges: Default::default(),
129 choices,
130 })
131 .ranges
132 .push(tabstop_start as isize..text.len() as isize);
133 Ok(source)
134}
135
136fn parse_int(source: &str) -> Result<(usize, &str)> {
137 let len = source
138 .find(|c: char| !c.is_ascii_digit())
139 .unwrap_or(source.len());
140 if len == 0 {
141 return Err(anyhow!("expected an integer"));
142 }
143 let (prefix, suffix) = source.split_at(len);
144 Ok((prefix.parse()?, suffix))
145}
146
147fn parse_choices<'a>(
148 mut source: &'a str,
149 text: &mut String,
150) -> Result<(&'a str, Option<Vec<String>>)> {
151 let mut found_default_choice = false;
152 let mut current_choice = String::new();
153 let mut choices = Vec::new();
154
155 loop {
156 match source.chars().next() {
157 None => return Ok(("", Some(choices))),
158 Some('\\') => {
159 source = &source[1..];
160
161 if let Some(c) = source.chars().next() {
162 if !found_default_choice {
163 current_choice.push(c);
164 text.push(c);
165 }
166 source = &source[c.len_utf8()..];
167 }
168 }
169 Some(',') => {
170 found_default_choice = true;
171 source = &source[1..];
172 choices.push(current_choice);
173 current_choice = String::new();
174 }
175 Some('|') => {
176 source = &source[1..];
177 choices.push(current_choice);
178 return Ok((source, Some(choices)));
179 }
180 Some(_) => {
181 let chunk_end = source.find([',', '|', '\\']);
182
183 if chunk_end.is_none() {
184 return Err(anyhow!(
185 "Placeholder choice doesn't contain closing pipe-character '|'"
186 ));
187 }
188
189 let (chunk, rest) = source.split_at(chunk_end.unwrap());
190
191 if !found_default_choice {
192 text.push_str(chunk);
193 }
194
195 current_choice.push_str(chunk);
196 source = rest;
197 }
198 }
199 }
200}
201
202#[cfg(test)]
203mod tests {
204 use super::*;
205
206 #[test]
207 fn test_snippet_without_tabstops() {
208 let snippet = Snippet::parse("one-two-three").unwrap();
209 assert_eq!(snippet.text, "one-two-three");
210 assert_eq!(tabstops(&snippet), &[vec![13..13]]);
211 }
212
213 #[test]
214 fn test_snippet_with_tabstops() {
215 let snippet = Snippet::parse("one$1two").unwrap();
216 assert_eq!(snippet.text, "onetwo");
217 assert_eq!(tabstops(&snippet), &[vec![3..3], vec![6..6]]);
218 assert_eq!(tabstop_choices(&snippet), &[&None, &None]);
219
220 // Multi-digit numbers
221 let snippet = Snippet::parse("one$123-$99-two").unwrap();
222 assert_eq!(snippet.text, "one--two");
223 assert_eq!(tabstops(&snippet), &[vec![4..4], vec![3..3], vec![8..8]]);
224 assert_eq!(tabstop_choices(&snippet), &[&None, &None, &None]);
225 }
226
227 #[test]
228 fn test_snippet_with_last_tabstop_at_end() {
229 let snippet = Snippet::parse(r#"foo.$1"#).unwrap();
230
231 // If the final tabstop is already at the end of the text, don't insert
232 // an additional tabstop at the end.
233 assert_eq!(snippet.text, r#"foo."#);
234 assert_eq!(tabstops(&snippet), &[vec![4..4]]);
235 assert_eq!(tabstop_choices(&snippet), &[&None]);
236 }
237
238 #[test]
239 fn test_snippet_with_explicit_final_tabstop() {
240 let snippet = Snippet::parse(r#"<div class="$1">$0</div>"#).unwrap();
241
242 // If the final tabstop is explicitly specified via '$0', then
243 // don't insert an additional tabstop at the end.
244 assert_eq!(snippet.text, r#"<div class=""></div>"#);
245 assert_eq!(tabstops(&snippet), &[vec![12..12], vec![14..14]]);
246 assert_eq!(tabstop_choices(&snippet), &[&None, &None]);
247 }
248
249 #[test]
250 fn test_snippet_with_placeholders() {
251 let snippet = Snippet::parse("one${1:two}three${2:four}").unwrap();
252 assert_eq!(snippet.text, "onetwothreefour");
253 assert_eq!(
254 tabstops(&snippet),
255 &[vec![3..6], vec![11..15], vec![15..15]]
256 );
257 assert_eq!(tabstop_choices(&snippet), &[&None, &None, &None]);
258 }
259
260 #[test]
261 fn test_snippet_with_choice_placeholders() {
262 let snippet = Snippet::parse("type ${1|i32, u32|} = $2")
263 .expect("Should be able to unpack choice placeholders");
264
265 assert_eq!(snippet.text, "type i32 = ");
266 assert_eq!(tabstops(&snippet), &[vec![5..8], vec![11..11],]);
267 assert_eq!(
268 tabstop_choices(&snippet),
269 &[&Some(vec!["i32".to_string(), " u32".to_string()]), &None]
270 );
271
272 let snippet = Snippet::parse(r"${1|\$\{1\|one\,two\,tree\|\}|}")
273 .expect("Should be able to parse choice with escape characters");
274
275 assert_eq!(snippet.text, "${1|one,two,tree|}");
276 assert_eq!(tabstops(&snippet), &[vec![0..18], vec![18..18]]);
277 assert_eq!(
278 tabstop_choices(&snippet),
279 &[&Some(vec!["${1|one,two,tree|}".to_string(),]), &None]
280 );
281 }
282
283 #[test]
284 fn test_snippet_with_nested_placeholders() {
285 let snippet = Snippet::parse(
286 "for (${1:var ${2:i} = 0; ${2:i} < ${3:${4:array}.length}; ${2:i}++}) {$0}",
287 )
288 .unwrap();
289 assert_eq!(snippet.text, "for (var i = 0; i < array.length; i++) {}");
290 assert_eq!(
291 tabstops(&snippet),
292 &[
293 vec![5..37],
294 vec![9..10, 16..17, 34..35],
295 vec![20..32],
296 vec![20..25],
297 vec![40..40],
298 ]
299 );
300 assert_eq!(
301 tabstop_choices(&snippet),
302 &[&None, &None, &None, &None, &None]
303 );
304 }
305
306 #[test]
307 fn test_snippet_parsing_with_escaped_chars() {
308 let snippet = Snippet::parse("\"\\$schema\": $1").unwrap();
309 assert_eq!(snippet.text, "\"$schema\": ");
310 assert_eq!(tabstops(&snippet), &[vec![11..11]]);
311 assert_eq!(tabstop_choices(&snippet), &[&None]);
312
313 let snippet = Snippet::parse("{a\\}").unwrap();
314 assert_eq!(snippet.text, "{a}");
315 assert_eq!(tabstops(&snippet), &[vec![3..3]]);
316 assert_eq!(tabstop_choices(&snippet), &[&None]);
317
318 // backslash not functioning as an escape
319 let snippet = Snippet::parse("a\\b").unwrap();
320 assert_eq!(snippet.text, "a\\b");
321 assert_eq!(tabstops(&snippet), &[vec![3..3]]);
322
323 // first backslash cancelling escaping that would
324 // have happened with second backslash
325 let snippet = Snippet::parse("one\\\\$1two").unwrap();
326 assert_eq!(snippet.text, "one\\two");
327 assert_eq!(tabstops(&snippet), &[vec![4..4], vec![7..7]]);
328 }
329
330 fn tabstops(snippet: &Snippet) -> Vec<Vec<Range<isize>>> {
331 snippet.tabstops.iter().map(|t| t.ranges.to_vec()).collect()
332 }
333
334 fn tabstop_choices(snippet: &Snippet) -> Vec<&Option<Vec<String>>> {
335 snippet.tabstops.iter().map(|t| &t.choices).collect()
336 }
337}