-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffview.go
824 lines (764 loc) · 21.4 KB
/
diffview.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
// Copyright (c) 2020, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package texteditor
/*
// DiffFiles shows the diffs between this file as the A file, and other file as B file,
// in a DiffViewDialog
func DiffFiles(afile, bfile string) (*DiffView, error) {
ab, err := os.ReadFile(afile)
if err != nil {
slog.Error(err.Error())
return nil, err
}
bb, err := os.ReadFile(bfile)
if err != nil {
slog.Error(err.Error())
return nil, err
}
astr := strings.Split(strings.Replace(string(ab), "\r\n", "\n", -1), "\n") // windows safe
bstr := strings.Split(strings.Replace(string(bb), "\r\n", "\n", -1), "\n")
dlg := DiffViewDialog(nil, DlgOpts{Title: "Diff File View:"}, astr, bstr, afile, bfile, "", "")
return dlg, nil
}
// DiffViewDialogFromRevs opens a dialog for displaying diff between file
// at two different revisions from given repository
// if empty, defaults to: A = current HEAD, B = current WC file.
// -1, -2 etc also work as universal ways of specifying prior revisions.
func DiffViewDialogFromRevs(avp *gi.Scene, repo vci.Repo, file string, fbuf *Buf, rev_a, rev_b string) (*DiffView, error) {
var astr, bstr []string
if rev_b == "" { // default to current file
if fbuf != nil {
bstr = fbuf.Strings(false)
} else {
fb, err := textbuf.FileBytes(file)
if err != nil {
return nil, err
}
bstr = textbuf.BytesToLineStrings(fb, false) // don't add new lines
}
} else {
fb, err := repo.FileContents(file, rev_b)
if err != nil {
return nil, err
}
bstr = textbuf.BytesToLineStrings(fb, false) // don't add new lines
}
fb, err := repo.FileContents(file, rev_a)
if err != nil {
return nil, err
}
astr = textbuf.BytesToLineStrings(fb, false) // don't add new lines
if rev_a == "" {
rev_a = "HEAD"
}
return DiffViewDialog(nil, DlgOpts{Title: "DiffVcs: " + dirs.DirAndFile(file)}, astr, bstr, file, file, rev_a, rev_b), nil
}
// DiffViewDialog opens a dialog for displaying diff between two files as line-strings
func DiffViewDialog(ctx gi.Widget, opts DlgOpts, astr, bstr []string, afile, bfile, arev, brev string) *DiffView {
dlg := gi.NewStdDialog(ctx, opts.ToGiOpts(), nil)
frame := dlg.Stage.Scene
prIdx := dlg.PromptWidgetIdx()
dv := frame.InsertNewChild(DiffViewType, prIdx+1, "diff-view").(*DiffView)
dv.SetStretchMax()
dv.FileA = afile
dv.FileB = bfile
dv.RevA = arev
dv.RevB = brev
dv.DiffStrings(astr, bstr)
dlg.Run()
return dv
}
///////////////////////////////////////////////////////////////////
// DiffView
// DiffView presents two side-by-side TextView windows showing the differences
// between two files (represented as lines of strings).
type DiffView struct {
gi.Frame
// first file name being compared
FileA string `desc:"first file name being compared"`
// second file name being compared
FileB string `desc:"second file name being compared"`
// revision for first file, if relevant
RevA string `desc:"revision for first file, if relevant"`
// revision for second file, if relevant
RevB string `desc:"revision for second file, if relevant"`
// the diff records
Diffs textbuf.Diffs `json:"-" xml:"-" desc:"the diff records"`
// textbuf for A
BufA *Buf `json:"-" xml:"-" desc:"textbuf for A"`
// textbuf for B
BufB *Buf `json:"-" xml:"-" desc:"textbuf for B"`
// aligned diffs records diff for aligned lines
AlignD textbuf.Diffs `json:"-" xml:"-" desc:"aligned diffs records diff for aligned lines"`
// edit diffs records aligned diffs with edits applied
EditA textbuf.Diffs `json:"-" xml:"-" desc:"edit diffs records aligned diffs with edits applied"`
// edit diffs records aligned diffs with edits applied
EditB textbuf.Diffs `json:"-" xml:"-" desc:"edit diffs records aligned diffs with edits applied"`
// undo diffs records aligned diffs with edits applied
UndoA textbuf.Diffs `json:"-" xml:"-" desc:"undo diffs records aligned diffs with edits applied"`
// undo diffs records aligned diffs with edits applied
UndoB textbuf.Diffs `json:"-" xml:"-" desc:"undo diffs records aligned diffs with edits applied"`
}
func (dv *DiffView) OnInit() {
dv.DiffViewStyles()
}
func (dv *DiffView) DiffViewStyles() {
dv.Style(func(s *styles.Style) {
s.Grow.Set(1,1)
})
dv.OnWidgetAdded(func(w gi.Widget) {
switch w.PathFrom(dv) {
case "text-a-lay", "text-b-lay":
w.Style(func(s *styles.Style) {
s.Grow.Set(1,1)
s.Min.X.Ch(80)
s.Min.Y.Em(40)
})
case "text-a", "text-b":
w.Style(func(s *styles.Style) {
s.Font.Family = string(gi.Prefs.MonoFont)
})
case "diff-lay":
df := w.(*gi.Layout)
w.Style(func(s *styles.Style) {
s.SetMainAxis(mat32.X)
s.Grow.Set(1,1)
})
}
})
}
// NextDiff moves to next diff region
func (dv *DiffView) NextDiff(ab int) bool {
tva, tvb := dv.TextViews()
tv := tva
if ab == 1 {
tv = tvb
}
nd := len(dv.AlignD)
curLn := tv.CursorPos.Ln
di, df := dv.AlignD.DiffForLine(curLn)
if di < 0 {
return false
}
for {
di++
if di >= nd {
return false
}
df = dv.AlignD[di]
if df.Tag != 'e' {
break
}
}
tva.SetCursorShow(lex.Pos{Ln: df.I1})
tva.ScrollCursorToVertCenter()
tvb.SetCursorShow(lex.Pos{Ln: df.I1})
tvb.ScrollCursorToVertCenter()
return true
}
// PrevDiff moves to previous diff region
func (dv *DiffView) PrevDiff(ab int) bool {
tva, tvb := dv.TextViews()
tv := tva
if ab == 1 {
tv = tvb
}
curLn := tv.CursorPos.Ln
di, df := dv.AlignD.DiffForLine(curLn)
if di < 0 {
return false
}
for {
di--
if di < 0 {
return false
}
df = dv.AlignD[di]
if df.Tag != 'e' {
break
}
}
tva.SetCursorShow(lex.Pos{Ln: df.I1})
tva.ScrollCursorToVertCenter()
tvb.SetCursorShow(lex.Pos{Ln: df.I1})
tvb.ScrollCursorToVertCenter()
return true
}
// ResetDiffs resets all active diff state -- after saving
func (dv *DiffView) ResetDiffs() {
dv.BufA.LineColors = nil
dv.BufB.LineColors = nil
dv.AlignD = nil
dv.EditA = nil
dv.UndoA = nil
dv.EditB = nil
dv.UndoB = nil
}
// RemoveAlignsA removes extra blank text lines added to align with B
func (dv *DiffView) RemoveAlignsA() {
nd := len(dv.EditA)
for i := nd - 1; i >= 0; i-- {
df := dv.EditA[i]
switch df.Tag {
case 'r':
if df.J2 > df.I2 {
spos := lex.Pos{Ln: df.I2, Ch: 0}
epos := lex.Pos{Ln: df.J2, Ch: 0}
dv.BufA.DeleteText(spos, epos, true)
}
case 'i':
spos := lex.Pos{Ln: df.J1, Ch: 0}
epos := lex.Pos{Ln: df.J2, Ch: 0}
dv.BufA.DeleteText(spos, epos, true)
}
}
}
// SaveFileA saves the current state of file A to given filename
func (dv *DiffView) SaveFileA(fname gi.FileName) {
dv.RemoveAlignsA()
dv.RemoveAlignsB()
dv.ResetDiffs()
dv.BufA.SaveAs(fname)
dv.UpdateToolbar()
}
// RemoveAlignsB removes extra blank text lines added to align with A
func (dv *DiffView) RemoveAlignsB() {
nd := len(dv.EditB)
for i := nd - 1; i >= 0; i-- {
df := dv.EditB[i]
switch df.Tag {
case 'r':
if df.I2 > df.J2 {
spos := lex.Pos{Ln: df.J2, Ch: 0}
epos := lex.Pos{Ln: df.I2, Ch: 0}
dv.BufB.DeleteText(spos, epos, true)
}
case 'd':
spos := lex.Pos{Ln: df.I1, Ch: 0}
epos := lex.Pos{Ln: df.I2, Ch: 0}
dv.BufB.DeleteText(spos, epos, true)
}
}
}
// SaveFileB saves the current state of file B to given filename
func (dv *DiffView) SaveFileB(fname gi.FileName) {
dv.RemoveAlignsA()
dv.RemoveAlignsB()
dv.ResetDiffs()
dv.BufB.SaveAs(fname)
dv.UpdateToolbar()
}
// DiffStrings computes differences between two lines-of-strings and displays in
// DiffView.
func (dv *DiffView) DiffStrings(astr, bstr []string) {
if !dv.IsConfiged() {
dv.Config(dv.Sc)
}
av, bv := dv.TextViews()
aupdt := av.UpdateStart()
bupdt := bv.UpdateStart()
dv.BufA.LineColors = nil
dv.BufB.LineColors = nil
del := colors.Red
ins := colors.Green
chg := colors.Blue
dv.Diffs = textbuf.DiffLines(astr, bstr)
nd := len(dv.Diffs)
dv.AlignD = make(textbuf.Diffs, nd)
dv.EditA = make(textbuf.Diffs, nd)
dv.EditB = make(textbuf.Diffs, nd)
var ab, bb [][]byte
absln := 0
bspc := []byte(" ")
for i, df := range dv.Diffs {
switch df.Tag {
case 'r':
di := df.I2 - df.I1
dj := df.J2 - df.J1
mx := max(di, dj)
ad := df
ad.I1 = absln
ad.I2 = absln + di
ad.J1 = absln
ad.J2 = absln + dj
dv.AlignD[i] = ad
dv.EditA[i] = ad
dv.EditB[i] = ad
for i := 0; i < mx; i++ {
dv.BufA.SetLineColor(absln+i, chg)
dv.BufB.SetLineColor(absln+i, chg)
blen := 0
alen := 0
if i < di {
aln := []byte(astr[df.I1+i])
alen = len(aln)
ab = append(ab, aln)
}
if i < dj {
bln := []byte(bstr[df.J1+i])
blen = len(bln)
bb = append(bb, bln)
} else {
bb = append(bb, bytes.Repeat(bspc, alen))
}
if i >= di {
ab = append(ab, bytes.Repeat(bspc, blen))
}
}
absln += mx
case 'd':
di := df.I2 - df.I1
ad := df
ad.I1 = absln
ad.I2 = absln + di
ad.J1 = absln
ad.J2 = absln + di
dv.AlignD[i] = ad
dv.EditA[i] = ad
dv.EditB[i] = ad
for i := 0; i < di; i++ {
dv.BufA.SetLineColor(absln+i, del)
dv.BufB.SetLineColor(absln+i, ins)
aln := []byte(astr[df.I1+i])
alen := len(aln)
ab = append(ab, aln)
bb = append(bb, bytes.Repeat(bspc, alen))
}
absln += di
case 'i':
dj := df.J2 - df.J1
ad := df
ad.I1 = absln
ad.I2 = absln + dj
ad.J1 = absln
ad.J2 = absln + dj
dv.AlignD[i] = ad
dv.EditA[i] = ad
dv.EditB[i] = ad
for i := 0; i < dj; i++ {
dv.BufA.SetLineColor(absln+i, ins)
dv.BufB.SetLineColor(absln+i, del)
bln := []byte(bstr[df.J1+i])
blen := len(bln)
bb = append(bb, bln)
ab = append(ab, bytes.Repeat(bspc, blen))
}
absln += dj
case 'e':
di := df.I2 - df.I1
ad := df
ad.I1 = absln
ad.I2 = absln + di
ad.J1 = absln
ad.J2 = absln + di
dv.AlignD[i] = ad
dv.EditA[i] = ad
dv.EditB[i] = ad
for i := 0; i < di; i++ {
ab = append(ab, []byte(astr[df.I1+i]))
bb = append(bb, []byte(bstr[df.J1+i]))
}
absln += di
}
}
dv.BufA.SetTextLines(ab, false) // don't copy
dv.BufB.SetTextLines(bb, false) // don't copy
dv.TagWordDiffs()
dv.BufA.ReMarkup()
dv.BufB.ReMarkup()
av.UpdateEnd(aupdt)
bv.UpdateEnd(bupdt)
}
// TagWordDiffs goes through replace diffs and tags differences at the
// word level between the two regions.
func (dv *DiffView) TagWordDiffs() {
for _, df := range dv.AlignD {
if df.Tag != 'r' {
continue
}
di := df.I2 - df.I1
dj := df.J2 - df.J1
mx := max(di, dj)
stln := df.I1
for i := 0; i < mx; i++ {
ln := stln + i
ra := dv.BufA.Lines[ln]
rb := dv.BufB.Lines[ln]
lna := lex.RuneFields(ra)
lnb := lex.RuneFields(rb)
fla := lna.RuneStrings(ra)
flb := lnb.RuneStrings(rb)
nab := max(len(fla), len(flb))
ldif := textbuf.DiffLines(fla, flb)
ndif := len(ldif)
if nab > 25 && ndif > nab/2 { // more than half of big diff -- skip
continue
}
for _, ld := range ldif {
switch ld.Tag {
case 'r':
sla := lna[ld.I1]
ela := lna[ld.I2-1]
dv.BufA.AddTag(ln, sla.St, ela.Ed, token.TextStyleError)
slb := lnb[ld.J1]
elb := lnb[ld.J2-1]
dv.BufB.AddTag(ln, slb.St, elb.Ed, token.TextStyleError)
case 'd':
sla := lna[ld.I1]
ela := lna[ld.I2-1]
dv.BufA.AddTag(ln, sla.St, ela.Ed, token.TextStyleDeleted)
case 'i':
slb := lnb[ld.J1]
elb := lnb[ld.J2-1]
dv.BufB.AddTag(ln, slb.St, elb.Ed, token.TextStyleDeleted)
}
}
}
}
}
// ApplyDiff applies change from the other buffer to the buffer for given file
// name, from diff that includes given line.
func (dv *DiffView) ApplyDiff(ab int, line int) bool {
tva, tvb := dv.TextViews()
tv := tva
if ab == 1 {
tv = tvb
}
if line < 0 {
line = tv.CursorPos.Ln
}
di, df := dv.AlignD.DiffForLine(line)
if di < 0 || df.Tag == 'e' {
return false
}
if ab == 0 {
dv.BufA.Undos.Off = false
// srcLen := len(dv.BufB.Lines[df.J2])
spos := lex.Pos{Ln: df.I1, Ch: 0}
epos := lex.Pos{Ln: df.I2, Ch: 0}
src := dv.BufB.Region(spos, epos)
dv.BufA.DeleteText(spos, epos, true)
dv.BufA.InsertText(spos, src.ToBytes(), true) // we always just copy, is blank for delete..
ei, _ := dv.EditA.DiffForLine(df.J1)
if ei >= 0 {
dv.UndoA = append(dv.UndoA, dv.EditA[ei])
if df.Tag == 'd' {
dv.EditA[ei].Tag = 'i' // switch to insert which means that it will delete at end
} else {
dv.EditA = append(dv.EditA[:ei], dv.EditA[ei+1:]...)
}
}
} else {
dv.BufB.Undos.Off = false
spos := lex.Pos{Ln: df.J1, Ch: 0}
epos := lex.Pos{Ln: df.J2, Ch: 0}
src := dv.BufA.Region(spos, epos)
dv.BufB.DeleteText(spos, epos, true)
dv.BufB.InsertText(spos, src.ToBytes(), true)
ei, _ := dv.EditB.DiffForLine(df.I1)
if ei >= 0 {
dv.UndoB = append(dv.UndoB, dv.EditB[ei])
if df.Tag == 'i' {
dv.EditB[ei].Tag = 'd' // switch to delete..
} else {
dv.EditB = append(dv.EditB[:ei], dv.EditB[ei+1:]...)
}
}
}
dv.UpdateToolbar()
return true
}
// UndoDiff undoes last applied change, if any -- just does Undo in buffer and
// updates the list of edits applied.
func (dv *DiffView) UndoDiff(ab int) {
tva, tvb := dv.TextViews()
tv := tva
if ab == 1 {
tv = tvb
}
if ab == 0 {
dv.BufA.Undos.Off = false
nd := len(dv.UndoA)
if nd == 0 {
return
}
df := dv.UndoA[nd-1]
dv.UndoA = dv.UndoA[:nd-1]
if df.Tag == 'd' {
ei, _ := dv.EditA.DiffForLine(df.J1) // existing record
if ei >= 0 {
dv.EditA[ei].Tag = 'd' // restore
}
} else {
ei, _ := dv.EditA.DiffForLine(df.J1 - 1) // place to insert
oi, od := dv.AlignD.DiffForLine(df.J1)
if oi >= 0 {
df.Tag = od.Tag // restore
}
if ei < 0 {
dv.EditA = append(textbuf.Diffs{df}, dv.EditA...)
} else {
tmp := append(dv.EditA[:ei+1], df)
tmp = append(tmp, dv.EditA[ei+1:]...)
dv.EditA = tmp
}
}
} else {
dv.BufB.Undos.Off = false
nd := len(dv.UndoB)
if nd == 0 {
return
}
df := dv.UndoB[nd-1]
dv.UndoB = dv.UndoB[:nd-1]
if df.Tag == 'i' {
ei, _ := dv.EditB.DiffForLine(df.J1) // existing record
if ei >= 0 {
dv.EditB[ei].Tag = 'i' // restore
}
} else {
ei, _ := dv.EditB.DiffForLine(df.J1 - 1) // place to insert
oi, od := dv.AlignD.DiffForLine(df.J1)
if oi >= 0 {
df.Tag = od.Tag // restore
}
if ei < 0 {
dv.EditB = append(textbuf.Diffs{df}, dv.EditB...)
} else {
tmp := append(dv.EditB[:ei+1], df)
tmp = append(tmp, dv.EditB[ei+1:]...)
dv.EditB = tmp
}
}
}
tv.Undo()
dv.UpdateToolbar()
}
func (dv *DiffView) ConfigWidget(vp *gi.Scene) {
dv.Lay = gi.LayoutVert
config := ki.Config{}
config.Add(gi.ToolbarType, "toolbar")
config.Add(gi.LayoutType, "diff-lay")
mods, updt := dv.ConfigChildren(config)
if !mods {
updt = dv.UpdateStart()
dv.SetTextNames()
} else {
dv.ConfigToolbar()
dv.ConfigTexts()
}
dv.UpdateEnd(updt)
}
func (dv *DiffView) FileModifiedUpdateA(bt *gi.Button) {
bt.SetEnabledStateUpdt(dv.BufA.IsChanged())
}
func (dv *DiffView) FileModifiedUpdateB(bt *gi.Button) {
bt.SetEnabledStateUpdt(dv.BufB.IsChanged())
}
func (dv *DiffView) HasDiffsUpdate(bt *gi.Button) {
bt.SetEnabledStateUpdt(len(dv.AlignD) > 1) // always has at least 1
}
func (dv *DiffView) ConfigToolbar() {
tb := dv.Toolbar()
txta := "A: " + dirs.DirAndFile(dv.FileA)
if dv.RevA != "" {
txta += ": " + dv.RevA
}
gi.NewLabel(tb, "label-a").SetText(txta)
tb.AddButton(gi.ActOpts{Label: "Next", Icon: icons.KeyboardArrowDown, Tooltip: "move down to next diff region", UpdateFunc: dv.HasDiffsUpdate}, func(act *gi.Button) {
dv.NextDiff(0)
})
tb.AddButton(gi.ActOpts{Label: "Prev", Icon: icons.KeyboardArrowUp, Tooltip: "move up to previous diff region", UpdateFunc: dv.HasDiffsUpdate}, func(act *gi.Button) {
dv.PrevDiff(0)
})
tb.AddButton(gi.ActOpts{Label: "A <- B", Icon: icons.ContentCopy, Tooltip: "for current diff region, apply change from corresponding version in B, and move to next diff", UpdateFunc: dv.HasDiffsUpdate}, func(act *gi.Button) {
dv.ApplyDiff(0, -1)
dv.NextDiff(0)
})
tb.AddButton(gi.ActOpts{Label: "Undo", Icon: icons.Undo, Tooltip: "undo last diff apply action (A <- B)", UpdateFunc: dv.FileModifiedUpdateA}, func(act *gi.Button) {
dv.UndoDiff(0)
})
tb.AddButton(gi.ActOpts{Label: "Save", Icon: icons.Save, Tooltip: "save edited version of file -- prompts for filename -- this will convert file back to its original form (removing side-by-side alignment) and end the diff editing function", UpdateFunc: dv.FileModifiedUpdateA}, func(act *gi.Button) {
CallMethod(dv, "SaveFileA", dv.Sc)
})
gi.NewStretch(tb, "str")
txtb := "B: " + dirs.DirAndFile(dv.FileB)
if dv.RevB != "" {
txtb += ": " + dv.RevB
}
gi.NewLabel(tb, "label-b", txtb)
tb.AddButton(gi.ActOpts{Label: "Next", Icon: icons.KeyboardArrowDown, Tooltip: "move down to next diff region", UpdateFunc: dv.HasDiffsUpdate}, func(act *gi.Button) {
dv.NextDiff(1)
})
tb.AddButton(gi.ActOpts{Label: "Prev", Icon: icons.KeyboardArrowUp, Tooltip: "move up to previous diff region", UpdateFunc: dv.HasDiffsUpdate}, func(act *gi.Button) {
dv.PrevDiff(1)
})
tb.AddButton(gi.ActOpts{Label: "A -> B", Icon: icons.ContentCopy, Tooltip: "for current diff region, apply change from corresponding version in A, and move to next diff", UpdateFunc: dv.HasDiffsUpdate}, func(act *gi.Button) {
dv.ApplyDiff(1, -1)
dv.NextDiff(1)
})
tb.AddButton(gi.ActOpts{Label: "Undo", Icon: icons.Undo, Tooltip: "undo last diff apply action (A -> B)", UpdateFunc: dv.FileModifiedUpdateB}, func(act *gi.Button) {
dv.UndoDiff(1)
})
tb.AddButton(gi.ActOpts{Label: "Save", Icon: icons.Save, Tooltip: "save edited version of file -- prompts for filename -- this will convert file back to its original form (removing side-by-side alignment) and end the diff editing function", UpdateFunc: dv.FileModifiedUpdateB}, func(act *gi.Button) {
CallMethod(dv, "SaveFileB", dv.Sc)
})
}
func (dv *DiffView) SetTextNames() {
tb := dv.Toolbar()
la := tb.ChildByName("label-a", 0).(*gi.Label)
txta := "A: " + dirs.DirAndFile(dv.FileA)
if dv.RevA != "" {
txta += ": " + dv.RevA
}
la.SetText(txta)
lb := tb.ChildByName("label-b", 4).(*gi.Label)
txtb := "B: " + dirs.DirAndFile(dv.FileB)
if dv.RevB != "" {
txtb += ": " + dv.RevB
}
lb.SetText(txtb)
}
func (dv *DiffView) UpdateToolbar() {
tb := dv.Toolbar()
tb.UpdateButtons()
}
func (dv *DiffView) Toolbar() *gi.Toolbar {
tb := dv.ChildByName("toolbar", 1).(*gi.Toolbar)
return tb
}
func (dv *DiffView) DiffLay() *gi.Layout {
lay := dv.ChildByName("diff-lay", 1).(*gi.Layout)
return lay
}
func (dv *DiffView) TextViewLays() (*gi.Layout, *gi.Layout) {
lay := dv.DiffLay()
a := lay.Child(0).(*gi.Layout)
b := lay.Child(1).(*gi.Layout)
return a, b
}
func (dv *DiffView) TextViews() (*DiffTextView, *DiffTextView) {
a, b := dv.TextViewLays()
av := a.Child(0).(*DiffTextView)
bv := b.Child(0).(*DiffTextView)
return av, bv
}
func (dv *DiffView) ConfigTexts() {
lay := dv.DiffLay()
if dv.BufA == nil {
dv.BufA = &Buf{}
dv.BufA.InitName(dv.BufA, "diff-buf-a")
dv.BufB = &Buf{}
dv.BufB.InitName(dv.BufB, "diff-buf-b")
}
dv.BufA.Filename = gi.FileName(dv.FileA)
dv.BufA.Opts.LineNos = true
dv.BufA.Stat() // update markup
dv.BufB.Filename = gi.FileName(dv.FileB)
dv.BufB.Opts.LineNos = true
dv.BufB.Stat() // update markup
config := ki.Config{}
config.Add(gi.LayoutType, "text-a-lay")
config.Add(gi.LayoutType, "text-b-lay")
mods, updt := lay.ConfigChildren(config)
al, bl := dv.TextViewLays()
if !mods {
updt = lay.UpdateStart()
} else {
av := NewDiffTextView(al, "text-a")
bv := NewDiffTextView(bl, "text-b")
// av.SetReadOnly()
// bv.SetReadOnly()
av.SetBuf(dv.BufA)
bv.SetBuf(dv.BufB)
// sync scrolling
al.On(events.Scroll, func(e events.Event) {
// bl.ScrollToPos(al.Pos(), data.(float32))
})
bl.On(events.Scroll, func(e events.Event) {
// al.ScrollToPos(mat32.Dims(sig), data.(float32))
})
}
lay.UpdateEnd(updt)
}
func (dv *DiffView) IsConfiged() bool {
if dv.NumChildren() > 0 && dv.BufA != nil {
return true
}
return false
}
// DiffViewProps are style properties for DiffView
var DiffViewProps = ki.Props{
"CallMethods": ki.PropSlice{
{"SaveFileA", ki.Props{
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "FileA",
}},
},
}},
{"SaveFileB", ki.Props{
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "FileB",
}},
},
}},
},
}
////////////////////////////////////////////////////////////////////////////////
// DiffTextView
// DiffTextView supports double-click based application of edits from one
// buffer to the other.
type DiffTextView struct {
View
}
func (tv *DiffTextView) OnInit() {
tv.TextViewEvents()
}
func (tv *DiffTextView) DiffView() *DiffView {
dvi := tv.ParentByType(DiffViewType, ki.NoEmbeds)
if dvi == nil {
return nil
}
return dvi.(*DiffView)
}
// MouseEvent handles the events.Event to process double-click
func (tv *DiffTextView) HandleDoubleClick(me events.Event) {
if me.MouseButton() != events.Left {
tv.TextView.MouseEvent(me)
return
}
pt := tv.PointToRelPos(me.LocalPos())
if pt.X >= 0 && pt.X < int(tv.LineNoOff) {
newPos := tv.PixelToCursor(pt)
ln := newPos.Ln
dv := tv.DiffView()
if dv != nil && tv.Buf != nil {
if tv.Nm == "text-a" {
dv.ApplyDiff(0, ln)
} else {
dv.ApplyDiff(1, ln)
}
}
me.SetHandled()
return
}
tv.View.MouseEvent(me)
}
// TextViewEvents sets connections between mouse and key events and actions
func (tv *DiffTextView) TextViewEvents() {
// tv.HoverTooltipEvent()
tv.MouseMoveEvent()
tv.MouseDragEvent()
tv.On(events.DoubleClick, func(e events.Event) {
tv.HandleDoubleClick(e)
})
tv.MouseFocusEvent()
tv.On(events.KeyChord, func(e events.Event) {
kt := e.(*events.Key)
tv.KeyInput(kt)
})
}
*/