1
/**
2
 * Source map for mapping span IDs to source code locations and related metadata.
3
 * This is used for error reporting and diagnostics.
4
 */
5
use crate::diagnostics::diagnostics_api::{Position, SymbolKind};
6
use rangemap::RangeMap;
7
pub type SpanId = u32;
8

            
9
#[derive(Debug, Clone)]
10
pub struct HoverInfo {
11
    pub description: String,       // keyword description, type info...
12
    pub kind: Option<SymbolKind>,  // var, domain, function...
13
    pub ty: Option<String>,        // type info like int(0..10)
14
    pub decl_span: Option<SpanId>, // where declared (not sure that's doable)
15
}
16
// source span with start and end positions
17
// in the essence source code
18
#[derive(Debug, Clone)]
19
pub struct SourceSpan {
20
    pub start_byte: usize, // byte offset in the source code
21
    pub end_byte: usize,
22
    pub start_point: Position,
23
    pub end_point: Position,
24
    pub hover_info: Option<HoverInfo>,
25
}
26

            
27
// can add more metadata for hovering and stuff
28
#[derive(Debug, Default, Clone)]
29
pub struct SourceMap {
30
    pub spans: Vec<SourceSpan>,
31
    pub by_byte: RangeMap<usize, SpanId>,
32
}
33

            
34
// allocate a new span and return span id
35
// put the position of the span in the source map
36
336742
pub fn alloc_span(
37
336742
    range: tree_sitter::Range,
38
336742
    source_map: &mut SourceMap,
39
336742
    hover_info: Option<HoverInfo>,
40
336742
) -> SpanId {
41
336742
    let span_id = source_map.spans.len() as SpanId;
42
336742
    source_map.spans.push(SourceSpan {
43
336742
        start_byte: range.start_byte,
44
336742
        end_byte: range.end_byte,
45
336742
        start_point: Position {
46
336742
            line: range.start_point.row as u32,
47
336742
            character: range.start_point.column as u32,
48
336742
        },
49
336742
        end_point: Position {
50
336742
            line: range.end_point.row as u32,
51
336742
            character: range.end_point.column as u32,
52
336742
        },
53
336742
        hover_info,
54
336742
    });
55
    // map byte offsets to span id (RangeMap handles lookup)
56
    // tree-sitter can generate zero-length ranges for missing tokens;
57
    // avoid inserting empty ranges, which RangeMap rejects.
58
336742
    if range.start_byte < range.end_byte {
59
336703
        source_map
60
336703
            .by_byte
61
336703
            .insert(range.start_byte..range.end_byte, span_id);
62
336703
    }
63
336742
    span_id
64
336742
}
65

            
66
impl SourceMap {
67
    // helper to get hover info for a given byte offset (e.g. cursor position)
68
    pub fn span_id_at_byte(&self, byte: usize) -> Option<SpanId> {
69
        self.by_byte.get(&byte).copied()
70
    }
71

            
72
    // helper to get hover info for a given byte offset (e.g. cursor position)
73
    pub fn hover_info_at_byte(&self, byte: usize) -> Option<&HoverInfo> {
74
        self.span_id_at_byte(byte)
75
            .and_then(|span_id| self.spans.get(span_id as usize))
76
            .and_then(|span| span.hover_info.as_ref())
77
    }
78
}
79

            
80
// helper to allocate a span with hover info directly from a tree-sitter node
81
// source is not used yet but could be for more complex hover info (e.g. showing the actual code snippet)
82
336742
pub fn span_with_hover(
83
336742
    node: &tree_sitter::Node,
84
336742
    _source: &str,
85
336742
    map: &mut SourceMap,
86
336742
    info: HoverInfo,
87
336742
) -> SpanId {
88
336742
    alloc_span(node.range(), map, Some(info))
89
336742
}