mdwright-lsp 0.1.2

Language Server Protocol delivery for mdwright
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#![allow(
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic,
    clippy::unwrap_used,
    reason = "integration tests: panic / unwrap on missing values is the desired failure mode"
)]

//! Smoke tests for the in-tree LSP server.
//!
//! Drives `LspService` directly through `tower::Service` rather than
//! framing JSON-RPC over a pipe, so failures point at the handler that
//! misbehaved rather than at the transport. The published-diagnostics
//! path is exercised by draining the `ClientSocket` stream, which is
//! what real clients see.

use std::time::Duration;

use crate::lsp::build_service_for_tests;
use futures::StreamExt;
use serde_json::{Value, json};
use tower::{Service, ServiceExt};
use tower_lsp::jsonrpc::Request;
use tower_lsp::lsp_types::Url;

fn req(id: i64, method: &'static str, params: Value) -> Request {
    Request::build(method).id(id).params(params).finish()
}

fn notif(method: &'static str, params: Value) -> Request {
    Request::build(method).params(params).finish()
}

fn init_params(utf8: bool, root_uri: Option<&str>) -> Value {
    let position_encodings: Vec<&str> = if utf8 { vec!["utf-8"] } else { vec!["utf-16"] };
    json!({
        "capabilities": {
            "general": {
                "positionEncodings": position_encodings,
            },
            "textDocument": {
                "publishDiagnostics": {},
            },
        },
        "processId": null,
        "rootUri": root_uri,
    })
}

async fn initialize(
    service: &mut tower_lsp::LspService<impl tower_lsp::LanguageServer + 'static>,
    utf8: bool,
) -> Value {
    initialize_with_root(service, utf8, None).await
}

async fn initialize_with_root(
    service: &mut tower_lsp::LspService<impl tower_lsp::LanguageServer + 'static>,
    utf8: bool,
    root_uri: Option<&str>,
) -> Value {
    let resp = service
        .ready()
        .await
        .expect("service ready")
        .call(req(1, "initialize", init_params(utf8, root_uri)))
        .await
        .expect("call ok")
        .expect("initialize returns a response");
    let (_, body) = resp.into_parts();
    let body = body.expect("initialize result is Ok");
    let _ack = service
        .ready()
        .await
        .expect("service ready")
        .call(notif("initialized", json!({})))
        .await
        .expect("call ok");
    body
}

async fn open_doc(
    service: &mut tower_lsp::LspService<impl tower_lsp::LanguageServer + 'static>,
    uri: &str,
    version: i32,
    source: &str,
) {
    let _ack = service
        .ready()
        .await
        .expect("service ready")
        .call(notif(
            "textDocument/didOpen",
            json!({
                "textDocument": {
                    "uri": uri,
                    "languageId": "markdown",
                    "version": version,
                    "text": source,
                }
            }),
        ))
        .await
        .expect("call ok");
}

async fn change_doc(
    service: &mut tower_lsp::LspService<impl tower_lsp::LanguageServer + 'static>,
    uri: &str,
    version: i32,
    source: &str,
) {
    let _ack = service
        .ready()
        .await
        .expect("service ready")
        .call(notif(
            "textDocument/didChange",
            json!({
                "textDocument": { "uri": uri, "version": version },
                "contentChanges": [{ "text": source }],
            }),
        ))
        .await
        .expect("call ok");
}

async fn request(
    service: &mut tower_lsp::LspService<impl tower_lsp::LanguageServer + 'static>,
    id: i64,
    method: &'static str,
    params: Value,
) -> Value {
    let resp = service
        .ready()
        .await
        .expect("service ready")
        .call(req(id, method, params))
        .await
        .expect("call ok")
        .unwrap_or_else(|| panic!("{method} returns a response"));
    let (_, body) = resp.into_parts();
    body.unwrap_or_else(|err| panic!("{method} returned error: {err:?}"))
}

#[tokio::test]
async fn initialize_returns_expected_capabilities() {
    let (mut service, _socket) = build_service_for_tests();
    let body = initialize(&mut service, true).await;
    let caps = &body["capabilities"];
    assert_eq!(caps["positionEncoding"], "utf-8", "utf-8 negotiated");
    assert_eq!(caps["textDocumentSync"], 1, "TextDocumentSyncKind::FULL");
    assert_eq!(caps["documentFormattingProvider"], true, "formatting advertised");
    assert_eq!(
        caps["documentRangeFormattingProvider"], true,
        "range formatting advertised"
    );
    assert_eq!(caps["hoverProvider"], true, "hover advertised");
    assert!(
        caps["codeActionProvider"].is_object(),
        "code actions advertised: {caps}"
    );
    let kinds = &caps["codeActionProvider"]["codeActionKinds"];
    assert!(
        kinds.as_array().is_some_and(|a| a.iter().any(|v| v == "quickfix")),
        "quickfix listed"
    );
    assert!(
        kinds.as_array().is_some_and(|a| a.iter().any(|v| v == "source.fixAll")),
        "fixAll listed",
    );
}

