lspf 0.9.1

A Rust framework for building extensible LSP language servers
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
//! ClientHandle-owned workspace queries leave the Workspace snapshot untouched
//! (issue #105).
//!
//! `ClientHandle::configuration` and `ClientHandle::workspace_folders` are read-only
//! queries: their results go to the caller and are never written into the
//! framework-owned Workspace state. The configuration snapshot stays under
//! `workspace/didChangeConfiguration` sync and the folder list stays under
//! `workspace/didChangeWorkspaceFolders` sync — a successful query result must
//! not disturb either, and later sync notifications must still apply.

use std::borrow::Cow;
use std::sync::{Arc, Mutex};

use bytes::Bytes;
use lspf::types::request::Request;
use lspf::types::{ConfigurationParams, WorkspaceFolder};
use lspf::{
    CancellationToken, ClientError, RawMessage, RequestId, Server, ServerContext, Transport,
    TransportError, TransportReader, TransportWriter,
};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::mpsc;

// --- Registrations -----------------------------------------------------------

/// A request whose handler runs the configuration query and records both the
/// helper result and the Workspace configuration snapshot it observed after
/// the query completed.
enum ConfigTrigger {}

impl Request for ConfigTrigger {
    type Params = ConfigurationParams;
    type Result = String;
    const METHOD: &'static str = "test/configTrigger";
}

/// A request whose handler runs the workspace-folders query and records both
/// the helper result and the Workspace folders it observed after the query
/// completed.
enum FoldersTrigger {}

impl Request for FoldersTrigger {
    type Params = ();
    type Result = String;
    const METHOD: &'static str = "test/foldersTrigger";
}

/// A probe returning the current Workspace configuration snapshot and folder
/// list, so the test can inspect framework state without sharing it.
#[derive(Debug, Deserialize, Serialize)]
struct WorkspaceProbeResult {
    configuration: Option<serde_json::Value>,
    folders: Vec<WorkspaceFolder>,
}

enum WorkspaceProbe {}

impl Request for WorkspaceProbe {
    type Params = ();
    type Result = WorkspaceProbeResult;
    const METHOD: &'static str = "test/workspaceProbe";
}

// --- Harness -----------------------------------------------------------------

struct ChannelTransport {
    incoming: mpsc::UnboundedReceiver<RawMessage>,
    outgoing: mpsc::UnboundedSender<RawMessage>,
}

struct ChannelReader(mpsc::UnboundedReceiver<RawMessage>);
struct ChannelWriter(mpsc::UnboundedSender<RawMessage>);

impl Transport for ChannelTransport {
    type Reader = ChannelReader;
    type Writer = ChannelWriter;

    fn split(self) -> (Self::Reader, Self::Writer) {
        (ChannelReader(self.incoming), ChannelWriter(self.outgoing))
    }
}

impl TransportReader for ChannelReader {
    async fn recv(&mut self) -> Result<RawMessage, TransportError> {
        self.0.recv().await.ok_or(TransportError::Closed)
    }
}

impl TransportWriter for ChannelWriter {
    async fn send(&mut self, message: RawMessage) -> Result<(), TransportError> {
        self.0.send(message).map_err(|_| TransportError::Closed)
    }

    async fn shutdown(self) -> Result<(), TransportError> {
        Ok(())
    }
}

struct Session {
    in_tx: mpsc::UnboundedSender<RawMessage>,
    out_rx: mpsc::UnboundedReceiver<RawMessage>,
    serve: tokio::task::JoinHandle<lspf::Result<lspf::Outcome>>,
}

fn inbound_request(id: i32, method: &'static str, params: serde_json::Value) -> RawMessage {
    RawMessage::Request {
        id: RequestId::Number(id),
        method: Cow::Borrowed(method),
        params: Bytes::from(serde_json::to_vec(&params).unwrap()),
    }
}

fn notification(method: &'static str, params: serde_json::Value) -> RawMessage {
    RawMessage::Notification {
        method: Cow::Borrowed(method),
        params: Bytes::from(serde_json::to_vec(&params).unwrap()),
    }
}

fn success_response(id: i32, result: serde_json::Value) -> RawMessage {
    RawMessage::Response {
        id: RequestId::Number(id),
        result: Ok(Bytes::from(serde_json::to_vec(&result).unwrap())),
    }
}

fn exit() -> RawMessage {
    RawMessage::Notification {
        method: Cow::Borrowed("exit"),
        params: Bytes::from_static(b"null"),
    }
}

async fn receive(outgoing: &mut mpsc::UnboundedReceiver<RawMessage>) -> RawMessage {
    tokio::time::timeout(std::time::Duration::from_secs(2), outgoing.recv())
        .await
        .expect("server output before watchdog timeout")
        .expect("server output channel remains open")
}

async fn start(server: Server<()>) -> Session {
    let (in_tx, incoming) = mpsc::unbounded_channel();
    let (outgoing, out_rx) = mpsc::unbounded_channel();
    let serve = tokio::spawn(server.serve(ChannelTransport { incoming, outgoing }));
    Session {
        in_tx,
        out_rx,
        serve,
    }
}

