lsp-bjolang/Fixes.fs

44 lines
1.5 KiB
Forth
Raw Normal View History

/// What the compiler offers to write, from a file's last check: the clauses a
/// match lacks, and what could fill a hole. The compiler hands them to
/// `Exhaustiveness.observe` and `Holes.observe` as it refuses the code, so
/// they cost nothing beyond the check.
module BjolangLsp.Fixes
open System.IO
open Bjolang
open Bjolang.Lexer
type Facts =
{ Missing: Exhaustiveness.MissingClauses list
Holes: Holes.Fillers list }
let private none = { Missing = []; Holes = [] }
let private byFile = System.Collections.Generic.Dictionary<string, Facts>()
/// Runs `check` of `path` with the compiler handing over what it refused, and
/// keeps what is in `path` as the file's.
let recording (path: string) (check: unit -> 'a) : 'a =
let full = Path.GetFullPath path
let inFile (r: Range) = r.File <> "" && Path.GetFullPath r.File = full
let missing = ResizeArray<Exhaustiveness.MissingClauses>()
let holes = ResizeArray<Holes.Fillers>()
Exhaustiveness.observe <- Some(fun m -> if inFile m.Range then missing.Add m)
Holes.observe <- Some(fun h -> if inFile h.Range then holes.Add h)
try
check ()
finally
Exhaustiveness.observe <- None
Holes.observe <- None
byFile[full] <-
{ Missing = missing |> Seq.distinctBy (fun m -> m.Range) |> List.ofSeq
Holes = holes |> Seq.distinctBy (fun h -> h.Range) |> List.ofSeq }
/// The facts of `path`'s last check.
let ofFile (path: string) : Facts =
match byFile.TryGetValue(Path.GetFullPath path) with
| true, facts -> facts
| _ -> none