oak-structural-view 0.0.11

AST-based document structure and structure generation for the Oak framework.
Documentation
#![feature(new_range_api)]
#![warn(missing_docs)]
#![doc = include_str!("readme.md")]
use core::range::Range;
use oak_core::{
    language::{Language, UniversalElementRole},
    tree::RedNode,
};

/// Represents an item in the document structure (e.g., in an outline or breadcrumbs).
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StructureItem {
    /// The name of this item (e.g., function name, class name).
    pub name: String,
    /// More detail about this item (e.g., function signature, type).
    pub detail: Option<String>,
    /// The universal role of this element.
    pub role: UniversalElementRole,
    /// The range of the entire element in the source code.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range", bound(serialize = "", deserialize = "")))]
    pub range: Range<usize>,
    /// The range that should be selected when clicking on this item.
    /// Usually the range of the identifier.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range", bound(serialize = "", deserialize = "")))]
    pub selection_range: Range<usize>,
    /// Whether this item is deprecated.
    pub deprecated: bool,
    /// Nested structure items (e.g., methods within a class).
    pub children: Vec<StructureItem>,
}

/// Trait for languages that support structure view and navigation.
///
/// Benchmarked against IntelliJ's Structure View and LSP's `textDocument/documentSymbol`.
pub trait StructureProvider<L: Language> {
    /// Returns the hierarchical structure of the document.
    fn structure(&self, root: &RedNode<L>) -> Vec<StructureItem>;
}