Skip to content

Commit d6d2bf0

Browse files
authored
Fix index out of bounds in parital inlay hint (#1948)
My mistake 🤦‍ Signed-off-by: Anders Eknert <anders.eknert@apple.com>
1 parent 7a1235c commit d6d2bf0

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

internal/lsp/inlayhint/inlayhint.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package inlayhint
22

33
import (
44
"fmt"
5+
"slices"
56

67
"github.com/open-policy-agent/opa/v1/ast"
78
"github.com/open-policy-agent/opa/v1/types"
@@ -44,18 +45,17 @@ func FromModule(module *ast.Module, builtins builtinsMap) []lspTypes.InlayHint {
4445
}
4546

4647
func Partial(parseErrors []lspTypes.Diagnostic, policy, uri string, builtins builtinsMap) []lspTypes.InlayHint {
47-
var firstErrorLine uint
48-
for _, parseError := range parseErrors {
49-
if parseError.Range.Start.Line > firstErrorLine {
50-
firstErrorLine = parseError.Range.Start.Line
51-
}
52-
}
48+
firstErrorLine := slices.MinFunc(parseErrors, func(a, b lspTypes.Diagnostic) int {
49+
return util.SafeUintToInt(a.Range.Start.Line - b.Range.Start.Line)
50+
}).Range.Start.Line
5351

52+
// try parse the lines up to the first parse error
5453
if numLines := util.NumLines(policy); firstErrorLine > 0 && firstErrorLine < numLines {
55-
// try parse the lines up to the first parse error
56-
end := util.IndexByteNth(policy, '\n', firstErrorLine+2)
57-
if mod, err := parse.Module(uri, policy[:end]); err == nil {
58-
return FromModule(mod, builtins)
54+
// (1-indexed so don't +1 here)
55+
if end := util.IndexByteNth(policy, '\n', firstErrorLine); end != -1 {
56+
if mod, err := parse.Module(uri, policy[:end]); err == nil {
57+
return FromModule(mod, builtins)
58+
}
5959
}
6060
}
6161

0 commit comments

Comments
 (0)