panache 2.44.0

An LSP, 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
//! Tests for diagnostic workflows (linting + code actions).

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

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

    // Open a document with heading hierarchy issue (h1 → h3 skip)
    let content = "# Heading 1\n\n### Heading 3\n\nContent.";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    // Note: Diagnostics are published via client.publish_diagnostics()
    // which we can't easily capture in this test setup.
    // For now, we'll test code actions which are request/response.

    // Request code actions for the h3 line
    let code_actions = server
        .get_code_actions(
            "file:///test.qmd",
            0, // Whole document range to include full diagnostic span
            0,
            4,
            99,
        )
        .await;

    // Should have a quick fix for heading hierarchy
    assert!(code_actions.is_some());
    let actions = code_actions.unwrap();

    // Find the heading hierarchy fix
    let fix = actions.iter().find(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action {
            ca.title.contains("heading")
        } else {
            false
        }
    });

    assert!(fix.is_some(), "Should have heading hierarchy fix");
}

// Document diagnostics are not currently supported in the test harness.

#[tokio::test]
async fn test_code_actions_filter_quickfixes_to_requested_range() {
    let server = TestLspServer::new();
    let content = "# Heading 1\n\n### Heading 3\n\nContent.\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions(
            "file:///test.qmd",
            0, // Request only first line range
            0,
            0,
            20,
        )
        .await
        .expect("code actions response");

    let has_heading_fix = code_actions.iter().any(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action {
            ca.title.contains("heading")
        } else {
            false
        }
    });

    assert!(
        !has_heading_fix,
        "Heading hierarchy quickfix should not appear outside requested range"
    );
}

#[tokio::test]
async fn test_code_actions_require_diagnostic_to_be_fully_within_requested_range() {
    let server = TestLspServer::new();
    let content = "# Heading 1\n\n### Heading 3\n\nContent.\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    // This range intersects the heading diagnostic line but does not fully contain
    // the full heading range.
    let code_actions = server
        .get_code_actions(
            "file:///test.qmd",
            2, // "### Heading 3"
            0,
            2,
            1, // very narrow range
        )
        .await
        .expect("code actions response");

    let has_heading_fix = code_actions.iter().any(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action {
            ca.title.contains("heading")
        } else {
            false
        }
    });

    assert!(
        !has_heading_fix,
        "Quickfix should not be offered when request only partially overlaps diagnostic range"
    );
}

#[tokio::test]
async fn test_code_actions_offer_quickfix_for_cursor_inside_diagnostic() {
    let server = TestLspServer::new();
    let content = "# Heading 1\n\n### Heading 3\n\nContent.\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions(
            "file:///test.qmd",
            2, // "### Heading 3"
            2, // cursor inside heading text
            2,
            2, // zero-width LSP cursor range
        )
        .await
        .expect("code actions response");

    let has_heading_fix = code_actions.iter().any(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action {
            ca.title.contains("heading")
        } else {
            false
        }
    });

    assert!(
        has_heading_fix,
        "Quickfix should be offered when cursor is inside diagnostic range"
    );
}

#[tokio::test]
async fn test_code_actions_offer_source_fix_all_for_fixable_diagnostics() {
    let server = TestLspServer::new();
    let content = "# Heading 1\n\n### Heading 3\n\nContent.\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 4, 99)
        .await
        .expect("code actions response");

    let fix_all = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.kind == Some(CodeActionKind::SOURCE_FIX_ALL)
        {
            return Some(ca);
        }
        None
    });
    let fix_all = fix_all.expect("expected source.fixAll code action");

    let edits = fix_all
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .and_then(|changes| changes.get(&"file:///test.qmd".parse::<Uri>().expect("uri")))
        .expect("source.fixAll workspace edits");
    assert!(
        !edits.is_empty(),
        "source.fixAll should include at least one text edit"
    );
}

#[tokio::test]
async fn test_code_actions_do_not_offer_source_fix_all_without_fixes() {
    let server = TestLspServer::new();
    let content = "# Heading 1\n\n## Heading 2\n\nContent.\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 4, 99)
        .await
        .expect("code actions response");

    let has_fix_all = code_actions.iter().any(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action {
            ca.kind == Some(CodeActionKind::SOURCE_FIX_ALL)
        } else {
            false
        }
    });
    assert!(
        !has_fix_all,
        "source.fixAll should not be offered when there are no fixable diagnostics"
    );
}

#[tokio::test]
async fn test_code_actions_no_refactors_inside_yaml_frontmatter() {
    let server = TestLspServer::new();
    let content = "---\ntitle: Report\nlist:\n  - a\n  - b\n---\n\nBody.\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions(
            "file:///test.qmd",
            3, // inside frontmatter list item
            2,
            3,
            8,
        )
        .await
        .expect("code actions response");

    let has_refactor = code_actions.iter().any(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action {
            ca.kind
                .as_ref()
                .is_some_and(|kind| *kind == CodeActionKind::REFACTOR)
        } else {
            false
        }
    });
    assert!(
        !has_refactor,
        "Refactor code actions should not be offered inside YAML frontmatter"
    );
}

#[tokio::test]
async fn test_hashpipe_yaml_parse_error_in_built_in_lint_plan() {
    let server = TestLspServer::new();
    let content = "```{r}\n#| fig-cap: [\na <- 1\n```\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let diagnostics = server
        .get_built_in_diagnostics("file:///test.qmd")
        .await
        .expect("diagnostics");

    let yaml_parse_error = diagnostics
        .iter()
        .find(|diag| diag.code == "yaml-parse-error")
        .expect("expected yaml-parse-error diagnostic");
    assert!(
        yaml_parse_error.message.contains("YAML parse error"),
        "expected YAML parse error message, got: {}",
        yaml_parse_error.message
    );
}

