This commit is contained in:
Antonio Scandurra 2023-03-23 14:20:39 +01:00 committed by Mikayla Maki
parent 59d9277a74
commit 8ba9e63ab8

View file

@ -18,11 +18,8 @@ use util::{
actions!(copilot, [SignIn, SignOut]); actions!(copilot, [SignIn, SignOut]);
pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) { pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) {
let (copilot, task) = Copilot::start(client.http_client(), cx); let copilot = cx.add_model(|cx| Copilot::start(client.http_client(), cx));
cx.set_global(copilot); cx.set_global(copilot);
cx.spawn(|mut cx| async move {
task.await?;
cx.update(|cx| {
cx.add_global_action(|_: &SignIn, cx: &mut MutableAppContext| { cx.add_global_action(|_: &SignIn, cx: &mut MutableAppContext| {
if let Some(copilot) = Copilot::global(cx) { if let Some(copilot) = Copilot::global(cx) {
copilot copilot
@ -37,10 +34,6 @@ pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) {
.detach_and_log_err(cx); .detach_and_log_err(cx);
} }
}); });
});
anyhow::Ok(())
})
.detach_and_log_err(cx);
} }
enum CopilotServer { enum CopilotServer {
@ -83,25 +76,12 @@ impl Copilot {
} }
} }
fn start( fn start(http: Arc<dyn HttpClient>, cx: &mut ModelContext<Self>) -> Self {
http: Arc<dyn HttpClient>, cx.spawn(|this, mut cx| async move {
cx: &mut MutableAppContext,
) -> (ModelHandle<Self>, Task<Result<()>>) {
let this = cx.add_model(|_| Self {
server: CopilotServer::Downloading,
});
let task = cx.spawn({
let this = this.clone();
|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_lsp_binary(http).await?;
let server = LanguageServer::new( let server =
0, LanguageServer::new(0, &server_path, &["--stdio"], Path::new("/"), cx.clone())?;
&server_path,
&["--stdio"],
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 {
@ -121,17 +101,17 @@ impl Copilot {
status: SignInStatus::SignedOut, status: SignInStatus::SignedOut,
}; };
this.update_sign_in_status(status, cx); this.update_sign_in_status(status, cx);
Ok(())
} }
Err(error) => { Err(error) => {
this.server = CopilotServer::Error(error.to_string()); this.server = CopilotServer::Error(error.to_string());
Err(error)
} }
} }
}) })
})
.detach();
Self {
server: CopilotServer::Downloading,
} }
});
(this, task)
} }
fn sign_in(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { fn sign_in(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {