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