tinymist_debug/
cov.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
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
//! Tinymist coverage support for Typst.
use core::fmt;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock};

use parking_lot::Mutex;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use tinymist_analysis::location::PositionEncoding;
use tinymist_std::hash::FxHashMap;
use tinymist_world::debug_loc::LspRange;
use tinymist_world::vfs::{FileId, WorkspaceResolver};
use tinymist_world::{CompilerFeat, CompilerWorld};
use typst::diag::FileResult;
use typst::foundations::func;
use typst::syntax::ast::AstNode;
use typst::syntax::{ast, Source, Span, SyntaxNode};
use typst::{World, WorldExt};

use crate::instrument::Instrumenter;

/// The coverage result.
pub struct CoverageResult {
    /// The coverage meta.
    pub meta: FxHashMap<FileId, Arc<InstrumentMeta>>,
    /// The coverage map.
    pub regions: FxHashMap<FileId, CovRegion>,
}

impl CoverageResult {
    /// Converts the coverage result to JSON.
    pub fn to_json<F: CompilerFeat>(&self, w: &CompilerWorld<F>) -> serde_json::Value {
        let lsp_position_encoding = PositionEncoding::Utf16;

        let mut result = VscodeCoverage::new();

        for (file_id, region) in &self.regions {
            let file_path = w
                .path_for_id(*file_id)
                .unwrap()
                .as_path()
                .to_str()
                .unwrap()
                .to_string();

            let mut details = vec![];

            let meta = self.meta.get(file_id).unwrap();

            let Ok(typst_source) = w.source(*file_id) else {
                continue;
            };

            let hits = region.hits.lock();
            for (idx, (span, _kind)) in meta.meta.iter().enumerate() {
                let Some(typst_range) = w.range(*span) else {
                    continue;
                };

                let rng = tinymist_analysis::location::to_lsp_range(
                    typst_range,
                    &typst_source,
                    lsp_position_encoding,
                );

                details.push(VscodeFileCoverageDetail {
                    executed: hits[idx] > 0,
                    location: rng,
                });
            }

            result.insert(file_path, details);
        }

        serde_json::to_value(result).unwrap()
    }

    /// Summarizes the coverage result.
    pub fn summarize<'a>(&'a self, short: bool, prefix: &'a str) -> SummarizedCoverage<'a> {
        SummarizedCoverage {
            prefix,
            result: self,
            short,
        }
    }
}

pub struct SummarizedCoverage<'a> {
    prefix: &'a str,
    result: &'a CoverageResult,
    short: bool,
}

impl SummarizedCoverage<'_> {
    fn line(
        &self,
        f: &mut fmt::Formatter<'_>,
        name: &str,
        total: usize,
        cov: usize,
        is_summary: bool,
    ) -> fmt::Result {
        let pre = self.prefix;
        let r = if total == 0 {
            100.0
        } else {
            cov as f64 / total as f64 * 100.0
        };
        if is_summary {
            write!(f, "{pre}{name} {cov}/{total} ({r:.2}%)")
        } else {
            let r = format!("{r:.2}");
            writeln!(f, "{pre} {cov:<5} / {total:<5} ({r:>6}%)  {name}")
        }
    }
}

impl fmt::Display for SummarizedCoverage<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut ids = self.result.regions.keys().collect::<Vec<_>>();
        ids.sort_by(|a, b| {
            a.package()
                .map(crate::PackageSpecCmp::from)
                .cmp(&b.package().map(crate::PackageSpecCmp::from))
                .then_with(|| a.vpath().cmp(b.vpath()))
        });

        let summary = ids
            .par_iter()
            .flat_map(|&id| {
                let region = self.result.regions.get(id)?;
                let meta = self.result.meta.get(id)?;

                let hits = region.hits.lock();
                let region_covered = hits.par_iter().filter(|&&x| x > 0).count();

                Some((id, region_covered, meta.meta.len()))
            })
            .collect::<Vec<_>>();

        let total = summary.iter().map(|(_, _, l)| l).sum::<usize>();
        let covered = summary.iter().map(|(_, c, _)| c).sum::<usize>();

        if !self.short {
            for (id, covered, total) in summary {
                let id = format!("{:?}", WorkspaceResolver::display(Some(*id)));
                self.line(f, &id, total, covered, false)?;
            }
        }
        self.line(f, "Coverage Summary", total, covered, true)?;

        Ok(())
    }
}

