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(None, 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 aggressive 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(buffer.language_at(position).as_ref(), buffer.file(), cx);
789 let tab_size = settings.tab_size;
790 let hard_tabs = settings.hard_tabs;
791 let relative_path = buffer
792 .file()
793 .map(|file| file.path().to_path_buf())
794 .unwrap_or_default();
795
796 cx.foreground().spawn(async move {
797 let (version, snapshot) = snapshot.await?;
798 let result = lsp
799 .request::<R>(request::GetCompletionsParams {
800 doc: request::GetCompletionsDocument {
801 uri,
802 tab_size: tab_size.into(),
803 indent_size: 1,
804 insert_spaces: !hard_tabs,
805 relative_path: relative_path.to_string_lossy().into(),
806 position: point_to_lsp(position),
807 version: version.try_into().unwrap(),
808 },
809 })
810 .await?;
811 let completions = result
812 .completions
813 .into_iter()
814 .map(|completion| {
815 let start = snapshot
816 .clip_point_utf16(point_from_lsp(completion.range.start), Bias::Left);
817 let end =
818 snapshot.clip_point_utf16(point_from_lsp(completion.range.end), Bias::Left);
819 Completion {
820 uuid: completion.uuid,
821 range: snapshot.anchor_before(start)..snapshot.anchor_after(end),
822 text: completion.text,
823 }
824 })
825 .collect();
826 anyhow::Ok(completions)
827 })
828 }
829
830 pub fn status(&self) -> Status {
831 match &self.server {
832 CopilotServer::Starting { task } => Status::Starting { task: task.clone() },
833 CopilotServer::Disabled => Status::Disabled,
834 CopilotServer::Error(error) => Status::Error(error.clone()),
835 CopilotServer::Running(RunningCopilotServer { sign_in_status, .. }) => {
836 match sign_in_status {
837 SignInStatus::Authorized { .. } => Status::Authorized,
838 SignInStatus::Unauthorized { .. } => Status::Unauthorized,
839 SignInStatus::SigningIn { prompt, .. } => Status::SigningIn {
840 prompt: prompt.clone(),
841 },
842 SignInStatus::SignedOut => Status::SignedOut,
843 }
844 }
845 }
846 }
847
848 fn update_sign_in_status(
849 &mut self,
850 lsp_status: request::SignInStatus,
851 cx: &mut ModelContext<Self>,
852 ) {
853 self.buffers.retain(|_, buffer| buffer.is_upgradable(cx));
854
855 if let Ok(server) = self.server.as_running() {
856 match lsp_status {
857 request::SignInStatus::Ok { .. }
858 | request::SignInStatus::MaybeOk { .. }
859 | request::SignInStatus::AlreadySignedIn { .. } => {
860 server.sign_in_status = SignInStatus::Authorized;
861 for buffer in self.buffers.values().cloned().collect::<Vec<_>>() {
862 if let Some(buffer) = buffer.upgrade(cx) {
863 self.register_buffer(&buffer, cx);
864 }
865 }
866 }
867 request::SignInStatus::NotAuthorized { .. } => {
868 server.sign_in_status = SignInStatus::Unauthorized;
869 for buffer_id in self.buffers.keys().copied().collect::<Vec<_>>() {
870 self.unregister_buffer(buffer_id);
871 }
872 }
873 request::SignInStatus::NotSignedIn => {
874 server.sign_in_status = SignInStatus::SignedOut;
875 for buffer_id in self.buffers.keys().copied().collect::<Vec<_>>() {
876 self.unregister_buffer(buffer_id);
877 }
878 }
879 }
880
881 cx.notify();
882 }
883 }
884}
885
886fn id_for_language(language: Option<&Arc<Language>>) -> String {
887 let language_name = language.map(|language| language.name());
888 match language_name.as_deref() {
889 Some("Plain Text") => "plaintext".to_string(),
890 Some(language_name) => language_name.to_lowercase(),
891 None => "plaintext".to_string(),
892 }
893}
894
895fn uri_for_buffer(buffer: &ModelHandle<Buffer>, cx: &AppContext) -> lsp::Url {
896 if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
897 lsp::Url::from_file_path(file.abs_path(cx)).unwrap()
898 } else {
899 format!("buffer://{}", buffer.read(cx).remote_id())
900 .parse()
901 .unwrap()
902 }
903}
904
905async fn clear_copilot_dir() {
906 remove_matching(&paths::COPILOT_DIR, |_| true).await
907}
908
909async fn get_copilot_lsp(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
910 const SERVER_PATH: &'static str = "dist/agent.js";
911
912 ///Check for the latest copilot language server and download it if we haven't already
913 async fn fetch_latest(http: Arc<dyn HttpClient>) -> anyhow::Result<PathBuf> {
914 let release = latest_github_release("zed-industries/copilot", false, http.clone()).await?;
915
916 let version_dir = &*paths::COPILOT_DIR.join(format!("copilot-{}", release.name));
917
918 fs::create_dir_all(version_dir).await?;
919 let server_path = version_dir.join(SERVER_PATH);
920
921 if fs::metadata(&server_path).await.is_err() {
922 // Copilot LSP looks for this dist dir specifcially, so lets add it in.
923 let dist_dir = version_dir.join("dist");
924 fs::create_dir_all(dist_dir.as_path()).await?;
925
926 let url = &release
927 .assets
928 .get(0)
929 .context("Github release for copilot contained no assets")?
930 .browser_download_url;
931
932 let mut response = http
933 .get(&url, Default::default(), true)
934 .await
935 .map_err(|err| anyhow!("error downloading copilot release: {}", err))?;
936 let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
937 let archive = Archive::new(decompressed_bytes);
938 archive.unpack(dist_dir).await?;
939
940 remove_matching(&paths::COPILOT_DIR, |entry| entry != version_dir).await;
941 }
942
943 Ok(server_path)
944 }
945
946 match fetch_latest(http).await {
947 ok @ Result::Ok(..) => ok,
948 e @ Err(..) => {
949 e.log_err();
950 // Fetch a cached binary, if it exists
951 (|| async move {
952 let mut last_version_dir = None;
953 let mut entries = fs::read_dir(paths::COPILOT_DIR.as_path()).await?;
954 while let Some(entry) = entries.next().await {
955 let entry = entry?;
956 if entry.file_type().await?.is_dir() {
957 last_version_dir = Some(entry.path());
958 }
959 }
960 let last_version_dir =
961 last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
962 let server_path = last_version_dir.join(SERVER_PATH);
963 if server_path.exists() {
964 Ok(server_path)
965 } else {
966 Err(anyhow!(
967 "missing executable in directory {:?}",
968 last_version_dir
969 ))
970 }
971 })()
972 .await
973 }
974 }
975}
976
977#[cfg(test)]
978mod tests {
979 use super::*;
980 use gpui::{executor::Deterministic, TestAppContext};
981
982 #[gpui::test(iterations = 10)]
983 async fn test_buffer_management(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
984 deterministic.forbid_parking();
985 let (copilot, mut lsp) = Copilot::fake(cx);
986
987 let buffer_1 = cx.add_model(|cx| Buffer::new(0, "Hello", cx));
988 let buffer_1_uri: lsp::Url = format!("buffer://{}", buffer_1.id()).parse().unwrap();
989 copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_1, cx));
990 assert_eq!(
991 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
992 .await,
993 lsp::DidOpenTextDocumentParams {
994 text_document: lsp::TextDocumentItem::new(
995 buffer_1_uri.clone(),
996 "plaintext".into(),
997 0,
998 "Hello".into()
999 ),
1000 }
1001 );
1002
1003 let buffer_2 = cx.add_model(|cx| Buffer::new(0, "Goodbye", cx));
1004 let buffer_2_uri: lsp::Url = format!("buffer://{}", buffer_2.id()).parse().unwrap();
1005 copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_2, cx));
1006 assert_eq!(
1007 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1008 .await,
1009 lsp::DidOpenTextDocumentParams {
1010 text_document: lsp::TextDocumentItem::new(
1011 buffer_2_uri.clone(),
1012 "plaintext".into(),
1013 0,
1014 "Goodbye".into()
1015 ),
1016 }
1017 );
1018
1019 buffer_1.update(cx, |buffer, cx| buffer.edit([(5..5, " world")], None, cx));
1020 assert_eq!(
1021 lsp.receive_notification::<lsp::notification::DidChangeTextDocument>()
1022 .await,
1023 lsp::DidChangeTextDocumentParams {
1024 text_document: lsp::VersionedTextDocumentIdentifier::new(buffer_1_uri.clone(), 1),
1025 content_changes: vec![lsp::TextDocumentContentChangeEvent {
1026 range: Some(lsp::Range::new(
1027 lsp::Position::new(0, 5),
1028 lsp::Position::new(0, 5)
1029 )),
1030 range_length: None,
1031 text: " world".into(),
1032 }],
1033 }
1034 );
1035
1036 // Ensure updates to the file are reflected in the LSP.
1037 buffer_1
1038 .update(cx, |buffer, cx| {
1039 buffer.file_updated(
1040 Arc::new(File {
1041 abs_path: "/root/child/buffer-1".into(),
1042 path: Path::new("child/buffer-1").into(),
1043 }),
1044 cx,
1045 )
1046 })
1047 .await;
1048 assert_eq!(
1049 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1050 .await,
1051 lsp::DidCloseTextDocumentParams {
1052 text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri),
1053 }
1054 );
1055 let buffer_1_uri = lsp::Url::from_file_path("/root/child/buffer-1").unwrap();
1056 assert_eq!(
1057 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1058 .await,
1059 lsp::DidOpenTextDocumentParams {
1060 text_document: lsp::TextDocumentItem::new(
1061 buffer_1_uri.clone(),
1062 "plaintext".into(),
1063 1,
1064 "Hello world".into()
1065 ),
1066 }
1067 );
1068
1069 // Ensure all previously-registered buffers are closed when signing out.
1070 lsp.handle_request::<request::SignOut, _, _>(|_, _| async {
1071 Ok(request::SignOutResult {})
1072 });
1073 copilot
1074 .update(cx, |copilot, cx| copilot.sign_out(cx))
1075 .await
1076 .unwrap();
1077 assert_eq!(
1078 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1079 .await,
1080 lsp::DidCloseTextDocumentParams {
1081 text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri.clone()),
1082 }
1083 );
1084 assert_eq!(
1085 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1086 .await,
1087 lsp::DidCloseTextDocumentParams {
1088 text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri.clone()),
1089 }
1090 );
1091
1092 // Ensure all previously-registered buffers are re-opened when signing in.
1093 lsp.handle_request::<request::SignInInitiate, _, _>(|_, _| async {
1094 Ok(request::SignInInitiateResult::AlreadySignedIn {
1095 user: "user-1".into(),
1096 })
1097 });
1098 copilot
1099 .update(cx, |copilot, cx| copilot.sign_in(cx))
1100 .await
1101 .unwrap();
1102 assert_eq!(
1103 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1104 .await,
1105 lsp::DidOpenTextDocumentParams {
1106 text_document: lsp::TextDocumentItem::new(
1107 buffer_2_uri.clone(),
1108 "plaintext".into(),
1109 0,
1110 "Goodbye".into()
1111 ),
1112 }
1113 );
1114 assert_eq!(
1115 lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1116 .await,
1117 lsp::DidOpenTextDocumentParams {
1118 text_document: lsp::TextDocumentItem::new(
1119 buffer_1_uri.clone(),
1120 "plaintext".into(),
1121 0,
1122 "Hello world".into()
1123 ),
1124 }
1125 );
1126
1127 // Dropping a buffer causes it to be closed on the LSP side as well.
1128 cx.update(|_| drop(buffer_2));
1129 assert_eq!(
1130 lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1131 .await,
1132 lsp::DidCloseTextDocumentParams {
1133 text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri),
1134 }
1135 );
1136 }
1137
1138 struct File {
1139 abs_path: PathBuf,
1140 path: Arc<Path>,
1141 }
1142
1143 impl language::File for File {
1144 fn as_local(&self) -> Option<&dyn language::LocalFile> {
1145 Some(self)
1146 }
1147
1148 fn mtime(&self) -> std::time::SystemTime {
1149 unimplemented!()
1150 }
1151
1152 fn path(&self) -> &Arc<Path> {
1153 &self.path
1154 }
1155
1156 fn full_path(&self, _: &AppContext) -> PathBuf {
1157 unimplemented!()
1158 }
1159
1160 fn file_name<'a>(&'a self, _: &'a AppContext) -> &'a std::ffi::OsStr {
1161 unimplemented!()
1162 }
1163
1164 fn is_deleted(&self) -> bool {
1165 unimplemented!()
1166 }
1167
1168 fn as_any(&self) -> &dyn std::any::Any {
1169 unimplemented!()
1170 }
1171
1172 fn to_proto(&self) -> rpc::proto::File {
1173 unimplemented!()
1174 }
1175
1176 fn worktree_id(&self) -> usize {
1177 0
1178 }
1179 }
1180
1181 impl language::LocalFile for File {
1182 fn abs_path(&self, _: &AppContext) -> PathBuf {
1183 self.abs_path.clone()
1184 }
1185
1186 fn load(&self, _: &AppContext) -> Task<Result<String>> {
1187 unimplemented!()
1188 }
1189
1190 fn buffer_reloaded(
1191 &self,
1192 _: u64,
1193 _: &clock::Global,
1194 _: language::RopeFingerprint,
1195 _: ::fs::LineEnding,
1196 _: std::time::SystemTime,
1197 _: &mut AppContext,
1198 ) {
1199 unimplemented!()
1200 }
1201 }
1202}