croft-software 0.2.32

VSCode-style TUI written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//! User snippets, loaded from `~/.config/croft/snippets.json`.
//!
//! Format mirrors VS Code's global snippets file: a JSON object keyed by a
//! human name, each value carrying a `prefix`, a `body` (string or array of
//! lines), an optional `description`, and an optional comma-separated `scope`
//! of language ids the snippet applies to (empty = every language).
//!
//! ```json
//! {
//!   "Print to console": {
//!     "prefix": "log",
//!     "body": "console.log($1)$0",
//!     "scope": "javascript,typescript"
//!   }
//! }
//! ```
//!
//! The body is a VS Code snippet string: `$1`, `$2`, … are tab stops, `$0` the
//! final caret, `${1:name}` a placeholder. [`parse_body`] turns it into the
//! literal text to insert plus the ordered stop positions the editor drives.
//!
//! ponytail: supported tab-stop syntax is `$N`, `${N}`, nested
//! `${N:placeholder}`, choices `${N|a,b|}` (the first option is inserted) and
//! the `\$` / `\}` / `\\` escapes. Not supported (name the ceiling, add on
//! demand): variables (`$TM_FILENAME`), transforms, and live mirroring of a
//! repeated `$N` (the placeholder text is duplicated, but only the first
//! occurrence is a navigable stop).

use std::path::{Path, PathBuf};

use serde::Deserialize;

/// One resolved tab stop within an expanded snippet body.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TabStop {
    /// The stop number (`$1` -> 1, `$0` -> 0).
    pub num: u32,
    /// Char offset of the stop within the resolved text.
    pub offset: usize,
    /// Length in chars of the placeholder selected on landing (0 for a bare stop).
    pub len: usize,
}

/// The literal text an expanded snippet inserts, plus its ordered tab stops
/// (ascending by number, with `$0` last).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedBody {
    pub text: String,
    pub stops: Vec<TabStop>,
}

/// Parse a VS Code snippet body into the text to insert and its tab stops.
///
/// Language servers send the same syntax for snippet completions, so it
/// follows the LSP grammar: placeholders nest (`${1:foo(${2:x})}`), a choice
/// inserts its first option (`${1|one,two|}`), and only `\$`, `\}` and `\\`
/// are escapes - any other backslash is text, as in `printf("%d\n", $1)`.
pub fn parse_body(body: &str) -> ParsedBody {
    let mut p = Parser {
        text: String::new(),
        pos: 0,
        stops: Vec::new(),
        seen: Vec::new(),
    };
    let mut chars = body.chars().peekable();
    p.seq(&mut chars, false);
    let mut stops = p.stops;
    // Ascending by number, but `$0` (the final caret) always sorts last.
    stops.sort_by_key(|s| if s.num == 0 { u32::MAX } else { s.num });
    ParsedBody {
        text: p.text,
        stops,
    }
}

type Chars<'a> = std::iter::Peekable<std::str::Chars<'a>>;

struct Parser {
    text: String,
    /// Char offset into `text`.
    pos: usize,
    stops: Vec<TabStop>,
    seen: Vec<u32>,
}

impl Parser {
    fn push(&mut self, c: char) {
        self.text.push(c);
        self.pos += 1;
    }

    /// A stop at `offset` spanning `len` chars. A repeated `$N` duplicates
    /// its text, but only the first occurrence is a navigable stop.
    fn stop(&mut self, num: u32, offset: usize, len: usize) {
        if !self.seen.contains(&num) {
            self.seen.push(num);
            self.stops.push(TabStop { num, offset, len });
        }
    }

    /// Text up to the end, or inside a placeholder up to its closing `}`
    /// (consumed).
    fn seq(&mut self, chars: &mut Chars, in_placeholder: bool) {
        while let Some(c) = chars.next() {
            match c {
                '\\' => match chars.peek() {
                    Some(&n) if matches!(n, '$' | '}' | '\\') => {
                        chars.next();
                        self.push(n);
                    }
                    _ => self.push('\\'),
                },
                '}' if in_placeholder => return,
                '$' => self.dollar(chars),
                _ => self.push(c),
            }
        }
    }

