tinymist_query/analysis/completion/
scope.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
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
//! Completion of definitions in scope.

use typst::foundations::{Array, Dict};

use crate::ty::SigWithTy;

use super::*;

#[derive(BindTyCtx)]
#[bind(types)]
pub(crate) struct Defines {
    pub types: Arc<TypeInfo>,
    pub defines: BTreeMap<EcoString, Ty>,
    pub docs: BTreeMap<EcoString, EcoString>,
}

impl Defines {
    pub fn insert(&mut self, name: EcoString, item: Ty) {
        if name.is_empty() {
            return;
        }

        if let std::collections::btree_map::Entry::Vacant(entry) = self.defines.entry(name.clone())
        {
            entry.insert(item);
        }
    }

    pub fn insert_ty(&mut self, ty: Ty, name: &EcoString) {
        self.insert(name.clone(), ty);
    }

    pub fn insert_scope(&mut self, scope: &Scope) {
        // filter(Some(value)) &&
        for (name, bind) in scope.iter() {
            if !self.defines.contains_key(name) {
                self.insert(name.clone(), Ty::Value(InsTy::new(bind.read().clone())));
            }
        }
    }
}

impl CompletionPair<'_, '_, '_> {
    /// Add completions for definitions that are available at the cursor.
    pub fn scope_completions(&mut self, parens: bool) {
        let Some(defines) = self.scope_defs() else {
            return;
        };

        self.def_completions(defines, parens);
    }

    pub fn scope_defs(&mut self) -> Option<Defines> {
        let mut defines = Defines {
            types: self.worker.ctx.type_check(&self.cursor.source),
            defines: Default::default(),
            docs: Default::default(),
        };

        let mode = self.cursor.leaf_mode();

        previous_decls(self.cursor.leaf.clone(), |node| -> Option<()> {
            match node {
                PreviousDecl::Ident(ident) => {
                    let ty = self
                        .worker
                        .ctx
                        .type_of_span(ident.span())
                        .unwrap_or(Ty::Any);
                    defines.insert_ty(ty, ident.get());
                }
                PreviousDecl::ImportSource(src) => {
                    let ty = analyze_import_source(self.worker.ctx, &defines.types, src)?;
                    let name = ty.name().as_ref().into();
                    defines.insert_ty(ty, &name);
                }
                // todo: cache completion items
                PreviousDecl::ImportAll(mi) => {
                    let ty = analyze_import_source(self.worker.ctx, &defines.types, mi.source())?;
                    ty.iface_surface(
                        true,
                        &mut CompletionScopeChecker {
                            check_kind: ScopeCheckKind::Import,
                            defines: &mut defines,
                            ctx: self.worker.ctx,
                        },
                    );
                }
            }
            None
        });

        let in_math = matches!(mode, InterpretMode::Math);

        let lib = self.worker.world().library();
        let scope = if in_math { &lib.math } else { &lib.global }
            .scope()
            .clone();
        defines.insert_scope(&scope);

        defines.insert(
            EcoString::inline("std"),
            Ty::Value(InsTy::new(lib.std.read().clone())),
        );

        Some(defines)
    }

    /// Add completions for definitions.
    pub fn def_completions(&mut self, defines: Defines, parens: bool) {
        let default_docs = defines.docs;
        let defines = defines.defines;

        let mode = self.cursor.leaf_mode();
        let surrounding_syntax = self.cursor.surrounding_syntax;

        let mut kind_checker = CompletionKindChecker {
            symbols: HashSet::default(),
            functions: HashSet::default(),
        };

        let filter = |checker: &CompletionKindChecker| {
            match surrounding_syntax {
                SurroundingSyntax::Regular => true,
                SurroundingSyntax::StringContent => false,
                SurroundingSyntax::ImportList | SurroundingSyntax::ParamList => false,
                // SurroundingSyntax::Selector => 'selector: {
                //     for func in &checker.functions {
                //         if func.element().is_some() {
                //             break 'selector true;
                //         }
                //     }

                //     false
                // }
                // SurroundingSyntax::ShowTransform => !checker.functions.is_empty(),
                SurroundingSyntax::Selector | SurroundingSyntax::ShowTransform => true,
                SurroundingSyntax::SetRule => 'set_rule: {
                    // todo: user defined elements
                    for func in &checker.functions {
                        if let Some(elem) = func.element() {
                            if elem.params().iter().any(|param| param.settable) {
                                break 'set_rule true;
                            }
                        }
                    }

                    false
                }
            }
        };

        // we don't check literal type here for faster completion
        for (name, ty) in &defines {
            if name.is_empty() {
                continue;
            }

            kind_checker.check(ty);
            if !filter(&kind_checker) {
                continue;
            }

            // todo: describe all chars
            if let Some(sym) = kind_checker.symbols.iter().min_by_key(|s| s.get()) {
                self.symbol_completions(name.clone(), sym);
                continue;
            }

            let docs = default_docs.get(name).cloned();

            let label_details = ty.describe().or_else(|| Some("any".into()));

            crate::log_debug_ct!("scope completions!: {name} {ty:?} {label_details:?}");
            let detail = docs.or_else(|| label_details.clone());

            if !kind_checker.functions.is_empty() {
                let fn_feat =
                    FnCompletionFeat::default().check(kind_checker.functions.iter().copied());
                crate::log_debug_ct!("fn_feat: {name} {ty:?} -> {fn_feat:?}");
                self.func_completion(mode, fn_feat, name.clone(), label_details, detail, parens);
                continue;
            }

            let kind = type_to_completion_kind(ty);
            self.push_completion(Completion {
                kind,
                label: name.clone(),
                label_details,
                detail,
                ..Completion::default()
            });
        }
    }
}

fn analyze_import_source(ctx: &LocalContext, types: &TypeInfo, s: ast::Expr) -> Option<Ty> {
    if let Some(res) = types.type_of_span(s.span()) {
        if !matches!(res.value(), Some(Value::Str(..))) {
            return Some(types.simplify(res, false));
        }
    }

    let m = ctx.module_by_syntax(s.to_untyped())?;
    Some(Ty::Value(InsTy::new_at(m, s.span())))
}

pub(crate) enum ScopeCheckKind {
    Import,
    FieldAccess,
}

#[derive(BindTyCtx)]
#[bind(defines)]
pub(crate) struct CompletionScopeChecker<'a> {
    pub check_kind: ScopeCheckKind,
    pub defines: &'a mut Defines,
    pub ctx: &'a mut LocalContext,
}

impl CompletionScopeChecker<'_> {
    fn is_only_importable(&self) -> bool {
        matches!(self.check_kind, ScopeCheckKind::Import)
    }

    fn is_field_access(&self) -> bool {
        matches!(self.check_kind, ScopeCheckKind::FieldAccess)
    }

    fn type_methods(&mut self, bound_self: Option<Ty>, ty: Type) {
        for name in fields_on(ty) {
            self.defines.insert((*name).into(), Ty::Any);
        }
        let bound_self = bound_self.map(|this| SigTy::unary(this, Ty::Any));
        for (name, bind) in ty.scope().iter() {
            let val = bind.read().clone();
            let has_self = bound_self.is_some()
                && (if let Value::Func(func) = &val {
                    let first_pos = func
                        .params()
                        .and_then(|params| params.iter().find(|p| p.required));
                    first_pos.is_some_and(|p| p.name == "self")
                } else {
                    false
                });
            let ty = Ty::Value(InsTy::new(val));
            let ty = if has_self {
                if let Some(bound_self) = bound_self.as_ref() {
                    Ty::With(SigWithTy::new(ty.into(), bound_self.clone()))
                } else {
                    ty
                }
            } else {
                ty
            };

            self.defines.insert(name.into(), ty);
        }
    }
}

impl IfaceChecker for CompletionScopeChecker<'_> {
    fn check(
        &mut self,
        iface: Iface,
        _ctx: &mut crate::ty::IfaceCheckContext,
        _pol: bool,
    ) -> Option<()> {
        match iface {
            // dict is not importable
            Iface::Dict(d) if !self.is_only_importable() => {
                for (name, term) in d.interface() {
                    self.defines.insert(name.as_ref().into(), term.clone());
                }
            }
            Iface::Value { val, .. } if !self.is_only_importable() => {
                for (name, value) in val.iter() {
                    let term = Ty::Value(InsTy::new(value.clone()));
                    self.defines.insert(name.clone().into(), term);
                }
            }
            Iface::Content { val, .. } if self.is_field_access() => {
                // 255 is the magic "label"
                let styles = StyleChain::default();
                for field_id in 0u8..254u8 {
                    let Some(field_name) = val.field_name(field_id) else {
                        continue;
                    };
                    let param_info = val.params().iter().find(|p| p.name == field_name);
                    let param_docs = param_info.map(|p| p.docs.into());
                    let ty_from_param = param_info.map(|f| Ty::from_cast_info(&f.input));

                    let ty_from_style = val
                        .field_from_styles(field_id, styles)
                        .ok()
                        .map(|v| Ty::Builtin(BuiltinTy::Type(v.ty())));

                    let field_ty = match (ty_from_param, ty_from_style) {
                        (Some(param), None) => Some(param),
                        (Some(opt), Some(_)) | (None, Some(opt)) => Some(Ty::from_types(
                            [opt, Ty::Builtin(BuiltinTy::None)].into_iter(),
                        )),
                        (None, None) => None,
                    };

                    self.defines
                        .insert(field_name.into(), field_ty.unwrap_or(Ty::Any));

                    if let Some(docs) = param_docs {
                        self.defines.docs.insert(field_name.into(), docs);
                    }
                }
            }
            Iface::Type { val, at } if self.is_field_access() => {
                self.type_methods(Some(at.clone()), *val);
            }
            Iface::TypeType { val, .. } if self.is_field_access() => {
                self.type_methods(None, *val);
            }
            Iface::Func { .. } if self.is_field_access() => {
                self.type_methods(Some(iface.to_type()), Type::of::<Func>());
            }
            Iface::Array { .. } | Iface::Tuple { .. } if self.is_field_access() => {
                self.type_methods(Some(iface.to_type()), Type::of::<Array>());
            }
            Iface::Dict { .. } if self.is_field_access() => {
                self.type_methods(Some(iface.to_type()), Type::of::<Dict>());
            }
            Iface::Content { val, .. } => {
                self.defines.insert_scope(val.scope());
            }
            // todo: distingusish TypeType and Type
            Iface::TypeType { val, .. } | Iface::Type { val, .. } => {
                self.defines.insert_scope(val.scope());
            }
            Iface::Func { val, .. } => {
                if let Some(s) = val.scope() {
                    self.defines.insert_scope(s);
                }
            }
            Iface::Module { val, .. } => {
                let ti = self.ctx.type_check_by_id(val);
                if !ti.valid {
                    self.defines
                        .insert_scope(self.ctx.module_by_id(val).ok()?.scope());
                } else {
                    for (name, ty) in ti.exports.iter() {
                        // todo: Interned -> EcoString here
                        let ty = ti.simplify(ty.clone(), false);
                        self.defines.insert(name.as_ref().into(), ty);
                    }
                }
            }
            Iface::ModuleVal { val, .. } => {
                self.defines.insert_scope(val.scope());
            }
            Iface::Array { .. } | Iface::Tuple { .. } | Iface::Dict(..) | Iface::Value { .. } => {}
        }
        None
    }
}