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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Types for sending data to and from the language client.

use futures::{
    channel::{mpsc, oneshot},
    future::{self, Shared},
    select,
    sink::SinkExt,
    FutureExt,
};
use std::{
    convert::TryFrom,
    fmt::{self, Debug, Formatter},
    future::Future,
    pin::Pin,
    sync::{
        atomic::{AtomicBool, AtomicU64, Ordering},
        Arc,
    },
};

type TokenFuture = Shared<Pin<Box<dyn Future<Output = Result<(), oneshot::Canceled>> + Send>>>;

/// A structure used to construct and cancel [`CancellationToken`].
pub struct TokenCanceller {
    cancelled: Arc<AtomicBool>,
    future: TokenFuture,
    sender: Option<oneshot::Sender<()>>,
}

impl TokenCanceller {
    /// Creates a new [`TokenCanceller`].
    pub fn new() -> Self {
        let cancelled = Arc::new(AtomicBool::new(false));
        let (sender, receiver) = oneshot::channel();
        let future = receiver.boxed().shared();
        TokenCanceller {
            cancelled,
            future,
            sender: Some(sender),
        }
    }

    /// Signals to all subordinate [`CancellationToken`] that they have been cancelled.
    pub fn cancel(&mut self) {
        if let Some(sender) = self.sender.take() {
            if let Ok(()) = sender.send(()) {
                self.cancelled.store(true, Ordering::SeqCst);
            } else {
                unreachable!()
            }
        }
    }

    /// Create a subordinate [`CancellationToken`].
    pub fn token(&self) -> CancellationToken {
        let cancelled = self.cancelled.clone();
        let future = self.future.clone();
        CancellationToken { cancelled, future }
    }
}

impl Debug for TokenCanceller {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_struct(stringify!(LspService))
            .field("cancelled", &self.cancelled)
            .field("sender", &self.sender)
            .finish()
    }
}

impl Default for TokenCanceller {
    fn default() -> Self {
        Self::new()
    }
}

/// A token which listens for a cancellation signal from a [`TokenCanceller`].
#[derive(Debug, Clone)]
pub struct CancellationToken {
    cancelled: Arc<AtomicBool>,
    future: TokenFuture,
}

impl CancellationToken {
    /// Checks whether the cancellation signal has been given.
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::SeqCst)
    }

    /// Returns a future which only resolves once the cancellation signal has been given.
    pub fn wait(&self) -> TokenFuture {
        self.future.clone()
    }
}

impl Default for CancellationToken {
    fn default() -> Self {
        let cancelled = Default::default();
        let future = future::pending().boxed().shared();
        CancellationToken { cancelled, future }
    }
}

struct ClientInner {
    sender: mpsc::Sender<crate::jsonrpc::Outgoing>,
    request_id: AtomicU64,
    pending_requests: Arc<crate::jsonrpc::ClientRequests>,
    state: Arc<crate::server::State>,
}

/// Handle for communicating with the language client.
///
/// This type provides a very cheap implementation of [`Clone`] so API consumers can cheaply clone
/// and pass it around as needed.
///
/// [`Clone`]: trait@std::clone::Clone
#[derive(Clone)]
pub struct Client {
    inner: Arc<ClientInner>,
}

impl Client {
    pub(super) fn new(
        sender: mpsc::Sender<crate::jsonrpc::Outgoing>,
        pending_requests: Arc<crate::jsonrpc::ClientRequests>,
        state: Arc<crate::server::State>,
    ) -> Self {
        Client {
            inner: Arc::new(ClientInner {
                sender,
                request_id: AtomicU64::new(0),
                pending_requests,
                state,
            }),
        }
    }

    /// Notifies the client to log a particular message.
    ///
    /// This corresponds to the [`window/logMessage`] notification.
    ///
    /// [`window/logMessage`]: https://microsoft.github.io/language-server-protocol/specification#window_logMessage
    pub async fn log_message<M: std::fmt::Display>(&self, typ: lsp::MessageType, message: M) {
        let message = message.to_string();
        let params = lsp::LogMessageParams { typ, message };
        self.send_notification::<lsp::notification::LogMessage>(params).await;
    }

