symbolique 0.1.0

Symbol table pipeline for language servers — parse, link, merge, and resolve symbols across files, built on the laburnum LSP framework.
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! Resolution reader extension trait.
//!
//! This module provides [`ResolutionReadExt`], for querying resolution mappings
//! from `QueryClient`.

use {
  crate::{
    core::{Ident, SymbolPath, Value},
    partitions::{
      records::ResolutionEntry, resolution::SymbolResolution, symbols::Symbols,
    },
  },
  laburnum::database::{
    HasPartition, RecordRef,
    query::QueryClient,
    storage::Partitions,
  },
};

/// Extension trait for reading resolution mappings from partitions.
///
/// Provides typed methods for querying reference-to-definition mappings written
/// during the resolution stage.
pub trait ResolutionReadExt<P: Partitions> {
  /// Look up a resolution by exact sort key.
  fn resolution_at_path<V, I, Path>(
    &self,
    sort_key: &str,
  ) -> Option<ResolutionEntry<V, I, Path>>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    P::Stores: HasPartition<SymbolResolution<V, I, Path>>;

  /// Query resolutions with a sort key prefix.
  fn resolutions_with_prefix<V, I, Path>(
    &self,
    prefix: &str,
  ) -> Vec<(String, ResolutionEntry<V, I, Path>)>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    P::Stores: HasPartition<SymbolResolution<V, I, Path>>;

  /// Follow a resolution entry's target handle to retrieve the full symbol
  /// shape.
  fn resolve_resolution_target<V, I, Path>(
    &self,
    entry: &ResolutionEntry<V, I, Path>,
  ) -> Option<RecordRef<'_, Symbols<V, I, Path>>>
  where
    V: Value<I> + 'static,
    I: Ident,
    Path: SymbolPath + 'static,
    P::Stores: HasPartition<Symbols<V, I, Path>>;
}

impl<P: Partitions> ResolutionReadExt<P> for QueryClient<P> {
  fn resolution_at_path<V, I, Path>(
    &self,
    sort_key: &str,
  ) -> Option<ResolutionEntry<V, I, Path>>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    P::Stores: HasPartition<SymbolResolution<V, I, Path>>,
  {
    self.index_get::<SymbolResolution<V, I, Path>>(sort_key)
  }

  fn resolutions_with_prefix<V, I, Path>(
    &self,
    prefix: &str,
  ) -> Vec<(String, ResolutionEntry<V, I, Path>)>
  where
    V: Value<I>,
    I: Ident,
    Path: SymbolPath,
    P::Stores: HasPartition<SymbolResolution<V, I, Path>>,
  {
    self.index_range::<SymbolResolution<V, I, Path>>(prefix)
  }

  fn resolve_resolution_target<V, I, Path>(
    &self,
    entry: &ResolutionEntry<V, I, Path>,
  ) -> Option<RecordRef<'_, Symbols<V, I, Path>>>
  where
    V: Value<I> + 'static,
    I: Ident,
    Path: SymbolPath + 'static,
    P::Stores: HasPartition<Symbols<V, I, Path>>,
  {
    self.get::<Symbols<V, I, Path>>(entry.target.content_hash())
  }
}