summaryrefslogtreecommitdiff
path: root/src/ide/complete.rs
blob: f0808b21f657ad1099f59225f05744af01eb8c6a (plain) (blame)
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
use std::collections::HashSet;

use if_chain::if_chain;

use super::{plain_docs_sentence, summarize_font_family};
use crate::model::{CastInfo, Scope, Value};
use crate::syntax::ast::AstNode;
use crate::syntax::{ast, LinkedNode, Source, SyntaxKind};
use crate::util::{format_eco, EcoString};
use crate::World;

/// Autocomplete a cursor position in a source file.
///
/// Returns the position from which the completions apply and a list of
/// completions.
///
/// When `explicit` is `true`, the user requested the completion by pressing
/// control and space or something similar.
pub fn autocomplete(
    world: &dyn World,
    source: &Source,
    cursor: usize,
    explicit: bool,
) -> Option<(usize, Vec<Completion>)> {
    let mut ctx = CompletionContext::new(world, source, cursor, explicit)?;

    let _ = complete_rules(&mut ctx)
        || complete_params(&mut ctx)
        || complete_symbols(&mut ctx)
        || complete_markup(&mut ctx)
        || complete_math(&mut ctx)
        || complete_code(&mut ctx);

    Some((ctx.from, ctx.completions))
}

/// An autocompletion option.
#[derive(Debug, Clone)]
pub struct Completion {
    /// The kind of item this completes to.
    pub kind: CompletionKind,
    /// The label the completion is shown with.
    pub label: EcoString,
    /// The completed version of the input, possibly described with snippet
    /// syntax like `${lhs} + ${rhs}`.
    ///
    /// Should default to the `label` if `None`.
    pub apply: Option<EcoString>,
    /// An optional short description, at most one sentence.
    pub detail: Option<EcoString>,
}

/// A kind of item that can be completed.
#[derive(Debug, Clone)]
pub enum CompletionKind {
    /// A syntactical structure.
    Syntax,
    /// A function.
    Func,
    /// A function parameter.
    Param,
    /// A constant.
    Constant,
    /// A font family.
    Font,
    /// A symmie symbol.
    Symbol(char),
}

/// Complete set and show rules.
fn complete_rules(ctx: &mut CompletionContext) -> bool {
    // We don't want to complete directly behind the keyword.
    if !ctx.leaf.kind().is_trivia() {
        return false;
    }

    let Some(prev) = ctx.leaf.prev_leaf() else { return false };

    // Behind the set keyword: "set |".
    if matches!(prev.kind(), SyntaxKind::Set) {
        ctx.from = ctx.cursor;
        ctx.set_rule_completions();
        return true;
    }

    // Behind the show keyword: "show |".
    if matches!(prev.kind(), SyntaxKind::Show) {
        ctx.from = ctx.cursor;
        ctx.show_rule_selector_completions();
        return true;
    }

    // Behind a half-completed show rule: "show strong: |".
    if_chain! {
        if let Some(prev) = ctx.leaf.prev_leaf();
        if matches!(prev.kind(), SyntaxKind::Colon);
        if matches!(prev.parent_kind(), Some(SyntaxKind::ShowRule));
        then {
            ctx.from = ctx.cursor;
            ctx.show_rule_recipe_completions();
            return true;
        }
    }

    false
}

