tinymist_query/analysis/completion/
mode.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
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
//! Completion by [`crate::syntax::InterpretMode`].

use super::*;
impl CompletionPair<'_, '_, '_> {
    /// Complete in comments. Or rather, don't!
    pub fn complete_comments(&mut self) -> bool {
        let text = self.cursor.leaf.get().text();
        // check if next line defines a function
        if_chain! {
            if text == "///" || text == "/// ";
            // hash node
            if let Some(next) = self.cursor.leaf.next_leaf();
            // let node
            if let Some(next_next) = next.next_leaf();
            if let Some(next_next) = next_next.next_leaf();
            if matches!(next_next.parent_kind(), Some(SyntaxKind::Closure));
            if let Some(closure) = next_next.parent();
            if let Some(closure) = closure.cast::<ast::Expr>();
            if let ast::Expr::Closure(c) = closure;
            then {
                let mut doc_snippet: String = if text == "///" {
                    " $0\n///".to_string()
                } else {
                    "$0\n///".to_string()
                };
                let mut i = 0;
                for param in c.params().children() {
                    // TODO: Properly handle Pos and Spread argument
                    let param: &EcoString = match param {
                        Param::Pos(p) => {
                            match p {
                                ast::Pattern::Normal(ast::Expr::Ident(ident)) => ident.get(),
                                _ => &"_".into()
                            }
                        }
                        Param::Named(n) => n.name().get(),
                        Param::Spread(s) => {
                            if let Some(ident) = s.sink_ident() {
                                &eco_format!("{}", ident.get())
                            } else {
                                &EcoString::new()
                            }
                        }
                    };
                    log::info!("param: {param}, index: {i}");
                    doc_snippet += &format!("\n/// - {param} (${}): ${}", i + 1, i + 2);
                    i += 2;
                }
                doc_snippet += &format!("\n/// -> ${}", i + 1);
                self.push_completion(Completion {
                    label: "Document function".into(),
                    apply: Some(doc_snippet.into()),
                    ..Completion::default()
                });
            }
        };

        true
    }

    /// Complete in markup mode.
    pub fn complete_markup(&mut self) -> bool {
        let parent_raw =
            node_ancestors(&self.cursor.leaf).find(|node| matches!(node.kind(), SyntaxKind::Raw));

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

        // Behind a half-completed context block: "#context |".
        if_chain! {
            if let Some(prev) = self.cursor.leaf.prev_leaf();
            if prev.kind() == SyntaxKind::Context;
            then {
                self.cursor.from = self.cursor.cursor;
                self.code_completions(false);
                return true;
            }
        }

        // Directly after a raw block.
        if let Some(parent_raw) = parent_raw {
            let mut s = Scanner::new(self.cursor.text);
            s.jump(parent_raw.offset());
            if s.eat_if("```") {
                s.eat_while('`');
                let start = s.cursor();
                if s.eat_if(is_id_start) {
                    s.eat_while(is_id_continue);
                }
                if s.cursor() == self.cursor.cursor {
                    self.cursor.from = start;
                    self.raw_completions();
                }
                return true;
            }
        }

        // Anywhere: "|".
        if !is_triggered_by_punc(self.worker.trigger_character) && self.worker.explicit {
            self.cursor.from = self.cursor.cursor;
            self.snippet_completions(Some(InterpretMode::Markup), None);
            return true;
        }

        false
    }

    /// Complete in math mode.
    pub fn complete_math(&mut self) -> bool {
        // Behind existing atom or identifier: "$a|$" or "$abc|$".
        if !is_triggered_by_punc(self.worker.trigger_character)
            && matches!(
                self.cursor.leaf.kind(),
                SyntaxKind::Text | SyntaxKind::MathIdent | SyntaxKind::MathText
            )
        {
            self.cursor.from = self.cursor.leaf.offset();
            self.scope_completions(true);
            self.snippet_completions(Some(InterpretMode::Math), None);
            return true;
        }

        // Anywhere: "$|$".
        if !is_triggered_by_punc(self.worker.trigger_character) && self.worker.explicit {
            self.cursor.from = self.cursor.cursor;
            self.scope_completions(true);
            self.snippet_completions(Some(InterpretMode::Math), None);
            return true;
        }

        false
    }

    /// Complete in code mode.
    pub fn complete_code(&mut self) -> bool {
        // Start of an interpolated identifier: "#|".
        if self.cursor.leaf.kind() == SyntaxKind::Hash {
            self.cursor.from = self.cursor.cursor;
            self.code_completions(true);

            return true;
        }

        // Start of an interpolated identifier: "#pa|".
        if self.cursor.leaf.kind() == SyntaxKind::Ident {
            self.cursor.from = self.cursor.leaf.offset();
            self.code_completions(is_hash_expr(&self.cursor.leaf));
            return true;
        }

        // Behind a half-completed context block: "context |".
        if_chain! {
            if let Some(prev) = self.cursor.leaf.prev_leaf();
            if prev.kind() == SyntaxKind::Context;
            then {
                self.cursor.from = self.cursor.cursor;
                self.code_completions(false);
                return true;
            }
        }

        // An existing identifier: "{ pa| }".
        if self.cursor.leaf.kind() == SyntaxKind::Ident
            && !matches!(
                self.cursor.leaf.parent_kind(),
                Some(SyntaxKind::FieldAccess)
            )
        {
            self.cursor.from = self.cursor.leaf.offset();
            self.code_completions(false);
            return true;
        }

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

        false
    }

    /// Add completions for expression snippets.
    fn code_completions(&mut self, hash: bool) {
        // todo: filter code completions
        // matches!(value, Value::Symbol(_) | Value::Func(_) | Value::Type(_) |
        // Value::Module(_))
        self.scope_completions(true);

        self.snippet_completions(Some(InterpretMode::Code), None);

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