Repository navigation
Expand file tree
/
Copy pathsurface.go
More file actions
116 lines (105 loc) · 3.03 KB
/
Copy pathsurface.go
File metadata and controls
116 lines (105 loc) · 3.03 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package review
import (
"strings"
"unicode"
"github.com/git-pkgs/outline"
)
// maxSurfaceCells caps the LCS table so a huge skeleton can't stall a run.
const maxSurfaceCells = 4_000_000
// SurfaceLine is one declaration-level line that differs between the base and
// head outlines. Context is the enclosing top-level declaration for indented
// lines such as struct fields or methods.
type SurfaceLine struct {
Op string `json:"op"`
Text string `json:"text"`
Context string `json:"context,omitempty"`
}
// Surface compares the outlines (signatures, types, imports, comments) of a
// file's two versions. An empty Lines with Truncated false means only
// function bodies changed.
type Surface struct {
Lines []SurfaceLine `json:"lines"`
Truncated bool `json:"truncated,omitempty"`
}
// BodyOnly reports whether the change left every declaration untouched.
func (s *Surface) BodyOnly() bool { return len(s.Lines) == 0 && !s.Truncated }
type skeletonLine struct {
text string
context string
}
// skeleton returns the declaration lines of src, or false when outline does
// not support the file.
func skeleton(src []byte, name string) ([]skeletonLine, bool) {
if src == nil {
return nil, outline.Supported(name)
}
out, ok := outline.Outline(src, name)
if !ok {
return nil, false
}
var lines []skeletonLine
var top string
for raw := range strings.SplitSeq(out, "\n") {
text := strings.TrimRightFunc(raw, unicode.IsSpace)
if text == "" || strings.TrimSpace(text) == "⋮----" || !hasWord(text) {
continue
}
indented := text[0] == ' ' || text[0] == '\t'
if !indented {
top = text
}
line := skeletonLine{text: strings.TrimSpace(text)}
if indented {
line.context = top
}
lines = append(lines, line)
}
return lines, true
}
func hasWord(s string) bool {
return strings.IndexFunc(s, func(r rune) bool { return unicode.IsLetter(r) || unicode.IsDigit(r) }) >= 0
}
// diffSurface returns nil when the file's language has no outline support.
func diffSurface(name string, base, head []byte) *Surface {
a, okA := skeleton(base, name)
b, okB := skeleton(head, name)
if !okA || !okB {
return nil
}
if len(a)*len(b) > maxSurfaceCells {
return &Surface{Lines: []SurfaceLine{}, Truncated: true}
}
return &Surface{Lines: lcsDiff(a, b)}
}
func lcsDiff(a, b []skeletonLine) []SurfaceLine {
n, m := len(a), len(b)
table := make([][]int, n+1)
for i := range table {
table[i] = make([]int, m+1)
}
for i := n - 1; i >= 0; i-- {
for j := m - 1; j >= 0; j-- {
if a[i].text == b[j].text {
table[i][j] = table[i+1][j+1] + 1
} else {
table[i][j] = max(table[i+1][j], table[i][j+1])
}
}
}
lines := []SurfaceLine{}
i, j := 0, 0
for i < n || j < m {
switch {
case i < n && j < m && a[i].text == b[j].text:
i++
j++
case i < n && (j == m || table[i+1][j] >= table[i][j+1]):
lines = append(lines, SurfaceLine{Op: "-", Text: a[i].text, Context: a[i].context})
i++
default:
lines = append(lines, SurfaceLine{Op: "+", Text: b[j].text, Context: b[j].context})
j++
}
}
return lines
}