Inferred types: in hovers, and as inlay hints

A check keeps what the compiler inferred, through Inference.observe, as
each use of a name with its type there and each binding with the type it
binds. A hover shows (: name type) for a local, and a generic function's
type at the use hovered. Inlay hints show the type after each local name
where it is bound and no type is written.

Hints before the first change since the last check are sent; the ones after
it wait for the check, which then asks the client to ask again. A hover runs
a check that is due first.

🤖 Generated with [ECA](https://eca.dev)

Co-Authored-By: eca-agent <git@eca.dev>
This commit is contained in:
Linus Björnstam 2026-10-22 12:58:19 +02:00
parent cc62a8a360
commit 471b49d9e4
9 changed files with 571 additions and 20 deletions

View file

@ -48,6 +48,10 @@ let private str (node: JsonNode) (path: string list) =
/// follow the buffer.
let private texts = Collections.Generic.Dictionary<string, string>()
/// Whether the client takes `workspace/inlayHint/refresh`, as it says when it
/// starts.
let mutable private refreshesHints = false
let private publish (uri: string) =
let buffers =
texts |> Seq.map (fun kv -> IO.Path.GetFullPath(uriToPath kv.Key), kv.Value) |> Map.ofSeq
@ -74,6 +78,11 @@ let private publish (uri: string) =
Rpc.notify "textDocument/publishDiagnostics" (Check.publishParams uri (note @ found))
// The hints are of this check's types. A client asks for them as the text
// changes, which is before the check, so it is told to ask again.
if refreshesHints then
Rpc.request "workspace/inlayHint/refresh" null
/// When each changed file is next to be checked. A check waits for typing to
/// pause, as one per keystroke would queue up behind each other.
let private dueChecks = Collections.Generic.Dictionary<string, DateTime>()
@ -161,19 +170,34 @@ let private describe (uri: string) (name: string) : string option =
| Some found -> Some(Hover.text found)
| None -> Hover.builtin name
/// The type the last check inferred for the local `b`: at the use written at
/// `at`, or where it is bound.
let private localType (uri: string) (b: Scope.Binder) (at: Bjolang.Lexer.Range) : string option =
let path = uriToPath uri
match Types.useAt path b.Name at with
| Some u -> Some u.Type
| None -> Types.boundAt path b.Name b.Range
let private hoverAt (uri: string) (line: int) (character: int) : JsonNode =
let answer (text: string) (at: Bjolang.Lexer.Range) =
JsonObject(dict [ "contents", markdownContent text; "range", range at ]) :> JsonNode
match localAt uri line character with
| Some(b, at) -> answer (Hover.local b (Symbols.sliceOf (textOf uri) b.Shown)) at
| Some(b, at) -> answer (Hover.local b (localType uri b at) (Symbols.sliceOf (textOf uri) b.Shown)) at
| None ->
match Symbols.nameAt (uriToPath uri) (textOf uri) (line + 1) character with
| None -> null
| Some(name, at) ->
let here =
match Types.useAt (uriToPath uri) name at with
| Some u when u.Generic -> [ Hover.instantiated name u.Type ]
| _ -> []
match describe uri name with
| None -> null
| Some text -> answer text at
| None when here.IsEmpty -> null
| None -> answer (String.concat "\n\n" here) at
| Some text -> answer (String.concat "\n\n" (text :: here)) at
/// The completions at a position. Each replaces the whole word before the
/// cursor. Only names that start with the word's first character are sent,
@ -225,7 +249,7 @@ let private completionAt (uri: string) (line: int) (character: int) : JsonNode =
let localDoc =
locals
|> List.map (fun b -> b.Name, Hover.local b (Symbols.sliceOf text b.Shown))
|> List.map (fun b -> b.Name, Hover.local b (Types.boundAt (uriToPath uri) b.Name b.Range) (Symbols.sliceOf text b.Shown))
|> Map.ofList
let items =
@ -251,6 +275,26 @@ let private completionAt (uri: string) (line: int) (character: int) : JsonNode =
JsonObject(dict [ "isIncomplete", JsonValue.Create false :> JsonNode; "items", JsonArray(Array.ofList items) ])
/// The inlay hints in an LSP range of lines.
let private hintsIn (uri: string) (first: int) (last: int) : JsonNode =
Inlay.between (uriToPath uri) (textOf uri) (first + 1) (last + 1)
|> List.map (fun h ->
JsonObject(
dict
[ "position",
JsonObject(
dict [ "line", JsonValue.Create(h.At.Line - 1) :> JsonNode; "character", JsonValue.Create h.At.Column ]
)
:> JsonNode
"label", JsonValue.Create h.Label
"kind", JsonValue.Create 1
"tooltip", JsonValue.Create h.Tooltip ]
)
:> JsonNode)
|> Array.ofList
|> JsonArray
:> JsonNode
/// A completion item with its doc, which is the hover of its name.
let private resolve (item: JsonNode) : JsonNode =
let data = item["data"]
@ -273,8 +317,9 @@ let private capabilities () : JsonNode =
"documentSymbolProvider": true,
"definitionProvider": true,
"hoverProvider": true,
"inlayHintProvider": true,
"completionProvider": { "resolveProvider": true, "triggerCharacters": [ "(" ] } },
"serverInfo": { "name": "bjolang-lsp", "version": "0.4.0" } }
"serverInfo": { "name": "bjolang-lsp", "version": "0.5.0" } }
"""
[<EntryPoint>]
@ -345,7 +390,15 @@ let main _ =
| _ ->
try
match methodName with
| "initialize" -> Rpc.respond id (capabilities ())
| "initialize" ->
refreshesHints <-
[ "params"; "capabilities"; "workspace"; "inlayHint"; "refreshSupport" ]
|> List.fold (fun (n: JsonNode) key -> if isNull n then null else n[key]) message
|> function
| null -> false
| v -> v.GetValue<bool>()
Rpc.respond id (capabilities ())
| "shutdown" ->
shutdownAsked <- true
Rpc.respond id null
@ -385,6 +438,12 @@ let main _ =
let character = position["character"].GetValue<int>()
let answer = if methodName = "textDocument/hover" then hoverAt else definitionAt
// A hover's types are from the last check, which has
// to be of the text as it stands for them to be at
// the right places.
if methodName = "textDocument/hover" then
uri () |> Option.iter (fun u -> if dueChecks.Remove u then publish u)
Rpc.respond id (uri () |> Option.map (fun u -> answer u line character) |> Option.defaultValue null)
| "textDocument/completion" ->
let position = message["params"]["position"]
@ -393,6 +452,17 @@ let main _ =
Rpc.respond id (uri () |> Option.map (fun u -> completionAt u line character) |> Option.defaultValue null)
| "completionItem/resolve" -> Rpc.respond id (resolve message["params"])
// Not run first when a check is due, as a hover is: a
// client asks for hints on every change. The check sends
// a refresh when it has run.
| "textDocument/inlayHint" ->
let r = message["params"]["range"]
let startsAt = r["start"]
let endsAt = r["end"]
let first = startsAt["line"].GetValue<int>()
let last = endsAt["line"].GetValue<int>()
Rpc.respond id (uri () |> Option.map (fun u -> hintsIn u first last) |> Option.defaultValue (JsonArray()))
// Not part of LSP: the expansion of the macro call at a
// position, from the file's last check. A check that is
// due runs first, so that the expansion is of the text as
@ -422,6 +492,8 @@ let main _ =
|> Option.defaultValue null
Rpc.respond id answer
// The client's answer to a request of the server's.
| "" -> ()
| _ when not (isNull id) -> Rpc.respondError id -32601 $"%s{methodName} is not supported"
// Any other notification, `initialized` included, needs no
// answer.