-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support suggested fixes by analyzing a diff
Added `GetSuggestedFix` function creates unified diff for `unmodifiedFile` and `formattedFile`. Then analyzes the diff and creates `analysis.SuggestedFix` if needed. The Analyzer checks the result of `GetSuggestedFix` function and reports as `analysis.Diagnostic`. Signed-off-by: Sergey Vilgelm <sergey@vilgelm.com>
- Loading branch information
Showing
2 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"go/token" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/pmezard/go-difflib/difflib" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var hunkRE = regexp.MustCompile(`@@ -(\d+),(\d+) \+\d+,\d+ @@`) | ||
|
||
func GetSuggestedFix(file *token.File, a, b []byte) (*analysis.SuggestedFix, error) { | ||
d := difflib.UnifiedDiff{ | ||
A: difflib.SplitLines(string(a)), | ||
B: difflib.SplitLines(string(b)), | ||
Context: 1, | ||
} | ||
diff, err := difflib.GetUnifiedDiffString(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if diff == "" { | ||
return nil, nil | ||
} | ||
var ( | ||
fix analysis.SuggestedFix | ||
first = true | ||
edit analysis.TextEdit | ||
buf bytes.Buffer | ||
) | ||
for _, line := range strings.Split(diff, "\n") { | ||
if line == "" { | ||
continue | ||
} | ||
hunk := hunkRE.FindStringSubmatch(line) | ||
switch { | ||
case len(hunk) > 0: | ||
if !first { | ||
edit.NewText = buf.Bytes() | ||
buf = bytes.Buffer{} | ||
fix.TextEdits = append(fix.TextEdits, edit) | ||
edit = analysis.TextEdit{} | ||
} | ||
first = false | ||
start, err := strconv.Atoi(hunk[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
lines, err := strconv.Atoi(hunk[2]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
edit.Pos = file.LineStart(start) | ||
end := start + lines | ||
if end > file.LineCount() { | ||
edit.End = token.Pos(file.Size()) | ||
} else { | ||
edit.End = file.LineStart(end) | ||
} | ||
case line[0] == '+': | ||
buf.WriteString(line[1:]) | ||
buf.WriteRune('\n') | ||
case line[0] == '-': | ||
// just skip | ||
default: | ||
buf.WriteString(line) | ||
buf.WriteRune('\n') | ||
} | ||
} | ||
edit.NewText = buf.Bytes() | ||
fix.TextEdits = append(fix.TextEdits, edit) | ||
|
||
return &fix, nil | ||
} |