84 lines
3.4 KiB
Forth
84 lines
3.4 KiB
Forth
|
|
/// Inlay hints: the type the compiler inferred for each local name where it
|
||
|
|
/// is bound, after the name, where the program writes no type.
|
||
|
|
///
|
||
|
|
/// A local is found as go to definition finds one (`Scope`), and its type is
|
||
|
|
/// the last check's (`Types`). A top-level function's parameters have none:
|
||
|
|
/// its signature, which says their types, is written above it.
|
||
|
|
module BjolangLsp.Inlay
|
||
|
|
|
||
|
|
open System
|
||
|
|
open Bjolang.Lexer
|
||
|
|
open Bjolang.Ast
|
||
|
|
|
||
|
|
type Hint =
|
||
|
|
{ /// Right after the name. Lines are 1-based, columns 0-based.
|
||
|
|
At: Position
|
||
|
|
Label: string
|
||
|
|
Tooltip: string }
|
||
|
|
|
||
|
|
/// How much of a type a hint shows; the tooltip has the rest.
|
||
|
|
let private longest = 40
|
||
|
|
|
||
|
|
/// The symbols in `s` that could be a local's binding: not the name in a
|
||
|
|
/// `(: name Type)`, which has its type written, and not a top-level
|
||
|
|
/// function's parameters.
|
||
|
|
let rec private candidates (topLevel: bool) (s: SExpr) : (string * Range) list =
|
||
|
|
match s with
|
||
|
|
| SAtom { Token = Symbol n; Range = r } -> [ n, r ]
|
||
|
|
| SList(SAtom { Token = Colon } :: _, _) -> []
|
||
|
|
| SList(SAtom { Token = Symbol head } as h :: SList _ :: rest, _) when topLevel && head.StartsWith "def" ->
|
||
|
|
candidates false h @ (rest |> List.collect (candidates false))
|
||
|
|
| SList(items, _) -> items |> List.collect (candidates false)
|
||
|
|
| _ -> []
|
||
|
|
|
||
|
|
let private before (a: Position) (b: Position) =
|
||
|
|
a.Line < b.Line || (a.Line = b.Line && a.Column <= b.Column)
|
||
|
|
|
||
|
|
/// Where `now` first differs from `seen`, as a position; `None` when they
|
||
|
|
/// are the same.
|
||
|
|
let private firstChange (seen: string) (now: string) : Position option =
|
||
|
|
if seen = now then
|
||
|
|
None
|
||
|
|
else
|
||
|
|
let common = Seq.zip seen now |> Seq.takeWhile (fun (a, b) -> a = b) |> Seq.length
|
||
|
|
let prefix = seen.Substring(0, common)
|
||
|
|
let line = prefix |> Seq.filter ((=) '\n') |> Seq.length
|
||
|
|
let lineStart = prefix.LastIndexOf '\n' + 1
|
||
|
|
Some { Line = line + 1; Column = common - lineStart }
|
||
|
|
|
||
|
|
/// The hints between `first` and `last` (1-based lines, inclusive) of `path`,
|
||
|
|
/// whose text is `text`.
|
||
|
|
///
|
||
|
|
/// The types are from the last check. Where the text has changed since, a
|
||
|
|
/// hint after the first change could be at the wrong place, so those wait for
|
||
|
|
/// the next check; the ones before it are where they were.
|
||
|
|
let between (path: string) (text: string) (first: int) (last: int) : Hint list =
|
||
|
|
match Symbols.forms path text, Types.checkedText path with
|
||
|
|
| Some forms, Some seen ->
|
||
|
|
let stale = firstChange seen text
|
||
|
|
|
||
|
|
forms
|
||
|
|
|> List.filter (fun f ->
|
||
|
|
let r = getRange f
|
||
|
|
r.End.Line >= first && r.Start.Line <= last)
|
||
|
|
|> List.collect (candidates true)
|
||
|
|
|> List.filter (fun (n, r) ->
|
||
|
|
n <> "_"
|
||
|
|
&& r.Start.Line >= first
|
||
|
|
&& r.Start.Line <= last
|
||
|
|
&& (match stale with
|
||
|
|
| Some change -> before r.End change
|
||
|
|
| None -> true))
|
||
|
|
|> List.choose (fun (n, r) ->
|
||
|
|
match Scope.find forms r.Start.Line r.Start.Column n with
|
||
|
|
| Some b when b.Range.Start = r.Start ->
|
||
|
|
Types.boundAt path n b.Range
|
||
|
|
|> Option.map (fun t ->
|
||
|
|
let shown = if t.Length <= longest then t else t.Substring(0, longest - 1) + "…"
|
||
|
|
|
||
|
|
{ At = r.End
|
||
|
|
Label = ": " + shown
|
||
|
|
Tooltip = $"(: %s{n} %s{t})" })
|
||
|
|
| _ -> None)
|
||
|
|
| _ -> []
|