bock-lsp 1.0.0

Language Server Protocol implementation for the Bock language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! `textDocument/inlayHint` support — inferred type hints for `let` bindings.
//!
//! For every `let` (or `let mut`) binding *without* an explicit type
//! annotation, the LSP renders an inline `: T` hint immediately after the
//! binding name, where `T` is the type the checker inferred for the binding.
//! `for`-loop binders are collected through the same machinery (the binder
//! pattern survives the checker's iterator desugaring with its `NodeId`
//! intact), so they produce hints whenever the element type resolves.
//!
//! Flow:
//! 1. Run the hover pipeline (lex → parse → resolve → lower → check).
//! 2. *Before* checking, walk the freshly lowered AIR module and record every
//!    bind-pattern site under an unannotated `let` or a `for` binder. Doing
//!    this pre-check matters: the checker synthesizes its own helper
//!    `let __bock_iter_N = …` bindings while desugaring `for` loops, and
//!    collecting first keeps those out of the hint set.
//! 3. After checking, look each site's inferred type up by the pattern's
//!    `NodeId`, drop any that are unresolved or poisoned by type errors, and
//!    render the rest via [`format_type`](crate::type_display::format_type).
//!
//! Hints whose insertion point falls outside the requested range are
//! filtered out, and pathologically long type renders are truncated to
//! [`TYPE_RENDER_BUDGET`] characters.

use std::path::PathBuf;

use bock_air::{
    lower_module, resolve_names_with_registry, visitor::walk_node, visitor::Visitor, AIRNode,
    ModuleRegistry, NodeId, NodeIdGen, NodeKind, SymbolTable,
};
use bock_errors::{FileId, Span};
use bock_lexer::Lexer;
use bock_parser::Parser;
use bock_source::SourceMap;
use bock_types::{seed_imports, seed_prelude, Type, TypeChecker};
use tower_lsp::lsp_types::Range;

use crate::goto_definition::position_to_offset;
use crate::pipeline::register_builtins;
use crate::type_display::format_type;

/// Maximum number of characters of a rendered type shown in a hint label.
/// Longer renders are cut at the budget and terminated with `…`.
pub const TYPE_RENDER_BUDGET: usize = 60;

/// One inferred-type hint, ready for conversion to an LSP `InlayHint`.
pub struct TypeHint {
    /// Zero-width span at the insertion point — immediately after the
    /// binding name. Convert with [`span_to_range`](crate::span_to_range)
    /// and use the range's `start` as the hint position.
    pub span: Span,
    /// Hint label, including the leading `: ` separator.
    pub label: String,
}

/// Result of computing inlay hints for a document.
pub struct InlayHintsResult {
    /// Owned source map containing the document (keeps `SourceFile`
    /// borrows valid for the lifetime of the result).
    pub source_map: SourceMap,
    /// Id of the added file inside [`InlayHintsResult::source_map`].
    pub file_id: FileId,
    /// Hints inside the requested range, sorted by insertion offset.
    pub hints: Vec<TypeHint>,
}

/// Compute inferred-type inlay hints for the unannotated `let` bindings
/// (and `for` binders) whose names end inside `range`.
#[must_use]
pub fn inlay_hints(path: PathBuf, content: String, range: Range) -> InlayHintsResult {
    let mut source_map = SourceMap::new();
    let file_id = source_map.add_file(path, content);
    let source_file = source_map.get_file(file_id);

    // Clamp the requested range to byte offsets. A position past the end of
    // the document clamps to the document end.
    let eof = source_file.content.len();
    let range_start = position_to_offset(
        &source_file.content,
        range.start.line,
        range.start.character,
    )
    .unwrap_or(eof);
    let range_end = position_to_offset(&source_file.content, range.end.line, range.end.character)
        .unwrap_or(eof);

    // Lex + parse. As in hover, we tolerate diagnostics — a partial AST
    // still yields useful hints for the parts that did check.
    let mut lexer = Lexer::new(source_file);
    let tokens = lexer.tokenize();
    let mut parser = Parser::new(tokens, source_file);
    let module = parser.parse_module();

    // Resolve (single-file — no cross-file registry).
    let registry = ModuleRegistry::new();
    let mut symbols = SymbolTable::new();
    let _ = resolve_names_with_registry(&module, &mut symbols, &registry);

    // Lower to AIR.
    let id_gen = NodeIdGen::new();
    let mut air_module = lower_module(&module, &id_gen, &symbols);

    // Collect hint sites BEFORE type checking: the checker rewrites `for`
    // loops in place and synthesizes helper `let` bindings while doing so;
    // collecting first guarantees only user-written binders produce hints.
    let mut sites = Vec::new();
    SiteCollector { sites: &mut sites }.visit_node(&air_module);

    // Type-check (records inferred pattern types keyed by `NodeId`).
    let mut checker = TypeChecker::new();
    register_builtins(&mut checker);
    seed_prelude(&mut checker, &registry);
    seed_imports(&mut checker, &module.imports, &registry);
    checker.check_module(&mut air_module);

    let mut hints = Vec::new();
    for site in sites {
        let offset = site.name_span.end;
        if offset < range_start || offset > range_end {
            continue;
        }
        let Some(ty) = checker.type_of(site.pattern_id) else {
            continue;
        };
        // Re-apply the substitution so lingering inference variables get
        // their final answer (mirrors the hover path).
        let ty = checker.subst.apply(ty);
        if !is_renderable(&ty) {
            continue;
        }
        let label = format!(": {}", truncate_render(format_type(&ty)));
        hints.push(TypeHint {
            span: Span {
                file: file_id,
                start: offset,
                end: offset,
            },
            label,
        });
    }
    hints.sort_unstable_by_key(|h| h.span.start);

    InlayHintsResult {
        source_map,
        file_id,
        hints,
    }
}

