forked from go-gremlins/gremlins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
63 lines (45 loc) · 1.04 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package diff
import (
"go/token"
"github.com/bluekeyes/go-gitdiff/gitdiff"
)
type FileName string
type Change struct {
StartLine int
EndLine int
}
type Diff map[FileName][]Change
func newDiff(files []*gitdiff.File) Diff {
result := map[FileName][]Change{}
for _, file := range files {
name, changes := newChanges(file)
result[name] = changes
}
return result
}
func newChanges(file *gitdiff.File) (FileName, []Change) {
var changes []Change
for _, fragment := range file.TextFragments {
if fragment.LinesAdded == 0 {
continue
}
startLine := int(fragment.NewPosition + fragment.LeadingContext)
changes = append(changes, Change{
StartLine: startLine,
EndLine: startLine + int(fragment.LinesAdded-1),
})
}
return FileName(file.NewName), changes
}
func (d Diff) IsChanged(pos token.Position) bool {
if len(d) == 0 {
return true
}
fileDiff := d[FileName(pos.Filename)]
for _, change := range fileDiff {
if pos.Line >= change.StartLine && pos.Line <= change.EndLine {
return true
}
}
return false
}