Skip to main content

logicaffeine_lsp/
definition.rs

1use lsp_types::{GotoDefinitionResponse, Location, Position, Range, Url};
2
3use crate::document::DocumentState;
4
5/// Handle go-to-definition request.
6///
7/// Given a cursor position, find the token at that position, look up its
8/// name in the symbol index, and return the definition's location.
9pub fn goto_definition(
10    doc: &DocumentState,
11    position: Position,
12    uri: &Url,
13) -> Option<GotoDefinitionResponse> {
14    let offset = doc.line_index.offset(position);
15
16    // ★ A RESOLVED REFERENCE WINS OVER THE TOKEN UNDER THE CURSOR, and inside a template it is
17    // the only thing there is: a `View:` body arrives as ONE `RawBlock` token, so the lookup
18    // below would read the WHOLE template as the name and match nothing. Jumping from `{count}`
19    // to the `Let count be 0.` it names — the most common movement in an LGX file — was
20    // impossible for that reason alone.
21    //
22    // It is the better answer everywhere else too: a reference was resolved against the nearest
23    // scope when the index was built, while the token path only knows a name.
24    if let Some(reference) = doc
25        .symbol_index
26        .references
27        .iter()
28        .find(|r| offset >= r.span.start && offset < r.span.end)
29    {
30        if let Some(def) = reference
31            .definition_idx
32            .and_then(|i| doc.symbol_index.definitions.get(i))
33        {
34            if def.span != logicaffeine_language::token::Span::default() {
35                return Some(GotoDefinitionResponse::Scalar(Location {
36                    uri: uri.clone(),
37                    range: Range {
38                        start: doc.line_index.position(def.span.start),
39                        end: doc.line_index.position(def.span.end),
40                    },
41                }));
42            }
43        }
44    }
45
46    // Find the token at the cursor position
47    let token = doc
48        .tokens
49        .iter()
50        .find(|t| offset >= t.span.start && offset < t.span.end)?;
51
52    let name = doc.source.get(token.span.start..token.span.end)?;
53
54    // Look up definitions for this name
55    let defs = doc.symbol_index.definitions_of(name);
56    if defs.is_empty() {
57        return None;
58    }
59
60    let locations: Vec<Location> = defs
61        .iter()
62        .filter(|d| d.span != logicaffeine_language::token::Span::default())
63        .map(|d| Location {
64            uri: uri.clone(),
65            range: Range {
66                start: doc.line_index.position(d.span.start),
67                end: doc.line_index.position(d.span.end),
68            },
69        })
70        .collect();
71
72    if locations.is_empty() {
73        None
74    } else if locations.len() == 1 {
75        Some(GotoDefinitionResponse::Scalar(
76            locations.into_iter().next().unwrap(),
77        ))
78    } else {
79        Some(GotoDefinitionResponse::Array(locations))
80    }
81}
82
83#[cfg(test)]
84#[path = "../../logicaffeine_tests/unit/logicaffeine_lsp/definition/tests.rs"]
85mod tests;