/// The coverage result in the format of the VSCode coverage data.
pub type VscodeCoverage = HashMap<String, Vec<VscodeFileCoverageDetail>>;

/// Converts the coverage result to the VSCode coverage data.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct VscodeFileCoverageDetail {
    /// Whether the location is being executed
    pub executed: bool,
    /// The location of the coverage.
    pub location: LspRange,
}

#[derive(Default)]
pub struct CovInstr {
    /// The coverage map.
    pub map: Mutex<FxHashMap<FileId, Arc<InstrumentMeta>>>,
}

impl Instrumenter for CovInstr {
    fn instrument(&self, _source: Source) -> FileResult<Source> {
        let (new, meta) = instrument_coverage(_source)?;
        let region = CovRegion {
            hits: Arc::new(Mutex::new(vec![0; meta.meta.len()])),
        };

        let mut map = self.map.lock();
        map.insert(new.id(), meta);

        let mut cov_map = COVERAGE_MAP.lock();
        cov_map.regions.insert(new.id(), region);

        Ok(new)
    }
}

/// The coverage map.
#[derive(Default)]
pub struct CoverageMap {
    last_hit: Option<(FileId, CovRegion)>,
    /// The coverage map.
    pub regions: FxHashMap<FileId, CovRegion>,
}

/// The coverage region
#[derive(Default, Clone)]
pub struct CovRegion {
    /// The hits
    pub hits: Arc<Mutex<Vec<u8>>>,
}

pub static COVERAGE_LOCK: LazyLock<Mutex<()>> = LazyLock::new(Mutex::default);
pub static COVERAGE_MAP: LazyLock<Mutex<CoverageMap>> = LazyLock::new(Mutex::default);

#[func(name = "__cov_pc", title = "Coverage function")]
pub fn __cov_pc(span: Span, pc: i64) {
    let Some(fid) = span.id() else {
        return;
    };
    let mut map = COVERAGE_MAP.lock();
    if let Some(last_hit) = map.last_hit.as_ref() {
        if last_hit.0 == fid {
            let mut hits = last_hit.1.hits.lock();
            let c = &mut hits[pc as usize];
            *c = c.saturating_add(1);
            return;
        }
    }

    let region = map.regions.entry(fid).or_default();
    {
        let mut hits = region.hits.lock();
        let c = &mut hits[pc as usize];
        *c = c.saturating_add(1);
    }
    map.last_hit = Some((fid, region.clone()));
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    OpenBrace,
    CloseBrace,
    Show,
}

#[derive(Default)]
pub struct InstrumentMeta {
    pub meta: Vec<(Span, Kind)>,
}

#[comemo::memoize]
fn instrument_coverage(source: Source) -> FileResult<(Source, Arc<InstrumentMeta>)> {
    let node = source.root();
    let mut worker = InstrumentWorker {
        meta: InstrumentMeta::default(),
        instrumented: String::new(),
    };

    worker.visit_node(node);
    let new_source: Source = Source::new(source.id(), worker.instrumented);

    Ok((new_source, Arc::new(worker.meta)))
}

struct InstrumentWorker {
    meta: InstrumentMeta,
    instrumented: String,
}

impl InstrumentWorker {
    fn instrument_block_child(&mut self, container: &SyntaxNode, b1: Span, b2: Span) {
        for child in container.children() {
            if b1 == child.span() || b2 == child.span() {
                self.instrument_block(child);
            } else {
                self.visit_node(child);
            }
        }
    }

