1pub mod request;
2mod sign_in;
3
4use anyhow::{anyhow, Context, Result};
5use async_compression::futures::bufread::GzipDecoder;
6use async_tar::Archive;
7use collections::{HashMap, HashSet};
8use futures::{channel::oneshot, future::Shared, Future, FutureExt, TryFutureExt};
9use gpui::{
10 actions, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle,
11};
12use language::{
13 language_settings::{all_language_settings, language_settings},
14 point_from_lsp, point_to_lsp, Anchor, Bias, Buffer, BufferSnapshot, Language,
15 LanguageServerName, PointUtf16, ToPointUtf16,
16};
17use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
18use node_runtime::NodeRuntime;
19use request::StatusNotification;
20use settings::SettingsStore;
21use smol::{fs, io::BufReader, stream::StreamExt};
22use std::{
23 ffi::OsString,
24 mem,
25 ops::Range,
26 path::{Path, PathBuf},
27 pin::Pin,
28 sync::Arc,
29};
30use util::{
31 fs::remove_matching, github::latest_github_release, http::HttpClient, paths, ResultExt,
32};
33
34const COPILOT_AUTH_NAMESPACE: &'static str = "copilot_auth";
35actions!(copilot_auth, [SignIn, SignOut]);
36
37const COPILOT_NAMESPACE: &'static str = "copilot";
38actions!(
39 copilot,
40 [Suggest, NextSuggestion, PreviousSuggestion, Reinstall]
41);
42
43pub fn init(
44 new_server_id: LanguageServerId,
45 http: Arc<dyn HttpClient>,
46 node_runtime: Arc<dyn NodeRuntime>,
47 cx: &mut AppContext,
48) {
49 let copilot = cx.add_model({
50 let node_runtime = node_runtime.clone();
51 move |cx| Copilot::start(new_server_id, http, node_runtime, cx)
52 });
53 cx.set_global(copilot.clone());
54
55 cx.observe(&copilot, |handle, cx| {
56 let status = handle.read(cx).status();
57 cx.update_default_global::<collections::CommandPaletteFilter, _, _>(move |filter, _cx| {
58 match status {
59 Status::Disabled => {
60 filter.filtered_namespaces.insert(COPILOT_NAMESPACE);
61 filter.filtered_namespaces.insert(COPILOT_AUTH_NAMESPACE);
62 }
63 Status::Authorized => {
64 filter.filtered_namespaces.remove(COPILOT_NAMESPACE);
65 filter.filtered_namespaces.remove(COPILOT_AUTH_NAMESPACE);
66 }
67 _ => {
68 filter.filtered_namespaces.insert(COPILOT_NAMESPACE);
69 filter.filtered_namespaces.remove(COPILOT_AUTH_NAMESPACE);
70 }
71 }
72 });
73 })
74 .detach();
75
76 sign_in::init(cx);
77 cx.add_global_action(|_: &SignIn, cx| {
78 if let Some(copilot) = Copilot::global(cx) {
79 copilot
80 .update(cx, |copilot, cx| copilot.sign_in(cx))
81 .detach_and_log_err(cx);
82 }
83 });
84 cx.add_global_action(|_: &SignOut, cx| {
85 if let Some(copilot) = Copilot::global(cx) {
86 copilot
87 .update(cx, |copilot, cx| copilot.sign_out(cx))
88 .detach_and_log_err(cx);
89 }
90 });
91
92 cx.add_global_action(|_: &Reinstall, cx| {
93 if let Some(copilot) = Copilot::global(cx) {
94 copilot
95 .update(cx, |copilot, cx| copilot.reinstall(cx))
96 .detach();
97 }
98 });
99}
100
101enum CopilotServer {
102 Disabled,
103 Starting { task: Shared<Task<()>> },
104 Error(Arc<str>),
105 Running(RunningCopilotServer),
106}
107
108impl CopilotServer {
109 fn as_authenticated(&mut self) -> Result<&mut RunningCopilotServer> {
110 let server = self.as_running()?;
111 if matches!(server.sign_in_status, SignInStatus::Authorized { .. }) {
112 Ok(server)
113 } else {
114 Err(anyhow!("must sign in before using copilot"))
115 }
116 }
117
118 fn as_running(&mut self) -> Result<&mut RunningCopilotServer> {
119 match self {
120 CopilotServer::Starting { .. } => Err(anyhow!("copilot is still starting")),
121 CopilotServer::Disabled => Err(anyhow!("copilot is disabled")),
122 CopilotServer::Error(error) => Err(anyhow!(
123 "copilot was not started because of an error: {}",
124 error
125 )),
126 CopilotServer::Running(server) => Ok(server),
127 }
128 }
129}
130
131struct RunningCopilotServer {
132 name: LanguageServerName,
133 lsp: Arc<LanguageServer>,
134 sign_in_status: SignInStatus,
135 registered_buffers: HashMap<usize, RegisteredBuffer>,
136}
137
138#[derive(Clone, Debug)]
139enum SignInStatus {
140 Authorized,
141 Unauthorized,
142 SigningIn {
143 prompt: Option<request::PromptUserDeviceFlow>,
144 task: Shared<Task<Result<(), Arc<anyhow::Error>>>>,
145 },
146 SignedOut,
147}
148
149#[derive(Debug, Clone)]
150pub enum Status {
151 Starting {
152 task: Shared<Task<()>>,
153 },
154 Error(Arc<str>),
155 Disabled,
156 SignedOut,
157 SigningIn {
158 prompt: Option<request::PromptUserDeviceFlow>,
159 },
160 Unauthorized,
161 Authorized,
162}
163
164impl Status {
165 pub fn is_authorized(&self) -> bool {
166 matches!(self, Status::Authorized)
167 }
168}
169
170struct RegisteredBuffer {
171 uri: lsp::Url,
172 language_id: String,
173 snapshot: BufferSnapshot,
174 snapshot_version: i32,
175 _subscriptions: [gpui::Subscription; 2],
176 pending_buffer_change: Task<Option<()>>,
177}
178
179impl RegisteredBuffer {
180 fn report_changes(
181 &mut self,
182 buffer: &ModelHandle<Buffer>,
183 cx: &mut ModelContext<Copilot>,
184 ) -> oneshot::Receiver<(i32, BufferSnapshot)> {
185 let (done_tx, done_rx) = oneshot::channel();
186
187 if buffer.read(cx).version() == self.snapshot.version {
188 let _ = done_tx.send((self.snapshot_version, self.snapshot.clone()));
189 } else {
190 let buffer = buffer.downgrade();
191 let id = buffer.id();
192 let prev_pending_change =
193 mem::replace(&mut self.pending_buffer_change, Task::ready(None));
194 self.pending_buffer_change = cx.spawn_weak(|copilot, mut cx| async move {
195 prev_pending_change.await;
196
197 let old_version = copilot.upgrade(&cx)?.update(&mut cx, |copilot, _| {
198 let server = copilot.server.as_authenticated().log_err()?;
199 let buffer = server.registered_buffers.get_mut(&id)?;
200 Some(buffer.snapshot.version.clone())
201 })?;
202 let new_snapshot = buffer
203 .upgrade(&cx)?
204 .read_with(&cx, |buffer, _| buffer.snapshot());
205
206 let content_changes = cx
207 .background()
208 .spawn({
209 let new_snapshot = new_snapshot.clone();
210 async move {
211 new_snapshot
212 .edits_since::<(PointUtf16, usize)>(&old_version)
213 .map(|edit| {
214 let edit_start = edit.new.start.0;
215 let edit_end = edit_start + (edit.old.end.0 - edit.old.start.0);
216 let new_text = new_snapshot
217 .text_for_range(edit.new.start.1..edit.new.end.1)
218 .collect();
219 lsp::TextDocumentContentChangeEvent {
220 range: Some(lsp::Range::new(
221 point_to_lsp(edit_start),
222 point_to_lsp(edit_end),
223 )),
224 range_length: None,
225 text: new_text,
226 }
227 })
228 .collect::<Vec<_>>()
229 }
230 })
231 .await;
232
233 copilot.upgrade(&cx)?.update(&mut cx, |copilot, _| {
234 let server = copilot.server.as_authenticated().log_err()?;
235 let buffer = server.registered_buffers.get_mut(&id)?;
236 if !content_changes.is_empty() {
237 buffer.snapshot_version += 1;
238 buffer.snapshot = new_snapshot;
239 server
240 .lsp
241 .notify::<lsp::notification::DidChangeTextDocument>(
242 lsp::DidChangeTextDocumentParams {
243 text_document: lsp::VersionedTextDocumentIdentifier::new(
244 buffer.uri.clone(),
245 buffer.snapshot_version,
246 ),
247 content_changes,
248 },
249 )
250 .log_err();
251 }
252 let _ = done_tx.send((buffer.snapshot_version, buffer.snapshot.clone()));
253 Some(())
254 })?;
255
256 Some(())
257 });
258 }
259
260 done_rx
261 }
262}
263
264#[derive(Debug)]
265pub struct Completion {
266 pub uuid: String,
267 pub range: Range<Anchor>,
268 pub text: String,
269}
270
271pub struct Copilot {
272 http: Arc<dyn HttpClient>,
273 node_runtime: Arc<dyn NodeRuntime>,
274 server: CopilotServer,
275 buffers: HashSet<WeakModelHandle<Buffer>>,
276 server_id: LanguageServerId,
277}
278
279pub enum Event {
280 CopilotLanguageServerStarted,
281}
282
283impl Entity for Copilot {
284 type Event = Event;
285
286 fn app_will_quit(
287 &mut self,
288 _: &mut AppContext,
289 ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
290 match mem::replace(&mut self.server, CopilotServer::Disabled) {
291 CopilotServer::Running(server) => Some(Box::pin(async move {
292 if let Some(shutdown) = server.lsp.shutdown() {
293 shutdown.await;
294 }
295 })),
296 _ => None,
297 }
298 }
299}
300
301impl Copilot {
302 pub fn global(cx: &AppContext) -> Option<ModelHandle<Self>> {
303 if cx.has_global::<ModelHandle<Self>>() {
304 Some(cx.global::<ModelHandle<Self>>().clone())
305 } else {
306 None
307 }
308 }
309
310 fn start(
311 new_server_id: LanguageServerId,
312 http: Arc<dyn HttpClient>,
313 node_runtime: Arc<dyn NodeRuntime>,
314 cx: &mut ModelContext<Self>,
315 ) -> Self {
316 let mut this = Self {
317 server_id: new_server_id,
318 http,
319 node_runtime,
320 server: CopilotServer::Disabled,
321 buffers: Default::default(),
322 };
323 this.enable_or_disable_copilot(cx);
324 cx.observe_global::<SettingsStore, _>(move |this, cx| this.enable_or_disable_copilot(cx))
325 .detach();
326 this
327 }
328
329 fn enable_or_disable_copilot(&mut self, cx: &mut ModelContext<Copilot>) {
330 let server_id = self.server_id;
331 let http = self.http.clone();
332 let node_runtime = self.node_runtime.clone();
333 if all_language_settings(None, cx).copilot_enabled(None, None) {
334 if matches!(self.server, CopilotServer::Disabled) {
335 let start_task = cx
336 .spawn({
337 move |this, cx| {
338 Self::start_language_server(server_id, http, node_runtime, this, cx)
339 }
340 })
341 .shared();
342 self.server = CopilotServer::Starting { task: start_task };
343 cx.notify();
344 }
345 } else {
346 self.server = CopilotServer::Disabled;
347 cx.notify();
348 }
349 }
350
351 #[cfg(any(test, feature = "test-support"))]
352 pub fn fake(cx: &mut gpui::TestAppContext) -> (ModelHandle<Self>, lsp::FakeLanguageServer) {
353 use node_runtime::FakeNodeRuntime;
354
355 let (server, fake_server) =
356 LanguageServer::fake("copilot".into(), Default::default(), cx.to_async());
357 let http = util::http::FakeHttpClient::create(|_| async { unreachable!() });
358 let node_runtime = FakeNodeRuntime::new();
359 let this = cx.add_model(|_| Self {
360 server_id: LanguageServerId(0),
361 http: http.clone(),
362 node_runtime,
363 server: CopilotServer::Running(RunningCopilotServer {
364 name: LanguageServerName(Arc::from("copilot")),
365 lsp: Arc::new(server),
366 sign_in_status: SignInStatus::Authorized,
367 registered_buffers: Default::default(),
368 }),
369 buffers: Default::default(),
370 });
371 (this, fake_server)
372 }
373
374 fn start_language_server(
375 new_server_id: LanguageServerId,
376 http: Arc<dyn HttpClient>,
377 node_runtime: Arc<dyn NodeRuntime>,
378 this: ModelHandle<Self>,
379 mut cx: AsyncAppContext,
380 ) -> impl Future<Output = ()> {
381 async move {
382 let start_language_server = async {
383 let server_path = get_copilot_lsp(http).await?;
384 let node_path = node_runtime.binary_path().await?;
385 let arguments: Vec<OsString> = vec![server_path.into(), "--stdio".into()];
386 let binary = LanguageServerBinary {
387 path: node_path,
388 arguments,
389 };
390 let server =
391 LanguageServer::new(new_server_id, binary, Path::new("/"), None, cx.clone())?;
392
393 server
394 .on_notification::<StatusNotification, _>(
395 |_, _| { /* Silence the notification */ },
396 )
397 .detach();
398
399 let server = server.initialize(Default::default()).await?;
400
401 let status = server
402 .request::<request::CheckStatus>(request::CheckStatusParams {
403 local_checks_only: false,
404 })
405 .await?;
406
407 server
408 .request::<request::SetEditorInfo>(request::SetEditorInfoParams {
409 editor_info: request::EditorInfo {
410 name: "zed".into(),
411 version: env!("CARGO_PKG_VERSION").into(),
412 },
413 editor_plugin_info: request::EditorPluginInfo {
414 name: "zed-copilot".into(),
415 version: "0.0.1".into(),
416 },
417 })
418 .await?;
419
420 anyhow::Ok((server, status))
421 };
422
423 let server = start_language_server.await;
424 this.update(&mut cx, |this, cx| {
425 cx.notify();
426 match server {
427 Ok((server, status)) => {
428 this.server = CopilotServer::Running(RunningCopilotServer {
429 name: LanguageServerName(Arc::from("copilot")),
430 lsp: server,
431 sign_in_status: SignInStatus::SignedOut,
432 registered_buffers: Default::default(),
433 });
434 cx.emit(Event::CopilotLanguageServerStarted);
435 this.update_sign_in_status(status, cx);
436 }
437 Err(error) => {
438 this.server = CopilotServer::Error(error.to_string().into());
439 cx.notify()
440 }
441 }
442 })
443 }
444 }
445
446 pub fn sign_in(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
447 if let CopilotServer::Running(server) = &mut self.server {
448 let task = match &server.sign_in_status {
449 SignInStatus::Authorized { .. } => Task::ready(Ok(())).shared(),
450 SignInStatus::SigningIn { task, .. } => {
451 cx.notify();
452 task.clone()
453 }
454 SignInStatus::SignedOut | SignInStatus::Unauthorized { .. } => {
455 let lsp = server.lsp.clone();
456 let task = cx
457 .spawn(|this, mut cx| async move {
458 let sign_in = async {
459 let sign_in = lsp
460 .request::<request::SignInInitiate>(
461 request::SignInInitiateParams {},
462 )
463 .await?;
464 match sign_in {
465 request::SignInInitiateResult::AlreadySignedIn { user } => {
466 Ok(request::SignInStatus::Ok { user })
467 }
468 request::SignInInitiateResult::PromptUserDeviceFlow(flow) => {
469 this.update(&mut cx, |this, cx| {
470 if let CopilotServer::Running(RunningCopilotServer {
471 sign_in_status: status,
472 ..
473 }) = &mut this.server
474 {
475 if let SignInStatus::SigningIn {
476 prompt: prompt_flow,
477 ..
478 } = status
479 {
480 *prompt_flow = Some(flow.clone());
481 cx.notify();
482 }
483 }
484 });
485 let response = lsp
486 .request::<request::SignInConfirm>(
487 request::SignInConfirmParams {
488 user_code: flow.user_code,
489 },
490 )
491 .await?;
492 Ok(response)
493 }
494 }
495 };
496
497 let sign_in = sign_in.await;
498 this.update(&mut cx, |this, cx| match sign_in {
499 Ok(status) => {
500 this.update_sign_in_status(status, cx);
501 Ok(())
502 }
503 Err(error) => {
504 this.update_sign_in_status(
505 request::SignInStatus::NotSignedIn,
506 cx,
507 );
508 Err(Arc::new(error))
509 }
510 })
511 })
512 .shared();
513 server.sign_in_status = SignInStatus::SigningIn {
514 prompt: None,
515 task: task.clone(),
516 };
517 cx.notify();
518 task
519 }
520 };
521
522 cx.foreground()
523 .spawn(task.map_err(|err| anyhow!("{:?}", err)))
524 } else {
525 // If we're downloading, wait until download is finished
526 // If we're in a stuck state, display to the user
527 Task::ready(Err(anyhow!("copilot hasn't started yet")))
528 }
529 }
530
531 fn sign_out(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
532 self.update_sign_in_status(request::SignInStatus::NotSignedIn, cx);
533 if let CopilotServer::Running(RunningCopilotServer { lsp: server, .. }) = &self.server {
534 let server = server.clone();
535 cx.background().spawn(async move {
536 server
537 .request::<request::SignOut>(request::SignOutParams {})
538 .await?;
539 anyhow::Ok(())
540 })
541 } else {
542 Task::ready(Err(anyhow!("copilot hasn't started yet")))
543 }
544 }
545
546 pub fn reinstall(&mut self, cx: &mut ModelContext<Self>) -> Task<()> {
547 let start_task = cx
548 .spawn({
549 let http = self.http.clone();
550 let node_runtime = self.node_runtime.clone();
551 let server_id = self.server_id;
552 move |this, cx| async move {
553 clear_copilot_dir().await;
554 Self::start_language_server(server_id, http, node_runtime, this, cx).await
555 }
556 })
557 .shared();
558
559 self.server = CopilotServer::Starting {
560 task: start_task.clone(),
561 };
562
563 cx.notify();
564
565 cx.foreground().spawn(start_task)
566 }
567
568 pub fn language_server(&self) -> Option<(&LanguageServerName, &Arc<LanguageServer>)> {
569 if let CopilotServer::Running(server) = &self.server {
570 Some((&server.name, &server.lsp))
571 } else {
572 None
573 }
574 }
575
576 pub fn register_buffer(&mut self, buffer: &ModelHandle<Buffer>, cx: &mut ModelContext<Self>) {
577 let weak_buffer = buffer.downgrade();
578 self.buffers.insert(weak_buffer.clone());
579
580 if let CopilotServer::Running(RunningCopilotServer {
581 lsp: server,
582 sign_in_status: status,
583 registered_buffers,
584 ..
585 }) = &mut self.server
586 {
587 if !matches!(status, SignInStatus::Authorized { .. }) {
588 return;
589 }
590
591 registered_buffers.entry(buffer.id()).or_insert_with(|| {
592 let uri: lsp::Url = uri_for_buffer(buffer, cx);
593 let language_id = id_for_language(buffer.read(cx).language());
594 let snapshot = buffer.read(cx).snapshot();
595 server
596 .notify::<lsp::notification::DidOpenTextDocument>(
597 lsp::DidOpenTextDocumentParams {
598 text_document: lsp::TextDocumentItem {
599 uri: uri.clone(),
600 language_id: language_id.clone(),
601 version: 0,
602 text: snapshot.text(),
603 },
604 },
605 )
606 .log_err();
607
608 RegisteredBuffer {
609 uri,
610 language_id,
611 snapshot,
612 snapshot_version: 0,
613 pending_buffer_change: Task::ready(Some(())),
614 _subscriptions: [
615 cx.subscribe(buffer, |this, buffer, event, cx| {
616 this.handle_buffer_event(buffer, event, cx).log_err();
617 }),
618 cx.observe_release(buffer, move |this, _buffer, _cx| {
619 this.buffers.remove(&weak_buffer);
620 this.unregister_buffer(&weak_buffer);
621 }),
622 ],
623 }
624 });
625 }
626 }
627
628 fn handle_buffer_event(
629 &mut self,
630 buffer: ModelHandle<Buffer>,
631 event: &language::Event,
632 cx: &mut ModelContext<Self>,
633 ) -> Result<()> {
634 if let Ok(server) = self.server.as_running() {
635 if let Some(registered_buffer) = server.registered_buffers.get_mut(&buffer.id()) {
636 match event {
637 language::Event::Edited => {
638 let _ = registered_buffer.report_changes(&buffer, cx);
639 }
640 language::Event::Saved => {
641 server
642 .lsp
643 .notify::<lsp::notification::DidSaveTextDocument>(
644 lsp::DidSaveTextDocumentParams {
645 text_document: lsp::TextDocumentIdentifier::new(
646 registered_buffer.uri.clone(),
647 ),
648 text: None,
649 },
650 )?;
651 }
652 language::Event::FileHandleChanged | language::Event::LanguageChanged => {
653 let new_language_id = id_for_language(buffer.read(cx).language());
654 let new_uri = uri_for_buffer(&buffer, cx);
655 if new_uri != registered_buffer.uri
656 || new_language_id != registered_buffer.language_id
657 {
658 let old_uri = mem::replace(&mut registered_buffer.uri, new_uri);
659 registered_buffer.language_id = new_language_id;
660 server
661 .lsp
662 .notify::<lsp::notification::DidCloseTextDocument>(
663 lsp::DidCloseTextDocumentParams {
664 text_document: lsp::TextDocumentIdentifier::new(old_uri),
665 },
666 )?;
667 server
668 .lsp
669 .notify::<lsp::notification::DidOpenTextDocument>(
670 lsp::DidOpenTextDocumentParams {
671 text_document: lsp::TextDocumentItem::new(
672 registered_buffer.uri.clone(),
673 registered_buffer.language_id.clone(),
674 registered_buffer.snapshot_version,
675 registered_buffer.snapshot.text(),
676 ),
677 },
678 )?;
679 }
680 }
681 _ => {}
682 }
683 }
684 }
685
686 Ok(())
687 }
688
689 fn unregister_buffer(&mut self, buffer: &WeakModelHandle<Buffer>) {
690 if let Ok(server) = self.server.as_running() {
691 if let Some(buffer) = server.registered_buffers.remove(&buffer.id()) {
692 server
693 .lsp
694 .notify::<lsp::notification::DidCloseTextDocument>(
695 lsp::DidCloseTextDocumentParams {
696 text_document: lsp::TextDocumentIdentifier::new(buffer.uri),
697 },
698 )
699 .log_err();
700 }
701 }
702 }
703
704 pub fn completions<T>(
705 &mut self,
706 buffer: &ModelHandle<Buffer>,
707 position: T,
708 cx: &mut ModelContext<Self>,
709 ) -> Task<Result<Vec<Completion>>>
710 where
711 T: ToPointUtf16,
712 {
713 self.request_completions::<request::GetCompletions, _>(buffer, position, cx)
714 }
715
716 pub fn completions_cycling<T>(
717 &mut self,
718 buffer: &ModelHandle<Buffer>,
719 position: T,
720 cx: &mut ModelContext<Self>,
721 ) -> Task<Result<Vec<Completion>>>
722 where
723 T: ToPointUtf16,
724 {
725 self.request_completions::<request::GetCompletionsCycling, _>(buffer, position, cx)
726 }
727
728 pub fn accept_completion(
729 &mut self,
730 completion: &Completion,
731 cx: &mut ModelContext<Self>,
732 ) -> Task<Result<()>> {
733 let server = match self.server.as_authenticated() {
734 Ok(server) => server,
735 Err(error) => return Task::ready(Err(error)),
736 };
737 let request =
738 server
739 .lsp
740 .request::<request::NotifyAccepted>(request::NotifyAcceptedParams {
741 uuid: completion.uuid.clone(),
742 });
743 cx.background().spawn(async move {
744 request.await?;
745 Ok(())
746 })
747 }
748
749 pub fn discard_completions(
750 &mut self,
751 completions: &[Completion],
752 cx: &mut ModelContext<Self>,
753 ) -> Task<Result<()>> {
754 let server = match self.server.as_authenticated() {
755 Ok(server) => server,
756 Err(error) => return Task::ready(Err(error)),
757 };
758 let request =
759 server
760 .lsp
761 .request::<request::NotifyRejected>(request::NotifyRejectedParams {
762 uuids: completions
763 .iter()
764 .map(|completion| completion.uuid.clone())
765 .collect(),
766 });
767 cx.background().spawn(async move {
768 request.await?;
769 Ok(())
770 })
771 }
772
773 fn request_completions<R, T>(
774 &mut self,
775 buffer: &ModelHandle<Buffer>,
776 position: T,
777 cx: &mut ModelContext<Self>,
778 ) -> Task<Result<Vec<Completion>>>
779 where
780 R: 'static
781 + lsp::request::Request<
782 Params = request::GetCompletionsParams,
783 Result = request::GetCompletionsResult,
784 >,
785 T: ToPointUtf16,
786 {
787 self.register_buffer(buffer, cx);
788
789 let server = match self.server.as_authenticated() {
790 Ok(server) => server,
791 Err(error) => return Task::ready(Err(error)),
792 };
793 let lsp = server.lsp.clone();
794 let registered_buffer = server.registered_buffers.get_mut(&buffer.id()).unwrap();
795 let snapshot = registered_buffer.report_changes(buffer, cx);
796 let buffer = buffer.read(cx);
797 let uri = registered_buffer.uri.clone();
798 let position = position.to_point_utf16(buffer);
799 let settings = language_settings(buffer.language_at(position).as_ref(), buffer.file(), cx);
800 let tab_size = settings.tab_size;
801 let hard_tabs = settings.hard_tabs;
802 let relative_path = buffer
803 .file()
804 .map(|file| file.path().to_path_buf())
805 .unwrap_or_default();
806
807 cx.foreground().spawn(async move {
808 let (version, snapshot) = snapshot.await?;
809 let result = lsp
810 .request::<R>(request::GetCompletionsParams {
811 doc: request::GetCompletionsDocument {
812 uri,
813 tab_size: tab_size.into(),
814 indent_size: 1,
815 insert_spaces: !hard_tabs,
816 relative_path: relative_path.to_string_lossy().into(),
817 position: point_to_lsp(position),
818 version: version.try_into().unwrap(),
819 },
820 })
821 .await?;
822 let completions = result
823 .completions
824 .into_iter()
825 .map(|completion| {
826 let start = snapshot
827 .clip_point_utf16(point_from_lsp(completion.range.start), Bias::Left);
828 let end =
829 snapshot.clip_point_utf16(point_from_lsp(completion.range.end), Bias::Left);
830 Completion {
831 uuid: completion.uuid,
832 range: snapshot.anchor_before(start)..snapshot.anchor_after(end),
833 text: completion.text,
834 }
835 })
836 .collect();
837 anyhow::Ok(completions)
838 })
839 }
840
841 pub fn status(&self) -> Status {
842 match &self.server {
843 CopilotServer::Starting { task } => Status::Starting { task: task.clone() },
844 CopilotServer::Disabled => Status::Disabled,
845 CopilotServer::Error(error) => Status::Error(error.clone()),
846 CopilotServer::Running(RunningCopilotServer { sign_in_status, .. }) => {
847 match sign_in_status {
848 SignInStatus::Authorized { .. } => Status::Authorized,
849 SignInStatus::Unauthorized { .. } => Status::Unauthorized,
850 SignInStatus::SigningIn { prompt, .. } => Status::SigningIn {
851 prompt: prompt.clone(),
852 },
853 SignInStatus::SignedOut => Status::SignedOut,
854 }
855 }
856 }
857 }
858
859 fn update_sign_in_status(
860 &mut self,
861 lsp_status: request::SignInStatus,
862 cx: &mut ModelContext<Self>,
863 ) {
864 self.buffers.retain(|buffer| buffer.is_upgradable(cx));
865
866 if let Ok(server) = self.server.as_running() {
867 match lsp_status {
868 request::SignInStatus::Ok { .. }
869 | request::SignInStatus::MaybeOk { .. }
870 | request::SignInStatus::AlreadySignedIn { .. } => {
871 server.sign_in_status = SignInStatus::Authorized;
872 for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
873 if let Some(buffer) = buffer.upgrade(cx) {
874 self.register_buffer(&buffer, cx);
875 }
876 }
877 }
878 request::SignInStatus::NotAuthorized { .. } => {
879 server.sign_in_status = SignInStatus::Unauthorized;
880 for buffer in self.buffers.iter().copied().collect::<Vec<_>>() {
881 self.unregister_buffer(&buffer);
882 }
883 }
884 request::SignInStatus::NotSignedIn => {
885 server.sign_in_status = SignInStatus::SignedOut;
886 for buffer in self.buffers.iter().copied().collect::<Vec<_>>() {
887 self.unregister_buffer(&buffer);
888 }
889 }
890 }
891
892 cx.notify();
893 }
894 }
895}
896
897fn id_for_language(language: Option<&Arc<Language>>) -> String {
898 let language_name = language.map(|language| language.name());
899 match language_name.as_deref() {
900 Some("Plain Text") => "plaintext".to_string(),
901 Some(language_name) => language_name.to_lowercase(),
902 None => "plaintext".to_string(),
903 }
904}
905
906fn uri_for_buffer(buffer: &ModelHandle<Buffer>, cx: &AppContext) -> lsp::Url {
907 if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
908 lsp::Url::from_file_path(file.abs_path(cx)).unwrap()
909 } else {
910 format!("buffer://{}", buffer.id()).parse().unwrap()
911 }
912}
913
914async fn clear_copilot_dir() {
915 remove_matching(&paths::COPILOT_DIR, |_| true).await
916}
917
918async fn get_copilot_lsp(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
919 const SERVER_PATH: &'static str = "dist/agent.js";
920
921 ///Check for the latest copilot language server and download it if we haven't already
922 async fn fetch_latest(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
923 let release = latest_github_release("zed-industries/copilot", false, http.clone()).await?;
924
925 let version_dir = &*paths::COPILOT_DIR.join(format!("copilot-{}", release.name));
926
927 fs::create_dir_all(version_dir).await?;
928 let server_path = version_dir.join(SERVER_PATH);
929
930 if fs::metadata(&server_path).await.is_err() {
931 // Copilot LSP looks for this dist dir specifcially, so lets add it in.
932 let dist_dir = version_dir.join("dist");
933 fs::create_dir_all(dist_dir.as_path()).await?;
934
935 let url = &release
936 .assets
937 .get(0)
938 .context("Github release for copilot contained no assets")?
939 .browser_download_url;
940
941 let mut response = http
942 .get(&url, Default::default(), true)
943 .await
944 .map_err(|err| anyhow!("error downloading copilot release: {}", err))?;
945 let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
946 let archive = Archive::new(decompressed_bytes);
947 archive.unpack(dist_dir).await?;
948
949 remove_matching(&paths::COPILOT_DIR, |entry| entry != version_dir).await;
950 }
951
952 Ok(server_path)
953 }
954
955 match fetch_latest(http).await {
956 ok @ Result::Ok(..) => ok,
957 e @ Err(..) => {
958 e.log_err();
959 // Fetch a cached binary, if it exists
960 (|| async move {
961 let mut last_version_dir = None;
962 let mut entries = fs::read_dir(paths::COPILOT_DIR.as_path()).await?;
963 while let Some(entry) = entries.next().await {
964 let entry = entry?;
965 if entry.file_type().await?.is_dir() {
966 last_version_dir = Some(entry.path());
967 }
968 }
969 let last_version_dir =
970 last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
971 let server_path = last_version_dir.join(SERVER_PATH);
972 if server_path.exists() {
973 Ok(server_path)
974 } else {
975 Err(anyhow!(
976 "missing executable in directory {:?}",
977 last_version_dir
978 ))
979 }
980 })()
981 .await
982 }
983 }
984}
985
986#[cfg(test)]
987mod tests {
988 use super::*;
989 use gpui::{executor::Deterministic, TestAppContext};
990
991 #[gpui::test(iterations = 10)]
992 async fn test_buffer_management(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
993 deterministic.forbid_parking();
994 let (copilot, mut lsp) = Copilot::fake(cx);
995
996 let buffer_1 = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, "Hello"));
997 let buffer_1_uri: lsp::Url = format!("buffer://{}", buffer_1.id()).parse().unwrap();
998 copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_1, cx));
999 assert_eq!(
1000 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1001 .await,
1002 lsp::DidOpenTextDocumentParams {
1003 text_document: lsp::TextDocumentItem::new(
1004 buffer_1_uri.clone(),
1005 "plaintext".into(),
1006 0,
1007 "Hello".into()
1008 ),
1009 }
1010 );
1011
1012 let buffer_2 = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, "Goodbye"));
1013 let buffer_2_uri: lsp::Url = format!("buffer://{}", buffer_2.id()).parse().unwrap();
1014 copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_2, cx));
1015 assert_eq!(
1016 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1017 .await,
1018 lsp::DidOpenTextDocumentParams {
1019 text_document: lsp::TextDocumentItem::new(
1020 buffer_2_uri.clone(),
1021 "plaintext".into(),
1022 0,
1023 "Goodbye".into()
1024 ),
1025 }
1026 );
1027
1028 buffer_1.update(cx, |buffer, cx| buffer.edit([(5..5, " world")], None, cx));
1029 assert_eq!(
1030 lsp.receive_notification::<lsp::notification::DidChangeTextDocument>()
1031 .await,
1032 lsp::DidChangeTextDocumentParams {
1033 text_document: lsp::VersionedTextDocumentIdentifier::new(buffer_1_uri.clone(), 1),
1034 content_changes: vec![lsp::TextDocumentContentChangeEvent {
1035 range: Some(lsp::Range::new(
1036 lsp::Position::new(0, 5),
1037 lsp::Position::new(0, 5)
1038 )),
1039 range_length: None,
1040 text: " world".into(),
1041 }],
1042 }
1043 );
1044
1045 // Ensure updates to the file are reflected in the LSP.
1046 buffer_1
1047 .update(cx, |buffer, cx| {
1048 buffer.file_updated(
1049 Arc::new(File {
1050 abs_path: "/root/child/buffer-1".into(),
1051 path: Path::new("child/buffer-1").into(),
1052 }),
1053 cx,
1054 )
1055 })
1056 .await;
1057 assert_eq!(
1058 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1059 .await,
1060 lsp::DidCloseTextDocumentParams {
1061 text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri),
1062 }
1063 );
1064 let buffer_1_uri = lsp::Url::from_file_path("/root/child/buffer-1").unwrap();
1065 assert_eq!(
1066 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1067 .await,
1068 lsp::DidOpenTextDocumentParams {
1069 text_document: lsp::TextDocumentItem::new(
1070 buffer_1_uri.clone(),
1071 "plaintext".into(),
1072 1,
1073 "Hello world".into()
1074 ),
1075 }
1076 );
1077
1078 // Ensure all previously-registered buffers are closed when signing out.
1079 lsp.handle_request::<request::SignOut, _, _>(|_, _| async {
1080 Ok(request::SignOutResult {})
1081 });
1082 copilot
1083 .update(cx, |copilot, cx| copilot.sign_out(cx))
1084 .await
1085 .unwrap();
1086 assert_eq!(
1087 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1088 .await,
1089 lsp::DidCloseTextDocumentParams {
1090 text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri.clone()),
1091 }
1092 );
1093 assert_eq!(
1094 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1095 .await,
1096 lsp::DidCloseTextDocumentParams {
1097 text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri.clone()),
1098 }
1099 );
1100
1101 // Ensure all previously-registered buffers are re-opened when signing in.
1102 lsp.handle_request::<request::SignInInitiate, _, _>(|_, _| async {
1103 Ok(request::SignInInitiateResult::AlreadySignedIn {
1104 user: "user-1".into(),
1105 })
1106 });
1107 copilot
1108 .update(cx, |copilot, cx| copilot.sign_in(cx))
1109 .await
1110 .unwrap();
1111 assert_eq!(
1112 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1113 .await,
1114 lsp::DidOpenTextDocumentParams {
1115 text_document: lsp::TextDocumentItem::new(
1116 buffer_2_uri.clone(),
1117 "plaintext".into(),
1118 0,
1119 "Goodbye".into()
1120 ),
1121 }
1122 );
1123 assert_eq!(
1124 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1125 .await,
1126 lsp::DidOpenTextDocumentParams {
1127 text_document: lsp::TextDocumentItem::new(
1128 buffer_1_uri.clone(),
1129 "plaintext".into(),
1130 0,
1131 "Hello world".into()
1132 ),
1133 }
1134 );
1135
1136 // Dropping a buffer causes it to be closed on the LSP side as well.
1137 cx.update(|_| drop(buffer_2));
1138 assert_eq!(
1139 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1140 .await,
1141 lsp::DidCloseTextDocumentParams {
1142 text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri),
1143 }
1144 );
1145 }
1146
1147 struct File {
1148 abs_path: PathBuf,
1149 path: Arc<Path>,
1150 }
1151
1152 impl language::File for File {
1153 fn as_local(&self) -> Option<&dyn language::LocalFile> {
1154 Some(self)
1155 }
1156
1157 fn mtime(&self) -> std::time::SystemTime {
1158 unimplemented!()
1159 }
1160
1161 fn path(&self) -> &Arc<Path> {
1162 &self.path
1163 }
1164
1165 fn full_path(&self, _: &AppContext) -> PathBuf {
1166 unimplemented!()
1167 }
1168
1169 fn file_name<'a>(&'a self, _: &'a AppContext) -> &'a std::ffi::OsStr {
1170 unimplemented!()
1171 }
1172
1173 fn is_deleted(&self) -> bool {
1174 unimplemented!()
1175 }
1176
1177 fn as_any(&self) -> &dyn std::any::Any {
1178 unimplemented!()
1179 }
1180
1181 fn to_proto(&self) -> rpc::proto::File {
1182 unimplemented!()
1183 }
1184
1185 fn worktree_id(&self) -> usize {
1186 0
1187 }
1188 }
1189
1190 impl language::LocalFile for File {
1191 fn abs_path(&self, _: &AppContext) -> PathBuf {
1192 self.abs_path.clone()
1193 }
1194
1195 fn load(&self, _: &AppContext) -> Task<Result<String>> {
1196 unimplemented!()
1197 }
1198
1199 fn buffer_reloaded(
1200 &self,
1201 _: u64,
1202 _: &clock::Global,
1203 _: language::RopeFingerprint,
1204 _: language::LineEnding,
1205 _: std::time::SystemTime,
1206 _: &mut AppContext,
1207 ) {
1208 unimplemented!()
1209 }
1210 }
1211}