panache 3.12.0

Language server, formatter, and linter for Markdown, Quarto, and R Markdown
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Quarto execution units with occurrence-preserving source ownership.
//!
//! Linter adapters map into an arena of original document occurrences. Each
//! occurrence occupies a disjoint range, even when a partial is included twice.
//! Projection then restores file-local ranges and checks fix agreement before
//! hosts see diagnostics. This preserves the adapters' single-input contract.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::hash::{Hash, Hasher};
use std::ops::Range;
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};

use rowan::{TextRange, TextSize};

use crate::config::Flavor;
use crate::salsa::{Db, ExternalLintJob, FileConfig, FileText, SalsaDb};
use crate::utils::CodeSnippet;

use super::code_block_collector::concatenate_for_lint;
use super::diagnostics::{Diagnostic, DiagnosticNoteKind, Fix, Location};

#[derive(Clone, Copy)]
pub struct ExecutionRoot {
    pub file: FileText,
    pub config: FileConfig,
    /// A render target is also executed independently when another file includes it.
    pub render_target: bool,
}

#[derive(Debug, Clone)]
pub struct SourceOccurrence {
    pub path: PathBuf,
    pub text: Arc<str>,
    pub arena_range: Range<usize>,
    pub parent: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct SourceDiagnostic {
    pub path: PathBuf,
    pub diagnostic: Diagnostic,
    pub include_target: Option<PathBuf>,
}

#[derive(Debug, Clone, Default)]
pub struct ExecutionPlan {
    pub root: PathBuf,
    pub sources: Vec<SourceOccurrence>,
    pub dependencies: BTreeSet<PathBuf>,
    pub diagnostics: Vec<SourceDiagnostic>,
    pub original_input: String,
    pub jobs: Vec<ExternalLintJob>,
    pub complete: bool,
    pub config_key: String,
}

/// Normalize `.` and `..` without resolving symlinks or reading the filesystem.
/// VFS identities retain the spelling used by the host, including symlink roots.
pub fn normalize_path(path: &Path) -> PathBuf {
    let mut result = PathBuf::new();
    for part in path.components() {
        match part {
            Component::CurDir => {}
            Component::ParentDir if result.file_name().is_some_and(|name| name != "..") => {
                result.pop();
            }
            Component::ParentDir if result.has_root() => {}
            part => result.push(part.as_os_str()),
        }
    }
    result
}

#[salsa::tracked(returns(ref), no_eq, unsafe(non_salsa_values), lru = 512)]
pub fn execution_plan(db: &dyn Db, file: FileText, config: FileConfig) -> ExecutionPlan {
    let _ = db.file_set().ids(db);
    let Some(path) = db.path_of(file) else {
        return ExecutionPlan::default();
    };
    let root = normalize_path(&path);
    let base = root.parent().unwrap_or(Path::new(".")).to_path_buf();
    let project = crate::includes::find_quarto_root(&root).unwrap_or_else(|| base.clone());
    let mut plan = ExecutionPlan {
        root,
        complete: true,
        config_key: format!("{:?}", config.config(db)),
        ..Default::default()
    };
    let mut snippets = BTreeMap::<String, Vec<CodeSnippet>>::new();
    let mut active = BTreeSet::new();
    collect(
        db,
        file,
        config,
        &base,
        &project,
        None,
        &mut plan,
        &mut snippets,
        &mut active,
    );
    if plan.complete {
        let mut linters: Vec<_> = config.config(db).linters.iter().collect();
        linters.sort_by_key(|(language, _)| *language);
        for (language, linter) in linters {
            let Some(blocks) = snippets.get(language) else {
                continue;
            };
            for joined in concatenate_for_lint(blocks, config.config(db).flavor) {
                plan.jobs.push(ExternalLintJob {
                    language: language.clone(),
                    linter_name: linter.clone(),
                    content: joined.content,
                    mappings: joined.mappings,
                });
            }
        }
    }
    plan
}

#[allow(clippy::too_many_arguments)]
fn collect(
    db: &dyn Db,
    file: FileText,
    config: FileConfig,
    base: &Path,
    project: &Path,
    parent: Option<usize>,
    plan: &mut ExecutionPlan,
    snippets: &mut BTreeMap<String, Vec<CodeSnippet>>,
    active: &mut BTreeSet<PathBuf>,
) {
    db.unwind_if_revision_cancelled();
    let path = normalize_path(&db.path_of(file).expect("execution sources have paths"));
    plan.dependencies.insert(path.clone());
    let Some(text) = file.text(db).clone() else {
        plan.complete = false;
        return;
    };
    active.insert(path.clone());
    let base_offset = plan.original_input.len();
    plan.original_input.push_str(&text);
    // Unmapped separators keep a boundary insertion owned by one occurrence.
    plan.original_input.push('\n');
    let occurrence = plan.sources.len();
    plan.sources.push(SourceOccurrence {
        path: path.clone(),
        text: text.clone(),
        arena_range: base_offset..base_offset + text.len(),
        parent,
    });
    let tree = crate::salsa::parsed_tree_root(db, file, config);
    enum Event {
        Snippet(CodeSnippet),
        Include(crate::includes::IncludeOccurrence),
    }
    let mut events: Vec<_> = crate::utils::collect_code_snippets(&tree, &text)
        .into_values()
        .flatten()
        .map(|s| (s.original_range.start, Event::Snippet(s)))
        .collect();
    if config.config(db).flavor == Flavor::Quarto {
        events.extend(
            crate::includes::include_occurrences(&tree, base, Some(project), config.config(db))
                .into_iter()
                .map(|i| (usize::from(i.range.start()), Event::Include(i))),
        );
    }
    events.sort_by_key(|(offset, _)| *offset);
    for (_, event) in events {
        match event {
            Event::Snippet(mut snippet) => {
                snippet.original_range.start += base_offset;
                snippet.original_range.end += base_offset;
                for offset in &mut snippet.line_starts {
                    *offset += base_offset;
                }
                snippets
                    .entry(snippet.language.clone())
                    .or_default()
                    .push(snippet);
            }
            Event::Include(include) => {
                let target = normalize_path(&include.path);
                plan.dependencies.insert(target.clone());
                let child = db.file_text(target.clone());
                let diagnostic = if active.contains(&target) {
                    Some(crate::includes::include_cycle_diagnostic(
                        &text,
                        include.range,
                        &target,
                    ))
                } else if child.is_none_or(|child| child.text(db).is_none()) {
                    Some(crate::includes::include_not_found_diagnostic(
                        &text,
                        include.range,
                        &target,
                    ))
                } else {
                    None
                };
                if let Some(diagnostic) = diagnostic {
                    plan.complete = false;
                    plan.diagnostics.push(SourceDiagnostic {
                        path: path.clone(),
                        diagnostic,
                        include_target: Some(target),
                    });
                } else if let Some(child) = child {
                    collect(
                        db,
                        child,
                        config,
                        base,
                        project,
                        Some(occurrence),
                        plan,
                        snippets,
                        active,
                    );
                }
            }
        }
    }
    active.remove(&path);
}

/// Load execution dependencies on the writer, preserving already loaded buffers.
pub fn load_execution_sources(db: &mut SalsaDb, roots: &[ExecutionRoot]) -> LoadedExecutionSources {
    loop {
        let mut paths = BTreeSet::new();
        for root in roots {
            paths.extend(
                execution_plan(db, root.file, root.config)
                    .dependencies
                    .iter()
                    .cloned(),
            );
        }
        let mut changed = false;
        for path in &paths {
            let was_known = db.file_text_if_cached(path).is_some();
            let id = db.intern_file(Some(path.clone()));
            changed |= db.load_file_from_disk(id) || !was_known;
        }
        if !changed {
            let read_errors = paths
                .iter()
                .filter(|path| !db.file_text_is_loaded(path))
                .filter_map(|path| {
                    let error = std::fs::read_to_string(path).err()?;
                    (!matches!(
                        error.kind(),
                        std::io::ErrorKind::NotFound | std::io::ErrorKind::IsADirectory
                    ))
                    .then(|| (path.clone(), error.to_string()))
                })
                .collect();
            return LoadedExecutionSources { paths, read_errors };
        }
    }
}

#[derive(Default)]
pub struct LoadedExecutionSources {
    pub paths: BTreeSet<PathBuf>,
    pub read_errors: BTreeMap<PathBuf, String>,
}

#[derive(Debug, Clone, Default)]
pub struct ExecutionBatch {
    pub plans: Vec<ExecutionPlan>,
    /// Source closures for explicitly selected documents, including partials.
    pub scopes: BTreeMap<PathBuf, BTreeSet<PathBuf>>,
}

impl ExecutionBatch {
    /// Attach writer-observed I/O failures without reading files on workers.
    pub fn with_read_errors(mut self, errors: &BTreeMap<PathBuf, String>) -> Self {
        for plan in &mut self.plans {
            for issue in &mut plan.diagnostics {
                if issue.diagnostic.code != "include-not-found" {
                    continue;
                }
                let Some(target) = &issue.include_target else {
                    continue;
                };
                let Some(error) = errors.get(target) else {
                    continue;
                };
                let Some(source) = plan.sources.iter().find(|source| source.path == issue.path)
                else {
                    continue;
                };
                issue.diagnostic = crate::includes::include_read_error_diagnostic(
                    &source.text,
                    issue.diagnostic.location.range,
                    target,
                    error,
                );
            }
        }
        self
    }