    fn visit_node(&mut self, node: &SyntaxNode) {
        if let Some(expr) = node.cast::<ast::Expr>() {
            match expr {
                ast::Expr::Code(..) => {
                    self.instrument_block(node);
                    return;
                }
                ast::Expr::While(while_expr) => {
                    self.instrument_block_child(node, while_expr.body().span(), Span::detached());
                    return;
                }
                ast::Expr::For(for_expr) => {
                    self.instrument_block_child(node, for_expr.body().span(), Span::detached());
                    return;
                }
                ast::Expr::Conditional(cond_expr) => {
                    self.instrument_block_child(
                        node,
                        cond_expr.if_body().span(),
                        cond_expr.else_body().unwrap_or_default().span(),
                    );
                    return;
                }
                ast::Expr::Closure(closure) => {
                    self.instrument_block_child(node, closure.body().span(), Span::detached());
                    return;
                }
                ast::Expr::Show(show_rule) => {
                    let transform = show_rule.transform().to_untyped().span();
                    let is_set = matches!(show_rule.transform(), ast::Expr::Set(..));

                    for child in node.children() {
                        if transform == child.span() {
                            if is_set {
                                self.instrument_show_set(child);
                            } else {
                                self.instrument_show_transform(child);
                            }
                        } else {
                            self.visit_node(child);
                        }
                    }
                    return;
                }
                ast::Expr::Contextual(body) => {
                    self.instrumented.push_str("context (");
                    self.visit_node(body.body().to_untyped());
                    self.instrumented.push(')');
                    return;
                }
                ast::Expr::Text(..)
                | ast::Expr::Space(..)
                | ast::Expr::Linebreak(..)
                | ast::Expr::Parbreak(..)
                | ast::Expr::Escape(..)
                | ast::Expr::Shorthand(..)
                | ast::Expr::SmartQuote(..)
                | ast::Expr::Strong(..)
                | ast::Expr::Emph(..)
                | ast::Expr::Raw(..)
                | ast::Expr::Link(..)
                | ast::Expr::Label(..)
                | ast::Expr::Ref(..)
                | ast::Expr::Heading(..)
                | ast::Expr::List(..)
                | ast::Expr::Enum(..)
                | ast::Expr::Term(..)
                | ast::Expr::Equation(..)
                | ast::Expr::Math(..)
                | ast::Expr::MathText(..)
                | ast::Expr::MathIdent(..)
                | ast::Expr::MathShorthand(..)
                | ast::Expr::MathAlignPoint(..)
                | ast::Expr::MathDelimited(..)
                | ast::Expr::MathAttach(..)
                | ast::Expr::MathPrimes(..)
                | ast::Expr::MathFrac(..)
                | ast::Expr::MathRoot(..)
                | ast::Expr::Ident(..)
                | ast::Expr::None(..)
                | ast::Expr::Auto(..)
                | ast::Expr::Bool(..)
                | ast::Expr::Int(..)
                | ast::Expr::Float(..)
                | ast::Expr::Numeric(..)
                | ast::Expr::Str(..)
                | ast::Expr::Content(..)
                | ast::Expr::Parenthesized(..)
                | ast::Expr::Array(..)
                | ast::Expr::Dict(..)
                | ast::Expr::Unary(..)
                | ast::Expr::Binary(..)
                | ast::Expr::FieldAccess(..)
                | ast::Expr::FuncCall(..)
                | ast::Expr::Let(..)
                | ast::Expr::DestructAssign(..)
                | ast::Expr::Set(..)
                | ast::Expr::Import(..)
                | ast::Expr::Include(..)
                | ast::Expr::Break(..)
                | ast::Expr::Continue(..)
                | ast::Expr::Return(..) => {}
            }
        }

        self.visit_node_fallback(node);
    }

    fn visit_node_fallback(&mut self, node: &SyntaxNode) {
        let txt = node.text();
        if !txt.is_empty() {
            self.instrumented.push_str(txt);
        }

        for child in node.children() {
            self.visit_node(child);
        }
    }

    fn make_cov(&mut self, span: Span, kind: Kind) {
        let it = self.meta.meta.len();
        self.meta.meta.push((span, kind));
        self.instrumented.push_str("__cov_pc(");
        self.instrumented.push_str(&it.to_string());
        self.instrumented.push_str(");\n");
    }

    fn instrument_block(&mut self, child: &SyntaxNode) {
        self.instrumented.push_str("{\n");
        let (first, last) = {
            let mut children = child.children();
            let first = children
                .next()
                .map(|s| s.span())
                .unwrap_or_else(Span::detached);
            let last = children
                .last()
                .map(|s| s.span())
                .unwrap_or_else(Span::detached);

            (first, last)
        };
        self.make_cov(first, Kind::OpenBrace);
        self.visit_node_fallback(child);
        self.instrumented.push('\n');
        self.make_cov(last, Kind::CloseBrace);
        self.instrumented.push('}');
    }

    fn instrument_show_set(&mut self, child: &SyntaxNode) {
        self.instrumented.push_str("__it => {");
        self.make_cov(child.span(), Kind::Show);
        self.visit_node(child);
        self.instrumented.push_str("\n__it; }\n");
    }