/// `true` if `ty` contains no error poison, unresolved inference variable,
/// or sketch-mode placeholder anywhere — i.e. rendering it produces a
/// clean, fully resolved type string rather than `<error>`/`?N` noise.
fn is_renderable(ty: &Type) -> bool {
    match ty {
        Type::Primitive(_) | Type::Named(_) => true,
        Type::Generic(g) => g.args.iter().all(is_renderable),
        Type::Tuple(elems) => elems.iter().all(is_renderable),
        Type::Function(f) => f.params.iter().all(is_renderable) && is_renderable(&f.ret),
        Type::Optional(inner) => is_renderable(inner),
        Type::Result(ok, err) => is_renderable(ok) && is_renderable(err),
        Type::Refined(base, _) => is_renderable(base),
        Type::TypeVar(_) | Type::Flexible(_) | Type::Error => false,
    }
}

/// Cut a rendered type down to [`TYPE_RENDER_BUDGET`] characters, replacing
/// the overflow with a single `…`.
fn truncate_render(rendered: String) -> String {
    if rendered.chars().count() <= TYPE_RENDER_BUDGET {
        return rendered;
    }
    let mut out: String = rendered.chars().take(TYPE_RENDER_BUDGET - 1).collect();
    out.push('');
    out
}

// ─── AIR walker: collect unannotated binder sites ────────────────────────────

/// One candidate hint site recorded before type checking.
struct HintSite {
    /// AIR `NodeId` of the `BindPat` whose inferred type becomes the hint.
    pattern_id: NodeId,
    /// Span of the binding *name*; the hint is inserted at `span.end`.
    name_span: Span,
}

/// Visitor that records every bind-pattern under an unannotated `let`
/// binding or a `for`-loop binder. Destructuring patterns (tuples, lists,
/// constructors) contribute one site per bound name.
struct SiteCollector<'a> {
    sites: &'a mut Vec<HintSite>,
}

impl Visitor for SiteCollector<'_> {
    fn visit_node(&mut self, node: &AIRNode) {
        match &node.kind {
            NodeKind::LetBinding {
                ty: None, pattern, ..
            } => collect_bind_pats(pattern, self.sites),
            NodeKind::For { pattern, .. } => collect_bind_pats(pattern, self.sites),
            _ => {}
        }
        walk_node(self, node);
    }
}

