-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffview.go
More file actions
265 lines (244 loc) · 7.53 KB
/
diffview.go
File metadata and controls
265 lines (244 loc) · 7.53 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"image"
"image/color"
"strings"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
// DiffFile represents a changed file in the diff.
type DiffFile struct {
Name string
Line int // line index where this file's diff starts
}
// DiffView displays a diff with colored, selectable lines and a file list.
type DiffView struct {
editor StyledEditor
list widget.List
files []DiffFile
fileList SelectList
}
// NewDiffView creates a new DiffView.
func NewDiffView(th *Theme) DiffView {
return DiffView{
list: widget.List{
List: layout.List{
Axis: layout.Vertical,
},
},
fileList: NewVerticalSelectList(unit.Dp(th.TextSize) + 4),
}
}
// SetDiff parses the diff text into styled lines with tab expansion.
func (dv *DiffView) SetDiff(diff string, th *Theme) {
if diff == "" {
dv.editor.SetLines(nil)
dv.files = nil
} else {
raw := strings.Split(diff, "\n")
lines := make([]StyledLine, len(raw))
inDiff := false
for i, orig := range raw {
display := expandTabs(orig, 4)
if strings.HasPrefix(display, "diff --git") {
inDiff = true
}
fg, bg, bold := diffLineStyle(display, inDiff, &th.Colors)
lines[i] = StyledLine{
Text: display,
OriginalText: orig,
Foreground: fg,
Background: bg,
Bold: bold,
}
}
dv.editor.SetLines(lines)
dv.files = parseDiffFiles(lines)
}
dv.list.Position = layout.Position{}
dv.fileList.Selected = -1
}
// Restyle updates line colors in place without resetting scroll or selection.
func (dv *DiffView) Restyle(th *Theme) {
inDiff := false
for i := range dv.editor.Lines {
line := &dv.editor.Lines[i]
if strings.HasPrefix(line.Text, "diff --git") {
inDiff = true
}
line.Foreground, line.Background, line.Bold = diffLineStyle(line.Text, inDiff, &th.Colors)
}
}
func expandTabs(s string, tabWidth int) string {
if !strings.Contains(s, "\t") {
return s
}
var buf strings.Builder
col := 0
for _, r := range s {
if r == '\t' {
spaces := tabWidth - (col % tabWidth)
for range spaces {
buf.WriteByte(' ')
}
col += spaces
} else {
buf.WriteRune(r)
col++
}
}
return buf.String()
}
func parseDiffFiles(lines []StyledLine) []DiffFile {
var files []DiffFile
for i, line := range lines {
if strings.HasPrefix(line.Text, "diff --git ") {
name := line.Text
if idx := strings.Index(line.Text, " b/"); idx >= 0 {
name = line.Text[idx+3:]
}
files = append(files, DiffFile{Name: name, Line: i})
}
}
return files
}
// Layout draws the diff view with file list on the right.
func (dv *DiffView) Layout(th *Theme, gtx layout.Context) layout.Dimensions {
prevFileSelected := dv.fileList.Selected
dims := layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
return dv.layoutDiff(th, gtx)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if len(dv.files) == 0 {
return layout.Dimensions{}
}
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(VerticalLine{Width: 1, Color: th.Colors.SplitterColor}.Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return dv.layoutFileList(th, gtx)
}),
)
}),
)
if dv.fileList.Selected != prevFileSelected &&
dv.fileList.Selected >= 0 && dv.fileList.Selected < len(dv.files) {
dv.list.Position = layout.Position{
First: dv.files[dv.fileList.Selected].Line,
}
}
return dims
}
func (dv *DiffView) layoutDiff(th *Theme, gtx layout.Context) layout.Dimensions {
return FocusBorder(th, gtx.Focused(&dv.editor)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
size := gtx.Constraints.Max
gtx.Constraints = layout.Exact(size)
defer clip.Rect{Max: size}.Push(gtx.Ops).Pop()
event.Op(gtx.Ops, &dv.editor)
lineHeight := gtx.Metric.Sp(th.TextSize * 12 / 10)
charWidth := MeasureCharWidth(th, gtx)
leftInset := gtx.Metric.Dp(4)
numLines := len(dv.editor.Lines)
for {
ev, ok := gtx.Event(
key.FocusFilter{Target: &dv.editor},
// Selection keys.
key.Filter{Focus: &dv.editor, Name: "A", Required: key.ModShortcut},
key.Filter{Focus: &dv.editor, Name: "C", Required: key.ModShortcut},
// Scroll keys.
key.Filter{Focus: &dv.editor, Name: key.NameSpace},
key.Filter{Focus: &dv.editor, Name: key.NamePageDown},
key.Filter{Focus: &dv.editor, Name: key.NamePageUp},
key.Filter{Focus: &dv.editor, Name: key.NameHome},
key.Filter{Focus: &dv.editor, Name: key.NameEnd},
key.Filter{Focus: &dv.editor, Name: key.NameUpArrow},
key.Filter{Focus: &dv.editor, Name: key.NameDownArrow},
// Pointer.
pointer.Filter{Target: &dv.editor, Kinds: pointer.Press | pointer.Drag | pointer.Release},
)
if !ok {
break
}
switch ev := ev.(type) {
case pointer.Event:
dv.editor.HandlePointer(gtx, ev, lineHeight, charWidth, leftInset, dv.list.Position)
case key.Event:
if ev.State == key.Press {
switch ev.Name {
case "A":
dv.editor.SelectAll()
gtx.Execute(op.InvalidateCmd{})
case "C":
dv.editor.CopyToClipboard(gtx)
default:
if ScrollByKey(&dv.list.Position, ev.Name, numLines) {
gtx.Execute(op.InvalidateCmd{})
}
}
}
}
}
style := material.List(th.Theme, &dv.list)
style.AnchorStrategy = material.Overlay
return style.Layout(gtx, numLines,
func(gtx layout.Context, index int) layout.Dimensions {
gtx.Constraints = layout.Exact(image.Point{
X: gtx.Constraints.Max.X,
Y: lineHeight,
})
return dv.editor.LayoutLine(th, gtx, index, charWidth, th.Colors.SelectionColor)
})
})
}
func (dv *DiffView) layoutFileList(th *Theme, gtx layout.Context) layout.Dimensions {
return FixedWidth(gtx, gtx.Metric.Sp(th.TextSize*16), func(gtx layout.Context) layout.Dimensions {
return dv.fileList.Layout(th, gtx, len(dv.files),
func(gtx layout.Context, index int) layout.Dimensions {
file := &dv.files[index]
return dv.fileList.LayoutItem(th, gtx, index, func(gtx layout.Context, style ListItemStyle) layout.Dimensions {
label := material.Body1(th.Theme, file.Name)
label.Color = style.Foreground
label.MaxLines = 1
label.TextSize = th.TextSize * 8 / 10
return label.Layout(gtx)
})
})
})
}
func diffLineStyle(line string, inDiff bool, colors *Colors) (fg, bg color.NRGBA, bold bool) {
switch {
case strings.HasPrefix(line, "commit "):
return colors.CommitColor, color.NRGBA{}, true
case !inDiff && (strings.HasPrefix(line, "Author:") ||
strings.HasPrefix(line, "AuthorDate:") ||
strings.HasPrefix(line, "Commit:") ||
strings.HasPrefix(line, "CommitDate:") ||
strings.HasPrefix(line, "Merge:")):
return colors.CommitMetaColor, color.NRGBA{}, false
case strings.HasPrefix(line, "diff --git"):
return colors.DiffHeaderColor, color.NRGBA{}, true
case strings.HasPrefix(line, "index "),
strings.HasPrefix(line, "--- "),
strings.HasPrefix(line, "+++ "),
strings.HasPrefix(line, "new file"),
strings.HasPrefix(line, "deleted file"),
strings.HasPrefix(line, "similarity"),
strings.HasPrefix(line, "rename "):
return colors.DiffHeaderColor, color.NRGBA{}, false
case strings.HasPrefix(line, "@@"):
return colors.DiffHunkColor, color.NRGBA{}, false
case inDiff && strings.HasPrefix(line, "+"):
return colors.DiffAddColor, colors.DiffAddBg, false
case inDiff && strings.HasPrefix(line, "-"):
return colors.DiffRemoveColor, colors.DiffRemoveBg, false
default:
return colors.DiffNormalColor, color.NRGBA{}, false
}
}