Add YAML LSP initialization_options (#17479)

Makes YAML language server configurable under `lsp.yaml-language-server`:
- Add support for `initialization_options` 
- Add support for custom `bin` specification
This commit is contained in:
Peter Tripp 2024-09-06 11:42:36 -04:00 committed by GitHub
parent 903f92045a
commit 2d06d5c906
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 103 additions and 10 deletions

View file

@ -7,6 +7,7 @@ use language::{
};
use lsp::LanguageServerBinary;
use node_runtime::NodeRuntime;
use project::project_settings::ProjectSettings;
use serde_json::Value;
use settings::{Settings, SettingsLocation};
use smol::fs;
@ -16,7 +17,7 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use util::{maybe, ResultExt};
use util::{maybe, merge_json_value_into, ResultExt};
const SERVER_PATH: &str = "node_modules/yaml-language-server/bin/yaml-language-server";
@ -29,6 +30,7 @@ pub struct YamlLspAdapter {
}
impl YamlLspAdapter {
const SERVER_NAME: &'static str = "yaml-language-server";
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
YamlLspAdapter { node }
}
@ -37,7 +39,40 @@ impl YamlLspAdapter {
#[async_trait(?Send)]
impl LspAdapter for YamlLspAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("yaml-language-server".into())
LanguageServerName(Self::SERVER_NAME.into())
}
async fn check_if_user_installed(
&self,
_delegate: &dyn LspAdapterDelegate,
cx: &AsyncAppContext,
) -> Option<LanguageServerBinary> {
let configured_binary = cx
.update(|cx| {
ProjectSettings::get_global(cx)
.lsp
.get(Self::SERVER_NAME)
.and_then(|s| s.binary.clone())
})
.ok()??;
let path = if let Some(configured_path) = configured_binary.path.map(PathBuf::from) {
configured_path
} else {
self.node.binary_path().await.ok()?
};
let arguments = configured_binary
.arguments
.unwrap_or_default()
.iter()
.map(|arg| arg.into())
.collect();
Some(LanguageServerBinary {
path,
arguments,
env: None,
})
}
async fn fetch_latest_server_version(
@ -109,15 +144,18 @@ impl LspAdapter for YamlLspAdapter {
.language(Some("YAML"))
.tab_size
})?;
let mut options = serde_json::json!({"[yaml]": {"editor.tabSize": tab_size}});
Ok(serde_json::json!({
"yaml": {
"keyOrdering": false
},
"[yaml]": {
"editor.tabSize": tab_size
}
}))
let project_options = cx.update(|cx| {
ProjectSettings::get_global(cx)
.lsp
.get(Self::SERVER_NAME)
.and_then(|s| s.initialization_options.clone())
})?;
if let Some(override_options) = project_options {
merge_json_value_into(override_options, &mut options);
}
Ok(options)
}
}