    pub fn new(db: &dyn Db, roots: &[ExecutionRoot], targets: &[PathBuf]) -> Self {
        let candidates: Vec<_> = roots
            .iter()
            .map(|root| (root, execution_plan(db, root.file, root.config)))
            .collect();
        let mut plans: Vec<ExecutionPlan> = candidates
            .iter()
            .filter(|(root, plan)| {
                root.render_target
                    || !candidates.iter().any(|(_, other)| {
                        other.root != plan.root
                            && other.sources.iter().skip(1).any(|s| s.path == plan.root)
                    })
            })
            .map(|(_, plan)| (*plan).clone())
            .collect();
        // A cyclic component has no parentless candidate, even when unrelated
        // roots survive selection. Retain a selected entry into each such
        // component so its include-cycle diagnostic cannot disappear.
        for target in targets {
            let target = normalize_path(target);
            if !plans
                .iter()
                .any(|plan| plan.sources.iter().any(|source| source.path == target))
                && let Some((_, plan)) = candidates.iter().find(|(_, plan)| plan.root == target)
            {
                plans.push((*plan).clone());
            }
        }
        plans.sort_by(|a, b| a.root.cmp(&b.root));
        plans.dedup_by(|a, b| a.root == b.root && a.config_key == b.config_key);
        let mut scopes = BTreeMap::new();
        for target in targets {
            let target = normalize_path(target);
            let mut scope = BTreeSet::from([target.clone()]);
            for plan in &plans {
                let mut inside = Vec::new();
                for source in &plan.sources {
                    let selected =
                        source.path == target || source.parent.is_some_and(|p| inside[p]);
                    inside.push(selected);
                    if selected {
                        scope.insert(source.path.clone());
                    }
                }
            }
            scopes.insert(target, scope);
        }
        let visible: BTreeSet<_> = scopes.values().flatten().cloned().collect();
        plans.retain(|plan| {
            plan.sources
                .iter()
                .any(|source| visible.contains(&source.path))
        });
        Self { plans, scopes }
    }

