Hover on fields, and on locals of record types

A field read shows the field's type and its declaration; a local of a
record, struct or union type shows the type. Structs and opaque types get
their symbols, and the file's own types drop their module qualifier.
This commit is contained in:
Linus Björnstam 2026-11-03 14:45:55 +01:00
parent 29977ca301
commit 78d18c9854
6 changed files with 166 additions and 11 deletions

View file

@ -183,15 +183,48 @@ let private localType (uri: string) (b: Scope.Binder) (at: Bjolang.Lexer.Range)
| Some u -> Some u.Type
| None -> Types.boundAt path b.Name b.Range
/// The record, struct or union a type as shown names, if this file sees its
/// definition: `Leg`, `module/Leg`, `(Pair int)`.
let private typeDefinition (uri: string) (shown: string) : Symbols.Found option =
let head =
if shown.StartsWith "(" then
shown.Substring(1).Split([| ' '; ')' |], StringSplitOptions.RemoveEmptyEntries) |> Array.tryHead
else
Some shown
head
|> Option.map (fun h -> h.Substring(h.LastIndexOf '/' + 1))
|> Option.filter (fun h -> h <> "" && Char.IsUpper h[0])
|> Option.bind (Symbols.definition (uriToPath uri) (textOf uri))
|> Option.filter (fun found -> found.Symbol.Kind = Symbols.Kind.Struct || found.Symbol.Kind = Symbols.Kind.Class)
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 (localType uri b at) (Symbols.sliceOf (textOf uri) b.Shown)) at
| Some(b, at) ->
let inferred = localType uri b at
// A record's or a struct's definition beside a local of its type.
let ofType =
inferred
|> Option.bind (typeDefinition uri)
|> Option.map (fun found -> Hover.text found None)
|> Option.toList
answer (String.concat "\n\n---\n\n" (Hover.local b inferred (Symbols.sliceOf (textOf uri) b.Shown) :: ofType)) at
| None ->
match Symbols.nameAt (uriToPath uri) (textOf uri) (line + 1) character with
| None -> null
// `(.x r)`: the field read, with its type there.
| Some(name, at) when
name.Length > 1
&& name.StartsWith "."
&& (Types.fieldAt (uriToPath uri) (name.Substring 1) at).IsSome
->
let f = (Types.fieldAt (uriToPath uri) (name.Substring 1) at).Value
answer (Hover.field f.Field f.Type f.Record (typeDefinition uri f.Record)) at
| Some(name, at) ->
let here =
match Types.useAt (uriToPath uri) name at with