command/format: Fix empty overlap diagnostics
Diagnostics where the highlight range has an empty overlap with a line would skip lines of the output. This is because if two ranges abut each other, they can be considered to overlap, but that overlap is empty. This results in an edge case in the diagnostic printer which causes the line not to be printed.
This commit is contained in:
parent
2dd64a7816
commit
c87c0a9d2a
|
@ -100,7 +100,7 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color
|
||||||
if !lineRange.Overlaps(snippetRange) {
|
if !lineRange.Overlaps(snippetRange) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if lineRange.Overlaps(highlightRange) {
|
if !lineRange.Overlap(highlightRange).Empty() {
|
||||||
beforeRange, highlightedRange, afterRange := lineRange.PartitionAround(highlightRange)
|
beforeRange, highlightedRange, afterRange := lineRange.PartitionAround(highlightRange)
|
||||||
before := beforeRange.SliceBytes(src)
|
before := beforeRange.SliceBytes(src)
|
||||||
highlighted := highlightedRange.SliceBytes(src)
|
highlighted := highlightedRange.SliceBytes(src)
|
||||||
|
|
|
@ -120,3 +120,50 @@ Error: Some error
|
||||||
t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected)
|
t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDiagnostic_emptyOverlapHighlightContext(t *testing.T) {
|
||||||
|
var diags tfdiags.Diagnostics
|
||||||
|
|
||||||
|
diags = diags.Append(&hcl.Diagnostic{
|
||||||
|
Severity: hcl.DiagError,
|
||||||
|
Summary: "Some error",
|
||||||
|
Detail: "...",
|
||||||
|
Subject: &hcl.Range{
|
||||||
|
Filename: "source.tf",
|
||||||
|
Start: hcl.Pos{Line: 3, Column: 10, Byte: 38},
|
||||||
|
End: hcl.Pos{Line: 4, Column: 1, Byte: 39},
|
||||||
|
},
|
||||||
|
Context: &hcl.Range{
|
||||||
|
Filename: "source.tf",
|
||||||
|
Start: hcl.Pos{Line: 2, Column: 13, Byte: 27},
|
||||||
|
End: hcl.Pos{Line: 4, Column: 1, Byte: 39},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
sources := map[string][]byte{
|
||||||
|
"source.tf": []byte(`variable "x" {
|
||||||
|
default = {
|
||||||
|
"foo"
|
||||||
|
}
|
||||||
|
`),
|
||||||
|
}
|
||||||
|
color := &colorstring.Colorize{
|
||||||
|
Colors: colorstring.DefaultColors,
|
||||||
|
Reset: true,
|
||||||
|
Disable: true,
|
||||||
|
}
|
||||||
|
expected := `
|
||||||
|
Error: Some error
|
||||||
|
|
||||||
|
on source.tf line 3, in variable "x":
|
||||||
|
2: default = {
|
||||||
|
3: "foo"
|
||||||
|
4: }
|
||||||
|
|
||||||
|
...
|
||||||
|
`
|
||||||
|
output := Diagnostic(diags[0], sources, color, 80)
|
||||||
|
|
||||||
|
if output != expected {
|
||||||
|
t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue