copilot.rs

   1mod copilot_edit_prediction_delegate;
   2pub mod request;
   3
   4use crate::request::{
   5    DidFocus, DidFocusParams, FormattingOptions, InlineCompletionContext,
   6    InlineCompletionTriggerKind, InlineCompletions, NextEditSuggestions,
   7};
   8use ::fs::Fs;
   9use anyhow::{Context as _, Result, anyhow};
  10use collections::{HashMap, HashSet};
  11use command_palette_hooks::CommandPaletteFilter;
  12use futures::future;
  13use futures::{Future, FutureExt, TryFutureExt, channel::oneshot, future::Shared, select_biased};
  14use gpui::{
  15    App, AppContext as _, AsyncApp, Context, Entity, EntityId, EventEmitter, Global, Subscription,
  16    Task, WeakEntity, actions,
  17};
  18use language::language_settings::{AllLanguageSettings, CopilotSettings};
  19use language::{
  20    Anchor, Bias, Buffer, BufferSnapshot, Language, PointUtf16, ToPointUtf16,
  21    language_settings::{EditPredictionProvider, all_language_settings},
  22    point_from_lsp, point_to_lsp,
  23};
  24use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId, LanguageServerName};
  25use node_runtime::{NodeRuntime, VersionStrategy};
  26use parking_lot::Mutex;
  27use project::{DisableAiSettings, Project};
  28use request::DidChangeStatus;
  29use semver::Version;
  30use serde_json::json;
  31use settings::{Settings, SettingsStore};
  32use std::{
  33    any::TypeId,
  34    collections::hash_map::Entry,
  35    env,
  36    ffi::OsString,
  37    mem,
  38    ops::Range,
  39    path::{Path, PathBuf},
  40    sync::Arc,
  41};
  42use sum_tree::Dimensions;
  43use util::{ResultExt, fs::remove_matching};
  44use workspace::AppState;
  45
  46pub use crate::copilot_edit_prediction_delegate::CopilotEditPredictionDelegate;
  47
  48actions!(
  49    copilot,
  50    [
  51        /// Requests a code completion suggestion from Copilot.
  52        Suggest,
  53        /// Cycles to the next Copilot suggestion.
  54        NextSuggestion,
  55        /// Cycles to the previous Copilot suggestion.
  56        PreviousSuggestion,
  57        /// Reinstalls the Copilot language server.
  58        Reinstall,
  59        /// Signs in to GitHub Copilot.
  60        SignIn,
  61        /// Signs out of GitHub Copilot.
  62        SignOut
  63    ]
  64);
  65
  66enum CopilotServer {
  67    Disabled,
  68    Starting { task: Shared<Task<()>> },
  69    Error(Arc<str>),
  70    Running(RunningCopilotServer),
  71}
  72
  73impl CopilotServer {
  74    fn as_authenticated(&mut self) -> Result<&mut RunningCopilotServer> {
  75        let server = self.as_running()?;
  76        anyhow::ensure!(
  77            matches!(server.sign_in_status, SignInStatus::Authorized),
  78            "must sign in before using copilot"
  79        );
  80        Ok(server)
  81    }
  82
  83    fn as_running(&mut self) -> Result<&mut RunningCopilotServer> {
  84        match self {
  85            CopilotServer::Starting { .. } => anyhow::bail!("copilot is still starting"),
  86            CopilotServer::Disabled => anyhow::bail!("copilot is disabled"),
  87            CopilotServer::Error(error) => {
  88                anyhow::bail!("copilot was not started because of an error: {error}")
  89            }
  90            CopilotServer::Running(server) => Ok(server),
  91        }
  92    }
  93}
  94
  95struct RunningCopilotServer {
  96    lsp: Arc<LanguageServer>,
  97    sign_in_status: SignInStatus,
  98    registered_buffers: HashMap<EntityId, RegisteredBuffer>,
  99}
 100
 101#[derive(Clone, Debug)]
 102enum SignInStatus {
 103    Authorized,
 104    Unauthorized,
 105    SigningIn {
 106        prompt: Option<request::PromptUserDeviceFlow>,
 107        task: Shared<Task<Result<(), Arc<anyhow::Error>>>>,
 108    },
 109    SignedOut {
 110        awaiting_signing_in: bool,
 111    },
 112}
 113
 114#[derive(Debug, Clone)]
 115pub enum Status {
 116    Starting {
 117        task: Shared<Task<()>>,
 118    },
 119    Error(Arc<str>),
 120    Disabled,
 121    SignedOut {
 122        awaiting_signing_in: bool,
 123    },
 124    SigningIn {
 125        prompt: Option<request::PromptUserDeviceFlow>,
 126    },
 127    Unauthorized,
 128    Authorized,
 129}
 130
 131impl Status {
 132    pub fn is_authorized(&self) -> bool {
 133        matches!(self, Status::Authorized)
 134    }
 135
 136    pub fn is_configured(&self) -> bool {
 137        matches!(
 138            self,
 139            Status::Starting { .. }
 140                | Status::Error(_)
 141                | Status::SigningIn { .. }
 142                | Status::Authorized
 143        )
 144    }
 145}
 146
 147struct RegisteredBuffer {
 148    uri: lsp::Uri,
 149    language_id: String,
 150    snapshot: BufferSnapshot,
 151    snapshot_version: i32,
 152    _subscriptions: [gpui::Subscription; 2],
 153    pending_buffer_change: Task<Option<()>>,
 154}
 155
 156impl RegisteredBuffer {
 157    fn report_changes(
 158        &mut self,
 159        buffer: &Entity<Buffer>,
 160        cx: &mut Context<Copilot>,
 161    ) -> oneshot::Receiver<(i32, BufferSnapshot)> {
 162        let (done_tx, done_rx) = oneshot::channel();
 163
 164        if buffer.read(cx).version() == self.snapshot.version {
 165            let _ = done_tx.send((self.snapshot_version, self.snapshot.clone()));
 166        } else {
 167            let buffer = buffer.downgrade();
 168            let id = buffer.entity_id();
 169            let prev_pending_change =
 170                mem::replace(&mut self.pending_buffer_change, Task::ready(None));
 171            self.pending_buffer_change = cx.spawn(async move |copilot, cx| {
 172                prev_pending_change.await;
 173
 174                let old_version = copilot
 175                    .update(cx, |copilot, _| {
 176                        let server = copilot.server.as_authenticated().log_err()?;
 177                        let buffer = server.registered_buffers.get_mut(&id)?;
 178                        Some(buffer.snapshot.version.clone())
 179                    })
 180                    .ok()??;
 181                let new_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot()).ok()?;
 182
 183                let content_changes = cx
 184                    .background_spawn({
 185                        let new_snapshot = new_snapshot.clone();
 186                        async move {
 187                            new_snapshot
 188                                .edits_since::<Dimensions<PointUtf16, usize>>(&old_version)
 189                                .map(|edit| {
 190                                    let edit_start = edit.new.start.0;
 191                                    let edit_end = edit_start + (edit.old.end.0 - edit.old.start.0);
 192                                    let new_text = new_snapshot
 193                                        .text_for_range(edit.new.start.1..edit.new.end.1)
 194                                        .collect();
 195                                    lsp::TextDocumentContentChangeEvent {
 196                                        range: Some(lsp::Range::new(
 197                                            point_to_lsp(edit_start),
 198                                            point_to_lsp(edit_end),
 199                                        )),
 200                                        range_length: None,
 201                                        text: new_text,
 202                                    }
 203                                })
 204                                .collect::<Vec<_>>()
 205                        }
 206                    })
 207                    .await;
 208
 209                copilot
 210                    .update(cx, |copilot, _| {
 211                        let server = copilot.server.as_authenticated().log_err()?;
 212                        let buffer = server.registered_buffers.get_mut(&id)?;
 213                        if !content_changes.is_empty() {
 214                            buffer.snapshot_version += 1;
 215                            buffer.snapshot = new_snapshot;
 216                            server
 217                                .lsp
 218                                .notify::<lsp::notification::DidChangeTextDocument>(
 219                                    lsp::DidChangeTextDocumentParams {
 220                                        text_document: lsp::VersionedTextDocumentIdentifier::new(
 221                                            buffer.uri.clone(),
 222                                            buffer.snapshot_version,
 223                                        ),
 224                                        content_changes,
 225                                    },
 226                                )
 227                                .ok();
 228                        }
 229                        let _ = done_tx.send((buffer.snapshot_version, buffer.snapshot.clone()));
 230                        Some(())
 231                    })
 232                    .ok()?;
 233
 234                Some(())
 235            });
 236        }
 237
 238        done_rx
 239    }
 240}
 241
 242#[derive(Debug)]
 243pub struct Completion {
 244    pub uuid: String,
 245    pub range: Range<Anchor>,
 246    pub text: String,
 247}
 248
 249pub struct Copilot {
 250    fs: Arc<dyn Fs>,
 251    node_runtime: NodeRuntime,
 252    server: CopilotServer,
 253    buffers: HashSet<WeakEntity<Buffer>>,
 254    server_id: LanguageServerId,
 255    _subscriptions: Vec<Subscription>,
 256}
 257
 258pub enum Event {
 259    CopilotAuthSignedIn,
 260    CopilotAuthSignedOut,
 261}
 262
 263impl EventEmitter<Event> for Copilot {}
 264
 265#[derive(Clone)]
 266pub struct GlobalCopilotAuth(pub Entity<Copilot>);
 267
 268impl GlobalCopilotAuth {
 269    pub fn set_global(
 270        server_id: LanguageServerId,
 271        fs: Arc<dyn Fs>,
 272        node_runtime: NodeRuntime,
 273        cx: &mut App,
 274    ) -> GlobalCopilotAuth {
 275        let auth =
 276            GlobalCopilotAuth(cx.new(|cx| Copilot::new(None, server_id, fs, node_runtime, cx)));
 277        cx.set_global(auth.clone());
 278        auth
 279    }
 280    pub fn try_global(cx: &mut App) -> Option<&GlobalCopilotAuth> {
 281        cx.try_global()
 282    }
 283
 284    pub fn get_or_init(app_state: Arc<AppState>, cx: &mut App) -> GlobalCopilotAuth {
 285        if let Some(copilot) = cx.try_global::<Self>() {
 286            copilot.clone()
 287        } else {
 288            Self::set_global(
 289                app_state.languages.next_language_server_id(),
 290                app_state.fs.clone(),
 291                app_state.node_runtime.clone(),
 292                cx,
 293            )
 294        }
 295    }
 296}
 297impl Global for GlobalCopilotAuth {}
 298
 299#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 300pub(crate) enum CompletionSource {
 301    NextEditSuggestion,
 302    InlineCompletion,
 303}
 304
 305/// Copilot's NextEditSuggestion response, with coordinates converted to Anchors.
 306#[derive(Clone)]
 307pub(crate) struct CopilotEditPrediction {
 308    pub(crate) buffer: Entity<Buffer>,
 309    pub(crate) range: Range<Anchor>,
 310    pub(crate) text: String,
 311    pub(crate) command: Option<lsp::Command>,
 312    pub(crate) snapshot: BufferSnapshot,
 313    pub(crate) source: CompletionSource,
 314}
 315
 316impl Copilot {
 317    pub fn new(
 318        project: Option<Entity<Project>>,
 319        new_server_id: LanguageServerId,
 320        fs: Arc<dyn Fs>,
 321        node_runtime: NodeRuntime,
 322        cx: &mut Context<Self>,
 323    ) -> Self {
 324        let send_focus_notification = project.map(|project| {
 325            cx.subscribe(&project, |this, project, e: &project::Event, cx| {
 326                if let project::Event::ActiveEntryChanged(new_entry) = e
 327                    && let Ok(running) = this.server.as_authenticated()
 328                {
 329                    let uri = new_entry
 330                        .and_then(|id| project.read(cx).path_for_entry(id, cx))
 331                        .and_then(|entry| project.read(cx).absolute_path(&entry, cx))
 332                        .and_then(|abs_path| lsp::Uri::from_file_path(abs_path).ok());
 333
 334                    _ = running.lsp.notify::<DidFocus>(DidFocusParams { uri });
 335                }
 336            })
 337        });
 338        let global_authentication_events =
 339            cx.try_global::<GlobalCopilotAuth>().cloned().map(|auth| {
 340                cx.subscribe(&auth.0, |_, _, _: &Event, cx| {
 341                    cx.spawn(async move |this, cx| {
 342                        let Some(server) = this
 343                            .update(cx, |this, _| this.language_server().cloned())
 344                            .ok()
 345                            .flatten()
 346                        else {
 347                            return;
 348                        };
 349                        let status = server
 350                            .request::<request::CheckStatus>(request::CheckStatusParams {
 351                                local_checks_only: false,
 352                            })
 353                            .await
 354                            .into_response()
 355                            .ok();
 356                        if let Some(status) = status {
 357                            this.update(cx, |copilot, cx| {
 358                                copilot.update_sign_in_status(status, cx);
 359                            })
 360                            .ok();
 361                        }
 362                    })
 363                    .detach()
 364                })
 365            });
 366        let _subscriptions = std::iter::once(cx.on_app_quit(Self::shutdown_language_server))
 367            .chain(send_focus_notification)
 368            .chain(global_authentication_events)
 369            .collect();
 370        let mut this = Self {
 371            server_id: new_server_id,
 372            fs,
 373            node_runtime,
 374            server: CopilotServer::Disabled,
 375            buffers: Default::default(),
 376            _subscriptions,
 377        };
 378        this.start_copilot(true, false, cx);
 379        cx.observe_global::<SettingsStore>(move |this, cx| {
 380            this.start_copilot(true, false, cx);
 381            if let Ok(server) = this.server.as_running() {
 382                notify_did_change_config_to_server(&server.lsp, cx)
 383                    .context("copilot setting change: did change configuration")
 384                    .log_err();
 385            }
 386            this.update_action_visibilities(cx);
 387        })
 388        .detach();
 389        cx.observe_self(|copilot, cx| {
 390            copilot.update_action_visibilities(cx);
 391        })
 392        .detach();
 393        this
 394    }
 395
 396    fn shutdown_language_server(
 397        &mut self,
 398        _cx: &mut Context<Self>,
 399    ) -> impl Future<Output = ()> + use<> {
 400        let shutdown = match mem::replace(&mut self.server, CopilotServer::Disabled) {
 401            CopilotServer::Running(server) => Some(Box::pin(async move { server.lsp.shutdown() })),
 402            _ => None,
 403        };
 404
 405        async move {
 406            if let Some(shutdown) = shutdown {
 407                shutdown.await;
 408            }
 409        }
 410    }
 411
 412    pub fn start_copilot(
 413        &mut self,
 414        check_edit_prediction_provider: bool,
 415        awaiting_sign_in_after_start: bool,
 416        cx: &mut Context<Self>,
 417    ) {
 418        if !matches!(self.server, CopilotServer::Disabled) {
 419            return;
 420        }
 421        let language_settings = all_language_settings(None, cx);
 422        if check_edit_prediction_provider
 423            && language_settings.edit_predictions.provider != EditPredictionProvider::Copilot
 424        {
 425            return;
 426        }
 427        let server_id = self.server_id;
 428        let fs = self.fs.clone();
 429        let node_runtime = self.node_runtime.clone();
 430        let env = self.build_env(&language_settings.edit_predictions.copilot);
 431        let start_task = cx
 432            .spawn(async move |this, cx| {
 433                Self::start_language_server(
 434                    server_id,
 435                    fs,
 436                    node_runtime,
 437                    env,
 438                    this,
 439                    awaiting_sign_in_after_start,
 440                    cx,
 441                )
 442                .await
 443            })
 444            .shared();
 445        self.server = CopilotServer::Starting { task: start_task };
 446        cx.notify();
 447    }
 448
 449    fn build_env(&self, copilot_settings: &CopilotSettings) -> Option<HashMap<String, String>> {
 450        let proxy_url = copilot_settings.proxy.clone()?;
 451        let no_verify = copilot_settings.proxy_no_verify;
 452        let http_or_https_proxy = if proxy_url.starts_with("http:") {
 453            Some("HTTP_PROXY")
 454        } else if proxy_url.starts_with("https:") {
 455            Some("HTTPS_PROXY")
 456        } else {
 457            log::error!(
 458                "Unsupported protocol scheme for language server proxy (must be http or https)"
 459            );
 460            None
 461        };
 462
 463        let mut env = HashMap::default();
 464
 465        if let Some(proxy_type) = http_or_https_proxy {
 466            env.insert(proxy_type.to_string(), proxy_url);
 467            if let Some(true) = no_verify {
 468                env.insert("NODE_TLS_REJECT_UNAUTHORIZED".to_string(), "0".to_string());
 469            };
 470        }
 471
 472        if let Ok(oauth_token) = env::var(copilot_chat::COPILOT_OAUTH_ENV_VAR) {
 473            env.insert(copilot_chat::COPILOT_OAUTH_ENV_VAR.to_string(), oauth_token);
 474        }
 475
 476        if env.is_empty() { None } else { Some(env) }
 477    }
 478
 479    #[cfg(any(test, feature = "test-support"))]
 480    pub fn fake(cx: &mut gpui::TestAppContext) -> (Entity<Self>, lsp::FakeLanguageServer) {
 481        use fs::FakeFs;
 482        use gpui::Subscription;
 483        use lsp::FakeLanguageServer;
 484        use node_runtime::NodeRuntime;
 485
 486        let (server, fake_server) = FakeLanguageServer::new(
 487            LanguageServerId(0),
 488            LanguageServerBinary {
 489                path: "path/to/copilot".into(),
 490                arguments: vec![],
 491                env: None,
 492            },
 493            "copilot".into(),
 494            Default::default(),
 495            &mut cx.to_async(),
 496        );
 497        let node_runtime = NodeRuntime::unavailable();
 498        let send_focus_notification = Subscription::new(|| {});
 499        let this = cx.new(|cx| Self {
 500            server_id: LanguageServerId(0),
 501            fs: FakeFs::new(cx.background_executor().clone()),
 502            node_runtime,
 503            server: CopilotServer::Running(RunningCopilotServer {
 504                lsp: Arc::new(server),
 505                sign_in_status: SignInStatus::Authorized,
 506                registered_buffers: Default::default(),
 507            }),
 508            _subscriptions: vec![
 509                send_focus_notification,
 510                cx.on_app_quit(Self::shutdown_language_server),
 511            ],
 512            buffers: Default::default(),
 513        });
 514        (this, fake_server)
 515    }
 516
 517    async fn start_language_server(
 518        new_server_id: LanguageServerId,
 519        fs: Arc<dyn Fs>,
 520        node_runtime: NodeRuntime,
 521        env: Option<HashMap<String, String>>,
 522        this: WeakEntity<Self>,
 523        awaiting_sign_in_after_start: bool,
 524        cx: &mut AsyncApp,
 525    ) {
 526        let start_language_server = async {
 527            let server_path = get_copilot_lsp(fs, node_runtime.clone()).await?;
 528            let node_path = node_runtime.binary_path().await?;
 529            ensure_node_version_for_copilot(&node_path).await?;
 530
 531            let arguments: Vec<OsString> = vec![
 532                "--experimental-sqlite".into(),
 533                server_path.into(),
 534                "--stdio".into(),
 535            ];
 536            let binary = LanguageServerBinary {
 537                path: node_path,
 538                arguments,
 539                env,
 540            };
 541
 542            let root_path = if cfg!(target_os = "windows") {
 543                Path::new("C:/")
 544            } else {
 545                Path::new("/")
 546            };
 547
 548            let server_name = LanguageServerName("copilot".into());
 549            let server = LanguageServer::new(
 550                Arc::new(Mutex::new(None)),
 551                new_server_id,
 552                server_name,
 553                binary,
 554                root_path,
 555                None,
 556                Default::default(),
 557                cx,
 558            )?;
 559
 560            server
 561                .on_notification::<DidChangeStatus, _>({
 562                    let this = this.clone();
 563                    move |params, cx| {
 564                        if params.kind == request::StatusKind::Normal {
 565                            let this = this.clone();
 566                            cx.spawn(async move |cx| {
 567                                let lsp = this
 568                                    .read_with(cx, |copilot, _| {
 569                                        if let CopilotServer::Running(server) = &copilot.server {
 570                                            Some(server.lsp.clone())
 571                                        } else {
 572                                            None
 573                                        }
 574                                    })
 575                                    .ok()
 576                                    .flatten();
 577                                let Some(lsp) = lsp else { return };
 578                                let status = lsp
 579                                    .request::<request::CheckStatus>(request::CheckStatusParams {
 580                                        local_checks_only: false,
 581                                    })
 582                                    .await
 583                                    .into_response()
 584                                    .ok();
 585                                if let Some(status) = status {
 586                                    this.update(cx, |copilot, cx| {
 587                                        copilot.update_sign_in_status(status, cx);
 588                                    })
 589                                    .ok();
 590                                }
 591                            })
 592                            .detach();
 593                        }
 594                    }
 595                })
 596                .detach();
 597
 598            server
 599                .on_request::<lsp::request::ShowDocument, _, _>(move |params, cx| {
 600                    if params.external.unwrap_or(false) {
 601                        let url = params.uri.to_string();
 602                        cx.update(|cx| cx.open_url(&url));
 603                    }
 604                    async move { Ok(lsp::ShowDocumentResult { success: true }) }
 605                })
 606                .detach();
 607
 608            let configuration = lsp::DidChangeConfigurationParams {
 609                settings: Default::default(),
 610            };
 611
 612            let editor_info = request::SetEditorInfoParams {
 613                editor_info: request::EditorInfo {
 614                    name: "zed".into(),
 615                    version: env!("CARGO_PKG_VERSION").into(),
 616                },
 617                editor_plugin_info: request::EditorPluginInfo {
 618                    name: "zed-copilot".into(),
 619                    version: "0.0.1".into(),
 620                },
 621            };
 622            let editor_info_json = serde_json::to_value(&editor_info)?;
 623
 624            let server = cx
 625                .update(|cx| {
 626                    let mut params = server.default_initialize_params(false, cx);
 627                    params.initialization_options = Some(editor_info_json);
 628                    params
 629                        .capabilities
 630                        .window
 631                        .get_or_insert_with(Default::default)
 632                        .show_document =
 633                        Some(lsp::ShowDocumentClientCapabilities { support: true });
 634                    server.initialize(params, configuration.into(), cx)
 635                })
 636                .await?;
 637
 638            this.update(cx, |_, cx| notify_did_change_config_to_server(&server, cx))?
 639                .context("copilot: did change configuration")?;
 640
 641            let status = server
 642                .request::<request::CheckStatus>(request::CheckStatusParams {
 643                    local_checks_only: false,
 644                })
 645                .await
 646                .into_response()
 647                .context("copilot: check status")?;
 648
 649            anyhow::Ok((server, status))
 650        };
 651
 652        let server = start_language_server.await;
 653        this.update(cx, |this, cx| {
 654            cx.notify();
 655
 656            if env::var("ZED_FORCE_COPILOT_ERROR").is_ok() {
 657                this.server = CopilotServer::Error(
 658                    "Forced error for testing (ZED_FORCE_COPILOT_ERROR)".into(),
 659                );
 660                return;
 661            }
 662
 663            match server {
 664                Ok((server, status)) => {
 665                    this.server = CopilotServer::Running(RunningCopilotServer {
 666                        lsp: server,
 667                        sign_in_status: SignInStatus::SignedOut {
 668                            awaiting_signing_in: awaiting_sign_in_after_start,
 669                        },
 670                        registered_buffers: Default::default(),
 671                    });
 672                    this.update_sign_in_status(status, cx);
 673                }
 674                Err(error) => {
 675                    this.server = CopilotServer::Error(error.to_string().into());
 676                    cx.notify()
 677                }
 678            }
 679        })
 680        .ok();
 681    }
 682
 683    pub fn is_authenticated(&self) -> bool {
 684        return matches!(
 685            self.server,
 686            CopilotServer::Running(RunningCopilotServer {
 687                sign_in_status: SignInStatus::Authorized,
 688                ..
 689            })
 690        );
 691    }
 692
 693    pub fn sign_in(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
 694        if let CopilotServer::Running(server) = &mut self.server {
 695            let task = match &server.sign_in_status {
 696                SignInStatus::Authorized => Task::ready(Ok(())).shared(),
 697                SignInStatus::SigningIn { task, .. } => {
 698                    cx.notify();
 699                    task.clone()
 700                }
 701                SignInStatus::SignedOut { .. } | SignInStatus::Unauthorized => {
 702                    let lsp = server.lsp.clone();
 703
 704                    let task = cx
 705                        .spawn(async move |this, cx| {
 706                            let sign_in = async {
 707                                let flow = lsp
 708                                    .request::<request::SignIn>(request::SignInParams {})
 709                                    .await
 710                                    .into_response()
 711                                    .context("copilot sign-in")?;
 712
 713                                this.update(cx, |this, cx| {
 714                                    if let CopilotServer::Running(RunningCopilotServer {
 715                                        sign_in_status: status,
 716                                        ..
 717                                    }) = &mut this.server
 718                                        && let SignInStatus::SigningIn {
 719                                            prompt: prompt_flow,
 720                                            ..
 721                                        } = status
 722                                    {
 723                                        *prompt_flow = Some(flow.clone());
 724                                        cx.notify();
 725                                    }
 726                                })?;
 727
 728                                anyhow::Ok(())
 729                            };
 730
 731                            let sign_in = sign_in.await;
 732                            this.update(cx, |this, cx| match sign_in {
 733                                Ok(()) => Ok(()),
 734                                Err(error) => {
 735                                    this.update_sign_in_status(
 736                                        request::SignInStatus::NotSignedIn,
 737                                        cx,
 738                                    );
 739                                    Err(Arc::new(error))
 740                                }
 741                            })?
 742                        })
 743                        .shared();
 744                    server.sign_in_status = SignInStatus::SigningIn {
 745                        prompt: None,
 746                        task: task.clone(),
 747                    };
 748                    cx.notify();
 749                    task
 750                }
 751            };
 752
 753            cx.background_spawn(task.map_err(|err| anyhow!("{err:?}")))
 754        } else {
 755            // If we're downloading, wait until download is finished
 756            // If we're in a stuck state, display to the user
 757            Task::ready(Err(anyhow!("copilot hasn't started yet")))
 758        }
 759    }
 760
 761    pub fn sign_out(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
 762        self.update_sign_in_status(request::SignInStatus::NotSignedIn, cx);
 763        match &self.server {
 764            CopilotServer::Running(RunningCopilotServer { lsp: server, .. }) => {
 765                let server = server.clone();
 766                cx.background_spawn(async move {
 767                    server
 768                        .request::<request::SignOut>(request::SignOutParams {})
 769                        .await
 770                        .into_response()
 771                        .context("copilot: sign in confirm")?;
 772                    anyhow::Ok(())
 773                })
 774            }
 775            CopilotServer::Disabled => cx.background_spawn(async {
 776                clear_copilot_config_dir().await;
 777                anyhow::Ok(())
 778            }),
 779            _ => Task::ready(Err(anyhow!("copilot hasn't started yet"))),
 780        }
 781    }
 782
 783    pub fn reinstall(&mut self, cx: &mut Context<Self>) -> Shared<Task<()>> {
 784        let language_settings = all_language_settings(None, cx);
 785        let env = self.build_env(&language_settings.edit_predictions.copilot);
 786        let start_task = cx
 787            .spawn({
 788                let fs = self.fs.clone();
 789                let node_runtime = self.node_runtime.clone();
 790                let server_id = self.server_id;
 791                async move |this, cx| {
 792                    clear_copilot_dir().await;
 793                    Self::start_language_server(server_id, fs, node_runtime, env, this, false, cx)
 794                        .await
 795                }
 796            })
 797            .shared();
 798
 799        self.server = CopilotServer::Starting {
 800            task: start_task.clone(),
 801        };
 802
 803        cx.notify();
 804
 805        start_task
 806    }
 807
 808    pub fn language_server(&self) -> Option<&Arc<LanguageServer>> {
 809        if let CopilotServer::Running(server) = &self.server {
 810            Some(&server.lsp)
 811        } else {
 812            None
 813        }
 814    }
 815
 816    pub fn register_buffer(&mut self, buffer: &Entity<Buffer>, cx: &mut Context<Self>) {
 817        let weak_buffer = buffer.downgrade();
 818        self.buffers.insert(weak_buffer.clone());
 819
 820        if let CopilotServer::Running(RunningCopilotServer {
 821            lsp: server,
 822            sign_in_status: status,
 823            registered_buffers,
 824            ..
 825        }) = &mut self.server
 826        {
 827            if !matches!(status, SignInStatus::Authorized) {
 828                return;
 829            }
 830
 831            let entry = registered_buffers.entry(buffer.entity_id());
 832            if let Entry::Vacant(e) = entry {
 833                let Ok(uri) = uri_for_buffer(buffer, cx) else {
 834                    return;
 835                };
 836                let language_id = id_for_language(buffer.read(cx).language());
 837                let snapshot = buffer.read(cx).snapshot();
 838                server
 839                    .notify::<lsp::notification::DidOpenTextDocument>(
 840                        lsp::DidOpenTextDocumentParams {
 841                            text_document: lsp::TextDocumentItem {
 842                                uri: uri.clone(),
 843                                language_id: language_id.clone(),
 844                                version: 0,
 845                                text: snapshot.text(),
 846                            },
 847                        },
 848                    )
 849                    .ok();
 850
 851                e.insert(RegisteredBuffer {
 852                    uri,
 853                    language_id,
 854                    snapshot,
 855                    snapshot_version: 0,
 856                    pending_buffer_change: Task::ready(Some(())),
 857                    _subscriptions: [
 858                        cx.subscribe(buffer, |this, buffer, event, cx| {
 859                            this.handle_buffer_event(buffer, event, cx).log_err();
 860                        }),
 861                        cx.observe_release(buffer, move |this, _buffer, _cx| {
 862                            this.buffers.remove(&weak_buffer);
 863                            this.unregister_buffer(&weak_buffer);
 864                        }),
 865                    ],
 866                });
 867            }
 868        }
 869    }
 870
 871    fn handle_buffer_event(
 872        &mut self,
 873        buffer: Entity<Buffer>,
 874        event: &language::BufferEvent,
 875        cx: &mut Context<Self>,
 876    ) -> Result<()> {
 877        if let Ok(server) = self.server.as_running()
 878            && let Some(registered_buffer) = server.registered_buffers.get_mut(&buffer.entity_id())
 879        {
 880            match event {
 881                language::BufferEvent::Edited => {
 882                    drop(registered_buffer.report_changes(&buffer, cx));
 883                }
 884                language::BufferEvent::Saved => {
 885                    server
 886                        .lsp
 887                        .notify::<lsp::notification::DidSaveTextDocument>(
 888                            lsp::DidSaveTextDocumentParams {
 889                                text_document: lsp::TextDocumentIdentifier::new(
 890                                    registered_buffer.uri.clone(),
 891                                ),
 892                                text: None,
 893                            },
 894                        )
 895                        .ok();
 896                }
 897                language::BufferEvent::FileHandleChanged
 898                | language::BufferEvent::LanguageChanged(_) => {
 899                    let new_language_id = id_for_language(buffer.read(cx).language());
 900                    let Ok(new_uri) = uri_for_buffer(&buffer, cx) else {
 901                        return Ok(());
 902                    };
 903                    if new_uri != registered_buffer.uri
 904                        || new_language_id != registered_buffer.language_id
 905                    {
 906                        let old_uri = mem::replace(&mut registered_buffer.uri, new_uri);
 907                        registered_buffer.language_id = new_language_id;
 908                        server
 909                            .lsp
 910                            .notify::<lsp::notification::DidCloseTextDocument>(
 911                                lsp::DidCloseTextDocumentParams {
 912                                    text_document: lsp::TextDocumentIdentifier::new(old_uri),
 913                                },
 914                            )
 915                            .ok();
 916                        server
 917                            .lsp
 918                            .notify::<lsp::notification::DidOpenTextDocument>(
 919                                lsp::DidOpenTextDocumentParams {
 920                                    text_document: lsp::TextDocumentItem::new(
 921                                        registered_buffer.uri.clone(),
 922                                        registered_buffer.language_id.clone(),
 923                                        registered_buffer.snapshot_version,
 924                                        registered_buffer.snapshot.text(),
 925                                    ),
 926                                },
 927                            )
 928                            .ok();
 929                    }
 930                }
 931                _ => {}
 932            }
 933        }
 934
 935        Ok(())
 936    }
 937
 938    fn unregister_buffer(&mut self, buffer: &WeakEntity<Buffer>) {
 939        if let Ok(server) = self.server.as_running()
 940            && let Some(buffer) = server.registered_buffers.remove(&buffer.entity_id())
 941        {
 942            server
 943                .lsp
 944                .notify::<lsp::notification::DidCloseTextDocument>(
 945                    lsp::DidCloseTextDocumentParams {
 946                        text_document: lsp::TextDocumentIdentifier::new(buffer.uri),
 947                    },
 948                )
 949                .ok();
 950        }
 951    }
 952
 953    pub(crate) fn completions(
 954        &mut self,
 955        buffer: &Entity<Buffer>,
 956        position: Anchor,
 957        cx: &mut Context<Self>,
 958    ) -> Task<Result<Vec<CopilotEditPrediction>>> {
 959        self.register_buffer(buffer, cx);
 960
 961        let server = match self.server.as_authenticated() {
 962            Ok(server) => server,
 963            Err(error) => return Task::ready(Err(error)),
 964        };
 965        let buffer_entity = buffer.clone();
 966        let lsp = server.lsp.clone();
 967        let registered_buffer = server
 968            .registered_buffers
 969            .get_mut(&buffer.entity_id())
 970            .unwrap();
 971        let pending_snapshot = registered_buffer.report_changes(buffer, cx);
 972        let buffer = buffer.read(cx);
 973        let uri = registered_buffer.uri.clone();
 974        let position = position.to_point_utf16(buffer);
 975        let snapshot = buffer.snapshot();
 976        let settings = snapshot.settings_at(0, cx);
 977        let tab_size = settings.tab_size.get();
 978        let hard_tabs = settings.hard_tabs;
 979        drop(settings);
 980
 981        let nes_enabled = AllLanguageSettings::get_global(cx)
 982            .edit_predictions
 983            .copilot
 984            .enable_next_edit_suggestions
 985            .unwrap_or(true);
 986
 987        cx.background_spawn(async move {
 988            let (version, snapshot) = pending_snapshot.await?;
 989            let lsp_position = point_to_lsp(position);
 990
 991            let nes_fut = if nes_enabled {
 992                lsp.request::<NextEditSuggestions>(request::NextEditSuggestionsParams {
 993                    text_document: lsp::VersionedTextDocumentIdentifier {
 994                        uri: uri.clone(),
 995                        version,
 996                    },
 997                    position: lsp_position,
 998                })
 999                .map(|resp| {
1000                    resp.into_response()
1001                        .ok()
1002                        .map(|result| {
1003                            result
1004                                .edits
1005                                .into_iter()
1006                                .map(|completion| {
1007                                    let start = snapshot.clip_point_utf16(
1008                                        point_from_lsp(completion.range.start),
1009                                        Bias::Left,
1010                                    );
1011                                    let end = snapshot.clip_point_utf16(
1012                                        point_from_lsp(completion.range.end),
1013                                        Bias::Left,
1014                                    );
1015                                    CopilotEditPrediction {
1016                                        buffer: buffer_entity.clone(),
1017                                        range: snapshot.anchor_before(start)
1018                                            ..snapshot.anchor_after(end),
1019                                        text: completion.text,
1020                                        command: completion.command,
1021                                        snapshot: snapshot.clone(),
1022                                        source: CompletionSource::NextEditSuggestion,
1023                                    }
1024                                })
1025                                .collect::<Vec<_>>()
1026                        })
1027                        .unwrap_or_default()
1028                })
1029                .left_future()
1030                .fuse()
1031            } else {
1032                future::ready(Vec::<CopilotEditPrediction>::new())
1033                    .right_future()
1034                    .fuse()
1035            };
1036
1037            let inline_fut = lsp
1038                .request::<InlineCompletions>(request::InlineCompletionsParams {
1039                    text_document: lsp::VersionedTextDocumentIdentifier {
1040                        uri: uri.clone(),
1041                        version,
1042                    },
1043                    position: lsp_position,
1044                    context: InlineCompletionContext {
1045                        trigger_kind: InlineCompletionTriggerKind::Automatic,
1046                    },
1047                    formatting_options: Some(FormattingOptions {
1048                        tab_size,
1049                        insert_spaces: !hard_tabs,
1050                    }),
1051                })
1052                .map(|resp| {
1053                    resp.into_response()
1054                        .ok()
1055                        .map(|result| {
1056                            result
1057                                .items
1058                                .into_iter()
1059                                .map(|item| {
1060                                    let start = snapshot.clip_point_utf16(
1061                                        point_from_lsp(item.range.start),
1062                                        Bias::Left,
1063                                    );
1064                                    let end = snapshot.clip_point_utf16(
1065                                        point_from_lsp(item.range.end),
1066                                        Bias::Left,
1067                                    );
1068                                    CopilotEditPrediction {
1069                                        buffer: buffer_entity.clone(),
1070                                        range: snapshot.anchor_before(start)
1071                                            ..snapshot.anchor_after(end),
1072                                        text: item.insert_text,
1073                                        command: item.command,
1074                                        snapshot: snapshot.clone(),
1075                                        source: CompletionSource::InlineCompletion,
1076                                    }
1077                                })
1078                                .collect::<Vec<_>>()
1079                        })
1080                        .unwrap_or_default()
1081                })
1082                .fuse();
1083
1084            futures::pin_mut!(nes_fut, inline_fut);
1085
1086            let mut nes_result: Option<Vec<CopilotEditPrediction>> = None;
1087            let mut inline_result: Option<Vec<CopilotEditPrediction>> = None;
1088
1089            loop {
1090                select_biased! {
1091                    nes = nes_fut => {
1092                        if !nes.is_empty() {
1093                            return Ok(nes);
1094                        }
1095                        nes_result = Some(nes);
1096                    }
1097                    inline = inline_fut => {
1098                        if !inline.is_empty() {
1099                            return Ok(inline);
1100                        }
1101                        inline_result = Some(inline);
1102                    }
1103                    complete => break,
1104                }
1105
1106                if let (Some(nes), Some(inline)) = (&nes_result, &inline_result) {
1107                    return if !nes.is_empty() {
1108                        Ok(nes.clone())
1109                    } else {
1110                        Ok(inline.clone())
1111                    };
1112                }
1113            }
1114
1115            Ok(nes_result.or(inline_result).unwrap_or_default())
1116        })
1117    }
1118
1119    pub(crate) fn accept_completion(
1120        &mut self,
1121        completion: &CopilotEditPrediction,
1122        cx: &mut Context<Self>,
1123    ) -> Task<Result<()>> {
1124        let server = match self.server.as_authenticated() {
1125            Ok(server) => server,
1126            Err(error) => return Task::ready(Err(error)),
1127        };
1128        if let Some(command) = &completion.command {
1129            let request = server
1130                .lsp
1131                .request::<lsp::ExecuteCommand>(lsp::ExecuteCommandParams {
1132                    command: command.command.clone(),
1133                    arguments: command.arguments.clone().unwrap_or_default(),
1134                    ..Default::default()
1135                });
1136            cx.background_spawn(async move {
1137                request
1138                    .await
1139                    .into_response()
1140                    .context("copilot: notify accepted")?;
1141                Ok(())
1142            })
1143        } else {
1144            Task::ready(Ok(()))
1145        }
1146    }
1147
1148    pub fn status(&self) -> Status {
1149        match &self.server {
1150            CopilotServer::Starting { task } => Status::Starting { task: task.clone() },
1151            CopilotServer::Disabled => Status::Disabled,
1152            CopilotServer::Error(error) => Status::Error(error.clone()),
1153            CopilotServer::Running(RunningCopilotServer { sign_in_status, .. }) => {
1154                match sign_in_status {
1155                    SignInStatus::Authorized => Status::Authorized,
1156                    SignInStatus::Unauthorized => Status::Unauthorized,
1157                    SignInStatus::SigningIn { prompt, .. } => Status::SigningIn {
1158                        prompt: prompt.clone(),
1159                    },
1160                    SignInStatus::SignedOut {
1161                        awaiting_signing_in,
1162                    } => Status::SignedOut {
1163                        awaiting_signing_in: *awaiting_signing_in,
1164                    },
1165                }
1166            }
1167        }
1168    }
1169
1170    pub fn update_sign_in_status(
1171        &mut self,
1172        lsp_status: request::SignInStatus,
1173        cx: &mut Context<Self>,
1174    ) {
1175        self.buffers.retain(|buffer| buffer.is_upgradable());
1176
1177        if let Ok(server) = self.server.as_running() {
1178            match lsp_status {
1179                request::SignInStatus::Ok { user: Some(_) }
1180                | request::SignInStatus::MaybeOk { .. }
1181                | request::SignInStatus::AlreadySignedIn { .. } => {
1182                    server.sign_in_status = SignInStatus::Authorized;
1183                    cx.emit(Event::CopilotAuthSignedIn);
1184                    for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
1185                        if let Some(buffer) = buffer.upgrade() {
1186                            self.register_buffer(&buffer, cx);
1187                        }
1188                    }
1189                }
1190                request::SignInStatus::NotAuthorized { .. } => {
1191                    server.sign_in_status = SignInStatus::Unauthorized;
1192                    for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
1193                        self.unregister_buffer(&buffer);
1194                    }
1195                }
1196                request::SignInStatus::Ok { user: None } | request::SignInStatus::NotSignedIn => {
1197                    if !matches!(server.sign_in_status, SignInStatus::SignedOut { .. }) {
1198                        server.sign_in_status = SignInStatus::SignedOut {
1199                            awaiting_signing_in: false,
1200                        };
1201                    }
1202                    cx.emit(Event::CopilotAuthSignedOut);
1203                    for buffer in self.buffers.iter().cloned().collect::<Vec<_>>() {
1204                        self.unregister_buffer(&buffer);
1205                    }
1206                }
1207            }
1208
1209            cx.notify();
1210        }
1211    }
1212
1213    fn update_action_visibilities(&self, cx: &mut App) {
1214        let signed_in_actions = [
1215            TypeId::of::<Suggest>(),
1216            TypeId::of::<NextSuggestion>(),
1217            TypeId::of::<PreviousSuggestion>(),
1218            TypeId::of::<Reinstall>(),
1219        ];
1220        let auth_actions = [TypeId::of::<SignOut>()];
1221        let no_auth_actions = [TypeId::of::<SignIn>()];
1222        let status = self.status();
1223
1224        let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
1225        let filter = CommandPaletteFilter::global_mut(cx);
1226
1227        if is_ai_disabled {
1228            filter.hide_action_types(&signed_in_actions);
1229            filter.hide_action_types(&auth_actions);
1230            filter.hide_action_types(&no_auth_actions);
1231        } else {
1232            match status {
1233                Status::Disabled => {
1234                    filter.hide_action_types(&signed_in_actions);
1235                    filter.hide_action_types(&auth_actions);
1236                    filter.hide_action_types(&no_auth_actions);
1237                }
1238                Status::Authorized => {
1239                    filter.hide_action_types(&no_auth_actions);
1240                    filter.show_action_types(signed_in_actions.iter().chain(&auth_actions));
1241                }
1242                _ => {
1243                    filter.hide_action_types(&signed_in_actions);
1244                    filter.hide_action_types(&auth_actions);
1245                    filter.show_action_types(&no_auth_actions);
1246                }
1247            }
1248        }
1249    }
1250}
1251
1252fn id_for_language(language: Option<&Arc<Language>>) -> String {
1253    language
1254        .map(|language| language.lsp_id())
1255        .unwrap_or_else(|| "plaintext".to_string())
1256}
1257
1258fn uri_for_buffer(buffer: &Entity<Buffer>, cx: &App) -> Result<lsp::Uri, ()> {
1259    if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
1260        lsp::Uri::from_file_path(file.abs_path(cx))
1261    } else {
1262        format!("buffer://{}", buffer.entity_id())
1263            .parse()
1264            .map_err(|_| ())
1265    }
1266}
1267
1268fn notify_did_change_config_to_server(
1269    server: &Arc<LanguageServer>,
1270    cx: &mut Context<Copilot>,
1271) -> std::result::Result<(), anyhow::Error> {
1272    let copilot_settings = all_language_settings(None, cx)
1273        .edit_predictions
1274        .copilot
1275        .clone();
1276
1277    if let Some(copilot_chat) = copilot_chat::CopilotChat::global(cx) {
1278        copilot_chat.update(cx, |chat, cx| {
1279            chat.set_configuration(
1280                copilot_chat::CopilotChatConfiguration {
1281                    enterprise_uri: copilot_settings.enterprise_uri.clone(),
1282                },
1283                cx,
1284            );
1285        });
1286    }
1287
1288    let settings = json!({
1289        "http": {
1290            "proxy": copilot_settings.proxy,
1291            "proxyStrictSSL": !copilot_settings.proxy_no_verify.unwrap_or(false)
1292        },
1293        "github-enterprise": {
1294            "uri": copilot_settings.enterprise_uri
1295        }
1296    });
1297
1298    server
1299        .notify::<lsp::notification::DidChangeConfiguration>(lsp::DidChangeConfigurationParams {
1300            settings,
1301        })
1302        .ok();
1303    Ok(())
1304}
1305
1306async fn clear_copilot_dir() {
1307    remove_matching(paths::copilot_dir(), |_| true).await
1308}
1309
1310async fn clear_copilot_config_dir() {
1311    remove_matching(copilot_chat::copilot_chat_config_dir(), |_| true).await
1312}
1313
1314async fn ensure_node_version_for_copilot(node_path: &Path) -> anyhow::Result<()> {
1315    const MIN_COPILOT_NODE_VERSION: Version = Version::new(20, 8, 0);
1316
1317    log::info!("Checking Node.js version for Copilot at: {:?}", node_path);
1318
1319    let output = util::command::new_smol_command(node_path)
1320        .arg("--version")
1321        .output()
1322        .await
1323        .with_context(|| format!("checking Node.js version at {:?}", node_path))?;
1324
1325    if !output.status.success() {
1326        anyhow::bail!(
1327            "failed to run node --version for Copilot. stdout: {}, stderr: {}",
1328            String::from_utf8_lossy(&output.stdout),
1329            String::from_utf8_lossy(&output.stderr),
1330        );
1331    }
1332
1333    let version_str = String::from_utf8_lossy(&output.stdout);
1334    let version = Version::parse(version_str.trim().trim_start_matches('v'))
1335        .with_context(|| format!("parsing Node.js version from '{}'", version_str.trim()))?;
1336
1337    if version < MIN_COPILOT_NODE_VERSION {
1338        anyhow::bail!(
1339            "GitHub Copilot language server requires Node.js {MIN_COPILOT_NODE_VERSION} or later, but found {version}. \
1340            Please update your Node.js version or configure a different Node.js path in settings."
1341        );
1342    }
1343
1344    log::info!(
1345        "Node.js version {} meets Copilot requirements (>= {})",
1346        version,
1347        MIN_COPILOT_NODE_VERSION
1348    );
1349    Ok(())
1350}
1351
1352async fn get_copilot_lsp(fs: Arc<dyn Fs>, node_runtime: NodeRuntime) -> anyhow::Result<PathBuf> {
1353    const PACKAGE_NAME: &str = "@github/copilot-language-server";
1354    const SERVER_PATH: &str =
1355        "node_modules/@github/copilot-language-server/dist/language-server.js";
1356
1357    let latest_version = node_runtime
1358        .npm_package_latest_version(PACKAGE_NAME)
1359        .await?;
1360    let server_path = paths::copilot_dir().join(SERVER_PATH);
1361
1362    fs.create_dir(paths::copilot_dir()).await?;
1363
1364    let should_install = node_runtime
1365        .should_install_npm_package(
1366            PACKAGE_NAME,
1367            &server_path,
1368            paths::copilot_dir(),
1369            VersionStrategy::Latest(&latest_version),
1370        )
1371        .await;
1372    if should_install {
1373        node_runtime
1374            .npm_install_packages(
1375                paths::copilot_dir(),
1376                &[(PACKAGE_NAME, &latest_version.to_string())],
1377            )
1378            .await?;
1379    }
1380
1381    Ok(server_path)
1382}
1383
1384#[cfg(test)]
1385mod tests {
1386    use super::*;
1387    use gpui::TestAppContext;
1388    use util::{
1389        path,
1390        paths::PathStyle,
1391        rel_path::{RelPath, rel_path},
1392    };
1393
1394    #[gpui::test(iterations = 10)]
1395    async fn test_buffer_management(cx: &mut TestAppContext) {
1396        let (copilot, mut lsp) = Copilot::fake(cx);
1397
1398        let buffer_1 = cx.new(|cx| Buffer::local("Hello", cx));
1399        let buffer_1_uri: lsp::Uri = format!("buffer://{}", buffer_1.entity_id().as_u64())
1400            .parse()
1401            .unwrap();
1402        copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_1, cx));
1403        assert_eq!(
1404            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1405                .await,
1406            lsp::DidOpenTextDocumentParams {
1407                text_document: lsp::TextDocumentItem::new(
1408                    buffer_1_uri.clone(),
1409                    "plaintext".into(),
1410                    0,
1411                    "Hello".into()
1412                ),
1413            }
1414        );
1415
1416        let buffer_2 = cx.new(|cx| Buffer::local("Goodbye", cx));
1417        let buffer_2_uri: lsp::Uri = format!("buffer://{}", buffer_2.entity_id().as_u64())
1418            .parse()
1419            .unwrap();
1420        copilot.update(cx, |copilot, cx| copilot.register_buffer(&buffer_2, cx));
1421        assert_eq!(
1422            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1423                .await,
1424            lsp::DidOpenTextDocumentParams {
1425                text_document: lsp::TextDocumentItem::new(
1426                    buffer_2_uri.clone(),
1427                    "plaintext".into(),
1428                    0,
1429                    "Goodbye".into()
1430                ),
1431            }
1432        );
1433
1434        buffer_1.update(cx, |buffer, cx| buffer.edit([(5..5, " world")], None, cx));
1435        assert_eq!(
1436            lsp.receive_notification::<lsp::notification::DidChangeTextDocument>()
1437                .await,
1438            lsp::DidChangeTextDocumentParams {
1439                text_document: lsp::VersionedTextDocumentIdentifier::new(buffer_1_uri.clone(), 1),
1440                content_changes: vec![lsp::TextDocumentContentChangeEvent {
1441                    range: Some(lsp::Range::new(
1442                        lsp::Position::new(0, 5),
1443                        lsp::Position::new(0, 5)
1444                    )),
1445                    range_length: None,
1446                    text: " world".into(),
1447                }],
1448            }
1449        );
1450
1451        // Ensure updates to the file are reflected in the LSP.
1452        buffer_1.update(cx, |buffer, cx| {
1453            buffer.file_updated(
1454                Arc::new(File {
1455                    abs_path: path!("/root/child/buffer-1").into(),
1456                    path: rel_path("child/buffer-1").into(),
1457                }),
1458                cx,
1459            )
1460        });
1461        assert_eq!(
1462            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1463                .await,
1464            lsp::DidCloseTextDocumentParams {
1465                text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri),
1466            }
1467        );
1468        let buffer_1_uri = lsp::Uri::from_file_path(path!("/root/child/buffer-1")).unwrap();
1469        assert_eq!(
1470            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1471                .await,
1472            lsp::DidOpenTextDocumentParams {
1473                text_document: lsp::TextDocumentItem::new(
1474                    buffer_1_uri.clone(),
1475                    "plaintext".into(),
1476                    1,
1477                    "Hello world".into()
1478                ),
1479            }
1480        );
1481
1482        // Ensure all previously-registered buffers are closed when signing out.
1483        lsp.set_request_handler::<request::SignOut, _, _>(|_, _| async {
1484            Ok(request::SignOutResult {})
1485        });
1486        copilot
1487            .update(cx, |copilot, cx| copilot.sign_out(cx))
1488            .await
1489            .unwrap();
1490        assert_eq!(
1491            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1492                .await,
1493            lsp::DidCloseTextDocumentParams {
1494                text_document: lsp::TextDocumentIdentifier::new(buffer_1_uri.clone()),
1495            }
1496        );
1497        assert_eq!(
1498            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1499                .await,
1500            lsp::DidCloseTextDocumentParams {
1501                text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri.clone()),
1502            }
1503        );
1504
1505        // Ensure all previously-registered buffers are re-opened when signing in.
1506        lsp.set_request_handler::<request::SignIn, _, _>(|_, _| async {
1507            Ok(request::PromptUserDeviceFlow {
1508                user_code: "test-code".into(),
1509                command: lsp::Command {
1510                    title: "Sign in".into(),
1511                    command: "github.copilot.finishDeviceFlow".into(),
1512                    arguments: None,
1513                },
1514            })
1515        });
1516        copilot
1517            .update(cx, |copilot, cx| copilot.sign_in(cx))
1518            .await
1519            .unwrap();
1520
1521        // Simulate auth completion by directly updating sign-in status
1522        copilot.update(cx, |copilot, cx| {
1523            copilot.update_sign_in_status(
1524                request::SignInStatus::Ok {
1525                    user: Some("user-1".into()),
1526                },
1527                cx,
1528            );
1529        });
1530
1531        assert_eq!(
1532            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1533                .await,
1534            lsp::DidOpenTextDocumentParams {
1535                text_document: lsp::TextDocumentItem::new(
1536                    buffer_1_uri.clone(),
1537                    "plaintext".into(),
1538                    0,
1539                    "Hello world".into()
1540                ),
1541            }
1542        );
1543        assert_eq!(
1544            lsp.receive_notification::<lsp::notification::DidOpenTextDocument>()
1545                .await,
1546            lsp::DidOpenTextDocumentParams {
1547                text_document: lsp::TextDocumentItem::new(
1548                    buffer_2_uri.clone(),
1549                    "plaintext".into(),
1550                    0,
1551                    "Goodbye".into()
1552                ),
1553            }
1554        );
1555        // Dropping a buffer causes it to be closed on the LSP side as well.
1556        cx.update(|_| drop(buffer_2));
1557        assert_eq!(
1558            lsp.receive_notification::<lsp::notification::DidCloseTextDocument>()
1559                .await,
1560            lsp::DidCloseTextDocumentParams {
1561                text_document: lsp::TextDocumentIdentifier::new(buffer_2_uri),
1562            }
1563        );
1564    }
1565
1566    struct File {
1567        abs_path: PathBuf,
1568        path: Arc<RelPath>,
1569    }
1570
1571    impl language::File for File {
1572        fn as_local(&self) -> Option<&dyn language::LocalFile> {
1573            Some(self)
1574        }
1575
1576        fn disk_state(&self) -> language::DiskState {
1577            language::DiskState::Present {
1578                mtime: ::fs::MTime::from_seconds_and_nanos(100, 42),
1579            }
1580        }
1581
1582        fn path(&self) -> &Arc<RelPath> {
1583            &self.path
1584        }
1585
1586        fn path_style(&self, _: &App) -> PathStyle {
1587            PathStyle::local()
1588        }
1589
1590        fn full_path(&self, _: &App) -> PathBuf {
1591            unimplemented!()
1592        }
1593
1594        fn file_name<'a>(&'a self, _: &'a App) -> &'a str {
1595            unimplemented!()
1596        }
1597
1598        fn to_proto(&self, _: &App) -> rpc::proto::File {
1599            unimplemented!()
1600        }
1601
1602        fn worktree_id(&self, _: &App) -> settings::WorktreeId {
1603            settings::WorktreeId::from_usize(0)
1604        }
1605
1606        fn is_private(&self) -> bool {
1607            false
1608        }
1609    }
1610
1611    impl language::LocalFile for File {
1612        fn abs_path(&self, _: &App) -> PathBuf {
1613            self.abs_path.clone()
1614        }
1615
1616        fn load(&self, _: &App) -> Task<Result<String>> {
1617            unimplemented!()
1618        }
1619
1620        fn load_bytes(&self, _cx: &App) -> Task<Result<Vec<u8>>> {
1621            unimplemented!()
1622        }
1623    }
1624}
1625
1626#[cfg(test)]
1627#[ctor::ctor]
1628fn init_logger() {
1629    zlog::init_test();
1630}