#[tokio::test]
async fn test_hashpipe_folded_scalar_parse_error_maps_to_host_position() {
    let server = TestLspServer::new();
    let content = "```{r}\n#| fig-cap: >-\n#|   A folded caption\n#| bad: [\na <- 1\n```\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let diagnostics = server
        .get_built_in_diagnostics("file:///test.qmd")
        .await
        .expect("diagnostics");

    let yaml_parse_error = diagnostics
        .iter()
        .find(|diag| diag.code == "yaml-parse-error")
        .expect("expected yaml-parse-error diagnostic");
    assert_eq!(yaml_parse_error.location.line, 4);
    assert_eq!(yaml_parse_error.location.column, 9);
}

#[tokio::test]
async fn test_code_action_convert_implicit_heading_link_to_explicit() {
    let server = TestLspServer::new();
    let content = "# Unordered Lists\n\n[unordered lists]\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 2, 2, 2, 18)
        .await
        .expect("code actions response");

    let action = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.title == "Convert to explicit heading link"
        {
            return Some(ca);
        }
        None
    });
    let action = action.expect("expected heading link conversion action");

    let changes = action
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .expect("workspace edit changes");
    let edits = changes
        .get(&"file:///test.qmd".parse::<Uri>().expect("uri"))
        .expect("edits for document");
    assert_eq!(edits.len(), 1);
    assert_eq!(edits[0].new_text, "[unordered lists](#unordered-lists)");
}

#[tokio::test]
async fn test_code_action_convert_bullet_list_to_ordered() {
    let server = TestLspServer::new();
    let content = "- First\n- Second\n- Third\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 0, 7)
        .await
        .expect("code actions response");

    let action = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.title == "Convert to ordered list"
        {
            return Some(ca);
        }
        None
    });
    let action = action.expect("expected ordered list conversion action");

    let changes = action
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .expect("workspace edit changes");
    let edits = changes
        .get(&"file:///test.qmd".parse::<Uri>().expect("uri"))
        .expect("edits for document");
    assert_eq!(edits.len(), 3);
    assert_eq!(edits[0].new_text, "1.");
    assert_eq!(edits[1].new_text, "2.");
    assert_eq!(edits[2].new_text, "3.");
}

#[tokio::test]
async fn test_code_action_convert_ordered_list_to_bullet() {
    let server = TestLspServer::new();
    let content = "1. First\n2. Second\n3. Third\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 0, 8)
        .await
        .expect("code actions response");

    let action = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.title == "Convert to bullet list"
        {
            return Some(ca);
        }
        None
    });
    let action = action.expect("expected bullet list conversion action");

    let changes = action
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .expect("workspace edit changes");
    let edits = changes
        .get(&"file:///test.qmd".parse::<Uri>().expect("uri"))
        .expect("edits for document");
    assert_eq!(edits.len(), 3);
    assert!(edits.iter().all(|edit| edit.new_text == "-"));
}

#[tokio::test]
async fn test_code_action_convert_bullet_list_to_task() {
    let server = TestLspServer::new();
    let content = "- First\n- Second\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 0, 7)
        .await
        .expect("code actions response");

    let action = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.title == "Convert to task list"
        {
            return Some(ca);
        }
        None
    });
    let action = action.expect("expected task list conversion action");

    let changes = action
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .expect("workspace edit changes");
    let edits = changes
        .get(&"file:///test.qmd".parse::<Uri>().expect("uri"))
        .expect("edits for document");
    assert_eq!(edits.len(), 2);
    assert!(edits.iter().all(|edit| edit.new_text == " [ ]"));
}

#[tokio::test]
async fn test_code_action_convert_ordered_list_to_task() {
    let server = TestLspServer::new();
    let content = "1. First\n2. Second\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 0, 8)
        .await
        .expect("code actions response");

    let action = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.title == "Convert to task list"
        {
            return Some(ca);
        }
        None
    });
    let action = action.expect("expected task list conversion action");

    let changes = action
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .expect("workspace edit changes");
    let edits = changes
        .get(&"file:///test.qmd".parse::<Uri>().expect("uri"))
        .expect("edits for document");
    assert_eq!(edits.len(), 4, "marker + checkbox edit per item");
    assert_eq!(edits[0].new_text, "-");
    assert_eq!(edits[1].new_text, " [ ]");
    assert_eq!(edits[2].new_text, "-");
    assert_eq!(edits[3].new_text, " [ ]");
}

#[tokio::test]
async fn test_code_action_convert_task_list_to_ordered() {
    let server = TestLspServer::new();
    let content = "- [ ] First\n- [x] Second\n";
    server
        .open_document("file:///test.qmd", content, "quarto")
        .await;

    let code_actions = server
        .get_code_actions("file:///test.qmd", 0, 0, 0, 10)
        .await
        .expect("code actions response");

    let action = code_actions.iter().find_map(|action| {
        if let CodeActionOrCommand::CodeAction(ca) = action
            && ca.title == "Convert to ordered list"
        {
            return Some(ca);
        }
        None
    });
    let action = action.expect("expected ordered list conversion action");

    let changes = action
        .edit
        .as_ref()
        .and_then(|edit| edit.changes.as_ref())
        .expect("workspace edit changes");
    let edits = changes
        .get(&"file:///test.qmd".parse::<Uri>().expect("uri"))
        .expect("edits for document");
    assert_eq!(edits.len(), 4, "marker + checkbox edit per item");
    assert_eq!(edits[0].new_text, "1.");
    assert_eq!(edits[1].new_text, "");
    assert_eq!(edits[2].new_text, "2.");
    assert_eq!(edits[3].new_text, "");
}