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.
355 lines
13 KiB
FSharp
355 lines
13 KiB
FSharp
/// What a hover shows for a name: its signature as written, and its doc from
|
|
/// the module's `(:doc ...)` forms, the `.bjodoc` files included, as Markdown.
|
|
module BjolangLsp.Hover
|
|
|
|
open System
|
|
open System.Text
|
|
open Bjolang
|
|
open Bjolang.Lexer
|
|
open Bjolang.Ast
|
|
open BjolangLsp.Symbols
|
|
|
|
/// A doc text's markup as Markdown. The texts use `@code{...}`, `@strong`,
|
|
/// `@em`, `@sup` and `@itemlist{@item{...} ...}`, which nest; anything else
|
|
/// after an `@` is left as it is.
|
|
let markdown (text: string) : string =
|
|
let out = StringBuilder()
|
|
|
|
// The index after the brace that closes the one at `start`.
|
|
let closing (start: int) =
|
|
let mutable depth = 0
|
|
let mutable i = start
|
|
let mutable found = -1
|
|
|
|
while found < 0 && i < text.Length do
|
|
match text[i] with
|
|
| '{' -> depth <- depth + 1
|
|
| '}' ->
|
|
depth <- depth - 1
|
|
if depth = 0 then found <- i
|
|
| _ -> ()
|
|
|
|
i <- i + 1
|
|
|
|
found
|
|
|
|
let rec go (from: int) (until: int) =
|
|
let mutable i = from
|
|
|
|
while i < until do
|
|
let c = text[i]
|
|
|
|
if c = '@' then
|
|
let mutable j = i + 1
|
|
|
|
while j < until && Char.IsLetter text[j] do
|
|
j <- j + 1
|
|
|
|
let command = text.Substring(i + 1, j - i - 1)
|
|
|
|
if j < until && text[j] = '{' && command <> "" then
|
|
let close = closing j
|
|
|
|
if close < 0 then
|
|
out.Append c |> ignore
|
|
i <- i + 1
|
|
else
|
|
let wrap (before: string) (after: string) =
|
|
out.Append before |> ignore
|
|
go (j + 1) close
|
|
out.Append after |> ignore
|
|
|
|
match command with
|
|
| "code" -> wrap "`" "`"
|
|
| "strong" -> wrap "**" "**"
|
|
| "em" -> wrap "*" "*"
|
|
| "sup" -> wrap "^" ""
|
|
| "item" -> wrap "\n- " ""
|
|
| "itemlist" -> wrap "" "\n"
|
|
| _ -> wrap ("@" + command + "{") "}"
|
|
|
|
i <- close + 1
|
|
else
|
|
out.Append c |> ignore
|
|
i <- i + 1
|
|
else
|
|
out.Append c |> ignore
|
|
i <- i + 1
|
|
|
|
go 0 text.Length
|
|
// The space between two `@item`s is left at the end of a line.
|
|
RegularExpressions.Regex.Replace(out.ToString(), " +\n", "\n")
|
|
|
|
/// The declaration of `name` among `forms`, as it reads with the name in it:
|
|
/// a `(: name Type)`, or a `(: Type)` written just above the definition, which
|
|
/// is shown as `(: name Type)`. `slice` gives a form's text.
|
|
let rec private declaration (slice: Range -> string) (name: string) (forms: SExpr list) : string option =
|
|
// The form that defines `name`: `(def name ...)`, `(defun (name ...) ...)`.
|
|
let defines (form: SExpr) =
|
|
match form with
|
|
| SList(SAtom { Token = Symbol head } :: binder :: _, _) when head.StartsWith "def" ->
|
|
match binder with
|
|
| SAtom { Token = Symbol n }
|
|
| SList(SAtom { Token = Symbol n } :: _, _)
|
|
| SList(SAtom { Token = Colon } :: SAtom { Token = Symbol n } :: _, _) -> n = name
|
|
| _ -> false
|
|
| _ -> false
|
|
|
|
// `(: Type)`, with what may follow the type: a keyword such as `#:sync`,
|
|
// or a `(where (Trait %a))`. Shown whole, with the name put in after the
|
|
// colon, so that the constraint is in the hover as it is in the source.
|
|
let nameless (form: SExpr) =
|
|
match form with
|
|
| SList(SAtom { Token = Colon } :: (SList _) :: rest, r)
|
|
| SList(SAtom { Token = Colon } :: (SAtom { Token = (Symbol _ | TypeVar _) }) :: rest, r) when
|
|
rest
|
|
|> List.forall (function
|
|
| SAtom { Token = Keyword _ } -> true
|
|
| SList(SAtom { Token = Symbol "where" } :: _, _) -> true
|
|
| _ -> false)
|
|
->
|
|
Some r
|
|
| _ -> None
|
|
|
|
let withName (r: Range) =
|
|
let written = slice r
|
|
let colon = written.IndexOf ':'
|
|
written.Substring(0, colon + 1) + " " + name + written.Substring(colon + 1)
|
|
|
|
let named =
|
|
forms
|
|
|> List.tryPick (fun form ->
|
|
match form with
|
|
| SList(SAtom { Token = Colon } :: SAtom { Token = Symbol n } :: _ :: _, r) when n = name -> Some(slice r)
|
|
| SList(SAtom { Token = Symbol("begin" | "module") } :: rest, _) -> declaration slice name rest
|
|
| _ -> None)
|
|
|
|
named
|
|
|> Option.orElse (
|
|
forms
|
|
|> List.pairwise
|
|
|> List.tryPick (fun (before, form) ->
|
|
if defines form then
|
|
nameless before |> Option.map withName
|
|
else
|
|
None)
|
|
)
|
|
|
|
/// The `(:doc ...)` entries among `forms`. One that does not read is left
|
|
/// out: the compiler reports it, and a hover has nothing to say about it.
|
|
let private entries (forms: SExpr list) : Docs.Entry list =
|
|
forms
|
|
|> List.filter Docs.isDocForm
|
|
|> List.choose (fun f ->
|
|
try
|
|
Some(Docs.parseEntry f)
|
|
with _ ->
|
|
None)
|
|
|
|
let private longest = 20
|
|
|
|
/// At most `longest` lines of `text`.
|
|
let private clip (text: string) =
|
|
let lines = text.Split('\n')
|
|
|
|
if lines.Length <= longest then
|
|
text
|
|
else
|
|
String.Join("\n", Array.truncate longest lines) + "\n…"
|
|
|
|
let private fence (code: string) = $"```lisp\n%s{code}\n```"
|
|
|
|
/// The signature: the `(: name Type)` declaration if there is one, or else
|
|
/// the type the compiler inferred, and the form's head with its parameters,
|
|
/// as written.
|
|
let private signature (found: Found) (inferred: string option) : string =
|
|
let s = found.Symbol
|
|
let m = found.Module
|
|
|
|
let declared =
|
|
declaration m.Slice s.Name m.Forms
|
|
|> Option.orElse (inferred |> Option.map (fun t -> $"(: %s{s.Name} %s{t})"))
|
|
let shape = m.Slice s.Signature
|
|
|
|
let head =
|
|
match s.Kind with
|
|
| Kind.Function when s.Detail.StartsWith "def" -> $"(%s{s.Detail} %s{shape})"
|
|
| Kind.Variable -> $"(%s{s.Detail} %s{shape})"
|
|
| _ -> shape
|
|
|
|
[ yield! Option.toList declared; yield clip head ] |> String.concat "\n" |> fence
|
|
|
|
let private docText (entry: Docs.Entry) (member': string option) : string =
|
|
let clauses = entry.Clauses |> List.map fst
|
|
|
|
let section (title: string) (items: string list) =
|
|
if items.IsEmpty then [] else [ $"**%s{title}**\n\n" + String.concat "\n" items ]
|
|
|
|
let item (name: string) (text: string) = $"- `%s{name}`: %s{markdown text}"
|
|
|
|
[ for c in clauses do
|
|
match c with
|
|
| Docs.Summary t -> yield markdown t
|
|
| _ -> ()
|
|
|
|
// The member hovered over, documented in its type's doc.
|
|
match member' with
|
|
| Some name ->
|
|
for c in clauses do
|
|
match c with
|
|
| Docs.Case(n, t)
|
|
| Docs.Field(n, t) when n = name -> yield $"`%s{name}`: %s{markdown t}"
|
|
| _ -> ()
|
|
| None -> ()
|
|
|
|
yield!
|
|
section
|
|
"Parameters"
|
|
(clauses
|
|
|> List.choose (function
|
|
| Docs.Arg(n, t) -> Some(item n t)
|
|
| Docs.Rest(n, t) -> Some(item $". %s{n}" t)
|
|
| Docs.Key(n, t) -> Some(item $"#:%s{n}" t)
|
|
| Docs.TParam(n, t) -> Some(item $"%%%s{n}" t)
|
|
| _ -> None))
|
|
|
|
if member'.IsNone then
|
|
yield!
|
|
section
|
|
"Cases"
|
|
(clauses
|
|
|> List.choose (function
|
|
| Docs.Case(n, t) -> Some(item n t)
|
|
| _ -> None))
|
|
|
|
yield!
|
|
section
|
|
"Fields"
|
|
(clauses
|
|
|> List.choose (function
|
|
| Docs.Field(n, t) -> Some(item n t)
|
|
| _ -> None))
|
|
|
|
for c in clauses do
|
|
match c with
|
|
| Docs.Returns t -> yield $"**Returns** %s{markdown t}"
|
|
| _ -> ()
|
|
|
|
yield!
|
|
section
|
|
"Raises"
|
|
(clauses
|
|
|> List.choose (function
|
|
| Docs.Raises(n, t) -> Some(item n t)
|
|
| _ -> None))
|
|
|
|
for c in clauses do
|
|
match c with
|
|
| Docs.Example t -> yield fence (t.Trim())
|
|
| _ -> ()
|
|
|
|
for c in clauses do
|
|
match c with
|
|
| Docs.Reference t -> yield markdown t
|
|
| _ -> ()
|
|
|
|
for c in clauses do
|
|
match c with
|
|
| Docs.See names -> yield "See " + (names |> List.map (fun n -> $"`%s{n}`") |> String.concat ", ") + "."
|
|
| _ -> () ]
|
|
|> String.concat "\n\n"
|
|
|
|
/// The doc of what `found` is: its own, or for a constructor or field, its
|
|
/// type's, with the member's line picked out.
|
|
let private doc (found: Found) : string option =
|
|
let all = entries found.Module.Forms
|
|
let named (n: string) = all |> List.tryFind (fun e -> e.Subject = Docs.Named n)
|
|
|
|
match named found.Symbol.Name with
|
|
| Some entry -> Some(docText entry None)
|
|
| None ->
|
|
match found.Symbol.Kind with
|
|
| Kind.EnumMember
|
|
| Kind.Field ->
|
|
named found.Symbol.Detail
|
|
|> Option.map (fun entry -> docText entry (Some found.Symbol.Name))
|
|
| _ -> None
|
|
|
|
/// The hover for a local name: its type, if the last check inferred one, its
|
|
/// binding as written, and what binds it.
|
|
let local (b: Scope.Binder) (inferred: string option) (shown: string) : string =
|
|
let what = b.Description
|
|
|
|
let sentence = string (Char.ToUpperInvariant what[0]) + what.Substring 1
|
|
let declared = inferred |> Option.map (fun t -> $"(: %s{b.Name} %s{t})") |> Option.toList
|
|
|
|
[ fence (String.concat "\n" (declared @ [ clip shown ])); $"*%s{sentence}.*" ]
|
|
|> String.concat "\n\n"
|
|
|
|
/// What a hover over a use of a generic binding adds: the type it has there.
|
|
let instantiated (name: string) (t: string) : string =
|
|
"*At this use:*\n\n" + fence $"(: %s{name} %s{t})"
|
|
|
|
/// The type of a builtin, one the compiler defines rather than a module, as a
|
|
/// declaration: `(: seq-unfold (-> ...))`. `None` for a name that is not one.
|
|
let builtinDeclaration (name: string) : string option =
|
|
Prelude.prelude.Bindings
|
|
|> Map.tryFind name
|
|
|> Option.map (fun b ->
|
|
let (TypedAST.Scheme(_, _, t)) = b.Scheme
|
|
$"(: %s{name} %s{DotNetInterop.showType t})")
|
|
|
|
/// The hover for a builtin: its type, and its doc, which the prelude's docs
|
|
/// hold although the prelude does not define it.
|
|
let builtin (name: string) : string option =
|
|
builtinDeclaration name
|
|
|> Option.map (fun declared ->
|
|
let entry =
|
|
Symbols.prelude ()
|
|
|> Option.bind (fun m -> entries m.Forms |> List.tryFind (fun e -> e.Subject = Docs.Named name))
|
|
|
|
[ yield fence declared
|
|
yield! entry |> Option.map (fun e -> docText e None) |> Option.toList
|
|
yield "*Built into the compiler.*" ]
|
|
|> String.concat "\n\n")
|
|
|
|
/// The hover for a field read, `(.x r)`: the field's type at this read, the
|
|
/// field as its type declares it, with its doc, and whose field it is.
|
|
/// `declared` is the record's or struct's definition, if it is found.
|
|
let field (name: string) (t: string) (record: string) (declared: Found option) : string =
|
|
let declaredField =
|
|
declared
|
|
|> Option.bind (fun d ->
|
|
d.Symbol.Children
|
|
|> List.tryFind (fun c -> c.Name = name)
|
|
|> Option.map (fun c -> { d with Symbol = c }))
|
|
|
|
let file =
|
|
declared
|
|
|> Option.map (fun d -> $", in %s{IO.Path.GetFileName d.Symbol.NameRange.File}")
|
|
|> Option.defaultValue ""
|
|
|
|
[ yield fence $"(: .%s{name} %s{t})"
|
|
match declaredField with
|
|
| Some f ->
|
|
yield fence (clip (f.Module.Slice f.Symbol.Signature))
|
|
yield! Option.toList (doc f)
|
|
| None -> ()
|
|
yield $"*A field of `%s{record}`%s{file}.*" ]
|
|
|> String.concat "\n\n"
|
|
|
|
/// The hover for `found`: its signature, where it is, and its doc.
|
|
let text (found: Found) (inferred: string option) : string =
|
|
let where =
|
|
let file = IO.Path.GetFileName found.Symbol.NameRange.File
|
|
|
|
match found.Symbol.Kind with
|
|
| Kind.EnumMember -> $"A case of `%s{found.Symbol.Detail}`, in %s{file}."
|
|
| Kind.Field -> $"A field of `%s{found.Symbol.Detail}`, in %s{file}."
|
|
| Kind.Method -> $"A method of the trait `%s{found.Symbol.Detail}`, in %s{file}."
|
|
| _ -> $"In %s{file}."
|
|
|
|
[ yield signature found inferred
|
|
yield! Option.toList (doc found)
|
|
yield $"*%s{where}*" ]
|
|
|> String.concat "\n\n"
|