    pub fn fingerprint(&self) -> String {
        let mut hash = std::collections::hash_map::DefaultHasher::new();
        for plan in &self.plans {
            plan.root.hash(&mut hash);
            plan.config_key.hash(&mut hash);
            plan.complete.hash(&mut hash);
            plan.dependencies.hash(&mut hash);
            plan.original_input.hash(&mut hash);
            for issue in &plan.diagnostics {
                issue.diagnostic.message.hash(&mut hash);
            }
        }
        self.scopes.hash(&mut hash);
        format!("{:016x}", hash.finish())
    }

    pub fn source_text(&self, path: &Path) -> Option<&str> {
        self.plans
            .iter()
            .flat_map(|p| &p.sources)
            .find(|s| s.path == path)
            .map(|s| s.text.as_ref())
    }

    pub fn run(&self) -> ExecutionResults {
        let registry = super::external_linters::ExternalLinterRegistry::new();
        let missing = super::external_linters::find_missing_linter_commands(
            self.plans
                .iter()
                .flat_map(|p| &p.jobs)
                .map(|j| j.linter_name.as_str()),
            &registry,
        );
        super::external_linters::log_missing_linter_commands(&missing);
        let mut results = Vec::new();
        for plan in &self.plans {
            let mut diagnostics = Vec::new();
            let mut complete = plan.complete;
            for job in &plan.jobs {
                if registry
                    .get(&job.linter_name)
                    .is_some_and(|tool| missing.contains(tool.command))
                {
                    complete = false;
                    continue;
                }
                let _permit = crate::external_tools_common::acquire_external_tool_permit();
                match super::external_linters_sync::run_linter_sync(
                    &job.linter_name,
                    &job.language,
                    &job.content,
                    &plan.original_input,
                    &registry,
                    Some(&job.mappings),
                ) {
                    Ok(items) => diagnostics.extend(
                        items
                            .into_iter()
                            .filter_map(|d| project_diagnostic(plan, d)),
                    ),
                    Err(error) => {
                        complete = false;
                        log::warn!("External linter '{}' failed: {error}", job.linter_name);
                    }
                }
            }
            results.push((complete, diagnostics));
        }
        self.merge(results)
    }

