lsp-bjolang/Fixes.fs
Linus Björnstam 5e4ac7d9aa Checks run on a thread of their own
A check held up every request, because the compiler could run one
compilation at a time. It can now run each in a context of its own.

- A check runs on a checking thread, in Compilation.inNew. The thread
  reports a check to the thread that serves requests, which writes to the
  client.
- The outline, definitions, references, completion, signature help and
  formatting are answered while a check runs.
- A hover, a code action and bjolang/expandMacro are answered when the
  check of their file ends, with the results of the text as it stands.
  Other requests are served meanwhile.
- A file that changes again before its check starts is checked once.
- The tables that a check writes are concurrent dictionaries.
- Standard output is the protocol's; the console's output goes to standard
  error.
- smoke: with a check of 1.6 s running, an outline is answered in 0.1 s.

Needs the compiler at its commit "Diagnostics.captured collects the output
of its own context".

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

51 lines
2.2 KiB
FSharp

/// What the compiler offers to write, from a file's last check: the clauses a
/// match lacks, what could fill a hole, and the signature of a function that
/// has none. The compiler hands them to `Exhaustiveness.observe`,
/// `Holes.observe` and `CheckDecl.observeMissingSignature` as it refuses the
/// code. The first two cost nothing beyond the check. For the third, the
/// compiler checks the body of each function that has no signature, which it
/// does not do with no observer.
module BjolangLsp.Fixes
open System.IO
open Bjolang
open Bjolang.Lexer
type Facts =
{ Missing: Exhaustiveness.MissingClauses list
Holes: Holes.Fillers list
Signatures: CheckDecl.MissingSignature list }
let private none = { Missing = []; Holes = []; Signatures = [] }
let private byFile = System.Collections.Concurrent.ConcurrentDictionary<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>()
let signatures = ResizeArray<CheckDecl.MissingSignature>()
Exhaustiveness.observe.Value <- Some(fun m -> if inFile m.Range then missing.Add m)
Holes.observe.Value <- Some(fun h -> if inFile h.Range then holes.Add h)
CheckDecl.observeMissingSignature.Value <- Some(fun s -> if inFile s.Range then signatures.Add s)
try
check ()
finally
Exhaustiveness.observe.Value <- None
Holes.observe.Value <- None
CheckDecl.observeMissingSignature.Value <- None
byFile[full] <-
{ Missing = missing |> Seq.distinctBy (fun m -> m.Range) |> List.ofSeq
Holes = holes |> Seq.distinctBy (fun h -> h.Range) |> List.ofSeq
Signatures = signatures |> Seq.distinctBy (fun s -> s.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