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