This PR needs suggestions, especially from the Zed team. As I mentioned in a previous issue #13394 , the `vscode-json-languageserver` that Zed originally relied on has some issues with JSON schema validation on Windows, and it hasn't been updated for a long time. This PR uses the more frequently updated `vscode-langservers-extracted`, which resolves this issue. Currently, `vscode-langservers-extracted` includes not only the JSON LSP server but also LSP servers for other languages. I think we might need a package specifically for the JSON LSP server, such as something like `vscode-json-langserver-extracted`, or we could consider using the LSP servers for other languages from this package as well. And, there are some issues with installing `vscode-langservers-extracted` on Windows, causing the `postinstall` script to fail. However, this does not seem to affect any functionality. Therefore, I think the best solution is for the Zed team to maintain a package like `vscode-json-langserver-extracted` or something else. This way, we can update it promptly and address the installation issues on Windows. Any suggestions or advices are welcome. #### JSON vaildation on Winodws https://github.com/zed-industries/zed/assets/14981363/8cd7ff54-28ec-4601-b2e5-183e2fae2051 Closes #13394 Release Notes: - Fixed JSON schema validation issue on Windows.(#13394 )
254 lines
8.1 KiB
Rust
254 lines
8.1 KiB
Rust
use anyhow::{anyhow, Result};
|
|
use async_trait::async_trait;
|
|
use collections::HashMap;
|
|
use feature_flags::FeatureFlagAppExt;
|
|
use futures::StreamExt;
|
|
use gpui::{AppContext, AsyncAppContext};
|
|
use language::{LanguageRegistry, LanguageServerName, LspAdapter, LspAdapterDelegate};
|
|
use lsp::LanguageServerBinary;
|
|
use node_runtime::NodeRuntime;
|
|
use project::ContextProviderWithTasks;
|
|
use serde_json::{json, Value};
|
|
use settings::{KeymapFile, SettingsJsonSchemaParams, SettingsStore};
|
|
use smol::fs;
|
|
use std::{
|
|
any::Any,
|
|
ffi::OsString,
|
|
path::{Path, PathBuf},
|
|
str::FromStr,
|
|
sync::{Arc, OnceLock},
|
|
};
|
|
use task::{TaskTemplate, TaskTemplates, VariableName};
|
|
use util::{maybe, ResultExt};
|
|
|
|
const SERVER_PATH: &str =
|
|
"node_modules/vscode-langservers-extracted/bin/vscode-json-language-server";
|
|
|
|
// Origin: https://github.com/SchemaStore/schemastore
|
|
const TSCONFIG_SCHEMA: &str = include_str!("json/schemas/tsconfig.json");
|
|
const PACKAGE_JSON_SCHEMA: &str = include_str!("json/schemas/package.json");
|
|
|
|
pub(super) fn json_task_context() -> ContextProviderWithTasks {
|
|
ContextProviderWithTasks::new(TaskTemplates(vec![
|
|
TaskTemplate {
|
|
label: "package script $ZED_CUSTOM_script".to_owned(),
|
|
command: "npm --prefix $ZED_DIRNAME run".to_owned(),
|
|
args: vec![VariableName::Custom("script".into()).template_value()],
|
|
tags: vec!["package-script".into()],
|
|
..TaskTemplate::default()
|
|
},
|
|
TaskTemplate {
|
|
label: "composer script $ZED_CUSTOM_script".to_owned(),
|
|
command: "composer -d $ZED_DIRNAME".to_owned(),
|
|
args: vec![VariableName::Custom("script".into()).template_value()],
|
|
tags: vec!["composer-script".into()],
|
|
..TaskTemplate::default()
|
|
},
|
|
]))
|
|
}
|
|
|
|
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
|
|
vec![server_path.into(), "--stdio".into()]
|
|
}
|
|
|
|
pub struct JsonLspAdapter {
|
|
node: Arc<dyn NodeRuntime>,
|
|
languages: Arc<LanguageRegistry>,
|
|
workspace_config: OnceLock<Value>,
|
|
}
|
|
|
|
impl JsonLspAdapter {
|
|
pub fn new(node: Arc<dyn NodeRuntime>, languages: Arc<LanguageRegistry>) -> Self {
|
|
Self {
|
|
node,
|
|
languages,
|
|
workspace_config: Default::default(),
|
|
}
|
|
}
|
|
|
|
fn get_workspace_config(language_names: Vec<String>, cx: &mut AppContext) -> Value {
|
|
let action_names = cx.all_action_names();
|
|
let staff_mode = cx.is_staff();
|
|
|
|
let font_names = &cx.text_system().all_font_names();
|
|
let settings_schema = cx.global::<SettingsStore>().json_schema(
|
|
&SettingsJsonSchemaParams {
|
|
language_names: &language_names,
|
|
staff_mode,
|
|
font_names,
|
|
},
|
|
cx,
|
|
);
|
|
let tasks_schema = task::TaskTemplates::generate_json_schema();
|
|
let tsconfig_schema = serde_json::Value::from_str(TSCONFIG_SCHEMA).unwrap();
|
|
let package_json_schema = serde_json::Value::from_str(PACKAGE_JSON_SCHEMA).unwrap();
|
|
|
|
serde_json::json!({
|
|
"json": {
|
|
"format": {
|
|
"enable": true,
|
|
},
|
|
"schemas": [
|
|
{
|
|
"fileMatch": ["tsconfig.json"],
|
|
"schema":tsconfig_schema
|
|
},
|
|
{
|
|
"fileMatch": ["package.json"],
|
|
"schema":package_json_schema
|
|
},
|
|
{
|
|
"fileMatch": [
|
|
schema_file_match(paths::settings_file()),
|
|
paths::local_settings_file_relative_path()
|
|
],
|
|
"schema": settings_schema,
|
|
},
|
|
{
|
|
"fileMatch": [schema_file_match(paths::keymap_file())],
|
|
"schema": KeymapFile::generate_json_schema(&action_names),
|
|
},
|
|
{
|
|
"fileMatch": [
|
|
schema_file_match(paths::tasks_file()),
|
|
paths::local_tasks_file_relative_path()
|
|
],
|
|
"schema": tasks_schema,
|
|
}
|
|
|
|
]
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[async_trait(?Send)]
|
|
impl LspAdapter for JsonLspAdapter {
|
|
fn name(&self) -> LanguageServerName {
|
|
LanguageServerName("json-language-server".into())
|
|
}
|
|
|
|
async fn fetch_latest_server_version(
|
|
&self,
|
|
_: &dyn LspAdapterDelegate,
|
|
) -> Result<Box<dyn 'static + Send + Any>> {
|
|
Ok(Box::new(
|
|
self.node
|
|
.npm_package_latest_version("vscode-langservers-extracted")
|
|
.await?,
|
|
) as Box<_>)
|
|
}
|
|
|
|
async fn fetch_server_binary(
|
|
&self,
|
|
latest_version: Box<dyn 'static + Send + Any>,
|
|
container_dir: PathBuf,
|
|
_: &dyn LspAdapterDelegate,
|
|
) -> Result<LanguageServerBinary> {
|
|
let latest_version = latest_version.downcast::<String>().unwrap();
|
|
let server_path = container_dir.join(SERVER_PATH);
|
|
let package_name = "vscode-langservers-extracted";
|
|
|
|
let should_install_language_server = self
|
|
.node
|
|
.should_install_npm_package(package_name, &server_path, &container_dir, &latest_version)
|
|
.await;
|
|
|
|
if should_install_language_server {
|
|
// TODO: the postinstall fails on Windows
|
|
self.node
|
|
.npm_install_packages(&container_dir, &[(package_name, latest_version.as_str())])
|
|
.await
|
|
.log_err();
|
|
}
|
|
|
|
Ok(LanguageServerBinary {
|
|
path: self.node.binary_path().await?,
|
|
env: None,
|
|
arguments: server_binary_arguments(&server_path),
|
|
})
|
|
}
|
|
|
|
async fn cached_server_binary(
|
|
&self,
|
|
container_dir: PathBuf,
|
|
_: &dyn LspAdapterDelegate,
|
|
) -> Option<LanguageServerBinary> {
|
|
get_cached_server_binary(container_dir, &*self.node).await
|
|
}
|
|
|
|
async fn installation_test_binary(
|
|
&self,
|
|
container_dir: PathBuf,
|
|
) -> Option<LanguageServerBinary> {
|
|
get_cached_server_binary(container_dir, &*self.node).await
|
|
}
|
|
|
|
async fn initialization_options(
|
|
self: Arc<Self>,
|
|
_: &Arc<dyn LspAdapterDelegate>,
|
|
) -> Result<Option<serde_json::Value>> {
|
|
Ok(Some(json!({
|
|
"provideFormatter": true
|
|
})))
|
|
}
|
|
|
|
async fn workspace_configuration(
|
|
self: Arc<Self>,
|
|
_: &Arc<dyn LspAdapterDelegate>,
|
|
cx: &mut AsyncAppContext,
|
|
) -> Result<Value> {
|
|
cx.update(|cx| {
|
|
self.workspace_config
|
|
.get_or_init(|| Self::get_workspace_config(self.languages.language_names(), cx))
|
|
.clone()
|
|
})
|
|
}
|
|
|
|
fn language_ids(&self) -> HashMap<String, String> {
|
|
[("JSON".into(), "jsonc".into())].into_iter().collect()
|
|
}
|
|
}
|
|
|
|
async fn get_cached_server_binary(
|
|
container_dir: PathBuf,
|
|
node: &dyn NodeRuntime,
|
|
) -> Option<LanguageServerBinary> {
|
|
maybe!(async {
|
|
let mut last_version_dir = None;
|
|
let mut entries = fs::read_dir(&container_dir).await?;
|
|
while let Some(entry) = entries.next().await {
|
|
let entry = entry?;
|
|
if entry.file_type().await?.is_dir() {
|
|
last_version_dir = Some(entry.path());
|
|
}
|
|
}
|
|
|
|
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
|
|
let server_path = last_version_dir.join(SERVER_PATH);
|
|
if server_path.exists() {
|
|
Ok(LanguageServerBinary {
|
|
path: node.binary_path().await?,
|
|
env: None,
|
|
arguments: server_binary_arguments(&server_path),
|
|
})
|
|
} else {
|
|
Err(anyhow!(
|
|
"missing executable in directory {:?}",
|
|
last_version_dir
|
|
))
|
|
}
|
|
})
|
|
.await
|
|
.log_err()
|
|
}
|
|
|
|
#[inline]
|
|
fn schema_file_match(path: &Path) -> String {
|
|
path.strip_prefix(path.parent().unwrap().parent().unwrap())
|
|
.unwrap()
|
|
.display()
|
|
.to_string()
|
|
.replace('\\', "/")
|
|
}
|