lsp-bjolang/Actions.fs
Linus Björnstam ce426b94df Signature help, formatting, and a fix that writes a missing signature
- textDocument/signatureHelp: the function whose call the cursor is in,
  with the parameter that is being written marked. A definition shows its
  head as written; an import, a trait's method and a builtin show their
  type. It reads tokens as far as the cursor, so a call that is not closed
  has help.
- textDocument/formatting: the whole text as bjo fmt lays it out. The
  server runs bjo -q fmt - on the buffer's text.
- A quick fix at a function with no signature writes the one its body
  gives it, on a line above the definition
  (CheckDecl.observeMissingSignature, in the compiler).
- The server says version 0.8.0.

Needs the compiler at its commit "CheckDecl.observeMissingSignature".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 08:01:34 +01:00

113 lines
4.5 KiB
FSharp

/// 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.
/// - Write the signature of a function that has none, on a line of its own
/// above the definition: the type that its body gives it.
///
/// 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 }))
// Offered on the definition's first line, where the diagnostic is, and
// not in its body.
let signatures =
facts.Signatures
|> List.filter (fun s ->
touches s.Range first last
&& first.Line = s.Range.Start.Line
&& (Symbols.sliceOf text s.Range).StartsWith "(")
|> List.map (fun s ->
{ Title = $"Write the signature %s{s.Signature}"
Range = { s.Range with End = s.Range.Start }
NewText = s.Signature + "\n" + String.replicate s.Range.Start.Column " " })
clauses @ fills @ signatures