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