tinymist-query 0.14.16-rc1

Language queries for tinymist.
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
use std::ops::Deref;

use comemo::Track;
use serde::{Deserialize, Serialize};
use tinymist_analysis::analyze_expr;
use tinymist_project::{DiagnosticFormat, PathPattern};
use tinymist_std::error::prelude::*;
use tinymist_world::vfs::WorkspaceResolver;
use tinymist_world::{EntryReader, EntryState, ShadowApi, diag::print_diagnostics_to_string};
use typst::diag::{At, SourceResult};
use typst::foundations::{Args, Dict, NativeFunc, eco_format};
use typst::syntax::Span;
use typst::utils::LazyHash;
use typst::{
    foundations::{Bytes, IntoValue, StyleChain},
    text::TextElem,
};
use typst_shim::eval::{Eval, Vm};
use typst_shim::syntax::LinkedNodeExt;

use crate::{
    prelude::*,
    syntax::{InterpretMode, interpret_mode_at},
};

/// A query to get the mode at a specific position in a text document.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum InteractCodeContextQuery {
    /// (Experimental) Evaluate a path expression at a specific position in a
    /// text document.
    PathAt {
        /// Code to evaluate. If the code starts with `{` and ends with `}`, it
        /// will be evaluated as a code expression, otherwise it will be
        /// evaluated as a path pattern.
        ///
        /// ## Example
        ///
        /// evaluate a path pattern, which could use following definitions:
        ///
        /// ```plain
        /// $root/x/$dir/../$name // is evaluated as
        /// /path/to/root/x/dir/../main
        /// ```
        ///
        /// ## Example
        ///
        /// evaluate a code expression, which could use following definitions:
        /// - `root`: the root of the workspace
        /// - `dir`: the directory of the current file
        /// - `name`: the name of the current file
        /// - `join(a, b, ...)`: join the arguments with the path separator
        ///
        /// ```plain
        /// { join(root, "x", dir, "y", name) } // is evaluated as
        /// /path/to/root/x/dir/y/main
        /// ```
        code: String,
        /// The extra `sys.inputs` for the code expression.
        inputs: Dict,
    },
    /// Get the mode at a specific position in a text document.
    ModeAt {
        /// The position inside the text document.
        position: LspPosition,
    },
    /// Get the style at a specific position in a text document.
    StyleAt {
        /// The position inside the text document.
        position: LspPosition,
        /// Style to query
        style: Vec<String>,
    },
}

/// A response to a `InteractCodeContextQuery`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum InteractCodeContextResponse {
    /// Evaluate a path expression at a specific position in a text document.
    PathAt(QueryResult<serde_json::Value>),
    /// Get the mode at a specific position in a text document.
    ModeAt {
        /// The mode at the requested position.
        mode: InterpretMode,
    },
    /// Get the style at a specific position in a text document.
    StyleAt {
        /// The style at the requested position.
        style: Vec<Option<JsonValue>>,
    },
}

/// A request to get the code context of a text document.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "kind")]
pub struct InteractCodeContextRequest {
    /// The path to the text document.
    pub path: PathBuf,
    /// The queries to execute.
    pub query: Vec<Option<InteractCodeContextQuery>>,
}

impl SemanticRequest for InteractCodeContextRequest {
    type Response = Vec<Option<InteractCodeContextResponse>>;

    fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
        let mut responses = Vec::new();

        let source = ctx.source_by_path(&self.path).ok()?;

        for query in self.query {
            responses.push(query.and_then(|query| match query {
                InteractCodeContextQuery::PathAt { code, inputs: base } => {
                    let res = eval_path_expr(ctx, &code, base)?;
                    Some(InteractCodeContextResponse::PathAt(res))
                }
                InteractCodeContextQuery::ModeAt { position } => {
                    let cursor = ctx.to_typst_pos(position, &source)?;
                    let mode = Self::mode_at(&source, cursor)?;
                    Some(InteractCodeContextResponse::ModeAt { mode })
                }
                InteractCodeContextQuery::StyleAt { position, style } => {
                    let mut world = ctx.world().clone();
                    log::info!(
                        "style at position {position:?} . {style:?} when main is {:?}",
                        world.main()
                    );
                    let cursor = ctx.to_typst_pos(position, &source)?;
                    let root = LinkedNode::new(source.root());
                    let mut leaf = root.leaf_at_compat(cursor)?;
                    log::info!("style at leaf {leaf:?} . {style:?}");

                    if !matches!(leaf.kind(), SyntaxKind::Text | SyntaxKind::MathText) {
                        return None;
                    }

                    if matches!(leaf.parent_kind(), Some(SyntaxKind::Raw)) {
                        leaf = leaf.parent()?.clone();
                    }

                    let mode = Self::mode_at(&source, cursor);
                    if !matches!(
                        mode,
                        Some(InterpretMode::Code | InterpretMode::Markup | InterpretMode::Math)
                    ) {
                        leaf = leaf.parent()?.clone();
                    }
                    let mut mapped_source = source.clone();
                    let (with, offset) = match mode {
                        Some(InterpretMode::Code) => ("context text.font", 8),
                        _ => ("#context text.font", 10),
                    };
                    let start = leaf.range().start;
                    mapped_source.edit(leaf.range(), with);

                    let _ = world.map_shadow_by_id(
                        mapped_source.id(),
                        Bytes::new(mapped_source.text().as_bytes().to_vec()),
                    );
                    world.take_db();

                    let root = LinkedNode::new(mapped_source.root());
                    let leaf = root.leaf_at_compat(start + offset)?;

                    log::info!("style at new_leaf {leaf:?} . {style:?}");

                    let mut cursor_styles = analyze_expr(&world, &leaf)
                        .iter()
                        .filter_map(|s| s.1.clone())
                        .collect::<Vec<_>>();
                    cursor_styles.sort_by_key(|x| x.as_slice().len());
                    log::info!("style at styles {cursor_styles:?} . {style:?}");
                    let cursor_style = cursor_styles.into_iter().next_back().unwrap_or_default();
                    let cursor_style = StyleChain::new(&cursor_style);

                    log::info!("style at style {cursor_style:?} . {style:?}");

                    let style = style
                        .iter()
                        .map(|style| Self::style_at(cursor_style, style))
                        .collect();
                    let _ = world.map_shadow_by_id(
                        mapped_source.id(),
                        Bytes::new(source.text().as_bytes().to_vec()),
                    );

                    Some(InteractCodeContextResponse::StyleAt { style })
                }
            }));
        }

        Some(responses)
    }
}

impl InteractCodeContextRequest {
    fn mode_at(source: &Source, pos: usize) -> Option<InterpretMode> {
        // Smart special cases that is definitely at markup
        if pos == 0 || pos >= source.text().len() {
            return Some(InterpretMode::Markup);
        }

        // Get mode
        let root = LinkedNode::new(source.root());
        Some(interpret_mode_at(root.leaf_at_compat(pos).as_ref()))
    }

    fn style_at(cursor_style: StyleChain, style: &str) -> Option<JsonValue> {
        match style {
            "text.font" => {
                let font = cursor_style.get_cloned(TextElem::font).into_value();
                serde_json::to_value(font).ok()
            }
            _ => None,
        }
    }
}

fn eval_path_expr(
    ctx: &mut LocalContext,
    code: &str,
    inputs: Dict,
) -> Option<QueryResult<serde_json::Value>> {
    let entry = ctx.world().entry_state();
    let path = if code.starts_with("{") && code.ends_with("}") {
        let id = entry
            .select_in_workspace(Path::new("/__path__.typ"))
            .main()?;

        let inputs = make_sys(&entry, ctx.world().inputs(), inputs);
        let (inputs, root, dir, name) = match inputs {
            Some(EvalSysCtx {
                inputs,
                root,
                dir,
                name,
            }) => (Some(inputs), Some(root), dir, Some(name)),
            None => (None, None, None, None),
        };

        let mut world = ctx.world().task(tinymist_world::TaskInputs {
            entry: None,
            inputs,
        });
        // todo: bad performance
        world.take_db();
        let _ = world.map_shadow_by_id(id, Bytes::from_string(code.to_owned()));

        tinymist_analysis::upstream::with_vm((&world as &dyn World).track(), |vm| {
            define_val(vm, "join", Value::Func(join::data().into()));
            for (key, value) in [("root", root), ("dir", dir), ("name", name)] {
                if let Some(value) = value {
                    define_val(vm, key, value);
                }
            }

            let mut expr = typst::syntax::parse_code(code);
            let span = Span::from_range(id, 0..code.len());
            expr.synthesize(span);

            let expr = match expr.cast::<ast::Code>() {
                Some(v) => v,
                None => bail!(
                    "code is not a valid code expression: kind={:?}",
                    expr.kind()
                ),
            };
            match expr.eval(vm) {
                Ok(value) => serde_json::to_value(value).context_ut("failed to serialize path"),
                Err(e) => {
                    let res =
                        print_diagnostics_to_string(&world, e.iter(), DiagnosticFormat::Human);
                    let err = res.unwrap_or_else(|e| e);
                    bail!("failed to evaluate path expression: {err}")
                }
            }
        })
    } else {
        PathPattern::new(code)
            .substitute(&entry)
            .context_ut("failed to substitute path pattern")
            .and_then(|path| {
                serde_json::to_value(path.deref()).context_ut("failed to serialize path")
            })
    };
    Some(path.into())
}

