129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.revisions.ChangeRegion;
|
|
14
|
131
|
15 import dwtx.jface.internal.text.revisions.HunkComputer; // packageimport
|
|
16 import dwtx.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport
|
|
17 import dwtx.jface.internal.text.revisions.Hunk; // packageimport
|
|
18 import dwtx.jface.internal.text.revisions.Colors; // packageimport
|
|
19 import dwtx.jface.internal.text.revisions.Range; // packageimport
|
|
20 import dwtx.jface.internal.text.revisions.RevisionPainter; // packageimport
|
|
21 import dwtx.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport
|
|
22
|
|
23
|
129
|
24 import dwt.dwthelper.utils;
|
|
25
|
|
26 import java.util.LinkedList;
|
|
27 import java.util.List;
|
|
28 import java.util.ListIterator;
|
|
29
|
|
30 import dwtx.core.runtime.Assert;
|
|
31 import dwtx.jface.text.revisions.Revision;
|
|
32 import dwtx.jface.text.source.ILineRange;
|
|
33 import dwtx.jface.text.source.LineRange;
|
|
34
|
|
35 /**
|
|
36 * A change region describes a contiguous range of lines that was changed in the same revision of a
|
|
37 * document. Once it is adjusted to diff information, the originally contiguous range may be split
|
|
38 * into several ranges or even be empty.
|
|
39 *
|
|
40 * @since 3.2
|
|
41 */
|
|
42 public final class ChangeRegion {
|
|
43 private final Revision fRevision;
|
|
44 private final ILineRange fLines;
|
|
45 private final List fAdjusted= new LinkedList();
|
|
46
|
|
47 /**
|
|
48 * Creates a new change region for the given revision and line range.
|
|
49 *
|
|
50 * @param revision the revision of the new region
|
|
51 * @param lines the line range of the new region
|
|
52 * @throws IndexOutOfBoundsException if the line range is not well-formed
|
|
53 */
|
|
54 public ChangeRegion(Revision revision, ILineRange lines) throws IndexOutOfBoundsException {
|
|
55 Assert.isLegal(revision !is null);
|
|
56 Assert.isLegal(lines !is null);
|
|
57 fLines= Range.copy(lines);
|
|
58 fRevision=revision;
|
|
59 clearDiff();
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * Returns the revision that this region belongs to.
|
|
64 *
|
|
65 * @return the revision that this region belongs to
|
|
66 */
|
|
67 public Revision getRevision() {
|
|
68 return fRevision;
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * Returns the original (before applying diff information) line range of this change region.
|
|
73 *
|
|
74 * @return the original (before applying diff information) line range of this change region
|
|
75 */
|
|
76 public ILineRange getOriginalRange() {
|
|
77 return fLines;
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * Returns the list of {@link ILineRange}s of this change region for which the revision
|
|
82 * information is still valid.
|
|
83 *
|
|
84 * @return the list of adjusted line ranges
|
|
85 */
|
|
86 public List getAdjustedRanges() {
|
|
87 return fAdjusted;
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 * Returns the line coverage of the adjusted ranges, an empty range if the coverage is empty.
|
|
92 *
|
|
93 * @return the line coverage of the adjusted ranges
|
|
94 */
|
|
95 public ILineRange getAdjustedCoverage() {
|
|
96 if (fAdjusted.isEmpty())
|
|
97 return new LineRange(fLines.getStartLine(), 0);
|
|
98
|
|
99 Range first= (Range) fAdjusted.get(0);
|
|
100 Range last= (Range) fAdjusted.get(fAdjusted.size() - 1);
|
|
101
|
|
102 return Range.createAbsolute(first.start(), last.end());
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Clears any adjusted ranges, restoring the original range.
|
|
107 */
|
|
108 public void clearDiff() {
|
|
109 fAdjusted.clear();
|
|
110 fAdjusted.add(Range.copy(fLines));
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Adjusts this change region to a diff hunk. This will change the adjusted ranges.
|
|
115 *
|
|
116 * @param hunk the diff hunk to adjust to
|
|
117 */
|
|
118 public void adjustTo(Hunk hunk) {
|
|
119 for (ListIterator it= fAdjusted.listIterator(); it.hasNext();) {
|
|
120 Range range= (Range) it.next();
|
|
121
|
|
122 // do we need a split?
|
|
123 int unchanged= getUnchanged(hunk, range.start());
|
|
124 if (unchanged > 0) {
|
|
125 if (unchanged >= range.length())
|
|
126 continue;
|
|
127 range= range.split(unchanged);
|
|
128 it.add(range);
|
|
129 it.previous(); it.next(); // needed so we can remove below
|
|
130 }
|
|
131
|
|
132 int line= range.start();
|
|
133 Assert.isTrue(hunk.line <= line);
|
|
134
|
|
135 // by how much do we shrink?
|
|
136 int overlap= getOverlap(hunk, line);
|
|
137 if (overlap >= range.length()) {
|
|
138 it.remove();
|
|
139 continue;
|
|
140 }
|
|
141
|
|
142 // by how much do we move?
|
|
143 range.moveBy(hunk.delta + overlap);
|
|
144 range.resizeBy(-overlap);
|
|
145 }
|
|
146
|
|
147 }
|
|
148
|
|
149 private int getUnchanged(Hunk hunk, int line) {
|
|
150 return Math.max(0, hunk.line - line);
|
|
151 }
|
|
152
|
|
153 /*
|
|
154 * Returns the number of lines after line that the hunk reports as changed.
|
|
155 */
|
|
156 private int getOverlap(Hunk hunk, int line) {
|
|
157
|
|
158 int deltaLine= hunk.line + hunk.changed;
|
|
159 if (hunk.delta >= 0) {
|
|
160 if (deltaLine <= line)
|
|
161 return 0;
|
|
162 return deltaLine - line;
|
|
163 }
|
|
164
|
|
165 // hunk.delta < 0
|
|
166 int hunkEnd= deltaLine - hunk.delta;
|
|
167 int cutCount= hunkEnd - line;
|
|
168 return Math.max(0, cutCount);
|
|
169 }
|
|
170
|
|
171 /*
|
|
172 * @see java.lang.Object#toString()
|
|
173 */
|
|
174 public String toString() {
|
|
175 return "ChangeRegion [" + fRevision.toString() + ", [" + fLines.getStartLine() + "+" + fLines.getNumberOfLines() + ")]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
|
176 }
|
|
177 }
|