/// Complete call and set rule parameters.
fn complete_params(ctx: &mut CompletionContext) -> bool {
    // Ensure that we are in a function call or set rule's argument list.
    let (callee, set, args) = if_chain! {
        if let Some(parent) = ctx.leaf.parent();
        if let Some(parent) = match parent.kind() {
            SyntaxKind::Named => parent.parent(),
            _ => Some(parent),
        };
        if let Some(args) = parent.cast::<ast::Args>();
        if let Some(grand) = parent.parent();
        if let Some(expr) = grand.cast::<ast::Expr>();
        let set = matches!(expr, ast::Expr::Set(_));
        if let Some(callee) = match expr {
            ast::Expr::FuncCall(call) => call.callee().as_untyped().cast(),
            ast::Expr::Set(set) => Some(set.target()),
            _ => None,
        };
        then {
            (callee, set, args)
        } else {
            return false;
        }
    };

    // Parameter values: "func(param:|)", "func(param: |)".
    if_chain! {
        if let Some(prev) = ctx.leaf.prev_leaf();
        if let Some(before_colon) = match (prev.kind(), ctx.leaf.kind()) {
            (_, SyntaxKind::Colon) => Some(prev),
            (SyntaxKind::Colon, _) => prev.prev_leaf(),
            _ => None,
        };
        if let Some(param) = before_colon.cast::<ast::Ident>();
        then {
            ctx.from = match ctx.leaf.kind() {
                SyntaxKind::Colon | SyntaxKind::Space  => ctx.cursor,
                _ => ctx.leaf.offset(),
            };
            ctx.named_param_value_completions(&callee, &param);
            return true;
        }
    }

    // Parameters: "func(|)", "func(hi|)", "func(12,|)".
    if_chain! {
        if let Some(deciding) = if ctx.leaf.kind().is_trivia() {
            ctx.leaf.prev_leaf()
        } else {
            Some(ctx.leaf.clone())
        };
        if matches!(
            deciding.kind(),
            SyntaxKind::LeftParen
                | SyntaxKind::Comma
                | SyntaxKind::Ident
        );
        then {
            ctx.from = match deciding.kind() {
                SyntaxKind::Ident => deciding.offset(),
                _ => ctx.cursor,
            };

            // Exclude arguments which are already present.
            let exclude: Vec<_> = args.items().filter_map(|arg| match arg {
                ast::Arg::Named(named) => Some(named.name()),
                _ => None,
            }).collect();

            ctx.param_completions(&callee, set, &exclude);
            return true;
        }
    }

    false
}

/// Complete symbols.
///
/// Exception: Math identifiers which can also be symbols are handled separately
/// in `math_completions`.
fn complete_symbols(ctx: &mut CompletionContext) -> bool {
    // Whether a colon is necessary.
    let needs_colon = !ctx.after.starts_with(':');

    // Behind half-completed symbol: "$arrow:|$".
    if_chain! {
        if matches!(ctx.leaf.kind(), SyntaxKind::Atom if ctx.leaf.text() == ":");
        if let Some(prev) = ctx.leaf.prev_leaf();
        if matches!(prev.kind(), SyntaxKind::Ident);
        then {
            ctx.from = prev.offset();
            ctx.symbol_completions(false);
            return true;
        }
    }

    // Start of a symbol: ":|".
    // Checking for a text node ensures that "\:" isn't completed.
    if ctx.before.ends_with(':')
        && matches!(ctx.leaf.kind(), SyntaxKind::Text | SyntaxKind::Atom)
    {
        ctx.from = ctx.cursor;
        ctx.symbol_completions(needs_colon);
        return true;
    }

    // An existing symbol: ":arrow:".
    if matches!(ctx.leaf.kind(), SyntaxKind::Symbol) {
        // We want to complete behind the colon, therefore plus 1.
        let has_colon = ctx.after.starts_with(':');
        ctx.from = ctx.leaf.offset() + (has_colon as usize);
        ctx.symbol_completions(has_colon && needs_colon);
        return true;
    }

    // Behind half-completed symbol: ":bar|" or ":arrow:dou|".
    if_chain! {
        if matches!(
            ctx.leaf.kind(),
            SyntaxKind::Text | SyntaxKind::Atom | SyntaxKind::Ident
        );
        if let Some(prev) = ctx.leaf.prev_leaf();
        if matches!(prev.kind(), SyntaxKind::Symbol) || matches!(
            prev.kind(),
            SyntaxKind::Text | SyntaxKind::Atom if prev.text() == ":"
        );
        then {
            // We want to complete behind the colon, therefore plus 1.
            ctx.from = prev.offset() + 1;
            ctx.symbol_completions(needs_colon);
            return true;
        }
    }

    false
}

/// Complete in markup mode.
fn complete_markup(ctx: &mut CompletionContext) -> bool {
    // Bail if we aren't even in markup.
    if !matches!(ctx.leaf.parent_kind(), None | Some(SyntaxKind::Markup)) {
        return false;
    }

    // Start of an interpolated identifier: "#|".
    // Checking for a text node ensures that "\#" isn't completed.
    if ctx.before.ends_with('#') && matches!(ctx.leaf.kind(), SyntaxKind::Text) {
        ctx.from = ctx.cursor;
        ctx.expr_completions(true);
        return true;
    }

    // An existing identifier: "#pa|".
    if matches!(ctx.leaf.kind(), SyntaxKind::Ident) {
        // We want to complete behind the hashtag, therefore plus 1.
        ctx.from = ctx.leaf.offset() + 1;
        ctx.expr_completions(true);
        return true;
    }

    // Behind a half-completed binding: "#let x = |".
    if_chain! {
        if let Some(prev) = ctx.leaf.prev_leaf();
        if matches!(prev.kind(), SyntaxKind::Eq);
        if matches!(prev.parent_kind(), Some(SyntaxKind::LetBinding));
        then {
            ctx.from = ctx.cursor;
            ctx.expr_completions(false);
            return true;
        }
    }

    // Anywhere: "|".
    if ctx.explicit {
        ctx.from = ctx.cursor;
        ctx.markup_completions();
        return true;
    }

    false
}