    /// Notifies the client to display a particular message in the user interface.
    ///
    /// This corresponds to the [`window/showMessage`] notification.
    ///
    /// [`window/showMessage`]: https://microsoft.github.io/language-server-protocol/specification#window_showMessage
    pub async fn show_message<M: std::fmt::Display>(&self, typ: lsp::MessageType, message: M) {
        let message = message.to_string();
        let params = lsp::ShowMessageParams { typ, message };
        self.send_notification::<lsp::notification::ShowMessage>(params).await;
    }

    /// Requests the client to display a particular message in the user interface.
    ///
    /// Unlike the `show_message` notification, this request can also pass a list of actions and
    /// wait for an answer from the client.
    ///
    /// This corresponds to the [`window/showMessageRequest`] request.
    ///
    /// [`window/showMessageRequest`]: https://microsoft.github.io/language-server-protocol/specification#window_showMessageRequest
    #[rustfmt::skip]
    pub async fn show_message_request<M: std::fmt::Display>(
        &self,
        typ: lsp::MessageType,
        message: M,
        actions: Option<Vec<lsp::MessageActionItem>>,
    ) -> crate::jsonrpc::Result<Option<lsp::MessageActionItem>> {
        let token = CancellationToken::default();
        let message = message.to_string();
        let params = lsp::ShowMessageRequestParams { typ, message, actions };
        self.send_request::<lsp::request::ShowMessageRequest>(params, token).await
    }

    /// Notifies the client to log a telemetry event.
    ///
    /// This corresponds to the [`telemetry/event`] notification.
    ///
    /// [`telemetry/event`]: https://microsoft.github.io/language-server-protocol/specification#telemetry_event
    pub async fn telemetry_event<S: serde::Serialize>(&self, data: S) {
        match serde_json::to_value(data) {
            Err(e) => log::error!("invalid JSON in `telemetry/event` notification: {}", e),
            Ok(mut value) => {
                if !value.is_null() && !value.is_array() && !value.is_object() {
                    value = serde_json::Value::Array(vec![value]);
                }
                self.send_notification::<lsp::notification::TelemetryEvent>(value).await;
            },
        }
    }

    /// Registers a new capability with the client.
    ///
    /// This corresponds to the [`client/registerCapability`] request.
    ///
    /// [`client/registerCapability`]: https://microsoft.github.io/language-server-protocol/specification#client_registerCapability
    ///
    /// # Initialization
    ///
    /// If the request is sent to client before the server has been initialized, this will
    /// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
    ///
    /// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
    #[rustfmt::skip]
    pub async fn register_capability(&self, registrations: Vec<lsp::Registration>) -> crate::jsonrpc::Result<()> {
        let token = CancellationToken::default();
        let params = lsp::RegistrationParams { registrations };
        self.send_request_initialized::<lsp::request::RegisterCapability>(params, token).await
    }

    /// Unregisters a capability with the client.
    ///
    /// This corresponds to the [`client/unregisterCapability`] request.
    ///
    /// [`client/unregisterCapability`]: https://microsoft.github.io/language-server-protocol/specification#client_unregisterCapability
    ///
    /// # Initialization
    ///
    /// If the request is sent to client before the server has been initialized, this will
    /// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
    ///
    /// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
    #[rustfmt::skip]
    pub async fn unregister_capability(
        &self,
        unregisterations: Vec<lsp::Unregistration>,
    ) -> crate::jsonrpc::Result<()> {
        let token = CancellationToken::default();
        let params = lsp::UnregistrationParams { unregisterations };
        self.send_request_initialized::<lsp::request::UnregisterCapability>(params, token).await
    }

    /// Fetches the current open list of workspace folders.
    ///
    /// Returns `None` if only a single file is open in the tool. Returns an empty `Vec` if a
    /// workspace is open but no folders are configured.
    ///
    /// This corresponds to the [`workspace/workspaceFolders`] request.
    ///
    /// [`workspace/workspaceFolders`]: https://microsoft.github.io/language-server-protocol/specification#workspace_workspaceFolders
    ///
    /// # Initialization
    ///
    /// If the request is sent to client before the server has been initialized, this will
    /// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
    ///
    /// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
    ///
    /// # Compatibility
    ///
    /// This request was introduced in specification version 3.6.0.
    #[rustfmt::skip]
    pub async fn workspace_folders(&self) -> crate::jsonrpc::Result<Option<Vec<lsp::WorkspaceFolder>>> {
        let token = CancellationToken::default();
        self.send_request_initialized::<lsp::request::WorkspaceFoldersRequest>((), token).await
    }

