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::{
  Ident,
  SourceKey,
  Uri,
  database::{
    DynPartition,
    PartitionKey,
  },
  protocol::lsp::SymbolKind,
};

pub struct WorkspaceSymbols;

impl PartitionKey for WorkspaceSymbols {
  const KEY: Ident = Ident::new("laburnum::workspace_symbols");
}
/// To write workspace symbols, create a wrapper partition with
/// [`impl_partition_for_dyn!`]:
///
/// ```rust,ignore
/// use laburnum::{impl_partition_for_dyn, partitions::WorkspaceSymbols};
///
/// // Your record type must implement WorkspaceSymbolRecord + Record
/// impl_partition_for_dyn!(MyWorkspaceSymbolsPartition, WorkspaceSymbols, MySymbol);
///
/// // Then write using the wrapper partition
/// let sort_key = WorkspaceSymbolSortKey::Symbol { name_lowercase, kind, source_key };
/// writer.write::<MyWorkspaceSymbolsPartition>(sort_key, symbol.into());
/// ```
///
/// [`impl_partition_for_dyn!`]: crate::impl_partition_for_dyn
impl DynPartition for WorkspaceSymbols {
  type DynSortKey = WorkspaceSymbolSortKey;
  type RecordConstraint = dyn WorkspaceSymbolRecord;
}

impl<R: WorkspaceSymbolRecord + crate::record::Record>
  crate::database::DynPartitionRecord<WorkspaceSymbols> for R
{
}

#[derive(Debug, Clone)]
pub enum WorkspaceSymbolSortKey {
  Symbol {
    name_lowercase: String,
    kind:           SymbolKind,
    source_key:     SourceKey,
  },
  All,
  NamePrefix {
    name_lowercase: String,
  },
}

impl std::fmt::Display for WorkspaceSymbolSortKey {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      | WorkspaceSymbolSortKey::Symbol {
        name_lowercase,
        kind,
        source_key,
      } => write!(f, "{}|{}|{}", name_lowercase, kind, source_key),
      | WorkspaceSymbolSortKey::All => Ok(()),
      | WorkspaceSymbolSortKey::NamePrefix { name_lowercase } => {
        write!(f, "{}", name_lowercase)
      },
    }
  }
}

pub trait WorkspaceSymbolRecord: Send + Sync + std::fmt::Debug {
  fn name(&self) -> &str;

  fn kind(&self) -> SymbolKind;

  fn uri(&self) -> &Uri;

  fn source_key(&self) -> SourceKey;

  fn range(&self) -> crate::protocol::lsp::Range;

  fn container_name(&self) -> Option<&str>;
}