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