Skip to main content

logicaffeine_lsp/
references.rs

1use lsp_types::{Location, Position, Range, Url};
2
3use crate::document::DocumentState;
4
5/// The name under the cursor, exactly as the reference machinery reads it.
6pub fn name_at(doc: &DocumentState, position: Position) -> Option<String> {
7    let offset = doc.line_index.offset(position);
8    let token = doc
9        .tokens
10        .iter()
11        .find(|t| offset >= t.span.start && offset < t.span.end)?;
12    doc.source
13        .get(token.span.start..token.span.end)
14        .map(str::to_string)
15}
16
17/// The occurrences of `name` in ANOTHER open document that belong to a
18/// cross-file symbol: unresolved locally (they reach across files), or any
19/// occurrence when this document itself defines the symbol. Definition spans
20/// ride along when `include_defs` (rename edits the definition too).
21pub fn cross_file_candidates(doc: &DocumentState, name: &str, include_defs: bool) -> Vec<Range> {
22    let local_defs = doc.symbol_index.definitions_of(name);
23    let doc_defines = local_defs.iter().any(|d| {
24        matches!(
25            d.kind,
26            crate::index::DefinitionKind::Function
27                | crate::index::DefinitionKind::Struct
28                | crate::index::DefinitionKind::Enum
29                | crate::index::DefinitionKind::Variant
30                | crate::index::DefinitionKind::Theorem
31        )
32    });
33
34    let mut ranges = Vec::new();
35    if include_defs && doc_defines {
36        for def in &local_defs {
37            if def.span != logicaffeine_language::token::Span::default() {
38                ranges.push(Range {
39                    start: doc.line_index.position(def.span.start),
40                    end: doc.line_index.position(def.span.end),
41                });
42            }
43        }
44    }
45    for reference in &doc.symbol_index.references {
46        if reference.name != name {
47            continue;
48        }
49        if reference.definition_idx.is_none() || doc_defines {
50            ranges.push(Range {
51                start: doc.line_index.position(reference.span.start),
52                end: doc.line_index.position(reference.span.end),
53            });
54        }
55    }
56    ranges
57}
58
59/// Is `name` a symbol other files can see from THIS document's point of
60/// view? True when the document doesn't define it (the usage reaches across
61/// files) or defines it as API shape. A local `Let x` is nobody's business.
62pub fn is_cross_file_symbol(doc: &DocumentState, name: &str) -> bool {
63    let defs = doc.symbol_index.definitions_of(name);
64    if defs.is_empty() {
65        return true;
66    }
67    defs.iter().any(|d| {
68        matches!(
69            d.kind,
70            crate::index::DefinitionKind::Function
71                | crate::index::DefinitionKind::Struct
72                | crate::index::DefinitionKind::Enum
73                | crate::index::DefinitionKind::Variant
74                | crate::index::DefinitionKind::Theorem
75        )
76    })
77}
78
79/// Handle find-all-references request.
80pub fn find_references(
81    doc: &DocumentState,
82    position: Position,
83    uri: &Url,
84    include_declaration: bool,
85) -> Vec<Location> {
86    let offset = doc.line_index.offset(position);
87
88    // Find the token at the cursor position
89    // ★ THROUGH THE SHARED RULE. "Find all references" asked from inside a `View:` read the whole
90    // `RawBlock` as the name and answered with nothing — the same failure hover, rename and
91    // go-to-definition each had, for the same reason, in four separate functions.
92    let name = match crate::document::name_at(doc, offset) {
93        Some((n, _)) => n,
94        None => return vec![],
95    };
96
97    let mut locations = Vec::new();
98
99    // Include definition locations if requested
100    if include_declaration {
101        for def in doc.symbol_index.definitions_of(&name) {
102            if def.span != logicaffeine_language::token::Span::default() {
103                locations.push(Location {
104                    uri: uri.clone(),
105                    range: Range {
106                        start: doc.line_index.position(def.span.start),
107                        end: doc.line_index.position(def.span.end),
108                    },
109                });
110            }
111        }
112    }
113
114    // Include all references
115    for reference in doc.symbol_index.references_to(&name) {
116        locations.push(Location {
117            uri: uri.clone(),
118            range: Range {
119                start: doc.line_index.position(reference.span.start),
120                end: doc.line_index.position(reference.span.end),
121            },
122        });
123    }
124
125    locations
126}
127
128#[cfg(test)]
129#[path = "../../logicaffeine_tests/unit/logicaffeine_lsp/references/tests.rs"]
130mod tests;