@@ -378,7 +378,22 @@ func CheckIfElseToSwitch(pass *analysis.Pass) (interface{}, error) {
378
378
return
379
379
}
380
380
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 )))}
382
397
for item := ifstmt ; item != nil ; {
383
398
var end token.Pos
384
399
if item .Else != nil {
@@ -413,8 +428,6 @@ func CheckIfElseToSwitch(pass *analysis.Pass) (interface{}, error) {
413
428
panic (fmt .Sprintf ("unreachable: %T" , els ))
414
429
}
415
430
}
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 ))))
418
431
report .Report (pass , ifstmt , fmt .Sprintf ("could use tagged switch on %s" , report .Render (pass , x )),
419
432
report .Fixes (edit .Fix ("Replace with tagged switch" , edits ... )),
420
433
report .ShortRange ())
0 commit comments