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