    /// Fetches configuration settings from the client.
    ///
    /// The request can fetch several configuration settings in one roundtrip. The order of the
    /// returned configuration settings correspond to the order of the passed
    /// [`ConfigurationItem`]s (e.g. the first item in the response is the result for the first
    /// configuration item in the params).
    ///
    /// [`ConfigurationItem`]: https://docs.rs/lsp-types/0.74.0/lsp_types/struct.ConfigurationItem.html
    ///
    /// This corresponds to the [`workspace/configuration`] request.
    ///
    /// [`workspace/configuration`]: https://microsoft.github.io/language-server-protocol/specification#workspace_configuration
    ///
    /// # Initialization
    ///
    /// If the request is sent to client before the server has been initialized, this will
    /// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
    ///
    /// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
    ///
    /// # Compatibility
    ///
    /// This request was introduced in specification version 3.6.0.
    #[rustfmt::skip]
    pub async fn configuration(
        &self,
        items: Vec<lsp::ConfigurationItem>,
    ) -> crate::jsonrpc::Result<Vec<serde_json::Value>> {
        let token = CancellationToken::default();
        let params = lsp::ConfigurationParams { items };
        self.send_request_initialized::<lsp::request::WorkspaceConfiguration>(params, token).await
    }

    /// Requests a workspace resource be edited on the client side and returns whether the edit was
    /// applied.
    ///
    /// This corresponds to the [`workspace/applyEdit`] request.
    ///
    /// [`workspace/applyEdit`]: https://microsoft.github.io/language-server-protocol/specification#workspace_applyEdit
    ///
    /// # Initialization
    ///
    /// If the request is sent to client before the server has been initialized, this will
    /// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
    ///
    /// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
    #[rustfmt::skip]
    pub async fn apply_edit(
        &self,
        edit: lsp::WorkspaceEdit,
        label: Option<String>,
    ) -> crate::jsonrpc::Result<lsp::ApplyWorkspaceEditResponse> {
        let token = CancellationToken::default();
        let params = lsp::ApplyWorkspaceEditParams { label, edit };
        self.send_request_initialized::<lsp::request::ApplyWorkspaceEdit>(params, token).await
    }

    /// Submits validation diagnostics for an open file with the given URI.
    ///
    /// This corresponds to the [`textDocument/publishDiagnostics`] notification.
    ///
    /// [`textDocument/publishDiagnostics`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_publishDiagnostics
    ///
    /// # Initialization
    ///
    /// This notification will only be sent if the server is initialized.
    #[rustfmt::skip]
    pub async fn publish_diagnostics(&self, uri: lsp::Url, diags: Vec<lsp::Diagnostic>, version: Option<i32>) {
        let params = lsp::PublishDiagnosticsParams::new(uri, diags, version);
        self.send_notification_initialized::<lsp::notification::PublishDiagnostics>(params).await;
    }

    /// Sends a custom notification to the client.
    ///
    /// # Initialization
    ///
    /// This notification will only be sent if the server is initialized.
    pub async fn send_custom_notification<N>(&self, params: N::Params)
    where
        N: lsp::notification::Notification,
    {
        self.send_notification_initialized::<N>(params).await;
    }

    async fn send_notification<N>(&self, params: N::Params)
    where
        N: lsp::notification::Notification,
    {
        let mut sender = self.inner.sender.clone();
        let message = crate::jsonrpc::Outgoing::Request(crate::jsonrpc::ClientRequest::notification::<N>(params));
        if sender.send(message).await.is_err() {
            log::error!("failed to send notification")
        }
    }

    async fn send_notification_initialized<N>(&self, params: N::Params)
    where
        N: lsp::notification::Notification,
    {
        if let crate::server::StateKind::Initialized | crate::server::StateKind::ShutDown = self.inner.state.get() {
            self.send_notification::<N>(params).await;
        } else {
            let msg = crate::jsonrpc::ClientRequest::notification::<N>(params);
            log::trace!("server not initialized, supressing message: {}", msg);
        }
    }

