vue_language_server_ext.rs

  1use anyhow::Context as _;
  2use gpui::WeakEntity;
  3use lsp::{LanguageServer, LanguageServerName};
  4use serde::{Deserialize, Serialize};
  5
  6use crate::LspStore;
  7
  8struct TypescriptServerRequest;
  9struct TypescriptServerResponse;
 10
 11#[derive(Serialize, Deserialize)]
 12struct ServerRequestParams {
 13    command: String,
 14    arguments: ServerRequestArguments,
 15}
 16
 17#[derive(Serialize, Deserialize)]
 18struct ServerRequestArguments {}
 19
 20impl lsp::request::Request for TypescriptServerRequest {
 21    type Params = ServerRequestParams;
 22
 23    type Result = ();
 24
 25    const METHOD: &'static str = "tsserver/request";
 26}
 27
 28impl lsp::request::Request for TypescriptServerResponse {
 29    type Params = serde_json::Value;
 30
 31    type Result = ();
 32
 33    const METHOD: &'static str = "tsserver/response";
 34}
 35
 36const VUE_SERVER_NAME: LanguageServerName = LanguageServerName::new_static("vue-language-server");
 37const VTSLS: LanguageServerName = LanguageServerName::new_static("vtsls");
 38const TS_LS: LanguageServerName = LanguageServerName::new_static("typescript-language-server");
 39
 40pub fn register_requests(lsp_store: WeakEntity<LspStore>, language_server: &LanguageServer) {
 41    let language_server_name = language_server.name();
 42    if language_server_name == VUE_SERVER_NAME {
 43        language_server
 44            .on_request::<TypescriptServerRequest, _, _>({
 45                let this = lsp_store.clone();
 46                move |params, cx| {
 47                    let this = this.clone();
 48                    let cx = cx.clone();
 49                    async move {
 50                        let language_server = this
 51                            .read_with(&cx, |this, _| {
 52                                let language_server_id = this
 53                                    .as_local()
 54                                    .and_then(|local| {
 55                                        local.language_server_ids.iter().find_map(|(seed, v)| {
 56                                            [VTSLS, TS_LS].contains(&seed.name).then_some(v.id)
 57                                        })
 58                                    })
 59                                    .context("Could not find language server")?;
 60
 61                                this.language_server_for_id(language_server_id)
 62                                    .context("language server not found")
 63                            })
 64                            .flatten()?;
 65                        language_server
 66                            .request::<TypescriptServerRequest>(params)
 67                            .await
 68                            .into_response()
 69                    }
 70                }
 71            })
 72            .detach();
 73    } else if language_server_name == VTSLS || language_server_name == TS_LS {
 74        language_server
 75            .on_request::<TypescriptServerResponse, _, _>({
 76                let this = lsp_store.clone();
 77                move |params, cx| {
 78                    let this = this.clone();
 79                    let cx = cx.clone();
 80                    async move {
 81                        let language_server = this
 82                            .read_with(&cx, |this, _| {
 83                                let language_server_id = this
 84                                    .as_local()
 85                                    .and_then(|local| {
 86                                        local.language_server_ids.iter().find_map(|(seed, v)| {
 87                                            // todo: improve this
 88                                            [VUE_SERVER_NAME].contains(&seed.name).then_some(v.id)
 89                                        })
 90                                    })
 91                                    .context("Could not find language server")?;
 92
 93                                this.language_server_for_id(language_server_id)
 94                                    .context("language server not found")
 95                            })
 96                            .flatten()?;
 97                        language_server
 98                            .request::<TypescriptServerResponse>(params)
 99                            .await
100                            .into_response()
101                    }
102                }
103            })
104            .detach();
105    }
106}