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

@ -1,12 +1,8 @@
/// Renaming a name: a local binding, or a top-level `def` or `defun` the
/// module does not export.
///
/// The places to rename are found as go to definition finds a name's binding
/// (`Scope`), on the forms as the reader gives them, in the module's own file
/// and the files it includes. What is not a reference is left: quoted data,
/// the part of a `#'` template that is not unquoted, a type's declaration, and
/// what the reader wrote itself, such as the `fun` of `#(...)`, which has no
/// text of its own.
/// The places to rename are the name's uses in the module, as `Uses` finds
/// them.
///
/// A rename is refused rather than guessed at where it could change what the
/// program means: where the new name would capture a use of another binding,
@ -28,49 +24,9 @@ type Plan =
{ Old: string
Edits: Edit list }
/// A module's files: its own, and what it includes, each with its forms.
let private moduleFiles (textOf: string -> string) (path: string) : (string * string * SExpr list) list =
let rec go (seen: Set<string>) (file: string) =
let text = textOf file
let fs = Symbols.forms file text |> Option.defaultValue []
let included =
fs
|> List.choose (fun form ->
match form with
| SList([ SAtom { Token = Symbol "include" }; SAtom { Token = StringLit rel } ], _) -> Some rel
| _ -> Docs.fileForm form |> Option.map fst)
|> List.map (fun rel -> Path.GetFullPath(Path.Combine(Path.GetDirectoryName file, rel)))
|> List.filter (fun f -> File.Exists f && not (seen.Contains f))
(file, text, fs)
:: (included |> List.collect (fun f -> go (Set.add f seen) f))
let full = Path.GetFullPath path
go (Set.singleton full) full
/// The symbols named `name` in `form` that can be references: not quoted data,
/// not a `#'` template's own names (an unquoted part of either is code), and
/// nothing in a type's declaration.
let rec private symbolsNamed (name: string) (form: SExpr) : Range list =
let rec quoted (items: SExpr list) =
match items with
| SAtom { Token = (Comma | CommaAt) } :: unquoted :: rest -> symbolsNamed name unquoted @ quoted rest
| SList(inner, _) :: rest -> quoted inner @ quoted rest
| _ :: rest -> quoted rest
| [] -> []
match form with
| SAtom { Token = Symbol n; Range = r } when n = name -> [ r ]
| SList(SAtom { Token = Symbol("quoted-list" | "syntax-quote") } :: items, _) -> quoted items
| SList(SAtom { Token = Symbol("type" | "type-rec") } :: _, _) -> []
| SList(items, _) -> items |> List.collect (symbolsNamed name)
| _ -> []
/// Whether the source at `r` is `name`: the reader writes some symbols
/// itself, at the range of the form that made them.
let private written (text: string) (name: string) (r: Range) =
r.Start.Line = r.End.Line && Symbols.sliceOf text r = name
let private moduleFiles = Uses.moduleFiles
let private symbolsNamed = Uses.symbolsNamed
let private written = Uses.written
/// Why `name` cannot be what something is renamed to, if it cannot.
let private invalidName (path: string) (name: string) : string option =
@ -122,14 +78,9 @@ let plan (textOf: string -> string) (path: string) (line: int) (column: int) : R
| Some binder ->
// Every symbol of that name that this binding is what it means.
let edits =
forms
|> List.collect (symbolsNamed name)
|> List.filter (fun r ->
written text name r
&& (match Scope.find forms r.Start.Line r.Start.Column name with
| Some b -> b.Range = binder.Range
| None -> false))
|> List.map (fun r -> { File = path; Range = r })
Uses.ofLocal path text forms binder
|> List.filter (fun u -> written text name u.Range)
|> List.map (fun u -> { File = u.File; Range = u.Range })
// A parameter of a top-level function is also named by the
// function's doc: `(arg x "...")` and `(rest xs "...")`.