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