lib.rs

  1use plugin::prelude::*;
  2use serde_json::json;
  3use std::fs;
  4use std::path::PathBuf;
  5
  6// import
  7// fn command(string: String) -> Option<String>;
  8
  9#[bind]
 10pub fn name() -> &'static str {
 11    "vscode-json-languageserver"
 12}
 13
 14#[bind]
 15pub fn server_args() -> Vec<String> {
 16    vec!["--stdio".into()]
 17}
 18
 19#[bind]
 20pub fn fetch_latest_server_version() -> Option<String> {
 21    // #[derive(Deserialize)]
 22    // struct NpmInfo {
 23    //     versions: Vec<String>,
 24    // }
 25
 26    // let output = command("npm info vscode-json-languageserver --json")?;
 27    // if !output.status.success() {
 28    //     return None;
 29    // }
 30
 31    // let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
 32    // info.versions.pop()
 33    println!("fetching server version");
 34    Some("1.3.4".into())
 35}
 36
 37// #[bind]
 38// pub fn fetch_server_binary(version: String) -> Option<PathBuf> {
 39//     let version_dir = container_dir.join(version.as_str());
 40//     fs::create_dir_all(&version_dir)
 41//         .await
 42//         .context("failed to create version directory")?;
 43//     let binary_path = version_dir.join(Self::BIN_PATH);
 44
 45//     if fs::metadata(&binary_path).await.is_err() {
 46//         let output = smol::process::Command::new("npm")
 47//             .current_dir(&version_dir)
 48//             .arg("install")
 49//             .arg(format!("vscode-json-languageserver@{}", version))
 50//             .output()
 51//             .await
 52//             .context("failed to run npm install")?;
 53//         if !output.status.success() {
 54//             Err(anyhow!("failed to install vscode-json-languageserver"))?;
 55//         }
 56
 57//         if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
 58//             while let Some(entry) = entries.next().await {
 59//                 if let Some(entry) = entry.log_err() {
 60//                     let entry_path = entry.path();
 61//                     if entry_path.as_path() != version_dir {
 62//                         fs::remove_dir_all(&entry_path).await.log_err();
 63//                     }
 64//                 }
 65//             }
 66//         }
 67//     }
 68
 69//     Ok(binary_path)
 70// }
 71
 72const BIN_PATH: &'static str =
 73    "node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
 74
 75// #[bind]
 76// pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
 77//     println!("Finding cached server binary...");
 78//     let mut last_version_dir = None;
 79//     let mut entries = fs::read_dir(&container_dir).ok()?;
 80//     println!("Read Entries...");
 81//     while let Some(entry) = entries.next() {
 82//         let entry = entry.ok()?;
 83//         if entry.file_type().ok()?.is_dir() {
 84//             last_version_dir = Some(entry.path());
 85//         }
 86//     }
 87//     let last_version_dir = last_version_dir?;
 88//     let bin_path = last_version_dir.join(BIN_PATH);
 89//     if bin_path.exists() {
 90//         println!("{}", bin_path.display());
 91//         Some(bin_path)
 92//     } else {
 93//         None
 94//     }
 95// }
 96
 97#[bind]
 98pub fn initialization_options() -> Option<serde_json::Value> {
 99    Some(json!({
100        "provideFormatter": true
101    }))
102}
103
104#[bind]
105pub fn id_for_language(name: String) -> Option<String> {
106    if name == "JSON" {
107        Some("jsonc".into())
108    } else {
109        None
110    }
111}