rumdl 0.1.89

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
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
//! Test that LSP properly handles initialization options for configuration
//! This verifies that VSCode extension settings are properly passed through

use tower_lsp::lsp_types::*;
use tower_lsp::{LanguageServer, LspService};

use rumdl_lib::lsp::RumdlLspConfig;
use rumdl_lib::lsp::server::RumdlLanguageServer;

/// Test that initialization options are properly handled
#[tokio::test]
async fn test_lsp_initialization_options() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Create initialization options that would come from VSCode
    let lsp_config = RumdlLspConfig {
        config_path: Some("/path/to/config.toml".to_string()),
        enable_linting: true,
        enable_auto_fix: false,
        enable_rules: Some(vec!["MD001".to_string(), "MD002".to_string()]),
        disable_rules: Some(vec!["MD013".to_string()]),
        ..Default::default()
    };

    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: Some(serde_json::to_value(lsp_config).unwrap()),
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    let result = service.inner().initialize(init_params).await;
    assert!(result.is_ok(), "Initialization with options should succeed");

    // The server should now be configured with the provided options
    service.inner().initialized(InitializedParams {}).await;
}

/// Test that enable_rules properly filters rules
#[tokio::test]
async fn test_enable_rules_filtering() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Only enable MD001 and MD018
    let lsp_config = RumdlLspConfig {
        config_path: None,
        enable_linting: true,
        enable_auto_fix: false,
        enable_rules: Some(vec!["MD001".to_string(), "MD018".to_string()]),
        disable_rules: None,
        ..Default::default()
    };

    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: Some(serde_json::to_value(lsp_config).unwrap()),
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Open a document with multiple issues
    let uri = Url::parse("file:///test/select.md").unwrap();
    let text = r#"#Missing space after hash (MD018)

## Heading 2

This line is way too long and should trigger MD013 but it's not in enable_rules so it should be ignored completely."#;

    service
        .inner()
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "markdown".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    // Request diagnostics
    let diag_params = DocumentDiagnosticParams {
        text_document: TextDocumentIdentifier { uri: uri.clone() },
        identifier: None,
        previous_result_id: None,
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = service.inner().diagnostic(diag_params).await;
    assert!(result.is_ok(), "Diagnostic request should succeed");

    if let Ok(DocumentDiagnosticReportResult::Report(report)) = result {
        match report {
            DocumentDiagnosticReport::Full(full_report) => {
                let diagnostics = full_report.full_document_diagnostic_report.items;

                // Should only have MD018 diagnostic, not MD013
                assert!(
                    diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD018".to_string()))),
                    "Should have MD018 diagnostic"
                );
                assert!(
                    !diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD013".to_string()))),
                    "Should NOT have MD013 diagnostic as it's not in enable_rules"
                );
            }
            _ => panic!("Expected full diagnostic report"),
        }
    }
}

/// Test that disable_rules properly filters out rules
#[tokio::test]
async fn test_disable_rules_filtering() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Ignore MD013 (line length) and MD018 (no space after hash)
    let lsp_config = RumdlLspConfig {
        config_path: None,
        enable_linting: true,
        enable_auto_fix: false,
        enable_rules: None,
        disable_rules: Some(vec!["MD013".to_string(), "MD018".to_string()]),
        ..Default::default()
    };

    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: Some(serde_json::to_value(lsp_config).unwrap()),
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Open a document with issues that should be ignored
    let uri = Url::parse("file:///test/ignore.md").unwrap();
    let text = r#"#Missing space (MD018 - should be ignored)

This line is way too long and would normally trigger MD013 but it should be ignored due to disable_rules configuration."#;

    service
        .inner()
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "markdown".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    // Request diagnostics
    let diag_params = DocumentDiagnosticParams {
        text_document: TextDocumentIdentifier { uri },
        identifier: None,
        previous_result_id: None,
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = service.inner().diagnostic(diag_params).await;
    assert!(result.is_ok(), "Diagnostic request should succeed");

    if let Ok(DocumentDiagnosticReportResult::Report(report)) = result {
        match report {
            DocumentDiagnosticReport::Full(full_report) => {
                let diagnostics = full_report.full_document_diagnostic_report.items;

                // Should not have MD013 or MD018 diagnostics as they're ignored
                assert!(
                    !diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD013".to_string()))),
                    "Should NOT have MD013 diagnostic as it's in disable_rules"
                );
                assert!(
                    !diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD018".to_string()))),
                    "Should NOT have MD018 diagnostic as it's in disable_rules"
                );
            }
            _ => panic!("Expected full diagnostic report"),
        }
    }
}

