lsp_command.rs

   1pub mod signature_help;
   2
   3use crate::{
   4    CodeAction, CompletionSource, CoreCompletion, CoreCompletionResponse, DocumentColor,
   5    DocumentHighlight, DocumentSymbol, Hover, HoverBlock, HoverBlockKind, InlayHint,
   6    InlayHintLabel, InlayHintLabelPart, InlayHintLabelPartTooltip, InlayHintTooltip, Location,
   7    LocationLink, LspAction, LspPullDiagnostics, MarkupContent, PrepareRenameResponse,
   8    ProjectTransaction, PulledDiagnostics, ResolveState,
   9    lsp_store::{LocalLspStore, LspFoldingRange, LspStore},
  10};
  11use anyhow::{Context as _, Result};
  12use async_trait::async_trait;
  13use client::proto::{self, PeerId};
  14use clock::Global;
  15use collections::HashMap;
  16use futures::future;
  17use gpui::{App, AsyncApp, Entity, SharedString, Task, prelude::FluentBuilder};
  18use language::{
  19    Anchor, Bias, Buffer, BufferSnapshot, CachedLspAdapter, CharKind, CharScopeContext,
  20    OffsetRangeExt, PointUtf16, ToOffset, ToPointUtf16, Transaction, Unclipped,
  21    language_settings::{InlayHintKind, LanguageSettings},
  22    point_from_lsp, point_to_lsp,
  23    proto::{
  24        deserialize_anchor, deserialize_anchor_range, deserialize_version, serialize_anchor,
  25        serialize_anchor_range, serialize_version,
  26    },
  27    range_from_lsp, range_to_lsp,
  28};
  29use lsp::{
  30    AdapterServerCapabilities, CodeActionKind, CodeActionOptions, CodeDescription,
  31    CompletionContext, CompletionListItemDefaultsEditRange, CompletionTriggerKind,
  32    DocumentHighlightKind, LanguageServer, LanguageServerId, LinkedEditingRangeServerCapabilities,
  33    OneOf, RenameOptions, ServerCapabilities,
  34};
  35use serde_json::Value;
  36use signature_help::{lsp_to_proto_signature, proto_to_lsp_signature};
  37use std::{
  38    cmp::Reverse, collections::hash_map, mem, ops::Range, path::Path, str::FromStr, sync::Arc,
  39};
  40use text::{BufferId, LineEnding};
  41use util::{ResultExt as _, debug_panic};
  42
  43pub use signature_help::SignatureHelp;
  44
  45fn code_action_kind_matches(requested: &lsp::CodeActionKind, actual: &lsp::CodeActionKind) -> bool {
  46    let requested_str = requested.as_str();
  47    let actual_str = actual.as_str();
  48
  49    // Exact match or hierarchical match
  50    actual_str == requested_str
  51        || actual_str
  52            .strip_prefix(requested_str)
  53            .is_some_and(|suffix| suffix.starts_with('.'))
  54}
  55
  56pub fn lsp_formatting_options(settings: &LanguageSettings) -> lsp::FormattingOptions {
  57    lsp::FormattingOptions {
  58        tab_size: settings.tab_size.into(),
  59        insert_spaces: !settings.hard_tabs,
  60        trim_trailing_whitespace: Some(settings.remove_trailing_whitespace_on_save),
  61        trim_final_newlines: Some(settings.ensure_final_newline_on_save),
  62        insert_final_newline: Some(settings.ensure_final_newline_on_save),
  63        ..lsp::FormattingOptions::default()
  64    }
  65}
  66
  67pub fn file_path_to_lsp_url(path: &Path) -> Result<lsp::Uri> {
  68    match lsp::Uri::from_file_path(path) {
  69        Ok(url) => Ok(url),
  70        Err(()) => anyhow::bail!("Invalid file path provided to LSP request: {path:?}"),
  71    }
  72}
  73
  74pub(crate) fn make_text_document_identifier(path: &Path) -> Result<lsp::TextDocumentIdentifier> {
  75    Ok(lsp::TextDocumentIdentifier {
  76        uri: file_path_to_lsp_url(path)?,
  77    })
  78}
  79
  80pub(crate) fn make_lsp_text_document_position(
  81    path: &Path,
  82    position: PointUtf16,
  83) -> Result<lsp::TextDocumentPositionParams> {
  84    Ok(lsp::TextDocumentPositionParams {
  85        text_document: make_text_document_identifier(path)?,
  86        position: point_to_lsp(position),
  87    })
  88}
  89
  90#[async_trait(?Send)]
  91pub trait LspCommand: 'static + Sized + Send + std::fmt::Debug {
  92    type Response: 'static + Default + Send + std::fmt::Debug;
  93    type LspRequest: 'static + Send + lsp::request::Request;
  94    type ProtoRequest: 'static + Send + proto::RequestMessage;
  95
  96    fn display_name(&self) -> &str;
  97
  98    fn status(&self) -> Option<String> {
  99        None
 100    }
 101
 102    fn to_lsp_params_or_response(
 103        &self,
 104        path: &Path,
 105        buffer: &Buffer,
 106        language_server: &Arc<LanguageServer>,
 107        cx: &App,
 108    ) -> Result<
 109        LspParamsOrResponse<<Self::LspRequest as lsp::request::Request>::Params, Self::Response>,
 110    > {
 111        if self.check_capabilities(language_server.adapter_server_capabilities()) {
 112            Ok(LspParamsOrResponse::Params(self.to_lsp(
 113                path,
 114                buffer,
 115                language_server,
 116                cx,
 117            )?))
 118        } else {
 119            Ok(LspParamsOrResponse::Response(Default::default()))
 120        }
 121    }
 122
 123    /// When false, `to_lsp_params_or_response` default implementation will return the default response.
 124    fn check_capabilities(&self, _: AdapterServerCapabilities) -> bool;
 125
 126    fn to_lsp(
 127        &self,
 128        path: &Path,
 129        buffer: &Buffer,
 130        language_server: &Arc<LanguageServer>,
 131        cx: &App,
 132    ) -> Result<<Self::LspRequest as lsp::request::Request>::Params>;
 133
 134    async fn response_from_lsp(
 135        self,
 136        message: <Self::LspRequest as lsp::request::Request>::Result,
 137        lsp_store: Entity<LspStore>,
 138        buffer: Entity<Buffer>,
 139        server_id: LanguageServerId,
 140        cx: AsyncApp,
 141    ) -> Result<Self::Response>;
 142
 143    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest;
 144
 145    async fn from_proto(
 146        message: Self::ProtoRequest,
 147        lsp_store: Entity<LspStore>,
 148        buffer: Entity<Buffer>,
 149        cx: AsyncApp,
 150    ) -> Result<Self>;
 151
 152    fn response_to_proto(
 153        response: Self::Response,
 154        lsp_store: &mut LspStore,
 155        peer_id: PeerId,
 156        buffer_version: &clock::Global,
 157        cx: &mut App,
 158    ) -> <Self::ProtoRequest as proto::RequestMessage>::Response;
 159
 160    async fn response_from_proto(
 161        self,
 162        message: <Self::ProtoRequest as proto::RequestMessage>::Response,
 163        lsp_store: Entity<LspStore>,
 164        buffer: Entity<Buffer>,
 165        cx: AsyncApp,
 166    ) -> Result<Self::Response>;
 167
 168    fn buffer_id_from_proto(message: &Self::ProtoRequest) -> Result<BufferId>;
 169}
 170
 171pub enum LspParamsOrResponse<P, R> {
 172    Params(P),
 173    Response(R),
 174}
 175
 176#[derive(Debug)]
 177pub(crate) struct PrepareRename {
 178    pub position: PointUtf16,
 179}
 180
 181#[derive(Debug)]
 182pub(crate) struct PerformRename {
 183    pub position: PointUtf16,
 184    pub new_name: String,
 185    pub push_to_history: bool,
 186}
 187
 188#[derive(Debug, Clone, Copy)]
 189pub struct GetDefinitions {
 190    pub position: PointUtf16,
 191}
 192
 193#[derive(Debug, Clone, Copy)]
 194pub(crate) struct GetDeclarations {
 195    pub position: PointUtf16,
 196}
 197
 198#[derive(Debug, Clone, Copy)]
 199pub(crate) struct GetTypeDefinitions {
 200    pub position: PointUtf16,
 201}
 202
 203#[derive(Debug, Clone, Copy)]
 204pub(crate) struct GetImplementations {
 205    pub position: PointUtf16,
 206}
 207
 208#[derive(Debug, Clone, Copy)]
 209pub(crate) struct GetReferences {
 210    pub position: PointUtf16,
 211}
 212
 213#[derive(Debug)]
 214pub(crate) struct GetDocumentHighlights {
 215    pub position: PointUtf16,
 216}
 217
 218#[derive(Debug, Copy, Clone)]
 219pub(crate) struct GetDocumentSymbols;
 220
 221#[derive(Clone, Debug)]
 222pub(crate) struct GetSignatureHelp {
 223    pub position: PointUtf16,
 224}
 225
 226#[derive(Clone, Debug)]
 227pub(crate) struct GetHover {
 228    pub position: PointUtf16,
 229}
 230
 231#[derive(Debug)]
 232pub(crate) struct GetCompletions {
 233    pub position: PointUtf16,
 234    pub context: CompletionContext,
 235    pub server_id: Option<lsp::LanguageServerId>,
 236}
 237
 238#[derive(Clone, Debug)]
 239pub(crate) struct GetCodeActions {
 240    pub range: Range<Anchor>,
 241    pub kinds: Option<Vec<lsp::CodeActionKind>>,
 242}
 243
 244#[derive(Debug)]
 245pub(crate) struct OnTypeFormatting {
 246    pub position: PointUtf16,
 247    pub trigger: String,
 248    pub options: lsp::FormattingOptions,
 249    pub push_to_history: bool,
 250}
 251
 252#[derive(Clone, Debug)]
 253pub(crate) struct InlayHints {
 254    pub range: Range<Anchor>,
 255}
 256
 257#[derive(Debug, Clone, Copy)]
 258pub(crate) struct SemanticTokensFull {
 259    pub for_server: Option<LanguageServerId>,
 260}
 261
 262#[derive(Debug, Clone)]
 263pub(crate) struct SemanticTokensDelta {
 264    pub previous_result_id: SharedString,
 265}
 266
 267#[derive(Debug)]
 268pub(crate) enum SemanticTokensResponse {
 269    Full {
 270        data: Vec<u32>,
 271        result_id: Option<SharedString>,
 272    },
 273    Delta {
 274        edits: Vec<SemanticTokensEdit>,
 275        result_id: Option<SharedString>,
 276    },
 277}
 278
 279impl Default for SemanticTokensResponse {
 280    fn default() -> Self {
 281        Self::Delta {
 282            edits: Vec::new(),
 283            result_id: None,
 284        }
 285    }
 286}
 287
 288#[derive(Debug)]
 289pub(crate) struct SemanticTokensEdit {
 290    pub start: u32,
 291    pub delete_count: u32,
 292    pub data: Vec<u32>,
 293}
 294
 295#[derive(Debug, Copy, Clone)]
 296pub(crate) struct GetCodeLens;
 297
 298#[derive(Debug, Copy, Clone)]
 299pub(crate) struct GetDocumentColor;
 300
 301#[derive(Debug, Copy, Clone)]
 302pub(crate) struct GetFoldingRanges;
 303
 304impl GetCodeLens {
 305    pub(crate) fn can_resolve_lens(capabilities: &ServerCapabilities) -> bool {
 306        capabilities
 307            .code_lens_provider
 308            .as_ref()
 309            .and_then(|code_lens_options| code_lens_options.resolve_provider)
 310            .unwrap_or(false)
 311    }
 312}
 313
 314#[derive(Debug)]
 315pub(crate) struct LinkedEditingRange {
 316    pub position: Anchor,
 317}
 318
 319#[derive(Clone, Debug)]
 320pub struct GetDocumentDiagnostics {
 321    /// We cannot blindly rely on server's capabilities.diagnostic_provider, as they're a singular field, whereas
 322    /// a server can register multiple diagnostic providers post-mortem.
 323    pub registration_id: Option<SharedString>,
 324    pub identifier: Option<SharedString>,
 325    pub previous_result_id: Option<SharedString>,
 326}
 327
 328#[async_trait(?Send)]
 329impl LspCommand for PrepareRename {
 330    type Response = PrepareRenameResponse;
 331    type LspRequest = lsp::request::PrepareRenameRequest;
 332    type ProtoRequest = proto::PrepareRename;
 333
 334    fn display_name(&self) -> &str {
 335        "Prepare rename"
 336    }
 337
 338    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
 339        capabilities
 340            .server_capabilities
 341            .rename_provider
 342            .is_some_and(|capability| match capability {
 343                OneOf::Left(enabled) => enabled,
 344                OneOf::Right(options) => options.prepare_provider.unwrap_or(false),
 345            })
 346    }
 347
 348    fn to_lsp_params_or_response(
 349        &self,
 350        path: &Path,
 351        buffer: &Buffer,
 352        language_server: &Arc<LanguageServer>,
 353        cx: &App,
 354    ) -> Result<LspParamsOrResponse<lsp::TextDocumentPositionParams, PrepareRenameResponse>> {
 355        let rename_provider = language_server
 356            .adapter_server_capabilities()
 357            .server_capabilities
 358            .rename_provider;
 359        match rename_provider {
 360            Some(lsp::OneOf::Right(RenameOptions {
 361                prepare_provider: Some(true),
 362                ..
 363            })) => Ok(LspParamsOrResponse::Params(self.to_lsp(
 364                path,
 365                buffer,
 366                language_server,
 367                cx,
 368            )?)),
 369            Some(lsp::OneOf::Right(_)) => Ok(LspParamsOrResponse::Response(
 370                PrepareRenameResponse::OnlyUnpreparedRenameSupported,
 371            )),
 372            Some(lsp::OneOf::Left(true)) => Ok(LspParamsOrResponse::Response(
 373                PrepareRenameResponse::OnlyUnpreparedRenameSupported,
 374            )),
 375            _ => anyhow::bail!("Rename not supported"),
 376        }
 377    }
 378
 379    fn to_lsp(
 380        &self,
 381        path: &Path,
 382        _: &Buffer,
 383        _: &Arc<LanguageServer>,
 384        _: &App,
 385    ) -> Result<lsp::TextDocumentPositionParams> {
 386        make_lsp_text_document_position(path, self.position)
 387    }
 388
 389    async fn response_from_lsp(
 390        self,
 391        message: Option<lsp::PrepareRenameResponse>,
 392        _: Entity<LspStore>,
 393        buffer: Entity<Buffer>,
 394        _: LanguageServerId,
 395        cx: AsyncApp,
 396    ) -> Result<PrepareRenameResponse> {
 397        buffer.read_with(&cx, |buffer, _| match message {
 398            Some(lsp::PrepareRenameResponse::Range(range))
 399            | Some(lsp::PrepareRenameResponse::RangeWithPlaceholder { range, .. }) => {
 400                let Range { start, end } = range_from_lsp(range);
 401                if buffer.clip_point_utf16(start, Bias::Left) == start.0
 402                    && buffer.clip_point_utf16(end, Bias::Left) == end.0
 403                {
 404                    Ok(PrepareRenameResponse::Success(
 405                        buffer.anchor_after(start)..buffer.anchor_before(end),
 406                    ))
 407                } else {
 408                    Ok(PrepareRenameResponse::InvalidPosition)
 409                }
 410            }
 411            Some(lsp::PrepareRenameResponse::DefaultBehavior { .. }) => {
 412                let snapshot = buffer.snapshot();
 413                let (range, _) = snapshot.surrounding_word(self.position, None);
 414                let range = snapshot.anchor_after(range.start)..snapshot.anchor_before(range.end);
 415                Ok(PrepareRenameResponse::Success(range))
 416            }
 417            None => Ok(PrepareRenameResponse::InvalidPosition),
 418        })
 419    }
 420
 421    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::PrepareRename {
 422        proto::PrepareRename {
 423            project_id,
 424            buffer_id: buffer.remote_id().into(),
 425            position: Some(language::proto::serialize_anchor(
 426                &buffer.anchor_before(self.position),
 427            )),
 428            version: serialize_version(&buffer.version()),
 429        }
 430    }
 431
 432    async fn from_proto(
 433        message: proto::PrepareRename,
 434        _: Entity<LspStore>,
 435        buffer: Entity<Buffer>,
 436        mut cx: AsyncApp,
 437    ) -> Result<Self> {
 438        let position = message
 439            .position
 440            .and_then(deserialize_anchor)
 441            .context("invalid position")?;
 442        buffer
 443            .update(&mut cx, |buffer, _| {
 444                buffer.wait_for_version(deserialize_version(&message.version))
 445            })
 446            .await?;
 447
 448        Ok(Self {
 449            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
 450        })
 451    }
 452
 453    fn response_to_proto(
 454        response: PrepareRenameResponse,
 455        _: &mut LspStore,
 456        _: PeerId,
 457        buffer_version: &clock::Global,
 458        _: &mut App,
 459    ) -> proto::PrepareRenameResponse {
 460        match response {
 461            PrepareRenameResponse::Success(range) => proto::PrepareRenameResponse {
 462                can_rename: true,
 463                only_unprepared_rename_supported: false,
 464                start: Some(language::proto::serialize_anchor(&range.start)),
 465                end: Some(language::proto::serialize_anchor(&range.end)),
 466                version: serialize_version(buffer_version),
 467            },
 468            PrepareRenameResponse::OnlyUnpreparedRenameSupported => proto::PrepareRenameResponse {
 469                can_rename: false,
 470                only_unprepared_rename_supported: true,
 471                start: None,
 472                end: None,
 473                version: vec![],
 474            },
 475            PrepareRenameResponse::InvalidPosition => proto::PrepareRenameResponse {
 476                can_rename: false,
 477                only_unprepared_rename_supported: false,
 478                start: None,
 479                end: None,
 480                version: vec![],
 481            },
 482        }
 483    }
 484
 485    async fn response_from_proto(
 486        self,
 487        message: proto::PrepareRenameResponse,
 488        _: Entity<LspStore>,
 489        buffer: Entity<Buffer>,
 490        mut cx: AsyncApp,
 491    ) -> Result<PrepareRenameResponse> {
 492        if message.can_rename {
 493            buffer
 494                .update(&mut cx, |buffer, _| {
 495                    buffer.wait_for_version(deserialize_version(&message.version))
 496                })
 497                .await?;
 498            if let (Some(start), Some(end)) = (
 499                message.start.and_then(deserialize_anchor),
 500                message.end.and_then(deserialize_anchor),
 501            ) {
 502                Ok(PrepareRenameResponse::Success(start..end))
 503            } else {
 504                anyhow::bail!(
 505                    "Missing start or end position in remote project PrepareRenameResponse"
 506                );
 507            }
 508        } else if message.only_unprepared_rename_supported {
 509            Ok(PrepareRenameResponse::OnlyUnpreparedRenameSupported)
 510        } else {
 511            Ok(PrepareRenameResponse::InvalidPosition)
 512        }
 513    }
 514
 515    fn buffer_id_from_proto(message: &proto::PrepareRename) -> Result<BufferId> {
 516        BufferId::new(message.buffer_id)
 517    }
 518}
 519
 520#[async_trait(?Send)]
 521impl LspCommand for PerformRename {
 522    type Response = ProjectTransaction;
 523    type LspRequest = lsp::request::Rename;
 524    type ProtoRequest = proto::PerformRename;
 525
 526    fn display_name(&self) -> &str {
 527        "Rename"
 528    }
 529
 530    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
 531        capabilities
 532            .server_capabilities
 533            .rename_provider
 534            .is_some_and(|capability| match capability {
 535                OneOf::Left(enabled) => enabled,
 536                OneOf::Right(_) => true,
 537            })
 538    }
 539
 540    fn to_lsp(
 541        &self,
 542        path: &Path,
 543        _: &Buffer,
 544        _: &Arc<LanguageServer>,
 545        _: &App,
 546    ) -> Result<lsp::RenameParams> {
 547        Ok(lsp::RenameParams {
 548            text_document_position: make_lsp_text_document_position(path, self.position)?,
 549            new_name: self.new_name.clone(),
 550            work_done_progress_params: Default::default(),
 551        })
 552    }
 553
 554    async fn response_from_lsp(
 555        self,
 556        message: Option<lsp::WorkspaceEdit>,
 557        lsp_store: Entity<LspStore>,
 558        buffer: Entity<Buffer>,
 559        server_id: LanguageServerId,
 560        mut cx: AsyncApp,
 561    ) -> Result<ProjectTransaction> {
 562        if let Some(edit) = message {
 563            let (_, lsp_server) =
 564                language_server_for_buffer(&lsp_store, &buffer, server_id, &mut cx)?;
 565            LocalLspStore::deserialize_workspace_edit(
 566                lsp_store,
 567                edit,
 568                self.push_to_history,
 569                lsp_server,
 570                &mut cx,
 571            )
 572            .await
 573        } else {
 574            Ok(ProjectTransaction::default())
 575        }
 576    }
 577
 578    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::PerformRename {
 579        proto::PerformRename {
 580            project_id,
 581            buffer_id: buffer.remote_id().into(),
 582            position: Some(language::proto::serialize_anchor(
 583                &buffer.anchor_before(self.position),
 584            )),
 585            new_name: self.new_name.clone(),
 586            version: serialize_version(&buffer.version()),
 587        }
 588    }
 589
 590    async fn from_proto(
 591        message: proto::PerformRename,
 592        _: Entity<LspStore>,
 593        buffer: Entity<Buffer>,
 594        mut cx: AsyncApp,
 595    ) -> Result<Self> {
 596        let position = message
 597            .position
 598            .and_then(deserialize_anchor)
 599            .context("invalid position")?;
 600        buffer
 601            .update(&mut cx, |buffer, _| {
 602                buffer.wait_for_version(deserialize_version(&message.version))
 603            })
 604            .await?;
 605        Ok(Self {
 606            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
 607            new_name: message.new_name,
 608            push_to_history: false,
 609        })
 610    }
 611
 612    fn response_to_proto(
 613        response: ProjectTransaction,
 614        lsp_store: &mut LspStore,
 615        peer_id: PeerId,
 616        _: &clock::Global,
 617        cx: &mut App,
 618    ) -> proto::PerformRenameResponse {
 619        let transaction = lsp_store.buffer_store().update(cx, |buffer_store, cx| {
 620            buffer_store.serialize_project_transaction_for_peer(response, peer_id, cx)
 621        });
 622        proto::PerformRenameResponse {
 623            transaction: Some(transaction),
 624        }
 625    }
 626
 627    async fn response_from_proto(
 628        self,
 629        message: proto::PerformRenameResponse,
 630        lsp_store: Entity<LspStore>,
 631        _: Entity<Buffer>,
 632        mut cx: AsyncApp,
 633    ) -> Result<ProjectTransaction> {
 634        let message = message.transaction.context("missing transaction")?;
 635        lsp_store
 636            .update(&mut cx, |lsp_store, cx| {
 637                lsp_store.buffer_store().update(cx, |buffer_store, cx| {
 638                    buffer_store.deserialize_project_transaction(message, self.push_to_history, cx)
 639                })
 640            })
 641            .await
 642    }
 643
 644    fn buffer_id_from_proto(message: &proto::PerformRename) -> Result<BufferId> {
 645        BufferId::new(message.buffer_id)
 646    }
 647}
 648
 649#[async_trait(?Send)]
 650impl LspCommand for GetDefinitions {
 651    type Response = Vec<LocationLink>;
 652    type LspRequest = lsp::request::GotoDefinition;
 653    type ProtoRequest = proto::GetDefinition;
 654
 655    fn display_name(&self) -> &str {
 656        "Get definition"
 657    }
 658
 659    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
 660        capabilities
 661            .server_capabilities
 662            .definition_provider
 663            .is_some_and(|capability| match capability {
 664                OneOf::Left(supported) => supported,
 665                OneOf::Right(_options) => true,
 666            })
 667    }
 668
 669    fn to_lsp(
 670        &self,
 671        path: &Path,
 672        _: &Buffer,
 673        _: &Arc<LanguageServer>,
 674        _: &App,
 675    ) -> Result<lsp::GotoDefinitionParams> {
 676        Ok(lsp::GotoDefinitionParams {
 677            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
 678            work_done_progress_params: Default::default(),
 679            partial_result_params: Default::default(),
 680        })
 681    }
 682
 683    async fn response_from_lsp(
 684        self,
 685        message: Option<lsp::GotoDefinitionResponse>,
 686        lsp_store: Entity<LspStore>,
 687        buffer: Entity<Buffer>,
 688        server_id: LanguageServerId,
 689        cx: AsyncApp,
 690    ) -> Result<Vec<LocationLink>> {
 691        location_links_from_lsp(message, lsp_store, buffer, server_id, cx).await
 692    }
 693
 694    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetDefinition {
 695        proto::GetDefinition {
 696            project_id,
 697            buffer_id: buffer.remote_id().into(),
 698            position: Some(language::proto::serialize_anchor(
 699                &buffer.anchor_before(self.position),
 700            )),
 701            version: serialize_version(&buffer.version()),
 702        }
 703    }
 704
 705    async fn from_proto(
 706        message: proto::GetDefinition,
 707        _: Entity<LspStore>,
 708        buffer: Entity<Buffer>,
 709        mut cx: AsyncApp,
 710    ) -> Result<Self> {
 711        let position = message
 712            .position
 713            .and_then(deserialize_anchor)
 714            .context("invalid position")?;
 715        buffer
 716            .update(&mut cx, |buffer, _| {
 717                buffer.wait_for_version(deserialize_version(&message.version))
 718            })
 719            .await?;
 720        Ok(Self {
 721            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
 722        })
 723    }
 724
 725    fn response_to_proto(
 726        response: Vec<LocationLink>,
 727        lsp_store: &mut LspStore,
 728        peer_id: PeerId,
 729        _: &clock::Global,
 730        cx: &mut App,
 731    ) -> proto::GetDefinitionResponse {
 732        let links = location_links_to_proto(response, lsp_store, peer_id, cx);
 733        proto::GetDefinitionResponse { links }
 734    }
 735
 736    async fn response_from_proto(
 737        self,
 738        message: proto::GetDefinitionResponse,
 739        lsp_store: Entity<LspStore>,
 740        _: Entity<Buffer>,
 741        cx: AsyncApp,
 742    ) -> Result<Vec<LocationLink>> {
 743        location_links_from_proto(message.links, lsp_store, cx).await
 744    }
 745
 746    fn buffer_id_from_proto(message: &proto::GetDefinition) -> Result<BufferId> {
 747        BufferId::new(message.buffer_id)
 748    }
 749}
 750
 751#[async_trait(?Send)]
 752impl LspCommand for GetDeclarations {
 753    type Response = Vec<LocationLink>;
 754    type LspRequest = lsp::request::GotoDeclaration;
 755    type ProtoRequest = proto::GetDeclaration;
 756
 757    fn display_name(&self) -> &str {
 758        "Get declaration"
 759    }
 760
 761    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
 762        capabilities
 763            .server_capabilities
 764            .declaration_provider
 765            .is_some_and(|capability| match capability {
 766                lsp::DeclarationCapability::Simple(supported) => supported,
 767                lsp::DeclarationCapability::RegistrationOptions(..) => true,
 768                lsp::DeclarationCapability::Options(..) => true,
 769            })
 770    }
 771
 772    fn to_lsp(
 773        &self,
 774        path: &Path,
 775        _: &Buffer,
 776        _: &Arc<LanguageServer>,
 777        _: &App,
 778    ) -> Result<lsp::GotoDeclarationParams> {
 779        Ok(lsp::GotoDeclarationParams {
 780            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
 781            work_done_progress_params: Default::default(),
 782            partial_result_params: Default::default(),
 783        })
 784    }
 785
 786    async fn response_from_lsp(
 787        self,
 788        message: Option<lsp::GotoDeclarationResponse>,
 789        lsp_store: Entity<LspStore>,
 790        buffer: Entity<Buffer>,
 791        server_id: LanguageServerId,
 792        cx: AsyncApp,
 793    ) -> Result<Vec<LocationLink>> {
 794        location_links_from_lsp(message, lsp_store, buffer, server_id, cx).await
 795    }
 796
 797    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetDeclaration {
 798        proto::GetDeclaration {
 799            project_id,
 800            buffer_id: buffer.remote_id().into(),
 801            position: Some(language::proto::serialize_anchor(
 802                &buffer.anchor_before(self.position),
 803            )),
 804            version: serialize_version(&buffer.version()),
 805        }
 806    }
 807
 808    async fn from_proto(
 809        message: proto::GetDeclaration,
 810        _: Entity<LspStore>,
 811        buffer: Entity<Buffer>,
 812        mut cx: AsyncApp,
 813    ) -> Result<Self> {
 814        let position = message
 815            .position
 816            .and_then(deserialize_anchor)
 817            .context("invalid position")?;
 818        buffer
 819            .update(&mut cx, |buffer, _| {
 820                buffer.wait_for_version(deserialize_version(&message.version))
 821            })
 822            .await?;
 823        Ok(Self {
 824            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
 825        })
 826    }
 827
 828    fn response_to_proto(
 829        response: Vec<LocationLink>,
 830        lsp_store: &mut LspStore,
 831        peer_id: PeerId,
 832        _: &clock::Global,
 833        cx: &mut App,
 834    ) -> proto::GetDeclarationResponse {
 835        let links = location_links_to_proto(response, lsp_store, peer_id, cx);
 836        proto::GetDeclarationResponse { links }
 837    }
 838
 839    async fn response_from_proto(
 840        self,
 841        message: proto::GetDeclarationResponse,
 842        lsp_store: Entity<LspStore>,
 843        _: Entity<Buffer>,
 844        cx: AsyncApp,
 845    ) -> Result<Vec<LocationLink>> {
 846        location_links_from_proto(message.links, lsp_store, cx).await
 847    }
 848
 849    fn buffer_id_from_proto(message: &proto::GetDeclaration) -> Result<BufferId> {
 850        BufferId::new(message.buffer_id)
 851    }
 852}
 853
 854#[async_trait(?Send)]
 855impl LspCommand for GetImplementations {
 856    type Response = Vec<LocationLink>;
 857    type LspRequest = lsp::request::GotoImplementation;
 858    type ProtoRequest = proto::GetImplementation;
 859
 860    fn display_name(&self) -> &str {
 861        "Get implementation"
 862    }
 863
 864    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
 865        capabilities
 866            .server_capabilities
 867            .implementation_provider
 868            .is_some_and(|capability| match capability {
 869                lsp::ImplementationProviderCapability::Simple(enabled) => enabled,
 870                lsp::ImplementationProviderCapability::Options(_options) => true,
 871            })
 872    }
 873
 874    fn to_lsp(
 875        &self,
 876        path: &Path,
 877        _: &Buffer,
 878        _: &Arc<LanguageServer>,
 879        _: &App,
 880    ) -> Result<lsp::GotoImplementationParams> {
 881        Ok(lsp::GotoImplementationParams {
 882            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
 883            work_done_progress_params: Default::default(),
 884            partial_result_params: Default::default(),
 885        })
 886    }
 887
 888    async fn response_from_lsp(
 889        self,
 890        message: Option<lsp::GotoImplementationResponse>,
 891        lsp_store: Entity<LspStore>,
 892        buffer: Entity<Buffer>,
 893        server_id: LanguageServerId,
 894        cx: AsyncApp,
 895    ) -> Result<Vec<LocationLink>> {
 896        location_links_from_lsp(message, lsp_store, buffer, server_id, cx).await
 897    }
 898
 899    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetImplementation {
 900        proto::GetImplementation {
 901            project_id,
 902            buffer_id: buffer.remote_id().into(),
 903            position: Some(language::proto::serialize_anchor(
 904                &buffer.anchor_before(self.position),
 905            )),
 906            version: serialize_version(&buffer.version()),
 907        }
 908    }
 909
 910    async fn from_proto(
 911        message: proto::GetImplementation,
 912        _: Entity<LspStore>,
 913        buffer: Entity<Buffer>,
 914        mut cx: AsyncApp,
 915    ) -> Result<Self> {
 916        let position = message
 917            .position
 918            .and_then(deserialize_anchor)
 919            .context("invalid position")?;
 920        buffer
 921            .update(&mut cx, |buffer, _| {
 922                buffer.wait_for_version(deserialize_version(&message.version))
 923            })
 924            .await?;
 925        Ok(Self {
 926            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
 927        })
 928    }
 929
 930    fn response_to_proto(
 931        response: Vec<LocationLink>,
 932        lsp_store: &mut LspStore,
 933        peer_id: PeerId,
 934        _: &clock::Global,
 935        cx: &mut App,
 936    ) -> proto::GetImplementationResponse {
 937        let links = location_links_to_proto(response, lsp_store, peer_id, cx);
 938        proto::GetImplementationResponse { links }
 939    }
 940
 941    async fn response_from_proto(
 942        self,
 943        message: proto::GetImplementationResponse,
 944        project: Entity<LspStore>,
 945        _: Entity<Buffer>,
 946        cx: AsyncApp,
 947    ) -> Result<Vec<LocationLink>> {
 948        location_links_from_proto(message.links, project, cx).await
 949    }
 950
 951    fn buffer_id_from_proto(message: &proto::GetImplementation) -> Result<BufferId> {
 952        BufferId::new(message.buffer_id)
 953    }
 954}
 955
 956#[async_trait(?Send)]
 957impl LspCommand for GetTypeDefinitions {
 958    type Response = Vec<LocationLink>;
 959    type LspRequest = lsp::request::GotoTypeDefinition;
 960    type ProtoRequest = proto::GetTypeDefinition;
 961
 962    fn display_name(&self) -> &str {
 963        "Get type definition"
 964    }
 965
 966    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
 967        !matches!(
 968            &capabilities.server_capabilities.type_definition_provider,
 969            None | Some(lsp::TypeDefinitionProviderCapability::Simple(false))
 970        )
 971    }
 972
 973    fn to_lsp(
 974        &self,
 975        path: &Path,
 976        _: &Buffer,
 977        _: &Arc<LanguageServer>,
 978        _: &App,
 979    ) -> Result<lsp::GotoTypeDefinitionParams> {
 980        Ok(lsp::GotoTypeDefinitionParams {
 981            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
 982            work_done_progress_params: Default::default(),
 983            partial_result_params: Default::default(),
 984        })
 985    }
 986
 987    async fn response_from_lsp(
 988        self,
 989        message: Option<lsp::GotoTypeDefinitionResponse>,
 990        project: Entity<LspStore>,
 991        buffer: Entity<Buffer>,
 992        server_id: LanguageServerId,
 993        cx: AsyncApp,
 994    ) -> Result<Vec<LocationLink>> {
 995        location_links_from_lsp(message, project, buffer, server_id, cx).await
 996    }
 997
 998    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetTypeDefinition {
 999        proto::GetTypeDefinition {
1000            project_id,
1001            buffer_id: buffer.remote_id().into(),
1002            position: Some(language::proto::serialize_anchor(
1003                &buffer.anchor_before(self.position),
1004            )),
1005            version: serialize_version(&buffer.version()),
1006        }
1007    }
1008
1009    async fn from_proto(
1010        message: proto::GetTypeDefinition,
1011        _: Entity<LspStore>,
1012        buffer: Entity<Buffer>,
1013        mut cx: AsyncApp,
1014    ) -> Result<Self> {
1015        let position = message
1016            .position
1017            .and_then(deserialize_anchor)
1018            .context("invalid position")?;
1019        buffer
1020            .update(&mut cx, |buffer, _| {
1021                buffer.wait_for_version(deserialize_version(&message.version))
1022            })
1023            .await?;
1024        Ok(Self {
1025            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
1026        })
1027    }
1028
1029    fn response_to_proto(
1030        response: Vec<LocationLink>,
1031        lsp_store: &mut LspStore,
1032        peer_id: PeerId,
1033        _: &clock::Global,
1034        cx: &mut App,
1035    ) -> proto::GetTypeDefinitionResponse {
1036        let links = location_links_to_proto(response, lsp_store, peer_id, cx);
1037        proto::GetTypeDefinitionResponse { links }
1038    }
1039
1040    async fn response_from_proto(
1041        self,
1042        message: proto::GetTypeDefinitionResponse,
1043        project: Entity<LspStore>,
1044        _: Entity<Buffer>,
1045        cx: AsyncApp,
1046    ) -> Result<Vec<LocationLink>> {
1047        location_links_from_proto(message.links, project, cx).await
1048    }
1049
1050    fn buffer_id_from_proto(message: &proto::GetTypeDefinition) -> Result<BufferId> {
1051        BufferId::new(message.buffer_id)
1052    }
1053}
1054
1055fn language_server_for_buffer(
1056    lsp_store: &Entity<LspStore>,
1057    buffer: &Entity<Buffer>,
1058    server_id: LanguageServerId,
1059    cx: &mut AsyncApp,
1060) -> Result<(Arc<CachedLspAdapter>, Arc<LanguageServer>)> {
1061    lsp_store
1062        .update(cx, |lsp_store, cx| {
1063            buffer.update(cx, |buffer, cx| {
1064                lsp_store
1065                    .language_server_for_local_buffer(buffer, server_id, cx)
1066                    .map(|(adapter, server)| (adapter.clone(), server.clone()))
1067            })
1068        })
1069        .context("no language server found for buffer")
1070}
1071
1072pub async fn location_links_from_proto(
1073    proto_links: Vec<proto::LocationLink>,
1074    lsp_store: Entity<LspStore>,
1075    mut cx: AsyncApp,
1076) -> Result<Vec<LocationLink>> {
1077    let mut links = Vec::new();
1078
1079    for link in proto_links {
1080        links.push(location_link_from_proto(link, lsp_store.clone(), &mut cx).await?)
1081    }
1082
1083    Ok(links)
1084}
1085
1086pub fn location_link_from_proto(
1087    link: proto::LocationLink,
1088    lsp_store: Entity<LspStore>,
1089    cx: &mut AsyncApp,
1090) -> Task<Result<LocationLink>> {
1091    cx.spawn(async move |cx| {
1092        let origin = match link.origin {
1093            Some(origin) => {
1094                let buffer_id = BufferId::new(origin.buffer_id)?;
1095                let buffer = lsp_store
1096                    .update(cx, |lsp_store, cx| {
1097                        lsp_store.wait_for_remote_buffer(buffer_id, cx)
1098                    })
1099                    .await?;
1100                let start = origin
1101                    .start
1102                    .and_then(deserialize_anchor)
1103                    .context("missing origin start")?;
1104                let end = origin
1105                    .end
1106                    .and_then(deserialize_anchor)
1107                    .context("missing origin end")?;
1108                buffer
1109                    .update(cx, |buffer, _| buffer.wait_for_anchors([start, end]))
1110                    .await?;
1111                Some(Location {
1112                    buffer,
1113                    range: start..end,
1114                })
1115            }
1116            None => None,
1117        };
1118
1119        let target = link.target.context("missing target")?;
1120        let buffer_id = BufferId::new(target.buffer_id)?;
1121        let buffer = lsp_store
1122            .update(cx, |lsp_store, cx| {
1123                lsp_store.wait_for_remote_buffer(buffer_id, cx)
1124            })
1125            .await?;
1126        let start = target
1127            .start
1128            .and_then(deserialize_anchor)
1129            .context("missing target start")?;
1130        let end = target
1131            .end
1132            .and_then(deserialize_anchor)
1133            .context("missing target end")?;
1134        buffer
1135            .update(cx, |buffer, _| buffer.wait_for_anchors([start, end]))
1136            .await?;
1137        let target = Location {
1138            buffer,
1139            range: start..end,
1140        };
1141        Ok(LocationLink { origin, target })
1142    })
1143}
1144
1145pub async fn location_links_from_lsp(
1146    message: Option<lsp::GotoDefinitionResponse>,
1147    lsp_store: Entity<LspStore>,
1148    buffer: Entity<Buffer>,
1149    server_id: LanguageServerId,
1150    mut cx: AsyncApp,
1151) -> Result<Vec<LocationLink>> {
1152    let message = match message {
1153        Some(message) => message,
1154        None => return Ok(Vec::new()),
1155    };
1156
1157    let mut unresolved_links = Vec::new();
1158    match message {
1159        lsp::GotoDefinitionResponse::Scalar(loc) => {
1160            unresolved_links.push((None, loc.uri, loc.range));
1161        }
1162
1163        lsp::GotoDefinitionResponse::Array(locs) => {
1164            unresolved_links.extend(locs.into_iter().map(|l| (None, l.uri, l.range)));
1165        }
1166
1167        lsp::GotoDefinitionResponse::Link(links) => {
1168            unresolved_links.extend(links.into_iter().map(|l| {
1169                (
1170                    l.origin_selection_range,
1171                    l.target_uri,
1172                    l.target_selection_range,
1173                )
1174            }));
1175        }
1176    }
1177
1178    let (_, language_server) = language_server_for_buffer(&lsp_store, &buffer, server_id, &mut cx)?;
1179    let mut definitions = Vec::new();
1180    for (origin_range, target_uri, target_range) in unresolved_links {
1181        let target_buffer_handle = lsp_store
1182            .update(&mut cx, |this, cx| {
1183                this.open_local_buffer_via_lsp(target_uri, language_server.server_id(), cx)
1184            })
1185            .await?;
1186
1187        cx.update(|cx| {
1188            let origin_location = origin_range.map(|origin_range| {
1189                let origin_buffer = buffer.read(cx);
1190                let origin_start =
1191                    origin_buffer.clip_point_utf16(point_from_lsp(origin_range.start), Bias::Left);
1192                let origin_end =
1193                    origin_buffer.clip_point_utf16(point_from_lsp(origin_range.end), Bias::Left);
1194                Location {
1195                    buffer: buffer.clone(),
1196                    range: origin_buffer.anchor_after(origin_start)
1197                        ..origin_buffer.anchor_before(origin_end),
1198                }
1199            });
1200
1201            let target_buffer = target_buffer_handle.read(cx);
1202            let target_start =
1203                target_buffer.clip_point_utf16(point_from_lsp(target_range.start), Bias::Left);
1204            let target_end =
1205                target_buffer.clip_point_utf16(point_from_lsp(target_range.end), Bias::Left);
1206            let target_location = Location {
1207                buffer: target_buffer_handle,
1208                range: target_buffer.anchor_after(target_start)
1209                    ..target_buffer.anchor_before(target_end),
1210            };
1211
1212            definitions.push(LocationLink {
1213                origin: origin_location,
1214                target: target_location,
1215            })
1216        });
1217    }
1218    Ok(definitions)
1219}
1220
1221pub async fn location_link_from_lsp(
1222    link: lsp::LocationLink,
1223    lsp_store: &Entity<LspStore>,
1224    buffer: &Entity<Buffer>,
1225    server_id: LanguageServerId,
1226    cx: &mut AsyncApp,
1227) -> Result<LocationLink> {
1228    let (_, language_server) = language_server_for_buffer(lsp_store, buffer, server_id, cx)?;
1229
1230    let (origin_range, target_uri, target_range) = (
1231        link.origin_selection_range,
1232        link.target_uri,
1233        link.target_selection_range,
1234    );
1235
1236    let target_buffer_handle = lsp_store
1237        .update(cx, |lsp_store, cx| {
1238            lsp_store.open_local_buffer_via_lsp(target_uri, language_server.server_id(), cx)
1239        })
1240        .await?;
1241
1242    Ok(cx.update(|cx| {
1243        let origin_location = origin_range.map(|origin_range| {
1244            let origin_buffer = buffer.read(cx);
1245            let origin_start =
1246                origin_buffer.clip_point_utf16(point_from_lsp(origin_range.start), Bias::Left);
1247            let origin_end =
1248                origin_buffer.clip_point_utf16(point_from_lsp(origin_range.end), Bias::Left);
1249            Location {
1250                buffer: buffer.clone(),
1251                range: origin_buffer.anchor_after(origin_start)
1252                    ..origin_buffer.anchor_before(origin_end),
1253            }
1254        });
1255
1256        let target_buffer = target_buffer_handle.read(cx);
1257        let target_start =
1258            target_buffer.clip_point_utf16(point_from_lsp(target_range.start), Bias::Left);
1259        let target_end =
1260            target_buffer.clip_point_utf16(point_from_lsp(target_range.end), Bias::Left);
1261        let target_location = Location {
1262            buffer: target_buffer_handle,
1263            range: target_buffer.anchor_after(target_start)
1264                ..target_buffer.anchor_before(target_end),
1265        };
1266
1267        LocationLink {
1268            origin: origin_location,
1269            target: target_location,
1270        }
1271    }))
1272}
1273
1274pub fn location_links_to_proto(
1275    links: Vec<LocationLink>,
1276    lsp_store: &mut LspStore,
1277    peer_id: PeerId,
1278    cx: &mut App,
1279) -> Vec<proto::LocationLink> {
1280    links
1281        .into_iter()
1282        .map(|definition| location_link_to_proto(definition, lsp_store, peer_id, cx))
1283        .collect()
1284}
1285
1286pub fn location_link_to_proto(
1287    location: LocationLink,
1288    lsp_store: &mut LspStore,
1289    peer_id: PeerId,
1290    cx: &mut App,
1291) -> proto::LocationLink {
1292    let origin = location.origin.map(|origin| {
1293        lsp_store
1294            .buffer_store()
1295            .update(cx, |buffer_store, cx| {
1296                buffer_store.create_buffer_for_peer(&origin.buffer, peer_id, cx)
1297            })
1298            .detach_and_log_err(cx);
1299
1300        let buffer_id = origin.buffer.read(cx).remote_id().into();
1301        proto::Location {
1302            start: Some(serialize_anchor(&origin.range.start)),
1303            end: Some(serialize_anchor(&origin.range.end)),
1304            buffer_id,
1305        }
1306    });
1307
1308    lsp_store
1309        .buffer_store()
1310        .update(cx, |buffer_store, cx| {
1311            buffer_store.create_buffer_for_peer(&location.target.buffer, peer_id, cx)
1312        })
1313        .detach_and_log_err(cx);
1314
1315    let buffer_id = location.target.buffer.read(cx).remote_id().into();
1316    let target = proto::Location {
1317        start: Some(serialize_anchor(&location.target.range.start)),
1318        end: Some(serialize_anchor(&location.target.range.end)),
1319        buffer_id,
1320    };
1321
1322    proto::LocationLink {
1323        origin,
1324        target: Some(target),
1325    }
1326}
1327
1328#[async_trait(?Send)]
1329impl LspCommand for GetReferences {
1330    type Response = Vec<Location>;
1331    type LspRequest = lsp::request::References;
1332    type ProtoRequest = proto::GetReferences;
1333
1334    fn display_name(&self) -> &str {
1335        "Find all references"
1336    }
1337
1338    fn status(&self) -> Option<String> {
1339        Some("Finding references...".to_owned())
1340    }
1341
1342    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
1343        match &capabilities.server_capabilities.references_provider {
1344            Some(OneOf::Left(has_support)) => *has_support,
1345            Some(OneOf::Right(_)) => true,
1346            None => false,
1347        }
1348    }
1349
1350    fn to_lsp(
1351        &self,
1352        path: &Path,
1353        _: &Buffer,
1354        _: &Arc<LanguageServer>,
1355        _: &App,
1356    ) -> Result<lsp::ReferenceParams> {
1357        Ok(lsp::ReferenceParams {
1358            text_document_position: make_lsp_text_document_position(path, self.position)?,
1359            work_done_progress_params: Default::default(),
1360            partial_result_params: Default::default(),
1361            context: lsp::ReferenceContext {
1362                include_declaration: true,
1363            },
1364        })
1365    }
1366
1367    async fn response_from_lsp(
1368        self,
1369        locations: Option<Vec<lsp::Location>>,
1370        lsp_store: Entity<LspStore>,
1371        buffer: Entity<Buffer>,
1372        server_id: LanguageServerId,
1373        mut cx: AsyncApp,
1374    ) -> Result<Vec<Location>> {
1375        let mut references = Vec::new();
1376        let (_, language_server) =
1377            language_server_for_buffer(&lsp_store, &buffer, server_id, &mut cx)?;
1378
1379        if let Some(locations) = locations {
1380            for lsp_location in locations {
1381                let target_buffer_handle = lsp_store
1382                    .update(&mut cx, |lsp_store, cx| {
1383                        lsp_store.open_local_buffer_via_lsp(
1384                            lsp_location.uri,
1385                            language_server.server_id(),
1386                            cx,
1387                        )
1388                    })
1389                    .await?;
1390
1391                target_buffer_handle
1392                    .clone()
1393                    .read_with(&cx, |target_buffer, _| {
1394                        let target_start = target_buffer
1395                            .clip_point_utf16(point_from_lsp(lsp_location.range.start), Bias::Left);
1396                        let target_end = target_buffer
1397                            .clip_point_utf16(point_from_lsp(lsp_location.range.end), Bias::Left);
1398                        references.push(Location {
1399                            buffer: target_buffer_handle,
1400                            range: target_buffer.anchor_after(target_start)
1401                                ..target_buffer.anchor_before(target_end),
1402                        });
1403                    });
1404            }
1405        }
1406
1407        Ok(references)
1408    }
1409
1410    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetReferences {
1411        proto::GetReferences {
1412            project_id,
1413            buffer_id: buffer.remote_id().into(),
1414            position: Some(language::proto::serialize_anchor(
1415                &buffer.anchor_before(self.position),
1416            )),
1417            version: serialize_version(&buffer.version()),
1418        }
1419    }
1420
1421    async fn from_proto(
1422        message: proto::GetReferences,
1423        _: Entity<LspStore>,
1424        buffer: Entity<Buffer>,
1425        mut cx: AsyncApp,
1426    ) -> Result<Self> {
1427        let position = message
1428            .position
1429            .and_then(deserialize_anchor)
1430            .context("invalid position")?;
1431        buffer
1432            .update(&mut cx, |buffer, _| {
1433                buffer.wait_for_version(deserialize_version(&message.version))
1434            })
1435            .await?;
1436        Ok(Self {
1437            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
1438        })
1439    }
1440
1441    fn response_to_proto(
1442        response: Vec<Location>,
1443        lsp_store: &mut LspStore,
1444        peer_id: PeerId,
1445        _: &clock::Global,
1446        cx: &mut App,
1447    ) -> proto::GetReferencesResponse {
1448        let locations = response
1449            .into_iter()
1450            .map(|definition| {
1451                lsp_store
1452                    .buffer_store()
1453                    .update(cx, |buffer_store, cx| {
1454                        buffer_store.create_buffer_for_peer(&definition.buffer, peer_id, cx)
1455                    })
1456                    .detach_and_log_err(cx);
1457                let buffer_id = definition.buffer.read(cx).remote_id();
1458                proto::Location {
1459                    start: Some(serialize_anchor(&definition.range.start)),
1460                    end: Some(serialize_anchor(&definition.range.end)),
1461                    buffer_id: buffer_id.into(),
1462                }
1463            })
1464            .collect();
1465        proto::GetReferencesResponse { locations }
1466    }
1467
1468    async fn response_from_proto(
1469        self,
1470        message: proto::GetReferencesResponse,
1471        project: Entity<LspStore>,
1472        _: Entity<Buffer>,
1473        mut cx: AsyncApp,
1474    ) -> Result<Vec<Location>> {
1475        let mut locations = Vec::new();
1476        for location in message.locations {
1477            let buffer_id = BufferId::new(location.buffer_id)?;
1478            let target_buffer = project
1479                .update(&mut cx, |this, cx| {
1480                    this.wait_for_remote_buffer(buffer_id, cx)
1481                })
1482                .await?;
1483            let start = location
1484                .start
1485                .and_then(deserialize_anchor)
1486                .context("missing target start")?;
1487            let end = location
1488                .end
1489                .and_then(deserialize_anchor)
1490                .context("missing target end")?;
1491            target_buffer
1492                .update(&mut cx, |buffer, _| buffer.wait_for_anchors([start, end]))
1493                .await?;
1494            locations.push(Location {
1495                buffer: target_buffer,
1496                range: start..end,
1497            })
1498        }
1499        Ok(locations)
1500    }
1501
1502    fn buffer_id_from_proto(message: &proto::GetReferences) -> Result<BufferId> {
1503        BufferId::new(message.buffer_id)
1504    }
1505}
1506
1507#[async_trait(?Send)]
1508impl LspCommand for GetDocumentHighlights {
1509    type Response = Vec<DocumentHighlight>;
1510    type LspRequest = lsp::request::DocumentHighlightRequest;
1511    type ProtoRequest = proto::GetDocumentHighlights;
1512
1513    fn display_name(&self) -> &str {
1514        "Get document highlights"
1515    }
1516
1517    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
1518        capabilities
1519            .server_capabilities
1520            .document_highlight_provider
1521            .is_some_and(|capability| match capability {
1522                OneOf::Left(supported) => supported,
1523                OneOf::Right(_options) => true,
1524            })
1525    }
1526
1527    fn to_lsp(
1528        &self,
1529        path: &Path,
1530        _: &Buffer,
1531        _: &Arc<LanguageServer>,
1532        _: &App,
1533    ) -> Result<lsp::DocumentHighlightParams> {
1534        Ok(lsp::DocumentHighlightParams {
1535            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
1536            work_done_progress_params: Default::default(),
1537            partial_result_params: Default::default(),
1538        })
1539    }
1540
1541    async fn response_from_lsp(
1542        self,
1543        lsp_highlights: Option<Vec<lsp::DocumentHighlight>>,
1544        _: Entity<LspStore>,
1545        buffer: Entity<Buffer>,
1546        _: LanguageServerId,
1547        cx: AsyncApp,
1548    ) -> Result<Vec<DocumentHighlight>> {
1549        Ok(buffer.read_with(&cx, |buffer, _| {
1550            let mut lsp_highlights = lsp_highlights.unwrap_or_default();
1551            lsp_highlights.sort_unstable_by_key(|h| (h.range.start, Reverse(h.range.end)));
1552            lsp_highlights
1553                .into_iter()
1554                .map(|lsp_highlight| {
1555                    let start = buffer
1556                        .clip_point_utf16(point_from_lsp(lsp_highlight.range.start), Bias::Left);
1557                    let end = buffer
1558                        .clip_point_utf16(point_from_lsp(lsp_highlight.range.end), Bias::Left);
1559                    DocumentHighlight {
1560                        range: buffer.anchor_after(start)..buffer.anchor_before(end),
1561                        kind: lsp_highlight
1562                            .kind
1563                            .unwrap_or(lsp::DocumentHighlightKind::READ),
1564                    }
1565                })
1566                .collect()
1567        }))
1568    }
1569
1570    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetDocumentHighlights {
1571        proto::GetDocumentHighlights {
1572            project_id,
1573            buffer_id: buffer.remote_id().into(),
1574            position: Some(language::proto::serialize_anchor(
1575                &buffer.anchor_before(self.position),
1576            )),
1577            version: serialize_version(&buffer.version()),
1578        }
1579    }
1580
1581    async fn from_proto(
1582        message: proto::GetDocumentHighlights,
1583        _: Entity<LspStore>,
1584        buffer: Entity<Buffer>,
1585        mut cx: AsyncApp,
1586    ) -> Result<Self> {
1587        let position = message
1588            .position
1589            .and_then(deserialize_anchor)
1590            .context("invalid position")?;
1591        buffer
1592            .update(&mut cx, |buffer, _| {
1593                buffer.wait_for_version(deserialize_version(&message.version))
1594            })
1595            .await?;
1596        Ok(Self {
1597            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
1598        })
1599    }
1600
1601    fn response_to_proto(
1602        response: Vec<DocumentHighlight>,
1603        _: &mut LspStore,
1604        _: PeerId,
1605        _: &clock::Global,
1606        _: &mut App,
1607    ) -> proto::GetDocumentHighlightsResponse {
1608        let highlights = response
1609            .into_iter()
1610            .map(|highlight| proto::DocumentHighlight {
1611                start: Some(serialize_anchor(&highlight.range.start)),
1612                end: Some(serialize_anchor(&highlight.range.end)),
1613                kind: match highlight.kind {
1614                    DocumentHighlightKind::TEXT => proto::document_highlight::Kind::Text.into(),
1615                    DocumentHighlightKind::WRITE => proto::document_highlight::Kind::Write.into(),
1616                    DocumentHighlightKind::READ => proto::document_highlight::Kind::Read.into(),
1617                    _ => proto::document_highlight::Kind::Text.into(),
1618                },
1619            })
1620            .collect();
1621        proto::GetDocumentHighlightsResponse { highlights }
1622    }
1623
1624    async fn response_from_proto(
1625        self,
1626        message: proto::GetDocumentHighlightsResponse,
1627        _: Entity<LspStore>,
1628        buffer: Entity<Buffer>,
1629        mut cx: AsyncApp,
1630    ) -> Result<Vec<DocumentHighlight>> {
1631        let mut highlights = Vec::new();
1632        for highlight in message.highlights {
1633            let start = highlight
1634                .start
1635                .and_then(deserialize_anchor)
1636                .context("missing target start")?;
1637            let end = highlight
1638                .end
1639                .and_then(deserialize_anchor)
1640                .context("missing target end")?;
1641            buffer
1642                .update(&mut cx, |buffer, _| buffer.wait_for_anchors([start, end]))
1643                .await?;
1644            let kind = match proto::document_highlight::Kind::from_i32(highlight.kind) {
1645                Some(proto::document_highlight::Kind::Text) => DocumentHighlightKind::TEXT,
1646                Some(proto::document_highlight::Kind::Read) => DocumentHighlightKind::READ,
1647                Some(proto::document_highlight::Kind::Write) => DocumentHighlightKind::WRITE,
1648                None => DocumentHighlightKind::TEXT,
1649            };
1650            highlights.push(DocumentHighlight {
1651                range: start..end,
1652                kind,
1653            });
1654        }
1655        Ok(highlights)
1656    }
1657
1658    fn buffer_id_from_proto(message: &proto::GetDocumentHighlights) -> Result<BufferId> {
1659        BufferId::new(message.buffer_id)
1660    }
1661}
1662
1663#[async_trait(?Send)]
1664impl LspCommand for GetDocumentSymbols {
1665    type Response = Vec<DocumentSymbol>;
1666    type LspRequest = lsp::request::DocumentSymbolRequest;
1667    type ProtoRequest = proto::GetDocumentSymbols;
1668
1669    fn display_name(&self) -> &str {
1670        "Get document symbols"
1671    }
1672
1673    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
1674        capabilities
1675            .server_capabilities
1676            .document_symbol_provider
1677            .is_some_and(|capability| match capability {
1678                OneOf::Left(supported) => supported,
1679                OneOf::Right(_options) => true,
1680            })
1681    }
1682
1683    fn to_lsp(
1684        &self,
1685        path: &Path,
1686        _: &Buffer,
1687        _: &Arc<LanguageServer>,
1688        _: &App,
1689    ) -> Result<lsp::DocumentSymbolParams> {
1690        Ok(lsp::DocumentSymbolParams {
1691            text_document: make_text_document_identifier(path)?,
1692            work_done_progress_params: Default::default(),
1693            partial_result_params: Default::default(),
1694        })
1695    }
1696
1697    async fn response_from_lsp(
1698        self,
1699        lsp_symbols: Option<lsp::DocumentSymbolResponse>,
1700        _: Entity<LspStore>,
1701        _: Entity<Buffer>,
1702        _: LanguageServerId,
1703        _: AsyncApp,
1704    ) -> Result<Vec<DocumentSymbol>> {
1705        let Some(lsp_symbols) = lsp_symbols else {
1706            return Ok(Vec::new());
1707        };
1708
1709        let symbols = match lsp_symbols {
1710            lsp::DocumentSymbolResponse::Flat(symbol_information) => symbol_information
1711                .into_iter()
1712                .map(|lsp_symbol| DocumentSymbol {
1713                    name: lsp_symbol.name,
1714                    kind: lsp_symbol.kind,
1715                    range: range_from_lsp(lsp_symbol.location.range),
1716                    selection_range: range_from_lsp(lsp_symbol.location.range),
1717                    children: Vec::new(),
1718                })
1719                .collect(),
1720            lsp::DocumentSymbolResponse::Nested(nested_responses) => {
1721                fn convert_symbol(lsp_symbol: lsp::DocumentSymbol) -> DocumentSymbol {
1722                    DocumentSymbol {
1723                        name: lsp_symbol.name,
1724                        kind: lsp_symbol.kind,
1725                        range: range_from_lsp(lsp_symbol.range),
1726                        selection_range: range_from_lsp(lsp_symbol.selection_range),
1727                        children: lsp_symbol
1728                            .children
1729                            .map(|children| {
1730                                children.into_iter().map(convert_symbol).collect::<Vec<_>>()
1731                            })
1732                            .unwrap_or_default(),
1733                    }
1734                }
1735                nested_responses.into_iter().map(convert_symbol).collect()
1736            }
1737        };
1738        Ok(symbols)
1739    }
1740
1741    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetDocumentSymbols {
1742        proto::GetDocumentSymbols {
1743            project_id,
1744            buffer_id: buffer.remote_id().into(),
1745            version: serialize_version(&buffer.version()),
1746        }
1747    }
1748
1749    async fn from_proto(
1750        message: proto::GetDocumentSymbols,
1751        _: Entity<LspStore>,
1752        buffer: Entity<Buffer>,
1753        mut cx: AsyncApp,
1754    ) -> Result<Self> {
1755        buffer
1756            .update(&mut cx, |buffer, _| {
1757                buffer.wait_for_version(deserialize_version(&message.version))
1758            })
1759            .await?;
1760        Ok(Self)
1761    }
1762
1763    fn response_to_proto(
1764        response: Vec<DocumentSymbol>,
1765        _: &mut LspStore,
1766        _: PeerId,
1767        _: &clock::Global,
1768        _: &mut App,
1769    ) -> proto::GetDocumentSymbolsResponse {
1770        let symbols = response
1771            .into_iter()
1772            .map(|symbol| {
1773                fn convert_symbol_to_proto(symbol: DocumentSymbol) -> proto::DocumentSymbol {
1774                    proto::DocumentSymbol {
1775                        name: symbol.name.clone(),
1776                        kind: unsafe { mem::transmute::<lsp::SymbolKind, i32>(symbol.kind) },
1777                        start: Some(proto::PointUtf16 {
1778                            row: symbol.range.start.0.row,
1779                            column: symbol.range.start.0.column,
1780                        }),
1781                        end: Some(proto::PointUtf16 {
1782                            row: symbol.range.end.0.row,
1783                            column: symbol.range.end.0.column,
1784                        }),
1785                        selection_start: Some(proto::PointUtf16 {
1786                            row: symbol.selection_range.start.0.row,
1787                            column: symbol.selection_range.start.0.column,
1788                        }),
1789                        selection_end: Some(proto::PointUtf16 {
1790                            row: symbol.selection_range.end.0.row,
1791                            column: symbol.selection_range.end.0.column,
1792                        }),
1793                        children: symbol
1794                            .children
1795                            .into_iter()
1796                            .map(convert_symbol_to_proto)
1797                            .collect(),
1798                    }
1799                }
1800                convert_symbol_to_proto(symbol)
1801            })
1802            .collect::<Vec<_>>();
1803
1804        proto::GetDocumentSymbolsResponse { symbols }
1805    }
1806
1807    async fn response_from_proto(
1808        self,
1809        message: proto::GetDocumentSymbolsResponse,
1810        _: Entity<LspStore>,
1811        _: Entity<Buffer>,
1812        _: AsyncApp,
1813    ) -> Result<Vec<DocumentSymbol>> {
1814        let mut symbols = Vec::with_capacity(message.symbols.len());
1815        for serialized_symbol in message.symbols {
1816            fn deserialize_symbol_with_children(
1817                serialized_symbol: proto::DocumentSymbol,
1818            ) -> Result<DocumentSymbol> {
1819                let kind =
1820                    unsafe { mem::transmute::<i32, lsp::SymbolKind>(serialized_symbol.kind) };
1821
1822                let start = serialized_symbol.start.context("invalid start")?;
1823                let end = serialized_symbol.end.context("invalid end")?;
1824
1825                let selection_start = serialized_symbol
1826                    .selection_start
1827                    .context("invalid selection start")?;
1828                let selection_end = serialized_symbol
1829                    .selection_end
1830                    .context("invalid selection end")?;
1831
1832                Ok(DocumentSymbol {
1833                    name: serialized_symbol.name,
1834                    kind,
1835                    range: Unclipped(PointUtf16::new(start.row, start.column))
1836                        ..Unclipped(PointUtf16::new(end.row, end.column)),
1837                    selection_range: Unclipped(PointUtf16::new(
1838                        selection_start.row,
1839                        selection_start.column,
1840                    ))
1841                        ..Unclipped(PointUtf16::new(selection_end.row, selection_end.column)),
1842                    children: serialized_symbol
1843                        .children
1844                        .into_iter()
1845                        .filter_map(|symbol| deserialize_symbol_with_children(symbol).ok())
1846                        .collect::<Vec<_>>(),
1847                })
1848            }
1849
1850            symbols.push(deserialize_symbol_with_children(serialized_symbol)?);
1851        }
1852
1853        Ok(symbols)
1854    }
1855
1856    fn buffer_id_from_proto(message: &proto::GetDocumentSymbols) -> Result<BufferId> {
1857        BufferId::new(message.buffer_id)
1858    }
1859}
1860
1861#[async_trait(?Send)]
1862impl LspCommand for GetSignatureHelp {
1863    type Response = Option<SignatureHelp>;
1864    type LspRequest = lsp::SignatureHelpRequest;
1865    type ProtoRequest = proto::GetSignatureHelp;
1866
1867    fn display_name(&self) -> &str {
1868        "Get signature help"
1869    }
1870
1871    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
1872        capabilities
1873            .server_capabilities
1874            .signature_help_provider
1875            .is_some()
1876    }
1877
1878    fn to_lsp(
1879        &self,
1880        path: &Path,
1881        _: &Buffer,
1882        _: &Arc<LanguageServer>,
1883        _cx: &App,
1884    ) -> Result<lsp::SignatureHelpParams> {
1885        Ok(lsp::SignatureHelpParams {
1886            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
1887            context: None,
1888            work_done_progress_params: Default::default(),
1889        })
1890    }
1891
1892    async fn response_from_lsp(
1893        self,
1894        message: Option<lsp::SignatureHelp>,
1895        lsp_store: Entity<LspStore>,
1896        _: Entity<Buffer>,
1897        id: LanguageServerId,
1898        cx: AsyncApp,
1899    ) -> Result<Self::Response> {
1900        let Some(message) = message else {
1901            return Ok(None);
1902        };
1903        Ok(cx.update(|cx| {
1904            SignatureHelp::new(
1905                message,
1906                Some(lsp_store.read(cx).languages.clone()),
1907                Some(id),
1908                cx,
1909            )
1910        }))
1911    }
1912
1913    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest {
1914        let offset = buffer.point_utf16_to_offset(self.position);
1915        proto::GetSignatureHelp {
1916            project_id,
1917            buffer_id: buffer.remote_id().to_proto(),
1918            position: Some(serialize_anchor(&buffer.anchor_after(offset))),
1919            version: serialize_version(&buffer.version()),
1920        }
1921    }
1922
1923    async fn from_proto(
1924        payload: Self::ProtoRequest,
1925        _: Entity<LspStore>,
1926        buffer: Entity<Buffer>,
1927        mut cx: AsyncApp,
1928    ) -> Result<Self> {
1929        buffer
1930            .update(&mut cx, |buffer, _| {
1931                buffer.wait_for_version(deserialize_version(&payload.version))
1932            })
1933            .await
1934            .with_context(|| format!("waiting for version for buffer {}", buffer.entity_id()))?;
1935        let buffer_snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
1936        Ok(Self {
1937            position: payload
1938                .position
1939                .and_then(deserialize_anchor)
1940                .context("invalid position")?
1941                .to_point_utf16(&buffer_snapshot),
1942        })
1943    }
1944
1945    fn response_to_proto(
1946        response: Self::Response,
1947        _: &mut LspStore,
1948        _: PeerId,
1949        _: &Global,
1950        _: &mut App,
1951    ) -> proto::GetSignatureHelpResponse {
1952        proto::GetSignatureHelpResponse {
1953            signature_help: response
1954                .map(|signature_help| lsp_to_proto_signature(signature_help.original_data)),
1955        }
1956    }
1957
1958    async fn response_from_proto(
1959        self,
1960        response: proto::GetSignatureHelpResponse,
1961        lsp_store: Entity<LspStore>,
1962        _: Entity<Buffer>,
1963        cx: AsyncApp,
1964    ) -> Result<Self::Response> {
1965        Ok(cx.update(|cx| {
1966            response
1967                .signature_help
1968                .map(proto_to_lsp_signature)
1969                .and_then(|signature| {
1970                    SignatureHelp::new(
1971                        signature,
1972                        Some(lsp_store.read(cx).languages.clone()),
1973                        None,
1974                        cx,
1975                    )
1976                })
1977        }))
1978    }
1979
1980    fn buffer_id_from_proto(message: &Self::ProtoRequest) -> Result<BufferId> {
1981        BufferId::new(message.buffer_id)
1982    }
1983}
1984
1985#[async_trait(?Send)]
1986impl LspCommand for GetHover {
1987    type Response = Option<Hover>;
1988    type LspRequest = lsp::request::HoverRequest;
1989    type ProtoRequest = proto::GetHover;
1990
1991    fn display_name(&self) -> &str {
1992        "Get hover"
1993    }
1994
1995    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
1996        match capabilities.server_capabilities.hover_provider {
1997            Some(lsp::HoverProviderCapability::Simple(enabled)) => enabled,
1998            Some(lsp::HoverProviderCapability::Options(_)) => true,
1999            None => false,
2000        }
2001    }
2002
2003    fn to_lsp(
2004        &self,
2005        path: &Path,
2006        _: &Buffer,
2007        _: &Arc<LanguageServer>,
2008        _: &App,
2009    ) -> Result<lsp::HoverParams> {
2010        Ok(lsp::HoverParams {
2011            text_document_position_params: make_lsp_text_document_position(path, self.position)?,
2012            work_done_progress_params: Default::default(),
2013        })
2014    }
2015
2016    async fn response_from_lsp(
2017        self,
2018        message: Option<lsp::Hover>,
2019        _: Entity<LspStore>,
2020        buffer: Entity<Buffer>,
2021        _: LanguageServerId,
2022        cx: AsyncApp,
2023    ) -> Result<Self::Response> {
2024        let Some(hover) = message else {
2025            return Ok(None);
2026        };
2027
2028        let (language, range) = buffer.read_with(&cx, |buffer, _| {
2029            (
2030                buffer.language().cloned(),
2031                hover.range.map(|range| {
2032                    let token_start =
2033                        buffer.clip_point_utf16(point_from_lsp(range.start), Bias::Left);
2034                    let token_end = buffer.clip_point_utf16(point_from_lsp(range.end), Bias::Left);
2035                    buffer.anchor_after(token_start)..buffer.anchor_before(token_end)
2036                }),
2037            )
2038        });
2039
2040        fn hover_blocks_from_marked_string(marked_string: lsp::MarkedString) -> Option<HoverBlock> {
2041            let block = match marked_string {
2042                lsp::MarkedString::String(content) => HoverBlock {
2043                    text: content,
2044                    kind: HoverBlockKind::Markdown,
2045                },
2046                lsp::MarkedString::LanguageString(lsp::LanguageString { language, value }) => {
2047                    HoverBlock {
2048                        text: value,
2049                        kind: HoverBlockKind::Code { language },
2050                    }
2051                }
2052            };
2053            if block.text.is_empty() {
2054                None
2055            } else {
2056                Some(block)
2057            }
2058        }
2059
2060        let contents = match hover.contents {
2061            lsp::HoverContents::Scalar(marked_string) => {
2062                hover_blocks_from_marked_string(marked_string)
2063                    .into_iter()
2064                    .collect()
2065            }
2066            lsp::HoverContents::Array(marked_strings) => marked_strings
2067                .into_iter()
2068                .filter_map(hover_blocks_from_marked_string)
2069                .collect(),
2070            lsp::HoverContents::Markup(markup_content) => vec![HoverBlock {
2071                text: markup_content.value,
2072                kind: if markup_content.kind == lsp::MarkupKind::Markdown {
2073                    HoverBlockKind::Markdown
2074                } else {
2075                    HoverBlockKind::PlainText
2076                },
2077            }],
2078        };
2079
2080        Ok(Some(Hover {
2081            contents,
2082            range,
2083            language,
2084        }))
2085    }
2086
2087    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest {
2088        proto::GetHover {
2089            project_id,
2090            buffer_id: buffer.remote_id().into(),
2091            position: Some(language::proto::serialize_anchor(
2092                &buffer.anchor_before(self.position),
2093            )),
2094            version: serialize_version(&buffer.version),
2095        }
2096    }
2097
2098    async fn from_proto(
2099        message: Self::ProtoRequest,
2100        _: Entity<LspStore>,
2101        buffer: Entity<Buffer>,
2102        mut cx: AsyncApp,
2103    ) -> Result<Self> {
2104        let position = message
2105            .position
2106            .and_then(deserialize_anchor)
2107            .context("invalid position")?;
2108        buffer
2109            .update(&mut cx, |buffer, _| {
2110                buffer.wait_for_version(deserialize_version(&message.version))
2111            })
2112            .await?;
2113        Ok(Self {
2114            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
2115        })
2116    }
2117
2118    fn response_to_proto(
2119        response: Self::Response,
2120        _: &mut LspStore,
2121        _: PeerId,
2122        _: &clock::Global,
2123        _: &mut App,
2124    ) -> proto::GetHoverResponse {
2125        if let Some(response) = response {
2126            let (start, end) = if let Some(range) = response.range {
2127                (
2128                    Some(language::proto::serialize_anchor(&range.start)),
2129                    Some(language::proto::serialize_anchor(&range.end)),
2130                )
2131            } else {
2132                (None, None)
2133            };
2134
2135            let contents = response
2136                .contents
2137                .into_iter()
2138                .map(|block| proto::HoverBlock {
2139                    text: block.text,
2140                    is_markdown: block.kind == HoverBlockKind::Markdown,
2141                    language: if let HoverBlockKind::Code { language } = block.kind {
2142                        Some(language)
2143                    } else {
2144                        None
2145                    },
2146                })
2147                .collect();
2148
2149            proto::GetHoverResponse {
2150                start,
2151                end,
2152                contents,
2153            }
2154        } else {
2155            proto::GetHoverResponse {
2156                start: None,
2157                end: None,
2158                contents: Vec::new(),
2159            }
2160        }
2161    }
2162
2163    async fn response_from_proto(
2164        self,
2165        message: proto::GetHoverResponse,
2166        _: Entity<LspStore>,
2167        buffer: Entity<Buffer>,
2168        mut cx: AsyncApp,
2169    ) -> Result<Self::Response> {
2170        let contents: Vec<_> = message
2171            .contents
2172            .into_iter()
2173            .map(|block| HoverBlock {
2174                text: block.text,
2175                kind: if let Some(language) = block.language {
2176                    HoverBlockKind::Code { language }
2177                } else if block.is_markdown {
2178                    HoverBlockKind::Markdown
2179                } else {
2180                    HoverBlockKind::PlainText
2181                },
2182            })
2183            .collect();
2184        if contents.is_empty() {
2185            return Ok(None);
2186        }
2187
2188        let language = buffer.read_with(&cx, |buffer, _| buffer.language().cloned());
2189        let range = if let (Some(start), Some(end)) = (message.start, message.end) {
2190            language::proto::deserialize_anchor(start)
2191                .and_then(|start| language::proto::deserialize_anchor(end).map(|end| start..end))
2192        } else {
2193            None
2194        };
2195        if let Some(range) = range.as_ref() {
2196            buffer
2197                .update(&mut cx, |buffer, _| {
2198                    buffer.wait_for_anchors([range.start, range.end])
2199                })
2200                .await?;
2201        }
2202
2203        Ok(Some(Hover {
2204            contents,
2205            range,
2206            language,
2207        }))
2208    }
2209
2210    fn buffer_id_from_proto(message: &Self::ProtoRequest) -> Result<BufferId> {
2211        BufferId::new(message.buffer_id)
2212    }
2213}
2214
2215impl GetCompletions {
2216    pub fn can_resolve_completions(capabilities: &lsp::ServerCapabilities) -> bool {
2217        capabilities
2218            .completion_provider
2219            .as_ref()
2220            .and_then(|options| options.resolve_provider)
2221            .unwrap_or(false)
2222    }
2223}
2224
2225#[async_trait(?Send)]
2226impl LspCommand for GetCompletions {
2227    type Response = CoreCompletionResponse;
2228    type LspRequest = lsp::request::Completion;
2229    type ProtoRequest = proto::GetCompletions;
2230
2231    fn display_name(&self) -> &str {
2232        "Get completion"
2233    }
2234
2235    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
2236        capabilities
2237            .server_capabilities
2238            .completion_provider
2239            .is_some()
2240    }
2241
2242    fn to_lsp(
2243        &self,
2244        path: &Path,
2245        _: &Buffer,
2246        _: &Arc<LanguageServer>,
2247        _: &App,
2248    ) -> Result<lsp::CompletionParams> {
2249        Ok(lsp::CompletionParams {
2250            text_document_position: make_lsp_text_document_position(path, self.position)?,
2251            context: Some(self.context.clone()),
2252            work_done_progress_params: Default::default(),
2253            partial_result_params: Default::default(),
2254        })
2255    }
2256
2257    async fn response_from_lsp(
2258        self,
2259        completions: Option<lsp::CompletionResponse>,
2260        lsp_store: Entity<LspStore>,
2261        buffer: Entity<Buffer>,
2262        server_id: LanguageServerId,
2263        mut cx: AsyncApp,
2264    ) -> Result<Self::Response> {
2265        let mut response_list = None;
2266        let (mut completions, mut is_incomplete) = if let Some(completions) = completions {
2267            match completions {
2268                lsp::CompletionResponse::Array(completions) => (completions, false),
2269                lsp::CompletionResponse::List(mut list) => {
2270                    let is_incomplete = list.is_incomplete;
2271                    let items = std::mem::take(&mut list.items);
2272                    response_list = Some(list);
2273                    (items, is_incomplete)
2274                }
2275            }
2276        } else {
2277            (Vec::new(), false)
2278        };
2279
2280        let unfiltered_completions_count = completions.len();
2281
2282        let language_server_adapter = lsp_store
2283            .read_with(&cx, |lsp_store, _| {
2284                lsp_store.language_server_adapter_for_id(server_id)
2285            })
2286            .with_context(|| format!("no language server with id {server_id}"))?;
2287
2288        let lsp_defaults = response_list
2289            .as_ref()
2290            .and_then(|list| list.item_defaults.clone())
2291            .map(Arc::new);
2292
2293        let mut completion_edits = Vec::new();
2294        buffer.update(&mut cx, |buffer, _cx| {
2295            let snapshot = buffer.snapshot();
2296            let clipped_position = buffer.clip_point_utf16(Unclipped(self.position), Bias::Left);
2297
2298            let mut range_for_token = None;
2299            completions.retain(|lsp_completion| {
2300                let lsp_edit = lsp_completion.text_edit.clone().or_else(|| {
2301                    let default_text_edit = lsp_defaults.as_deref()?.edit_range.as_ref()?;
2302                    let new_text = lsp_completion
2303                        .text_edit_text
2304                        .as_ref()
2305                        .unwrap_or(&lsp_completion.label)
2306                        .clone();
2307                    match default_text_edit {
2308                        CompletionListItemDefaultsEditRange::Range(range) => {
2309                            Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
2310                                range: *range,
2311                                new_text,
2312                            }))
2313                        }
2314                        CompletionListItemDefaultsEditRange::InsertAndReplace {
2315                            insert,
2316                            replace,
2317                        } => Some(lsp::CompletionTextEdit::InsertAndReplace(
2318                            lsp::InsertReplaceEdit {
2319                                new_text,
2320                                insert: *insert,
2321                                replace: *replace,
2322                            },
2323                        )),
2324                    }
2325                });
2326
2327                let edit = match lsp_edit {
2328                    // If the language server provides a range to overwrite, then
2329                    // check that the range is valid.
2330                    Some(completion_text_edit) => {
2331                        match parse_completion_text_edit(&completion_text_edit, &snapshot) {
2332                            Some(edit) => edit,
2333                            None => return false,
2334                        }
2335                    }
2336                    // If the language server does not provide a range, then infer
2337                    // the range based on the syntax tree.
2338                    None => {
2339                        if self.position != clipped_position {
2340                            log::info!("completion out of expected range ");
2341                            return false;
2342                        }
2343
2344                        let default_edit_range = lsp_defaults.as_ref().and_then(|lsp_defaults| {
2345                            lsp_defaults
2346                                .edit_range
2347                                .as_ref()
2348                                .and_then(|range| match range {
2349                                    CompletionListItemDefaultsEditRange::Range(r) => Some(r),
2350                                    _ => None,
2351                                })
2352                        });
2353
2354                        let range = if let Some(range) = default_edit_range {
2355                            let range = range_from_lsp(*range);
2356                            let start = snapshot.clip_point_utf16(range.start, Bias::Left);
2357                            let end = snapshot.clip_point_utf16(range.end, Bias::Left);
2358                            if start != range.start.0 || end != range.end.0 {
2359                                log::info!("completion out of expected range");
2360                                return false;
2361                            }
2362
2363                            snapshot.anchor_before(start)..snapshot.anchor_after(end)
2364                        } else {
2365                            range_for_token
2366                                .get_or_insert_with(|| {
2367                                    let offset = self.position.to_offset(&snapshot);
2368                                    let (range, kind) = snapshot.surrounding_word(
2369                                        offset,
2370                                        Some(CharScopeContext::Completion),
2371                                    );
2372                                    let range = if kind == Some(CharKind::Word) {
2373                                        range
2374                                    } else {
2375                                        offset..offset
2376                                    };
2377
2378                                    snapshot.anchor_before(range.start)
2379                                        ..snapshot.anchor_after(range.end)
2380                                })
2381                                .clone()
2382                        };
2383
2384                        // We already know text_edit is None here
2385                        let text = lsp_completion
2386                            .insert_text
2387                            .as_ref()
2388                            .unwrap_or(&lsp_completion.label)
2389                            .clone();
2390
2391                        ParsedCompletionEdit {
2392                            replace_range: range,
2393                            insert_range: None,
2394                            new_text: text,
2395                        }
2396                    }
2397                };
2398
2399                completion_edits.push(edit);
2400                true
2401            });
2402        });
2403
2404        // If completions were filtered out due to errors that may be transient, mark the result
2405        // incomplete so that it is re-queried.
2406        if unfiltered_completions_count != completions.len() {
2407            is_incomplete = true;
2408        }
2409
2410        language_server_adapter
2411            .process_completions(&mut completions)
2412            .await;
2413
2414        let completions = completions
2415            .into_iter()
2416            .zip(completion_edits)
2417            .map(|(mut lsp_completion, mut edit)| {
2418                LineEnding::normalize(&mut edit.new_text);
2419                if lsp_completion.data.is_none()
2420                    && let Some(default_data) = lsp_defaults
2421                        .as_ref()
2422                        .and_then(|item_defaults| item_defaults.data.clone())
2423                {
2424                    // Servers (e.g. JDTLS) prefer unchanged completions, when resolving the items later,
2425                    // so we do not insert the defaults here, but `data` is needed for resolving, so this is an exception.
2426                    lsp_completion.data = Some(default_data);
2427                }
2428                CoreCompletion {
2429                    replace_range: edit.replace_range,
2430                    new_text: edit.new_text,
2431                    source: CompletionSource::Lsp {
2432                        insert_range: edit.insert_range,
2433                        server_id,
2434                        lsp_completion: Box::new(lsp_completion),
2435                        lsp_defaults: lsp_defaults.clone(),
2436                        resolved: false,
2437                    },
2438                }
2439            })
2440            .collect();
2441
2442        Ok(CoreCompletionResponse {
2443            completions,
2444            is_incomplete,
2445        })
2446    }
2447
2448    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetCompletions {
2449        let anchor = buffer.anchor_after(self.position);
2450        proto::GetCompletions {
2451            project_id,
2452            buffer_id: buffer.remote_id().into(),
2453            position: Some(language::proto::serialize_anchor(&anchor)),
2454            version: serialize_version(&buffer.version()),
2455            server_id: self.server_id.map(|id| id.to_proto()),
2456        }
2457    }
2458
2459    async fn from_proto(
2460        message: proto::GetCompletions,
2461        _: Entity<LspStore>,
2462        buffer: Entity<Buffer>,
2463        mut cx: AsyncApp,
2464    ) -> Result<Self> {
2465        let version = deserialize_version(&message.version);
2466        buffer
2467            .update(&mut cx, |buffer, _| buffer.wait_for_version(version))
2468            .await?;
2469        let position = message
2470            .position
2471            .and_then(language::proto::deserialize_anchor)
2472            .map(|p| {
2473                buffer.read_with(&cx, |buffer, _| {
2474                    buffer.clip_point_utf16(Unclipped(p.to_point_utf16(buffer)), Bias::Left)
2475                })
2476            })
2477            .context("invalid position")?;
2478        Ok(Self {
2479            position,
2480            context: CompletionContext {
2481                trigger_kind: CompletionTriggerKind::INVOKED,
2482                trigger_character: None,
2483            },
2484            server_id: message
2485                .server_id
2486                .map(|id| lsp::LanguageServerId::from_proto(id)),
2487        })
2488    }
2489
2490    fn response_to_proto(
2491        response: CoreCompletionResponse,
2492        _: &mut LspStore,
2493        _: PeerId,
2494        buffer_version: &clock::Global,
2495        _: &mut App,
2496    ) -> proto::GetCompletionsResponse {
2497        proto::GetCompletionsResponse {
2498            completions: response
2499                .completions
2500                .iter()
2501                .map(LspStore::serialize_completion)
2502                .collect(),
2503            version: serialize_version(buffer_version),
2504            can_reuse: !response.is_incomplete,
2505        }
2506    }
2507
2508    async fn response_from_proto(
2509        self,
2510        message: proto::GetCompletionsResponse,
2511        _project: Entity<LspStore>,
2512        buffer: Entity<Buffer>,
2513        mut cx: AsyncApp,
2514    ) -> Result<Self::Response> {
2515        buffer
2516            .update(&mut cx, |buffer, _| {
2517                buffer.wait_for_version(deserialize_version(&message.version))
2518            })
2519            .await?;
2520
2521        let completions = message
2522            .completions
2523            .into_iter()
2524            .map(LspStore::deserialize_completion)
2525            .collect::<Result<Vec<_>>>()?;
2526
2527        Ok(CoreCompletionResponse {
2528            completions,
2529            is_incomplete: !message.can_reuse,
2530        })
2531    }
2532
2533    fn buffer_id_from_proto(message: &proto::GetCompletions) -> Result<BufferId> {
2534        BufferId::new(message.buffer_id)
2535    }
2536}
2537
2538pub struct ParsedCompletionEdit {
2539    pub replace_range: Range<Anchor>,
2540    pub insert_range: Option<Range<Anchor>>,
2541    pub new_text: String,
2542}
2543
2544pub(crate) fn parse_completion_text_edit(
2545    edit: &lsp::CompletionTextEdit,
2546    snapshot: &BufferSnapshot,
2547) -> Option<ParsedCompletionEdit> {
2548    let (replace_range, insert_range, new_text) = match edit {
2549        lsp::CompletionTextEdit::Edit(edit) => (edit.range, None, &edit.new_text),
2550        lsp::CompletionTextEdit::InsertAndReplace(edit) => {
2551            (edit.replace, Some(edit.insert), &edit.new_text)
2552        }
2553    };
2554
2555    let replace_range = {
2556        let range = range_from_lsp(replace_range);
2557        let start = snapshot.clip_point_utf16(range.start, Bias::Left);
2558        let end = snapshot.clip_point_utf16(range.end, Bias::Left);
2559        if start != range.start.0 || end != range.end.0 {
2560            log::info!(
2561                "completion out of expected range, start: {start:?}, end: {end:?}, range: {range:?}"
2562            );
2563            return None;
2564        }
2565        snapshot.anchor_before(start)..snapshot.anchor_after(end)
2566    };
2567
2568    let insert_range = match insert_range {
2569        None => None,
2570        Some(insert_range) => {
2571            let range = range_from_lsp(insert_range);
2572            let start = snapshot.clip_point_utf16(range.start, Bias::Left);
2573            let end = snapshot.clip_point_utf16(range.end, Bias::Left);
2574            if start != range.start.0 || end != range.end.0 {
2575                log::info!("completion (insert) out of expected range");
2576                return None;
2577            }
2578            Some(snapshot.anchor_before(start)..snapshot.anchor_after(end))
2579        }
2580    };
2581
2582    Some(ParsedCompletionEdit {
2583        insert_range,
2584        replace_range,
2585        new_text: new_text.clone(),
2586    })
2587}
2588
2589#[async_trait(?Send)]
2590impl LspCommand for GetCodeActions {
2591    type Response = Vec<CodeAction>;
2592    type LspRequest = lsp::request::CodeActionRequest;
2593    type ProtoRequest = proto::GetCodeActions;
2594
2595    fn display_name(&self) -> &str {
2596        "Get code actions"
2597    }
2598
2599    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
2600        match &capabilities.server_capabilities.code_action_provider {
2601            None => false,
2602            Some(lsp::CodeActionProviderCapability::Simple(false)) => false,
2603            _ => {
2604                // If we do know that we want specific code actions AND we know that
2605                // the server only supports specific code actions, then we want to filter
2606                // down to the ones that are supported.
2607                if let Some((requested, supported)) = self
2608                    .kinds
2609                    .as_ref()
2610                    .zip(Self::supported_code_action_kinds(capabilities))
2611                {
2612                    requested.iter().any(|requested_kind| {
2613                        supported.iter().any(|supported_kind| {
2614                            code_action_kind_matches(requested_kind, supported_kind)
2615                        })
2616                    })
2617                } else {
2618                    true
2619                }
2620            }
2621        }
2622    }
2623
2624    fn to_lsp(
2625        &self,
2626        path: &Path,
2627        buffer: &Buffer,
2628        language_server: &Arc<LanguageServer>,
2629        _: &App,
2630    ) -> Result<lsp::CodeActionParams> {
2631        let mut relevant_diagnostics = Vec::new();
2632        for entry in buffer
2633            .snapshot()
2634            .diagnostics_in_range::<_, language::PointUtf16>(self.range.clone(), false)
2635        {
2636            relevant_diagnostics.push(entry.to_lsp_diagnostic_stub()?);
2637        }
2638
2639        let only = if let Some(requested) = &self.kinds {
2640            if let Some(supported_kinds) =
2641                Self::supported_code_action_kinds(language_server.adapter_server_capabilities())
2642            {
2643                let filtered = requested
2644                    .iter()
2645                    .filter(|requested_kind| {
2646                        supported_kinds.iter().any(|supported_kind| {
2647                            code_action_kind_matches(requested_kind, supported_kind)
2648                        })
2649                    })
2650                    .cloned()
2651                    .collect();
2652                Some(filtered)
2653            } else {
2654                Some(requested.clone())
2655            }
2656        } else {
2657            None
2658        };
2659
2660        Ok(lsp::CodeActionParams {
2661            text_document: make_text_document_identifier(path)?,
2662            range: range_to_lsp(self.range.to_point_utf16(buffer))?,
2663            work_done_progress_params: Default::default(),
2664            partial_result_params: Default::default(),
2665            context: lsp::CodeActionContext {
2666                diagnostics: relevant_diagnostics,
2667                only,
2668                ..lsp::CodeActionContext::default()
2669            },
2670        })
2671    }
2672
2673    async fn response_from_lsp(
2674        self,
2675        actions: Option<lsp::CodeActionResponse>,
2676        lsp_store: Entity<LspStore>,
2677        _: Entity<Buffer>,
2678        server_id: LanguageServerId,
2679        cx: AsyncApp,
2680    ) -> Result<Vec<CodeAction>> {
2681        let requested_kinds = self.kinds.as_ref();
2682
2683        let language_server = cx.update(|cx| {
2684            lsp_store
2685                .read(cx)
2686                .language_server_for_id(server_id)
2687                .with_context(|| {
2688                    format!("Missing the language server that just returned a response {server_id}")
2689                })
2690        })?;
2691
2692        let server_capabilities = language_server.capabilities();
2693        let available_commands = server_capabilities
2694            .execute_command_provider
2695            .as_ref()
2696            .map(|options| options.commands.as_slice())
2697            .unwrap_or_default();
2698        Ok(actions
2699            .unwrap_or_default()
2700            .into_iter()
2701            .filter_map(|entry| {
2702                let (lsp_action, resolved) = match entry {
2703                    lsp::CodeActionOrCommand::CodeAction(lsp_action) => {
2704                        if let Some(command) = lsp_action.command.as_ref()
2705                            && !available_commands.contains(&command.command)
2706                        {
2707                            return None;
2708                        }
2709                        (LspAction::Action(Box::new(lsp_action)), false)
2710                    }
2711                    lsp::CodeActionOrCommand::Command(command) => {
2712                        if available_commands.contains(&command.command) {
2713                            (LspAction::Command(command), true)
2714                        } else {
2715                            return None;
2716                        }
2717                    }
2718                };
2719
2720                if let Some((kinds, kind)) = requested_kinds.zip(lsp_action.action_kind())
2721                    && !kinds
2722                        .iter()
2723                        .any(|requested_kind| code_action_kind_matches(requested_kind, &kind))
2724                {
2725                    return None;
2726                }
2727
2728                Some(CodeAction {
2729                    server_id,
2730                    range: self.range.clone(),
2731                    lsp_action,
2732                    resolved,
2733                })
2734            })
2735            .collect())
2736    }
2737
2738    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetCodeActions {
2739        proto::GetCodeActions {
2740            project_id,
2741            buffer_id: buffer.remote_id().into(),
2742            start: Some(language::proto::serialize_anchor(&self.range.start)),
2743            end: Some(language::proto::serialize_anchor(&self.range.end)),
2744            version: serialize_version(&buffer.version()),
2745        }
2746    }
2747
2748    async fn from_proto(
2749        message: proto::GetCodeActions,
2750        _: Entity<LspStore>,
2751        buffer: Entity<Buffer>,
2752        mut cx: AsyncApp,
2753    ) -> Result<Self> {
2754        let start = message
2755            .start
2756            .and_then(language::proto::deserialize_anchor)
2757            .context("invalid start")?;
2758        let end = message
2759            .end
2760            .and_then(language::proto::deserialize_anchor)
2761            .context("invalid end")?;
2762        buffer
2763            .update(&mut cx, |buffer, _| {
2764                buffer.wait_for_version(deserialize_version(&message.version))
2765            })
2766            .await?;
2767
2768        Ok(Self {
2769            range: start..end,
2770            kinds: None,
2771        })
2772    }
2773
2774    fn response_to_proto(
2775        code_actions: Vec<CodeAction>,
2776        _: &mut LspStore,
2777        _: PeerId,
2778        buffer_version: &clock::Global,
2779        _: &mut App,
2780    ) -> proto::GetCodeActionsResponse {
2781        proto::GetCodeActionsResponse {
2782            actions: code_actions
2783                .iter()
2784                .map(LspStore::serialize_code_action)
2785                .collect(),
2786            version: serialize_version(buffer_version),
2787        }
2788    }
2789
2790    async fn response_from_proto(
2791        self,
2792        message: proto::GetCodeActionsResponse,
2793        _: Entity<LspStore>,
2794        buffer: Entity<Buffer>,
2795        mut cx: AsyncApp,
2796    ) -> Result<Vec<CodeAction>> {
2797        buffer
2798            .update(&mut cx, |buffer, _| {
2799                buffer.wait_for_version(deserialize_version(&message.version))
2800            })
2801            .await?;
2802        message
2803            .actions
2804            .into_iter()
2805            .map(LspStore::deserialize_code_action)
2806            .collect()
2807    }
2808
2809    fn buffer_id_from_proto(message: &proto::GetCodeActions) -> Result<BufferId> {
2810        BufferId::new(message.buffer_id)
2811    }
2812}
2813
2814impl GetCodeActions {
2815    fn supported_code_action_kinds(
2816        capabilities: AdapterServerCapabilities,
2817    ) -> Option<Vec<CodeActionKind>> {
2818        match capabilities.server_capabilities.code_action_provider {
2819            Some(lsp::CodeActionProviderCapability::Options(CodeActionOptions {
2820                code_action_kinds: Some(supported_action_kinds),
2821                ..
2822            })) => Some(supported_action_kinds),
2823            _ => capabilities.code_action_kinds,
2824        }
2825    }
2826
2827    pub fn can_resolve_actions(capabilities: &ServerCapabilities) -> bool {
2828        capabilities
2829            .code_action_provider
2830            .as_ref()
2831            .and_then(|options| match options {
2832                lsp::CodeActionProviderCapability::Simple(_is_supported) => None,
2833                lsp::CodeActionProviderCapability::Options(options) => options.resolve_provider,
2834            })
2835            .unwrap_or(false)
2836    }
2837}
2838
2839impl OnTypeFormatting {
2840    pub fn supports_on_type_formatting(trigger: &str, capabilities: &ServerCapabilities) -> bool {
2841        let Some(on_type_formatting_options) = &capabilities.document_on_type_formatting_provider
2842        else {
2843            return false;
2844        };
2845        on_type_formatting_options
2846            .first_trigger_character
2847            .contains(trigger)
2848            || on_type_formatting_options
2849                .more_trigger_character
2850                .iter()
2851                .flatten()
2852                .any(|chars| chars.contains(trigger))
2853    }
2854}
2855
2856#[async_trait(?Send)]
2857impl LspCommand for OnTypeFormatting {
2858    type Response = Option<Transaction>;
2859    type LspRequest = lsp::request::OnTypeFormatting;
2860    type ProtoRequest = proto::OnTypeFormatting;
2861
2862    fn display_name(&self) -> &str {
2863        "Formatting on typing"
2864    }
2865
2866    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
2867        Self::supports_on_type_formatting(&self.trigger, &capabilities.server_capabilities)
2868    }
2869
2870    fn to_lsp(
2871        &self,
2872        path: &Path,
2873        _: &Buffer,
2874        _: &Arc<LanguageServer>,
2875        _: &App,
2876    ) -> Result<lsp::DocumentOnTypeFormattingParams> {
2877        Ok(lsp::DocumentOnTypeFormattingParams {
2878            text_document_position: make_lsp_text_document_position(path, self.position)?,
2879            ch: self.trigger.clone(),
2880            options: self.options.clone(),
2881        })
2882    }
2883
2884    async fn response_from_lsp(
2885        self,
2886        message: Option<Vec<lsp::TextEdit>>,
2887        lsp_store: Entity<LspStore>,
2888        buffer: Entity<Buffer>,
2889        server_id: LanguageServerId,
2890        mut cx: AsyncApp,
2891    ) -> Result<Option<Transaction>> {
2892        if let Some(edits) = message {
2893            let (lsp_adapter, lsp_server) =
2894                language_server_for_buffer(&lsp_store, &buffer, server_id, &mut cx)?;
2895            LocalLspStore::deserialize_text_edits(
2896                lsp_store,
2897                buffer,
2898                edits,
2899                self.push_to_history,
2900                lsp_adapter,
2901                lsp_server,
2902                &mut cx,
2903            )
2904            .await
2905        } else {
2906            Ok(None)
2907        }
2908    }
2909
2910    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::OnTypeFormatting {
2911        proto::OnTypeFormatting {
2912            project_id,
2913            buffer_id: buffer.remote_id().into(),
2914            position: Some(language::proto::serialize_anchor(
2915                &buffer.anchor_before(self.position),
2916            )),
2917            trigger: self.trigger.clone(),
2918            version: serialize_version(&buffer.version()),
2919        }
2920    }
2921
2922    async fn from_proto(
2923        message: proto::OnTypeFormatting,
2924        _: Entity<LspStore>,
2925        buffer: Entity<Buffer>,
2926        mut cx: AsyncApp,
2927    ) -> Result<Self> {
2928        let position = message
2929            .position
2930            .and_then(deserialize_anchor)
2931            .context("invalid position")?;
2932        buffer
2933            .update(&mut cx, |buffer, _| {
2934                buffer.wait_for_version(deserialize_version(&message.version))
2935            })
2936            .await?;
2937
2938        let options = buffer.update(&mut cx, |buffer, cx| {
2939            lsp_formatting_options(LanguageSettings::for_buffer(buffer, cx).as_ref())
2940        });
2941
2942        Ok(Self {
2943            position: buffer.read_with(&cx, |buffer, _| position.to_point_utf16(buffer)),
2944            trigger: message.trigger.clone(),
2945            options,
2946            push_to_history: false,
2947        })
2948    }
2949
2950    fn response_to_proto(
2951        response: Option<Transaction>,
2952        _: &mut LspStore,
2953        _: PeerId,
2954        _: &clock::Global,
2955        _: &mut App,
2956    ) -> proto::OnTypeFormattingResponse {
2957        proto::OnTypeFormattingResponse {
2958            transaction: response
2959                .map(|transaction| language::proto::serialize_transaction(&transaction)),
2960        }
2961    }
2962
2963    async fn response_from_proto(
2964        self,
2965        message: proto::OnTypeFormattingResponse,
2966        _: Entity<LspStore>,
2967        _: Entity<Buffer>,
2968        _: AsyncApp,
2969    ) -> Result<Option<Transaction>> {
2970        let Some(transaction) = message.transaction else {
2971            return Ok(None);
2972        };
2973        Ok(Some(language::proto::deserialize_transaction(transaction)?))
2974    }
2975
2976    fn buffer_id_from_proto(message: &proto::OnTypeFormatting) -> Result<BufferId> {
2977        BufferId::new(message.buffer_id)
2978    }
2979}
2980
2981impl InlayHints {
2982    pub async fn lsp_to_project_hint(
2983        lsp_hint: lsp::InlayHint,
2984        buffer_handle: &Entity<Buffer>,
2985        server_id: LanguageServerId,
2986        resolve_state: ResolveState,
2987        force_no_type_left_padding: bool,
2988        cx: &mut AsyncApp,
2989    ) -> anyhow::Result<InlayHint> {
2990        let kind = lsp_hint.kind.and_then(|kind| match kind {
2991            lsp::InlayHintKind::TYPE => Some(InlayHintKind::Type),
2992            lsp::InlayHintKind::PARAMETER => Some(InlayHintKind::Parameter),
2993            _ => None,
2994        });
2995
2996        let position = buffer_handle.read_with(cx, |buffer, _| {
2997            let position = buffer.clip_point_utf16(point_from_lsp(lsp_hint.position), Bias::Left);
2998            if kind == Some(InlayHintKind::Parameter) {
2999                buffer.anchor_before(position)
3000            } else {
3001                buffer.anchor_after(position)
3002            }
3003        });
3004        let label = Self::lsp_inlay_label_to_project(lsp_hint.label, server_id)
3005            .await
3006            .context("lsp to project inlay hint conversion")?;
3007        let padding_left = if force_no_type_left_padding && kind == Some(InlayHintKind::Type) {
3008            false
3009        } else {
3010            lsp_hint.padding_left.unwrap_or(false)
3011        };
3012
3013        Ok(InlayHint {
3014            position,
3015            padding_left,
3016            padding_right: lsp_hint.padding_right.unwrap_or(false),
3017            label,
3018            kind,
3019            tooltip: lsp_hint.tooltip.map(|tooltip| match tooltip {
3020                lsp::InlayHintTooltip::String(s) => InlayHintTooltip::String(s),
3021                lsp::InlayHintTooltip::MarkupContent(markup_content) => {
3022                    InlayHintTooltip::MarkupContent(MarkupContent {
3023                        kind: match markup_content.kind {
3024                            lsp::MarkupKind::PlainText => HoverBlockKind::PlainText,
3025                            lsp::MarkupKind::Markdown => HoverBlockKind::Markdown,
3026                        },
3027                        value: markup_content.value,
3028                    })
3029                }
3030            }),
3031            resolve_state,
3032        })
3033    }
3034
3035    async fn lsp_inlay_label_to_project(
3036        lsp_label: lsp::InlayHintLabel,
3037        server_id: LanguageServerId,
3038    ) -> anyhow::Result<InlayHintLabel> {
3039        let label = match lsp_label {
3040            lsp::InlayHintLabel::String(s) => InlayHintLabel::String(s),
3041            lsp::InlayHintLabel::LabelParts(lsp_parts) => {
3042                let mut parts = Vec::with_capacity(lsp_parts.len());
3043                for lsp_part in lsp_parts {
3044                    parts.push(InlayHintLabelPart {
3045                        value: lsp_part.value,
3046                        tooltip: lsp_part.tooltip.map(|tooltip| match tooltip {
3047                            lsp::InlayHintLabelPartTooltip::String(s) => {
3048                                InlayHintLabelPartTooltip::String(s)
3049                            }
3050                            lsp::InlayHintLabelPartTooltip::MarkupContent(markup_content) => {
3051                                InlayHintLabelPartTooltip::MarkupContent(MarkupContent {
3052                                    kind: match markup_content.kind {
3053                                        lsp::MarkupKind::PlainText => HoverBlockKind::PlainText,
3054                                        lsp::MarkupKind::Markdown => HoverBlockKind::Markdown,
3055                                    },
3056                                    value: markup_content.value,
3057                                })
3058                            }
3059                        }),
3060                        location: Some(server_id).zip(lsp_part.location),
3061                    });
3062                }
3063                InlayHintLabel::LabelParts(parts)
3064            }
3065        };
3066
3067        Ok(label)
3068    }
3069
3070    pub fn project_to_proto_hint(response_hint: InlayHint) -> proto::InlayHint {
3071        let (state, lsp_resolve_state) = match response_hint.resolve_state {
3072            ResolveState::Resolved => (0, None),
3073            ResolveState::CanResolve(server_id, resolve_data) => (
3074                1,
3075                Some(proto::resolve_state::LspResolveState {
3076                    server_id: server_id.0 as u64,
3077                    value: resolve_data.map(|json_data| {
3078                        serde_json::to_string(&json_data)
3079                            .expect("failed to serialize resolve json data")
3080                    }),
3081                }),
3082            ),
3083            ResolveState::Resolving => (2, None),
3084        };
3085        let resolve_state = Some(proto::ResolveState {
3086            state,
3087            lsp_resolve_state,
3088        });
3089        proto::InlayHint {
3090            position: Some(language::proto::serialize_anchor(&response_hint.position)),
3091            padding_left: response_hint.padding_left,
3092            padding_right: response_hint.padding_right,
3093            label: Some(proto::InlayHintLabel {
3094                label: Some(match response_hint.label {
3095                    InlayHintLabel::String(s) => proto::inlay_hint_label::Label::Value(s),
3096                    InlayHintLabel::LabelParts(label_parts) => {
3097                        proto::inlay_hint_label::Label::LabelParts(proto::InlayHintLabelParts {
3098                            parts: label_parts.into_iter().map(|label_part| {
3099                                let location_url = label_part.location.as_ref().map(|(_, location)| location.uri.to_string());
3100                                let location_range_start = label_part.location.as_ref().map(|(_, location)| point_from_lsp(location.range.start).0).map(|point| proto::PointUtf16 { row: point.row, column: point.column });
3101                                let location_range_end = label_part.location.as_ref().map(|(_, location)| point_from_lsp(location.range.end).0).map(|point| proto::PointUtf16 { row: point.row, column: point.column });
3102                                proto::InlayHintLabelPart {
3103                                value: label_part.value,
3104                                tooltip: label_part.tooltip.map(|tooltip| {
3105                                    let proto_tooltip = match tooltip {
3106                                        InlayHintLabelPartTooltip::String(s) => proto::inlay_hint_label_part_tooltip::Content::Value(s),
3107                                        InlayHintLabelPartTooltip::MarkupContent(markup_content) => proto::inlay_hint_label_part_tooltip::Content::MarkupContent(proto::MarkupContent {
3108                                            is_markdown: markup_content.kind == HoverBlockKind::Markdown,
3109                                            value: markup_content.value,
3110                                        }),
3111                                    };
3112                                    proto::InlayHintLabelPartTooltip {content: Some(proto_tooltip)}
3113                                }),
3114                                location_url,
3115                                location_range_start,
3116                                location_range_end,
3117                                language_server_id: label_part.location.as_ref().map(|(server_id, _)| server_id.0 as u64),
3118                            }}).collect()
3119                        })
3120                    }
3121                }),
3122            }),
3123            kind: response_hint.kind.map(|kind| kind.name().to_string()),
3124            tooltip: response_hint.tooltip.map(|response_tooltip| {
3125                let proto_tooltip = match response_tooltip {
3126                    InlayHintTooltip::String(s) => proto::inlay_hint_tooltip::Content::Value(s),
3127                    InlayHintTooltip::MarkupContent(markup_content) => {
3128                        proto::inlay_hint_tooltip::Content::MarkupContent(proto::MarkupContent {
3129                            is_markdown: markup_content.kind == HoverBlockKind::Markdown,
3130                            value: markup_content.value,
3131                        })
3132                    }
3133                };
3134                proto::InlayHintTooltip {
3135                    content: Some(proto_tooltip),
3136                }
3137            }),
3138            resolve_state,
3139        }
3140    }
3141
3142    pub fn proto_to_project_hint(message_hint: proto::InlayHint) -> anyhow::Result<InlayHint> {
3143        let resolve_state = message_hint.resolve_state.as_ref().unwrap_or_else(|| {
3144            panic!("incorrect proto inlay hint message: no resolve state in hint {message_hint:?}",)
3145        });
3146        let resolve_state_data = resolve_state
3147            .lsp_resolve_state.as_ref()
3148            .map(|lsp_resolve_state| {
3149                let value = lsp_resolve_state.value.as_deref().map(|value| {
3150                    serde_json::from_str::<Option<lsp::LSPAny>>(value)
3151                        .with_context(|| format!("incorrect proto inlay hint message: non-json resolve state {lsp_resolve_state:?}"))
3152                }).transpose()?.flatten();
3153                anyhow::Ok((LanguageServerId(lsp_resolve_state.server_id as usize), value))
3154            })
3155            .transpose()?;
3156        let resolve_state = match resolve_state.state {
3157            0 => ResolveState::Resolved,
3158            1 => {
3159                let (server_id, lsp_resolve_state) = resolve_state_data.with_context(|| {
3160                    format!(
3161                        "No lsp resolve data for the hint that can be resolved: {message_hint:?}"
3162                    )
3163                })?;
3164                ResolveState::CanResolve(server_id, lsp_resolve_state)
3165            }
3166            2 => ResolveState::Resolving,
3167            invalid => {
3168                anyhow::bail!("Unexpected resolve state {invalid} for hint {message_hint:?}")
3169            }
3170        };
3171        Ok(InlayHint {
3172            position: message_hint
3173                .position
3174                .and_then(language::proto::deserialize_anchor)
3175                .context("invalid position")?,
3176            label: match message_hint
3177                .label
3178                .and_then(|label| label.label)
3179                .context("missing label")?
3180            {
3181                proto::inlay_hint_label::Label::Value(s) => InlayHintLabel::String(s),
3182                proto::inlay_hint_label::Label::LabelParts(parts) => {
3183                    let mut label_parts = Vec::new();
3184                    for part in parts.parts {
3185                        label_parts.push(InlayHintLabelPart {
3186                            value: part.value,
3187                            tooltip: part.tooltip.map(|tooltip| match tooltip.content {
3188                                Some(proto::inlay_hint_label_part_tooltip::Content::Value(s)) => {
3189                                    InlayHintLabelPartTooltip::String(s)
3190                                }
3191                                Some(
3192                                    proto::inlay_hint_label_part_tooltip::Content::MarkupContent(
3193                                        markup_content,
3194                                    ),
3195                                ) => InlayHintLabelPartTooltip::MarkupContent(MarkupContent {
3196                                    kind: if markup_content.is_markdown {
3197                                        HoverBlockKind::Markdown
3198                                    } else {
3199                                        HoverBlockKind::PlainText
3200                                    },
3201                                    value: markup_content.value,
3202                                }),
3203                                None => InlayHintLabelPartTooltip::String(String::new()),
3204                            }),
3205                            location: {
3206                                match part
3207                                    .location_url
3208                                    .zip(
3209                                        part.location_range_start.and_then(|start| {
3210                                            Some(start..part.location_range_end?)
3211                                        }),
3212                                    )
3213                                    .zip(part.language_server_id)
3214                                {
3215                                    Some(((uri, range), server_id)) => Some((
3216                                        LanguageServerId(server_id as usize),
3217                                        lsp::Location {
3218                                            uri: lsp::Uri::from_str(&uri)
3219                                                .context("invalid uri in hint part {part:?}")?,
3220                                            range: lsp::Range::new(
3221                                                point_to_lsp(PointUtf16::new(
3222                                                    range.start.row,
3223                                                    range.start.column,
3224                                                )),
3225                                                point_to_lsp(PointUtf16::new(
3226                                                    range.end.row,
3227                                                    range.end.column,
3228                                                )),
3229                                            ),
3230                                        },
3231                                    )),
3232                                    None => None,
3233                                }
3234                            },
3235                        });
3236                    }
3237
3238                    InlayHintLabel::LabelParts(label_parts)
3239                }
3240            },
3241            padding_left: message_hint.padding_left,
3242            padding_right: message_hint.padding_right,
3243            kind: message_hint
3244                .kind
3245                .as_deref()
3246                .and_then(InlayHintKind::from_name),
3247            tooltip: message_hint.tooltip.and_then(|tooltip| {
3248                Some(match tooltip.content? {
3249                    proto::inlay_hint_tooltip::Content::Value(s) => InlayHintTooltip::String(s),
3250                    proto::inlay_hint_tooltip::Content::MarkupContent(markup_content) => {
3251                        InlayHintTooltip::MarkupContent(MarkupContent {
3252                            kind: if markup_content.is_markdown {
3253                                HoverBlockKind::Markdown
3254                            } else {
3255                                HoverBlockKind::PlainText
3256                            },
3257                            value: markup_content.value,
3258                        })
3259                    }
3260                })
3261            }),
3262            resolve_state,
3263        })
3264    }
3265
3266    pub fn project_to_lsp_hint(hint: InlayHint, snapshot: &BufferSnapshot) -> lsp::InlayHint {
3267        lsp::InlayHint {
3268            position: point_to_lsp(hint.position.to_point_utf16(snapshot)),
3269            kind: hint.kind.map(|kind| match kind {
3270                InlayHintKind::Type => lsp::InlayHintKind::TYPE,
3271                InlayHintKind::Parameter => lsp::InlayHintKind::PARAMETER,
3272            }),
3273            text_edits: None,
3274            tooltip: hint.tooltip.and_then(|tooltip| {
3275                Some(match tooltip {
3276                    InlayHintTooltip::String(s) => lsp::InlayHintTooltip::String(s),
3277                    InlayHintTooltip::MarkupContent(markup_content) => {
3278                        lsp::InlayHintTooltip::MarkupContent(lsp::MarkupContent {
3279                            kind: match markup_content.kind {
3280                                HoverBlockKind::PlainText => lsp::MarkupKind::PlainText,
3281                                HoverBlockKind::Markdown => lsp::MarkupKind::Markdown,
3282                                HoverBlockKind::Code { .. } => return None,
3283                            },
3284                            value: markup_content.value,
3285                        })
3286                    }
3287                })
3288            }),
3289            label: match hint.label {
3290                InlayHintLabel::String(s) => lsp::InlayHintLabel::String(s),
3291                InlayHintLabel::LabelParts(label_parts) => lsp::InlayHintLabel::LabelParts(
3292                    label_parts
3293                        .into_iter()
3294                        .map(|part| lsp::InlayHintLabelPart {
3295                            value: part.value,
3296                            tooltip: part.tooltip.and_then(|tooltip| {
3297                                Some(match tooltip {
3298                                    InlayHintLabelPartTooltip::String(s) => {
3299                                        lsp::InlayHintLabelPartTooltip::String(s)
3300                                    }
3301                                    InlayHintLabelPartTooltip::MarkupContent(markup_content) => {
3302                                        lsp::InlayHintLabelPartTooltip::MarkupContent(
3303                                            lsp::MarkupContent {
3304                                                kind: match markup_content.kind {
3305                                                    HoverBlockKind::PlainText => {
3306                                                        lsp::MarkupKind::PlainText
3307                                                    }
3308                                                    HoverBlockKind::Markdown => {
3309                                                        lsp::MarkupKind::Markdown
3310                                                    }
3311                                                    HoverBlockKind::Code { .. } => return None,
3312                                                },
3313                                                value: markup_content.value,
3314                                            },
3315                                        )
3316                                    }
3317                                })
3318                            }),
3319                            location: part.location.map(|(_, location)| location),
3320                            command: None,
3321                        })
3322                        .collect(),
3323                ),
3324            },
3325            padding_left: Some(hint.padding_left),
3326            padding_right: Some(hint.padding_right),
3327            data: match hint.resolve_state {
3328                ResolveState::CanResolve(_, data) => data,
3329                ResolveState::Resolving | ResolveState::Resolved => None,
3330            },
3331        }
3332    }
3333
3334    pub fn can_resolve_inlays(capabilities: &ServerCapabilities) -> bool {
3335        capabilities
3336            .inlay_hint_provider
3337            .as_ref()
3338            .and_then(|options| match options {
3339                OneOf::Left(_is_supported) => None,
3340                OneOf::Right(capabilities) => match capabilities {
3341                    lsp::InlayHintServerCapabilities::Options(o) => o.resolve_provider,
3342                    lsp::InlayHintServerCapabilities::RegistrationOptions(o) => {
3343                        o.inlay_hint_options.resolve_provider
3344                    }
3345                },
3346            })
3347            .unwrap_or(false)
3348    }
3349
3350    pub fn check_capabilities(capabilities: &ServerCapabilities) -> bool {
3351        capabilities
3352            .inlay_hint_provider
3353            .as_ref()
3354            .is_some_and(|inlay_hint_provider| match inlay_hint_provider {
3355                lsp::OneOf::Left(enabled) => *enabled,
3356                lsp::OneOf::Right(_) => true,
3357            })
3358    }
3359}
3360
3361#[async_trait(?Send)]
3362impl LspCommand for InlayHints {
3363    type Response = Vec<InlayHint>;
3364    type LspRequest = lsp::InlayHintRequest;
3365    type ProtoRequest = proto::InlayHints;
3366
3367    fn display_name(&self) -> &str {
3368        "Inlay hints"
3369    }
3370
3371    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
3372        Self::check_capabilities(&capabilities.server_capabilities)
3373    }
3374
3375    fn to_lsp(
3376        &self,
3377        path: &Path,
3378        buffer: &Buffer,
3379        _: &Arc<LanguageServer>,
3380        _: &App,
3381    ) -> Result<lsp::InlayHintParams> {
3382        Ok(lsp::InlayHintParams {
3383            text_document: lsp::TextDocumentIdentifier {
3384                uri: file_path_to_lsp_url(path)?,
3385            },
3386            range: range_to_lsp(self.range.to_point_utf16(buffer))?,
3387            work_done_progress_params: Default::default(),
3388        })
3389    }
3390
3391    async fn response_from_lsp(
3392        self,
3393        message: Option<Vec<lsp::InlayHint>>,
3394        lsp_store: Entity<LspStore>,
3395        buffer: Entity<Buffer>,
3396        server_id: LanguageServerId,
3397        mut cx: AsyncApp,
3398    ) -> anyhow::Result<Vec<InlayHint>> {
3399        let (lsp_adapter, lsp_server) =
3400            language_server_for_buffer(&lsp_store, &buffer, server_id, &mut cx)?;
3401        // `typescript-language-server` adds padding to the left for type hints, turning
3402        // `const foo: boolean` into `const foo : boolean` which looks odd.
3403        // `rust-analyzer` does not have the padding for this case, and we have to accommodate both.
3404        //
3405        // We could trim the whole string, but being pessimistic on par with the situation above,
3406        // there might be a hint with multiple whitespaces at the end(s) which we need to display properly.
3407        // Hence let's use a heuristic first to handle the most awkward case and look for more.
3408        let force_no_type_left_padding =
3409            lsp_adapter.name.0.as_ref() == "typescript-language-server";
3410
3411        let hints = message.unwrap_or_default().into_iter().map(|lsp_hint| {
3412            let resolve_state = if InlayHints::can_resolve_inlays(&lsp_server.capabilities()) {
3413                ResolveState::CanResolve(lsp_server.server_id(), lsp_hint.data.clone())
3414            } else {
3415                ResolveState::Resolved
3416            };
3417
3418            let buffer = buffer.clone();
3419            cx.spawn(async move |cx| {
3420                InlayHints::lsp_to_project_hint(
3421                    lsp_hint,
3422                    &buffer,
3423                    server_id,
3424                    resolve_state,
3425                    force_no_type_left_padding,
3426                    cx,
3427                )
3428                .await
3429            })
3430        });
3431        future::join_all(hints)
3432            .await
3433            .into_iter()
3434            .collect::<anyhow::Result<_>>()
3435            .context("lsp to project inlay hints conversion")
3436    }
3437
3438    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::InlayHints {
3439        proto::InlayHints {
3440            project_id,
3441            buffer_id: buffer.remote_id().into(),
3442            start: Some(language::proto::serialize_anchor(&self.range.start)),
3443            end: Some(language::proto::serialize_anchor(&self.range.end)),
3444            version: serialize_version(&buffer.version()),
3445        }
3446    }
3447
3448    async fn from_proto(
3449        message: proto::InlayHints,
3450        _: Entity<LspStore>,
3451        buffer: Entity<Buffer>,
3452        mut cx: AsyncApp,
3453    ) -> Result<Self> {
3454        let start = message
3455            .start
3456            .and_then(language::proto::deserialize_anchor)
3457            .context("invalid start")?;
3458        let end = message
3459            .end
3460            .and_then(language::proto::deserialize_anchor)
3461            .context("invalid end")?;
3462        buffer
3463            .update(&mut cx, |buffer, _| {
3464                buffer.wait_for_version(deserialize_version(&message.version))
3465            })
3466            .await?;
3467
3468        Ok(Self { range: start..end })
3469    }
3470
3471    fn response_to_proto(
3472        response: Vec<InlayHint>,
3473        _: &mut LspStore,
3474        _: PeerId,
3475        buffer_version: &clock::Global,
3476        _: &mut App,
3477    ) -> proto::InlayHintsResponse {
3478        proto::InlayHintsResponse {
3479            hints: response
3480                .into_iter()
3481                .map(InlayHints::project_to_proto_hint)
3482                .collect(),
3483            version: serialize_version(buffer_version),
3484        }
3485    }
3486
3487    async fn response_from_proto(
3488        self,
3489        message: proto::InlayHintsResponse,
3490        _: Entity<LspStore>,
3491        buffer: Entity<Buffer>,
3492        mut cx: AsyncApp,
3493    ) -> anyhow::Result<Vec<InlayHint>> {
3494        buffer
3495            .update(&mut cx, |buffer, _| {
3496                buffer.wait_for_version(deserialize_version(&message.version))
3497            })
3498            .await?;
3499
3500        let mut hints = Vec::new();
3501        for message_hint in message.hints {
3502            hints.push(InlayHints::proto_to_project_hint(message_hint)?);
3503        }
3504
3505        Ok(hints)
3506    }
3507
3508    fn buffer_id_from_proto(message: &proto::InlayHints) -> Result<BufferId> {
3509        BufferId::new(message.buffer_id)
3510    }
3511}
3512
3513#[async_trait(?Send)]
3514impl LspCommand for SemanticTokensFull {
3515    type Response = SemanticTokensResponse;
3516    type LspRequest = lsp::SemanticTokensFullRequest;
3517    type ProtoRequest = proto::SemanticTokens;
3518
3519    fn display_name(&self) -> &str {
3520        "Semantic tokens full"
3521    }
3522
3523    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
3524        capabilities
3525            .server_capabilities
3526            .semantic_tokens_provider
3527            .as_ref()
3528            .is_some_and(|semantic_tokens_provider| {
3529                let options = match semantic_tokens_provider {
3530                    lsp::SemanticTokensServerCapabilities::SemanticTokensOptions(opts) => opts,
3531                    lsp::SemanticTokensServerCapabilities::SemanticTokensRegistrationOptions(
3532                        opts,
3533                    ) => &opts.semantic_tokens_options,
3534                };
3535
3536                match options.full {
3537                    Some(lsp::SemanticTokensFullOptions::Bool(is_supported)) => is_supported,
3538                    Some(lsp::SemanticTokensFullOptions::Delta { .. }) => true,
3539                    None => false,
3540                }
3541            })
3542    }
3543
3544    fn to_lsp(
3545        &self,
3546        path: &Path,
3547        _: &Buffer,
3548        _: &Arc<LanguageServer>,
3549        _: &App,
3550    ) -> Result<lsp::SemanticTokensParams> {
3551        Ok(lsp::SemanticTokensParams {
3552            text_document: lsp::TextDocumentIdentifier {
3553                uri: file_path_to_lsp_url(path)?,
3554            },
3555            partial_result_params: Default::default(),
3556            work_done_progress_params: Default::default(),
3557        })
3558    }
3559
3560    async fn response_from_lsp(
3561        self,
3562        message: Option<lsp::SemanticTokensResult>,
3563        _: Entity<LspStore>,
3564        _: Entity<Buffer>,
3565        _: LanguageServerId,
3566        _: AsyncApp,
3567    ) -> anyhow::Result<SemanticTokensResponse> {
3568        match message {
3569            Some(lsp::SemanticTokensResult::Tokens(tokens)) => Ok(SemanticTokensResponse::Full {
3570                data: tokens.data,
3571                result_id: tokens.result_id.map(SharedString::new),
3572            }),
3573            Some(lsp::SemanticTokensResult::Partial(_)) => {
3574                anyhow::bail!(
3575                    "Unexpected semantic tokens response with partial result for inlay hints"
3576                )
3577            }
3578            None => Ok(Default::default()),
3579        }
3580    }
3581
3582    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::SemanticTokens {
3583        proto::SemanticTokens {
3584            project_id,
3585            buffer_id: buffer.remote_id().into(),
3586            version: serialize_version(&buffer.version()),
3587            for_server: self.for_server.map(|id| id.to_proto()),
3588        }
3589    }
3590
3591    async fn from_proto(
3592        message: proto::SemanticTokens,
3593        _: Entity<LspStore>,
3594        buffer: Entity<Buffer>,
3595        mut cx: AsyncApp,
3596    ) -> Result<Self> {
3597        buffer
3598            .update(&mut cx, |buffer, _| {
3599                buffer.wait_for_version(deserialize_version(&message.version))
3600            })
3601            .await?;
3602
3603        Ok(Self {
3604            for_server: message
3605                .for_server
3606                .map(|id| LanguageServerId::from_proto(id)),
3607        })
3608    }
3609
3610    fn response_to_proto(
3611        response: SemanticTokensResponse,
3612        _: &mut LspStore,
3613        _: PeerId,
3614        buffer_version: &clock::Global,
3615        _: &mut App,
3616    ) -> proto::SemanticTokensResponse {
3617        match response {
3618            SemanticTokensResponse::Full { data, result_id } => proto::SemanticTokensResponse {
3619                data,
3620                edits: Vec::new(),
3621                result_id: result_id.map(|s| s.to_string()),
3622                version: serialize_version(buffer_version),
3623            },
3624            SemanticTokensResponse::Delta { edits, result_id } => proto::SemanticTokensResponse {
3625                data: Vec::new(),
3626                edits: edits
3627                    .into_iter()
3628                    .map(|edit| proto::SemanticTokensEdit {
3629                        start: edit.start,
3630                        delete_count: edit.delete_count,
3631                        data: edit.data,
3632                    })
3633                    .collect(),
3634                result_id: result_id.map(|s| s.to_string()),
3635                version: serialize_version(buffer_version),
3636            },
3637        }
3638    }
3639
3640    async fn response_from_proto(
3641        self,
3642        message: proto::SemanticTokensResponse,
3643        _: Entity<LspStore>,
3644        buffer: Entity<Buffer>,
3645        mut cx: AsyncApp,
3646    ) -> anyhow::Result<SemanticTokensResponse> {
3647        buffer
3648            .update(&mut cx, |buffer, _| {
3649                buffer.wait_for_version(deserialize_version(&message.version))
3650            })
3651            .await?;
3652
3653        Ok(SemanticTokensResponse::Full {
3654            data: message.data,
3655            result_id: message.result_id.map(SharedString::new),
3656        })
3657    }
3658
3659    fn buffer_id_from_proto(message: &proto::SemanticTokens) -> Result<BufferId> {
3660        BufferId::new(message.buffer_id)
3661    }
3662}
3663
3664#[async_trait(?Send)]
3665impl LspCommand for SemanticTokensDelta {
3666    type Response = SemanticTokensResponse;
3667    type LspRequest = lsp::SemanticTokensFullDeltaRequest;
3668    type ProtoRequest = proto::SemanticTokens;
3669
3670    fn display_name(&self) -> &str {
3671        "Semantic tokens delta"
3672    }
3673
3674    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
3675        capabilities
3676            .server_capabilities
3677            .semantic_tokens_provider
3678            .as_ref()
3679            .is_some_and(|semantic_tokens_provider| {
3680                let options = match semantic_tokens_provider {
3681                    lsp::SemanticTokensServerCapabilities::SemanticTokensOptions(opts) => opts,
3682                    lsp::SemanticTokensServerCapabilities::SemanticTokensRegistrationOptions(
3683                        opts,
3684                    ) => &opts.semantic_tokens_options,
3685                };
3686
3687                match options.full {
3688                    Some(lsp::SemanticTokensFullOptions::Delta { delta }) => delta.unwrap_or(false),
3689                    // `full: true` (instead of `full: { delta: true }`) means no support for delta.
3690                    _ => false,
3691                }
3692            })
3693    }
3694
3695    fn to_lsp(
3696        &self,
3697        path: &Path,
3698        _: &Buffer,
3699        _: &Arc<LanguageServer>,
3700        _: &App,
3701    ) -> Result<lsp::SemanticTokensDeltaParams> {
3702        Ok(lsp::SemanticTokensDeltaParams {
3703            text_document: lsp::TextDocumentIdentifier {
3704                uri: file_path_to_lsp_url(path)?,
3705            },
3706            previous_result_id: self.previous_result_id.clone().map(|s| s.to_string()),
3707            partial_result_params: Default::default(),
3708            work_done_progress_params: Default::default(),
3709        })
3710    }
3711
3712    async fn response_from_lsp(
3713        self,
3714        message: Option<lsp::SemanticTokensFullDeltaResult>,
3715        _: Entity<LspStore>,
3716        _: Entity<Buffer>,
3717        _: LanguageServerId,
3718        _: AsyncApp,
3719    ) -> anyhow::Result<SemanticTokensResponse> {
3720        match message {
3721            Some(lsp::SemanticTokensFullDeltaResult::Tokens(tokens)) => {
3722                Ok(SemanticTokensResponse::Full {
3723                    data: tokens.data,
3724                    result_id: tokens.result_id.map(SharedString::new),
3725                })
3726            }
3727            Some(lsp::SemanticTokensFullDeltaResult::TokensDelta(delta)) => {
3728                Ok(SemanticTokensResponse::Delta {
3729                    edits: delta
3730                        .edits
3731                        .into_iter()
3732                        .map(|e| SemanticTokensEdit {
3733                            start: e.start,
3734                            delete_count: e.delete_count,
3735                            data: e.data.unwrap_or_default(),
3736                        })
3737                        .collect(),
3738                    result_id: delta.result_id.map(SharedString::new),
3739                })
3740            }
3741            Some(lsp::SemanticTokensFullDeltaResult::PartialTokensDelta { .. }) => {
3742                anyhow::bail!(
3743                    "Unexpected semantic tokens response with partial result for inlay hints"
3744                )
3745            }
3746            None => Ok(Default::default()),
3747        }
3748    }
3749
3750    fn to_proto(&self, _: u64, _: &Buffer) -> proto::SemanticTokens {
3751        unimplemented!("Delta requests are never initialted on the remote client side")
3752    }
3753
3754    async fn from_proto(
3755        _: proto::SemanticTokens,
3756        _: Entity<LspStore>,
3757        _: Entity<Buffer>,
3758        _: AsyncApp,
3759    ) -> Result<Self> {
3760        unimplemented!("Delta requests are never initialted on the remote client side")
3761    }
3762
3763    fn response_to_proto(
3764        response: SemanticTokensResponse,
3765        _: &mut LspStore,
3766        _: PeerId,
3767        buffer_version: &clock::Global,
3768        _: &mut App,
3769    ) -> proto::SemanticTokensResponse {
3770        match response {
3771            SemanticTokensResponse::Full { data, result_id } => proto::SemanticTokensResponse {
3772                data,
3773                edits: Vec::new(),
3774                result_id: result_id.map(|s| s.to_string()),
3775                version: serialize_version(buffer_version),
3776            },
3777            SemanticTokensResponse::Delta { edits, result_id } => proto::SemanticTokensResponse {
3778                data: Vec::new(),
3779                edits: edits
3780                    .into_iter()
3781                    .map(|edit| proto::SemanticTokensEdit {
3782                        start: edit.start,
3783                        delete_count: edit.delete_count,
3784                        data: edit.data,
3785                    })
3786                    .collect(),
3787                result_id: result_id.map(|s| s.to_string()),
3788                version: serialize_version(buffer_version),
3789            },
3790        }
3791    }
3792
3793    async fn response_from_proto(
3794        self,
3795        message: proto::SemanticTokensResponse,
3796        _: Entity<LspStore>,
3797        buffer: Entity<Buffer>,
3798        mut cx: AsyncApp,
3799    ) -> anyhow::Result<SemanticTokensResponse> {
3800        buffer
3801            .update(&mut cx, |buffer, _| {
3802                buffer.wait_for_version(deserialize_version(&message.version))
3803            })
3804            .await?;
3805
3806        Ok(SemanticTokensResponse::Full {
3807            data: message.data,
3808            result_id: message.result_id.map(SharedString::new),
3809        })
3810    }
3811
3812    fn buffer_id_from_proto(message: &proto::SemanticTokens) -> Result<BufferId> {
3813        BufferId::new(message.buffer_id)
3814    }
3815}
3816
3817#[async_trait(?Send)]
3818impl LspCommand for GetCodeLens {
3819    type Response = Vec<CodeAction>;
3820    type LspRequest = lsp::CodeLensRequest;
3821    type ProtoRequest = proto::GetCodeLens;
3822
3823    fn display_name(&self) -> &str {
3824        "Code Lens"
3825    }
3826
3827    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
3828        capabilities
3829            .server_capabilities
3830            .code_lens_provider
3831            .is_some()
3832    }
3833
3834    fn to_lsp(
3835        &self,
3836        path: &Path,
3837        _: &Buffer,
3838        _: &Arc<LanguageServer>,
3839        _: &App,
3840    ) -> Result<lsp::CodeLensParams> {
3841        Ok(lsp::CodeLensParams {
3842            text_document: lsp::TextDocumentIdentifier {
3843                uri: file_path_to_lsp_url(path)?,
3844            },
3845            work_done_progress_params: lsp::WorkDoneProgressParams::default(),
3846            partial_result_params: lsp::PartialResultParams::default(),
3847        })
3848    }
3849
3850    async fn response_from_lsp(
3851        self,
3852        message: Option<Vec<lsp::CodeLens>>,
3853        lsp_store: Entity<LspStore>,
3854        buffer: Entity<Buffer>,
3855        server_id: LanguageServerId,
3856        cx: AsyncApp,
3857    ) -> anyhow::Result<Vec<CodeAction>> {
3858        let snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
3859        let language_server = cx.update(|cx| {
3860            lsp_store
3861                .read(cx)
3862                .language_server_for_id(server_id)
3863                .with_context(|| {
3864                    format!("Missing the language server that just returned a response {server_id}")
3865                })
3866        })?;
3867        let server_capabilities = language_server.capabilities();
3868        let available_commands = server_capabilities
3869            .execute_command_provider
3870            .as_ref()
3871            .map(|options| options.commands.as_slice())
3872            .unwrap_or_default();
3873        Ok(message
3874            .unwrap_or_default()
3875            .into_iter()
3876            .filter(|code_lens| {
3877                code_lens
3878                    .command
3879                    .as_ref()
3880                    .is_none_or(|command| available_commands.contains(&command.command))
3881            })
3882            .map(|code_lens| {
3883                let code_lens_range = range_from_lsp(code_lens.range);
3884                let start = snapshot.clip_point_utf16(code_lens_range.start, Bias::Left);
3885                let end = snapshot.clip_point_utf16(code_lens_range.end, Bias::Right);
3886                let range = snapshot.anchor_before(start)..snapshot.anchor_after(end);
3887                CodeAction {
3888                    server_id,
3889                    range,
3890                    lsp_action: LspAction::CodeLens(code_lens),
3891                    resolved: false,
3892                }
3893            })
3894            .collect())
3895    }
3896
3897    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetCodeLens {
3898        proto::GetCodeLens {
3899            project_id,
3900            buffer_id: buffer.remote_id().into(),
3901            version: serialize_version(&buffer.version()),
3902        }
3903    }
3904
3905    async fn from_proto(
3906        message: proto::GetCodeLens,
3907        _: Entity<LspStore>,
3908        buffer: Entity<Buffer>,
3909        mut cx: AsyncApp,
3910    ) -> Result<Self> {
3911        buffer
3912            .update(&mut cx, |buffer, _| {
3913                buffer.wait_for_version(deserialize_version(&message.version))
3914            })
3915            .await?;
3916        Ok(Self)
3917    }
3918
3919    fn response_to_proto(
3920        response: Vec<CodeAction>,
3921        _: &mut LspStore,
3922        _: PeerId,
3923        buffer_version: &clock::Global,
3924        _: &mut App,
3925    ) -> proto::GetCodeLensResponse {
3926        proto::GetCodeLensResponse {
3927            lens_actions: response
3928                .iter()
3929                .map(LspStore::serialize_code_action)
3930                .collect(),
3931            version: serialize_version(buffer_version),
3932        }
3933    }
3934
3935    async fn response_from_proto(
3936        self,
3937        message: proto::GetCodeLensResponse,
3938        _: Entity<LspStore>,
3939        buffer: Entity<Buffer>,
3940        mut cx: AsyncApp,
3941    ) -> anyhow::Result<Vec<CodeAction>> {
3942        buffer
3943            .update(&mut cx, |buffer, _| {
3944                buffer.wait_for_version(deserialize_version(&message.version))
3945            })
3946            .await?;
3947        message
3948            .lens_actions
3949            .into_iter()
3950            .map(LspStore::deserialize_code_action)
3951            .collect::<Result<Vec<_>>>()
3952            .context("deserializing proto code lens response")
3953    }
3954
3955    fn buffer_id_from_proto(message: &proto::GetCodeLens) -> Result<BufferId> {
3956        BufferId::new(message.buffer_id)
3957    }
3958}
3959
3960impl LinkedEditingRange {
3961    pub fn check_server_capabilities(capabilities: ServerCapabilities) -> bool {
3962        let Some(linked_editing_options) = capabilities.linked_editing_range_provider else {
3963            return false;
3964        };
3965        if let LinkedEditingRangeServerCapabilities::Simple(false) = linked_editing_options {
3966            return false;
3967        }
3968        true
3969    }
3970}
3971
3972#[async_trait(?Send)]
3973impl LspCommand for LinkedEditingRange {
3974    type Response = Vec<Range<Anchor>>;
3975    type LspRequest = lsp::request::LinkedEditingRange;
3976    type ProtoRequest = proto::LinkedEditingRange;
3977
3978    fn display_name(&self) -> &str {
3979        "Linked editing range"
3980    }
3981
3982    fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool {
3983        Self::check_server_capabilities(capabilities.server_capabilities)
3984    }
3985
3986    fn to_lsp(
3987        &self,
3988        path: &Path,
3989        buffer: &Buffer,
3990        _server: &Arc<LanguageServer>,
3991        _: &App,
3992    ) -> Result<lsp::LinkedEditingRangeParams> {
3993        let position = self.position.to_point_utf16(&buffer.snapshot());
3994        Ok(lsp::LinkedEditingRangeParams {
3995            text_document_position_params: make_lsp_text_document_position(path, position)?,
3996            work_done_progress_params: Default::default(),
3997        })
3998    }
3999
4000    async fn response_from_lsp(
4001        self,
4002        message: Option<lsp::LinkedEditingRanges>,
4003        _: Entity<LspStore>,
4004        buffer: Entity<Buffer>,
4005        _server_id: LanguageServerId,
4006        cx: AsyncApp,
4007    ) -> Result<Vec<Range<Anchor>>> {
4008        if let Some(lsp::LinkedEditingRanges { mut ranges, .. }) = message {
4009            ranges.sort_by_key(|range| range.start);
4010
4011            Ok(buffer.read_with(&cx, |buffer, _| {
4012                ranges
4013                    .into_iter()
4014                    .map(|range| {
4015                        let start =
4016                            buffer.clip_point_utf16(point_from_lsp(range.start), Bias::Left);
4017                        let end = buffer.clip_point_utf16(point_from_lsp(range.end), Bias::Left);
4018                        buffer.anchor_before(start)..buffer.anchor_after(end)
4019                    })
4020                    .collect()
4021            }))
4022        } else {
4023            Ok(vec![])
4024        }
4025    }
4026
4027    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::LinkedEditingRange {
4028        proto::LinkedEditingRange {
4029            project_id,
4030            buffer_id: buffer.remote_id().to_proto(),
4031            position: Some(serialize_anchor(&self.position)),
4032            version: serialize_version(&buffer.version()),
4033        }
4034    }
4035
4036    async fn from_proto(
4037        message: proto::LinkedEditingRange,
4038        _: Entity<LspStore>,
4039        buffer: Entity<Buffer>,
4040        mut cx: AsyncApp,
4041    ) -> Result<Self> {
4042        let position = message.position.context("invalid position")?;
4043        buffer
4044            .update(&mut cx, |buffer, _| {
4045                buffer.wait_for_version(deserialize_version(&message.version))
4046            })
4047            .await?;
4048        let position = deserialize_anchor(position).context("invalid position")?;
4049        buffer
4050            .update(&mut cx, |buffer, _| buffer.wait_for_anchors([position]))
4051            .await?;
4052        Ok(Self { position })
4053    }
4054
4055    fn response_to_proto(
4056        response: Vec<Range<Anchor>>,
4057        _: &mut LspStore,
4058        _: PeerId,
4059        buffer_version: &clock::Global,
4060        _: &mut App,
4061    ) -> proto::LinkedEditingRangeResponse {
4062        proto::LinkedEditingRangeResponse {
4063            items: response
4064                .into_iter()
4065                .map(|range| proto::AnchorRange {
4066                    start: Some(serialize_anchor(&range.start)),
4067                    end: Some(serialize_anchor(&range.end)),
4068                })
4069                .collect(),
4070            version: serialize_version(buffer_version),
4071        }
4072    }
4073
4074    async fn response_from_proto(
4075        self,
4076        message: proto::LinkedEditingRangeResponse,
4077        _: Entity<LspStore>,
4078        buffer: Entity<Buffer>,
4079        mut cx: AsyncApp,
4080    ) -> Result<Vec<Range<Anchor>>> {
4081        buffer
4082            .update(&mut cx, |buffer, _| {
4083                buffer.wait_for_version(deserialize_version(&message.version))
4084            })
4085            .await?;
4086        let items: Vec<Range<Anchor>> = message
4087            .items
4088            .into_iter()
4089            .filter_map(|range| {
4090                let start = deserialize_anchor(range.start?)?;
4091                let end = deserialize_anchor(range.end?)?;
4092                Some(start..end)
4093            })
4094            .collect();
4095        for range in &items {
4096            buffer
4097                .update(&mut cx, |buffer, _| {
4098                    buffer.wait_for_anchors([range.start, range.end])
4099                })
4100                .await?;
4101        }
4102        Ok(items)
4103    }
4104
4105    fn buffer_id_from_proto(message: &proto::LinkedEditingRange) -> Result<BufferId> {
4106        BufferId::new(message.buffer_id)
4107    }
4108}
4109
4110impl GetDocumentDiagnostics {
4111    pub fn diagnostics_from_proto(
4112        response: proto::GetDocumentDiagnosticsResponse,
4113    ) -> Vec<LspPullDiagnostics> {
4114        response
4115            .pulled_diagnostics
4116            .into_iter()
4117            .filter_map(|diagnostics| {
4118                Some(LspPullDiagnostics::Response {
4119                    registration_id: diagnostics.registration_id.map(SharedString::from),
4120                    server_id: LanguageServerId::from_proto(diagnostics.server_id),
4121                    uri: lsp::Uri::from_str(diagnostics.uri.as_str()).log_err()?,
4122                    diagnostics: if diagnostics.changed {
4123                        PulledDiagnostics::Unchanged {
4124                            result_id: SharedString::new(diagnostics.result_id?),
4125                        }
4126                    } else {
4127                        PulledDiagnostics::Changed {
4128                            result_id: diagnostics.result_id.map(SharedString::new),
4129                            diagnostics: diagnostics
4130                                .diagnostics
4131                                .into_iter()
4132                                .filter_map(|diagnostic| {
4133                                    GetDocumentDiagnostics::deserialize_lsp_diagnostic(diagnostic)
4134                                        .context("deserializing diagnostics")
4135                                        .log_err()
4136                                })
4137                                .collect(),
4138                        }
4139                    },
4140                })
4141            })
4142            .collect()
4143    }
4144
4145    pub fn deserialize_lsp_diagnostic(diagnostic: proto::LspDiagnostic) -> Result<lsp::Diagnostic> {
4146        let start = diagnostic.start.context("invalid start range")?;
4147        let end = diagnostic.end.context("invalid end range")?;
4148
4149        let range = Range::<PointUtf16> {
4150            start: PointUtf16 {
4151                row: start.row,
4152                column: start.column,
4153            },
4154            end: PointUtf16 {
4155                row: end.row,
4156                column: end.column,
4157            },
4158        };
4159
4160        let data = diagnostic.data.and_then(|data| Value::from_str(&data).ok());
4161        let code = diagnostic.code.map(lsp::NumberOrString::String);
4162
4163        let related_information = diagnostic
4164            .related_information
4165            .into_iter()
4166            .map(|info| {
4167                let start = info.location_range_start.unwrap();
4168                let end = info.location_range_end.unwrap();
4169
4170                lsp::DiagnosticRelatedInformation {
4171                    location: lsp::Location {
4172                        range: lsp::Range {
4173                            start: point_to_lsp(PointUtf16::new(start.row, start.column)),
4174                            end: point_to_lsp(PointUtf16::new(end.row, end.column)),
4175                        },
4176                        uri: lsp::Uri::from_str(&info.location_url.unwrap()).unwrap(),
4177                    },
4178                    message: info.message,
4179                }
4180            })
4181            .collect::<Vec<_>>();
4182
4183        let tags = diagnostic
4184            .tags
4185            .into_iter()
4186            .filter_map(|tag| match proto::LspDiagnosticTag::from_i32(tag) {
4187                Some(proto::LspDiagnosticTag::Unnecessary) => Some(lsp::DiagnosticTag::UNNECESSARY),
4188                Some(proto::LspDiagnosticTag::Deprecated) => Some(lsp::DiagnosticTag::DEPRECATED),
4189                _ => None,
4190            })
4191            .collect::<Vec<_>>();
4192
4193        Ok(lsp::Diagnostic {
4194            range: language::range_to_lsp(range)?,
4195            severity: match proto::lsp_diagnostic::Severity::from_i32(diagnostic.severity).unwrap()
4196            {
4197                proto::lsp_diagnostic::Severity::Error => Some(lsp::DiagnosticSeverity::ERROR),
4198                proto::lsp_diagnostic::Severity::Warning => Some(lsp::DiagnosticSeverity::WARNING),
4199                proto::lsp_diagnostic::Severity::Information => {
4200                    Some(lsp::DiagnosticSeverity::INFORMATION)
4201                }
4202                proto::lsp_diagnostic::Severity::Hint => Some(lsp::DiagnosticSeverity::HINT),
4203                _ => None,
4204            },
4205            code,
4206            code_description: diagnostic
4207                .code_description
4208                .map(|code_description| CodeDescription {
4209                    href: Some(lsp::Uri::from_str(&code_description).unwrap()),
4210                }),
4211            related_information: Some(related_information),
4212            tags: Some(tags),
4213            source: diagnostic.source.clone(),
4214            message: diagnostic.message,
4215            data,
4216        })
4217    }
4218
4219    pub fn serialize_lsp_diagnostic(diagnostic: lsp::Diagnostic) -> Result<proto::LspDiagnostic> {
4220        let range = language::range_from_lsp(diagnostic.range);
4221        let related_information = diagnostic
4222            .related_information
4223            .unwrap_or_default()
4224            .into_iter()
4225            .map(|related_information| {
4226                let location_range_start =
4227                    point_from_lsp(related_information.location.range.start).0;
4228                let location_range_end = point_from_lsp(related_information.location.range.end).0;
4229
4230                Ok(proto::LspDiagnosticRelatedInformation {
4231                    location_url: Some(related_information.location.uri.to_string()),
4232                    location_range_start: Some(proto::PointUtf16 {
4233                        row: location_range_start.row,
4234                        column: location_range_start.column,
4235                    }),
4236                    location_range_end: Some(proto::PointUtf16 {
4237                        row: location_range_end.row,
4238                        column: location_range_end.column,
4239                    }),
4240                    message: related_information.message,
4241                })
4242            })
4243            .collect::<Result<Vec<_>>>()?;
4244
4245        let tags = diagnostic
4246            .tags
4247            .unwrap_or_default()
4248            .into_iter()
4249            .map(|tag| match tag {
4250                lsp::DiagnosticTag::UNNECESSARY => proto::LspDiagnosticTag::Unnecessary,
4251                lsp::DiagnosticTag::DEPRECATED => proto::LspDiagnosticTag::Deprecated,
4252                _ => proto::LspDiagnosticTag::None,
4253            } as i32)
4254            .collect();
4255
4256        Ok(proto::LspDiagnostic {
4257            start: Some(proto::PointUtf16 {
4258                row: range.start.0.row,
4259                column: range.start.0.column,
4260            }),
4261            end: Some(proto::PointUtf16 {
4262                row: range.end.0.row,
4263                column: range.end.0.column,
4264            }),
4265            severity: match diagnostic.severity {
4266                Some(lsp::DiagnosticSeverity::ERROR) => proto::lsp_diagnostic::Severity::Error,
4267                Some(lsp::DiagnosticSeverity::WARNING) => proto::lsp_diagnostic::Severity::Warning,
4268                Some(lsp::DiagnosticSeverity::INFORMATION) => {
4269                    proto::lsp_diagnostic::Severity::Information
4270                }
4271                Some(lsp::DiagnosticSeverity::HINT) => proto::lsp_diagnostic::Severity::Hint,
4272                _ => proto::lsp_diagnostic::Severity::None,
4273            } as i32,
4274            code: diagnostic.code.as_ref().map(|code| match code {
4275                lsp::NumberOrString::Number(code) => code.to_string(),
4276                lsp::NumberOrString::String(code) => code.clone(),
4277            }),
4278            source: diagnostic.source.clone(),
4279            related_information,
4280            tags,
4281            code_description: diagnostic
4282                .code_description
4283                .and_then(|desc| desc.href.map(|url| url.to_string())),
4284            message: diagnostic.message,
4285            data: diagnostic.data.as_ref().map(|data| data.to_string()),
4286        })
4287    }
4288
4289    pub fn deserialize_workspace_diagnostics_report(
4290        report: lsp::WorkspaceDiagnosticReportResult,
4291        server_id: LanguageServerId,
4292        registration_id: Option<SharedString>,
4293    ) -> Vec<WorkspaceLspPullDiagnostics> {
4294        let mut pulled_diagnostics = HashMap::default();
4295        match report {
4296            lsp::WorkspaceDiagnosticReportResult::Report(workspace_diagnostic_report) => {
4297                for report in workspace_diagnostic_report.items {
4298                    match report {
4299                        lsp::WorkspaceDocumentDiagnosticReport::Full(report) => {
4300                            process_full_workspace_diagnostics_report(
4301                                &mut pulled_diagnostics,
4302                                server_id,
4303                                report,
4304                                registration_id.clone(),
4305                            )
4306                        }
4307                        lsp::WorkspaceDocumentDiagnosticReport::Unchanged(report) => {
4308                            process_unchanged_workspace_diagnostics_report(
4309                                &mut pulled_diagnostics,
4310                                server_id,
4311                                report,
4312                                registration_id.clone(),
4313                            )
4314                        }
4315                    }
4316                }
4317            }
4318            lsp::WorkspaceDiagnosticReportResult::Partial(
4319                workspace_diagnostic_report_partial_result,
4320            ) => {
4321                for report in workspace_diagnostic_report_partial_result.items {
4322                    match report {
4323                        lsp::WorkspaceDocumentDiagnosticReport::Full(report) => {
4324                            process_full_workspace_diagnostics_report(
4325                                &mut pulled_diagnostics,
4326                                server_id,
4327                                report,
4328                                registration_id.clone(),
4329                            )
4330                        }
4331                        lsp::WorkspaceDocumentDiagnosticReport::Unchanged(report) => {
4332                            process_unchanged_workspace_diagnostics_report(
4333                                &mut pulled_diagnostics,
4334                                server_id,
4335                                report,
4336                                registration_id.clone(),
4337                            )
4338                        }
4339                    }
4340                }
4341            }
4342        }
4343        pulled_diagnostics.into_values().collect()
4344    }
4345}
4346
4347#[derive(Debug)]
4348pub struct WorkspaceLspPullDiagnostics {
4349    pub version: Option<i32>,
4350    pub diagnostics: LspPullDiagnostics,
4351}
4352
4353fn process_full_workspace_diagnostics_report(
4354    diagnostics: &mut HashMap<lsp::Uri, WorkspaceLspPullDiagnostics>,
4355    server_id: LanguageServerId,
4356    report: lsp::WorkspaceFullDocumentDiagnosticReport,
4357    registration_id: Option<SharedString>,
4358) {
4359    let mut new_diagnostics = HashMap::default();
4360    process_full_diagnostics_report(
4361        &mut new_diagnostics,
4362        server_id,
4363        report.uri,
4364        report.full_document_diagnostic_report,
4365        registration_id,
4366    );
4367    diagnostics.extend(new_diagnostics.into_iter().map(|(uri, diagnostics)| {
4368        (
4369            uri,
4370            WorkspaceLspPullDiagnostics {
4371                version: report.version.map(|v| v as i32),
4372                diagnostics,
4373            },
4374        )
4375    }));
4376}
4377
4378fn process_unchanged_workspace_diagnostics_report(
4379    diagnostics: &mut HashMap<lsp::Uri, WorkspaceLspPullDiagnostics>,
4380    server_id: LanguageServerId,
4381    report: lsp::WorkspaceUnchangedDocumentDiagnosticReport,
4382    registration_id: Option<SharedString>,
4383) {
4384    let mut new_diagnostics = HashMap::default();
4385    process_unchanged_diagnostics_report(
4386        &mut new_diagnostics,
4387        server_id,
4388        report.uri,
4389        report.unchanged_document_diagnostic_report,
4390        registration_id,
4391    );
4392    diagnostics.extend(new_diagnostics.into_iter().map(|(uri, diagnostics)| {
4393        (
4394            uri,
4395            WorkspaceLspPullDiagnostics {
4396                version: report.version.map(|v| v as i32),
4397                diagnostics,
4398            },
4399        )
4400    }));
4401}
4402
4403#[async_trait(?Send)]
4404impl LspCommand for GetDocumentDiagnostics {
4405    type Response = Vec<LspPullDiagnostics>;
4406    type LspRequest = lsp::request::DocumentDiagnosticRequest;
4407    type ProtoRequest = proto::GetDocumentDiagnostics;
4408
4409    fn display_name(&self) -> &str {
4410        "Get diagnostics"
4411    }
4412
4413    fn check_capabilities(&self, _: AdapterServerCapabilities) -> bool {
4414        true
4415    }
4416
4417    fn to_lsp(
4418        &self,
4419        path: &Path,
4420        _: &Buffer,
4421        _: &Arc<LanguageServer>,
4422        _: &App,
4423    ) -> Result<lsp::DocumentDiagnosticParams> {
4424        Ok(lsp::DocumentDiagnosticParams {
4425            text_document: lsp::TextDocumentIdentifier {
4426                uri: file_path_to_lsp_url(path)?,
4427            },
4428            identifier: self.identifier.as_ref().map(ToString::to_string),
4429            previous_result_id: self.previous_result_id.as_ref().map(ToString::to_string),
4430            partial_result_params: Default::default(),
4431            work_done_progress_params: Default::default(),
4432        })
4433    }
4434
4435    async fn response_from_lsp(
4436        self,
4437        message: lsp::DocumentDiagnosticReportResult,
4438        _: Entity<LspStore>,
4439        buffer: Entity<Buffer>,
4440        server_id: LanguageServerId,
4441        cx: AsyncApp,
4442    ) -> Result<Self::Response> {
4443        let url = buffer.read_with(&cx, |buffer, cx| {
4444            buffer
4445                .file()
4446                .and_then(|file| file.as_local())
4447                .map(|file| {
4448                    let abs_path = file.abs_path(cx);
4449                    file_path_to_lsp_url(&abs_path)
4450                })
4451                .transpose()?
4452                .with_context(|| format!("missing url on buffer {}", buffer.remote_id()))
4453        })?;
4454
4455        let mut pulled_diagnostics = HashMap::default();
4456        match message {
4457            lsp::DocumentDiagnosticReportResult::Report(report) => match report {
4458                lsp::DocumentDiagnosticReport::Full(report) => {
4459                    if let Some(related_documents) = report.related_documents {
4460                        process_related_documents(
4461                            &mut pulled_diagnostics,
4462                            server_id,
4463                            related_documents,
4464                            self.registration_id.clone(),
4465                        );
4466                    }
4467                    process_full_diagnostics_report(
4468                        &mut pulled_diagnostics,
4469                        server_id,
4470                        url,
4471                        report.full_document_diagnostic_report,
4472                        self.registration_id,
4473                    );
4474                }
4475                lsp::DocumentDiagnosticReport::Unchanged(report) => {
4476                    if let Some(related_documents) = report.related_documents {
4477                        process_related_documents(
4478                            &mut pulled_diagnostics,
4479                            server_id,
4480                            related_documents,
4481                            self.registration_id.clone(),
4482                        );
4483                    }
4484                    process_unchanged_diagnostics_report(
4485                        &mut pulled_diagnostics,
4486                        server_id,
4487                        url,
4488                        report.unchanged_document_diagnostic_report,
4489                        self.registration_id,
4490                    );
4491                }
4492            },
4493            lsp::DocumentDiagnosticReportResult::Partial(report) => {
4494                if let Some(related_documents) = report.related_documents {
4495                    process_related_documents(
4496                        &mut pulled_diagnostics,
4497                        server_id,
4498                        related_documents,
4499                        self.registration_id,
4500                    );
4501                }
4502            }
4503        }
4504
4505        Ok(pulled_diagnostics.into_values().collect())
4506    }
4507
4508    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::GetDocumentDiagnostics {
4509        proto::GetDocumentDiagnostics {
4510            project_id,
4511            buffer_id: buffer.remote_id().into(),
4512            version: serialize_version(&buffer.version()),
4513        }
4514    }
4515
4516    async fn from_proto(
4517        _: proto::GetDocumentDiagnostics,
4518        _: Entity<LspStore>,
4519        _: Entity<Buffer>,
4520        _: AsyncApp,
4521    ) -> Result<Self> {
4522        anyhow::bail!(
4523            "proto::GetDocumentDiagnostics is not expected to be converted from proto directly, as it needs `previous_result_id` fetched first"
4524        )
4525    }
4526
4527    fn response_to_proto(
4528        response: Self::Response,
4529        _: &mut LspStore,
4530        _: PeerId,
4531        _: &clock::Global,
4532        _: &mut App,
4533    ) -> proto::GetDocumentDiagnosticsResponse {
4534        let pulled_diagnostics = response
4535            .into_iter()
4536            .filter_map(|diagnostics| match diagnostics {
4537                LspPullDiagnostics::Default => None,
4538                LspPullDiagnostics::Response {
4539                    server_id,
4540                    uri,
4541                    diagnostics,
4542                    registration_id,
4543                } => {
4544                    let mut changed = false;
4545                    let (diagnostics, result_id) = match diagnostics {
4546                        PulledDiagnostics::Unchanged { result_id } => (Vec::new(), Some(result_id)),
4547                        PulledDiagnostics::Changed {
4548                            result_id,
4549                            diagnostics,
4550                        } => {
4551                            changed = true;
4552                            (diagnostics, result_id)
4553                        }
4554                    };
4555                    Some(proto::PulledDiagnostics {
4556                        changed,
4557                        result_id: result_id.map(|id| id.to_string()),
4558                        uri: uri.to_string(),
4559                        server_id: server_id.to_proto(),
4560                        diagnostics: diagnostics
4561                            .into_iter()
4562                            .filter_map(|diagnostic| {
4563                                GetDocumentDiagnostics::serialize_lsp_diagnostic(diagnostic)
4564                                    .context("serializing diagnostics")
4565                                    .log_err()
4566                            })
4567                            .collect(),
4568                        registration_id: registration_id.as_ref().map(ToString::to_string),
4569                    })
4570                }
4571            })
4572            .collect();
4573
4574        proto::GetDocumentDiagnosticsResponse { pulled_diagnostics }
4575    }
4576
4577    async fn response_from_proto(
4578        self,
4579        response: proto::GetDocumentDiagnosticsResponse,
4580        _: Entity<LspStore>,
4581        _: Entity<Buffer>,
4582        _: AsyncApp,
4583    ) -> Result<Self::Response> {
4584        Ok(Self::diagnostics_from_proto(response))
4585    }
4586
4587    fn buffer_id_from_proto(message: &proto::GetDocumentDiagnostics) -> Result<BufferId> {
4588        BufferId::new(message.buffer_id)
4589    }
4590}
4591
4592#[async_trait(?Send)]
4593impl LspCommand for GetDocumentColor {
4594    type Response = Vec<DocumentColor>;
4595    type LspRequest = lsp::request::DocumentColor;
4596    type ProtoRequest = proto::GetDocumentColor;
4597
4598    fn display_name(&self) -> &str {
4599        "Document color"
4600    }
4601
4602    fn check_capabilities(&self, server_capabilities: AdapterServerCapabilities) -> bool {
4603        server_capabilities
4604            .server_capabilities
4605            .color_provider
4606            .as_ref()
4607            .is_some_and(|capability| match capability {
4608                lsp::ColorProviderCapability::Simple(supported) => *supported,
4609                lsp::ColorProviderCapability::ColorProvider(..) => true,
4610                lsp::ColorProviderCapability::Options(..) => true,
4611            })
4612    }
4613
4614    fn to_lsp(
4615        &self,
4616        path: &Path,
4617        _: &Buffer,
4618        _: &Arc<LanguageServer>,
4619        _: &App,
4620    ) -> Result<lsp::DocumentColorParams> {
4621        Ok(lsp::DocumentColorParams {
4622            text_document: make_text_document_identifier(path)?,
4623            work_done_progress_params: Default::default(),
4624            partial_result_params: Default::default(),
4625        })
4626    }
4627
4628    async fn response_from_lsp(
4629        self,
4630        message: Vec<lsp::ColorInformation>,
4631        _: Entity<LspStore>,
4632        _: Entity<Buffer>,
4633        _: LanguageServerId,
4634        _: AsyncApp,
4635    ) -> Result<Self::Response> {
4636        Ok(message
4637            .into_iter()
4638            .map(|color| DocumentColor {
4639                lsp_range: color.range,
4640                color: color.color,
4641                resolved: false,
4642                color_presentations: Vec::new(),
4643            })
4644            .collect())
4645    }
4646
4647    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest {
4648        proto::GetDocumentColor {
4649            project_id,
4650            buffer_id: buffer.remote_id().to_proto(),
4651            version: serialize_version(&buffer.version()),
4652        }
4653    }
4654
4655    async fn from_proto(
4656        _: Self::ProtoRequest,
4657        _: Entity<LspStore>,
4658        _: Entity<Buffer>,
4659        _: AsyncApp,
4660    ) -> Result<Self> {
4661        Ok(Self {})
4662    }
4663
4664    fn response_to_proto(
4665        response: Self::Response,
4666        _: &mut LspStore,
4667        _: PeerId,
4668        buffer_version: &clock::Global,
4669        _: &mut App,
4670    ) -> proto::GetDocumentColorResponse {
4671        proto::GetDocumentColorResponse {
4672            colors: response
4673                .into_iter()
4674                .map(|color| {
4675                    let start = point_from_lsp(color.lsp_range.start).0;
4676                    let end = point_from_lsp(color.lsp_range.end).0;
4677                    proto::ColorInformation {
4678                        red: color.color.red,
4679                        green: color.color.green,
4680                        blue: color.color.blue,
4681                        alpha: color.color.alpha,
4682                        lsp_range_start: Some(proto::PointUtf16 {
4683                            row: start.row,
4684                            column: start.column,
4685                        }),
4686                        lsp_range_end: Some(proto::PointUtf16 {
4687                            row: end.row,
4688                            column: end.column,
4689                        }),
4690                    }
4691                })
4692                .collect(),
4693            version: serialize_version(buffer_version),
4694        }
4695    }
4696
4697    async fn response_from_proto(
4698        self,
4699        message: proto::GetDocumentColorResponse,
4700        _: Entity<LspStore>,
4701        _: Entity<Buffer>,
4702        _: AsyncApp,
4703    ) -> Result<Self::Response> {
4704        Ok(message
4705            .colors
4706            .into_iter()
4707            .filter_map(|color| {
4708                let start = color.lsp_range_start?;
4709                let start = PointUtf16::new(start.row, start.column);
4710                let end = color.lsp_range_end?;
4711                let end = PointUtf16::new(end.row, end.column);
4712                Some(DocumentColor {
4713                    resolved: false,
4714                    color_presentations: Vec::new(),
4715                    lsp_range: lsp::Range {
4716                        start: point_to_lsp(start),
4717                        end: point_to_lsp(end),
4718                    },
4719                    color: lsp::Color {
4720                        red: color.red,
4721                        green: color.green,
4722                        blue: color.blue,
4723                        alpha: color.alpha,
4724                    },
4725                })
4726            })
4727            .collect())
4728    }
4729
4730    fn buffer_id_from_proto(message: &Self::ProtoRequest) -> Result<BufferId> {
4731        BufferId::new(message.buffer_id)
4732    }
4733}
4734
4735#[async_trait(?Send)]
4736impl LspCommand for GetFoldingRanges {
4737    type Response = Vec<LspFoldingRange>;
4738    type LspRequest = lsp::request::FoldingRangeRequest;
4739    type ProtoRequest = proto::GetFoldingRanges;
4740
4741    fn display_name(&self) -> &str {
4742        "Folding ranges"
4743    }
4744
4745    fn check_capabilities(&self, server_capabilities: AdapterServerCapabilities) -> bool {
4746        server_capabilities
4747            .server_capabilities
4748            .folding_range_provider
4749            .as_ref()
4750            .is_some_and(|capability| match capability {
4751                lsp::FoldingRangeProviderCapability::Simple(supported) => *supported,
4752                lsp::FoldingRangeProviderCapability::FoldingProvider(..)
4753                | lsp::FoldingRangeProviderCapability::Options(..) => true,
4754            })
4755    }
4756
4757    fn to_lsp(
4758        &self,
4759        path: &Path,
4760        _: &Buffer,
4761        _: &Arc<LanguageServer>,
4762        _: &App,
4763    ) -> Result<lsp::FoldingRangeParams> {
4764        Ok(lsp::FoldingRangeParams {
4765            text_document: make_text_document_identifier(path)?,
4766            work_done_progress_params: Default::default(),
4767            partial_result_params: Default::default(),
4768        })
4769    }
4770
4771    async fn response_from_lsp(
4772        self,
4773        message: Option<Vec<lsp::FoldingRange>>,
4774        _: Entity<LspStore>,
4775        buffer: Entity<Buffer>,
4776        _: LanguageServerId,
4777        cx: AsyncApp,
4778    ) -> Result<Self::Response> {
4779        let snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
4780        let max_point = snapshot.max_point_utf16();
4781        Ok(message
4782            .unwrap_or_default()
4783            .into_iter()
4784            .filter(|range| range.start_line < range.end_line)
4785            .filter(|range| range.start_line <= max_point.row && range.end_line <= max_point.row)
4786            .map(|folding_range| {
4787                let start_col = folding_range.start_character.unwrap_or(u32::MAX);
4788                let end_col = folding_range.end_character.unwrap_or(u32::MAX);
4789                let start = snapshot.clip_point_utf16(
4790                    Unclipped(PointUtf16::new(folding_range.start_line, start_col)),
4791                    Bias::Right,
4792                );
4793                let end = snapshot.clip_point_utf16(
4794                    Unclipped(PointUtf16::new(folding_range.end_line, end_col)),
4795                    Bias::Left,
4796                );
4797                let start = snapshot.anchor_after(start);
4798                let end = snapshot.anchor_before(end);
4799                let collapsed_text = folding_range
4800                    .collapsed_text
4801                    .filter(|t| !t.is_empty())
4802                    .map(|t| SharedString::from(crate::lsp_store::collapse_newlines(&t, " ")));
4803                LspFoldingRange {
4804                    range: start..end,
4805                    collapsed_text,
4806                }
4807            })
4808            .collect())
4809    }
4810
4811    fn to_proto(&self, project_id: u64, buffer: &Buffer) -> Self::ProtoRequest {
4812        proto::GetFoldingRanges {
4813            project_id,
4814            buffer_id: buffer.remote_id().to_proto(),
4815            version: serialize_version(&buffer.version()),
4816        }
4817    }
4818
4819    async fn from_proto(
4820        _: Self::ProtoRequest,
4821        _: Entity<LspStore>,
4822        _: Entity<Buffer>,
4823        _: AsyncApp,
4824    ) -> Result<Self> {
4825        Ok(Self)
4826    }
4827
4828    fn response_to_proto(
4829        response: Self::Response,
4830        _: &mut LspStore,
4831        _: PeerId,
4832        buffer_version: &clock::Global,
4833        _: &mut App,
4834    ) -> proto::GetFoldingRangesResponse {
4835        let mut ranges = Vec::with_capacity(response.len());
4836        let mut collapsed_texts = Vec::with_capacity(response.len());
4837        for folding_range in response {
4838            ranges.push(serialize_anchor_range(folding_range.range));
4839            collapsed_texts.push(
4840                folding_range
4841                    .collapsed_text
4842                    .map(|t| t.to_string())
4843                    .unwrap_or_default(),
4844            );
4845        }
4846        proto::GetFoldingRangesResponse {
4847            ranges,
4848            collapsed_texts,
4849            version: serialize_version(buffer_version),
4850        }
4851    }
4852
4853    async fn response_from_proto(
4854        self,
4855        message: proto::GetFoldingRangesResponse,
4856        _: Entity<LspStore>,
4857        buffer: Entity<Buffer>,
4858        mut cx: AsyncApp,
4859    ) -> Result<Self::Response> {
4860        buffer
4861            .update(&mut cx, |buffer, _| {
4862                buffer.wait_for_version(deserialize_version(&message.version))
4863            })
4864            .await?;
4865        message
4866            .ranges
4867            .into_iter()
4868            .zip(
4869                message
4870                    .collapsed_texts
4871                    .into_iter()
4872                    .map(Some)
4873                    .chain(std::iter::repeat(None)),
4874            )
4875            .map(|(range, collapsed_text)| {
4876                Ok(LspFoldingRange {
4877                    range: deserialize_anchor_range(range)?,
4878                    collapsed_text: collapsed_text
4879                        .filter(|t| !t.is_empty())
4880                        .map(SharedString::from),
4881                })
4882            })
4883            .collect()
4884    }
4885
4886    fn buffer_id_from_proto(message: &Self::ProtoRequest) -> Result<BufferId> {
4887        BufferId::new(message.buffer_id)
4888    }
4889}
4890
4891fn process_related_documents(
4892    diagnostics: &mut HashMap<lsp::Uri, LspPullDiagnostics>,
4893    server_id: LanguageServerId,
4894    documents: impl IntoIterator<Item = (lsp::Uri, lsp::DocumentDiagnosticReportKind)>,
4895    registration_id: Option<SharedString>,
4896) {
4897    for (url, report_kind) in documents {
4898        match report_kind {
4899            lsp::DocumentDiagnosticReportKind::Full(report) => process_full_diagnostics_report(
4900                diagnostics,
4901                server_id,
4902                url,
4903                report,
4904                registration_id.clone(),
4905            ),
4906            lsp::DocumentDiagnosticReportKind::Unchanged(report) => {
4907                process_unchanged_diagnostics_report(
4908                    diagnostics,
4909                    server_id,
4910                    url,
4911                    report,
4912                    registration_id.clone(),
4913                )
4914            }
4915        }
4916    }
4917}
4918
4919fn process_unchanged_diagnostics_report(
4920    diagnostics: &mut HashMap<lsp::Uri, LspPullDiagnostics>,
4921    server_id: LanguageServerId,
4922    uri: lsp::Uri,
4923    report: lsp::UnchangedDocumentDiagnosticReport,
4924    registration_id: Option<SharedString>,
4925) {
4926    let result_id = SharedString::new(report.result_id);
4927    match diagnostics.entry(uri.clone()) {
4928        hash_map::Entry::Occupied(mut o) => match o.get_mut() {
4929            LspPullDiagnostics::Default => {
4930                o.insert(LspPullDiagnostics::Response {
4931                    server_id,
4932                    uri,
4933                    diagnostics: PulledDiagnostics::Unchanged { result_id },
4934                    registration_id,
4935                });
4936            }
4937            LspPullDiagnostics::Response {
4938                server_id: existing_server_id,
4939                uri: existing_uri,
4940                diagnostics: existing_diagnostics,
4941                ..
4942            } => {
4943                if server_id != *existing_server_id || &uri != existing_uri {
4944                    debug_panic!(
4945                        "Unexpected state: file {uri} has two different sets of diagnostics reported"
4946                    );
4947                }
4948                match existing_diagnostics {
4949                    PulledDiagnostics::Unchanged { .. } => {
4950                        *existing_diagnostics = PulledDiagnostics::Unchanged { result_id };
4951                    }
4952                    PulledDiagnostics::Changed { .. } => {}
4953                }
4954            }
4955        },
4956        hash_map::Entry::Vacant(v) => {
4957            v.insert(LspPullDiagnostics::Response {
4958                server_id,
4959                uri,
4960                diagnostics: PulledDiagnostics::Unchanged { result_id },
4961                registration_id,
4962            });
4963        }
4964    }
4965}
4966
4967fn process_full_diagnostics_report(
4968    diagnostics: &mut HashMap<lsp::Uri, LspPullDiagnostics>,
4969    server_id: LanguageServerId,
4970    uri: lsp::Uri,
4971    report: lsp::FullDocumentDiagnosticReport,
4972    registration_id: Option<SharedString>,
4973) {
4974    let result_id = report.result_id.map(SharedString::new);
4975    match diagnostics.entry(uri.clone()) {
4976        hash_map::Entry::Occupied(mut o) => match o.get_mut() {
4977            LspPullDiagnostics::Default => {
4978                o.insert(LspPullDiagnostics::Response {
4979                    server_id,
4980                    uri,
4981                    diagnostics: PulledDiagnostics::Changed {
4982                        result_id,
4983                        diagnostics: report.items,
4984                    },
4985                    registration_id,
4986                });
4987            }
4988            LspPullDiagnostics::Response {
4989                server_id: existing_server_id,
4990                uri: existing_uri,
4991                diagnostics: existing_diagnostics,
4992                ..
4993            } => {
4994                if server_id != *existing_server_id || &uri != existing_uri {
4995                    debug_panic!(
4996                        "Unexpected state: file {uri} has two different sets of diagnostics reported"
4997                    );
4998                }
4999                match existing_diagnostics {
5000                    PulledDiagnostics::Unchanged { .. } => {
5001                        *existing_diagnostics = PulledDiagnostics::Changed {
5002                            result_id,
5003                            diagnostics: report.items,
5004                        };
5005                    }
5006                    PulledDiagnostics::Changed {
5007                        result_id: existing_result_id,
5008                        diagnostics: existing_diagnostics,
5009                    } => {
5010                        if result_id.is_some() {
5011                            *existing_result_id = result_id;
5012                        }
5013                        existing_diagnostics.extend(report.items);
5014                    }
5015                }
5016            }
5017        },
5018        hash_map::Entry::Vacant(v) => {
5019            v.insert(LspPullDiagnostics::Response {
5020                server_id,
5021                uri,
5022                diagnostics: PulledDiagnostics::Changed {
5023                    result_id,
5024                    diagnostics: report.items,
5025                },
5026                registration_id,
5027            });
5028        }
5029    }
5030}