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