Skip to main content

oak_core/tree/
metadata.rs

1use core::range::Range;
2
3/// Represents the provenance of a token, describing how its text was composed.
4///
5/// This is used for advanced IDE features like renaming synthetic identifiers.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct TokenProvenance {
9    /// The parts that compose this token's text.
10    pub parts: Vec<ProvenancePart>,
11}
12
13/// A single component of a token's provenance.
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub enum ProvenancePart {
17    /// Part of the text comes directly from a source range.
18    Source(#[cfg_attr(feature = "serde", serde(with = "crate::serde_range"))] Range<usize>),
19    /// Part of the text is synthesized (e.g., by a macro).
20    /// The string is typically an interned ID or a small literal.
21    Synthesized(String),
22    /// An opaque tag for language-specific transformations (e.g., case conversion).
23    /// Oak doesn't understand these tags, but passes them to the LSP/IDE.
24    OpaqueTag(String),
25}
26
27impl TokenProvenance {
28    /// Creates a new provenance from a single source range.
29    pub fn from_source(range: Range<usize>) -> Self {
30        Self { parts: vec![ProvenancePart::Source(range)] }
31    }
32
33    /// Creates a new provenance from a synthesized string.
34    pub fn from_synthesized(s: impl Into<String>) -> Self {
35        Self { parts: vec![ProvenancePart::Synthesized(s.into())] }
36    }
37}