diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rs index 8bea11b1f72e155cefdf0a05f6da98a6002a5097..c768442611720f63381d8ef9538aebad6645c8b7 100644 --- a/crates/copilot/src/copilot.rs +++ b/crates/copilot/src/copilot.rs @@ -18,29 +18,22 @@ use util::{ actions!(copilot, [SignIn, SignOut]); pub fn init(client: Arc, 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.spawn(|mut cx| async move { - task.await?; - cx.update(|cx| { - cx.add_global_action(|_: &SignIn, cx: &mut MutableAppContext| { - if let Some(copilot) = Copilot::global(cx) { - copilot - .update(cx, |copilot, cx| copilot.sign_in(cx)) - .detach_and_log_err(cx); - } - }); - cx.add_global_action(|_: &SignOut, cx: &mut MutableAppContext| { - if let Some(copilot) = Copilot::global(cx) { - copilot - .update(cx, |copilot, cx| copilot.sign_out(cx)) - .detach_and_log_err(cx); - } - }); - }); - anyhow::Ok(()) - }) - .detach_and_log_err(cx); + cx.add_global_action(|_: &SignIn, cx: &mut MutableAppContext| { + if let Some(copilot) = Copilot::global(cx) { + copilot + .update(cx, |copilot, cx| copilot.sign_in(cx)) + .detach_and_log_err(cx); + } + }); + cx.add_global_action(|_: &SignOut, cx: &mut MutableAppContext| { + if let Some(copilot) = Copilot::global(cx) { + copilot + .update(cx, |copilot, cx| copilot.sign_out(cx)) + .detach_and_log_err(cx); + } + }); } enum CopilotServer { @@ -83,55 +76,42 @@ impl Copilot { } } - fn start( - http: Arc, - cx: &mut MutableAppContext, - ) -> (ModelHandle, Task>) { - 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 server_path = get_lsp_binary(http).await?; - let server = LanguageServer::new( - 0, - &server_path, - &["--stdio"], - Path::new("/"), - cx.clone(), - )?; - let server = server.initialize(Default::default()).await?; - let status = server - .request::(request::CheckStatusParams { - local_checks_only: false, - }) - .await?; - anyhow::Ok((server, status)) - }; + fn start(http: Arc, cx: &mut ModelContext) -> Self { + cx.spawn(|this, mut cx| async move { + let start_language_server = async { + let server_path = get_lsp_binary(http).await?; + let server = + LanguageServer::new(0, &server_path, &["--stdio"], Path::new("/"), cx.clone())?; + let server = server.initialize(Default::default()).await?; + let status = server + .request::(request::CheckStatusParams { + local_checks_only: false, + }) + .await?; + anyhow::Ok((server, status)) + }; - let server = start_language_server.await; - this.update(&mut cx, |this, cx| { - cx.notify(); - match server { - Ok((server, status)) => { - this.server = CopilotServer::Started { - server, - status: SignInStatus::SignedOut, - }; - this.update_sign_in_status(status, cx); - Ok(()) - } - Err(error) => { - this.server = CopilotServer::Error(error.to_string()); - Err(error) - } + let server = start_language_server.await; + this.update(&mut cx, |this, cx| { + cx.notify(); + match server { + Ok((server, status)) => { + this.server = CopilotServer::Started { + server, + status: SignInStatus::SignedOut, + }; + this.update_sign_in_status(status, cx); } - }) - } - }); - (this, task) + Err(error) => { + this.server = CopilotServer::Error(error.to_string()); + } + } + }) + }) + .detach(); + Self { + server: CopilotServer::Downloading, + } } fn sign_in(&mut self, cx: &mut ModelContext) -> Task> {