Merge pull request #1252 from zed-industries/plugin
Language Server WebAssembly Plugin Integration (Part 2)
This commit is contained in:
commit
afc8e9050c
41 changed files with 3683 additions and 1196 deletions
|
@ -1,12 +1,13 @@
|
|||
use gpui::Task;
|
||||
use gpui::executor::Background;
|
||||
pub use language::*;
|
||||
use rust_embed::RustEmbed;
|
||||
use std::{borrow::Cow, str, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
mod c;
|
||||
mod go;
|
||||
mod installation;
|
||||
mod json;
|
||||
mod language_plugin;
|
||||
mod python;
|
||||
mod rust;
|
||||
mod typescript;
|
||||
|
@ -16,28 +17,30 @@ mod typescript;
|
|||
#[exclude = "*.rs"]
|
||||
struct LanguageDir;
|
||||
|
||||
pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegistry {
|
||||
let languages = LanguageRegistry::new(login_shell_env_loaded);
|
||||
pub async fn init(languages: Arc<LanguageRegistry>, executor: Arc<Background>) {
|
||||
for (name, grammar, lsp_adapter) in [
|
||||
(
|
||||
"c",
|
||||
tree_sitter_c::language(),
|
||||
Some(Arc::new(c::CLspAdapter) as Arc<dyn LspAdapter>),
|
||||
Some(CachedLspAdapter::new(c::CLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"cpp",
|
||||
tree_sitter_cpp::language(),
|
||||
Some(Arc::new(c::CLspAdapter) as Arc<dyn LspAdapter>),
|
||||
Some(CachedLspAdapter::new(c::CLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"go",
|
||||
tree_sitter_go::language(),
|
||||
Some(Arc::new(go::GoLspAdapter) as Arc<dyn LspAdapter>),
|
||||
Some(CachedLspAdapter::new(go::GoLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"json",
|
||||
tree_sitter_json::language(),
|
||||
Some(Arc::new(json::JsonLspAdapter)),
|
||||
match language_plugin::new_json(executor).await.log_err() {
|
||||
Some(lang) => Some(CachedLspAdapter::new(lang).await),
|
||||
None => None,
|
||||
},
|
||||
),
|
||||
(
|
||||
"markdown",
|
||||
|
@ -47,12 +50,12 @@ pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegi
|
|||
(
|
||||
"python",
|
||||
tree_sitter_python::language(),
|
||||
Some(Arc::new(python::PythonLspAdapter)),
|
||||
Some(CachedLspAdapter::new(python::PythonLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"rust",
|
||||
tree_sitter_rust::language(),
|
||||
Some(Arc::new(rust::RustLspAdapter)),
|
||||
Some(CachedLspAdapter::new(rust::RustLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"toml",
|
||||
|
@ -62,28 +65,27 @@ pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegi
|
|||
(
|
||||
"tsx",
|
||||
tree_sitter_typescript::language_tsx(),
|
||||
Some(Arc::new(typescript::TypeScriptLspAdapter)),
|
||||
Some(CachedLspAdapter::new(typescript::TypeScriptLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"typescript",
|
||||
tree_sitter_typescript::language_typescript(),
|
||||
Some(Arc::new(typescript::TypeScriptLspAdapter)),
|
||||
Some(CachedLspAdapter::new(typescript::TypeScriptLspAdapter).await),
|
||||
),
|
||||
(
|
||||
"javascript",
|
||||
tree_sitter_typescript::language_tsx(),
|
||||
Some(Arc::new(typescript::TypeScriptLspAdapter)),
|
||||
Some(CachedLspAdapter::new(typescript::TypeScriptLspAdapter).await),
|
||||
),
|
||||
] {
|
||||
languages.add(Arc::new(language(name, grammar, lsp_adapter)));
|
||||
}
|
||||
languages
|
||||
}
|
||||
|
||||
pub(crate) fn language(
|
||||
name: &str,
|
||||
grammar: tree_sitter::Language,
|
||||
lsp_adapter: Option<Arc<dyn LspAdapter>>,
|
||||
lsp_adapter: Option<Arc<CachedLspAdapter>>,
|
||||
) -> Language {
|
||||
let config = toml::from_slice(
|
||||
&LanguageDir::get(&format!("{}/config.toml", name))
|
||||
|
|
|
@ -1,102 +1,91 @@
|
|||
use super::installation::{latest_github_release, GitHubLspBinaryVersion};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use futures::StreamExt;
|
||||
pub use language::*;
|
||||
use smol::fs::{self, File};
|
||||
use std::{
|
||||
any::Any,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
use std::{any::Any, path::PathBuf, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
pub struct CLspAdapter;
|
||||
|
||||
#[async_trait]
|
||||
impl super::LspAdapter for CLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
async fn name(&self) -> LanguageServerName {
|
||||
LanguageServerName("clangd".into())
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
async fn fetch_latest_server_version(
|
||||
&self,
|
||||
http: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||
async move {
|
||||
let release = latest_github_release("clangd/clangd", http).await?;
|
||||
let asset_name = format!("clangd-mac-{}.zip", release.name);
|
||||
let asset = release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
||||
let version = GitHubLspBinaryVersion {
|
||||
name: release.name,
|
||||
url: asset.browser_download_url.clone(),
|
||||
};
|
||||
Ok(Box::new(version) as Box<_>)
|
||||
}
|
||||
.boxed()
|
||||
) -> Result<Box<dyn 'static + Send + Any>> {
|
||||
let release = latest_github_release("clangd/clangd", http).await?;
|
||||
let asset_name = format!("clangd-mac-{}.zip", release.name);
|
||||
let asset = release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
||||
let version = GitHubLspBinaryVersion {
|
||||
name: release.name,
|
||||
url: asset.browser_download_url.clone(),
|
||||
};
|
||||
Ok(Box::new(version) as Box<_>)
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
async fn fetch_server_binary(
|
||||
&self,
|
||||
version: Box<dyn 'static + Send + Any>,
|
||||
http: Arc<dyn HttpClient>,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
container_dir: PathBuf,
|
||||
) -> Result<PathBuf> {
|
||||
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
|
||||
async move {
|
||||
let zip_path = container_dir.join(format!("clangd_{}.zip", version.name));
|
||||
let version_dir = container_dir.join(format!("clangd_{}", version.name));
|
||||
let binary_path = version_dir.join("bin/clangd");
|
||||
let zip_path = container_dir.join(format!("clangd_{}.zip", version.name));
|
||||
let version_dir = container_dir.join(format!("clangd_{}", version.name));
|
||||
let binary_path = version_dir.join("bin/clangd");
|
||||
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
let mut response = http
|
||||
.get(&version.url, Default::default(), true)
|
||||
.await
|
||||
.context("error downloading release")?;
|
||||
let mut file = File::create(&zip_path).await?;
|
||||
if !response.status().is_success() {
|
||||
Err(anyhow!(
|
||||
"download failed with status {}",
|
||||
response.status().to_string()
|
||||
))?;
|
||||
}
|
||||
futures::io::copy(response.body_mut(), &mut file).await?;
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
let mut response = http
|
||||
.get(&version.url, Default::default(), true)
|
||||
.await
|
||||
.context("error downloading release")?;
|
||||
let mut file = File::create(&zip_path).await?;
|
||||
if !response.status().is_success() {
|
||||
Err(anyhow!(
|
||||
"download failed with status {}",
|
||||
response.status().to_string()
|
||||
))?;
|
||||
}
|
||||
futures::io::copy(response.body_mut(), &mut file).await?;
|
||||
|
||||
let unzip_status = smol::process::Command::new("unzip")
|
||||
.current_dir(&container_dir)
|
||||
.arg(&zip_path)
|
||||
.output()
|
||||
.await?
|
||||
.status;
|
||||
if !unzip_status.success() {
|
||||
Err(anyhow!("failed to unzip clangd archive"))?;
|
||||
}
|
||||
let unzip_status = smol::process::Command::new("unzip")
|
||||
.current_dir(&container_dir)
|
||||
.arg(&zip_path)
|
||||
.output()
|
||||
.await?
|
||||
.status;
|
||||
if !unzip_status.success() {
|
||||
Err(anyhow!("failed to unzip clangd archive"))?;
|
||||
}
|
||||
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
.boxed()
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
|
||||
fn cached_server_binary(
|
||||
&self,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
async move {
|
||||
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
|
||||
(|| async move {
|
||||
let mut last_clangd_dir = None;
|
||||
let mut entries = fs::read_dir(&container_dir).await?;
|
||||
while let Some(entry) = entries.next().await {
|
||||
|
@ -115,12 +104,12 @@ impl super::LspAdapter for CLspAdapter {
|
|||
clangd_dir
|
||||
))
|
||||
}
|
||||
}
|
||||
})()
|
||||
.await
|
||||
.log_err()
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
async fn label_for_completion(
|
||||
&self,
|
||||
completion: &lsp::CompletionItem,
|
||||
language: &Language,
|
||||
|
@ -197,7 +186,7 @@ impl super::LspAdapter for CLspAdapter {
|
|||
Some(CodeLabel::plain(label.to_string(), None))
|
||||
}
|
||||
|
||||
fn label_for_symbol(
|
||||
async fn label_for_symbol(
|
||||
&self,
|
||||
name: &str,
|
||||
kind: lsp::SymbolKind,
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
use super::installation::latest_github_release;
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use futures::StreamExt;
|
||||
pub use language::*;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use smol::{fs, process};
|
||||
use std::{
|
||||
any::Any,
|
||||
ops::Range,
|
||||
path::{Path, PathBuf},
|
||||
str,
|
||||
sync::Arc,
|
||||
};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
use std::{any::Any, ops::Range, path::PathBuf, str, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct GoLspAdapter;
|
||||
|
@ -22,104 +17,96 @@ lazy_static! {
|
|||
static ref GOPLS_VERSION_REGEX: Regex = Regex::new(r"\d+\.\d+\.\d+").unwrap();
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl super::LspAdapter for GoLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
async fn name(&self) -> LanguageServerName {
|
||||
LanguageServerName("gopls".into())
|
||||
}
|
||||
|
||||
fn server_args(&self) -> &[&str] {
|
||||
&["-mode=stdio"]
|
||||
async fn server_args(&self) -> Vec<String> {
|
||||
vec!["-mode=stdio".into()]
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
async fn fetch_latest_server_version(
|
||||
&self,
|
||||
http: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||
async move {
|
||||
let release = latest_github_release("golang/tools", http).await?;
|
||||
let version: Option<String> = release.name.strip_prefix("gopls/v").map(str::to_string);
|
||||
if version.is_none() {
|
||||
log::warn!(
|
||||
"couldn't infer gopls version from github release name '{}'",
|
||||
release.name
|
||||
);
|
||||
}
|
||||
Ok(Box::new(version) as Box<_>)
|
||||
) -> Result<Box<dyn 'static + Send + Any>> {
|
||||
let release = latest_github_release("golang/tools", http).await?;
|
||||
let version: Option<String> = release.name.strip_prefix("gopls/v").map(str::to_string);
|
||||
if version.is_none() {
|
||||
log::warn!(
|
||||
"couldn't infer gopls version from github release name '{}'",
|
||||
release.name
|
||||
);
|
||||
}
|
||||
.boxed()
|
||||
Ok(Box::new(version) as Box<_>)
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
async fn fetch_server_binary(
|
||||
&self,
|
||||
version: Box<dyn 'static + Send + Any>,
|
||||
_: Arc<dyn HttpClient>,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
container_dir: PathBuf,
|
||||
) -> Result<PathBuf> {
|
||||
let version = version.downcast::<Option<String>>().unwrap();
|
||||
let this = *self;
|
||||
|
||||
async move {
|
||||
if let Some(version) = *version {
|
||||
let binary_path = container_dir.join(&format!("gopls_{version}"));
|
||||
if let Ok(metadata) = fs::metadata(&binary_path).await {
|
||||
if metadata.is_file() {
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != binary_path
|
||||
&& entry.file_name() != "gobin"
|
||||
{
|
||||
fs::remove_file(&entry_path).await.log_err();
|
||||
}
|
||||
if let Some(version) = *version {
|
||||
let binary_path = container_dir.join(&format!("gopls_{version}"));
|
||||
if let Ok(metadata) = fs::metadata(&binary_path).await {
|
||||
if metadata.is_file() {
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != binary_path
|
||||
&& entry.file_name() != "gobin"
|
||||
{
|
||||
fs::remove_file(&entry_path).await.log_err();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(binary_path.to_path_buf());
|
||||
}
|
||||
|
||||
return Ok(binary_path.to_path_buf());
|
||||
}
|
||||
} else if let Some(path) = this.cached_server_binary(container_dir.clone()).await {
|
||||
return Ok(path.to_path_buf());
|
||||
}
|
||||
|
||||
let gobin_dir = container_dir.join("gobin");
|
||||
fs::create_dir_all(&gobin_dir).await?;
|
||||
let install_output = process::Command::new("go")
|
||||
.env("GO111MODULE", "on")
|
||||
.env("GOBIN", &gobin_dir)
|
||||
.args(["install", "golang.org/x/tools/gopls@latest"])
|
||||
.output()
|
||||
.await?;
|
||||
if !install_output.status.success() {
|
||||
Err(anyhow!("failed to install gopls. Is go installed?"))?;
|
||||
}
|
||||
|
||||
let installed_binary_path = gobin_dir.join("gopls");
|
||||
let version_output = process::Command::new(&installed_binary_path)
|
||||
.arg("version")
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| anyhow!("failed to run installed gopls binary {:?}", e))?;
|
||||
let version_stdout = str::from_utf8(&version_output.stdout)
|
||||
.map_err(|_| anyhow!("gopls version produced invalid utf8"))?;
|
||||
let version = GOPLS_VERSION_REGEX
|
||||
.find(version_stdout)
|
||||
.ok_or_else(|| anyhow!("failed to parse gopls version output"))?
|
||||
.as_str();
|
||||
let binary_path = container_dir.join(&format!("gopls_{version}"));
|
||||
fs::rename(&installed_binary_path, &binary_path).await?;
|
||||
|
||||
Ok(binary_path.to_path_buf())
|
||||
} else if let Some(path) = this.cached_server_binary(container_dir.clone()).await {
|
||||
return Ok(path.to_path_buf());
|
||||
}
|
||||
.boxed()
|
||||
|
||||
let gobin_dir = container_dir.join("gobin");
|
||||
fs::create_dir_all(&gobin_dir).await?;
|
||||
let install_output = process::Command::new("go")
|
||||
.env("GO111MODULE", "on")
|
||||
.env("GOBIN", &gobin_dir)
|
||||
.args(["install", "golang.org/x/tools/gopls@latest"])
|
||||
.output()
|
||||
.await?;
|
||||
if !install_output.status.success() {
|
||||
Err(anyhow!("failed to install gopls. Is go installed?"))?;
|
||||
}
|
||||
|
||||
let installed_binary_path = gobin_dir.join("gopls");
|
||||
let version_output = process::Command::new(&installed_binary_path)
|
||||
.arg("version")
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| anyhow!("failed to run installed gopls binary {:?}", e))?;
|
||||
let version_stdout = str::from_utf8(&version_output.stdout)
|
||||
.map_err(|_| anyhow!("gopls version produced invalid utf8"))?;
|
||||
let version = GOPLS_VERSION_REGEX
|
||||
.find(version_stdout)
|
||||
.ok_or_else(|| anyhow!("failed to parse gopls version output"))?
|
||||
.as_str();
|
||||
let binary_path = container_dir.join(&format!("gopls_{version}"));
|
||||
fs::rename(&installed_binary_path, &binary_path).await?;
|
||||
|
||||
Ok(binary_path.to_path_buf())
|
||||
}
|
||||
|
||||
fn cached_server_binary(
|
||||
&self,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
async move {
|
||||
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
|
||||
(|| async move {
|
||||
let mut last_binary_path = None;
|
||||
let mut entries = fs::read_dir(&container_dir).await?;
|
||||
while let Some(entry) = entries.next().await {
|
||||
|
@ -139,12 +126,12 @@ impl super::LspAdapter for GoLspAdapter {
|
|||
} else {
|
||||
Err(anyhow!("no cached binary"))
|
||||
}
|
||||
}
|
||||
})()
|
||||
.await
|
||||
.log_err()
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
async fn label_for_completion(
|
||||
&self,
|
||||
completion: &lsp::CompletionItem,
|
||||
language: &Language,
|
||||
|
@ -244,7 +231,7 @@ impl super::LspAdapter for GoLspAdapter {
|
|||
None
|
||||
}
|
||||
|
||||
fn label_for_symbol(
|
||||
async fn label_for_symbol(
|
||||
&self,
|
||||
name: &str,
|
||||
kind: lsp::SymbolKind,
|
||||
|
@ -322,12 +309,12 @@ mod tests {
|
|||
use gpui::color::Color;
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
#[test]
|
||||
fn test_go_label_for_completion() {
|
||||
#[gpui::test]
|
||||
async fn test_go_label_for_completion() {
|
||||
let language = language(
|
||||
"go",
|
||||
tree_sitter_go::language(),
|
||||
Some(Arc::new(GoLspAdapter)),
|
||||
Some(CachedLspAdapter::new(GoLspAdapter).await),
|
||||
);
|
||||
|
||||
let theme = SyntaxTheme::new(vec![
|
||||
|
@ -347,12 +334,14 @@ mod tests {
|
|||
let highlight_field = grammar.highlight_id_for_name("property").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
language.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FUNCTION),
|
||||
label: "Hello".to_string(),
|
||||
detail: Some("func(a B) c.D".to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
language
|
||||
.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FUNCTION),
|
||||
label: "Hello".to_string(),
|
||||
detail: Some("func(a B) c.D".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "Hello(a B) c.D".to_string(),
|
||||
filter_range: 0..5,
|
||||
|
@ -366,12 +355,14 @@ mod tests {
|
|||
|
||||
// Nested methods
|
||||
assert_eq!(
|
||||
language.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::METHOD),
|
||||
label: "one.two.Three".to_string(),
|
||||
detail: Some("func() [3]interface{}".to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
language
|
||||
.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::METHOD),
|
||||
label: "one.two.Three".to_string(),
|
||||
detail: Some("func() [3]interface{}".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "one.two.Three() [3]interface{}".to_string(),
|
||||
filter_range: 0..13,
|
||||
|
@ -385,12 +376,14 @@ mod tests {
|
|||
|
||||
// Nested fields
|
||||
assert_eq!(
|
||||
language.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FIELD),
|
||||
label: "two.Three".to_string(),
|
||||
detail: Some("a.Bcd".to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
language
|
||||
.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FIELD),
|
||||
label: "two.Three".to_string(),
|
||||
detail: Some("a.Bcd".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "two.Three a.Bcd".to_string(),
|
||||
filter_range: 0..9,
|
||||
|
|
|
@ -1,120 +0,0 @@
|
|||
use super::installation::{npm_install_packages, npm_package_latest_version};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use language::{LanguageServerName, LspAdapter};
|
||||
use serde_json::json;
|
||||
use smol::fs;
|
||||
use std::{
|
||||
any::Any,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
|
||||
pub struct JsonLspAdapter;
|
||||
|
||||
impl JsonLspAdapter {
|
||||
const BIN_PATH: &'static str =
|
||||
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
|
||||
}
|
||||
|
||||
impl LspAdapter for JsonLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
LanguageServerName("vscode-json-languageserver".into())
|
||||
}
|
||||
|
||||
fn server_args(&self) -> &[&str] {
|
||||
&["--stdio"]
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
&self,
|
||||
_: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Any + Send>>> {
|
||||
async move {
|
||||
Ok(Box::new(npm_package_latest_version("vscode-json-languageserver").await?) as Box<_>)
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
&self,
|
||||
version: Box<dyn 'static + Send + Any>,
|
||||
_: Arc<dyn HttpClient>,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
let version = version.downcast::<String>().unwrap();
|
||||
async move {
|
||||
let version_dir = container_dir.join(version.as_str());
|
||||
fs::create_dir_all(&version_dir)
|
||||
.await
|
||||
.context("failed to create version directory")?;
|
||||
let binary_path = version_dir.join(Self::BIN_PATH);
|
||||
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
npm_install_packages(
|
||||
[("vscode-json-languageserver", version.as_str())],
|
||||
&version_dir,
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn cached_server_binary(
|
||||
&self,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
async move {
|
||||
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 bin_path = last_version_dir.join(Self::BIN_PATH);
|
||||
if bin_path.exists() {
|
||||
Ok(bin_path)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"missing executable in directory {:?}",
|
||||
last_version_dir
|
||||
))
|
||||
}
|
||||
}
|
||||
.log_err()
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn initialization_options(&self) -> Option<serde_json::Value> {
|
||||
Some(json!({
|
||||
"provideFormatter": true
|
||||
}))
|
||||
}
|
||||
|
||||
fn id_for_language(&self, name: &str) -> Option<String> {
|
||||
if name == "JSON" {
|
||||
Some("jsonc".into())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
145
crates/zed/src/languages/language_plugin.rs
Normal file
145
crates/zed/src/languages/language_plugin.rs
Normal file
|
@ -0,0 +1,145 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use client::http::HttpClient;
|
||||
use futures::lock::Mutex;
|
||||
use gpui::executor::Background;
|
||||
use language::{LanguageServerName, LspAdapter};
|
||||
use plugin_runtime::{Plugin, PluginBuilder, WasiFn};
|
||||
use std::{any::Any, path::PathBuf, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
|
||||
let plugin = PluginBuilder::new_with_default_ctx()?
|
||||
.host_function_async("command", |command: String| async move {
|
||||
let mut args = command.split(' ');
|
||||
let command = args.next().unwrap();
|
||||
smol::process::Command::new(command)
|
||||
.args(args)
|
||||
.output()
|
||||
.await
|
||||
.log_err()
|
||||
.map(|output| output.stdout)
|
||||
})?
|
||||
.init(
|
||||
true,
|
||||
include_bytes!("../../../../plugins/bin/json_language.wasm.pre"),
|
||||
)
|
||||
.await?;
|
||||
PluginLspAdapter::new(plugin, executor).await
|
||||
}
|
||||
|
||||
pub struct PluginLspAdapter {
|
||||
name: WasiFn<(), String>,
|
||||
server_args: WasiFn<(), Vec<String>>,
|
||||
fetch_latest_server_version: WasiFn<(), Option<String>>,
|
||||
fetch_server_binary: WasiFn<(PathBuf, String), Result<PathBuf, String>>,
|
||||
cached_server_binary: WasiFn<PathBuf, Option<PathBuf>>,
|
||||
initialization_options: WasiFn<(), String>,
|
||||
executor: Arc<Background>,
|
||||
runtime: Arc<Mutex<Plugin>>,
|
||||
}
|
||||
|
||||
impl PluginLspAdapter {
|
||||
pub async fn new(mut plugin: Plugin, executor: Arc<Background>) -> Result<Self> {
|
||||
Ok(Self {
|
||||
name: plugin.function("name")?,
|
||||
server_args: plugin.function("server_args")?,
|
||||
fetch_latest_server_version: plugin.function("fetch_latest_server_version")?,
|
||||
fetch_server_binary: plugin.function("fetch_server_binary")?,
|
||||
cached_server_binary: plugin.function("cached_server_binary")?,
|
||||
initialization_options: plugin.function("initialization_options")?,
|
||||
executor,
|
||||
runtime: Arc::new(Mutex::new(plugin)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LspAdapter for PluginLspAdapter {
|
||||
async fn name(&self) -> LanguageServerName {
|
||||
let name: String = self
|
||||
.runtime
|
||||
.lock()
|
||||
.await
|
||||
.call(&self.name, ())
|
||||
.await
|
||||
.unwrap();
|
||||
LanguageServerName(name.into())
|
||||
}
|
||||
|
||||
async fn server_args<'a>(&'a self) -> Vec<String> {
|
||||
self.runtime
|
||||
.lock()
|
||||
.await
|
||||
.call(&self.server_args, ())
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn fetch_latest_server_version(
|
||||
&self,
|
||||
_: Arc<dyn HttpClient>,
|
||||
) -> Result<Box<dyn 'static + Send + Any>> {
|
||||
let runtime = self.runtime.clone();
|
||||
let function = self.fetch_latest_server_version;
|
||||
self.executor
|
||||
.spawn(async move {
|
||||
let mut runtime = runtime.lock().await;
|
||||
let versions: Result<Option<String>> =
|
||||
runtime.call::<_, Option<String>>(&function, ()).await;
|
||||
versions
|
||||
.map_err(|e| anyhow!("{}", e))?
|
||||
.ok_or_else(|| anyhow!("Could not fetch latest server version"))
|
||||
.map(|v| Box::new(v) as Box<_>)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn fetch_server_binary(
|
||||
&self,
|
||||
version: Box<dyn 'static + Send + Any>,
|
||||
_: Arc<dyn HttpClient>,
|
||||
container_dir: PathBuf,
|
||||
) -> Result<PathBuf> {
|
||||
let version = *version.downcast::<String>().unwrap();
|
||||
let runtime = self.runtime.clone();
|
||||
let function = self.fetch_server_binary;
|
||||
self.executor
|
||||
.spawn(async move {
|
||||
let mut runtime = runtime.lock().await;
|
||||
let handle = runtime.attach_path(&container_dir)?;
|
||||
let result: Result<PathBuf, String> =
|
||||
runtime.call(&function, (container_dir, version)).await?;
|
||||
runtime.remove_resource(handle)?;
|
||||
result.map_err(|e| anyhow!("{}", e))
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
|
||||
let runtime = self.runtime.clone();
|
||||
let function = self.cached_server_binary;
|
||||
|
||||
self.executor
|
||||
.spawn(async move {
|
||||
let mut runtime = runtime.lock().await;
|
||||
let handle = runtime.attach_path(&container_dir).ok()?;
|
||||
let result: Option<PathBuf> = runtime.call(&function, container_dir).await.ok()?;
|
||||
runtime.remove_resource(handle).ok()?;
|
||||
result
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn initialization_options(&self) -> Option<serde_json::Value> {
|
||||
let string: String = self
|
||||
.runtime
|
||||
.lock()
|
||||
.await
|
||||
.call(&self.initialization_options, ())
|
||||
.await
|
||||
.log_err()?;
|
||||
|
||||
serde_json::from_str(&string).ok()
|
||||
}
|
||||
}
|
|
@ -1,15 +1,12 @@
|
|||
use super::installation::{npm_install_packages, npm_package_latest_version};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use futures::StreamExt;
|
||||
use language::{LanguageServerName, LspAdapter};
|
||||
use smol::fs;
|
||||
use std::{
|
||||
any::Any,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
use std::{any::Any, path::PathBuf, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
pub struct PythonLspAdapter;
|
||||
|
||||
|
@ -17,61 +14,56 @@ impl PythonLspAdapter {
|
|||
const BIN_PATH: &'static str = "node_modules/pyright/langserver.index.js";
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LspAdapter for PythonLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
async fn name(&self) -> LanguageServerName {
|
||||
LanguageServerName("pyright".into())
|
||||
}
|
||||
|
||||
fn server_args(&self) -> &[&str] {
|
||||
&["--stdio"]
|
||||
async fn server_args(&self) -> Vec<String> {
|
||||
vec!["--stdio".into()]
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
async fn fetch_latest_server_version(
|
||||
&self,
|
||||
_: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Any + Send>>> {
|
||||
async move { Ok(Box::new(npm_package_latest_version("pyright").await?) as Box<_>) }.boxed()
|
||||
) -> Result<Box<dyn 'static + Any + Send>> {
|
||||
Ok(Box::new(npm_package_latest_version("pyright").await?) as Box<_>)
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
async fn fetch_server_binary(
|
||||
&self,
|
||||
version: Box<dyn 'static + Send + Any>,
|
||||
_: Arc<dyn HttpClient>,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
container_dir: PathBuf,
|
||||
) -> Result<PathBuf> {
|
||||
let version = version.downcast::<String>().unwrap();
|
||||
async move {
|
||||
let version_dir = container_dir.join(version.as_str());
|
||||
fs::create_dir_all(&version_dir)
|
||||
.await
|
||||
.context("failed to create version directory")?;
|
||||
let binary_path = version_dir.join(Self::BIN_PATH);
|
||||
let version_dir = container_dir.join(version.as_str());
|
||||
fs::create_dir_all(&version_dir)
|
||||
.await
|
||||
.context("failed to create version directory")?;
|
||||
let binary_path = version_dir.join(Self::BIN_PATH);
|
||||
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
npm_install_packages([("pyright", version.as_str())], &version_dir).await?;
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
npm_install_packages([("pyright", version.as_str())], &version_dir).await?;
|
||||
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
.boxed()
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
|
||||
fn cached_server_binary(
|
||||
&self,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
async move {
|
||||
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
|
||||
(|| async move {
|
||||
let mut last_version_dir = None;
|
||||
let mut entries = fs::read_dir(&container_dir).await?;
|
||||
while let Some(entry) = entries.next().await {
|
||||
|
@ -90,12 +82,12 @@ impl LspAdapter for PythonLspAdapter {
|
|||
last_version_dir
|
||||
))
|
||||
}
|
||||
}
|
||||
})()
|
||||
.await
|
||||
.log_err()
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
async fn label_for_completion(
|
||||
&self,
|
||||
item: &lsp::CompletionItem,
|
||||
language: &language::Language,
|
||||
|
@ -116,7 +108,7 @@ impl LspAdapter for PythonLspAdapter {
|
|||
})
|
||||
}
|
||||
|
||||
fn label_for_symbol(
|
||||
async fn label_for_symbol(
|
||||
&self,
|
||||
name: &str,
|
||||
kind: lsp::SymbolKind,
|
||||
|
|
|
@ -1,116 +1,102 @@
|
|||
use super::installation::{latest_github_release, GitHubLspBinaryVersion};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_compression::futures::bufread::GzipDecoder;
|
||||
use async_trait::async_trait;
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, io::BufReader, FutureExt, StreamExt};
|
||||
use futures::{io::BufReader, StreamExt};
|
||||
pub use language::*;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use smol::fs::{self, File};
|
||||
use std::{
|
||||
any::Any,
|
||||
borrow::Cow,
|
||||
env::consts,
|
||||
path::{Path, PathBuf},
|
||||
str,
|
||||
sync::Arc,
|
||||
};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, str, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
pub struct RustLspAdapter;
|
||||
|
||||
#[async_trait]
|
||||
impl LspAdapter for RustLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
async fn name(&self) -> LanguageServerName {
|
||||
LanguageServerName("rust-analyzer".into())
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
async fn fetch_latest_server_version(
|
||||
&self,
|
||||
http: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||
async move {
|
||||
let release = latest_github_release("rust-analyzer/rust-analyzer", http).await?;
|
||||
let asset_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH);
|
||||
let asset = release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
||||
let version = GitHubLspBinaryVersion {
|
||||
name: release.name,
|
||||
url: asset.browser_download_url.clone(),
|
||||
};
|
||||
Ok(Box::new(version) as Box<_>)
|
||||
}
|
||||
.boxed()
|
||||
) -> Result<Box<dyn 'static + Send + Any>> {
|
||||
let release = latest_github_release("rust-analyzer/rust-analyzer", http).await?;
|
||||
let asset_name = format!("rust-analyzer-{}-apple-darwin.gz", consts::ARCH);
|
||||
let asset = release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == asset_name)
|
||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
||||
let version = GitHubLspBinaryVersion {
|
||||
name: release.name,
|
||||
url: asset.browser_download_url.clone(),
|
||||
};
|
||||
Ok(Box::new(version) as Box<_>)
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
async fn fetch_server_binary(
|
||||
&self,
|
||||
version: Box<dyn 'static + Send + Any>,
|
||||
http: Arc<dyn HttpClient>,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
async move {
|
||||
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
|
||||
let destination_path = container_dir.join(format!("rust-analyzer-{}", version.name));
|
||||
container_dir: PathBuf,
|
||||
) -> Result<PathBuf> {
|
||||
let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();
|
||||
let destination_path = container_dir.join(format!("rust-analyzer-{}", version.name));
|
||||
|
||||
if fs::metadata(&destination_path).await.is_err() {
|
||||
let mut response = http
|
||||
.get(&version.url, Default::default(), true)
|
||||
.await
|
||||
.map_err(|err| anyhow!("error downloading release: {}", err))?;
|
||||
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
|
||||
let mut file = File::create(&destination_path).await?;
|
||||
futures::io::copy(decompressed_bytes, &mut file).await?;
|
||||
fs::set_permissions(
|
||||
&destination_path,
|
||||
<fs::Permissions as fs::unix::PermissionsExt>::from_mode(0o755),
|
||||
)
|
||||
.await?;
|
||||
if fs::metadata(&destination_path).await.is_err() {
|
||||
let mut response = http
|
||||
.get(&version.url, Default::default(), true)
|
||||
.await
|
||||
.map_err(|err| anyhow!("error downloading release: {}", err))?;
|
||||
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
|
||||
let mut file = File::create(&destination_path).await?;
|
||||
futures::io::copy(decompressed_bytes, &mut file).await?;
|
||||
fs::set_permissions(
|
||||
&destination_path,
|
||||
<fs::Permissions as fs::unix::PermissionsExt>::from_mode(0o755),
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != destination_path {
|
||||
fs::remove_file(&entry_path).await.log_err();
|
||||
}
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != destination_path {
|
||||
fs::remove_file(&entry_path).await.log_err();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(destination_path)
|
||||
}
|
||||
.boxed()
|
||||
|
||||
Ok(destination_path)
|
||||
}
|
||||
|
||||
fn cached_server_binary(
|
||||
&self,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
async move {
|
||||
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
|
||||
(|| async move {
|
||||
let mut last = None;
|
||||
let mut entries = fs::read_dir(&container_dir).await?;
|
||||
while let Some(entry) = entries.next().await {
|
||||
last = Some(entry?.path());
|
||||
}
|
||||
last.ok_or_else(|| anyhow!("no cached binary"))
|
||||
}
|
||||
})()
|
||||
.await
|
||||
.log_err()
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
|
||||
&["rustc"]
|
||||
async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
|
||||
vec!["rustc".into()]
|
||||
}
|
||||
|
||||
fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
|
||||
Some("rustAnalyzer/cargo check")
|
||||
async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
|
||||
Some("rustAnalyzer/cargo check".into())
|
||||
}
|
||||
|
||||
fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
|
||||
async fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
|
||||
lazy_static! {
|
||||
static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap();
|
||||
}
|
||||
|
@ -130,7 +116,7 @@ impl LspAdapter for RustLspAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
async fn label_for_completion(
|
||||
&self,
|
||||
completion: &lsp::CompletionItem,
|
||||
language: &Language,
|
||||
|
@ -206,7 +192,7 @@ impl LspAdapter for RustLspAdapter {
|
|||
None
|
||||
}
|
||||
|
||||
fn label_for_symbol(
|
||||
async fn label_for_symbol(
|
||||
&self,
|
||||
name: &str,
|
||||
kind: lsp::SymbolKind,
|
||||
|
@ -269,12 +255,12 @@ impl LspAdapter for RustLspAdapter {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::languages::{language, LspAdapter};
|
||||
use crate::languages::{language, CachedLspAdapter};
|
||||
use gpui::{color::Color, MutableAppContext};
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
#[test]
|
||||
fn test_process_rust_diagnostics() {
|
||||
#[gpui::test]
|
||||
async fn test_process_rust_diagnostics() {
|
||||
let mut params = lsp::PublishDiagnosticsParams {
|
||||
uri: lsp::Url::from_file_path("/a").unwrap(),
|
||||
version: None,
|
||||
|
@ -297,7 +283,7 @@ mod tests {
|
|||
},
|
||||
],
|
||||
};
|
||||
RustLspAdapter.process_diagnostics(&mut params);
|
||||
RustLspAdapter.process_diagnostics(&mut params).await;
|
||||
|
||||
assert_eq!(params.diagnostics[0].message, "use of moved value `a`");
|
||||
|
||||
|
@ -314,12 +300,12 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_label_for_completion() {
|
||||
#[gpui::test]
|
||||
async fn test_rust_label_for_completion() {
|
||||
let language = language(
|
||||
"rust",
|
||||
tree_sitter_rust::language(),
|
||||
Some(Arc::new(RustLspAdapter)),
|
||||
Some(CachedLspAdapter::new(RustLspAdapter).await),
|
||||
);
|
||||
let grammar = language.grammar().unwrap();
|
||||
let theme = SyntaxTheme::new(vec![
|
||||
|
@ -337,12 +323,14 @@ mod tests {
|
|||
let highlight_field = grammar.highlight_id_for_name("property").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
language.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FUNCTION),
|
||||
label: "hello(…)".to_string(),
|
||||
detail: Some("fn(&mut Option<T>) -> Vec<T>".to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
language
|
||||
.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FUNCTION),
|
||||
label: "hello(…)".to_string(),
|
||||
detail: Some("fn(&mut Option<T>) -> Vec<T>".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "hello(&mut Option<T>) -> Vec<T>".to_string(),
|
||||
filter_range: 0..5,
|
||||
|
@ -358,12 +346,14 @@ mod tests {
|
|||
);
|
||||
|
||||
assert_eq!(
|
||||
language.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FIELD),
|
||||
label: "len".to_string(),
|
||||
detail: Some("usize".to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
language
|
||||
.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FIELD),
|
||||
label: "len".to_string(),
|
||||
detail: Some("usize".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "len: usize".to_string(),
|
||||
filter_range: 0..3,
|
||||
|
@ -372,12 +362,14 @@ mod tests {
|
|||
);
|
||||
|
||||
assert_eq!(
|
||||
language.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FUNCTION),
|
||||
label: "hello(…)".to_string(),
|
||||
detail: Some("fn(&mut Option<T>) -> Vec<T>".to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
language
|
||||
.label_for_completion(&lsp::CompletionItem {
|
||||
kind: Some(lsp::CompletionItemKind::FUNCTION),
|
||||
label: "hello(…)".to_string(),
|
||||
detail: Some("fn(&mut Option<T>) -> Vec<T>".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "hello(&mut Option<T>) -> Vec<T>".to_string(),
|
||||
filter_range: 0..5,
|
||||
|
@ -393,12 +385,12 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_label_for_symbol() {
|
||||
#[gpui::test]
|
||||
async fn test_rust_label_for_symbol() {
|
||||
let language = language(
|
||||
"rust",
|
||||
tree_sitter_rust::language(),
|
||||
Some(Arc::new(RustLspAdapter)),
|
||||
Some(CachedLspAdapter::new(RustLspAdapter).await),
|
||||
);
|
||||
let grammar = language.grammar().unwrap();
|
||||
let theme = SyntaxTheme::new(vec![
|
||||
|
@ -415,7 +407,9 @@ mod tests {
|
|||
let highlight_keyword = grammar.highlight_id_for_name("keyword").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
language.label_for_symbol("hello", lsp::SymbolKind::FUNCTION),
|
||||
language
|
||||
.label_for_symbol("hello", lsp::SymbolKind::FUNCTION)
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "fn hello".to_string(),
|
||||
filter_range: 3..8,
|
||||
|
@ -424,7 +418,9 @@ mod tests {
|
|||
);
|
||||
|
||||
assert_eq!(
|
||||
language.label_for_symbol("World", lsp::SymbolKind::TYPE_PARAMETER),
|
||||
language
|
||||
.label_for_symbol("World", lsp::SymbolKind::TYPE_PARAMETER)
|
||||
.await,
|
||||
Some(CodeLabel {
|
||||
text: "type World".to_string(),
|
||||
filter_range: 5..10,
|
||||
|
|
|
@ -1,16 +1,13 @@
|
|||
use super::installation::{npm_install_packages, npm_package_latest_version};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use client::http::HttpClient;
|
||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||
use futures::StreamExt;
|
||||
use language::{LanguageServerName, LspAdapter};
|
||||
use serde_json::json;
|
||||
use smol::fs;
|
||||
use std::{
|
||||
any::Any,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use util::{ResultExt, TryFutureExt};
|
||||
use std::{any::Any, path::PathBuf, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
pub struct TypeScriptLspAdapter;
|
||||
|
||||
|
@ -23,80 +20,75 @@ struct Versions {
|
|||
server_version: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LspAdapter for TypeScriptLspAdapter {
|
||||
fn name(&self) -> LanguageServerName {
|
||||
async fn name(&self) -> LanguageServerName {
|
||||
LanguageServerName("typescript-language-server".into())
|
||||
}
|
||||
|
||||
fn server_args(&self) -> &[&str] {
|
||||
&["--stdio", "--tsserver-path", "node_modules/typescript/lib"]
|
||||
async fn server_args(&self) -> Vec<String> {
|
||||
["--stdio", "--tsserver-path", "node_modules/typescript/lib"]
|
||||
.into_iter()
|
||||
.map(str::to_string)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn fetch_latest_server_version(
|
||||
async fn fetch_latest_server_version(
|
||||
&self,
|
||||
_: Arc<dyn HttpClient>,
|
||||
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
||||
async move {
|
||||
Ok(Box::new(Versions {
|
||||
typescript_version: npm_package_latest_version("typescript").await?,
|
||||
server_version: npm_package_latest_version("typescript-language-server").await?,
|
||||
}) as Box<_>)
|
||||
}
|
||||
.boxed()
|
||||
) -> Result<Box<dyn 'static + Send + Any>> {
|
||||
Ok(Box::new(Versions {
|
||||
typescript_version: npm_package_latest_version("typescript").await?,
|
||||
server_version: npm_package_latest_version("typescript-language-server").await?,
|
||||
}) as Box<_>)
|
||||
}
|
||||
|
||||
fn fetch_server_binary(
|
||||
async fn fetch_server_binary(
|
||||
&self,
|
||||
versions: Box<dyn 'static + Send + Any>,
|
||||
_: Arc<dyn HttpClient>,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Result<PathBuf>> {
|
||||
container_dir: PathBuf,
|
||||
) -> Result<PathBuf> {
|
||||
let versions = versions.downcast::<Versions>().unwrap();
|
||||
async move {
|
||||
let version_dir = container_dir.join(&format!(
|
||||
"typescript-{}:server-{}",
|
||||
versions.typescript_version, versions.server_version
|
||||
));
|
||||
fs::create_dir_all(&version_dir)
|
||||
.await
|
||||
.context("failed to create version directory")?;
|
||||
let binary_path = version_dir.join(Self::BIN_PATH);
|
||||
let version_dir = container_dir.join(&format!(
|
||||
"typescript-{}:server-{}",
|
||||
versions.typescript_version, versions.server_version
|
||||
));
|
||||
fs::create_dir_all(&version_dir)
|
||||
.await
|
||||
.context("failed to create version directory")?;
|
||||
let binary_path = version_dir.join(Self::BIN_PATH);
|
||||
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
npm_install_packages(
|
||||
[
|
||||
("typescript", versions.typescript_version.as_str()),
|
||||
(
|
||||
"typescript-language-server",
|
||||
&versions.server_version.as_str(),
|
||||
),
|
||||
],
|
||||
&version_dir,
|
||||
)
|
||||
.await?;
|
||||
if fs::metadata(&binary_path).await.is_err() {
|
||||
npm_install_packages(
|
||||
[
|
||||
("typescript", versions.typescript_version.as_str()),
|
||||
(
|
||||
"typescript-language-server",
|
||||
&versions.server_version.as_str(),
|
||||
),
|
||||
],
|
||||
&version_dir,
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
|
||||
while let Some(entry) = entries.next().await {
|
||||
if let Some(entry) = entry.log_err() {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.as_path() != version_dir {
|
||||
fs::remove_dir_all(&entry_path).await.log_err();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
.boxed()
|
||||
|
||||
Ok(binary_path)
|
||||
}
|
||||
|
||||
fn cached_server_binary(
|
||||
&self,
|
||||
container_dir: Arc<Path>,
|
||||
) -> BoxFuture<'static, Option<PathBuf>> {
|
||||
async move {
|
||||
async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
|
||||
(|| async move {
|
||||
let mut last_version_dir = None;
|
||||
let mut entries = fs::read_dir(&container_dir).await?;
|
||||
while let Some(entry) = entries.next().await {
|
||||
|
@ -115,12 +107,12 @@ impl LspAdapter for TypeScriptLspAdapter {
|
|||
last_version_dir
|
||||
))
|
||||
}
|
||||
}
|
||||
})()
|
||||
.await
|
||||
.log_err()
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
async fn label_for_completion(
|
||||
&self,
|
||||
item: &lsp::CompletionItem,
|
||||
language: &language::Language,
|
||||
|
@ -143,7 +135,7 @@ impl LspAdapter for TypeScriptLspAdapter {
|
|||
})
|
||||
}
|
||||
|
||||
fn initialization_options(&self) -> Option<serde_json::Value> {
|
||||
async fn initialization_options(&self) -> Option<serde_json::Value> {
|
||||
Some(json!({
|
||||
"provideFormatter": true
|
||||
}))
|
||||
|
|
|
@ -21,6 +21,7 @@ use futures::{
|
|||
};
|
||||
use gpui::{executor::Background, App, AssetSource, AsyncAppContext, Task};
|
||||
use isahc::{config::Configurable, AsyncBody, Request};
|
||||
use language::LanguageRegistry;
|
||||
use log::LevelFilter;
|
||||
use parking_lot::Mutex;
|
||||
use project::{Fs, ProjectStore};
|
||||
|
@ -163,7 +164,12 @@ fn main() {
|
|||
|
||||
app.run(move |cx| {
|
||||
let client = client::Client::new(http.clone());
|
||||
let mut languages = languages::build_language_registry(login_shell_env_loaded);
|
||||
let mut languages = LanguageRegistry::new(login_shell_env_loaded);
|
||||
languages.set_language_server_download_dir(zed::ROOT_PATH.clone());
|
||||
let languages = Arc::new(languages);
|
||||
let init_languages = cx
|
||||
.background()
|
||||
.spawn(languages::init(languages.clone(), cx.background().clone()));
|
||||
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
|
||||
|
||||
context_menu::init(cx);
|
||||
|
@ -208,17 +214,22 @@ fn main() {
|
|||
})
|
||||
.detach();
|
||||
|
||||
languages.set_language_server_download_dir(zed::ROOT_PATH.clone());
|
||||
let languages = Arc::new(languages);
|
||||
|
||||
cx.observe_global::<Settings, _>({
|
||||
let languages = languages.clone();
|
||||
move |cx| {
|
||||
languages.set_theme(&cx.global::<Settings>().theme.editor.syntax);
|
||||
languages.set_theme(cx.global::<Settings>().theme.clone());
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
cx.set_global(settings);
|
||||
cx.spawn({
|
||||
let languages = languages.clone();
|
||||
|cx| async move {
|
||||
cx.read(|cx| languages.set_theme(cx.global::<Settings>().theme.clone()));
|
||||
init_languages.await;
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
let project_store = cx.add_model(|_| ProjectStore::new(db.clone()));
|
||||
let app_state = Arc::new(AppState {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue