Hunk.java
3.28 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.util.diff;
import com.day.util.diff.DiffWriter;
import com.day.util.diff.Range;
import java.io.IOException;
public class Hunk {
static final String CVS_ID = "$URL$ $Rev$ $Date$";
public static final int UNMODIFIED = 0;
public static final int INSERTED = 1;
public static final int DELETED = 2;
public static final int CHANGED = 3;
public final Range left;
public final Range right;
public final int type;
private Hunk prev;
private Hunk next;
public Hunk(Range left, Range right, int type, Hunk prev) {
this.left = left;
this.right = right;
this.prev = prev;
this.type = type;
if (prev != null) {
prev.next = this;
}
}
public Hunk prev() {
return this.prev;
}
public Hunk next() {
return this.next;
}
public Hunk write(DiffWriter out, int numContextLines) throws IOException {
if (this.type == 0) {
return this.next;
}
Hunk last = this.next;
Hunk prevLast = this;
while (last != null && (last.type != 0 || last.left.len() <= 2 * numContextLines && last.next != null)) {
prevLast = last;
last = last.next;
}
Range prevRange = this.prev == null ? new Range(this.left.doc, this.left.low, this.left.low) : new Range(this.left.doc, Math.max(this.left.low - numContextLines, 0), this.left.low);
Range lastRange = last == null ? new Range(this.left.doc, prevLast.left.high, prevLast.left.high) : new Range(this.left.doc, last.left.low, Math.min(last.left.high, last.left.low + numContextLines));
int prevLen0 = prevRange.len();
int lastLen0 = lastRange.len();
int prevLen1 = prevRange.len();
int lastLen1 = lastRange.len();
if (this.left.len() == 0 && numContextLines == 0) {
prevLen0 = 1;
--lastLen0;
}
if (this.right.len() == 0 && numContextLines == 0) {
prevLen1 = 1;
--lastLen1;
}
Range newLeft = new Range(this.left.doc, this.left.low - prevLen0, prevLast.left.high + lastLen0);
Range newRight = new Range(this.right.doc, this.right.low - prevLen1, prevLast.right.high + lastLen1);
out.write("@@ -");
out.write(newLeft.toRangeString());
out.write(" +");
out.write(newRight.toRangeString());
out.write(" @@");
out.writeNewLine();
Hunk.writeData(out, prevRange, " ");
Hunk h = this;
while (h != last) {
h.writeBody(out);
h = h.next;
}
Hunk.writeData(out, lastRange, " ");
return last;
}
private void writeBody(DiffWriter out) throws IOException {
if (this.type == 0) {
Hunk.writeData(out, this.left, " ");
}
if ((this.type & 2) != 0) {
Hunk.writeData(out, this.left, "-");
}
if ((this.type & 1) != 0) {
Hunk.writeData(out, this.right, "+");
}
}
private static void writeData(DiffWriter out, Range range, String prefix) throws IOException {
for (int i = range.low; i < range.high; ++i) {
out.write(prefix);
out.write(range.doc.getElements()[i].getString());
}
}
}