/// Complete in math mode.
fn complete_math(ctx: &mut CompletionContext) -> bool {
    if !matches!(
        ctx.leaf.parent_kind(),
        Some(SyntaxKind::Math) | Some(SyntaxKind::Frac) | Some(SyntaxKind::Script)
    ) {
        return false;
    }

    // Start of an interpolated identifier: "#|".
    if matches!(ctx.leaf.kind(), SyntaxKind::Atom if ctx.leaf.text() == "#") {
        ctx.from = ctx.cursor;
        ctx.expr_completions(true);
        return true;
    }

    // Behind existing atom or identifier: "$a|$" or "$abc|$".
    if matches!(ctx.leaf.kind(), SyntaxKind::Atom | SyntaxKind::Ident) {
        ctx.from = ctx.leaf.offset();
        ctx.math_completions();
        return true;
    }

    // Anywhere: "$|$".
    if ctx.explicit {
        ctx.from = ctx.cursor;
        ctx.math_completions();
        return true;
    }

    false
}

/// Complete in code mode.
fn complete_code(ctx: &mut CompletionContext) -> bool {
    if matches!(
        ctx.leaf.parent_kind(),
        None | Some(SyntaxKind::Markup) | Some(SyntaxKind::Math)
    ) {
        return false;
    }

    // An existing identifier: "{ pa| }".
    if matches!(ctx.leaf.kind(), SyntaxKind::Ident) {
        ctx.from = ctx.leaf.offset();
        ctx.expr_completions(false);
        return true;
    }

    // Anywhere: "{ | }".
    // But not within or after an expression.
    if ctx.explicit
        && (ctx.leaf.kind().is_trivia()
            || matches!(ctx.leaf.kind(), SyntaxKind::LeftParen | SyntaxKind::LeftBrace))
    {
        ctx.from = ctx.cursor;
        ctx.expr_completions(false);
        return true;
    }

    false
}

/// Context for autocompletion.
struct CompletionContext<'a> {
    world: &'a dyn World,
    scope: &'a Scope,
    before: &'a str,
    after: &'a str,
    leaf: LinkedNode<'a>,
    cursor: usize,
    explicit: bool,
    from: usize,
    completions: Vec<Completion>,
    seen_casts: HashSet<u128>,
}

impl<'a> CompletionContext<'a> {
    /// Create a new autocompletion context.
    fn new(
        world: &'a dyn World,
        source: &'a Source,
        cursor: usize,
        explicit: bool,
    ) -> Option<Self> {
        let text = source.text();
        let leaf = LinkedNode::new(source.root()).leaf_at(cursor)?;
        Some(Self {
            world,
            scope: &world.library().scope,
            before: &text[..cursor],
            after: &text[cursor..],
            leaf,
            cursor,
            explicit,
            from: cursor,
            completions: vec![],
            seen_casts: HashSet::new(),
        })
    }

    /// Add a prefix and suffix to all applications.
    fn enrich(&mut self, prefix: &str, suffix: &str) {
        for Completion { label, apply, .. } in &mut self.completions {
            let current = apply.as_ref().unwrap_or(label);
            *apply = Some(format_eco!("{prefix}{current}{suffix}"));
        }
    }

    /// Add a snippet completion.
    fn snippet_completion(
        &mut self,
        label: &'static str,
        snippet: &'static str,
        docs: &'static str,
    ) {
        self.completions.push(Completion {
            kind: CompletionKind::Syntax,
            label: label.into(),
            apply: Some(snippet.into()),
            detail: Some(docs.into()),
        });
    }

    /// Add completions for a subset of the global scope.
    fn scope_completions(&mut self, filter: impl Fn(&Value) -> bool) {
        for (name, value) in self.scope.iter() {
            if filter(value) {
                self.value_completion(Some(name.clone()), value, None);
            }
        }
    }