/// Recursively collect every `BindPat` under `pattern` (handles
/// destructuring: tuple, list, constructor and record patterns).
fn collect_bind_pats(pattern: &AIRNode, sites: &mut Vec<HintSite>) {
    struct BindPatCollector<'a> {
        sites: &'a mut Vec<HintSite>,
    }

    impl Visitor for BindPatCollector<'_> {
        fn visit_node(&mut self, node: &AIRNode) {
            if let NodeKind::BindPat { name, .. } = &node.kind {
                self.sites.push(HintSite {
                    pattern_id: node.id,
                    name_span: name.span,
                });
            }
            walk_node(self, node);
        }
    }

    BindPatCollector { sites }.visit_node(pattern);
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use tower_lsp::lsp_types::Position;

    /// A range that covers any document (end clamps to EOF).
    fn full_range() -> Range {
        Range::new(Position::new(0, 0), Position::new(u32::MAX, 0))
    }

    fn run(src: &str) -> InlayHintsResult {
        inlay_hints(PathBuf::from("test.bock"), src.to_string(), full_range())
    }

    /// Assert that `hint` sits immediately after `name` in `src`.
    fn assert_after_name(src: &str, hint: &TypeHint, name: &str) {
        assert_eq!(
            hint.span.start, hint.span.end,
            "hint span must be zero-width"
        );
        assert!(
            src[..hint.span.start].ends_with(name),
            "hint at offset {} should sit immediately after `{name}`; text before it: {:?}",
            hint.span.start,
            &src[..hint.span.start],
        );
    }

    #[test]
    fn unannotated_let_gets_int_hint_after_name() {
        let src = "\
module m

fn main() {
    let answer = 42
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 1, "expected exactly one hint");
        assert_eq!(result.hints[0].label, ": Int");
        assert_after_name(src, &result.hints[0], "answer");
    }

    #[test]
    fn annotated_let_gets_no_hint() {
        let src = "\
module m

fn main() {
    let answer: Int = 42
}
";
        let result = run(src);
        assert!(
            result.hints.is_empty(),
            "annotated binding must not produce a hint, got: {:?}",
            result.hints.iter().map(|h| &h.label).collect::<Vec<_>>(),
        );
    }

    #[test]
    fn let_mut_gets_hint_after_name() {
        let src = "\
module m

fn main() {
    let mut count = 1
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 1);
        assert_eq!(result.hints[0].label, ": Int");
        assert_after_name(src, &result.hints[0], "count");
    }

    #[test]
    fn inferred_generic_list_type() {
        let src = "\
module m

fn main() {
    let xs = [1, 2, 3]
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 1);
        assert_eq!(result.hints[0].label, ": List[Int]");
        assert_after_name(src, &result.hints[0], "xs");
    }

    #[test]
    fn inferred_optional_from_fn_return() {
        let src = "\
module m

fn find() -> Int? {
    42
}

fn main() {
    let v = find()
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 1, "expected one hint for `v`");
        assert_eq!(result.hints[0].label, ": Int?");
        assert_after_name(src, &result.hints[0], "v");
    }

    #[test]
    fn error_typed_binding_produces_no_hint() {
        let src = "\
module m

fn main() {
    let x = nonexistent_fn(1)
}
";
        let result = run(src);
        assert!(
            result.hints.is_empty(),
            "error-typed binding must not produce a hint, got: {:?}",
            result.hints.iter().map(|h| &h.label).collect::<Vec<_>>(),
        );
    }

    #[test]
    fn unresolved_lambda_binding_produces_no_hint() {
        // With no call site, the lambda's parameter type stays an
        // inference variable — the hint must be suppressed rather than
        // rendering `Fn(?0) -> ?0`.
        let src = "\
module m

fn main() {
    let f = (x) => x
}
";
        let result = run(src);
        for hint in &result.hints {
            assert!(
                !hint.label.contains('?'),
                "hint must not leak inference variables: {}",
                hint.label,
            );
        }
    }

    #[test]
    fn range_filters_hints_outside_it() {
        let src = "\
module m

fn main() {
    let first = 1
    let second = \"two\"
}
";
        // Full range sees both.
        let all = run(src);
        assert_eq!(all.hints.len(), 2, "full range should see both hints");
        assert_eq!(all.hints[0].label, ": Int");
        assert_eq!(all.hints[1].label, ": String");

        // A range covering only line 4 (`let second = …`) sees one.
        let line4 = Range::new(Position::new(4, 0), Position::new(4, 99));
        let result = inlay_hints(PathBuf::from("test.bock"), src.to_string(), line4);
        assert_eq!(result.hints.len(), 1, "line-4 range should see one hint");
        assert_eq!(result.hints[0].label, ": String");
        assert_after_name(src, &result.hints[0], "second");
    }

    #[test]
    fn tuple_destructuring_hints_each_name() {
        let src = "\
module m

fn main() {
    let (a, b) = (1, \"hi\")
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 2, "expected a hint per bound name");
        assert_eq!(result.hints[0].label, ": Int");
        assert_after_name(src, &result.hints[0], "a");
        assert_eq!(result.hints[1].label, ": String");
        assert_after_name(src, &result.hints[1], "b");
    }

    #[test]
    fn long_type_render_is_truncated() {
        // 12-element tuple renders to well over the 60-char budget.
        let src = "\
module m

fn main() {
    let t = (1, \"a\", 1, \"a\", 1, \"a\", 1, \"a\", 1, \"a\", 1, \"a\", 1, \"a\")
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 1);
        let label = &result.hints[0].label;
        assert!(
            label.ends_with(''),
            "truncated label must end in …: {label}"
        );
        // `: ` prefix plus exactly the budget.
        assert_eq!(
            label.chars().count(),
            2 + TYPE_RENDER_BUDGET,
            "label: {label}"
        );
    }

    #[test]
    fn truncate_render_leaves_short_strings_alone() {
        assert_eq!(truncate_render("Int".to_string()), "Int");
        let exactly = "A".repeat(TYPE_RENDER_BUDGET);
        assert_eq!(truncate_render(exactly.clone()), exactly);
    }

    #[test]
    fn truncate_render_cuts_at_budget() {
        let long = "A".repeat(TYPE_RENDER_BUDGET + 40);
        let out = truncate_render(long);
        assert_eq!(out.chars().count(), TYPE_RENDER_BUDGET);
        assert!(out.ends_with(''));
    }

    #[test]
    fn for_loop_binder_gets_element_type_hint() {
        let src = "\
module m

fn main() {
    for x in [1, 2, 3] {
        println(\"hi\")
    }
}
";
        let result = run(src);
        assert_eq!(result.hints.len(), 1, "expected one hint for the binder");
        assert_eq!(result.hints[0].label, ": Int");
        assert_after_name(src, &result.hints[0], "x");
    }

    #[test]
    fn range_past_eof_yields_no_hints() {
        let src = "\
module m

fn main() {
    let x = 1
}
";
        let past_eof = Range::new(Position::new(90, 0), Position::new(99, 0));
        let result = inlay_hints(PathBuf::from("test.bock"), src.to_string(), past_eof);
        assert!(result.hints.is_empty());
    }
}