#[tokio::test]
async fn initialize_without_utf8_withdraws_formatting() {
    let (mut service, _socket) = build_service_for_tests();
    let body = initialize(&mut service, false).await;
    let caps = &body["capabilities"];
    assert!(
        caps.get("positionEncoding").is_none() || caps["positionEncoding"].is_null(),
        "no encoding advertised when client lacks UTF-8",
    );
    assert!(
        caps.get("documentFormattingProvider").is_none() || caps["documentFormattingProvider"].is_null(),
        "formatting withdrawn",
    );
    assert!(
        caps.get("documentRangeFormattingProvider").is_none() || caps["documentRangeFormattingProvider"].is_null(),
        "range formatting withdrawn",
    );
    assert!(
        caps.get("codeActionProvider").is_none() || caps["codeActionProvider"].is_null(),
        "code actions withdrawn",
    );
}

#[tokio::test]
async fn did_open_publishes_diagnostics() {
    let (mut service, socket) = build_service_for_tests();
    let _body = initialize(&mut service, true).await;

    let uri = "file:///tmp/mdwright-test-open.md";
    let source = "See https://example.com for details.\n";
    open_doc(&mut service, uri, 1, source).await;

    let published = wait_for_publish(socket, uri).await;
    let diags = published["diagnostics"].as_array().expect("diagnostics array");
    assert!(
        diags
            .iter()
            .any(|d| d["code"].as_str() == Some("bare-url") && d["source"].as_str() == Some("mdwright")),
        "expected a bare-url diagnostic from {source:?}, got {diags:?}",
    );
}

