copilot2.rs

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