Debian Codegraph @

internal/diff/merge.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/
6
7 import (
8 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
9 )
10
11 // Merge merges two valid, ordered lists of edits.
12 // It returns zero if there was a conflict.
13 //
14 // If corresponding edits in x and y are identical,
15 // they are coalesced in the result.
16 //
17 // If x and y both provide different insertions at the same point,
18 // the insertions from x will be first in the result.
19 //
20 // TODO(adonovan): this algorithm could be improved, for example by
21 // working harder to coalesce non-identical edits that share a common
22 // deletion or common prefix of insertion (see the tests).
23 // Survey the academic literature for insights.
24 func Mergescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Merge().(x, y []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) ([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, bool) {
25 // Make a defensive (premature) copy of the arrays.
26 x = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.25.0 slices/Clone().(x)
27 y = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.25.0 slices/Clone().(y)
28
29 var merged []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#
30 add := func(edit Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) {
31 merged = append(merged, edit)
32 }
33 var xi, yi int
34 for xi < len(x) && yi < len(y) {
35 px := &x[xi]
36 py := &y[yi]
37
38 if *px == *py {
39 // x and y are identical: coalesce.
40 add(*px)
41 xi++
42 yi++
43
44 } else if px.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. <= py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. {
45 // x is entirely before y,
46 // or an insertion at start of y.
47 add(*px)
48 xi++
49
50 } else if py.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. <= px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. {
51 // y is entirely before x,
52 // or an insertion at start of x.
53 add(*py)
54 yi++
55
56 } else if px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. < py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. {
57 // x is partly before y:
58 // split it into a deletion and an edit.
59 add(Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#{px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start., py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start., ""})
60 px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. = py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.
61
62 } else if py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. < px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. {
63 // y is partly before x:
64 // split it into a deletion and an edit.
65 add(Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#{py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start., px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start., ""})
66 py.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. = px.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.
67
68 } else {
69 // x and y are unequal non-insertions
70 // at the same point: conflict.
71 return nil, false
72 }
73 }
74 for ; xi < len(x); xi++ {
75 add(x[xi])
76 }
77 for ; yi < len(y); yi++ {
78 add(y[yi])
79 }
80 return merged, true
81 }