    /// What follows a `$`: a stop, a placeholder, a choice, or a literal `$`.
    fn dollar(&mut self, chars: &mut Chars) {
        let mut look = chars.clone();
        let braced = look.peek() == Some(&'{');
        if braced {
            look.next();
        }
        let mut digits = String::new();
        while let Some(&d) = look.peek().filter(|d| d.is_ascii_digit()) {
            digits.push(d);
            look.next();
        }
        let Ok(num) = digits.parse::<u32>() else {
            self.push('$');
            return;
        };
        if !braced {
            *chars = look;
            self.stop(num, self.pos, 0);
            return;
        }
        match look.next() {
            Some('}') => {
                *chars = look;
                self.stop(num, self.pos, 0);
            }
            Some(':') => {
                *chars = look;
                let start = self.pos;
                self.seq(chars, true);
                self.stop(num, start, self.pos - start);
            }
            Some('|') => {
                let Some(first) = choice_first(&mut look) else {
                    self.push('$');
                    return;
                };
                *chars = look;
                let start = self.pos;
                for c in first.chars() {
                    self.push(c);
                }
                self.stop(num, start, self.pos - start);
            }
            // Malformed `${N…`: not a stop, the `$` is literal.
            _ => self.push('$'),
        }
    }
}

/// The first option of a choice, reading past its closing `|}`. `\,`,
/// `\|` and `\\` escape inside it. `None` when the choice is unterminated.
fn choice_first(chars: &mut Chars) -> Option<String> {
    let mut first = String::new();
    let mut in_first = true;
    while let Some(c) = chars.next() {
        match c {
            '\\' => {
                let n = match chars.peek() {
                    Some(&n) if matches!(n, ',' | '|' | '\\') => {
                        chars.next();
                        n
                    }
                    _ => '\\',
                };
                if in_first {
                    first.push(n);
                }
            }
            ',' => in_first = false,
            '|' if chars.peek() == Some(&'}') => {
                chars.next();
                return Some(first);
            }
            _ if in_first => first.push(c),
            _ => {}
        }
    }
    None
}

/// Convert a char offset within a block of text to a (line delta, column)
/// relative to the block's start. Column is chars since the last newline.
pub fn offset_to_line_col(text: &str, offset: usize) -> (usize, usize) {
    let mut line = 0usize;
    let mut col = 0usize;
    for (i, c) in text.chars().enumerate() {
        if i == offset {
            break;
        }
        if c == '\n' {
            line += 1;
            col = 0;
        } else {
            col += 1;
        }
    }
    (line, col)
}

/// One user snippet.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Snippet {
    pub prefix: String,
    pub body: String,
    pub scope: Vec<String>,
}

impl Snippet {
    /// Whether this snippet applies to `language` (its scope is empty or names it).
    fn applies_to(&self, language: &str) -> bool {
        self.scope.is_empty() || self.scope.iter().any(|s| s == language)
    }
}

/// The loaded set of user snippets.
#[derive(Debug, Clone, Default)]
pub struct SnippetSet {
    snippets: Vec<Snippet>,
    /// What could not be loaded: the file itself, or one entry by name. The
    /// rest still loads (#1483).
    warnings: Vec<String>,
    /// The file did not parse at all, so nothing was loaded from it (#1191).
    broken: bool,
}

/// The on-disk value shape: `prefix` is a string or an array of them (VS Code
/// allows both); `body` is a string or an array of lines; `scope` is an
/// optional comma-separated language list.
#[derive(Debug, Deserialize)]
struct RawSnippet {
    #[serde(default)]
    prefix: Prefix,
    #[serde(default)]
    body: Body,
    #[serde(default)]
    scope: Option<String>,
}

