copilot.rs

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