2024-02-12 18:10:40 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use futures::StreamExt;
|
|
|
|
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
|
|
|
|
use lsp::LanguageServerBinary;
|
|
|
|
use node_runtime::NodeRuntime;
|
|
|
|
use serde_json::json;
|
|
|
|
use smol::fs;
|
|
|
|
use std::{
|
|
|
|
any::Any,
|
|
|
|
ffi::OsString,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2024-03-04 17:38:18 +00:00
|
|
|
use util::{async_maybe, ResultExt};
|
2024-02-12 18:10:40 +00:00
|
|
|
|
2024-03-02 05:40:49 +00:00
|
|
|
const SERVER_PATH: &str = "node_modules/@astrojs/language-server/bin/nodeServer.js";
|
2024-02-12 18:10:40 +00:00
|
|
|
|
|
|
|
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
|
|
|
|
vec![server_path.into(), "--stdio".into()]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AstroLspAdapter {
|
|
|
|
node: Arc<dyn NodeRuntime>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AstroLspAdapter {
|
|
|
|
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
|
|
|
|
AstroLspAdapter { node }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:42:03 +00:00
|
|
|
#[async_trait(?Send)]
|
2024-02-12 18:10:40 +00:00
|
|
|
impl LspAdapter for AstroLspAdapter {
|
|
|
|
fn name(&self) -> LanguageServerName {
|
|
|
|
LanguageServerName("astro-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("@astrojs/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,
|
|
|
|
&[("@astrojs/language-server", version.as_str())],
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(LanguageServerBinary {
|
|
|
|
path: self.node.binary_path().await?,
|
Detect and possibly use user-installed `gopls` / `zls` language servers (#8188)
After a lot of back-and-forth, this is a small attempt to implement
solutions (1) and (3) in
https://github.com/zed-industries/zed/issues/7902. The goal is to have a
minimal change that helps users get started with Zed, until we have
extensions ready.
Release Notes:
- Added detection of user-installed `gopls` to Go language server
adapter. If a user has `gopls` in `$PATH` when opening a worktree, it
will be used.
- Added detection of user-installed `zls` to Zig language server
adapter. If a user has `zls` in `$PATH` when opening a worktree, it will
be used.
Example:
I don't have `go` installed globally, but I do have `gopls`:
```
~ $ which go
go not found
~ $ which gopls
/Users/thorstenball/code/go/bin/gopls
```
But I do have `go` in a project's directory:
```
~/tmp/go-testing φ which go
/Users/thorstenball/.local/share/mise/installs/go/1.21.5/go/bin/go
~/tmp/go-testing φ which gopls
/Users/thorstenball/code/go/bin/gopls
```
With current Zed when I run `zed ~/tmp/go-testing`, I'd get the dreaded
error:
![screenshot-2024-02-23-11 14
08@2x](https://github.com/zed-industries/zed/assets/1185253/822ea59b-c63e-4102-a50e-75501cc4e0e3)
But with the changes in this PR, it works:
```
[2024-02-23T11:14:42+01:00 INFO language::language_registry] starting language server "gopls", path: "/Users/thorstenball/tmp/go-testing", id: 1
[2024-02-23T11:14:42+01:00 INFO language::language_registry] found user-installed language server for Go. path: "/Users/thorstenball/code/go/bin/gopls", arguments: ["-mode=stdio"]
[2024-02-23T11:14:42+01:00 INFO lsp] starting language server. binary path: "/Users/thorstenball/code/go/bin/gopls", working directory: "/Users/thorstenball/tmp/go-testing", args: ["-mode=stdio"]
```
---------
Co-authored-by: Antonio <antonio@zed.dev>
2024-02-23 12:39:14 +00:00
|
|
|
env: None,
|
2024-02-12 18:10:40 +00:00
|
|
|
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 initialization_options(&self) -> Option<serde_json::Value> {
|
|
|
|
Some(json!({
|
|
|
|
"provideFormatter": true,
|
|
|
|
"typescript": {
|
|
|
|
"tsdk": "node_modules/typescript/lib",
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prettier_plugins(&self) -> &[&'static str] {
|
|
|
|
&["prettier-plugin-astro"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_cached_server_binary(
|
|
|
|
container_dir: PathBuf,
|
|
|
|
node: &dyn NodeRuntime,
|
|
|
|
) -> Option<LanguageServerBinary> {
|
2024-03-04 17:38:18 +00:00
|
|
|
async_maybe!({
|
2024-02-12 18:10:40 +00:00
|
|
|
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?,
|
Detect and possibly use user-installed `gopls` / `zls` language servers (#8188)
After a lot of back-and-forth, this is a small attempt to implement
solutions (1) and (3) in
https://github.com/zed-industries/zed/issues/7902. The goal is to have a
minimal change that helps users get started with Zed, until we have
extensions ready.
Release Notes:
- Added detection of user-installed `gopls` to Go language server
adapter. If a user has `gopls` in `$PATH` when opening a worktree, it
will be used.
- Added detection of user-installed `zls` to Zig language server
adapter. If a user has `zls` in `$PATH` when opening a worktree, it will
be used.
Example:
I don't have `go` installed globally, but I do have `gopls`:
```
~ $ which go
go not found
~ $ which gopls
/Users/thorstenball/code/go/bin/gopls
```
But I do have `go` in a project's directory:
```
~/tmp/go-testing φ which go
/Users/thorstenball/.local/share/mise/installs/go/1.21.5/go/bin/go
~/tmp/go-testing φ which gopls
/Users/thorstenball/code/go/bin/gopls
```
With current Zed when I run `zed ~/tmp/go-testing`, I'd get the dreaded
error:
![screenshot-2024-02-23-11 14
08@2x](https://github.com/zed-industries/zed/assets/1185253/822ea59b-c63e-4102-a50e-75501cc4e0e3)
But with the changes in this PR, it works:
```
[2024-02-23T11:14:42+01:00 INFO language::language_registry] starting language server "gopls", path: "/Users/thorstenball/tmp/go-testing", id: 1
[2024-02-23T11:14:42+01:00 INFO language::language_registry] found user-installed language server for Go. path: "/Users/thorstenball/code/go/bin/gopls", arguments: ["-mode=stdio"]
[2024-02-23T11:14:42+01:00 INFO lsp] starting language server. binary path: "/Users/thorstenball/code/go/bin/gopls", working directory: "/Users/thorstenball/tmp/go-testing", args: ["-mode=stdio"]
```
---------
Co-authored-by: Antonio <antonio@zed.dev>
2024-02-23 12:39:14 +00:00
|
|
|
env: None,
|
2024-02-12 18:10:40 +00:00
|
|
|
arguments: server_binary_arguments(&server_path),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(anyhow!(
|
|
|
|
"missing executable in directory {:?}",
|
|
|
|
last_version_dir
|
|
|
|
))
|
|
|
|
}
|
2024-03-04 17:38:18 +00:00
|
|
|
})
|
2024-02-12 18:10:40 +00:00
|
|
|
.await
|
|
|
|
.log_err()
|
|
|
|
}
|