Diagnostics at the compiler's ranges, and a link to one in another file

- A diagnostic is marked from where the compiler's range of it starts to
  where it ends (Diagnostics.rangeOf, in the compiler). A quoted list, an
  array literal and an interpolated string were marked as one character.
  A location with no range behind it is read from the message, as before.
- A diagnostic in an included or an imported file has its place there as
  related information.
- The README and the smoke test name bin/Release/net11.0, where the build
  is since the compiler's target changed.
- smoke: the prelude's definitions are in prelude/core.bjo.

Needs the compiler at its commit "Diagnostics.rangeOf".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-25 07:30:13 +01:00
parent ceb6f1b93a
commit d421ec9363
4 changed files with 175 additions and 32 deletions

View file

@ -16,7 +16,12 @@ type Found =
EndLine: int
EndColumn: int
IsError: bool
Message: string }
Message: string
/// For a diagnostic in another file, such as an imported module: where
/// it is there. The diagnostic itself is then at the top of the file
/// that was checked, and the client gets this as related information,
/// which an editor shows as a place to go to.
Elsewhere: Lexer.Range option }
/// `at file.bjo:12:3`, as `Lexer.formatPos` and `Lexer.formatAt` write a
/// location into a message, the column counted from 1, or `at file.bjo:12`.
@ -125,6 +130,11 @@ let private formAt (text: string) (line: int) (column: int) =
/// Where a diagnostic is, if it is in `file`. Anywhere else, such as in an
/// imported module, it is `None`, and the caller puts it at the top of `file`
/// with the location left in the message.
///
/// The compiler's range of the diagnostic is used where it has one
/// (`Diagnostics.rangeOf`): it has the end, which the location in the message
/// does not. A location with no range behind it, such as the tokenizer's, is
/// read from the message, and the form that starts there is marked.
let private placeIn (text: string option) (file: string) (d: Diagnostics.Diagnostic) =
let markFrom (line: int) (column: int) =
text |> Option.bind (fun t -> formAt t line column)
@ -137,7 +147,7 @@ let private placeIn (text: string option) (file: string) (d: Diagnostics.Diagnos
else
other = Path.GetFileName file
match d.Where with
match Diagnostics.rangeOf d with
| Some r when sameFile r.File ->
// The lexer's lines are 1-based and its columns 0-based, as LSP's are.
let line = max 0 (r.Start.Line - 1)
@ -187,7 +197,8 @@ let private found (text: string option) (file: string) (d: Diagnostics.Diagnosti
EndLine = p.EndLine
EndColumn = p.EndColumn
IsError = isError
Message = withoutLocations d.Message }
Message = withoutLocations d.Message
Elsewhere = None }
| None ->
let where =
match d.Where with
@ -196,12 +207,18 @@ let private found (text: string option) (file: string) (d: Diagnostics.Diagnosti
let p = wholeLine 0
// A range of another file, with a path that a client can open.
let elsewhere =
Diagnostics.rangeOf d
|> Option.filter (fun r -> Path.IsPathRooted r.File && Path.GetFullPath r.File <> Path.GetFullPath file)
{ Line = p.Line
Column = p.Column
EndLine = p.EndLine
EndColumn = p.EndColumn
IsError = isError
Message = d.Message + where }
Message = d.Message + where
Elsewhere = elsewhere }
let private options: Build.Options = { Build.dependencyOptions with IsLibrary = false; Check = true }
@ -260,7 +277,8 @@ let file (buffers: Map<string, string>) (path: string) : Found list =
EndLine = 1
EndColumn = 0
IsError = true
Message = message } ]
Message = message
Elsewhere = None } ]
else
collected
@ -269,17 +287,43 @@ let publishParams (uri: string) (found: Found list) : JsonNode =
let position (line: int) (character: int) =
JsonObject(dict [ "line", JsonValue.Create line :> JsonNode; "character", JsonValue.Create character ])
let range (startLine: int) (startColumn: int) (endLine: int) (endColumn: int) =
JsonObject(dict [ "start", position startLine startColumn :> JsonNode; "end", position endLine endColumn ])
let diagnostic (f: Found) =
JsonObject(
dict
[ "range",
JsonObject(dict [ "start", position f.Line f.Column :> JsonNode; "end", position f.EndLine f.EndColumn ])
:> JsonNode
"severity", JsonValue.Create(if f.IsError then 1 else 2)
"source", JsonValue.Create "bjolang"
"message", JsonValue.Create f.Message ]
)
:> JsonNode
let node =
JsonObject(
dict
[ "range", range f.Line f.Column f.EndLine f.EndColumn :> JsonNode
"severity", JsonValue.Create(if f.IsError then 1 else 2)
"source", JsonValue.Create "bjolang"
"message", JsonValue.Create f.Message ]
)
match f.Elsewhere with
| Some r ->
// The lexer's lines are 1-based and its columns 0-based.
let line = max 0 (r.Start.Line - 1)
let endLine = max line (r.End.Line - 1)
let endColumn = if endLine = line then max (r.Start.Column + 1) r.End.Column else r.End.Column
node["relatedInformation"] <-
JsonArray(
JsonObject(
dict
[ "location",
JsonObject(
dict
[ "uri", JsonValue.Create(Uri(r.File).AbsoluteUri) :> JsonNode
"range", range line r.Start.Column endLine endColumn ]
)
:> JsonNode
"message", JsonValue.Create "The compiler found it here." ]
)
)
| None -> ()
node :> JsonNode
JsonObject(
dict