mirror of
https://github.com/zed-industries/zed.git
synced 2024-09-18 18:08:07 +03:00
Move command palette filter into collections crate
Filter out copilot commands from command palette when not active
This commit is contained in:
parent
aea8475d30
commit
0b0c7e4ce9
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1338,6 +1338,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"async-compression",
|
"async-compression",
|
||||||
"client",
|
"client",
|
||||||
|
"collections",
|
||||||
"futures 0.3.25",
|
"futures 0.3.25",
|
||||||
"gpui",
|
"gpui",
|
||||||
"language",
|
"language",
|
||||||
|
@ -24,3 +24,10 @@ pub type HashMap<K, V> = std::collections::HashMap<K, V>;
|
|||||||
pub type HashSet<T> = std::collections::HashSet<T>;
|
pub type HashSet<T> = std::collections::HashSet<T>;
|
||||||
|
|
||||||
pub use std::collections::*;
|
pub use std::collections::*;
|
||||||
|
|
||||||
|
// NEW TYPES
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct CommandPaletteFilter {
|
||||||
|
pub filtered_namespaces: HashSet<&'static str>,
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use collections::HashSet;
|
use collections::CommandPaletteFilter;
|
||||||
use fuzzy::{StringMatch, StringMatchCandidate};
|
use fuzzy::{StringMatch, StringMatchCandidate};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions,
|
actions,
|
||||||
@ -12,11 +12,6 @@ use settings::Settings;
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
use workspace::Workspace;
|
use workspace::Workspace;
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct CommandPaletteFilter {
|
|
||||||
pub filtered_namespaces: HashSet<&'static str>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(cx: &mut MutableAppContext) {
|
pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_action(CommandPalette::toggle);
|
cx.add_action(CommandPalette::toggle);
|
||||||
Picker::<CommandPalette>::init(cx);
|
Picker::<CommandPalette>::init(cx);
|
||||||
|
@ -9,6 +9,7 @@ path = "src/copilot.rs"
|
|||||||
doctest = false
|
doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
collections = { path = "../collections" }
|
||||||
gpui = { path = "../gpui" }
|
gpui = { path = "../gpui" }
|
||||||
language = { path = "../language" }
|
language = { path = "../language" }
|
||||||
settings = { path = "../settings" }
|
settings = { path = "../settings" }
|
||||||
|
@ -2,7 +2,6 @@ mod request;
|
|||||||
mod sign_in;
|
mod sign_in;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use async_compression::futures::bufread::GzipDecoder;
|
|
||||||
use client::Client;
|
use client::Client;
|
||||||
use futures::{future::Shared, FutureExt, TryFutureExt};
|
use futures::{future::Shared, FutureExt, TryFutureExt};
|
||||||
use gpui::{actions, AppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task};
|
use gpui::{actions, AppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task};
|
||||||
@ -10,17 +9,18 @@ use language::{point_from_lsp, point_to_lsp, Anchor, Bias, Buffer, BufferSnapsho
|
|||||||
use lsp::LanguageServer;
|
use lsp::LanguageServer;
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use smol::{fs, io::BufReader, stream::StreamExt};
|
use smol::{fs, stream::StreamExt};
|
||||||
use std::{
|
use std::{
|
||||||
env::consts,
|
ffi::OsString,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
use util::{
|
use util::{fs::remove_matching, http::HttpClient, paths, ResultExt};
|
||||||
fs::remove_matching, github::latest_github_release, http::HttpClient, paths, ResultExt,
|
|
||||||
};
|
|
||||||
|
|
||||||
actions!(copilot, [SignIn, SignOut, NextSuggestion]);
|
actions!(copilot_auth, [SignIn, SignOut]);
|
||||||
|
|
||||||
|
const COPILOT_NAMESPACE: &'static str = "copilot";
|
||||||
|
actions!(copilot, [NextSuggestion]);
|
||||||
|
|
||||||
pub fn init(client: Arc<Client>, node_runtime: Arc<NodeRuntime>, cx: &mut MutableAppContext) {
|
pub fn init(client: Arc<Client>, node_runtime: Arc<NodeRuntime>, cx: &mut MutableAppContext) {
|
||||||
let copilot = cx.add_model(|cx| Copilot::start(client.http_client(), node_runtime, cx));
|
let copilot = cx.add_model(|cx| Copilot::start(client.http_client(), node_runtime, cx));
|
||||||
@ -37,6 +37,18 @@ pub fn init(client: Arc<Client>, node_runtime: Arc<NodeRuntime>, cx: &mut Mutabl
|
|||||||
.update(cx, |copilot, cx| copilot.sign_out(cx))
|
.update(cx, |copilot, cx| copilot.sign_out(cx))
|
||||||
.detach_and_log_err(cx);
|
.detach_and_log_err(cx);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
cx.observe(&copilot, |handle, cx| {
|
||||||
|
let status = handle.read(cx).status();
|
||||||
|
cx.update_global::<collections::CommandPaletteFilter, _, _>(
|
||||||
|
move |filter, _cx| match status {
|
||||||
|
Status::Authorized => filter.filtered_namespaces.remove(COPILOT_NAMESPACE),
|
||||||
|
_ => filter.filtered_namespaces.insert(COPILOT_NAMESPACE),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
sign_in::init(cx);
|
sign_in::init(cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,9 +125,12 @@ impl Copilot {
|
|||||||
// TODO: Don't eagerly download the LSP
|
// TODO: Don't eagerly download the LSP
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
let start_language_server = async {
|
let start_language_server = async {
|
||||||
let server_path = get_lsp_binary(http).await?;
|
let server_path = get_copilot_lsp(http, node_runtime.clone()).await?;
|
||||||
|
let node_path = node_runtime.binary_path().await?;
|
||||||
|
let arguments: &[OsString] = &[server_path.into(), "--stdio".into()];
|
||||||
let server =
|
let server =
|
||||||
LanguageServer::new(0, &server_path, &["--stdio"], Path::new("/"), cx.clone())?;
|
LanguageServer::new(0, &node_path, arguments, Path::new("/"), cx.clone())?;
|
||||||
|
|
||||||
let server = server.initialize(Default::default()).await?;
|
let server = server.initialize(Default::default()).await?;
|
||||||
let status = server
|
let status = server
|
||||||
.request::<request::CheckStatus>(request::CheckStatusParams {
|
.request::<request::CheckStatus>(request::CheckStatusParams {
|
||||||
@ -414,53 +429,61 @@ fn completion_from_lsp(completion: request::Completion, buffer: &BufferSnapshot)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_lsp_binary(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
|
async fn get_copilot_lsp(
|
||||||
|
http: Arc<dyn HttpClient>,
|
||||||
|
node: Arc<NodeRuntime>,
|
||||||
|
) -> anyhow::Result<PathBuf> {
|
||||||
|
const SERVER_PATH: &'static str = "node_modules/copilot-node-server/copilot/dist/agent.js";
|
||||||
|
|
||||||
///Check for the latest copilot language server and download it if we haven't already
|
///Check for the latest copilot language server and download it if we haven't already
|
||||||
async fn fetch_latest(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
|
async fn fetch_latest(
|
||||||
let release = latest_github_release("zed-industries/copilot", http.clone()).await?;
|
_http: Arc<dyn HttpClient>,
|
||||||
let asset_name = format!("copilot-darwin-{}.gz", consts::ARCH);
|
node: Arc<NodeRuntime>,
|
||||||
let asset = release
|
) -> anyhow::Result<PathBuf> {
|
||||||
.assets
|
const COPILOT_NPM_PACKAGE: &'static str = "copilot-node-server";
|
||||||
.iter()
|
|
||||||
.find(|asset| asset.name == asset_name)
|
|
||||||
.ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;
|
|
||||||
|
|
||||||
fs::create_dir_all(&*paths::COPILOT_DIR).await?;
|
let release = node.npm_package_latest_version(COPILOT_NPM_PACKAGE).await?;
|
||||||
let destination_path =
|
|
||||||
paths::COPILOT_DIR.join(format!("copilot-{}-{}", release.name, consts::ARCH));
|
|
||||||
|
|
||||||
if fs::metadata(&destination_path).await.is_err() {
|
let version_dir = &*paths::COPILOT_DIR.join(format!("copilot-{}", release.clone()));
|
||||||
let mut response = http
|
|
||||||
.get(&asset.browser_download_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 = fs::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?;
|
|
||||||
|
|
||||||
remove_matching(&paths::COPILOT_DIR, |entry| entry != destination_path).await;
|
fs::create_dir_all(version_dir).await?;
|
||||||
|
let server_path = version_dir.join(SERVER_PATH);
|
||||||
|
|
||||||
|
if fs::metadata(&server_path).await.is_err() {
|
||||||
|
node.npm_install_packages([(COPILOT_NPM_PACKAGE, release.as_str())], version_dir)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
remove_matching(&paths::COPILOT_DIR, |entry| entry != version_dir).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(destination_path)
|
Ok(server_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
match fetch_latest(http).await {
|
match fetch_latest(http, node).await {
|
||||||
ok @ Result::Ok(..) => ok,
|
ok @ Result::Ok(..) => ok,
|
||||||
e @ Err(..) => {
|
e @ Err(..) => {
|
||||||
e.log_err();
|
e.log_err();
|
||||||
// Fetch a cached binary, if it exists
|
// Fetch a cached binary, if it exists
|
||||||
(|| async move {
|
(|| async move {
|
||||||
let mut last = None;
|
let mut last_version_dir = None;
|
||||||
let mut entries = fs::read_dir(paths::COPILOT_DIR.as_path()).await?;
|
let mut entries = fs::read_dir(paths::COPILOT_DIR.as_path()).await?;
|
||||||
while let Some(entry) = entries.next().await {
|
while let Some(entry) = entries.next().await {
|
||||||
last = Some(entry?.path());
|
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(server_path)
|
||||||
|
} else {
|
||||||
|
Err(anyhow!(
|
||||||
|
"missing executable in directory {:?}",
|
||||||
|
last_version_dir
|
||||||
|
))
|
||||||
}
|
}
|
||||||
last.ok_or_else(|| anyhow!("no cached binary"))
|
|
||||||
})()
|
})()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{request::PromptUserDeviceFlow, Copilot};
|
use crate::{request::PromptUserDeviceFlow, Copilot};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
elements::*, geometry::rect::RectF, impl_internal_actions, ClipboardItem, Element, Entity,
|
elements::*, geometry::rect::RectF, ClipboardItem, Element, Entity, MutableAppContext, View,
|
||||||
MutableAppContext, View, WindowKind, WindowOptions,
|
WindowKind, WindowOptions,
|
||||||
};
|
};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
|
|
||||||
@ -11,8 +11,6 @@ struct CopyUserCode;
|
|||||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||||
struct OpenGithub;
|
struct OpenGithub;
|
||||||
|
|
||||||
impl_internal_actions!(copilot_sign_in, [CopyUserCode, OpenGithub]);
|
|
||||||
|
|
||||||
const _COPILOT_SIGN_UP_URL: &'static str = "https://github.com/features/copilot";
|
const _COPILOT_SIGN_UP_URL: &'static str = "https://github.com/features/copilot";
|
||||||
|
|
||||||
enum SignInContents {
|
enum SignInContents {
|
||||||
|
@ -12,7 +12,7 @@ mod visual;
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use command_palette::CommandPaletteFilter;
|
use collections::CommandPaletteFilter;
|
||||||
use editor::{Bias, Cancel, Editor, EditorMode};
|
use editor::{Bias, Cancel, Editor, EditorMode};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, impl_actions, MutableAppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
|
actions, impl_actions, MutableAppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
|
||||||
|
Loading…
Reference in New Issue
Block a user