#[tokio::test]
async fn formatting_returns_expected_textedit() {
    let (mut service, _socket) = build_service_for_tests();
    let _body = initialize(&mut service, true).await;

    let uri = "file:///tmp/mdwright-test-fmt.md";
    // The LSP server discovers the repo's own `.mdwright.toml`
    // (`wrap = 120`), so the source must be one the loaded config
    // demonstrably rewrites. CRLF normalisation to LF is the
    // shortest-path guaranteed edit (`end_of_line = "lf"` default).
    let source = "alpha\r\nbeta\r\n";
    open_doc(&mut service, uri, 1, source).await;

    let body = request(
        &mut service,
        42,
        "textDocument/formatting",
        json!({
            "textDocument": { "uri": uri },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    let edits = body.as_array().cloned().unwrap_or_default();
    assert!(
        !edits.is_empty(),
        "format should produce at least one edit for {source:?}"
    );
    let edit = &edits[0];
    let new_text = edit["newText"].as_str().expect("newText is a string");
    // The LSP server discovers the repo's `.mdwright.toml` via the
    // server's CWD; mirror the discovery here so the expected output
    // uses the same fmt options.
    let cfg = mdwright_config::Config::discover(
        std::env::current_dir()
            .as_deref()
            .unwrap_or_else(|_| std::path::Path::new(".")),
    )
    .unwrap_or_else(|_| mdwright_config::Config::defaults());
    let expected = mdwright_format::format_document(
        &mdwright_document::Document::parse(source).expect("fixture parses"),
        cfg.fmt_options(),
    );
    assert_eq!(new_text, expected, "LSP format must match CLI format byte-for-byte");
}

#[tokio::test]
async fn parser_panic_input_publishes_parse_diagnostic_and_recovers() {
    let (mut service, mut socket) = build_service_for_tests();
    let _body = initialize(&mut service, true).await;

    let uri = "file:///tmp/mdwright-test-parser-boundary.md";
    let source = "- [n]:Z\r\n\t\t";
    open_doc(&mut service, uri, 1, source).await;

    let published = wait_for_publish(&mut socket, uri).await;
    let diags = published["diagnostics"].as_array().expect("diagnostics array");
    assert_eq!(diags.len(), 1, "parse failure should suppress lint diagnostics");
    assert!(
        diags[0]["message"]
            .as_str()
            .is_some_and(|msg| msg.contains("Markdown parser failed")),
        "unexpected parse diagnostic: {diags:?}",
    );
    assert_eq!(diags[0]["range"]["start"]["line"], 0);
    assert_eq!(diags[0]["range"]["start"]["character"], 0);
    assert_eq!(diags[0]["range"]["end"]["line"], 0);
    assert_eq!(diags[0]["range"]["end"]["character"], 0);

    let body = request(
        &mut service,
        43,
        "textDocument/formatting",
        json!({
            "textDocument": { "uri": uri },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    assert!(body.is_null(), "parse-failed document returns no edits");

    let body = request(
        &mut service,
        44,
        "textDocument/rangeFormatting",
        json!({
            "textDocument": { "uri": uri },
            "range": {
                "start": { "line": 0, "character": 0 },
                "end": { "line": 0, "character": 1 },
            },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    assert!(body.is_null(), "parse-failed range format returns no edits");

    let body = request(
        &mut service,
        45,
        "textDocument/onTypeFormatting",
        json!({
            "textDocument": { "uri": uri },
            "position": { "line": 0, "character": 0 },
            "ch": "\n",
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    assert!(body.is_null(), "parse-failed on-type format returns no edits");

    let body = request(
        &mut service,
        46,
        "textDocument/codeAction",
        json!({
            "textDocument": { "uri": uri },
            "range": {
                "start": { "line": 0, "character": 0 },
                "end": { "line": 0, "character": 0 },
            },
            "context": { "diagnostics": [] },
        }),
    )
    .await;
    assert!(body.is_null(), "parse-failed fix-all returns no actions");

    change_doc(&mut service, uri, 2, "See https://example.com now.\n").await;

    let published = wait_for_publish(&mut socket, uri).await;
    let diags = published["diagnostics"].as_array().expect("diagnostics array");
    assert!(
        diags.iter().any(|diag| diag["code"].as_str() == Some("bare-url")),
        "expected normal lint diagnostics after parseable edit, got {diags:?}",
    );
}

#[tokio::test]
async fn range_formatting_uses_verified_formatter_output() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(temp.path().join(".mdwright.toml"), "[fmt]\nwrap = 40\n").expect("write config");
    let root_uri = Url::from_directory_path(temp.path())
        .expect("directory uri")
        .to_string();
    let (mut service, _socket) = build_service_for_tests();
    let _body = initialize_with_root(&mut service, true, Some(&root_uri)).await;

    let uri = Url::from_file_path(temp.path().join("range.md"))
        .expect("file uri")
        .to_string();
    let source =
        "This is a long paragraph that should wrap when the LSP range formatter delegates to mdwright-format.\n";
    open_doc(&mut service, &uri, 1, source).await;

    let body = request(
        &mut service,
        47,
        "textDocument/rangeFormatting",
        json!({
            "textDocument": { "uri": uri },
            "range": {
                "start": { "line": 0, "character": 0 },
                "end": { "line": 0, "character": source.len() },
            },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    let edits = body.as_array().cloned().expect("range formatting returns edits");
    assert_eq!(edits.len(), 1, "expected one snapped range edit: {edits:?}");
    let got = edits[0]["newText"].as_str().expect("newText");

    let cfg = mdwright_config::Config::discover(temp.path()).expect("config");
    let doc = mdwright_document::Document::parse_with_options(source, cfg.parse_options()).expect("fixture parses");
    let checkpoints = mdwright_format::CheckpointTable::from_document(&doc);
    let expected =
        mdwright_format::format_range_with_checkpoints(&doc, cfg.fmt_options(), &checkpoints, 0..source.len());
    assert_eq!(got, expected);
}

#[tokio::test]
async fn stale_change_does_not_overwrite_newer_document_state() {
    let (mut service, _socket) = build_service_for_tests();
    let _body = initialize(&mut service, true).await;

    let uri = "file:///tmp/mdwright-test-stale-version.md";
    let source = "alpha\r\nbeta\r\n";
    open_doc(&mut service, uri, 2, source).await;
    change_doc(&mut service, uri, 1, "- [n]:Z\r\n\t\t").await;

    let body = request(
        &mut service,
        48,
        "textDocument/formatting",
        json!({
            "textDocument": { "uri": uri },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    let edits = body.as_array().cloned().unwrap_or_default();
    assert!(
        !edits.is_empty(),
        "stale parse-failing change must not replace the newer parseable text"
    );
}

#[tokio::test]
async fn oversized_document_publishes_diagnostic_and_does_not_format() {
    let (mut service, mut socket) = build_service_for_tests();
    let _body = initialize(&mut service, true).await;

    let uri = "file:///tmp/mdwright-test-large.md";
    let source = "a".repeat(10_000_001);
    open_doc(&mut service, uri, 1, &source).await;

    let published = wait_for_publish(&mut socket, uri).await;
    let diags = published["diagnostics"].as_array().expect("diagnostics array");
    assert_eq!(diags.len(), 1, "oversized input should suppress lint diagnostics");
    assert!(
        diags[0]["message"]
            .as_str()
            .is_some_and(|msg| msg.contains("exceeds the mdwright LSP limit")),
        "unexpected size diagnostic: {diags:?}",
    );

    let body = request(
        &mut service,
        49,
        "textDocument/formatting",
        json!({
            "textDocument": { "uri": uri },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    assert!(body.is_null(), "oversized document returns no format edits");
}

#[tokio::test]
async fn root_config_discovery_controls_lsp_diagnostics() {
    let temp = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        temp.path().join(".mdwright.toml"),
        "[lint]\npreset = \"default\"\nignore = [\"bare-url\"]\n",
    )
    .expect("write config");
    let root_uri = Url::from_directory_path(temp.path())
        .expect("directory uri")
        .to_string();
    let (mut service, socket) = build_service_for_tests();
    let _body = initialize_with_root(&mut service, true, Some(&root_uri)).await;

    let uri = Url::from_file_path(temp.path().join("doc.md"))
        .expect("file uri")
        .to_string();
    open_doc(&mut service, &uri, 1, "See https://example.com now.\n").await;

    let published = wait_for_publish(socket, &uri).await;
    let diags = published["diagnostics"].as_array().expect("diagnostics array");
    assert!(
        diags.iter().all(|diag| diag["code"].as_str() != Some("bare-url")),
        "root config should disable bare-url diagnostics: {diags:?}",
    );
}

#[tokio::test]
async fn config_reload_recomputes_parse_options_for_open_documents() {
    let temp = tempfile::tempdir().expect("tempdir");
    let config_path = temp.path().join(".mdwright.toml");
    std::fs::write(
        &config_path,
        "[fmt]\nheading-attrs = \"canonicalise\"\n[parse.extensions]\nheading-attribute-lists = false\n",
    )
    .expect("write config");
    let root_uri = Url::from_directory_path(temp.path())
        .expect("directory uri")
        .to_string();
    let (mut service, mut socket) = build_service_for_tests();
    let _body = initialize_with_root(&mut service, true, Some(&root_uri)).await;

    let uri = Url::from_file_path(temp.path().join("doc.md"))
        .expect("file uri")
        .to_string();
    let source = "# Title {.b #a}\n";
    open_doc(&mut service, &uri, 1, source).await;
    let _initial_publish = wait_for_publish(&mut socket, &uri).await;

    let body = request(
        &mut service,
        50,
        "textDocument/formatting",
        json!({
            "textDocument": { "uri": uri },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    assert!(
        body.as_array().is_some_and(Vec::is_empty),
        "disabled heading attrs should leave source unchanged: {body:?}",
    );

    std::fs::write(
        &config_path,
        "[fmt]\nheading-attrs = \"canonicalise\"\n[parse.extensions]\nheading-attribute-lists = true\n",
    )
    .expect("rewrite config");
    let _ack = service
        .ready()
        .await
        .expect("service ready")
        .call(notif(
            "workspace/didChangeWatchedFiles",
            json!({
                "changes": [{
                    "uri": Url::from_file_path(&config_path).expect("config uri").to_string(),
                    "type": 2
                }],
            }),
        ))
        .await
        .expect("call ok");
    let _reload_publish = wait_for_publish(&mut socket, &uri).await;

    let body = request(
        &mut service,
        51,
        "textDocument/formatting",
        json!({
            "textDocument": { "uri": uri },
            "options": { "tabSize": 4, "insertSpaces": true },
        }),
    )
    .await;
    let edits = body.as_array().cloned().expect("formatting edits after reload");
    assert!(
        edits
            .iter()
            .any(|edit| edit["newText"].as_str() == Some("# Title {#a .b}\n")),
        "config reload should rebuild document facts with updated parse policy: {edits:?}",
    );
}

async fn wait_for_publish<S>(mut socket: S, uri: &str) -> Value
where
    S: futures::Stream<Item = Request> + Unpin,
{
    let timeout = Duration::from_secs(5);
    loop {
        let next = tokio::time::timeout(timeout, socket.next()).await;
        let Ok(Some(msg)) = next else {
            panic!("socket closed or timed out waiting for publishDiagnostics for {uri}");
        };
        if msg.method() == "textDocument/publishDiagnostics" {
            let params = msg.params().cloned().unwrap_or(Value::Null);
            if params.get("uri").and_then(Value::as_str) == Some(uri) {
                return params;
            }
        }
    }
}