laburnum 1.17.0

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use {
  crate::{
    TRACER,
    database::PartitionWriteContextRef,
    protocol::{
      jsonrpc,
      lsp::{
        PartialResultParams,
        Position,
        Range,
        StaticTextDocumentRegistrationOptions,
        TextDocumentIdentifier,
        WorkDoneProgressOptions,
        WorkDoneProgressParams,
      },
    },
    scheduler::task::TaskContext,
  },
  serde::{
    Deserialize,
    Serialize,
  },
};

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectionRangeClientCapabilities {
  /// Whether implementation supports dynamic registration for selection range
  /// providers. If this is set to `true` the client supports the new
  /// `SelectionRangeRegistrationOptions` return value for the corresponding
  /// server capability as well.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub dynamic_registration: Option<bool>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct SelectionRangeOptions {
  #[serde(flatten)]
  pub work_done_progress_options: WorkDoneProgressOptions,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct SelectionRangeRegistrationOptions {
  #[serde(flatten)]
  pub selection_range_options: SelectionRangeOptions,

  #[serde(flatten)]
  pub registration_options: StaticTextDocumentRegistrationOptions,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum SelectionRangeProviderCapability {
  Simple(bool),
  Options(SelectionRangeOptions),
  RegistrationOptions(SelectionRangeRegistrationOptions),
}

impl From<SelectionRangeRegistrationOptions>
  for SelectionRangeProviderCapability
{
  fn from(from: SelectionRangeRegistrationOptions) -> Self {
    Self::RegistrationOptions(from)
  }
}

impl From<SelectionRangeOptions> for SelectionRangeProviderCapability {
  fn from(from: SelectionRangeOptions) -> Self {
    Self::Options(from)
  }
}

impl From<bool> for SelectionRangeProviderCapability {
  fn from(from: bool) -> Self {
    Self::Simple(from)
  }
}

/// A parameter literal used in selection range requests.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectionRangeParams {
  /// The text document.
  pub text_document: TextDocumentIdentifier,

  /// The positions inside the text document.
  pub positions: Vec<Position>,

  #[serde(flatten)]
  pub work_done_progress_params: WorkDoneProgressParams,

  #[serde(flatten)]
  pub partial_result_params: PartialResultParams,
}

/// Represents a selection range.
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectionRange {
  /// Range of the selection.
  pub range: Range,

  /// The parent selection range containing this range.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub parent: Option<Box<SelectionRange>>,
}

pub trait SelectionRangeService<
  P: crate::database::storage::Partitions,
  T: crate::protocol::lsp::LanguageServer<P>,
>: Send + Sync + 'static
{
  /// The [`textDocument/selectionRange`] request is sent from the client to the
  /// server to return suggested selection ranges at an array of given
  /// positions. A selection range is a range around the cursor position which
  /// the user might be interested in selecting.
  ///
  /// [`textDocument/selectionRange`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_selectionRange
  ///
  /// A selection range in the return array is for the position in the provided
  /// parameters at the same index. Therefore `params.positions[i]` must be
  /// contained in `result[i].range`.
  ///
  /// # Compatibility
  ///
  /// This request was introduced in specification version 3.15.0.
  fn selection_range(
    &self,
    params: SelectionRangeParams,
    ctx: &mut TaskContext<P, T>,
    writer: &mut PartitionWriteContextRef<'_, P>,
  ) -> impl std::future::Future<
    Output = jsonrpc::Result<Option<Vec<SelectionRange>>>,
  > + Send {
    async move {
      otel::span!(
        "laburnum.lsp.selection_range",
        "document.uri" = params.text_document.uri.to_string(),
        "positions.count" = params.positions.len() as i64
      );

      Err(jsonrpc::Error::method_not_found())
    }
  }
  const SELECTION_RANGE_LANE: crate::scheduler::lanes::Lane =
    crate::scheduler::lanes::DEFAULT_LANE;
}