DocumentDiff.java
7.91 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.util.diff;
import com.day.util.diff.*;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
public class DocumentDiff {
static final String CVS_ID = "$URL$ $Rev$ $Date$";
private Document left;
private Document right;
private final Diff.Change change;
private Hunk hunks;
private int numDelta = 0;
public DocumentDiff(Document left, Document right) {
this.left = left;
this.right = right;
Object[] leftElems = left.getElements();
Object[] rightElems = right.getElements();
this.change = new Diff(leftElems, rightElems).diff_2(false);
this.init();
}
public int getNumDeltaElements() {
return this.numDelta;
}
public Hunk getHunks() {
return this.hunks;
}
public void write(StringBuffer buf, int numContextLines) {
try {
StringWriter out = new StringWriter();
this.write(new DiffWriter(out), numContextLines);
out.close();
buf.append(out.getBuffer());
}
catch (IOException e) {
throw new IllegalStateException(e.toString());
}
}
public void write(StringBuffer buf, String lineSeparator, int numContextLines) {
try {
StringWriter out = new StringWriter();
this.write(new DiffWriter(out, lineSeparator), numContextLines);
out.close();
buf.append(out.getBuffer());
}
catch (IOException e) {
throw new IllegalStateException(e.toString());
}
}
public void write(Writer out, int numContextLines) throws IOException {
DiffWriter dw = new DiffWriter(out);
this.write(dw, numContextLines);
dw.flush();
}
public void write(DiffWriter out, int numContextLines) throws IOException {
if (this.hunks != null) {
if (this.left.getSource() != null && this.right.getSource() != null) {
out.write("--- ");
out.write(this.left.getSource().getLocation());
out.writeNewLine();
out.write("+++ ");
out.write(this.right.getSource().getLocation());
out.writeNewLine();
} else {
out.write("--- .mine");
out.writeNewLine();
out.write("+++ .theirs");
out.writeNewLine();
}
for (Hunk hunk = this.hunks; hunk != null; hunk = hunk.write((DiffWriter)out, (int)numContextLines)) {
}
}
}
private void init() {
int len;
Hunk first;
Diff.Change c = this.change;
int leftPos = 0;
int rightPos = 0;
Hunk hunk = first = new Hunk(null, null, 0, null);
while (c != null) {
this.numDelta += Math.max(c.deleted, c.inserted);
if (leftPos < c.line0) {
len = c.line0 - leftPos;
hunk = new Hunk(new Range(this.left, leftPos, leftPos + len), new Range(this.right, rightPos, rightPos + len), 0, hunk);
leftPos += len;
rightPos += len;
}
hunk = new Hunk(new Range(this.left, leftPos, leftPos + c.deleted), new Range(this.right, rightPos, rightPos + c.inserted), (c.deleted > 0 ? 2 : 0) | (c.inserted > 0 ? 1 : 0), hunk);
leftPos += c.deleted;
rightPos += c.inserted;
c = c.nextChange;
}
if (leftPos < this.left.getElements().length) {
len = this.left.getElements().length - leftPos;
new Hunk(new Range(this.left, leftPos, leftPos + len), new Range(this.right, rightPos, rightPos + len), 0, hunk);
}
this.hunks = first.next();
}
public void showChanges(ChangeListener listener, int numContextLines) {
Diff.Change c = this.change;
Document.Element[] lines0 = this.left.getElements();
Document.Element[] lines1 = this.right.getElements();
listener.onDocumentsStart(this.left, this.right);
while (c != null) {
int i;
Diff.Change first = c;
int start0 = Math.max(c.line0 - numContextLines, 0);
int end0 = Math.min(c.line0 + c.deleted + numContextLines, lines0.length);
int start1 = Math.max(c.line1 - numContextLines, 0);
int end1 = Math.min(c.line1 + c.inserted + numContextLines, lines1.length);
while (c != null && c.line0 <= end0) {
end0 = Math.min(c.line0 + c.deleted + numContextLines, lines0.length);
end1 = Math.min(c.line1 + c.inserted + numContextLines, lines1.length);
c = c.nextChange;
}
listener.onChangeStart(start0, end0 - start0 - 1, start1, end1 - start1 - 1);
while (first != c) {
while (start0 < first.line0) {
listener.onUnmodified(start0, start1, lines0[start0]);
++start0;
++start1;
}
for (i = 0; i < first.deleted; ++i) {
listener.onDeleted(start0, first.line1, lines0[start0]);
++start0;
}
for (i = 0; i < first.inserted; ++i) {
listener.onInserted(first.line0, start1, lines1[start1]);
++start1;
}
first = first.nextChange;
}
for (i = 0; i < numContextLines && start0 < lines0.length; ++i) {
listener.onUnmodified(start0, start1, lines0[start0]);
++start0;
++start1;
}
listener.onChangeEnd();
}
listener.onDocumentsEnd(this.left, this.right);
}
public ElementsFactory getMergedLeft() {
return new MergeElementFactory(true);
}
public ElementsFactory getMergedRight() {
return new MergeElementFactory(false);
}
private class MergeElementFactory
implements ElementsFactory {
boolean reverse;
public MergeElementFactory(boolean reverse) {
this.reverse = reverse;
}
public Document.Element[] getElements() {
Diff.Change c = DocumentDiff.this.change;
Document.Element[] lines0 = DocumentDiff.this.left.getElements();
Document.Element[] lines1 = DocumentDiff.this.right.getElements();
ArrayList<Document.Element> elems = new ArrayList<Document.Element>();
while (c != null) {
Diff.Change first = c;
int start0 = 0;
int end0 = lines0.length;
int start1 = 0;
int end1 = lines1.length;
while (c != null && c.line0 <= end0) {
end0 = lines0.length;
end1 = lines1.length;
c = c.nextChange;
}
while (first != c) {
int i;
while (start0 < first.line0) {
elems.add(lines0[start0]);
++start0;
++start1;
}
for (i = 0; i < first.deleted; ++i) {
if (this.reverse) {
elems.add(lines0[start0]);
}
++start0;
}
for (i = 0; i < first.inserted; ++i) {
if (!this.reverse) {
elems.add(lines1[start1]);
}
++start1;
}
first = first.nextChange;
}
while (start0 < lines0.length) {
elems.add(lines0[start0]);
++start0;
++start1;
}
}
return elems.toArray(new Document.Element[elems.size()]);
}
}
}