async fn init_with_params(session: &mut Session, params: serde_json::Value) {
    session
        .in_tx
        .send(inbound_request(1, "initialize", params))
        .unwrap();
    let response = receive(&mut session.out_rx).await;
    assert_eq!(response.id(), Some(&RequestId::Number(1)));
}

async fn finish(session: Session) {
    session.in_tx.send(exit()).unwrap();
    session
        .serve
        .await
        .expect("serve task did not panic")
        .expect("serve ended cleanly");
}

fn decode_result<T: DeserializeOwned>(response: &RawMessage) -> T {
    match response {
        RawMessage::Response {
            result: Ok(bytes), ..
        } => serde_json::from_slice(bytes).expect("the result decodes"),
        other => panic!("expected a success response, got {other:?}"),
    }
}

/// The numeric ID of an outbound request, which is how the mock peer answers
/// it.
fn outbound_number_id(message: &RawMessage) -> i32 {
    match message {
        RawMessage::Request { id, .. } => match id {
            RequestId::Number(n) => *n,
            _ => panic!("expected a numeric outbound request id"),
        },
        other => panic!("expected a request, got {other:?}"),
    }
}

/// Send a workspace probe request and return the decoded snapshot.
async fn probe(session: &mut Session, id: i32) -> WorkspaceProbeResult {
    session
        .in_tx
        .send(inbound_request(id, WorkspaceProbe::METHOD, json!(null)))
        .unwrap();
    let response = receive(&mut session.out_rx).await;
    assert_eq!(response.id(), Some(&RequestId::Number(id)));
    decode_result(&response)
}