    /// Add completions for the parameters of a function.
    fn param_completions(
        &mut self,
        callee: &ast::Ident,
        set: bool,
        exclude: &[ast::Ident],
    ) {
        let info = if_chain! {
            if let Some(Value::Func(func)) = self.scope.get(callee);
            if let Some(info) = func.info();
            then { info }
            else { return; }
        };

        if callee.as_str() == "text" {
            self.font_completions();
        }

        for param in &info.params {
            if exclude.iter().any(|ident| ident.as_str() == param.name) {
                continue;
            }

            if set && !param.settable {
                continue;
            }

            if param.named {
                self.completions.push(Completion {
                    kind: CompletionKind::Param,
                    label: param.name.into(),
                    apply: Some(format_eco!("{}: ${{}}", param.name)),
                    detail: Some(plain_docs_sentence(param.docs).into()),
                });
            }

            if param.positional {
                self.cast_completions(&param.cast);
            }
        }

        if self.before.ends_with(',') {
            self.enrich(" ", "");
        }
    }

    /// Add completions for the values of a function parameter.
    fn named_param_value_completions(&mut self, callee: &ast::Ident, name: &str) {
        let param = if_chain! {
            if let Some(Value::Func(func)) = self.scope.get(callee);
            if let Some(info) = func.info();
            if let Some(param) = info.param(name);
            if param.named;
            then { param }
            else { return; }
        };

        self.cast_completions(&param.cast);

        if self.before.ends_with(':') {
            self.enrich(" ", "");
        }
    }

    /// Add completions for a castable.
    fn cast_completions(&mut self, cast: &'a CastInfo) {
        // Prevent duplicate completions from appearing.
        if !self.seen_casts.insert(crate::util::hash128(cast)) {
            return;
        }

        match cast {
            CastInfo::Any => {}
            CastInfo::Value(value, docs) => {
                self.value_completion(None, value, Some(docs));
            }
            CastInfo::Type("none") => {
                self.snippet_completion("none", "none", "Nonexistent.")
            }
            CastInfo::Type("auto") => {
                self.snippet_completion("auto", "auto", "A smart default.");
            }
            CastInfo::Type("boolean") => {
                self.snippet_completion("false", "false", "No / Disabled.");
                self.snippet_completion("true", "true", "Yes / Enabled.");
            }
            CastInfo::Type("color") => {
                self.snippet_completion(
                    "luma()",
                    "luma(${v})",
                    "A custom grayscale color.",
                );
                self.snippet_completion(
                    "rgb()",
                    "rgb(${r}, ${g}, ${b}, ${a})",
                    "A custom RGBA color.",
                );
                self.snippet_completion(
                    "cmyk()",
                    "cmyk(${c}, ${m}, ${y}, ${k})",
                    "A custom CMYK color.",
                );
                self.scope_completions(|value| value.type_name() == "color");
            }
            CastInfo::Type("function") => {
                self.snippet_completion(
                    "function",
                    "(${params}) => ${output}",
                    "A custom function.",
                );
            }
            CastInfo::Type(ty) => {
                self.completions.push(Completion {
                    kind: CompletionKind::Syntax,
                    label: (*ty).into(),
                    apply: Some(format_eco!("${{{ty}}}")),
                    detail: Some(format_eco!("A value of type {ty}.")),
                });
                self.scope_completions(|value| value.type_name() == *ty);
            }
            CastInfo::Union(union) => {
                for info in union {
                    self.cast_completions(info);
                }
            }
        }
    }

    /// Add a completion for a specific value.
    fn value_completion(
        &mut self,
        label: Option<EcoString>,
        value: &Value,
        docs: Option<&'static str>,
    ) {
        let mut label = label.unwrap_or_else(|| value.repr().into());
        let mut apply = None;

        if label.starts_with('"') {
            let trimmed = label.trim_matches('"').into();
            apply = Some(label);
            label = trimmed;
        }

        let detail = docs.map(Into::into).or_else(|| match value {
            Value::Func(func) => {
                func.info().map(|info| plain_docs_sentence(info.docs).into())
            }
            Value::Color(color) => Some(format_eco!("The color {color:?}.")),
            Value::Auto => Some("A smart default.".into()),
            _ => None,
        });

        self.completions.push(Completion {
            kind: match value {
                Value::Func(_) => CompletionKind::Func,
                _ => CompletionKind::Constant,
            },
            label,
            apply,
            detail,
        });
    }

