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

@ -167,7 +167,11 @@ let private markdownContent (text: string) : JsonNode =
/// is.
let private describe (uri: string) (name: string) : string option =
match Symbols.definition (uriToPath uri) (textOf uri) name with
| Some found -> Some(Hover.text found)
| Some found ->
// What the compiler inferred, for a definition the last check of a
// file saw, which a hover shows where no signature is written.
let inferred = Types.boundAt found.Symbol.NameRange.File name found.Symbol.NameRange
Some(Hover.text found inferred)
| None -> Hover.builtin name
/// The type the last check inferred for the local `b`: at the use written at
@ -309,6 +313,110 @@ let private resolve (item: JsonNode) : JsonNode =
item.DeepClone()
/// A file's text: the editor's, where it has the file open, else what is on
/// disk.
let private textOfPath (path: string) : string =
let full = IO.Path.GetFullPath path
match texts |> Seq.tryFind (fun kv -> IO.Path.GetFullPath(uriToPath kv.Key) = full) with
| Some kv -> kv.Value
| None -> if IO.File.Exists full then IO.File.ReadAllText full else ""
/// `text` with each of `edits` replaced by `newText`. Lines are 1-based and
/// columns 0-based, as a compiler range has them.
let private applyEdits (text: string) (edits: Rename.Edit list) (newText: string) : string =
let lineStarts =
[| yield 0
for i in 0 .. text.Length - 1 do
if text[i] = '\n' then yield i + 1 |]
let offset (p: Bjolang.Lexer.Position) = lineStarts[p.Line - 1] + p.Column
edits
|> List.sortByDescending (fun e -> offset e.Range.Start)
|> List.fold
(fun (t: string) e ->
let start = offset e.Range.Start
t.Substring(0, start) + newText + t.Substring(offset e.Range.End))
text
/// The range of the name `prepareRename` asks about, and the name, or why it
/// cannot be renamed.
let private prepareRenameAt (uri: string) (line: int) (character: int) : Result<JsonNode, string> =
Rename.plan textOfPath (uriToPath uri) (line + 1) character
|> Result.map (fun p ->
match Symbols.nameAt (uriToPath uri) (textOf uri) (line + 1) character with
| Some(_, at) ->
JsonObject(dict [ "range", range at; "placeholder", JsonValue.Create p.Old :> JsonNode ]) :> JsonNode
| None -> null)
/// The edit that renames the name at a position to `newName`, or why it is
/// refused.
///
/// The renamed text is checked before it is offered, against a check of the
/// text as it is: an error the renamed program has and this one does not is
/// a rename that changed what something means, and is refused with it. The
/// check after it leaves the file's types those of the renamed text, so the
/// file is checked again as it stands.
let private renameAt (uri: string) (line: int) (character: int) (newName: string) : Result<JsonNode, string> =
let path = IO.Path.GetFullPath(uriToPath uri)
match Rename.plan textOfPath path (line + 1) character with
| Error why -> Error why
| Ok p ->
match Rename.capture textOfPath path p newName with
| Some why -> Error why
| None ->
let byFile = p.Edits |> List.groupBy (fun e -> e.File)
let buffers =
texts |> Seq.map (fun kv -> IO.Path.GetFullPath(uriToPath kv.Key), kv.Value) |> Map.ofSeq
let renamed =
byFile
|> List.fold
(fun (acc: Map<string, string>) (file, edits) ->
Map.add file (applyEdits (textOfPath file) edits newName) acc)
buffers
let errors (found: Check.Found list) =
found |> List.filter (fun f -> f.IsError) |> List.map (fun f -> f.Message)
// A message about the old name, as it reads about the new one. Only
// the whole name is replaced: `n` is in "not" too.
let renamedIn (message: string) =
Text.RegularExpressions.Regex.Replace(
message,
@"(?<![A-Za-z0-9_?!*/<>=+&.-])" + Text.RegularExpressions.Regex.Escape p.Old + @"(?![A-Za-z0-9_?!*/<>=+&.-])",
newName
)
let before = Check.file buffers path |> errors |> List.map renamedIn |> Set.ofList
let after = Check.file renamed path |> errors
dueChecks[uri] <- DateTime.UtcNow
match after |> List.tryFind (fun m -> not (before.Contains m)) with
| Some m ->
Error $"Renaming '%s{p.Old}' to '%s{newName}' would give the program an error it does not have now: %s{m}"
| None ->
let uriOf (file: string) =
if file = path then uri else Uri(file).AbsoluteUri
let changes =
byFile
|> List.map (fun (file, edits) ->
uriOf file,
JsonArray(
edits
|> List.map (fun e ->
JsonObject(dict [ "range", range e.Range; "newText", JsonValue.Create newName :> JsonNode ])
:> JsonNode)
|> Array.ofList
)
:> JsonNode)
Ok(JsonObject(dict [ "changes", JsonObject(dict changes) :> JsonNode ]) :> JsonNode)
let private capabilities () : JsonNode =
JsonNode.Parse
"""
@ -318,8 +426,9 @@ let private capabilities () : JsonNode =
"definitionProvider": true,
"hoverProvider": true,
"inlayHintProvider": true,
"renameProvider": { "prepareProvider": true },
"completionProvider": { "resolveProvider": true, "triggerCharacters": [ "(" ] } },
"serverInfo": { "name": "bjolang-lsp", "version": "0.5.0" } }
"serverInfo": { "name": "bjolang-lsp", "version": "0.6.0" } }
"""
[<EntryPoint>]
@ -452,6 +561,24 @@ let main _ =
Rpc.respond id (uri () |> Option.map (fun u -> completionAt u line character) |> Option.defaultValue null)
| "completionItem/resolve" -> Rpc.respond id (resolve message["params"])
// A refusal is an error answer: the client shows its
// message, which says why.
| "textDocument/prepareRename"
| "textDocument/rename" ->
let p = message["params"]
let position = p["position"]
let line = position["line"].GetValue<int>()
let character = position["character"].GetValue<int>()
let answer =
match uri () with
| None -> Error "No document."
| Some u when methodName = "textDocument/prepareRename" -> prepareRenameAt u line character
| Some u -> renameAt u line character (p["newName"].GetValue<string>())
match answer with
| Ok result -> Rpc.respond id result
| Error why -> Rpc.respondError id -32803 why
// Not run first when a check is due, as a hover is: a
// client asks for hints on every change. The check sends
// a refresh when it has run.