Add copilot crate

Refactor HTTP and github release downloading into util
Lazily download / upgrade the copilot LSP from Zed

Co-authored-by: Max <max@zed.dev>
Co-Authored-By: Antonio <antonio@zed.dev>
This commit is contained in:
Mikayla Maki
2023-03-28 09:41:59 -07:00
co-authored by Max Antonio
parent 35b2aceffb
commit 455cdc8b37
41 changed files with 435 additions and 265 deletions
+1 -1
View File
@@ -1,11 +1,11 @@
use anyhow::Context;
use client::http::HttpClient;
use gpui::executor::Background;
pub use language::*;
use node_runtime::NodeRuntime;
use rust_embed::RustEmbed;
use std::{borrow::Cow, str, sync::Arc};
use theme::ThemeRegistry;
use util::http::HttpClient;
mod c;
mod elixir;
+6 -12
View File
@@ -1,13 +1,16 @@
use super::github::{latest_github_release, GitHubLspBinaryVersion};
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
pub use language::*;
use smol::fs::{self, File};
use std::{any::Any, path::PathBuf, sync::Arc};
use util::fs::remove_matching;
use util::github::latest_github_release;
use util::http::HttpClient;
use util::ResultExt;
use super::github::GitHubLspBinaryVersion;
pub struct CLspAdapter;
#[async_trait]
@@ -69,16 +72,7 @@ impl super::LspAdapter for CLspAdapter {
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();
}
}
}
}
remove_matching(&container_dir, |entry| entry != version_dir).await;
}
Ok(LanguageServerBinary {
+6 -18
View File
@@ -1,14 +1,17 @@
use super::github::{latest_github_release, GitHubLspBinaryVersion};
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
pub use language::*;
use lsp::{CompletionItemKind, SymbolKind};
use smol::fs::{self, File};
use std::{any::Any, path::PathBuf, sync::Arc};
use util::fs::remove_matching;
use util::github::latest_github_release;
use util::http::HttpClient;
use util::ResultExt;
use super::github::GitHubLspBinaryVersion;
pub struct ElixirLspAdapter;
#[async_trait]
@@ -76,22 +79,7 @@ impl LspAdapter for ElixirLspAdapter {
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 {
if let Ok(metadata) = fs::metadata(&entry_path).await {
if metadata.is_file() {
fs::remove_file(&entry_path).await.log_err();
} else {
fs::remove_dir_all(&entry_path).await.log_err();
}
}
}
}
}
}
remove_matching(&container_dir, |entry| entry != version_dir).await;
}
Ok(LanguageServerBinary {
+1 -1
View File
@@ -1,8 +1,8 @@
use anyhow::{Context, Result};
use client::http::HttpClient;
use serde::Deserialize;
use smol::io::AsyncReadExt;
use std::sync::Arc;
use util::http::HttpClient;
pub struct GitHubLspBinaryVersion {
pub name: String,
+9 -15
View File
@@ -1,13 +1,15 @@
use super::github::latest_github_release;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
pub use language::*;
use lazy_static::lazy_static;
use regex::Regex;
use smol::{fs, process};
use std::{any::Any, ffi::OsString, ops::Range, path::PathBuf, str, sync::Arc};
use std::ffi::{OsStr, OsString};
use std::{any::Any, ops::Range, path::PathBuf, str, sync::Arc};
use util::fs::remove_matching;
use util::github::latest_github_release;
use util::http::HttpClient;
use util::ResultExt;
fn server_binary_arguments() -> Vec<OsString> {
@@ -55,18 +57,10 @@ impl super::LspAdapter for GoLspAdapter {
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();
}
}
}
}
remove_matching(&container_dir, |entry| {
entry != binary_path && entry.file_name() != Some(OsStr::new("gobin"))
})
.await;
return Ok(LanguageServerBinary {
path: binary_path.to_path_buf(),
+6 -17
View File
@@ -1,17 +1,15 @@
use super::node_runtime::NodeRuntime;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use serde_json::json;
use smol::fs;
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use std::ffi::OsString;
use std::path::Path;
use std::{any::Any, path::PathBuf, sync::Arc};
use util::fs::remove_matching;
use util::http::HttpClient;
use util::ResultExt;
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
@@ -69,16 +67,7 @@ impl LspAdapter for HtmlLspAdapter {
)
.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();
}
}
}
}
remove_matching(container_dir.as_path(), |entry| entry != version_dir).await;
}
Ok(LanguageServerBinary {
+2 -11
View File
@@ -1,7 +1,6 @@
use super::node_runtime::NodeRuntime;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use collections::HashMap;
use futures::{future::BoxFuture, FutureExt, StreamExt};
use gpui::MutableAppContext;
@@ -17,6 +16,7 @@ use std::{
sync::Arc,
};
use theme::ThemeRegistry;
use util::{fs::remove_matching, http::HttpClient};
use util::{paths, ResultExt, StaffMode};
const SERVER_PATH: &'static str =
@@ -84,16 +84,7 @@ impl LspAdapter for JsonLspAdapter {
)
.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();
}
}
}
}
remove_matching(&container_dir, |entry| entry != server_path).await;
}
Ok(LanguageServerBinary {
+1 -1
View File
@@ -1,12 +1,12 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use collections::HashMap;
use futures::lock::Mutex;
use gpui::executor::Background;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use plugin_runtime::{Plugin, PluginBinary, PluginBuilder, WasiFn};
use std::{any::Any, path::PathBuf, sync::Arc};
use util::http::HttpClient;
use util::ResultExt;
#[allow(dead_code)]
+3 -5
View File
@@ -1,16 +1,14 @@
use std::{any::Any, env::consts, ffi::OsString, path::PathBuf, sync::Arc};
use anyhow::{anyhow, bail, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use async_trait::async_trait;
use client::http::HttpClient;
use futures::{io::BufReader, StreamExt};
use language::{LanguageServerBinary, LanguageServerName};
use smol::fs;
use util::{async_iife, ResultExt};
use std::{any::Any, env::consts, ffi::OsString, path::PathBuf, sync::Arc};
use util::{async_iife, github::latest_github_release, http::HttpClient, ResultExt};
use super::github::{latest_github_release, GitHubLspBinaryVersion};
use super::github::GitHubLspBinaryVersion;
#[derive(Copy, Clone)]
pub struct LuaLspAdapter;
+1 -1
View File
@@ -1,7 +1,6 @@
use anyhow::{anyhow, bail, Context, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use client::http::HttpClient;
use futures::{future::Shared, FutureExt};
use gpui::{executor::Background, Task};
use parking_lot::Mutex;
@@ -12,6 +11,7 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use util::http::HttpClient;
const VERSION: &str = "v18.15.0";
+3 -11
View File
@@ -1,7 +1,6 @@
use super::node_runtime::NodeRuntime;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use smol::fs;
@@ -11,6 +10,8 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use util::fs::remove_matching;
use util::http::HttpClient;
use util::ResultExt;
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
@@ -60,16 +61,7 @@ impl LspAdapter for PythonLspAdapter {
.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();
}
}
}
}
remove_matching(&container_dir, |entry| entry != version_dir).await;
}
Ok(LanguageServerBinary {
+1 -1
View File
@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use std::{any::Any, path::PathBuf, sync::Arc};
use util::http::HttpClient;
pub struct RubyLanguageServer;
+3 -11
View File
@@ -2,13 +2,14 @@ use super::github::{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::{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::PathBuf, str, sync::Arc};
use util::fs::remove_matching;
use util::http::HttpClient;
use util::ResultExt;
pub struct RustLspAdapter;
@@ -60,16 +61,7 @@ impl LspAdapter for RustLspAdapter {
)
.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();
}
}
}
}
remove_matching(&container_dir, |entry| entry != destination_path).await;
}
Ok(LanguageServerBinary {
+3 -11
View File
@@ -1,7 +1,6 @@
use super::node_runtime::NodeRuntime;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::StreamExt;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use serde_json::json;
@@ -12,6 +11,8 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use util::fs::remove_matching;
use util::http::HttpClient;
use util::ResultExt;
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
@@ -90,16 +91,7 @@ impl LspAdapter for TypeScriptLspAdapter {
)
.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();
}
}
}
}
remove_matching(&container_dir, |entry| entry != version_dir).await;
}
Ok(LanguageServerBinary {
+2 -11
View File
@@ -1,7 +1,6 @@
use super::node_runtime::NodeRuntime;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use client::http::HttpClient;
use futures::{future::BoxFuture, FutureExt, StreamExt};
use gpui::MutableAppContext;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
@@ -16,6 +15,7 @@ use std::{
sync::Arc,
};
use util::ResultExt;
use util::{fs::remove_matching, http::HttpClient};
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
@@ -68,16 +68,7 @@ impl LspAdapter for YamlLspAdapter {
.npm_install_packages([("yaml-language-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();
}
}
}
}
remove_matching(&container_dir, |entry| entry != version_dir).await;
}
Ok(LanguageServerBinary {
+3 -5
View File
@@ -8,11 +8,7 @@ use cli::{
ipc::{self, IpcSender},
CliRequest, CliResponse, IpcHandshake,
};
use client::{
self,
http::{self, HttpClient},
UserStore, ZED_APP_VERSION, ZED_SECRET_CLIENT_TOKEN,
};
use client::{self, UserStore, ZED_APP_VERSION, ZED_SECRET_CLIENT_TOKEN};
use db::kvp::KEY_VALUE_STORE;
use futures::{
channel::{mpsc, oneshot},
@@ -36,6 +32,7 @@ use std::{
path::PathBuf, sync::Arc, thread, time::Duration,
};
use terminal_view::{get_working_directory, TerminalView};
use util::http::{self, HttpClient};
use welcome::{show_welcome_experience, FIRST_OPEN};
use fs::RealFs;
@@ -165,6 +162,7 @@ fn main() {
terminal_view::init(cx);
theme_testbench::init(cx);
recent_projects::init(cx);
copilot::init(client.clone(), cx);
cx.spawn(|cx| watch_themes(fs.clone(), themes.clone(), cx))
.detach();
+1 -1
View File
@@ -652,7 +652,6 @@ fn open_bundled_file(
mod tests {
use super::*;
use assets::Assets;
use client::test::FakeHttpClient;
use editor::{scroll::autoscroll::Autoscroll, DisplayPoint, Editor};
use gpui::{
executor::Deterministic, AssetSource, MutableAppContext, TestAppContext, ViewHandle,
@@ -665,6 +664,7 @@ mod tests {
path::{Path, PathBuf},
};
use theme::ThemeRegistry;
use util::http::FakeHttpClient;
use workspace::{
item::{Item, ItemHandle},
open_new, open_paths, pane, NewFile, Pane, SplitDirection, WorkspaceHandle,