panache 2.35.0

An LSP, formatter, and linter for Pandoc markdown, Quarto, and RMarkdown
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
//! Tests for hover (footnote and citation previews).

use super::helpers::*;
use tower_lsp_server::ls_types::*;

#[tokio::test]
async fn test_hover_on_included_footnote() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let child_path = temp_dir.path().join("_child.qmd");
    let parent_path = temp_dir.path().join("parent.qmd");

    std::fs::write(&child_path, "[^1]: Included footnote content.\n").unwrap();
    std::fs::write(&parent_path, "{{< include _child.qmd >}}\nRef[^1].\n").unwrap();

    let server = TestLspServer::new();
    let root_uri = Uri::from_file_path(temp_dir.path()).expect("root uri");
    let parent_uri = Uri::from_file_path(&parent_path).expect("parent uri");
    server.initialize(root_uri.as_str()).await;
    server
        .open_document(
            parent_uri.as_str(),
            &std::fs::read_to_string(&parent_path).unwrap(),
            "quarto",
        )
        .await;

    let hover = server.hover(parent_uri.as_str(), 1, 4).await;

    let Some(h) = hover else {
        panic!("Expected hover content");
    };
    if let HoverContents::Markup(markup) = h.contents {
        assert!(markup.value.contains("Included footnote content"));
    } else {
        panic!("Expected markup hover content");
    }
}

#[tokio::test]
async fn test_hover_included_updates_after_watcher_change() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let child_path = temp_dir.path().join("_child.qmd");
    let parent_path = temp_dir.path().join("parent.qmd");

    std::fs::write(&child_path, "[^1]: Included footnote content.\n").unwrap();
    std::fs::write(&parent_path, "{{< include _child.qmd >}}\nRef[^1].\n").unwrap();

    let server = TestLspServer::new();
    let root_uri = Uri::from_file_path(temp_dir.path()).expect("root uri");
    let parent_uri = Uri::from_file_path(&parent_path).expect("parent uri");
    server.initialize(root_uri.as_str()).await;
    server
        .open_document(
            parent_uri.as_str(),
            &std::fs::read_to_string(&parent_path).unwrap(),
            "quarto",
        )
        .await;

    // First hover caches the included file and its definition index.
    assert!(
        server.hover(parent_uri.as_str(), 1, 4).await.is_some(),
        "Sanity check: should resolve hover before edit"
    );

    // Change the included file on disk so the footnote no longer exists.
    std::fs::write(&child_path, "[^2]: Included footnote content.\n").unwrap();
    server
        .did_change_watched_files(vec![FileEvent {
            uri: Uri::from_file_path(&child_path).expect("child uri"),
            typ: FileChangeType::CHANGED,
        }])
        .await;

    let hover = server.hover(parent_uri.as_str(), 1, 4).await;
    assert!(
        hover.is_none(),
        "After watcher update, hover should no longer resolve"
    );
}

#[tokio::test]
async fn test_hover_on_footnote_reference() {
    let server = TestLspServer::new();

    // Open a document with footnote
    let content = r#"Text with footnote[^1] here.

[^1]: This is the footnote content with details.
"#;
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    // Request hover on the footnote reference [^1]
    let hover = server
        .hover(
            "file:///test.md",
            0,  // Line with footnote[^1]
            20, // Character position inside [^1]
        )
        .await;

    assert!(hover.is_some(), "Should have hover info for footnote");

    if let Some(h) = hover {
        // Check that it contains the footnote content
        if let HoverContents::Markup(markup) = h.contents {
            assert_eq!(markup.kind, MarkupKind::Markdown);
            assert!(
                markup.value.contains("footnote content"),
                "Should show footnote content"
            );
        } else {
            panic!("Expected markup hover content");
        }
    }
}

#[tokio::test]
async fn test_hover_on_plain_text() {
    let server = TestLspServer::new();

    // Open a document without any special elements
    let content = "Just plain text without footnotes.";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    // Request hover in plain text
    let hover = server.hover("file:///test.md", 0, 10).await;

    assert!(hover.is_none(), "Should not have hover for plain text");
}

#[tokio::test]
async fn test_hover_on_footnote_with_formatting() {
    let server = TestLspServer::new();

    // Open a document with formatted footnote
    let content = r#"Reference[^note] in text.

[^note]: Footnote with *emphasis* and `code`.
"#;
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    // Request hover on footnote
    let hover = server
        .hover(
            "file:///test.md",
            0,  // Line with [^note]
            10, // Inside [^note]
        )
        .await;

    assert!(hover.is_some(), "Should have hover for formatted footnote");

    if let Some(h) = hover
        && let HoverContents::Markup(markup) = h.contents
    {
        let content = markup.value;
        assert!(content.contains("*emphasis*"));
        assert!(content.contains("`code`"));
    }
}

#[tokio::test]
async fn test_hover_on_citation_preview() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let root = temp_dir.path();
    let bib_path = root.join("refs.bib");
    let doc_path = root.join("doc.qmd");

    std::fs::write(
        &bib_path,
        "@article{citekey,\n  author = {Doe, Jane},\n  year = {2020},\n  title = {Sample Title},\n  journal = {Journal Name},\n  volume = {12},\n  number = {3},\n  pages = {45-67}\n}\n",
    )
    .unwrap();

    std::fs::write(
        &doc_path,
        "---\nbibliography: refs.bib\n---\n\nSee [@citekey].\n",
    )
    .unwrap();

    let server = TestLspServer::new();
    let root_uri = Uri::from_file_path(root).expect("root uri");
    let doc_uri = Uri::from_file_path(&doc_path).expect("doc uri");
    server.initialize(root_uri.as_str()).await;
    server
        .open_document(
            doc_uri.as_str(),
            &std::fs::read_to_string(&doc_path).unwrap(),
            "quarto",
        )
        .await;

    let result = server.hover(doc_uri.as_str(), 4, 7).await;
    let Some(Hover { contents, .. }) = result else {
        panic!("Expected hover content");
    };
    let content = match contents {
        HoverContents::Markup(markup) => markup.value,
        HoverContents::Scalar(scalar) => match scalar {
            MarkedString::String(text) => text,
            MarkedString::LanguageString(lang) => lang.value,
        },
        HoverContents::Array(array) => array
            .iter()
            .map(|item| match item {
                MarkedString::String(text) => text.clone(),
                MarkedString::LanguageString(lang) => lang.value.clone(),
            })
            .collect::<Vec<_>>()
            .join("\n"),
    };
    assert!(content.contains("Doe"));
    assert!(content.contains("2020"));
    assert!(content.contains("Sample Title"));
    assert!(content.contains("Journal Name"));
}

#[tokio::test]
async fn test_hover_on_undefined_footnote() {
    let server = TestLspServer::new();

    // Open a document with footnote reference but no definition
    let content = "Text with undefined[^missing] footnote.";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    // Request hover on undefined footnote
    let hover = server
        .hover(
            "file:///test.md",
            0,
            25, // Inside [^missing]
        )
        .await;

    // Should return None when footnote definition doesn't exist
    assert!(
        hover.is_none(),
        "Should not have hover for undefined footnote"
    );
}

#[tokio::test]
async fn test_hover_returns_none_inside_yaml_frontmatter() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let root = temp_dir.path();
    let bib_path = root.join("refs.bib");
    let doc_path = root.join("doc.qmd");

    std::fs::write(&bib_path, "@article{known,\n  title = {Known}\n}\n").unwrap();
    std::fs::write(
        &doc_path,
        "---\ntitle: \"@known\"\nbibliography: refs.bib\n---\n\nSee [@known].\n",
    )
    .unwrap();

    let server = TestLspServer::new();
    let root_uri = Uri::from_file_path(root).expect("root uri");
    let doc_uri = Uri::from_file_path(&doc_path).expect("doc uri");
    server.initialize(root_uri.as_str()).await;
    server
        .open_document(
            doc_uri.as_str(),
            &std::fs::read_to_string(&doc_path).unwrap(),
            "quarto",
        )
        .await;

    let hover = server.hover(doc_uri.as_str(), 1, 10).await;
    assert!(
        hover.is_none(),
        "Expected no hover when cursor is inside YAML frontmatter"
    );
}

#[tokio::test]
async fn test_hover_on_heading_reference_shows_section_preview() {
    let server = TestLspServer::new();
    let content = "# Intro {#intro}\n\nFirst paragraph in intro section.\n\n## Next\n\nTail.\n\nSee [go](#intro).\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server
        .hover(
            "file:///test.md",
            8,  // See [go](#intro).
            10, // Inside intro anchor text
        )
        .await;

    let Some(h) = hover else {
        panic!("Expected hover content for heading reference");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("Section"));
    assert!(content.contains("Intro"));
    assert!(content.contains("First paragraph in intro section."));
}

#[tokio::test]
async fn test_hover_on_heading_declaration_returns_none() {
    let server = TestLspServer::new();
    let content = "# Intro {#intro}\n\nBody.\n\nSee [go](#intro).\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server.hover("file:///test.md", 0, 3).await;
    assert!(
        hover.is_none(),
        "Heading declaration should not produce section preview hover"
    );
}

#[tokio::test]
async fn test_hover_on_heading_reference_with_empty_section_shows_title_only() {
    let server = TestLspServer::new();
    let content = "# Intro {#intro}\n\n## Next\n\nSee [go](#intro).\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server
        .hover(
            "file:///test.md",
            4, // See [go](#intro).
            10,
        )
        .await;

    let Some(h) = hover else {
        panic!("Expected hover content for heading reference");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("Section"));
    assert!(content.contains("Intro"));
    assert!(!content.contains("..."));
}

#[tokio::test]
async fn test_hover_on_heading_reference_crops_preview() {
    let server = TestLspServer::new();
    let long_body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(8);
    let content = format!(
        "# Intro {{#intro}}\n\n{}\n\n## Next\n\nSee [go](#intro).\n",
        long_body
    );
    server
        .open_document("file:///test.md", &content, "markdown")
        .await;

    let hover = server.hover("file:///test.md", 6, 10).await;
    let Some(h) = hover else {
        panic!("Expected hover content for heading reference");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(
        content.ends_with("..."),
        "Expected cropped preview to end with ellipsis"
    );
}

#[tokio::test]
async fn test_hover_on_reference_link_definition_to_heading_shows_section_preview() {
    let server = TestLspServer::new();
    let content = "# Intro {#bar}\n\nSection body here.\n\nSee [foo][myref].\n\n[myref]: #bar\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server
        .hover(
            "file:///test.md",
            4,  // See [foo][myref].
            11, // inside [myref]
        )
        .await;

    let Some(h) = hover else {
        panic!("Expected hover content for reference-style heading link");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("Section"));
    assert!(content.contains("Intro"));
    assert!(content.contains("Section body here."));
}

#[tokio::test]
async fn test_hover_on_reference_link_definition_to_non_heading_returns_none() {
    let server = TestLspServer::new();
    let content = "# Intro {#bar}\n\nSection body here.\n\nSee [foo][myref].\n\n[myref]: https://example.com\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server.hover("file:///test.md", 4, 11).await;
    assert!(
        hover.is_none(),
        "Non-heading reference definitions should not produce section preview hover"
    );
}

#[tokio::test]
async fn test_hover_on_equation_reference_shows_equation_preview() {
    let server = TestLspServer::new();
    let content = "$$\na = b + c\n$$ {#eq-foo}\n\n@eq-foo\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let hover = server.hover("file:///test.qmd", 4, 4).await;
    let Some(h) = hover else {
        panic!("Expected hover content for equation reference");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("Equation"));
    assert!(content.contains("eq-foo"));
    assert!(content.contains("```tex"));
    assert!(content.contains("a = b + c"));
}

#[tokio::test]
async fn test_hover_on_equation_reference_crops_preview_lines() {
    let server = TestLspServer::new();
    let content =
        "$$\nline1\nline2\nline3\nline4\nline5\nline6\nline7\n$$ {#eq-long}\n\n@eq-long\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let hover = server.hover("file:///test.qmd", 10, 4).await;
    let Some(h) = hover else {
        panic!("Expected hover content for equation reference");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("line6"));
    assert!(!content.contains("line7"));
    assert!(content.contains("\n...\n```"));
}

#[tokio::test]
async fn test_hover_on_direct_local_markdown_link_shows_linked_doc_preview() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let doc_path = temp_dir.path().join("doc.qmd");
    let linked_path = temp_dir.path().join("linked.md");

    std::fs::write(
        &linked_path,
        "# Linked title\n\nLinked paragraph preview text.\n",
    )
    .unwrap();
    std::fs::write(&doc_path, "See [linked](./linked.md).\n").unwrap();

    let server = TestLspServer::new();
    let root_uri = Uri::from_file_path(temp_dir.path()).expect("root uri");
    let doc_uri = Uri::from_file_path(&doc_path).expect("doc uri");
    server.initialize(root_uri.as_str()).await;
    server
        .open_document(
            doc_uri.as_str(),
            &std::fs::read_to_string(&doc_path).unwrap(),
            "quarto",
        )
        .await;

    let hover = server.hover(doc_uri.as_str(), 0, 17).await;
    let Some(h) = hover else {
        panic!("Expected hover content for direct local markdown link");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("Linked document"));
    assert!(content.contains("linked.md"));
    assert!(content.contains("Linked title"));
    assert!(content.contains("Linked paragraph preview text."));
}

