Skip to content

Commit 917c595

Browse files
committed
quickfix: backport "QF1003: reorder edits to respect LSP spec"
1 parent b1b6abb commit 917c595

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

quickfix/lint.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,22 @@ func CheckIfElseToSwitch(pass *analysis.Pass) (interface{}, error) {
378378
return
379379
}
380380

381-
var edits []analysis.TextEdit
381+
// Note that we insert the switch statement as the first text edit instead of the last one so that gopls has an
382+
// easier time converting it to an LSP-conforming edit.
383+
//
384+
// Specifically:
385+
// > Text edits ranges must never overlap, that means no part of the original
386+
// > document must be manipulated by more than one edit. However, it is
387+
// > possible that multiple edits have the same start position: multiple
388+
// > inserts, or any number of inserts followed by a single remove or replace
389+
// > edit. If multiple inserts have the same position, the order in the array
390+
// > defines the order in which the inserted strings appear in the resulting
391+
// > text.
392+
//
393+
// See https://go.dev/issue/63930
394+
//
395+
// FIXME this edit forces the first case to begin in column 0 because we ignore indentation. try to fix that.
396+
edits := []analysis.TextEdit{edit.ReplaceWithString(edit.Range{ifstmt.If, ifstmt.If}, fmt.Sprintf("switch %s {\n", report.Render(pass, x)))}
382397
for item := ifstmt; item != nil; {
383398
var end token.Pos
384399
if item.Else != nil {
@@ -413,8 +428,6 @@ func CheckIfElseToSwitch(pass *analysis.Pass) (interface{}, error) {
413428
panic(fmt.Sprintf("unreachable: %T", els))
414429
}
415430
}
416-
// FIXME this forces the first case to begin in column 0. try to fix the indentation
417-
edits = append(edits, edit.ReplaceWithString(edit.Range{ifstmt.If, ifstmt.If}, fmt.Sprintf("switch %s {\n", report.Render(pass, x))))
418431
report.Report(pass, ifstmt, fmt.Sprintf("could use tagged switch on %s", report.Render(pass, x)),
419432
report.Fixes(edit.Fix("Replace with tagged switch", edits...)),
420433
report.ShortRange())

0 commit comments

Comments
 (0)