Skip to main content

oak_structural_view/
lib.rs

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