/// A snippet's trigger words. VS Code lets one snippet answer to several
/// (`"prefix": ["for", "fori"]`), and to none when it is only meant for
/// Insert Snippet.
#[derive(Debug, Deserialize, Default)]
#[serde(untagged)]
enum Prefix {
    #[default]
    Missing,
    One(String),
    Many(Vec<String>),
}

impl Prefix {
    fn into_vec(self) -> Vec<String> {
        match self {
            Prefix::Missing => Vec::new(),
            Prefix::One(p) => vec![p],
            Prefix::Many(ps) => ps,
        }
    }
}

#[derive(Debug, Deserialize, Default)]
#[serde(untagged)]
enum Body {
    #[default]
    Missing,
    One(String),
    Many(Vec<String>),
}

impl Body {
    fn joined(&self) -> String {
        match self {
            Body::Missing => String::new(),
            Body::One(s) => s.clone(),
            Body::Many(lines) => lines.join("\n"),
        }
    }
}

impl SnippetSet {
    pub fn load(path: &Path) -> Self {
        let Ok(json) = std::fs::read_to_string(path) else {
            return Self::default();
        };
        Self::from_json(&json)
    }

    pub fn from_json(json: &str) -> Self {
        Self::parse(json).0
    }

    /// The snippets in `json`, read as JSONC like VS Code's snippet files
    /// and croft's settings (comments and trailing commas, #1191), entry by
    /// entry, so one snippet croft cannot read is skipped and named in
    /// [`warnings`](Self::warnings) instead of taking every other snippet
    /// with it (#1483). The second half is why none were loaded when the
    /// file does not parse at all; it is in the warnings too.
    pub fn parse(json: &str) -> (Self, Option<String>) {
        let stripped = crate::tasks::strip_jsonc(json);
        if stripped.trim().is_empty() {
            return (Self::default(), None);
        }
        let entries: serde_json::Map<String, serde_json::Value> = match serde_json::from_str(
            &stripped,
        ) {
            Ok(entries) => entries,
            Err(e) => {
                let why = format!(
                    "snippets.json could not be read: the file does not parse ({e}); NO snippets were loaded from it"
                );
                let set = Self {
                    snippets: Vec::new(),
                    warnings: vec![why.clone()],
                    broken: true,
                };
                return (set, Some(why));
            }
        };
        let mut snippets = Vec::new();
        let mut warnings = Vec::new();
        for (name, value) in entries {
            let raw: RawSnippet = match serde_json::from_value(value) {
                Ok(raw) => raw,
                Err(e) => {
                    warnings.push(format!("snippet {name:?} skipped: {e}"));
                    continue;
                }
            };
            let body = raw.body.joined();
            let scope: Vec<String> = raw
                .scope
                .map(|s| {
                    s.split(',')
                        .map(|p| p.trim().to_string())
                        .filter(|p| !p.is_empty())
                        .collect()
                })
                .unwrap_or_default();
            for prefix in raw.prefix.into_vec() {
                if prefix.is_empty() {
                    continue;
                }
                snippets.push(Snippet {
                    prefix,
                    body: body.clone(),
                    scope: scope.clone(),
                });
            }
        }
        let set = Self {
            snippets,
            warnings,
            broken: false,
        };
        (set, None)
    }

    pub fn is_empty(&self) -> bool {
        self.snippets.is_empty()
    }

    /// What could not be loaded, one line each, for OUTPUT · Snippets.
    pub fn warnings(&self) -> &[String] {
        &self.warnings
    }

    /// Whether the file did not parse at all, so nothing was loaded (#1191).
    pub fn is_broken(&self) -> bool {
        self.broken
    }

    /// Snippets in `language` whose prefix begins with `word` (case-sensitive,
    /// like VS Code); for injecting into the completion popup. An empty `word`
    /// still filters by language but matches every prefix.
    pub fn matching(&self, word: &str, language: &str) -> Vec<&Snippet> {
        self.snippets
            .iter()
            .filter(|s| s.applies_to(language) && s.prefix.starts_with(word))
            .collect()
    }

