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