    /// Sends a custom request to the client.
    ///
    /// # Initialization
    ///
    /// This request will only be sent if the server is initialized.
    pub async fn send_custom_request<R>(
        &self,
        params: R::Params,
        token: CancellationToken,
    ) -> crate::jsonrpc::Result<R::Result>
    where
        R: lsp::request::Request,
    {
        self.send_request_initialized::<R>(params, token).await
    }

    async fn send_request<R>(&self, params: R::Params, token: CancellationToken) -> crate::jsonrpc::Result<R::Result>
    where
        R: lsp::request::Request,
    {
        let id = self.inner.request_id.fetch_add(1, Ordering::Relaxed);
        let message = crate::jsonrpc::Outgoing::Request(crate::jsonrpc::ClientRequest::request::<R>(id, params));

        let response_waiter = self.inner.pending_requests.wait(crate::jsonrpc::Id::Number(id));

        if self.inner.sender.clone().send(message).await.is_err() {
            log::error!("failed to send request");
            return Err(crate::jsonrpc::Error::internal_error());
        }

        select! {
            _ = token.wait() => {
                if self.inner.pending_requests.0.remove(&crate::jsonrpc::Id::Number(id)).is_none() {
                    log::warn!("received response with unknown request ID: {}", id);
                }
                let params = {
                    let id = i32::try_from(id).expect("error converting u64 to i32");
                    lsp::CancelParams { id: lsp::NumberOrString::Number(id) }
                };
                self.send_notification::<lsp::notification::Cancel>(params).await;
                Err(crate::jsonrpc::Error::request_cancelled())
            },
            response = response_waiter.fuse() => {
                let (_, result) = response.into_parts();
                result.and_then(|v| {
                    serde_json::from_value(v).map_err(|e| crate::jsonrpc::Error {
                        code: crate::jsonrpc::ErrorCode::ParseError,
                        message: e.to_string(),
                        data: None,
                    })
                })
            },
        }
    }

    async fn send_request_initialized<R>(
        &self,
        params: R::Params,
        token: CancellationToken,
    ) -> crate::jsonrpc::Result<R::Result>
    where
        R: lsp::request::Request,
    {
        if let crate::server::StateKind::Initialized | crate::server::StateKind::ShutDown = self.inner.state.get() {
            self.send_request::<R>(params, token).await
        } else {
            let id = self.inner.request_id.load(Ordering::SeqCst) + 1;
            let msg = crate::jsonrpc::ClientRequest::request::<R>(id, params);
            log::trace!("server not initialized, supressing message: {}", msg);
            Err(crate::jsonrpc::not_initialized_error())
        }
    }
}

impl Debug for Client {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_struct(stringify!(Client))
            .field("request_id", &self.inner.request_id)
            .field("pending_requests", &self.inner.pending_requests)
            .field("state", &self.inner.state)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod cancellation_token {
        use super::*;

        #[test]
        fn is_cancelled() {
            let mut canceller = TokenCanceller::default();
            let token = canceller.token();
            assert!(!token.is_cancelled());
            canceller.cancel();
            assert!(token.is_cancelled());
        }
    }

    mod client {
        use super::*;
        use crate::jsonrpc::{ClientRequest, Id, Outgoing, Response};
        use futures::StreamExt;
        use serde_json::json;

        mod helper {
            use super::*;
            use crate::jsonrpc::Outgoing;
            use futures::channel::mpsc;

            pub(super) fn client(initialize: bool) -> (Client, mpsc::Receiver<Outgoing>) {
                let state = Arc::new(crate::server::State::new());
                let (tx, rx) = mpsc::channel(4);
                let pending_client = Arc::new(crate::jsonrpc::ClientRequests::new());
                let client = crate::client::Client::new(tx, pending_client, state);
                if initialize {
                    client.inner.state.set(crate::server::StateKind::Initialized);
                }
                (client, rx)
            }
        }

        #[tokio::test]
        async fn apply_edit() -> anyhow::Result<()> {
            let (client, _rx) = helper::client(false);

            let req = {
                let edit = lsp::WorkspaceEdit::default();
                let label = Default::default();
                client.apply_edit(edit, label)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(lsp::ApplyWorkspaceEditResponse {
                    applied: Default::default(),
                    failure_reason: Default::default(),
                    failed_change: Default::default(),
                })
                .unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };
            let (result, ()) = futures::future::join(req, rsp).await;
            assert_eq!(result, Err(crate::jsonrpc::not_initialized_error()));

            Ok(())
        }