    /// The single snippet whose prefix exactly equals `word` in `language`, if
    /// any — the Tab-to-expand lookup.
    pub fn exact(&self, word: &str, language: &str) -> Option<&Snippet> {
        self.snippets
            .iter()
            .find(|s| s.prefix == word && s.applies_to(language))
    }
}

pub fn snippets_path() -> PathBuf {
    crate::profiles::file("snippets.json")
}

/// Seeded on first "Configure User Snippets" so the user starts from a working
/// example rather than a blank buffer.
pub const TEMPLATE: &str = r#"// croft user snippets. Keyed by name; each has a "prefix" (or an array of
// them) you type then expand with Tab, a "body" (string or array of lines), and an optional "scope"
// (comma-separated language ids; omit for all languages).
// Tab stops: $1, $2 … in order, $0 final caret, ${1:placeholder} with default text.
{
  "Print to console": {
    "prefix": "log",
    "body": "console.log($1)$0",
    "scope": "javascript,typescript"
  },
  "Python main guard": {
    "prefix": "main",
    "body": [
      "if __name__ == \"__main__\":",
      "    ${1:main()}$0"
    ],
    "scope": "python"
  }
}
"#;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn snippets_with_a_trailing_comma_or_block_comment_still_load() {
        // #1191: VS Code's snippet files are JSONC, and a pasted one has both.
        let set = SnippetSet::from_json(
            "{\n  /* logging */\n  \"log\": { \"prefix\": \"log\", \"body\": \"console.log($1);\" },\n  \"todo\": { \"prefix\": \"todo\", \"body\": [\"// TODO: $1\",], },\n}\n",
        );
        assert_eq!(set.matching("log", "javascript").len(), 1);
        let todo = set.matching("todo", "javascript");
        assert_eq!(todo.len(), 1);
        assert_eq!(
            todo[0].body, "// TODO: $1",
            "a comment marker inside a string stays"
        );
    }

    #[test]
    fn snippet_bodies_keep_what_only_looks_like_jsonc() {
        // Negative: commas before a brace and comment markers inside strings
        // are the snippet's text, not JSONC to strip.
        let set =
            SnippetSet::from_json(r#"{"c": {"prefix": "cm", "body": "/* $1 */ f(a, }) // end"}}"#);
        assert_eq!(set.matching("cm", "c")[0].body, "/* $1 */ f(a, }) // end");
    }

    #[test]
    fn a_snippets_file_that_does_not_parse_says_so() {
        // A file that is broken (not merely JSONC) loads nothing, as before,
        // but no longer silently: the reason comes back for OUTPUT.
        let (set, warning) = SnippetSet::parse(r#"{"log": {"prefix": "log" "body": "x"}}"#);
        assert!(set.matching("log", "javascript").is_empty());
        let warning = warning.expect("a warning");
        assert!(warning.contains("does not parse"), "{warning}");
        let (_, none) = SnippetSet::parse("{}");
        assert_eq!(none, None, "an empty file is fine");
    }

    #[test]
    fn lsp_snippets_keep_backslashes_nest_placeholders_and_pick_a_choice() {
        let p = parse_body(r#"printf("%d\n", $1)"#);
        assert_eq!(p.text, r#"printf("%d\n", )"#);
        let p = parse_body("${1:foo(${2:x})}$0");
        assert_eq!(p.text, "foo(x)");
        assert_eq!(
            p.stops[0],
            TabStop {
                num: 1,
                offset: 0,
                len: 6
            }
        );
        assert_eq!(
            p.stops[1],
            TabStop {
                num: 2,
                offset: 4,
                len: 1
            }
        );
        let p = parse_body(r"${1|one,two|} \$5 \} a\b");
        assert_eq!(p.text, "one $5 } a\\b");
        assert_eq!(
            p.stops[0],
            TabStop {
                num: 1,
                offset: 0,
                len: 3
            }
        );
    }

    #[test]
    fn parses_single_bare_stop() {
        let p = parse_body("console.log($1)");
        assert_eq!(p.text, "console.log()");
        assert_eq!(
            p.stops,
            vec![TabStop {
                num: 1,
                offset: 12,
                len: 0
            }]
        );
    }

    #[test]
    fn placeholder_text_is_inserted_and_measured() {
        let p = parse_body("for ${1:i} in ${2:range}:");
        assert_eq!(p.text, "for i in range:");
        assert_eq!(
            p.stops,
            vec![
                TabStop {
                    num: 1,
                    offset: 4,
                    len: 1
                },
                TabStop {
                    num: 2,
                    offset: 9,
                    len: 5
                },
            ]
        );
    }

    #[test]
    fn final_zero_stop_sorts_last_regardless_of_position() {
        let p = parse_body("try:\n    $0\nexcept $1:");
        let nums: Vec<u32> = p.stops.iter().map(|s| s.num).collect();
        assert_eq!(nums, vec![1, 0], "$0 must be visited last");
    }

    #[test]
    fn repeated_number_duplicates_text_but_only_first_is_a_stop() {
        let p = parse_body("$1 = $1 + 1");
        assert_eq!(p.text, " =  + 1");
        assert_eq!(p.stops.len(), 1, "mirror occurrences are not extra stops");
        assert_eq!(p.stops[0].offset, 0);
    }

    #[test]
    fn escaped_dollar_is_literal() {
        let p = parse_body("cost is \\$5 for $1");
        assert_eq!(p.text, "cost is $5 for ");
        assert_eq!(p.stops.len(), 1);
    }

    #[test]
    fn dollar_before_non_stop_is_literal() {
        let p = parse_body("$foo and ${bar}");
        assert_eq!(p.text, "$foo and ${bar}");
        assert!(p.stops.is_empty());
    }

    #[test]
    fn offset_to_line_col_spans_newlines() {
        assert_eq!(offset_to_line_col("ab\ncd", 0), (0, 0));
        assert_eq!(offset_to_line_col("ab\ncd", 2), (0, 2));
        assert_eq!(offset_to_line_col("ab\ncd", 4), (1, 1));
    }

    #[test]
    fn loads_string_and_array_bodies_with_scope() {
        let json = r#"{
            "Log": { "prefix": "log", "body": "console.log($1)", "scope": "javascript, typescript" },
            "Guard": { "prefix": "main", "body": ["line1", "line2"] }
        }"#;
        let set = SnippetSet::from_json(json);
        // Scope with a space after the comma still matches typescript.
        assert!(set.exact("log", "typescript").is_some());
        assert!(set.exact("log", "python").is_none());
        // Array body joined with newlines; no scope -> every language.
        let guard = set
            .exact("main", "python")
            .expect("no-scope snippet is universal");
        assert_eq!(guard.body, "line1\nline2");
    }

    #[test]
    fn scope_filters_by_language() {
        let json = r#"{ "Log": { "prefix": "log", "body": "x", "scope": "javascript" } }"#;
        let set = SnippetSet::from_json(json);
        assert!(set.exact("log", "python").is_none());
        assert!(set.exact("log", "javascript").is_some());
    }

    #[test]
    fn matching_filters_by_prefix_and_scope_for_the_popup() {
        let json = r#"{
            "Log": { "prefix": "log", "body": "x", "scope": "javascript" },
            "Loop": { "prefix": "loop", "body": "y" }
        }"#;
        let set = SnippetSet::from_json(json);
        // Prefix "lo" matches both in JS (Loop has no scope -> universal).
        let js: Vec<&str> = set
            .matching("lo", "javascript")
            .iter()
            .map(|s| s.prefix.as_str())
            .collect();
        assert_eq!(js.len(), 2);
        // In Python only the unscoped "loop" survives.
        let py: Vec<&str> = set
            .matching("lo", "python")
            .iter()
            .map(|s| s.prefix.as_str())
            .collect();
        assert_eq!(py, vec!["loop"]);
    }

    #[test]
    fn exact_requires_full_prefix_match() {
        let json = r#"{ "Log": { "prefix": "log", "body": "x" } }"#;
        let set = SnippetSet::from_json(json);
        assert!(set.exact("lo", "javascript").is_none());
        assert!(set.exact("log", "javascript").is_some());
    }

    #[test]
    fn template_is_valid_and_expands() {
        let set = SnippetSet::from_json(TEMPLATE);
        assert!(!set.is_empty());
        let log = set
            .exact("log", "javascript")
            .expect("template log snippet");
        let parsed = parse_body(&log.body);
        assert_eq!(parsed.text, "console.log()");
    }

    #[test]
    fn junk_json_is_empty_not_fatal() {
        assert!(SnippetSet::from_json("not json").is_empty());
        assert!(SnippetSet::from_json("").is_empty());
    }

    #[test]
    fn an_array_prefix_expands_from_each_of_its_words() {
        let json = r#"{
            "For loop": {
                "prefix": ["for", "fori"],
                "body": ["for ${1:i} in range(${2:10}):", "    $0"],
                "scope": "python"
            }
        }"#;
        let set = SnippetSet::from_json(json);
        for word in ["for", "fori"] {
            let s = set
                .exact(word, "python")
                .unwrap_or_else(|| panic!("{word} must expand"));
            assert_eq!(s.body, "for ${1:i} in range(${2:10}):\n    $0");
        }
        assert!(set.exact("fori", "rust").is_none(), "scope still applies");
        assert!(set.warnings().is_empty());
    }

    #[test]
    fn an_array_prefix_does_not_hide_the_other_snippets() {
        let json = format!(
            "{}, \"For loop\": {{ \"prefix\": [\"for\", \"fori\"], \"body\": \"x\" }} }}",
            TEMPLATE.trim_end().trim_end_matches('}').trim_end()
        );
        let set = SnippetSet::from_json(&json);
        assert!(
            set.exact("log", "javascript").is_some(),
            "template snippets stay"
        );
        assert!(set.exact("fori", "python").is_some());
    }

    #[test]
    fn a_malformed_entry_is_skipped_and_named_while_the_rest_load() {
        let json = r#"{
            "Log": { "prefix": "log", "body": "console.log($1)" },
            "Broken": { "prefix": 42, "body": "x" },
            "Main": { "prefix": "main", "body": "if __name__ == '__main__':" }
        }"#;
        let set = SnippetSet::from_json(json);
        assert!(set.exact("log", "javascript").is_some());
        assert!(set.exact("main", "python").is_some());
        assert_eq!(set.warnings().len(), 1, "{:?}", set.warnings());
        assert!(
            set.warnings()[0].contains("\"Broken\""),
            "{:?}",
            set.warnings()
        );
    }

    #[test]
    fn an_entry_with_no_prefix_is_not_an_error() {
        // Legal in VS Code: such a snippet is only reachable from Insert Snippet.
        let json = r##"{
            "Header": { "body": "# header" },
            "Log": { "prefix": "log", "body": "x" }
        }"##;
        let set = SnippetSet::from_json(json);
        assert!(set.exact("log", "javascript").is_some());
        assert!(set.warnings().is_empty(), "{:?}", set.warnings());
    }

    #[test]
    fn an_unreadable_file_reports_an_error_instead_of_loading_nothing_silently() {
        let set = SnippetSet::from_json("{ \"Log\": { \"prefix\": \"log\", ");
        assert!(set.is_empty());
        assert_eq!(set.warnings().len(), 1);
        assert!(set.warnings()[0].starts_with("snippets.json could not be read"));
    }

    /// Negative: an empty or comment-only file is not a mistake.
    #[test]
    fn an_empty_file_has_no_warnings() {
        assert!(SnippetSet::from_json("").warnings().is_empty());
        assert!(
            SnippetSet::from_json("// nothing yet\n")
                .warnings()
                .is_empty()
        );
        assert!(SnippetSet::from_json("{}").warnings().is_empty());
    }
}