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

@ -27,9 +27,20 @@ type Bound =
Within: Range
Type: string }
type Index = { Uses: Use list; Bounds: Bound list }
/// A field read, `(.x r)` or `(record-ref r x)`: the field, the form, the
/// field's type there, and the record's type.
type FieldUse =
{ Field: string
Range: Range
Type: string
Record: string }
let private empty = { Uses = []; Bounds = [] }
type Index =
{ Uses: Use list
Bounds: Bound list
Fields: FieldUse list }
let private empty = { Uses = []; Bounds = []; Fields = [] }
/// A name the compiler invented in place of one the program wrote: `x__3`,
/// a generated suspending copy `f__bjo`, and `unused__4` for `_`.
@ -46,8 +57,15 @@ let private written (name: string) =
let private read (env: Env) (file: string) (decls: TDecl list) : Index =
let uses = ResizeArray<Use>()
let bounds = ResizeArray<Bound>()
let fields = ResizeArray<FieldUse>()
let inFile (r: Range) = r.File <> "" && IO.Path.GetFullPath r.File = file
// The file's own types by the names it writes: `Leg`, not `fields/Leg`.
let ownQualifier =
Text.RegularExpressions.Regex(
@"(?<![\w./-])" + Text.RegularExpressions.Regex.Escape(IO.Path.GetFileNameWithoutExtension file) + "/"
)
let show (t: HMType) =
let settled =
try
@ -55,7 +73,7 @@ let private read (env: Env) (file: string) (decls: TDecl list) : Index =
with _ ->
t
DotNetInterop.showType settled
ownQualifier.Replace(DotNetInterop.showType settled, "")
let bind (name: string) (within: Range) (t: HMType) =
if inFile within && not (invented name) then
@ -96,6 +114,12 @@ let private read (env: Env) (file: string) (decls: TDecl list) : Index =
Range = e.Range
Type = show e.Type
Generic = not tArgs.IsEmpty }
| TGetField(target, field) when inFile e.Range ->
fields.Add
{ Field = field
Range = e.Range
Type = show e.Type
Record = show target.Type }
| TLet(name, _, _, value, _) -> bind name e.Range value.Type
| TLetMutable(name, value, _) -> bind name e.Range value.Type
| TLetRec(bindings, _) -> for (name, _, _, value) in bindings do bind name e.Range value.Type
@ -147,7 +171,9 @@ let private read (env: Env) (file: string) (decls: TDecl list) : Index =
| _ -> ()
List.iter decl decls
{ Uses = List.ofSeq uses; Bounds = List.ofSeq bounds }
{ Uses = List.ofSeq uses
Bounds = List.ofSeq bounds
Fields = List.ofSeq fields }
/// Each file's index, from its last check.
let private byFile = Collections.Generic.Dictionary<string, Index>()
@ -172,7 +198,7 @@ let recording (path: string) (text: string) (check: unit -> 'a) : 'a =
Some(fun env decls ->
// The first program with names in the file is the file's: one
// checked before it is a dependency's.
if index.Uses.IsEmpty && index.Bounds.IsEmpty then
if index.Uses.IsEmpty && index.Bounds.IsEmpty && index.Fields.IsEmpty then
index <- read env full decls)
try
@ -212,3 +238,13 @@ let boundAt (path: string) (name: string) (at: Range) : string option =
|> List.sortBy (fun b -> size b.Within)
|> List.tryHead
|> Option.map (fun b -> b.Type))
/// The field read written at `at`: the innermost one of `field` whose form
/// holds it.
let fieldAt (path: string) (field: string) (at: Range) : FieldUse option =
indexOf path
|> Option.bind (fun index ->
index.Fields
|> List.filter (fun f -> f.Field = field && holds at f.Range)
|> List.sortBy (fun f -> size f.Range)
|> List.tryHead)