1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "github.com/sergi/go-diff/diffmatchpatch"
11 "golang.org/x/tools/internal/lsp/diff"
12 "golang.org/x/tools/internal/span"
15 func ComputeEdits(uri span.URI, before, after string) (edits []diff.TextEdit, err error) {
16 // The go-diff library has an unresolved panic (see golang/go#278774).
17 // TOOD(rstambler): Remove the recover once the issue has been fixed
20 if r := recover(); r != nil {
22 err = fmt.Errorf("unable to compute edits for %s: %s", uri.Filename(), r)
25 diffs := diffmatchpatch.New().DiffMain(before, after, true)
26 edits = make([]diff.TextEdit, 0, len(diffs))
28 for _, d := range diffs {
29 start := span.NewPoint(0, 0, offset)
31 case diffmatchpatch.DiffDelete:
33 edits = append(edits, diff.TextEdit{Span: span.New(uri, start, span.NewPoint(0, 0, offset))})
34 case diffmatchpatch.DiffEqual:
36 case diffmatchpatch.DiffInsert:
37 edits = append(edits, diff.TextEdit{Span: span.New(uri, start, span.Point{}), NewText: d.Text})