/// Test that both enable_rules and disable_rules work together
#[tokio::test]
async fn test_select_and_disable_rules_together() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Select MD001, MD013, MD018 but ignore MD013
    // Result: only MD001 and MD018 should be active
    let lsp_config = RumdlLspConfig {
        config_path: None,
        enable_linting: true,
        enable_auto_fix: false,
        enable_rules: Some(vec!["MD001".to_string(), "MD013".to_string(), "MD018".to_string()]),
        disable_rules: Some(vec!["MD013".to_string()]),
        ..Default::default()
    };

    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: Some(serde_json::to_value(lsp_config).unwrap()),
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Open a document with various issues
    let uri = Url::parse("file:///test/combined.md").unwrap();
    let text = r#"#Missing space (MD018)

This line is way too long and would trigger MD013 but it's in disable_rules so should be filtered out even though it's in enable_rules."#;

    service
        .inner()
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "markdown".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    // Request diagnostics
    let diag_params = DocumentDiagnosticParams {
        text_document: TextDocumentIdentifier { uri },
        identifier: None,
        previous_result_id: None,
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = service.inner().diagnostic(diag_params).await;
    assert!(result.is_ok(), "Diagnostic request should succeed");

    if let Ok(DocumentDiagnosticReportResult::Report(report)) = result {
        match report {
            DocumentDiagnosticReport::Full(full_report) => {
                let diagnostics = full_report.full_document_diagnostic_report.items;

                // Should have MD018 but not MD013
                assert!(
                    diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD018".to_string()))),
                    "Should have MD018 diagnostic as it's in enable_rules and not ignored"
                );
                assert!(
                    !diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD013".to_string()))),
                    "Should NOT have MD013 diagnostic as it's in disable_rules (even though also in enable_rules)"
                );
            }
            _ => panic!("Expected full diagnostic report"),
        }
    }
}

/// Test that workspace/didChangeConfiguration properly updates settings
#[tokio::test]
async fn test_did_change_configuration_neovim_style() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Initialize with default config - but USE initializationOptions to disable MD018
    // This tests that the initialization path works correctly
    let lsp_config = RumdlLspConfig {
        config_path: None,
        enable_linting: true,
        enable_auto_fix: false,
        enable_rules: None,
        disable_rules: Some(vec!["MD018".to_string()]),
        ..Default::default()
    };

    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: Some(serde_json::to_value(lsp_config).unwrap()),
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Open a document with MD018 issue (missing space after #)
    let uri = Url::parse("file:///test/config_change.md").unwrap();
    let text = r#"#Missing space after hash (MD018)

Some content here."#;

    service
        .inner()
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "markdown".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    // Request diagnostics - MD018 should be disabled from initialization
    let diag_params = DocumentDiagnosticParams {
        text_document: TextDocumentIdentifier { uri: uri.clone() },
        identifier: None,
        previous_result_id: None,
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = service.inner().diagnostic(diag_params).await;
    assert!(result.is_ok(), "Diagnostic request should succeed");

    if let Ok(DocumentDiagnosticReportResult::Report(report)) = result {
        match report {
            DocumentDiagnosticReport::Full(full_report) => {
                let diagnostics = full_report.full_document_diagnostic_report.items;

                // Should NOT have MD018 diagnostic because it was disabled in initialization
                assert!(
                    !diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD018".to_string()))),
                    "Should NOT have MD018 diagnostic after disabling via initializationOptions"
                );
            }
            _ => panic!("Expected full diagnostic report"),
        }
    }
}

/// Test that workspace/didChangeConfiguration can dynamically update settings
#[tokio::test]
async fn test_did_change_configuration_dynamic_update() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Initialize with default config (no rules disabled)
    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: None,
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Open a document with MD018 issue (missing space after #)
    let uri = Url::parse("file:///test/config_change2.md").unwrap();
    let text = r#"#Missing space after hash (MD018)

Some content here."#;

    service
        .inner()
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "markdown".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    // First verify MD018 is reported BEFORE configuration change
    let diag_params = DocumentDiagnosticParams {
        text_document: TextDocumentIdentifier { uri: uri.clone() },
        identifier: None,
        previous_result_id: None,
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = service.inner().diagnostic(diag_params.clone()).await;
    assert!(result.is_ok(), "Diagnostic request should succeed");

    if let Ok(DocumentDiagnosticReportResult::Report(report)) = result {
        match report {
            DocumentDiagnosticReport::Full(full_report) => {
                let diagnostics = full_report.full_document_diagnostic_report.items;

                // SHOULD have MD018 diagnostic initially
                assert!(
                    diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD018".to_string()))),
                    "Should have MD018 diagnostic before configuration change"
                );
            }
            _ => panic!("Expected full diagnostic report"),
        }
    }

    // Now send a configuration change to disable MD018 (Neovim style)
    // Format: { "rumdl": { "disable": ["MD018"] } }
    let settings = serde_json::json!({
        "rumdl": {
            "disable": ["MD018"]
        }
    });

    service
        .inner()
        .did_change_configuration(DidChangeConfigurationParams { settings })
        .await;

    // Request diagnostics again - MD018 should now be disabled
    let result = service.inner().diagnostic(diag_params).await;
    assert!(result.is_ok(), "Diagnostic request should succeed");

    if let Ok(DocumentDiagnosticReportResult::Report(report)) = result {
        match report {
            DocumentDiagnosticReport::Full(full_report) => {
                let diagnostics = full_report.full_document_diagnostic_report.items;

                // Should NOT have MD018 diagnostic after disabling it via config change
                assert!(
                    !diagnostics
                        .iter()
                        .any(|d| d.code == Some(NumberOrString::String("MD018".to_string()))),
                    "Should NOT have MD018 diagnostic after disabling via didChangeConfiguration"
                );
            }
            _ => panic!("Expected full diagnostic report"),
        }
    }
}

/// Test that workspace/didChangeConfiguration handles rule-specific settings
#[tokio::test]
async fn test_did_change_configuration_rule_settings() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Initialize
    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: None,
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Send configuration with rule-specific settings (Neovim style)
    // Format: { "rumdl": { "MD013": { "lineLength": 200 } } }
    let settings = serde_json::json!({
        "rumdl": {
            "MD013": {
                "lineLength": 200
            }
        }
    });

    service
        .inner()
        .did_change_configuration(DidChangeConfigurationParams { settings })
        .await;

    // The config should now have the rule settings stored
    // (We can't easily verify the internal state, but we verify it doesn't crash)
}

/// Test that workspace/didChangeConfiguration handles direct settings (no rumdl wrapper)
#[tokio::test]
async fn test_did_change_configuration_direct_settings() {
    let (service, _socket) = LspService::new(|client| RumdlLanguageServer::new(client, None));

    // Initialize
    let init_params = InitializeParams {
        process_id: None,
        root_path: None,
        root_uri: Some(Url::parse("file:///test").unwrap()),
        initialization_options: None,
        capabilities: ClientCapabilities::default(),
        trace: None,
        workspace_folders: None,
        client_info: None,
        locale: None,
    };

    service.inner().initialize(init_params).await.unwrap();
    service.inner().initialized(InitializedParams {}).await;

    // Send configuration directly without "rumdl" wrapper
    // Some editors might send settings this way
    let settings = serde_json::json!({
        "disable": ["MD009", "MD010"],
        "lineLength": 100
    });

    service
        .inner()
        .did_change_configuration(DidChangeConfigurationParams { settings })
        .await;

    // Should handle gracefully without crashing
}

// Note: Tests for invalid rule names and value types are not included as integration tests
// because the tower-lsp test harness blocks when notifications are sent without a consumer.
// The validation logic is tested through the is_valid_rule_name function in unit tests,
// and the behavior can be verified manually with a real LSP client.