Add ability to specify binary path/args for rust-analyzer (#9293)

This fixes #9292 by adding a section to the language server settings
that allows users to specify the binary path and arguments with which to
start up a language server.

Example user settings for `rust-analyzer`:

```json
{
  "lsp": {
    "rust-analyzer": {
      "binary": {
        "path": "/Users/thorstenball/tmp/rust-analyzer-aarch64-apple-darwin",
        "arguments": ["--no-log-buffering"]
      }
    }
  }
}
```

Constraints:

* Right now this only allows ABSOLUTE paths.
* This is only used by `rust-analyzer` integration right now, but the
setting can be used for other language servers. We just need to update
the adapters to also respect that setting.



Release Notes:

- Added ability to specify `rust-analyzer` binary `path` (must be
absolute) and `arguments` in user settings. Example: `{"lsp":
{"rust-analyzer": {"binary": {"path": "/my/abs/path/rust-analyzer",
"arguments": ["--no-log-buffering"] }}}}`
([#9292](https://github.com/zed-industries/zed/issues/9292)).

Co-authored-by: Ricard Mallafre <rikitzzz@gmail.com>
This commit is contained in:
Thorsten Ball 2024-03-13 18:42:03 +01:00 committed by GitHub
parent 6655b964ab
commit a56a260778
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 92 additions and 43 deletions

View file

@ -2,12 +2,15 @@ use anyhow::{anyhow, bail, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_trait::async_trait;
use futures::{io::BufReader, StreamExt};
use gpui::AsyncAppContext;
pub use language::*;
use lazy_static::lazy_static;
use lsp::LanguageServerBinary;
use project::project_settings::ProjectSettings;
use regex::Regex;
use settings::Settings;
use smol::fs::{self, File};
use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, str, sync::Arc};
use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, sync::Arc};
use util::{
async_maybe,
fs::remove_matching,
@ -17,10 +20,41 @@ use util::{
pub struct RustLspAdapter;
#[async_trait]
impl RustLspAdapter {
const SERVER_NAME: &'static str = "rust-analyzer";
}
#[async_trait(?Send)]
impl LspAdapter for RustLspAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("rust-analyzer".into())
LanguageServerName(Self::SERVER_NAME.into())
}
async fn check_if_user_installed(
&self,
_delegate: &dyn LspAdapterDelegate,
cx: &AsyncAppContext,
) -> Option<LanguageServerBinary> {
let binary = cx
.update(|cx| {
ProjectSettings::get_global(cx)
.lsp
.get(Self::SERVER_NAME)
.and_then(|s| s.binary.clone())
})
.ok()??;
let path = binary.path?;
Some(LanguageServerBinary {
path: path.into(),
arguments: binary
.arguments
.unwrap_or_default()
.iter()
.map(|arg| arg.into())
.collect(),
env: None,
})
}
async fn fetch_latest_server_version(