    /// Add completions for all font families.
    fn font_completions(&mut self) {
        for (family, iter) in self.world.book().families() {
            let detail = summarize_font_family(iter);
            self.completions.push(Completion {
                kind: CompletionKind::Font,
                label: family.into(),
                apply: Some(format_eco!("\"{family}\"")),
                detail: Some(detail.into()),
            })
        }
    }

    /// Add completions for all symbols.
    fn symbol_completions(&mut self, needs_colon: bool) {
        self.symbol_completions_where(needs_colon, |_| true);
    }

    /// Add completions for a subset of all symbols.
    fn symbol_completions_where(
        &mut self,
        needs_colon: bool,
        filter: impl Fn(char) -> bool,
    ) {
        self.completions.reserve(symmie::list().len());
        for &(name, c) in symmie::list() {
            if filter(c) {
                self.completions.push(Completion {
                    kind: CompletionKind::Symbol(c),
                    label: name.into(),
                    apply: None,
                    detail: None,
                });
            }
        }
        if needs_colon {
            self.enrich("", ":");
        }
    }

    /// Add completions for markup snippets.
    #[rustfmt::skip]
    fn markup_completions(&mut self) {
        self.snippet_completion(
            "linebreak",
            "\\\n${}",
            "Inserts a forced linebreak.",
        );

        self.snippet_completion(
            "symbol",
            ":${}:",
            "Inserts a symbol.",
        );

        self.snippet_completion(
            "strong text",
            "*${strong}*",
            "Strongly emphasizes content by increasing the font weight.",
        );

        self.snippet_completion(
            "emphasized text",
            "_${emphasized}_",
            "Emphasizes content by setting it in italic font style.",
        );

        self.snippet_completion(
            "raw text",
            "`${text}`",
            "Displays text verbatim, in monospace.",
        );

        self.snippet_completion(
            "code listing",
            "```${lang}\n${code}\n```",
            "Inserts computer code with syntax highlighting.",
        );

        self.snippet_completion(
            "hyperlink",
            "https://${example.com}",
            "Links to a URL.",
        );

        self.snippet_completion(
            "math (inline)",
            "$${x}$",
            "Inserts an inline-level mathematical formula.",
        );

        self.snippet_completion(
            "math (block)",
            "$ ${sum_x^2} $",
            "Inserts a block-level mathematical formula.",
        );

        self.snippet_completion(
            "label",
            "<${name}>",
            "Makes the preceding element referencable.",
        );

        self.snippet_completion(
            "reference",
            "@${name}",
            "Inserts a reference to a label.",
        );

        self.snippet_completion(
            "heading",
            "= ${title}",
            "Inserts a section heading.",
        );

        self.snippet_completion(
            "list item",
            "- ${item}",
            "Inserts an item of a bullet list.",
        );

        self.snippet_completion(
            "enumeration item",
            "+ ${item}",
            "Inserts an item of a numbered list.",
        );

        self.snippet_completion(
            "enumeration item (numbered)",
            "${number}. ${item}",
            "Inserts an explicitly numbered list item.",
        );

        self.snippet_completion(
            "term list item",
            "/ ${term}: ${description}",
            "Inserts an item of a term list.",
        );

        self.snippet_completion(
            "expression",
            "#${}",
            "Variables, function calls, and more.",
        );

        self.snippet_completion(
            "code block",
            "{ ${} }",
            "Switches into code mode.",
        );

        self.snippet_completion(
            "content block",
            "[${content}]",
            "Inserts a nested content block that isolates styles.",
        );
    }

    /// Add completions for math snippets.
    #[rustfmt::skip]
    fn math_completions(&mut self) {
        // Exclude non-technical symbols.
        self.symbol_completions_where(false, |c| match c as u32 {
            9728..=9983 => false,
            9984..=10175 => false,
            127744..=128511 => false,
            128512..=128591 => false,
            128640..=128767 => false,
            129280..=129535 => false,
            129648..=129791 => false,
            127136..=127231 => false,
            127024..=127135 => false,
            126976..=127023 => false,
            _ => true,
        });

        self.scope_completions(|value| {
            matches!(
                value,
                Value::Func(func) if func.info().map_or(false, |info| {
                    info.category == "math"
                }),
            )
        });

        self.snippet_completion(
            "subscript",
            "${x}_${2:2}",
            "Sets something in subscript.",
        );

        self.snippet_completion(
            "superscript",
            "${x}^${2:2}",
            "Sets something in superscript.",
        );

        self.snippet_completion(
            "fraction",
            "${x}/${y}",
            "Inserts a fraction.",
        );
    }

