1use plugin::prelude::*;
2use serde::Deserialize;
3use serde_json::json;
4use std::fs;
5use std::path::PathBuf;
6
7#[import]
8fn command(string: &str) -> Option<Vec<u8>>;
9
10const BIN_PATH: &'static str =
11 "node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
12
13#[export]
14pub fn name() -> &'static str {
15 "vscode-json-languageserver"
16}
17
18#[export]
19pub fn server_args() -> Vec<String> {
20 vec!["--stdio".into()]
21}
22
23#[export]
24pub fn fetch_latest_server_version() -> Option<String> {
25 #[derive(Deserialize)]
26 struct NpmInfo {
27 versions: Vec<String>,
28 }
29
30 // TODO: command returns error code
31 let output =
32 command("npm info vscode-json-languageserver --json").expect("could not run command");
33 // if !output.is_ok() {
34 // return None;
35 // }
36
37 let output = String::from_utf8(output).unwrap();
38
39 let mut info: NpmInfo = serde_json::from_str(&output).ok()?;
40 info.versions.pop()
41}
42
43#[export]
44pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<PathBuf, String> {
45 let version_dir = container_dir.join(version.as_str());
46 fs::create_dir_all(&version_dir)
47 .map_err(|_| "failed to create version directory".to_string())?;
48 let binary_path = version_dir.join(BIN_PATH);
49
50 if fs::metadata(&binary_path).is_err() {
51 let output = command(&format!(
52 "npm install vscode-json-languageserver@{}",
53 version
54 ));
55 let output = output.map(String::from_utf8);
56 if output.is_none() {
57 return Err("failed to install vscode-json-languageserver".to_string());
58 }
59
60 if let Some(mut entries) = fs::read_dir(&container_dir).ok() {
61 while let Some(entry) = entries.next() {
62 if let Some(entry) = entry.ok() {
63 let entry_path = entry.path();
64 if entry_path.as_path() != version_dir {
65 fs::remove_dir_all(&entry_path).ok();
66 }
67 }
68 }
69 }
70 }
71
72 Ok(binary_path)
73}
74
75#[export]
76pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
77 let mut last_version_dir = None;
78 let mut entries = fs::read_dir(&container_dir).ok()?;
79
80 while let Some(entry) = entries.next() {
81 let entry = entry.ok()?;
82 if entry.file_type().ok()?.is_dir() {
83 last_version_dir = Some(entry.path());
84 }
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 dbg!(&bin_path);
91 Some(bin_path)
92 } else {
93 println!("no binary found");
94 None
95 }
96}
97
98// #[export]
99// pub fn label_for_completion(
100// item: &lsp::CompletionItem,
101// // language: &language::Language,
102// ) -> Option<language::CodeLabel> {
103// // TODO: Push more of this method down into the plugin.
104// use lsp::CompletionItemKind as Kind;
105// let len = item.label.len();
106// let grammar = language.grammar()?;
107// let kind = format!("{:?}", item.kind?);
108
109// // TODO: implementation
110
111// let highlight_id = grammar.highlight_id_for_name(&name)?;
112// Some(language::CodeLabel {
113// text: item.label.clone(),
114// runs: vec![(0..len, highlight_id)],
115// filter_range: 0..len,
116// })
117// }
118
119#[export]
120pub fn initialization_options() -> Option<String> {
121 Some("{ \"provideFormatter\": true }".to_string())
122}
123
124#[export]
125pub fn id_for_language(name: String) -> Option<String> {
126 if name == "JSON" {
127 Some("jsonc".into())
128 } else {
129 None
130 }
131}