    fn merge(&self, results: Vec<(bool, Vec<(usize, Diagnostic)>)>) -> ExecutionResults {
        let mut out = ExecutionResults {
            successful: results.iter().all(|(complete, _)| *complete),
            ..Default::default()
        };
        let visible: BTreeSet<_> = self.scopes.values().flatten().collect();
        for (plan_index, plan) in self.plans.iter().enumerate() {
            for issue in &plan.diagnostics {
                if visible.contains(&issue.path) {
                    let items = out.diagnostics.entry(issue.path.clone()).or_default();
                    if !items.contains(&issue.diagnostic) {
                        items.push(issue.diagnostic.clone());
                    }
                }
            }
            for (occurrence, original) in &results[plan_index].1 {
                let source = &plan.sources[*occurrence];
                if !visible.contains(&source.path) {
                    continue;
                }
                let mut diagnostic = original.clone();
                if let Some(fix) = &diagnostic.fix {
                    let agreed = self.plans.iter().enumerate().all(|(idx, other)| {
                        other
                            .sources
                            .iter()
                            .enumerate()
                            .filter(|(_, s)| s.path == source.path)
                            .all(|(occ, _)| {
                                results[idx].0
                                    && results[idx].1.iter().any(|(candidate_occ, candidate)| {
                                        *candidate_occ == occ
                                            && candidate
                                                .fix
                                                .as_ref()
                                                .is_some_and(|candidate| same_edits(fix, candidate))
                                    })
                            })
                    });
                    if !agreed {
                        diagnostic.fix = None;
                    }
                }
                let items = out.diagnostics.entry(source.path.clone()).or_default();
                let matching = items.iter().position(|d| {
                    d.code == diagnostic.code
                        && d.severity == diagnostic.severity
                        && d.location == diagnostic.location
                        && d.message == diagnostic.message
                });
                let idx = matching.unwrap_or_else(|| {
                    items.push(diagnostic);
                    items.len() - 1
                });
                if plan.root != source.path {
                    let note = super::diagnostics::DiagnosticNote {
                        kind: DiagnosticNoteKind::Note,
                        message: format!("In execution context: {}", plan.root.display()),
                    };
                    if !items[idx].notes.contains(&note) {
                        items[idx].notes.push(note);
                    }
                }
            }
        }
        for items in out.diagnostics.values_mut() {
            items.sort_by(|a, b| {
                (a.location.range.start(), &a.code, &a.message).cmp(&(
                    b.location.range.start(),
                    &b.code,
                    &b.message,
                ))
            });
        }
        out
    }
}

fn same_edits(a: &Fix, b: &Fix) -> bool {
    a.safety == b.safety && a.edits == b.edits
}

fn local_range(range: TextRange, source: &SourceOccurrence) -> Option<TextRange> {
    let start = usize::from(range.start());
    let end = usize::from(range.end());
    if start < source.arena_range.start || end > source.arena_range.end {
        return None;
    }
    let start = start - source.arena_range.start;
    let end = end - source.arena_range.start;
    if !source.text.is_char_boundary(start) || !source.text.is_char_boundary(end) {
        return None;
    }
    Some(TextRange::new(
        TextSize::try_from(start).ok()?,
        TextSize::try_from(end).ok()?,
    ))
}

fn project_diagnostic(
    plan: &ExecutionPlan,
    mut diagnostic: Diagnostic,
) -> Option<(usize, Diagnostic)> {
    let (index, source, range) = plan.sources.iter().enumerate().find_map(|(idx, source)| {
        Some((idx, source, local_range(diagnostic.location.range, source)?))
    })?;
    diagnostic.location = Location::from_range(range, &source.text);
    diagnostic.fix = diagnostic.fix.and_then(|mut fix| {
        for edit in &mut fix.edits {
            edit.range = local_range(edit.range, source)?;
        }
        fix.edits.sort_by_key(|edit| {
            (
                edit.range.start(),
                edit.range.end(),
                edit.replacement.clone(),
            )
        });
        fix.edits.dedup();
        Some(fix)
    });
    Some((index, diagnostic))
}

#[derive(Debug, Clone, Default)]
pub struct ExecutionResults {
    pub diagnostics: BTreeMap<PathBuf, Vec<Diagnostic>>,
    pub successful: bool,
}

/// Host-owned subprocess cache. Salsa only memoizes pure execution plans.
#[derive(Default)]
pub struct ExecutionCache(Mutex<HashMap<String, Arc<OnceLock<Arc<ExecutionResults>>>>>);

impl ExecutionCache {
    pub fn get_or_run(&self, batch: &ExecutionBatch, run: bool) -> Arc<ExecutionResults> {
        let key = batch.fingerprint();
        let slot = {
            let mut entries = self.0.lock().unwrap_or_else(|error| error.into_inner());
            // Bound retained source histories in long-running language servers.
            if entries.len() >= 64 && !entries.contains_key(&key) {
                entries.clear();
            }
            entries.entry(key.clone()).or_default().clone()
        };
        if !run {
            return slot.get().cloned().unwrap_or_else(|| {
                Arc::new(batch.merge(batch.plans.iter().map(|_| (false, Vec::new())).collect()))
            });
        }
        let result = slot.get_or_init(|| Arc::new(batch.run())).clone();
        if !result.successful {
            self.0
                .lock()
                .unwrap_or_else(|error| error.into_inner())
                .remove(&key);
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::super::diagnostics::Edit;
    use super::*;
    use crate::config::{Config, Extensions};

    fn config(db: &SalsaDb) -> FileConfig {
        FileConfig::new(
            db,
            Config {
                flavor: Flavor::Quarto,
                extensions: Extensions::for_flavor(Flavor::Quarto),
                linters: HashMap::from([("r".into(), "arity".into())]),
                ..Default::default()
            },
        )
    }

    fn root(db: &mut SalsaDb, config: FileConfig, path: &str, text: &str) -> ExecutionRoot {
        let file = db.update_file_text(PathBuf::from(path), text.to_owned());
        ExecutionRoot {
            file,
            config,
            render_target: false,
        }
    }

    #[test]
    fn include_order_nested_paths_and_repeated_occurrences() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let parent = root(
            &mut db,
            config,
            "/execution/main.qmd",
            "`r before`\n\n{{< include parts/_one.qmd >}}\n\n`r between`\n\n{{< include parts/_one.qmd >}}\n\n`r after`\n",
        );
        root(
            &mut db,
            config,
            "/execution/parts/_one.qmd",
            "`r first`\n\n{{< include \"_two words.qmd\" >}}\n",
        );
        root(&mut db, config, "/execution/_two words.qmd", "`r second`\n");
        let plan = execution_plan(&db, parent.file, config);
        assert!(plan.complete);
        assert_eq!(plan.sources.len(), 5);
        let job = &plan.jobs[0];
        let mut cursor = 0;
        for word in [
            "before", "first", "second", "between", "first", "second", "after",
        ] {
            cursor += job.content[cursor..].find(word).unwrap() + word.len();
        }
        assert_ne!(plan.sources[1].arena_range, plan.sources[3].arena_range);
    }

    #[test]
    fn quarto_displayed_examples_are_separate_in_both_lint_plans() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let parent = root(
            &mut db,
            config,
            "/execution/main.qmd",
            "```r\ndisplayed\n```\n\n```{r}\n#| eval: false\ndisabled\n```\n\n```{r}\nactive\n```\n\n`r use`\n",
        );
        let plan = execution_plan(&db, parent.file, config);
        let local = crate::salsa::built_in_lint_plan(&db, parent.file, config);
        for jobs in [&plan.jobs, &local.external_jobs] {
            assert_eq!(jobs.len(), 3);
            assert!(jobs[0].content.contains("active"));
            assert!(jobs[0].content.contains("use"));
            assert!(!jobs[0].content.contains("displayed"));
            assert!(!jobs[0].content.contains("disabled"));
            assert_eq!(jobs[1].content.trim(), "displayed");
            assert_eq!(jobs[2].content.trim(), "disabled");
        }
    }