#[derive(Debug, Clone, Hash)]
struct EvalSysCtx {
    inputs: Arc<LazyHash<Dict>>,
    root: Value,
    dir: Option<Value>,
    name: Value,
}

#[comemo::memoize]
fn make_sys(entry: &EntryState, base: Arc<LazyHash<Dict>>, inputs: Dict) -> Option<EvalSysCtx> {
    let root = entry.root();
    let main = entry.main();

    log::debug!("Check path {main:?} and root {root:?}");

    let (root, main) = root.zip(main)?;

    // Files in packages are not exported
    if WorkspaceResolver::is_package_file(main) {
        return None;
    }
    // Files without a path are not exported
    let path = main.vpath().resolve(&root)?;

    // todo: handle untitled path
    if path.strip_prefix("/untitled").is_ok() {
        return None;
    }

    let path = path.strip_prefix(&root).ok()?;
    let dir = path.parent();
    let file_name = path.file_name().unwrap_or_default();

    let root = Value::Str(root.to_string_lossy().into());

    let dir = dir.map(|d| Value::Str(d.to_string_lossy().into()));

    let name = file_name.to_string_lossy();
    let name = name.as_ref().strip_suffix(".typ").unwrap_or(name.as_ref());
    let name = Value::Str(name.into());

    let mut dict = base.as_ref().deref().clone();
    for (key, value) in inputs {
        dict.insert(key, value);
    }
    dict.insert("root".into(), root.clone());
    if let Some(dir) = &dir {
        dict.insert("dir".into(), dir.clone());
    }
    dict.insert("name".into(), name.clone());

    Some(EvalSysCtx {
        inputs: Arc::new(LazyHash::new(dict)),
        root,
        dir,
        name,
    })
}

fn define_val(vm: &mut Vm, name: &str, value: Value) {
    let ident = SyntaxNode::leaf(SyntaxKind::Ident, name);
    vm.define(ident.cast::<ast::Ident>().unwrap(), value);
}

#[typst_macros::func(title = "Join function")]
fn join(args: &mut Args) -> SourceResult<Value> {
    let pos = args.take().to_pos();
    let mut res = PathBuf::new();
    for arg in pos {
        match arg {
            Value::Str(s) => res.push(s.as_str()),
            _ => {
                return Err(eco_format!("join argument is not a string: {arg:?}")).at(args.span);
            }
        };
    }
    Ok(Value::Str(res.to_string_lossy().into()))
}

/// A result of a query.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum QueryResult<T> {
    /// A successful result.
    Success {
        /// The value of the result.
        value: T,
    },
    /// An error result.
    Error {
        /// The error message.
        error: EcoString,
    },
}

impl<T> QueryResult<T> {
    /// Creates a successful result.
    pub fn success(value: T) -> Self {
        Self::Success { value }
    }

    /// Creates an error result.
    pub fn error(error: EcoString) -> Self {
        Self::Error { error }
    }
}

impl<T, E: std::error::Error> From<Result<T, E>> for QueryResult<T> {
    fn from(value: Result<T, E>) -> Self {
        match value {
            Ok(value) => QueryResult::success(value),
            Err(error) => QueryResult::error(eco_format!("{error}")),
        }
    }
}

#[cfg(test)]
mod tests {
    use typst::foundations::dict;

    use super::*;
    use crate::tests::*;

    #[test]
    fn test() {
        snapshot_testing("code_context_path_at", &|ctx, path| {
            let patterns = [
                "$root/$dir/$name",
                "$root/$name",
                "$root/assets",
                "$root/assets/$name",
                r#"{ join(root, "x", dir, "y", name) }"#,
                r#"{ join(root, 1) }"#,
                r#"{ join(roo, 1) }"#,
            ];
            let inp = [
                dict! {
                    "x-path-context" => "vscode-paste",
                    "x-path-input-uri" => "https://huh.io/img.png",
                    "x-path-input-name" => "img.png",
                },
                dict! {
                    "x-path-context" => "vscode-paste",
                    "x-path-input-uri" => "https://huh.io/text.md",
                    "x-path-input-name" => "text.md",
                },
            ];

            let cases = patterns
                .iter()
                .map(|pat| (*pat, inp[0].clone()))
                .chain(inp.iter().map(|inp| {
                    (
                        r#"{ import "/resolve.typ": resolve; resolve(join, root, dir, name) }"#,
                        inp.clone(),
                    )
                }));

            let result = cases
                .map(|(code, inputs)| {
                    let request = InteractCodeContextRequest {
                        path: path.clone(),
                        query: vec![Some(InteractCodeContextQuery::PathAt {
                            code: code.to_string(),
                            inputs: inputs.clone(),
                        })],
                    };
                    json!({ "code": code, "inputs": inputs, "response": request.request(ctx) })
                })
                .collect::<Vec<_>>();
            assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
        });
    }
}