laburnum 1.17.1

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::{
        DynamicRegistrationClientCapabilities,
        PartialResultParams,
        TextDocumentPositionParams,
        TextDocumentRegistrationOptions,
        WorkDoneProgressOptions,
        WorkDoneProgressParams,
      },
    },
    scheduler::task::TaskContext,
  },
  serde::{
    Deserialize,
    Serialize,
  },
};

pub type MonikerClientCapabilities = DynamicRegistrationClientCapabilities;

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum MonikerServerCapabilities {
  Options(MonikerOptions),
  RegistrationOptions(MonikerRegistrationOptions),
}

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

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MonikerRegistrationOptions {
  #[serde(flatten)]
  pub text_document_registration_options: TextDocumentRegistrationOptions,

  #[serde(flatten)]
  pub moniker_options: MonikerOptions,
}

/// Moniker uniqueness level to define scope of the moniker.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum UniquenessLevel {
  /// The moniker is only unique inside a document
  Document,
  /// The moniker is unique inside a project for which a dump got created
  Project,
  /// The moniker is unique inside the group to which a project belongs
  Group,
  /// The moniker is unique inside the moniker scheme.
  Scheme,
  /// The moniker is globally unique
  Global,
}

/// The moniker kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum MonikerKind {
  /// The moniker represent a symbol that is imported into a project
  Import,
  /// The moniker represent a symbol that is exported into a project
  Export,
  /// The moniker represents a symbol that is local to a project (e.g. a local
  /// variable of a function, a class not visible outside the project, ...)
  Local,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MonikerParams {
  #[serde(flatten)]
  pub text_document_position_params: TextDocumentPositionParams,

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

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

/// Moniker definition to match LSIF 0.5 moniker definition.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Moniker {
  /// The scheme of the moniker. For example tsc or .Net
  pub scheme: String,

  /// The identifier of the moniker. The value is opaque in LSIF however
  /// schema owners are allowed to define the structure if they want.
  pub identifier: String,

  /// The scope in which the moniker is unique
  pub unique: UniquenessLevel,

  /// The moniker kind if known.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub kind: Option<MonikerKind>,
}

pub trait MonikerService<
  P: crate::database::storage::Partitions,
  T: crate::protocol::lsp::LanguageServer<P>,
>: Send + Sync + 'static
{
  /// The [`textDocument/moniker`] request is sent from the client to the server
  /// to get the symbol monikers for a given text document position.
  ///
  /// [`textDocument/moniker`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_moniker
  ///
  /// An array of `Moniker` types is returned as response to indicate possible
  /// monikers at the given location. If no monikers can be calculated,
  /// `Some(vec![])` or `None` should be returned.
  ///
  /// # Concept
  ///
  /// The Language Server Index Format (LSIF) introduced the concept of _symbol
  /// monikers_ to help associate symbols across different indexes. This
  /// request adds capability for LSP server implementations to provide the
  /// same symbol moniker information given a text document position.
  ///
  /// Clients can utilize this method to get the moniker at the current location
  /// in a file the user is editing and do further code navigation queries in
  /// other services that rely on LSIF indexes and link symbols together.
  ///
  /// # Compatibility
  ///
  /// This request was introduced in specification version 3.16.0.
  fn moniker(
    &self,
    params: MonikerParams,
    ctx: &mut TaskContext<P, T>,
    writer: &mut PartitionWriteContextRef<'_, P>,
  ) -> impl std::future::Future<Output = jsonrpc::Result<Option<Vec<Moniker>>>> + Send
  {
    async move {
      otel::span!(
        "laburnum.lsp.moniker",
        "document.uri" = params
          .text_document_position_params
          .text_document
          .uri
          .to_string(),
        "position.line" =
          params.text_document_position_params.position.line as i64,
        "position.character" =
          params.text_document_position_params.position.character as i64
      );

      let _ = params;

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