// --- Tests -------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn configuration_result_never_enters_the_workspace_snapshot() {
    type Recorded = (
        Result<Vec<serde_json::Value>, ClientError>,
        Option<serde_json::Value>,
    );
    let recorded: Arc<Mutex<Option<Recorded>>> = Arc::new(Mutex::new(None));

    let recorded_handler = Arc::clone(&recorded);
    let server = Server::builder(())
        .request::<ConfigTrigger, _, _>(
            move |_state: Arc<()>,
                  ctx: ServerContext,
                  params: ConfigurationParams,
                  _ct: CancellationToken| {
                let recorded = Arc::clone(&recorded_handler);
                async move {
                    let result = ctx.client().configuration(params).await;
                    let snapshot = ctx.workspace().configuration();
                    *recorded.lock().unwrap() = Some((result, snapshot));
                    Ok("triggered".to_string())
                }
            },
        )
        .request::<WorkspaceProbe, _, _>(
            |_state: Arc<()>, ctx: ServerContext, _params: (), _ct: CancellationToken| async move {
                Ok(WorkspaceProbeResult {
                    configuration: ctx.workspace().configuration(),
                    folders: ctx.workspace().folders(),
                })
            },
        )
        .build()
        .expect("the workspace-query server builds");

    let mut session = start(server).await;
    init_with_params(
        &mut session,
        json!({ "processId": null, "rootUri": null, "capabilities": {} }),
    )
    .await;

    // Establish a known snapshot through the protocol notification path.
    session
        .in_tx
        .send(notification(
            "workspace/didChangeConfiguration",
            json!({ "settings": { "tabSize": 2 } }),
        ))
        .unwrap();

    // Trigger the query and pin the outgoing request's wire shape.
    session
        .in_tx
        .send(inbound_request(
            2,
            ConfigTrigger::METHOD,
            json!({ "items": [
                { "section": "editor" },
                { "section": "lspf.language" },
                { "section": "lspf.trace" },
            ] }),
        ))
        .unwrap();
    let outbound = receive(&mut session.out_rx).await;
    let RawMessage::Request { method, params, .. } = &outbound else {
        panic!("expected an outgoing request, got {outbound:?}")
    };
    assert_eq!(method.as_ref(), "workspace/configuration");
    assert_eq!(
        serde_json::from_slice::<serde_json::Value>(params).unwrap(),
        json!({ "items": [
            { "section": "editor" },
            { "section": "lspf.language" },
            { "section": "lspf.trace" },
        ] }),
        "the query items pass through verbatim"
    );

    // Answer with a result that differs from the snapshot and is one entry
    // shorter than the requested items: the framework must fill nothing in.
    let id = outbound_number_id(&outbound);
    session
        .in_tx
        .send(success_response(id, json!([{ "tabSize": 4 }, null])))
        .unwrap();

    // The trigger completes normally.
    let response = receive(&mut session.out_rx).await;
    assert_eq!(response.id(), Some(&RequestId::Number(2)));

    // The framework-owned snapshot is untouched by the query result.
    let snapshot = probe(&mut session, 3).await;
    assert_eq!(
        snapshot.configuration,
        Some(json!({ "tabSize": 2 })),
        "the query result never enters the configuration snapshot"
    );

    // The snapshot also stays live: a later notification still updates it.
    session
        .in_tx
        .send(notification(
            "workspace/didChangeConfiguration",
            json!({ "settings": { "tabSize": 8 } }),
        ))
        .unwrap();
    let snapshot = probe(&mut session, 4).await;
    assert_eq!(
        snapshot.configuration,
        Some(json!({ "tabSize": 8 })),
        "later didChangeConfiguration synchronization still applies"
    );

    finish(session).await;

    // The handler's recorded view agrees: the client's values came back in
    // order while the snapshot kept its pre-query value. The short reply
    // stays short — the framework does not fill in the missing entry.
    let (result, recorded_snapshot) = recorded.lock().unwrap().take().expect("recorded");
    assert_eq!(
        result.unwrap(),
        vec![json!({ "tabSize": 4 }), serde_json::Value::Null],
        "the client's order and length are preserved, with no filled-in values"
    );
    assert_eq!(recorded_snapshot, Some(json!({ "tabSize": 2 })));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn workspace_folders_result_never_overwrites_the_folder_snapshot() {
    type Recorded = (
        Result<Option<Vec<WorkspaceFolder>>, ClientError>,
        Vec<WorkspaceFolder>,
    );
    let recorded: Arc<Mutex<Option<Recorded>>> = Arc::new(Mutex::new(None));

    let recorded_handler = Arc::clone(&recorded);
    let server = Server::builder(())
        .request::<FoldersTrigger, _, _>(
            move |_state: Arc<()>, ctx: ServerContext, _params: (), _ct: CancellationToken| {
                let recorded = Arc::clone(&recorded_handler);
                async move {
                    let result = ctx.client().workspace_folders().await;
                    let snapshot = ctx.workspace().folders();
                    *recorded.lock().unwrap() = Some((result, snapshot));
                    Ok("triggered".to_string())
                }
            },
        )
        .request::<WorkspaceProbe, _, _>(
            |_state: Arc<()>, ctx: ServerContext, _params: (), _ct: CancellationToken| async move {
                Ok(WorkspaceProbeResult {
                    configuration: ctx.workspace().configuration(),
                    folders: ctx.workspace().folders(),
                })
            },
        )
        .build()
        .expect("the workspace-query server builds");

    let mut session = start(server).await;
    init_with_params(
        &mut session,
        json!({
            "processId": null,
            "rootUri": null,
            "capabilities": {},
            "workspaceFolders": [{ "uri": "file:///orig", "name": "orig" }],
        }),
    )
    .await;

    // Trigger the query and pin the outgoing request's wire shape.
    session
        .in_tx
        .send(inbound_request(2, FoldersTrigger::METHOD, json!(null)))
        .unwrap();
    let outbound = receive(&mut session.out_rx).await;
    let RawMessage::Request { method, params, .. } = &outbound else {
        panic!("expected an outgoing request, got {outbound:?}")
    };
    assert_eq!(method.as_ref(), "workspace/workspaceFolders");
    assert_eq!(
        serde_json::from_slice::<serde_json::Value>(params).unwrap(),
        serde_json::Value::Null,
        "the parameterless query sends null params"
    );

    // Answer with folders that differ from the initialized snapshot.
    let id = outbound_number_id(&outbound);
    session
        .in_tx
        .send(success_response(
            id,
            json!([{ "uri": "file:///other", "name": "other" }]),
        ))
        .unwrap();

    // The trigger completes normally.
    let response = receive(&mut session.out_rx).await;
    assert_eq!(response.id(), Some(&RequestId::Number(2)));

    // The framework-owned folder list is untouched by the query result.
    let snapshot = probe(&mut session, 3).await;
    assert_eq!(snapshot.folders.len(), 1);
    assert_eq!(snapshot.folders[0].uri.as_str(), "file:///orig");
    assert_eq!(
        snapshot.folders[0].name, "orig",
        "the query result never enters the folder list"
    );

    // The folder list also stays live: later synchronization still applies.
    session
        .in_tx
        .send(notification(
            "workspace/didChangeWorkspaceFolders",
            json!({ "event": {
                "added": [{ "uri": "file:///added", "name": "added" }],
                "removed": [],
            } }),
        ))
        .unwrap();
    let snapshot = probe(&mut session, 4).await;
    assert_eq!(snapshot.folders.len(), 2, "the added folder synchronized");
    assert_eq!(snapshot.folders[0].uri.as_str(), "file:///orig");
    assert_eq!(snapshot.folders[1].uri.as_str(), "file:///added");

    finish(session).await;

    // The handler's recorded view agrees: the client's folders came back while
    // the snapshot kept its pre-query list.
    let (result, recorded_snapshot) = recorded.lock().unwrap().take().expect("recorded");
    let queried = result.unwrap().expect("the client answered with folders");
    assert_eq!(queried.len(), 1);
    assert_eq!(queried[0].uri.as_str(), "file:///other");
    assert_eq!(queried[0].name, "other");
    assert_eq!(recorded_snapshot.len(), 1);
    assert_eq!(recorded_snapshot[0].uri.as_str(), "file:///orig");
}