MCPcopy Create free account
hub / github.com/cogentcore/core / ApplyOneDiff

Function ApplyOneDiff

texteditor/text/diffsel.go:73–113  ·  view source on GitHub ↗

ApplyOneDiff applies given diff operator to given "B" lines using original "A" lines and given b line map

(op difflib.OpCode, bedit *[]string, aorig []string, blmap []int)

Source from the content-addressed store, hash-verified

71// ApplyOneDiff applies given diff operator to given "B" lines
72// using original "A" lines and given b line map
73func ApplyOneDiff(op difflib.OpCode, bedit *[]string, aorig []string, blmap []int) {
74 // fmt.Println("applying:", DiffOpString(op))
75 switch op.Tag {
76 case 'r':
77 na := op.J2 - op.J1
78 nb := op.I2 - op.I1
79 b1 := blmap[op.I1]
80 nc := min(na, nb)
81 for i := 0; i < nc; i++ {
82 (*bedit)[b1+i] = aorig[op.J1+i]
83 }
84 db := na - nb
85 if db > 0 {
86 *bedit = slices.Insert(*bedit, b1+nb, aorig[op.J1+nb:op.J2]...)
87 } else {
88 *bedit = slices.Delete(*bedit, b1+na, b1+nb)
89 }
90 for i := op.I2; i < len(blmap); i++ {
91 blmap[i] = blmap[i] + db
92 }
93 case 'd':
94 nb := op.I2 - op.I1
95 b1 := blmap[op.I1]
96 *bedit = slices.Delete(*bedit, b1, b1+nb)
97 for i := op.I2; i < len(blmap); i++ {
98 blmap[i] = blmap[i] - nb
99 }
100 case 'i':
101 na := op.J2 - op.J1
102 b1 := op.I1
103 if op.I1 < len(blmap) {
104 b1 = blmap[op.I1]
105 } else {
106 b1 = len(*bedit)
107 }
108 *bedit = slices.Insert(*bedit, b1, aorig[op.J1:op.J2]...)
109 for i := op.I2; i < len(blmap); i++ {
110 blmap[i] = blmap[i] + na
111 }
112 }
113}
114
115// DiffSelected supports the incremental application of selected diffs
116// between two files (either A -> B or B <- A), with Undo

Callers 2

AtoBMethod · 0.85
BtoAMethod · 0.85

Calls 2

DeleteMethod · 0.65
InsertMethod · 0.45

Tested by

no test coverage detected