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