    #[test]
    fn missing_include_invalidates_when_loaded_and_cycles_stop_jobs() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let parent = root(
            &mut db,
            config,
            "/execution/main.qmd",
            "{{< include _child.qmd >}}\n\n`r value`\n",
        );
        let missing = execution_plan(&db, parent.file, config);
        assert!(!missing.complete);
        assert!(missing.jobs.is_empty());
        assert_eq!(missing.diagnostics[0].diagnostic.code, "include-not-found");
        root(&mut db, config, "/execution/_child.qmd", "`r binding`\n");
        assert!(execution_plan(&db, parent.file, config).complete);
        root(
            &mut db,
            config,
            "/execution/_child.qmd",
            "{{< include ./main.qmd >}}\n",
        );
        let cycle = execution_plan(&db, parent.file, config);
        assert!(!cycle.complete);
        assert!(cycle.jobs.is_empty());
        assert_eq!(cycle.diagnostics[0].diagnostic.code, "include-cycle");
    }

    #[test]
    fn cyclic_roots_remain_reportable_beside_unrelated_documents() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let a = root(
            &mut db,
            config,
            "/execution/a.qmd",
            "{{< include b.qmd >}}\n",
        );
        let b = root(
            &mut db,
            config,
            "/execution/b.qmd",
            "{{< include a.qmd >}}\n",
        );
        let unrelated = root(&mut db, config, "/execution/other.qmd", "Text\n");
        let targets = vec![
            PathBuf::from("/execution/a.qmd"),
            PathBuf::from("/execution/other.qmd"),
        ];
        let batch = ExecutionBatch::new(&db, &[a, b, unrelated], &targets);
        assert!(
            batch
                .plans
                .iter()
                .flat_map(|plan| &plan.diagnostics)
                .any(|issue| issue.diagnostic.code == "include-cycle")
        );
    }

    #[test]
    fn escaped_and_verbatim_shortcodes_do_not_load_files() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let parent = root(
            &mut db,
            config,
            "/execution/main.qmd",
            "{{{< include absent.qmd >}}}\n\n`{{< include absent.qmd >}}`\n",
        );
        let plan = execution_plan(&db, parent.file, config);
        assert!(plan.complete);
        assert_eq!(plan.dependencies.len(), 1);
    }

    #[test]
    fn tool_ranges_in_generated_padding_are_not_attributed_to_source() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let parent = root(
            &mut db,
            config,
            "/execution/main.qmd",
            "Prose\n\n```{r}\nvalue\n```\n",
        );
        let plan = execution_plan(&db, parent.file, config);
        let job = &plan.jobs[0];
        let outputs = [
            (
                "eslint",
                r#"[{"messages":[{"ruleId":"test","severity":2,"message":"padding","line":1,"column":1,"endLine":1,"endColumn":2}]}]"#,
            ),
            (
                "shellcheck",
                r#"[{"code":1,"level":"warning","message":"padding","line":1,"column":1,"endLine":1,"endColumn":2}]"#,
            ),
            (
                "staticcheck",
                r#"[{"code":"test","location":{"file":"test.go","line":1,"column":1},"message":"padding"}]"#,
            ),
        ];
        for (tool, output) in outputs {
            let diagnostics = super::super::external_linters::parse_linter_output(
                tool,
                output,
                &job.content,
                &plan.original_input,
                Some(&job.mappings),
            )
            .unwrap();
            assert!(diagnostics.is_empty(), "{tool}: {diagnostics:?}");
        }
    }

    #[test]
    fn mapping_restores_prefixed_crlf_and_unicode_source_and_drops_cross_source_fix() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let parent = root(
            &mut db,
            config,
            "/execution/main.qmd",
            "{{< include _child.qmd >}}\n\n`r last`\n",
        );
        let text = "> ```{r child}\r\n> value <- 1\r\n> ```\r\n\r\n🦀 `r value`\r\n";
        root(&mut db, config, "/execution/_child.qmd", text);
        let plan = execution_plan(&db, parent.file, config);
        let job = &plan.jobs[0];
        let start = job.content.find("value").unwrap();
        let output = serde_json::json!([{
            "rule": "test", "severity": "Warning", "range": { "start": start, "end": start + 5 },
            "message": {"name": "test", "body": "test"},
            "fix": {"start": start, "end": start + 5, "content": "other", "applicability": "safe", "description": "Rename"}
        }]);
        let raw = super::super::external_linters::parse_linter_output(
            "arity",
            &output.to_string(),
            &job.content,
            &plan.original_input,
            Some(&job.mappings),
        )
        .unwrap()
        .remove(0);
        let (occurrence, diagnostic) = project_diagnostic(plan, raw.clone()).unwrap();
        assert_eq!(
            plan.sources[occurrence].path,
            Path::new("/execution/_child.qmd")
        );
        assert_eq!(
            &text[usize::from(diagnostic.location.range.start())
                ..usize::from(diagnostic.location.range.end())],
            "value"
        );
        assert_eq!(
            (diagnostic.location.line, diagnostic.location.column),
            (2, 3)
        );
        assert_eq!(
            diagnostic.fix.unwrap().edits[0].range.start(),
            TextSize::from(text.find("value").unwrap() as u32)
        );
        let mut raw = raw;
        raw.fix.as_mut().unwrap().edits.push(Edit {
            range: TextRange::empty(0.into()),
            replacement: "bad".into(),
        });
        assert!(project_diagnostic(plan, raw).unwrap().1.fix.is_none());
    }

    fn proposal(plan: &ExecutionPlan, occurrence: usize) -> (usize, Diagnostic) {
        let source = &plan.sources[occurrence];
        let start = source.text.find("value").unwrap();
        let range = TextRange::new((start as u32).into(), ((start + 5) as u32).into());
        (
            occurrence,
            Diagnostic::warning(
                Location::from_range(range, &source.text),
                "unused-binding",
                "unused value",
            )
            .with_fix(Fix::unsafe_fix(
                "Remove",
                vec![Edit {
                    range,
                    replacement: String::new(),
                }],
            )),
        )
    }

    #[test]
    fn shared_fixes_require_all_parent_contexts_and_occurrences() {
        let mut db = SalsaDb::default();
        let config = config(&db);
        let a = root(
            &mut db,
            config,
            "/execution/a.qmd",
            "{{< include _child.qmd >}}\n",
        );
        let b = root(
            &mut db,
            config,
            "/execution/b.qmd",
            "{{< include _child.qmd >}}\n\n{{< include _child.qmd >}}\n",
        );
        let child = root(&mut db, config, "/execution/_child.qmd", "`r value`\n");
        let path = PathBuf::from("/execution/_child.qmd");
        let batch = ExecutionBatch::new(&db, &[a, b, child], std::slice::from_ref(&path));
        assert_eq!(batch.plans.len(), 2);
        let a_fix = proposal(&batch.plans[0], 1);
        let b_fix = proposal(&batch.plans[1], 1);
        let b_repeat = proposal(&batch.plans[1], 2);
        let partial = batch.merge(vec![(true, vec![a_fix.clone()]), (true, vec![])]);
        assert!(partial.diagnostics[&path][0].fix.is_none());
        assert_eq!(partial.diagnostics[&path][0].notes.len(), 1);
        let missing_occurrence = batch.merge(vec![
            (true, vec![a_fix.clone()]),
            (true, vec![b_fix.clone()]),
        ]);
        assert!(missing_occurrence.diagnostics[&path][0].fix.is_none());
        let agreed = batch.merge(vec![(true, vec![a_fix]), (true, vec![b_fix, b_repeat])]);
        assert_eq!(agreed.diagnostics[&path].len(), 1);
        assert!(agreed.diagnostics[&path][0].fix.is_some());
        assert_eq!(agreed.diagnostics[&path][0].notes.len(), 2);
    }
}