Merge pull request #2350 from zed-industries/copilot-disable

Disable copilot unless the staff mode flag is flipped
This commit is contained in:
Mikayla Maki 2023-04-03 22:00:47 -07:00 committed by Max Brunsfeld
parent 423ba0d351
commit c4b98b8cf1
16 changed files with 129 additions and 33 deletions

13
Cargo.lock generated
View file

@ -1123,6 +1123,7 @@ dependencies = [
"serde_derive", "serde_derive",
"settings", "settings",
"smol", "smol",
"staff_mode",
"sum_tree", "sum_tree",
"tempfile", "tempfile",
"thiserror", "thiserror",
@ -1351,6 +1352,7 @@ dependencies = [
"serde_derive", "serde_derive",
"settings", "settings",
"smol", "smol",
"staff_mode",
"theme", "theme",
"util", "util",
"workspace", "workspace",
@ -5948,6 +5950,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_path_to_error", "serde_path_to_error",
"sqlez", "sqlez",
"staff_mode",
"theme", "theme",
"toml", "toml",
"tree-sitter", "tree-sitter",
@ -6358,6 +6361,14 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "staff_mode"
version = "0.1.0"
dependencies = [
"anyhow",
"gpui",
]
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"
@ -6676,6 +6687,7 @@ dependencies = [
"postage", "postage",
"settings", "settings",
"smol", "smol",
"staff_mode",
"theme", "theme",
"util", "util",
"workspace", "workspace",
@ -8576,6 +8588,7 @@ dependencies = [
"simplelog", "simplelog",
"smallvec", "smallvec",
"smol", "smol",
"staff_mode",
"sum_tree", "sum_tree",
"tempdir", "tempdir",
"terminal_view", "terminal_view",

View file

@ -54,6 +54,7 @@ members = [
"crates/snippet", "crates/snippet",
"crates/sqlez", "crates/sqlez",
"crates/sqlez_macros", "crates/sqlez_macros",
"crates/staff_mode",
"crates/sum_tree", "crates/sum_tree",
"crates/terminal", "crates/terminal",
"crates/text", "crates/text",

View file

@ -17,6 +17,7 @@ db = { path = "../db" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
util = { path = "../util" } util = { path = "../util" }
rpc = { path = "../rpc" } rpc = { path = "../rpc" }
staff_mode = { path = "../staff_mode" }
sum_tree = { path = "../sum_tree" } sum_tree = { path = "../sum_tree" }
anyhow = "1.0.38" anyhow = "1.0.38"
async-recursion = "0.3" async-recursion = "0.3"

View file

@ -6,9 +6,10 @@ use gpui::{AsyncAppContext, Entity, ImageData, ModelContext, ModelHandle, Task};
use postage::{sink::Sink, watch}; use postage::{sink::Sink, watch};
use rpc::proto::{RequestMessage, UsersResponse}; use rpc::proto::{RequestMessage, UsersResponse};
use settings::Settings; use settings::Settings;
use staff_mode::StaffMode;
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use util::http::HttpClient; use util::http::HttpClient;
use util::{StaffMode, TryFutureExt as _}; use util::TryFutureExt as _;
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct User { pub struct User {

View file

@ -15,6 +15,7 @@ gpui = { path = "../gpui" }
language = { path = "../language" } language = { path = "../language" }
settings = { path = "../settings" } settings = { path = "../settings" }
theme = { path = "../theme" } theme = { path = "../theme" }
staff_mode = { path = "../staff_mode" }
lsp = { path = "../lsp" } lsp = { path = "../lsp" }
node_runtime = { path = "../node_runtime"} node_runtime = { path = "../node_runtime"}
util = { path = "../util" } util = { path = "../util" }

View file

@ -18,6 +18,8 @@ use node_runtime::NodeRuntime;
use request::{LogMessage, StatusNotification}; use request::{LogMessage, StatusNotification};
use settings::Settings; use settings::Settings;
use smol::{fs, io::BufReader, stream::StreamExt}; use smol::{fs, io::BufReader, stream::StreamExt};
use staff_mode::{not_staff_mode, staff_mode};
use std::{ use std::{
ffi::OsString, ffi::OsString,
ops::Range, ops::Range,
@ -35,28 +37,57 @@ const COPILOT_NAMESPACE: &'static str = "copilot";
actions!(copilot, [NextSuggestion, PreviousSuggestion, Reinstall]); actions!(copilot, [NextSuggestion, PreviousSuggestion, Reinstall]);
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)); staff_mode(cx, {
cx.set_global(copilot.clone()); move |cx| {
cx.update_global::<collections::CommandPaletteFilter, _, _>(|filter, _cx| {
filter.filtered_namespaces.remove(COPILOT_NAMESPACE);
filter.filtered_namespaces.remove(COPILOT_AUTH_NAMESPACE);
});
let copilot = cx.add_model({
let node_runtime = node_runtime.clone();
let http = client.http_client().clone();
move |cx| Copilot::start(http, node_runtime, cx)
});
cx.set_global(copilot.clone());
observe_namespaces(cx, copilot);
sign_in::init(cx);
}
});
not_staff_mode(cx, |cx| {
cx.update_global::<collections::CommandPaletteFilter, _, _>(|filter, _cx| {
filter.filtered_namespaces.insert(COPILOT_NAMESPACE);
filter.filtered_namespaces.insert(COPILOT_AUTH_NAMESPACE);
});
});
cx.add_global_action(|_: &SignIn, cx| { cx.add_global_action(|_: &SignIn, cx| {
let copilot = Copilot::global(cx).unwrap(); if let Some(copilot) = Copilot::global(cx) {
copilot copilot
.update(cx, |copilot, cx| copilot.sign_in(cx)) .update(cx, |copilot, cx| copilot.sign_in(cx))
.detach_and_log_err(cx); .detach_and_log_err(cx);
}
}); });
cx.add_global_action(|_: &SignOut, cx| { cx.add_global_action(|_: &SignOut, cx| {
let copilot = Copilot::global(cx).unwrap(); if let Some(copilot) = Copilot::global(cx) {
copilot copilot
.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.add_global_action(|_: &Reinstall, cx| { cx.add_global_action(|_: &Reinstall, cx| {
let copilot = Copilot::global(cx).unwrap(); if let Some(copilot) = Copilot::global(cx) {
copilot copilot
.update(cx, |copilot, cx| copilot.reinstall(cx)) .update(cx, |copilot, cx| copilot.reinstall(cx))
.detach(); .detach();
}
}); });
}
fn observe_namespaces(cx: &mut MutableAppContext, copilot: ModelHandle<Copilot>) {
cx.observe(&copilot, |handle, cx| { cx.observe(&copilot, |handle, cx| {
let status = handle.read(cx).status(); let status = handle.read(cx).status();
cx.update_global::<collections::CommandPaletteFilter, _, _>( cx.update_global::<collections::CommandPaletteFilter, _, _>(
@ -77,8 +108,6 @@ pub fn init(client: Arc<Client>, node_runtime: Arc<NodeRuntime>, cx: &mut Mutabl
); );
}) })
.detach(); .detach();
sign_in::init(cx);
} }
enum CopilotServer { enum CopilotServer {

View file

@ -20,6 +20,7 @@ fs = { path = "../fs" }
anyhow = "1.0.38" anyhow = "1.0.38"
futures = "0.3" futures = "0.3"
theme = { path = "../theme" } theme = { path = "../theme" }
staff_mode = { path = "../staff_mode" }
util = { path = "../util" } util = { path = "../util" }
json_comments = "0.2" json_comments = "0.2"
postage = { workspace = true } postage = { workspace = true }

View file

@ -177,6 +177,7 @@ pub struct EditorSettings {
pub ensure_final_newline_on_save: Option<bool>, pub ensure_final_newline_on_save: Option<bool>,
pub formatter: Option<Formatter>, pub formatter: Option<Formatter>,
pub enable_language_server: Option<bool>, pub enable_language_server: Option<bool>,
#[schemars(skip)]
pub copilot: Option<OnOff>, pub copilot: Option<OnOff>,
} }
@ -436,6 +437,7 @@ pub struct SettingsFileContent {
#[serde(default)] #[serde(default)]
pub base_keymap: Option<BaseKeymap>, pub base_keymap: Option<BaseKeymap>,
#[serde(default)] #[serde(default)]
#[schemars(skip)]
pub enable_copilot_integration: Option<bool>, pub enable_copilot_integration: Option<bool>,
} }
@ -779,6 +781,7 @@ pub fn settings_file_json_schema(
settings.option_add_null_type = false; settings.option_add_null_type = false;
}); });
let generator = SchemaGenerator::new(settings); let generator = SchemaGenerator::new(settings);
let mut root_schema = generator.into_root_schema_for::<SettingsFileContent>(); let mut root_schema = generator.into_root_schema_for::<SettingsFileContent>();
// Create a schema for a theme name. // Create a schema for a theme name.
@ -791,6 +794,7 @@ pub fn settings_file_json_schema(
// Create a schema for a 'languages overrides' object, associating editor // Create a schema for a 'languages overrides' object, associating editor
// settings with specific langauges. // settings with specific langauges.
assert!(root_schema.definitions.contains_key("EditorSettings")); assert!(root_schema.definitions.contains_key("EditorSettings"));
let languages_object_schema = SchemaObject { let languages_object_schema = SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))), instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
object: Some(Box::new(ObjectValidation { object: Some(Box::new(ObjectValidation {

View file

@ -0,0 +1,12 @@
[package]
name = "staff_mode"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
path = "src/staff_mode.rs"
[dependencies]
gpui = { path = "../gpui" }
anyhow = "1.0.38"

View file

@ -0,0 +1,42 @@
use gpui::MutableAppContext;
#[derive(Debug, Default)]
pub struct StaffMode(pub bool);
impl std::ops::Deref for StaffMode {
type Target = bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// Despite what the type system requires me to tell you, the init function will only be called a once
/// as soon as we know that the staff mode is enabled.
pub fn staff_mode<F: FnMut(&mut MutableAppContext) + 'static>(
cx: &mut MutableAppContext,
mut init: F,
) {
if **cx.default_global::<StaffMode>() {
init(cx)
} else {
let mut once = Some(());
cx.observe_global::<StaffMode, _>(move |cx| {
if **cx.global::<StaffMode>() && once.take().is_some() {
init(cx);
}
})
.detach();
}
}
/// Immediately checks and runs the init function if the staff mode is not enabled.
/// This is only included for symettry with staff_mode() above
pub fn not_staff_mode<F: FnOnce(&mut MutableAppContext) + 'static>(
cx: &mut MutableAppContext,
init: F,
) {
if !**cx.default_global::<StaffMode>() {
init(cx)
}
}

View file

@ -15,6 +15,7 @@ gpui = { path = "../gpui" }
picker = { path = "../picker" } picker = { path = "../picker" }
theme = { path = "../theme" } theme = { path = "../theme" }
settings = { path = "../settings" } settings = { path = "../settings" }
staff_mode = { path = "../staff_mode" }
workspace = { path = "../workspace" } workspace = { path = "../workspace" }
util = { path = "../util" } util = { path = "../util" }
log = { version = "0.4.16", features = ["kv_unstable_serde"] } log = { version = "0.4.16", features = ["kv_unstable_serde"] }

View file

@ -5,9 +5,9 @@ use gpui::{
}; };
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use settings::{settings_file::SettingsFile, Settings}; use settings::{settings_file::SettingsFile, Settings};
use staff_mode::StaffMode;
use std::sync::Arc; use std::sync::Arc;
use theme::{Theme, ThemeMeta, ThemeRegistry}; use theme::{Theme, ThemeMeta, ThemeRegistry};
use util::StaffMode;
use workspace::{AppState, Workspace}; use workspace::{AppState, Workspace};
pub struct ThemeSelector { pub struct ThemeSelector {

View file

@ -17,17 +17,6 @@ pub use backtrace::Backtrace;
use futures::Future; use futures::Future;
use rand::{seq::SliceRandom, Rng}; use rand::{seq::SliceRandom, Rng};
#[derive(Debug, Default)]
pub struct StaffMode(pub bool);
impl std::ops::Deref for StaffMode {
type Target = bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[macro_export] #[macro_export]
macro_rules! debug_panic { macro_rules! debug_panic {
( $($fmt_arg:tt)* ) => { ( $($fmt_arg:tt)* ) => {

View file

@ -55,6 +55,7 @@ project_symbols = { path = "../project_symbols" }
recent_projects = { path = "../recent_projects" } recent_projects = { path = "../recent_projects" }
rpc = { path = "../rpc" } rpc = { path = "../rpc" }
settings = { path = "../settings" } settings = { path = "../settings" }
staff_mode = { path = "../staff_mode" }
sum_tree = { path = "../sum_tree" } sum_tree = { path = "../sum_tree" }
text = { path = "../text" } text = { path = "../text" }
terminal_view = { path = "../terminal_view" } terminal_view = { path = "../terminal_view" }

View file

@ -8,6 +8,7 @@ use node_runtime::NodeRuntime;
use serde_json::json; use serde_json::json;
use settings::{keymap_file_json_schema, settings_file_json_schema}; use settings::{keymap_file_json_schema, settings_file_json_schema};
use smol::fs; use smol::fs;
use staff_mode::StaffMode;
use std::{ use std::{
any::Any, any::Any,
ffi::OsString, ffi::OsString,
@ -16,8 +17,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use theme::ThemeRegistry; use theme::ThemeRegistry;
use util::{fs::remove_matching, http::HttpClient}; use util::{fs::remove_matching, http::HttpClient, paths, ResultExt};
use util::{paths, ResultExt, StaffMode};
const SERVER_PATH: &'static str = const SERVER_PATH: &'static str =
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver"; "node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";

View file

@ -38,9 +38,9 @@ use welcome::{show_welcome_experience, FIRST_OPEN};
use fs::RealFs; use fs::RealFs;
use settings::watched_json::WatchedJsonFile; use settings::watched_json::WatchedJsonFile;
use theme::ThemeRegistry;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
use util::StaffMode; use staff_mode::StaffMode;
use theme::ThemeRegistry;
use util::{channel::RELEASE_CHANNEL, paths, ResultExt, TryFutureExt}; use util::{channel::RELEASE_CHANNEL, paths, ResultExt, TryFutureExt};
use workspace::{ use workspace::{
self, dock::FocusDock, item::ItemHandle, notifications::NotifyResultExt, AppState, NewFile, self, dock::FocusDock, item::ItemHandle, notifications::NotifyResultExt, AppState, NewFile,