Skip to main content

logicaffeine_lsp/
document_symbols.rs

1use lsp_types::{DocumentSymbol, Range, SymbolKind};
2
3use crate::document::DocumentState;
4use crate::index::DefinitionKind;
5use crate::line_index::LineIndex;
6
7#[allow(deprecated)]
8pub fn document_symbols(doc: &DocumentState) -> Vec<DocumentSymbol> {
9    let mut symbols = Vec::new();
10
11    // Add block-level symbols (## Main, ## To, etc.)
12    for (name, block_type, span) in &doc.symbol_index.block_spans {
13        let kind = match block_type {
14            logicaffeine_language::token::BlockType::Main => SymbolKind::FUNCTION,
15            logicaffeine_language::token::BlockType::Function => SymbolKind::FUNCTION,
16            logicaffeine_language::token::BlockType::Theorem => SymbolKind::CLASS,
17            logicaffeine_language::token::BlockType::Definition
18            | logicaffeine_language::token::BlockType::TypeDef => SymbolKind::STRUCT,
19            logicaffeine_language::token::BlockType::Policy => SymbolKind::INTERFACE,
20            logicaffeine_language::token::BlockType::Proof => SymbolKind::METHOD,
21            // The LGX blocks, kinded for what the reader is looking AT rather than left in the
22            // catch-all: a page, a route, a store and a directive all landing on NAMESPACE made
23            // the outline — the one view of a file an editor gives you for free — say nothing
24            // about the file's shape.
25            logicaffeine_language::token::BlockType::Component
26            | logicaffeine_language::token::BlockType::Page { .. }
27            | logicaffeine_language::token::BlockType::ErrorPage { .. } => SymbolKind::CLASS,
28            logicaffeine_language::token::BlockType::Route { .. }
29            | logicaffeine_language::token::BlockType::Middleware
30            | logicaffeine_language::token::BlockType::Redirect { .. } => SymbolKind::METHOD,
31            // A store is shared state a whole page reads through, which is what a module is.
32            logicaffeine_language::token::BlockType::Store => SymbolKind::MODULE,
33            // A directive is behaviour attached to an element — a function, called by the view.
34            logicaffeine_language::token::BlockType::Directive => SymbolKind::FUNCTION,
35            _ => SymbolKind::NAMESPACE,
36        };
37
38        let range = span_to_range(span, &doc.line_index);
39        let selection_range = Range {
40            start: doc.line_index.position(span.start),
41            end: doc
42                .line_index
43                .position(span.start + name.len().min(span.end - span.start)),
44        };
45
46        symbols.push(DocumentSymbol {
47            name: name.clone(),
48            detail: None,
49            kind,
50            tags: None,
51            deprecated: None,
52            range,
53            selection_range,
54            children: None,
55        });
56    }
57
58    // Add definitions from the symbol index, nesting children under parents.
59    // Track index of most recent parent symbol (Struct, Enum, Function) for nesting.
60    let mut current_parent_idx: Option<usize> = None;
61
62    for def in &doc.symbol_index.definitions {
63        if def.span == logicaffeine_language::token::Span::default() {
64            continue; // Skip definitions without known positions
65        }
66
67        let kind = match def.kind {
68            DefinitionKind::Function => SymbolKind::FUNCTION,
69            DefinitionKind::Variable => SymbolKind::VARIABLE,
70            DefinitionKind::Struct => SymbolKind::STRUCT,
71            DefinitionKind::Enum => SymbolKind::ENUM,
72            DefinitionKind::Field => SymbolKind::FIELD,
73            DefinitionKind::Parameter => SymbolKind::VARIABLE,
74            DefinitionKind::Variant => SymbolKind::ENUM_MEMBER,
75            DefinitionKind::Block => continue, // Already added from block_spans
76            DefinitionKind::Theorem => SymbolKind::CLASS,
77            DefinitionKind::Component => SymbolKind::CLASS,
78            DefinitionKind::Action => SymbolKind::METHOD,
79            DefinitionKind::StateField => SymbolKind::PROPERTY,
80        };
81
82        let range = span_to_range(&def.span, &doc.line_index);
83
84        let symbol = DocumentSymbol {
85            name: def.name.clone(),
86            detail: def.detail.clone(),
87            kind,
88            tags: None,
89            deprecated: None,
90            range,
91            selection_range: range,
92            children: None,
93        };
94
95        // Decide whether this is a child of the current parent
96        let is_child = matches!(
97            def.kind,
98            DefinitionKind::Field | DefinitionKind::Parameter | DefinitionKind::Variant
99        );
100
101        if is_child {
102            if let Some(parent_idx) = current_parent_idx {
103                let parent = &mut symbols[parent_idx];
104                parent.children.get_or_insert_with(Vec::new).push(symbol);
105                continue;
106            }
107        }
108
109        // This is a top-level symbol; track it as a potential parent
110        if matches!(
111            def.kind,
112            DefinitionKind::Struct | DefinitionKind::Enum | DefinitionKind::Function
113        ) {
114            current_parent_idx = Some(symbols.len());
115        } else {
116            // Non-parent top-level (Variable, Theorem, etc.) — reset parent tracking
117            current_parent_idx = None;
118        }
119
120        // Try nesting Variable/Theorem under their containing block
121        if matches!(def.kind, DefinitionKind::Variable | DefinitionKind::Theorem) {
122            if let Some(block_idx) = find_containing_block(&symbols, &def.span, &doc.line_index) {
123                symbols[block_idx]
124                    .children
125                    .get_or_insert_with(Vec::new)
126                    .push(symbol);
127                continue;
128            }
129        }
130
131        symbols.push(symbol);
132    }
133
134    symbols
135}
136
137/// Find the block symbol whose range contains the given span.
138#[allow(deprecated)]
139fn find_containing_block(
140    symbols: &[DocumentSymbol],
141    span: &logicaffeine_language::token::Span,
142    line_index: &LineIndex,
143) -> Option<usize> {
144    let pos = line_index.position(span.start);
145    for (i, sym) in symbols.iter().enumerate() {
146        let is_block = matches!(
147            sym.kind,
148            SymbolKind::FUNCTION
149                | SymbolKind::CLASS
150                | SymbolKind::STRUCT
151                | SymbolKind::INTERFACE
152                | SymbolKind::METHOD
153                | SymbolKind::NAMESPACE
154        );
155        if is_block && pos.line >= sym.range.start.line && pos.line <= sym.range.end.line {
156            return Some(i);
157        }
158    }
159    None
160}
161
162#[cfg(test)]
163#[path = "../../logicaffeine_tests/unit/logicaffeine_lsp/document_symbols/tests.rs"]
164mod tests;
165
166fn span_to_range(span: &logicaffeine_language::token::Span, line_index: &LineIndex) -> Range {
167    Range {
168        start: line_index.position(span.start),
169        end: line_index.position(span.end),
170    }
171}