    /// Add completions for expression snippets.
    #[rustfmt::skip]
    fn expr_completions(&mut self,  short_form: bool) {
        self.scope_completions(|value| {
            !short_form || matches!(
                value,
                Value::Func(func) if func.info().map_or(true, |info| {
                    info.category != "math"
                }),
            )
        });

        self.snippet_completion(
            "variable",
            "${variable}",
            "Accesses a variable.",
        );

        self.snippet_completion(
            "function call",
            "${function}(${arguments})[${body}]",
            "Evaluates a function.",
        );

        self.snippet_completion(
            "set rule",
            "set ${}",
            "Sets style properties on an element.",
        );

        self.snippet_completion(
            "show rule",
            "show ${}",
            "Redefines the look of an element.",
        );

        self.snippet_completion(
            "let binding",
            "let ${name} = ${value}",
            "Saves a value in a variable.",
        );

        self.snippet_completion(
            "let binding (function)",
            "let ${name}(${params}) = ${output}",
            "Defines a function.",
        );

        self.snippet_completion(
            "if conditional",
            "if ${1 < 2} {\n\t${}\n}",
            "Computes or inserts something conditionally.",
        );

        self.snippet_completion(
            "if-else conditional",
            "if ${1 < 2} {\n\t${}\n} else {\n\t${}\n}",
            "Computes or inserts different things based on a condition.",
        );

        self.snippet_completion(
            "while loop",
            "while ${1 < 2} {\n\t${}\n}",
            "Computes or inserts somthing while a condition is met.",
        );

        self.snippet_completion(
            "for loop",
            "for ${value} in ${(1, 2, 3)} {\n\t${}\n}",
            "Computes or inserts somthing for each value in a collection.",
        );

        self.snippet_completion(
            "for loop (with key)",
            "for ${key}, ${value} in ${(a: 1, b: 2)} {\n\t${}\n}",
            "Computes or inserts somthing for each key and value in a collection.",
        );

        self.snippet_completion(
            "break",
            "break",
            "Exits early from a loop.",
        );

        self.snippet_completion(
            "continue",
            "continue",
            "Continues with the next iteration of a loop.",
        );

        self.snippet_completion(
            "return",
            "return ${output}",
            "Returns early from a function.",
        );

        self.snippet_completion(
            "import",
            "import \"${file.typ}\": ${items}",
            "Imports variables from another file.",
        );

        self.snippet_completion(
            "include",
            "include \"${file.typ}\"",
            "Includes content from another file.",
        );

        if short_form {
            return;
        }

        self.snippet_completion(
            "code block",
            "{ ${} }",
            "Inserts a nested code block.",
        );

        self.snippet_completion(
            "content block",
            "[${content}]",
            "Switches into markup mode.",
        );

        self.snippet_completion(
            "array",
            "(${1, 2, 3})",
            "Creates a sequence of values.",
        );

        self.snippet_completion(
            "dictionary",
            "(${a: 1, b: 2})",
            "Creates a mapping from names to value.",
        );

        self.snippet_completion(
            "function",
            "(${params}) => ${output}",
            "Creates an unnamed function.",
        );
    }

    /// Add completions for all functions from the global scope.
    fn set_rule_completions(&mut self) {
        self.scope_completions(|value| {
            matches!(
                value,
                Value::Func(func) if func.info().map_or(false, |info| {
                    info.params.iter().any(|param| param.settable)
                }),
            )
        });
    }

    /// Add completions for selectors.
    fn show_rule_selector_completions(&mut self) {
        self.scope_completions(
            |value| matches!(value, Value::Func(func) if func.select(None).is_ok()),
        );

        self.enrich("", ": ");

        self.snippet_completion(
            "text selector",
            "\"${text}\": ${}",
            "Replace occurances of specific text.",
        );

        self.snippet_completion(
            "regex selector",
            "regex(\"${regex}\"): ${}",
            "Replace matches of a regular expression.",
        );
    }

    /// Add completions for recipes.
    fn show_rule_recipe_completions(&mut self) {
        self.snippet_completion(
            "replacement",
            "[${content}]",
            "Replace the selected element with content.",
        );

        self.snippet_completion(
            "replacement (string)",
            "\"${text}\"",
            "Replace the selected element with a string of text.",
        );

        self.snippet_completion(
            "transformation",
            "element => [${content}]",
            "Transform the element with a function.",
        );

        self.scope_completions(|value| matches!(value, Value::Func(_)));
    }
}