textDocument/rename and prepareRename rename a local binding, or a top-level def or defun the module does not export, with its signature, doc and the files the module includes; a parameter's (arg ...) in its function's doc goes with it. A rename that could change what the program means is refused with the reason: a capture, a keyword parameter, an exported or foreign name, a name that is not one. The renamed text is checked first, and one with an error the text before did not have is refused with it. A hover's first line, which is what the status bar shows, is the type: a nameless (: Type) above a definition is found and shown with the name, and a definition with no signature in the file being edited shows the type the compiler inferred. 🤖 Generated with [ECA](https://eca.dev) Co-Authored-By: eca-agent <git@eca.dev>
322 lines
11 KiB
FSharp
322 lines
11 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, such as `#:sync`.
|
|
let nameless (form: SExpr) =
|
|
match form with
|
|
| SList(SAtom { Token = Colon } :: (SList _ as t) :: rest, _)
|
|
| SList(SAtom { Token = Colon } :: (SAtom { Token = (Symbol _ | TypeVar _) } as t) :: rest, _) when
|
|
rest
|
|
|> List.forall (function
|
|
| SAtom { Token = Keyword _ } -> true
|
|
| _ -> false)
|
|
->
|
|
Some t
|
|
| _ -> None
|
|
|
|
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 (fun t -> $"(: %s{name} %s{slice (getRange t)})")
|
|
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 `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"
|