Skip to content

Commit 55644ea

Browse files
leitzlerstamblerre
authored andcommitted
internal/lsp: allow narrower scope for convenience CodeActions
Code actions that apply convenience fixes were filtered by the start line so it wasn't possible to narrow the scope to a specific range. This change allows clients to send a specific range (or cursor position) to filter all fixes where the range doesn't intersect with the provided range. It also widens the diagnostic returned by fillstruct analysis. The idea is to provide a way to narrow the scope without breaking clients that do want to ask for code actions using the entire line. Updates golang/go#40438 Change-Id: Ifd984a092a4a3bf0b3a2a5426d3e65023ba4eebc Reviewed-on: https://go-review.googlesource.com/c/tools/+/244519 Run-TryBot: Pontus Leitzler <leitzler@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent 7b4c4ad commit 55644ea

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

internal/lsp/analysis/fillstruct/fillstruct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
115115
}
116116
pass.Report(analysis.Diagnostic{
117117
Message: fmt.Sprintf("Fill %s with default values", name),
118-
Pos: expr.Lbrace,
119-
End: expr.Rbrace,
118+
Pos: expr.Pos(),
119+
End: expr.End(),
120120
})
121121
})
122122
return nil, nil

internal/lsp/code_action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ func convenienceFixes(ctx context.Context, snapshot source.Snapshot, ph source.P
364364
if d.URI != uri {
365365
continue
366366
}
367-
if d.Range.Start.Line != rng.Start.Line {
367+
368+
if !protocol.Intersect(d.Range, rng) {
368369
continue
369370
}
370371
action, err := diagnosticToCommandCodeAction(ctx, snapshot, d, nil, protocol.RefactorRewrite)

internal/lsp/protocol/span.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func ComparePosition(a, b Position) int {
130130
return 0
131131
}
132132

133+
func Intersect(a, b Range) bool {
134+
if a.Start.Line > b.End.Line || a.End.Line < b.Start.Line {
135+
return false
136+
}
137+
return !((a.Start.Line == b.End.Line) && a.Start.Character > b.End.Character ||
138+
(a.End.Line == b.Start.Line) && a.End.Character < b.Start.Character)
139+
}
140+
133141
func (r Range) Format(f fmt.State, _ rune) {
134142
fmt.Fprintf(f, "%v:%v-%v:%v", r.Start.Line, r.Start.Character, r.End.Line, r.End.Character)
135143
}

0 commit comments

Comments
 (0)