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. 4 5package hooks 6 7import ( 8 "fmt" 9 10 "github.com/sergi/go-diff/diffmatchpatch" 11 "golang.org/x/tools/internal/lsp/diff" 12 "golang.org/x/tools/internal/span" 13) 14 15func 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 18 // upstream. 19 defer func() { 20 if r := recover(); r != nil { 21 edits = nil 22 err = fmt.Errorf("unable to compute edits for %s: %s", uri.Filename(), r) 23 } 24 }() 25 diffs := diffmatchpatch.New().DiffMain(before, after, true) 26 edits = make([]diff.TextEdit, 0, len(diffs)) 27 offset := 0 28 for _, d := range diffs { 29 start := span.NewPoint(0, 0, offset) 30 switch d.Type { 31 case diffmatchpatch.DiffDelete: 32 offset += len(d.Text) 33 edits = append(edits, diff.TextEdit{Span: span.New(uri, start, span.NewPoint(0, 0, offset))}) 34 case diffmatchpatch.DiffEqual: 35 offset += len(d.Text) 36 case diffmatchpatch.DiffInsert: 37 edits = append(edits, diff.TextEdit{Span: span.New(uri, start, span.Point{}), NewText: d.Text}) 38 } 39 } 40 return edits, nil 41} 42