comparison org.eclipse.jface.text/src/org/eclipse/jface/internal/text/revisions/ChangeRegion.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children 6f068362a363
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.jface.internal.text.revisions.ChangeRegion;
14
15 import org.eclipse.jface.internal.text.revisions.HunkComputer; // packageimport
16 import org.eclipse.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport
17 import org.eclipse.jface.internal.text.revisions.Hunk; // packageimport
18 import org.eclipse.jface.internal.text.revisions.Colors; // packageimport
19 import org.eclipse.jface.internal.text.revisions.Range; // packageimport
20 import org.eclipse.jface.internal.text.revisions.RevisionPainter; // packageimport
21 import org.eclipse.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport
22
23 import java.lang.all;
24 import java.util.ListIterator;
25 import java.util.List;
26 import java.util.LinkedList;
27 import tango.text.convert.Format;
28
29 import org.eclipse.core.runtime.Assert;
30 import org.eclipse.jface.text.revisions.Revision;
31 import org.eclipse.jface.text.source.ILineRange;
32 import org.eclipse.jface.text.source.LineRange;
33
34 /**
35 * A change region describes a contiguous range of lines that was changed in the same revision of a
36 * document. Once it is adjusted to diff information, the originally contiguous range may be split
37 * into several ranges or even be empty.
38 *
39 * @since 3.2
40 */
41 public final class ChangeRegion {
42 private const Revision fRevision;
43 private const ILineRange fLines;
44 private const List fAdjusted;
45
46 /**
47 * Creates a new change region for the given revision and line range.
48 *
49 * @param revision the revision of the new region
50 * @param lines the line range of the new region
51 * @throws IndexOutOfBoundsException if the line range is not well-formed
52 */
53 public this(Revision revision, ILineRange lines) {
54 fAdjusted= new LinkedList();
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= cast(Range) fAdjusted.get(0);
100 Range last= cast(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= cast(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 override String toString() {
175 return Format("ChangeRegion [{},[{}+{})]", fRevision.toString(), fLines.getStartLine(), fLines.getNumberOfLines() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
176 }
177 }