Skip to content

Commit

Permalink
language_model: Remove dependency on inline_completion_button (#20930)
Browse files Browse the repository at this point in the history
This PR removes a dependency on the `inline_completion_button` crate
from the `language_model` crate.

We were taking on this dependency solely to call `initiate_sign_in`,
which can easily be moved to the `copilot` crate.

This allows `language_model` to move up in the crate dependency graph.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Nov 20, 2024
1 parent e31f444 commit e076f55
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 83 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/copilot/src/copilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use std::{
};
use util::{fs::remove_matching, maybe, ResultExt};

pub use copilot_completion_provider::CopilotCompletionProvider;
pub use sign_in::CopilotCodeVerification;
pub use crate::copilot_completion_provider::CopilotCompletionProvider;
pub use crate::sign_in::{initiate_sign_in, CopilotCodeVerification};

actions!(
copilot,
Expand Down
71 changes: 70 additions & 1 deletion crates/copilot/src/sign_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,79 @@ use gpui::{
Styled, Subscription, ViewContext,
};
use ui::{prelude::*, Button, Label, Vector, VectorName};
use workspace::ModalView;
use util::ResultExt as _;
use workspace::notifications::NotificationId;
use workspace::{ModalView, Toast, Workspace};

const COPILOT_SIGN_UP_URL: &str = "https://github.com/features/copilot";

struct CopilotStartingToast;

pub fn initiate_sign_in(cx: &mut WindowContext) {
let Some(copilot) = Copilot::global(cx) else {
return;
};
let status = copilot.read(cx).status();
let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {
return;
};
match status {
Status::Starting { task } => {
let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {

This comment has been minimized.

Copy link
@gubatron

gubatron Nov 20, 2024

why the redundant check here? just did this before the match

return;
};

let Ok(workspace) = workspace.update(cx, |workspace, cx| {
workspace.show_toast(
Toast::new(
NotificationId::unique::<CopilotStartingToast>(),
"Copilot is starting...",
),
cx,
);
workspace.weak_handle()
}) else {
return;
};

cx.spawn(|mut cx| async move {
task.await;
if let Some(copilot) = cx.update(|cx| Copilot::global(cx)).ok().flatten() {
workspace
.update(&mut cx, |workspace, cx| match copilot.read(cx).status() {
Status::Authorized => workspace.show_toast(
Toast::new(
NotificationId::unique::<CopilotStartingToast>(),
"Copilot has started!",
),
cx,
),
_ => {
workspace.dismiss_toast(
&NotificationId::unique::<CopilotStartingToast>(),
cx,
);
copilot
.update(cx, |copilot, cx| copilot.sign_in(cx))
.detach_and_log_err(cx);
}
})
.log_err();
}
})
.detach();
}
_ => {
copilot.update(cx, |this, cx| this.sign_in(cx)).detach();
workspace
.update(cx, |this, cx| {
this.toggle_modal(cx, |cx| CopilotCodeVerification::new(&copilot, cx));
})
.ok();
}
}
}

pub struct CopilotCodeVerification {
status: Status,
connect_clicked: bool,
Expand Down
1 change: 0 additions & 1 deletion crates/inline_completion_button/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ paths.workspace = true
settings.workspace = true
supermaven.workspace = true
ui.workspace = true
util.workspace = true
workspace.workspace = true
zed_actions.workspace = true

Expand Down
72 changes: 2 additions & 70 deletions crates/inline_completion_button/src/inline_completion_button.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use copilot::{Copilot, CopilotCodeVerification, Status};
use copilot::{Copilot, Status};
use editor::{scroll::Autoscroll, Editor};
use fs::Fs;
use gpui::{
Expand All @@ -15,7 +15,6 @@ use language::{
use settings::{update_settings_file, Settings, SettingsStore};
use std::{path::Path, sync::Arc};
use supermaven::{AccountStatus, Supermaven};
use util::ResultExt;
use workspace::{
create_and_open_local_file,
item::ItemHandle,
Expand All @@ -29,8 +28,6 @@ use zed_actions::OpenBrowser;

const COPILOT_SETTINGS_URL: &str = "https://github.com/settings/copilot";

struct CopilotStartingToast;

struct CopilotErrorToast;

pub struct InlineCompletionButton {
Expand Down Expand Up @@ -221,7 +218,7 @@ impl InlineCompletionButton {
pub fn build_copilot_start_menu(&mut self, cx: &mut ViewContext<Self>) -> View<ContextMenu> {
let fs = self.fs.clone();
ContextMenu::build(cx, |menu, _| {
menu.entry("Sign In", None, initiate_sign_in)
menu.entry("Sign In", None, copilot::initiate_sign_in)
.entry("Disable Copilot", None, {
let fs = fs.clone();
move |cx| hide_copilot(fs.clone(), cx)
Expand Down Expand Up @@ -484,68 +481,3 @@ fn hide_copilot(fs: Arc<dyn Fs>, cx: &mut AppContext) {
.inline_completion_provider = Some(InlineCompletionProvider::None);
});
}

pub fn initiate_sign_in(cx: &mut WindowContext) {
let Some(copilot) = Copilot::global(cx) else {
return;
};
let status = copilot.read(cx).status();
let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {
return;
};
match status {
Status::Starting { task } => {
let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {
return;
};

let Ok(workspace) = workspace.update(cx, |workspace, cx| {
workspace.show_toast(
Toast::new(
NotificationId::unique::<CopilotStartingToast>(),
"Copilot is starting...",
),
cx,
);
workspace.weak_handle()
}) else {
return;
};

cx.spawn(|mut cx| async move {
task.await;
if let Some(copilot) = cx.update(|cx| Copilot::global(cx)).ok().flatten() {
workspace
.update(&mut cx, |workspace, cx| match copilot.read(cx).status() {
Status::Authorized => workspace.show_toast(
Toast::new(
NotificationId::unique::<CopilotStartingToast>(),
"Copilot has started!",
),
cx,
),
_ => {
workspace.dismiss_toast(
&NotificationId::unique::<CopilotStartingToast>(),
cx,
);
copilot
.update(cx, |copilot, cx| copilot.sign_in(cx))
.detach_and_log_err(cx);
}
})
.log_err();
}
})
.detach();
}
_ => {
copilot.update(cx, |this, cx| this.sign_in(cx)).detach();
workspace
.update(cx, |this, cx| {
this.toggle_modal(cx, |cx| CopilotCodeVerification::new(&copilot, cx));
})
.ok();
}
}
}
1 change: 0 additions & 1 deletion crates/language_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ futures.workspace = true
google_ai = { workspace = true, features = ["schemars"] }
gpui.workspace = true
http_client.workspace = true
inline_completion_button.workspace = true
log.workspace = true
menu.workspace = true
ollama = { workspace = true, features = ["schemars"] }
Expand Down
4 changes: 1 addition & 3 deletions crates/language_model/src/provider/copilot_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,7 @@ impl Render for ConfigurationView {
.icon_size(IconSize::Medium)
.style(ui::ButtonStyle::Filled)
.full_width()
.on_click(|_, cx| {
inline_completion_button::initiate_sign_in(cx)
}),
.on_click(|_, cx| copilot::initiate_sign_in(cx)),
)
.child(
div().flex().w_full().items_center().child(
Expand Down
2 changes: 1 addition & 1 deletion crates/welcome/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ test-support = []
[dependencies]
anyhow.workspace = true
client.workspace = true
copilot.workspace = true
db.workspace = true
fuzzy.workspace = true
gpui.workspace = true
inline_completion_button.workspace = true
install_cli.workspace = true
picker.workspace = true
project.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/welcome/src/welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Render for WelcomePage {
this.telemetry.report_app_event(
"welcome page: sign in to copilot".to_string(),
);
inline_completion_button::initiate_sign_in(cx);
copilot::initiate_sign_in(cx);
}),
),
)
Expand Down

0 comments on commit e076f55

Please sign in to comment.