    fn instrument_show_transform(&mut self, child: &SyntaxNode) {
        self.instrumented.push_str("{\nlet __cov_show_body = ");
        let s = child.span();
        self.visit_node(child);
        self.instrumented.push_str("\n__it => {");
        self.make_cov(s, Kind::Show);
        self.instrumented
            .push_str("if type(__cov_show_body) == function { __cov_show_body(__it); } else { __cov_show_body } } }\n");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn instr(input: &str) -> String {
        let source = Source::detached(input);
        let (new, _meta) = instrument_coverage(source).unwrap();
        new.text().to_string()
    }

    #[test]
    fn test_physica_vector() {
        let instrumented = instr(include_str!("fixtures/instr_coverage/physica_vector.typ"));
        insta::assert_snapshot!(instrumented, @r###"
        // A show rule, should be used like:
        //   #show: super-plus-as-dagger
        //   U^+U = U U^+ = I
        // or in scope:
        //   #[
        //     #show: super-plus-as-dagger
        //     U^+U = U U^+ = I
        //   ]
        #let super-plus-as-dagger(document) = {
        __cov_pc(0);
        {
          show math.attach: {
        let __cov_show_body = elem => {
        __cov_pc(1);
        {
            if __eligible(elem.base) and elem.at("t", default: none) == [+] {
        __cov_pc(2);
        {
              $attach(elem.base, t: dagger, b: elem.at("b", default: #none))$
            }
        __cov_pc(3);
        } else {
        __cov_pc(4);
        {
              elem
            }
        __cov_pc(5);
        }
          }
        __cov_pc(6);
        }
        __it => {__cov_pc(7);
        if type(__cov_show_body) == function { __cov_show_body(__it); } else { __cov_show_body } } }


          document
        }
        __cov_pc(8);
        }
        "###);
    }

    #[test]
    fn test_playground() {
        let instrumented = instr(include_str!("fixtures/instr_coverage/playground.typ"));
        insta::assert_snapshot!(instrumented, @"");
    }

    #[test]
    fn test_instrument_coverage() {
        let source = Source::detached("#let a = 1;");
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @"#let a = 1;");
    }

    #[test]
    fn test_instrument_coverage_show_content() {
        let source = Source::detached("#show math.equation: context it => it");
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @r###"
        #show math.equation: {
        let __cov_show_body = context (it => {
        __cov_pc(0);
        it
        __cov_pc(1);
        })
        __it => {__cov_pc(2);
        if type(__cov_show_body) == function { __cov_show_body(__it); } else { __cov_show_body } } }
        "###);
    }

    #[test]
    fn test_instrument_inline_block() {
        let source = Source::detached("#let main-size = {1} + 2 + {3}");
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @r###"
        #let main-size = {
        __cov_pc(0);
        {1}
        __cov_pc(1);
        } + 2 + {
        __cov_pc(2);
        {3}
        __cov_pc(3);
        }
        "###);
    }

    #[test]
    fn test_instrument_if() {
        let source = Source::detached(
            "#let main-size = if is-web-target {
  16pt
} else {
  10.5pt
}",
        );
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @r###"
        #let main-size = if is-web-target {
        __cov_pc(0);
        {
          16pt
        }
        __cov_pc(1);
        } else {
        __cov_pc(2);
        {
          10.5pt
        }
        __cov_pc(3);
        }
        "###);
    }

    #[test]
    fn test_instrument_coverage_nested() {
        let source = Source::detached("#let a = {1};");
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @r###"
        #let a = {
        __cov_pc(0);
        {1}
        __cov_pc(1);
        };
        "###);
    }

    #[test]
    fn test_instrument_coverage_functor() {
        let source = Source::detached("#show: main");
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @r###"
        #show: {
        let __cov_show_body = main
        __it => {__cov_pc(0);
        if type(__cov_show_body) == function { __cov_show_body(__it); } else { __cov_show_body } } }
        "###);
    }

    #[test]
    fn test_instrument_coverage_set() {
        let source = Source::detached("#show raw: set text(12pt)");
        let (new, _meta) = instrument_coverage(source).unwrap();
        insta::assert_snapshot!(new.text(), @r###"
        #show raw: __it => {__cov_pc(0);
        set text(12pt)
        __it; }
        "###);
    }
}