Skip to main content

logicaffeine_lsp/
inlay_hints.rs

1use lsp_types::{
2    InlayHint, InlayHintKind, InlayHintLabel, InlayHintTooltip, MarkupContent, MarkupKind, Range,
3};
4
5use logicaffeine_compile::analysis::VarState;
6use logicaffeine_language::token::TokenType;
7
8use crate::document::DocumentState;
9use crate::index::{resolve_token_name, DefinitionKind};
10
11/// Handle inlay hints request.
12///
13/// Shows type annotations for variables declared without explicit types.
14pub fn inlay_hints(doc: &DocumentState, range: Range) -> Vec<InlayHint> {
15    let mut hints = Vec::new();
16    let is_default_range = range == Range::default();
17
18    for def in &doc.symbol_index.definitions {
19        if def.kind != DefinitionKind::Variable {
20            continue;
21        }
22        if def.span == logicaffeine_language::token::Span::default() {
23            continue;
24        }
25
26        if let Some(detail) = &def.detail {
27            if detail.contains("(inferred)") {
28                let pos = doc.line_index.position(def.span.end);
29                if !is_default_range && (pos.line < range.start.line || pos.line > range.end.line) {
30                    continue;
31                }
32                let type_label = extract_inferred_type(detail);
33                hints.push(InlayHint {
34                    position: pos,
35                    label: InlayHintLabel::String(format!(": {}", type_label)),
36                    kind: Some(InlayHintKind::TYPE),
37                    text_edits: None,
38                    tooltip: None,
39                    padding_left: Some(false),
40                    padding_right: Some(true),
41                    data: None,
42                });
43            }
44        }
45    }
46
47    // Ownership state hints for non-Owned variables
48    for token in &doc.tokens {
49        if !matches!(
50            &token.kind,
51            TokenType::Identifier
52                | TokenType::ProperName(_)
53                | TokenType::Adjective(_)
54                | TokenType::Noun(_)
55        ) {
56            continue;
57        }
58        if token.span == logicaffeine_language::token::Span::default() {
59            continue;
60        }
61
62        let pos = doc.line_index.position(token.span.end);
63        if !is_default_range && (pos.line < range.start.line || pos.line > range.end.line) {
64            continue;
65        }
66
67        if let Some(name) = resolve_token_name(token, &doc.interner) {
68            if let Some(state) = doc.ownership_states.get(name) {
69                let (label, tooltip) = match state {
70                    VarState::Moved => (
71                        "moved",
72                        "This variable has been given away and can no longer be used.".to_string(),
73                    ),
74                    VarState::MaybeMoved(given_in) => (
75                        "maybe moved",
76                        format!("This variable might have been given away in {given_in}."),
77                    ),
78                    VarState::Borrowed => (
79                        "borrowed",
80                        "This variable is currently borrowed (lent via Show).".to_string(),
81                    ),
82                    VarState::Owned => continue, // Don't show for Owned — that's noise
83                };
84                hints.push(InlayHint {
85                    position: pos,
86                    label: InlayHintLabel::String(format!(" {}", label)),
87                    kind: Some(InlayHintKind::PARAMETER),
88                    text_edits: None,
89                    tooltip: Some(InlayHintTooltip::MarkupContent(MarkupContent {
90                        kind: MarkupKind::Markdown,
91                        value: tooltip,
92                    })),
93                    padding_left: Some(true),
94                    padding_right: Some(false),
95                    data: None,
96                });
97            }
98        }
99    }
100
101    hints
102}
103
104/// Extract the inferred type from a detail string like "Let x: Int (inferred)".
105/// Returns the type name (e.g. "Int") or "auto" if not found.
106fn extract_inferred_type(detail: &str) -> &str {
107    // Format: "Let [mut ]name: TypeName (inferred)"
108    if let Some(colon_pos) = detail.rfind(": ") {
109        let after_colon = &detail[colon_pos + 2..];
110        if let Some(paren_pos) = after_colon.find(" (inferred)") {
111            let ty = after_colon[..paren_pos].trim();
112            if !ty.is_empty() {
113                return ty;
114            }
115        }
116    }
117    "auto"
118}
119
120#[cfg(test)]
121#[path = "../../logicaffeine_tests/unit/logicaffeine_lsp/inlay_hints/tests.rs"]
122mod tests;