#[tokio::test]
async fn test_hover_on_reference_definition_local_markdown_link_shows_linked_doc_preview() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let doc_path = temp_dir.path().join("doc.qmd");
    let linked_path = temp_dir.path().join("linked.qmd");

    std::fs::write(
        &linked_path,
        "# Ref linked title\n\nRef linked paragraph preview text.\n",
    )
    .unwrap();
    std::fs::write(&doc_path, "See [linked][ref].\n\n[ref]: ./linked.qmd\n").unwrap();

    let server = TestLspServer::new();
    let root_uri = Uri::from_file_path(temp_dir.path()).expect("root uri");
    let doc_uri = Uri::from_file_path(&doc_path).expect("doc uri");
    server.initialize(root_uri.as_str()).await;
    server
        .open_document(
            doc_uri.as_str(),
            &std::fs::read_to_string(&doc_path).unwrap(),
            "quarto",
        )
        .await;

    let hover = server.hover(doc_uri.as_str(), 0, 14).await;
    let Some(h) = hover else {
        panic!("Expected hover content for reference-definition local markdown link");
    };
    let content = match h.contents {
        HoverContents::Markup(markup) => markup.value,
        _ => panic!("Expected markdown hover content"),
    };
    assert!(content.contains("Linked document"));
    assert!(content.contains("linked.qmd"));
    assert!(content.contains("Ref linked title"));
    assert!(content.contains("Ref linked paragraph preview text."));
}

#[tokio::test]
async fn test_hover_on_missing_local_markdown_link_returns_none() {
    let server = TestLspServer::new();
    let content = "See [missing](./does-not-exist.md).\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server.hover("file:///test.md", 0, 20).await;
    assert!(
        hover.is_none(),
        "Missing local linked document should not produce hover preview"
    );
}

#[tokio::test]
async fn test_hover_on_external_url_link_returns_none_for_linked_doc_preview() {
    let server = TestLspServer::new();
    let content = "See [site](https://example.com).\n";
    server
        .open_document("file:///test.md", content, "markdown")
        .await;

    let hover = server.hover("file:///test.md", 0, 13).await;
    assert!(
        hover.is_none(),
        "External URL links should not produce linked-document previews"
    );
}