lsp_web/
client.rs

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
use std::{
    collections::HashMap,
    fmt::Display,
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use bevy_ecs::system::Resource;
use futures::FutureExt;
use lsp_core::client::{Client, ClientSync, Resp};
use lsp_types::{Diagnostic, MessageType, Url};
use tracing::info;

use crate::fetch::local_fetch;

#[derive(Resource, Clone)]
pub struct WebClient {
    client: tower_lsp::Client,
}
impl WebClient {
    pub fn new(client: tower_lsp::Client) -> Self {
        Self { client }
    }
}

struct Sendable<T>(pub T);

// Safety: WebAssembly will only ever run in a single-threaded context.
unsafe impl<T> Send for Sendable<T> {}
impl<O, T> Future for Sendable<T>
where
    T: Future<Output = O>,
{
    type Output = O;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // Safely access the inner future
        let inner = unsafe { self.map_unchecked_mut(|s| &mut s.0) };
        inner.poll(cx)
    }
}

impl ClientSync for WebClient {
    fn spawn<F: std::future::Future<Output = ()> + 'static>(&self, fut: F) {
        let _ = wasm_bindgen_futures::future_to_promise(async {
            fut.await;
            Ok("Good".into())
        });
    }

    fn fetch(
        &self,
        url: &str,
        headers: &HashMap<String, String>,
    ) -> Pin<Box<dyn Send + std::future::Future<Output = Result<Resp, String>>>> {
        tracing::info!("Fetching {}", url);
        let (tx, rx) = futures::channel::oneshot::channel();
        let url = url.to_string();
        let headers = headers.clone();
        let fut = async move {
            let _ = local_fetch(url, headers, tx).await;
        };
        self.spawn(Sendable(fut));
        async move {
            match rx.await {
                Ok(x) => x,
                Err(_) => Err(String::from("Canceled")),
            }
        }
        .boxed()
    }
}

#[tower_lsp::async_trait]
impl Client for WebClient {
    async fn log_message<M: Display + Sync + Send + 'static>(&self, ty: MessageType, msg: M) -> () {
        self.client.log_message(ty, msg).await;
    }

    async fn publish_diagnostics(
        &self,
        uri: Url,
        diags: Vec<Diagnostic>,
        version: Option<i32>,
    ) -> () {
        info!(
            "Publishing {} diagnostics {} (version {:?})",
            diags.len(),
            uri,
            version
        );
        self.client.publish_diagnostics(uri, diags, version).await;
    }
}