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

Method GetGroupedOpCodes

texteditor/difflib/difflib.go:418–461  ·  view source on GitHub ↗

GetGroupedOpCodes isolates change clusters by eliminating ranges with no changes. Return a generator of groups with up to n lines of context. Each group is in the same format as returned by GetOpCodes().

(n int)

Source from the content-addressed store, hash-verified

416// Return a generator of groups with up to n lines of context.
417// Each group is in the same format as returned by GetOpCodes().
418func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode {
419 if n < 0 {
420 n = 3
421 }
422 codes := m.GetOpCodes()
423 if len(codes) == 0 {
424 codes = []OpCode{{'e', 0, 1, 0, 1}}
425 }
426 // Fixup leading and trailing groups if they show no changes.
427 if codes[0].Tag == 'e' {
428 c := codes[0]
429 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
430 codes[0] = OpCode{c.Tag, max(i1, i2-n), i2, max(j1, j2-n), j2}
431 }
432 if codes[len(codes)-1].Tag == 'e' {
433 c := codes[len(codes)-1]
434 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
435 codes[len(codes)-1] = OpCode{c.Tag, i1, min(i2, i1+n), j1, min(j2, j1+n)}
436 }
437 nn := n + n
438
439 var (
440 groups [][]OpCode
441 group []OpCode
442 )
443
444 for _, c := range codes {
445 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
446 // End the current group and start a new one whenever
447 // there is a large range with no changes.
448 if c.Tag == 'e' && i2-i1 > nn {
449 group = append(group, OpCode{c.Tag, i1, min(i2, i1+n),
450 j1, min(j2, j1+n)})
451 groups = append(groups, group)
452 group = []OpCode{}
453 i1, j1 = max(i1, i2-n), max(j1, j2-n)
454 }
455 group = append(group, OpCode{c.Tag, i1, i2, j1, j2})
456 }
457 if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') {
458 groups = append(groups, group)
459 }
460 return groups
461}
462
463// Ratio returns a measure of the sequences' similarity (float in [0,1]).
464//

Callers 4

TestGroupedOpCodesFunction · 0.45
WriteUnifiedDiffFunction · 0.45
WriteContextDiffFunction · 0.45

Calls 1

GetOpCodesMethod · 0.95

Tested by 2

TestGroupedOpCodesFunction · 0.36