        #[tokio::test]
        async fn configuration() -> anyhow::Result<()> {
            let (client, _rx) = helper::client(false);

            let req = {
                let items = Default::default();
                client.configuration(items)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(Vec::<serde_json::Value>::new()).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };
            let (result, ()) = futures::future::join(req, rsp).await;
            assert_eq!(result, Err(crate::jsonrpc::not_initialized_error()));

            Ok(())
        }

        #[test]
        fn display() {
            let client = helper::client(true).0;
            let _ = format!("{:?}", client);
        }

        #[test]
        fn new() {
            let client = helper::client(false).0;
            assert_eq!(client.inner.state.get(), crate::server::StateKind::Uninitialized);
        }

        #[tokio::test]
        async fn log_message() {
            let (client, mut rx) = helper::client(true);
            let typ = lsp::MessageType::Info;
            let message = String::default();
            client.log_message(typ, message.clone()).await;
            if let Some(item) = rx.next().await {
                let params = lsp::LogMessageParams { typ, message };
                let message = Outgoing::Request(ClientRequest::notification::<lsp::notification::LogMessage>(params));
                assert_eq!(item, message);
            }
        }

        #[tokio::test]
        async fn publish_diagnostics() {
            let (client, mut rx) = helper::client(true);
            let uri = lsp::Url::parse("inmemory::///test").unwrap();
            let diags = Vec::<lsp::Diagnostic>::new();
            let version = Option::<i32>::default();
            client.publish_diagnostics(uri.clone(), diags.clone(), version).await;
            if let Some(item) = rx.next().await {
                let params = lsp::PublishDiagnosticsParams {
                    uri,
                    diagnostics: diags,
                    version,
                };
                let message = Outgoing::Request(ClientRequest::notification::<lsp::notification::PublishDiagnostics>(
                    params,
                ));
                assert_eq!(item, message);
            }
        }

        #[tokio::test]
        async fn register_capability() -> anyhow::Result<()> {
            let (client, _rx) = helper::client(true);

            let req = {
                let registrations = Default::default();
                client.register_capability(registrations)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(()).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };
            let (result, ()) = futures::future::join(req, rsp).await;
            assert_eq!(result, Ok(()));

            Ok(())
        }

        #[tokio::test]
        async fn send_notification_initialized_when_uninitialized() {
            let (client, _rx) = helper::client(false);
            let uri = lsp::Url::parse("inmemory::///test").unwrap();
            let diags = Vec::<lsp::Diagnostic>::new();
            let version = Option::<i32>::default();
            client.publish_diagnostics(uri.clone(), diags.clone(), version).await;
        }

        #[tokio::test]
        async fn send_custom_notification() {
            use serde::{Deserialize, Serialize};

            #[derive(Debug, Deserialize, Serialize)]
            struct CustomNotificationParams;

            enum CustomNotification {}

            impl lsp::notification::Notification for CustomNotification {
                type Params = CustomNotificationParams;

                const METHOD: &'static str = "custom/notification";
            }

            let (client, mut rx) = helper::client(true);
            let params = CustomNotificationParams;
            client.send_custom_notification::<CustomNotification>(params).await;

            if let Some(item) = rx.next().await {
                let params = CustomNotificationParams;
                let request = ClientRequest::notification::<CustomNotification>(params);
                let message = Outgoing::Request(request);
                assert_eq!(item, message);
            }
        }

        #[tokio::test]
        async fn send_custom_request() {
            use serde::{Deserialize, Serialize};

            #[derive(Debug, Deserialize, Serialize)]
            struct CustomRequestParams;

            enum CustomRequest {}

            impl lsp::request::Request for CustomRequest {
                type Params = CustomRequestParams;
                type Result = ();

                const METHOD: &'static str = "custom/request";
            }

            let mut canceller = TokenCanceller::new();
            let token = canceller.token();

            let (client, _rx) = helper::client(true);
            let req = {
                let params = CustomRequestParams;
                client.send_custom_request::<CustomRequest>(params, token)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(()).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };

            let cancel = async {
                tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
                canceller.cancel();
            };

            let (result, (), ()) = futures::future::join3(req, rsp, cancel).await;
            assert_eq!(result, Ok(()));
        }

