mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-12 05:27:07 +00:00
This PR removes unneeded `'static` lifetimes on `&str`s stored in `const` declarations. This addresses some Clippy lints about [`redundant_static_lifetimes`](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_static_lifetimes). In item-level `const` declarations we can rely on lifetime elision and use the default `'static` lifetime. Note that associated constants still require an explicit `'static` lifetime, as explained in https://github.com/rust-lang/rust/issues/115010. Release Notes: - N/A
136 lines
3.9 KiB
Rust
136 lines
3.9 KiB
Rust
use anyhow::{anyhow, Result};
|
|
use async_trait::async_trait;
|
|
use futures::StreamExt;
|
|
use gpui::AppContext;
|
|
use language::{
|
|
language_settings::all_language_settings, LanguageServerName, LspAdapter, LspAdapterDelegate,
|
|
};
|
|
use lsp::LanguageServerBinary;
|
|
use node_runtime::NodeRuntime;
|
|
use serde_json::Value;
|
|
use smol::fs;
|
|
use std::{
|
|
any::Any,
|
|
ffi::OsString,
|
|
path::{Path, PathBuf},
|
|
sync::Arc,
|
|
};
|
|
use util::{async_maybe, ResultExt};
|
|
|
|
const SERVER_PATH: &str = "node_modules/yaml-language-server/bin/yaml-language-server";
|
|
|
|
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
|
|
vec![server_path.into(), "--stdio".into()]
|
|
}
|
|
|
|
pub struct YamlLspAdapter {
|
|
node: Arc<dyn NodeRuntime>,
|
|
}
|
|
|
|
impl YamlLspAdapter {
|
|
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
|
|
YamlLspAdapter { node }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl LspAdapter for YamlLspAdapter {
|
|
fn name(&self) -> LanguageServerName {
|
|
LanguageServerName("yaml-language-server".into())
|
|
}
|
|
|
|
async fn fetch_latest_server_version(
|
|
&self,
|
|
_: &dyn LspAdapterDelegate,
|
|
) -> Result<Box<dyn 'static + Any + Send>> {
|
|
Ok(Box::new(
|
|
self.node
|
|
.npm_package_latest_version("yaml-language-server")
|
|
.await?,
|
|
) as Box<_>)
|
|
}
|
|
|
|
async fn fetch_server_binary(
|
|
&self,
|
|
version: Box<dyn 'static + Send + Any>,
|
|
container_dir: PathBuf,
|
|
_: &dyn LspAdapterDelegate,
|
|
) -> Result<LanguageServerBinary> {
|
|
let version = version.downcast::<String>().unwrap();
|
|
let server_path = container_dir.join(SERVER_PATH);
|
|
|
|
if fs::metadata(&server_path).await.is_err() {
|
|
self.node
|
|
.npm_install_packages(
|
|
&container_dir,
|
|
&[("yaml-language-server", version.as_str())],
|
|
)
|
|
.await?;
|
|
}
|
|
|
|
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
|
|
}
|
|
fn workspace_configuration(&self, _workspace_root: &Path, cx: &mut AppContext) -> Value {
|
|
serde_json::json!({
|
|
"yaml": {
|
|
"keyOrdering": false
|
|
},
|
|
"[yaml]": {
|
|
"editor.tabSize": all_language_settings(None, cx)
|
|
.language(Some("YAML"))
|
|
.tab_size,
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
async fn get_cached_server_binary(
|
|
container_dir: PathBuf,
|
|
node: &dyn NodeRuntime,
|
|
) -> Option<LanguageServerBinary> {
|
|
async_maybe!({
|
|
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()
|
|
}
|