Skip to content

Commit 7743d1d

Browse files
committed
internal/lsp: respect range for inlay hints
This is an optimization to avoid calculating inlayhints that are not in the requested range. Change-Id: I311f297d2998ae7d0db822eac540b1c12cae6e23 Reviewed-on: https://go-review.googlesource.com/c/tools/+/412455 gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent 0248714 commit 7743d1d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

internal/lsp/source/inlay_hint.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var AllInlayHints = map[string]*Hint{
104104
},
105105
}
106106

107-
func InlayHint(ctx context.Context, snapshot Snapshot, fh FileHandle, _ protocol.Range) ([]protocol.InlayHint, error) {
107+
func InlayHint(ctx context.Context, snapshot Snapshot, fh FileHandle, pRng protocol.Range) ([]protocol.InlayHint, error) {
108108
ctx, done := event.Start(ctx, "source.InlayHint")
109109
defer done()
110110

@@ -132,8 +132,23 @@ func InlayHint(ctx context.Context, snapshot Snapshot, fh FileHandle, _ protocol
132132
info := pkg.GetTypesInfo()
133133
q := Qualifier(pgf.File, pkg.GetTypes(), info)
134134

135+
// Set the range to the full file if the range is not valid.
136+
start, end := pgf.File.Pos(), pgf.File.End()
137+
if pRng.Start.Line < pRng.End.Line || pRng.Start.Character < pRng.End.Character {
138+
// Adjust start and end for the specified range.
139+
rng, err := pgf.Mapper.RangeToSpanRange(pRng)
140+
if err != nil {
141+
return nil, err
142+
}
143+
start, end = rng.Start, rng.End
144+
}
145+
135146
var hints []protocol.InlayHint
136147
ast.Inspect(pgf.File, func(node ast.Node) bool {
148+
// If not in range, we can stop looking.
149+
if node == nil || node.End() < start || node.Pos() > end {
150+
return false
151+
}
137152
for _, fn := range enabledHints {
138153
hints = append(hints, fn(node, tmap, info, &q)...)
139154
}

0 commit comments

Comments
 (0)