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