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