Rename, and a hover that leads with the type

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>
This commit is contained in:
Linus Björnstam 2026-10-22 16:36:22 +02:00
parent 6a92386761
commit 853b7c3163
6 changed files with 632 additions and 17 deletions

View file

@ -80,14 +80,52 @@ let markdown (text: string) : string =
// The space between two `@item`s is left at the end of a line.
RegularExpressions.Regex.Replace(out.ToString(), " +\n", "\n")
/// The `(: name Type)` declaration of `name` among `forms`, if it has one.
let rec private declaration (name: string) (forms: SExpr list) : SExpr option =
forms
|> List.tryPick (fun form ->
/// 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 = Colon } :: SAtom { Token = Symbol n } :: _, _) when n = name -> Some form
| SList(SAtom { Token = Symbol("begin" | "module") } :: rest, _) -> declaration name rest
| _ -> None)
| 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.
@ -113,12 +151,16 @@ let private clip (text: string) =
let private fence (code: string) = $"```lisp\n%s{code}\n```"
/// The signature: the `(: name Type)` declaration if there is one, and the
/// form's head with its parameters, as written.
let private signature (found: Found) : string =
/// 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 s.Name m.Forms |> Option.map (fun d -> m.Slice(getRange d))
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 =
@ -264,7 +306,7 @@ let builtin (name: string) : string option =
|> String.concat "\n\n")
/// The hover for `found`: its signature, where it is, and its doc.
let text (found: Found) : string =
let text (found: Found) (inferred: string option) : string =
let where =
let file = IO.Path.GetFileName found.Symbol.NameRange.File
@ -274,7 +316,7 @@ let text (found: Found) : string =
| Kind.Method -> $"A method of the trait `%s{found.Symbol.Detail}`, in %s{file}."
| _ -> $"In %s{file}."
[ yield signature found
[ yield signature found inferred
yield! Option.toList (doc found)
yield $"*%s{where}*" ]
|> String.concat "\n\n"