tinymist_query/analysis/completion/
func.rs

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
//! Completion for functions on nodes.

use super::*;

impl CompletionPair<'_, '_, '_> {
    pub fn func_completion(
        &mut self,
        mode: InterpretMode,
        fn_feat: FnCompletionFeat,
        name: EcoString,
        label_details: Option<EcoString>,
        detail: Option<EcoString>,
        parens: bool,
    ) {
        let base = Completion {
            kind: CompletionKind::Func,
            label_details,
            detail,
            command: self
                .worker
                .ctx
                .analysis
                .trigger_on_snippet_with_param_hint(true)
                .map(From::from),
            ..Default::default()
        };

        if matches!(
            self.cursor.surrounding_syntax,
            SurroundingSyntax::ShowTransform
        ) && (fn_feat.min_pos() > 0 || fn_feat.min_named() > 0)
        {
            self.push_completion(Completion {
                label: eco_format!("{name}.with"),
                apply: Some(eco_format!("{name}.with(${{}})")),
                ..base.clone()
            });
        }
        if fn_feat.is_element
            && matches!(self.cursor.surrounding_syntax, SurroundingSyntax::Selector)
        {
            self.push_completion(Completion {
                label: eco_format!("{name}.where"),
                apply: Some(eco_format!("{name}.where(${{}})")),
                ..base.clone()
            });
        }

        let bad_instantiate = matches!(
            self.cursor.surrounding_syntax,
            SurroundingSyntax::Selector | SurroundingSyntax::SetRule
        ) && !fn_feat.is_element;
        if !bad_instantiate {
            if !parens || matches!(self.cursor.surrounding_syntax, SurroundingSyntax::Selector) {
                self.push_completion(Completion {
                    label: name,
                    ..base
                });
            } else if (fn_feat.min_pos() < 1 || fn_feat.has_only_self()) && !fn_feat.has_rest {
                self.push_completion(Completion {
                    apply: Some(eco_format!("{}()${{}}", name)),
                    label: name,
                    command: None,
                    ..base
                });
            } else {
                let accept_content_arg = fn_feat.next_arg_is_content && !fn_feat.has_rest;
                let scope_reject_content = matches!(mode, InterpretMode::Math)
                    || matches!(
                        self.cursor.surrounding_syntax,
                        SurroundingSyntax::Selector | SurroundingSyntax::SetRule
                    );
                self.push_completion(Completion {
                    apply: Some(eco_format!("{name}(${{}})")),
                    label: name.clone(),
                    ..base.clone()
                });
                if !scope_reject_content && accept_content_arg {
                    self.push_completion(Completion {
                        apply: Some(eco_format!("{name}[${{}}]")),
                        label: eco_format!("{name}.bracket"),
                        ..base
                    });
                };
            }
        }
    }
}