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