        #[tokio::test]
        async fn send_custom_request_with_invalid_response() {
            use serde::{Deserialize, Serialize};

            #[derive(Debug, Deserialize, Serialize)]
            struct CustomRequestParams;

            enum CustomRequest {}

            impl lsp::request::Request for CustomRequest {
                type Params = CustomRequestParams;
                type Result = ();

                const METHOD: &'static str = "custom/request";
            }

            let (client, _rx) = helper::client(true);
            let req = {
                let token = CancellationToken::default();
                let params = CustomRequestParams;
                client.send_custom_request::<CustomRequest>(params, token)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(u64::MAX).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };

            let (result, ()) = futures::future::join(req, rsp).await;
            assert!(matches!(
                result,
                Err(crate::jsonrpc::Error {
                    code: crate::jsonrpc::ErrorCode::ParseError,
                    ..
                })
            ));
        }

        #[tokio::test]
        async fn send_custom_request_and_cancel() {
            use serde::{Deserialize, Serialize};

            #[derive(Debug, Deserialize, Serialize)]
            struct CustomRequestParams;

            enum CustomRequest {}

            impl lsp::request::Request for CustomRequest {
                type Params = CustomRequestParams;
                type Result = u64;

                const METHOD: &'static str = "custom/request";
            }

            let mut canceller = TokenCanceller::new();
            let token = canceller.token();

            let (client, _rx) = helper::client(true);
            let req = {
                let params = CustomRequestParams;
                client.send_custom_request::<CustomRequest>(params, token)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(()).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };

            canceller.cancel();

            let (result, ()) = futures::future::join(req, rsp).await;
            assert_eq!(result, Err(crate::jsonrpc::Error::request_cancelled()));
        }

        #[tokio::test]
        async fn show_message() {
            let (client, mut rx) = helper::client(true);
            let typ = lsp::MessageType::Info;
            let message = String::default();
            client.show_message(typ, message.clone()).await;
            if let Some(item) = rx.next().await {
                let params = lsp::ShowMessageParams { typ, message };
                let message = Outgoing::Request(ClientRequest::notification::<lsp::notification::ShowMessage>(params));
                assert_eq!(item, message);
            }
        }

        #[tokio::test]
        async fn show_message_request() -> anyhow::Result<()> {
            let (client, _rx) = helper::client(true);

            let typ = lsp::MessageType::Info;
            let message = String::default();
            let actions = Default::default();

            let req = client.show_message_request(typ, message.clone(), actions);
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(None::<lsp::MessageActionItem>).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };
            let (result, ()) = futures::future::join(req, rsp).await;
            assert!(result.is_ok());

            Ok(())
        }

        #[tokio::test]
        async fn telemetry_event() {
            let (client, mut rx) = helper::client(true);
            client.telemetry_event(42u8).await;
            if let Some(item) = rx.next().await {
                let params = json!([42u8]);
                let message =
                    Outgoing::Request(ClientRequest::notification::<lsp::notification::TelemetryEvent>(params));
                assert_eq!(item, message);
            }
        }

        #[tokio::test]
        async fn unregister_capability() -> anyhow::Result<()> {
            let (client, _rx) = helper::client(true);

            let req = {
                let unregistrations = Default::default();
                client.unregister_capability(unregistrations)
            };
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(()).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };
            let (result, ()) = futures::future::join(req, rsp).await;
            assert_eq!(result, Ok(()));

            Ok(())
        }

        #[tokio::test]
        async fn workspace_folders() -> anyhow::Result<()> {
            let (client, _rx) = helper::client(true);

            let req = client.workspace_folders();
            let rsp = async {
                let id = Id::Number(0);
                let result = serde_json::to_value(None::<Vec<lsp::WorkspaceFolder>>).unwrap();
                client.inner.pending_requests.insert(Response::ok(id, result));
            };
            let (result, ()) = futures::future::join(req, rsp).await;
            assert_eq!(result, Ok(None));

            Ok(())
        }
    }

    mod token_canceller {
        use super::*;

        #[test]
        fn debug() {
            let canceller = TokenCanceller::new();
            format!("{:?}", canceller);
        }

        #[test]
        fn default() {
            let canceller = TokenCanceller::default();
            format!("{:?}", canceller);
        }
    }
}