copilot.rs

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