References, and code actions that add a match's clauses and fill holes

textDocument/references and documentHighlight give the uses of the name
at point in its module, found as go to definition finds a binding: a
local, a top-level definition, a type or a constructor, or an imported
name where the module uses it. Uses.fs holds the search, which Rename
now shares.

textDocument/codeAction offers what the compiler worked out while it
checked (Exhaustiveness.observe, Holes.observe, kept by Fixes.fs): at an
incomplete match, the clauses it lacks, lined up after its last clause
with holes as bodies; at a typed hole, a binding in scope that fits, a
constructor of its type with holes for what it takes, or a fun.

Version 0.7.0.

🤖 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 20:34:41 +02:00
parent 853b7c3163
commit 23b6a0e55c
9 changed files with 634 additions and 60 deletions

View file

@ -417,6 +417,70 @@ let private renameAt (uri: string) (line: int) (character: int) (newName: string
Ok(JsonObject(dict [ "changes", JsonObject(dict changes) :> JsonNode ]) :> JsonNode)
/// The uses of the name at a position, in its module, as LSP locations; the
/// definition among them only if `withDefinition`.
let private referencesAt (uri: string) (line: int) (character: int) (withDefinition: bool) : JsonNode =
let path = IO.Path.GetFullPath(uriToPath uri)
let uriOf (file: string) = if file = path then uri else Uri(file).AbsoluteUri
match Uses.at textOfPath path (line + 1) character with
| None -> null
| Some(_, uses) ->
JsonArray(
uses
|> List.filter (fun u -> withDefinition || not u.IsDefinition)
|> List.map (fun u ->
JsonObject(dict [ "uri", JsonValue.Create(uriOf u.File) :> JsonNode; "range", range u.Range ]) :> JsonNode)
|> Array.ofList
)
:> JsonNode
/// The uses of the name at a position in this file, for the editor to mark:
/// the definition as a write, the rest as reads.
let private highlightsAt (uri: string) (line: int) (character: int) : JsonNode =
let path = IO.Path.GetFullPath(uriToPath uri)
match Uses.at textOfPath path (line + 1) character with
| None -> null
| Some(_, uses) ->
JsonArray(
uses
|> List.filter (fun u -> u.File = path)
|> List.map (fun u ->
JsonObject(dict [ "range", range u.Range; "kind", JsonValue.Create(if u.IsDefinition then 3 else 2) :> JsonNode ])
:> JsonNode)
|> Array.ofList
)
:> JsonNode
/// The code actions for the span from `first` to `last` of a file: what its
/// last check worked out there, if the text is still what it checked.
let private actionsAt (uri: string) (first: Bjolang.Lexer.Position) (last: Bjolang.Lexer.Position) : JsonNode =
let path = IO.Path.GetFullPath(uriToPath uri)
let text = textOf uri
let actions =
match Types.checkedText path with
| Some checkedText when checkedText = text -> Actions.at path text first last
| _ -> []
JsonArray(
actions
|> List.map (fun a ->
let edit =
JsonObject(dict [ "range", range a.Range; "newText", JsonValue.Create a.NewText :> JsonNode ])
JsonObject(
dict
[ "title", JsonValue.Create a.Title :> JsonNode
"kind", JsonValue.Create "quickfix"
"edit", JsonObject(dict [ "changes", JsonObject(dict [ uri, JsonArray(edit) :> JsonNode ]) :> JsonNode ]) ]
)
:> JsonNode)
|> Array.ofList
)
:> JsonNode
let private capabilities () : JsonNode =
JsonNode.Parse
"""
@ -427,8 +491,11 @@ let private capabilities () : JsonNode =
"hoverProvider": true,
"inlayHintProvider": true,
"renameProvider": { "prepareProvider": true },
"referencesProvider": true,
"documentHighlightProvider": true,
"codeActionProvider": { "codeActionKinds": [ "quickfix" ] },
"completionProvider": { "resolveProvider": true, "triggerCharacters": [ "(" ] } },
"serverInfo": { "name": "bjolang-lsp", "version": "0.6.0" } }
"serverInfo": { "name": "bjolang-lsp", "version": "0.7.0" } }
"""
[<EntryPoint>]
@ -561,6 +628,46 @@ 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"])
| "textDocument/references"
| "textDocument/documentHighlight" ->
let p = message["params"]
let position = p["position"]
let line = position["line"].GetValue<int>()
let character = position["character"].GetValue<int>()
let withDefinition =
match p["context"] with
| null -> true
| c ->
match c["includeDeclaration"] with
| null -> true
| v -> v.GetValue<bool>()
let answer =
uri ()
|> Option.map (fun u ->
if methodName = "textDocument/references" then referencesAt u line character withDefinition
else highlightsAt u line character)
|> Option.defaultValue null
Rpc.respond id answer
// What is offered is from the last check, which a check
// that is due replaces first, as for a hover.
| "textDocument/codeAction" ->
let r = message["params"]["range"]
let position (n: JsonNode) : Bjolang.Lexer.Position =
{ Line = n["line"].GetValue<int>() + 1
Column = n["character"].GetValue<int>() }
let answer =
uri ()
|> Option.map (fun u ->
if dueChecks.Remove u then publish u
actionsAt u (position r["start"]) (position r["end"]))
|> Option.defaultValue (JsonArray())
Rpc.respond id answer
// A refusal is an error answer: the client shows its
// message, which says why.
| "textDocument/prepareRename"