copilot.rs

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