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:
parent
853b7c3163
commit
23b6a0e55c
9 changed files with 634 additions and 60 deletions
98
Actions.fs
Normal file
98
Actions.fs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
/// Code actions: edits the compiler has worked out, offered where the code
|
||||
/// they are for is.
|
||||
///
|
||||
/// - Add the clauses a match lacks, each with a hole for its body, after its
|
||||
/// last clause and lined up with it. The next check then says what each
|
||||
/// body has to be.
|
||||
/// - Fill a hole with a binding in scope that fits it, a constructor of its
|
||||
/// type with holes for what it takes, or, for a function type, a `fun`
|
||||
/// with a hole for its body.
|
||||
///
|
||||
/// What is offered is from the file's last check (`Fixes`), and only for text
|
||||
/// that is still what was checked.
|
||||
module BjolangLsp.Actions
|
||||
|
||||
open Bjolang
|
||||
open Bjolang.Lexer
|
||||
open Bjolang.Ast
|
||||
|
||||
type Action =
|
||||
{ Title: string
|
||||
/// The text to replace, an empty range to insert at.
|
||||
Range: Range
|
||||
NewText: string }
|
||||
|
||||
let private before (a: Position) (b: Position) =
|
||||
a.Line < b.Line || (a.Line = b.Line && a.Column < b.Column)
|
||||
|
||||
/// Whether `r` and the span from `first` to `last` share a position, ends
|
||||
/// included, so that a cursor right after a hole is at it.
|
||||
let private touches (r: Range) (first: Position) (last: Position) =
|
||||
not (before r.End first || before last r.Start)
|
||||
|
||||
let private rangeOf = Ast.getRange
|
||||
|
||||
/// The `(match ...)` form that starts where `at` does.
|
||||
let rec private matchAt (at: Position) (forms: SExpr list) : (SExpr * SExpr list * Range) option =
|
||||
forms
|
||||
|> List.tryPick (fun form ->
|
||||
match form with
|
||||
| SList(SAtom { Token = Symbol "match" } :: scrutinee :: clauses, r) when r.Start = at -> Some(scrutinee, clauses, r)
|
||||
| SList(items, _) -> matchAt at items
|
||||
| _ -> None)
|
||||
|
||||
/// The parameters a `fun` of `n` of them is written with.
|
||||
let private parameterNames (n: int) =
|
||||
match n with
|
||||
| n when n <= 3 -> [ "x"; "y"; "z" ] |> List.truncate n
|
||||
| n -> List.init n (fun i -> $"x%d{i + 1}")
|
||||
|
||||
/// How many bindings that fit a hole are offered; the hole's message names
|
||||
/// the rest of them.
|
||||
let private fitsOffered = 12
|
||||
|
||||
/// The actions for the code from `first` to `last` of `path`, whose text is
|
||||
/// `text`, as the last check left it.
|
||||
let at (path: string) (text: string) (first: Position) (last: Position) : Action list =
|
||||
let facts = Fixes.ofFile path
|
||||
let forms = Symbols.forms path text |> Option.defaultValue []
|
||||
|
||||
let clauses =
|
||||
facts.Missing
|
||||
|> List.filter (fun m -> touches m.Range first last)
|
||||
|> List.choose (fun m ->
|
||||
matchAt m.Range.Start forms
|
||||
|> Option.map (fun (scrutinee, existing, r) ->
|
||||
let after, column =
|
||||
match List.tryLast existing with
|
||||
| Some clause -> (rangeOf clause).End, (rangeOf clause).Start.Column
|
||||
| None -> (rangeOf scrutinee).End, r.Start.Column + 2
|
||||
|
||||
let newText =
|
||||
m.Patterns
|
||||
|> List.map (fun p -> "\n" + String.replicate column " " + "(" + p + " _)")
|
||||
|> String.concat ""
|
||||
|
||||
let title =
|
||||
match m.Patterns with
|
||||
| [ one ] -> $"Add the missing clause %s{one}"
|
||||
| several -> $"Add the %d{several.Length} missing clauses"
|
||||
|
||||
{ Title = title
|
||||
Range = { r with Start = after; End = after }
|
||||
NewText = newText }))
|
||||
|
||||
let fills =
|
||||
facts.Holes
|
||||
|> List.filter (fun h -> touches h.Range first last && (Symbols.sliceOf text h.Range).StartsWith "_")
|
||||
|> List.collect (fun h ->
|
||||
let functions =
|
||||
match h.Parameters with
|
||||
| Some n -> [ "(fun (" + String.concat " " (parameterNames n) + ") _)" ]
|
||||
| None -> []
|
||||
|
||||
(List.truncate fitsOffered h.Fits @ h.Constructors @ functions)
|
||||
|> List.distinct
|
||||
|> List.map (fun filler -> { Title = $"Fill with %s{filler}"; Range = h.Range; NewText = filler }))
|
||||
|
||||
clauses @ fills
|
||||
Loading…
Add table
Add a link
Reference in a new issue