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