Skip to main content

oak_structure/
lib.rs

1#![feature(new_range_api)]
2#![warn(missing_docs)]
3//! Document structure support for the Oak language framework.
4//!
5//! This crate provides traits and structures for representing the hierarchical
6//! structure of a document, such as for an outline view or breadcrumbs.
7use core::range::Range;
8use oak_core::{
9    language::{Language, UniversalElementRole},
10    tree::RedNode,
11};
12use serde::{Deserialize, Serialize};
13
14/// Represents an item in the document structure (e.g., in an outline or breadcrumbs).
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct StructureItem {
17    /// The name of this item (e.g., function name, class name).
18    pub name: String,
19    /// More detail about this item (e.g., function signature, type).
20    pub detail: Option<String>,
21    /// The universal role of this element.
22    pub role: UniversalElementRole,
23    /// The range of the entire element in the source code.
24    #[serde(with = "oak_core::serde_range", bound(serialize = "", deserialize = ""))]
25    pub range: Range<usize>,
26    /// The range that should be selected when clicking on this item.
27    /// Usually the range of the identifier.
28    #[serde(with = "oak_core::serde_range", bound(serialize = "", deserialize = ""))]
29    pub selection_range: Range<usize>,
30    /// Whether this item is deprecated.
31    pub deprecated: bool,
32    /// Nested structure items (e.g., methods within a class).
33    pub children: Vec<StructureItem>,
34}
35
36/// Trait for languages that support structure view and navigation.
37///
38/// Benchmarked against IntelliJ's Structure View and LSP's `textDocument/documentSymbol`.
39pub trait StructureProvider<L: Language> {
40    /// Returns the hierarchical structure of the document.
41    fn structure(&self, root: &RedNode<L>) -> Vec<StructureItem>;
42}