Mercurial > projects > dwt-addons
annotate dwtx/jface/text/TreeLineTracker.d @ 172:d994a8b2cdf7
again compile fixes
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 10 Sep 2008 23:14:02 +0200 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2005, 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.text.TreeLineTracker; | |
14 | |
131 | 15 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport |
16 import dwtx.jface.text.DefaultTextHover; // packageimport | |
17 import dwtx.jface.text.AbstractInformationControl; // packageimport | |
18 import dwtx.jface.text.TextUtilities; // packageimport | |
19 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport | |
20 import dwtx.jface.text.AbstractInformationControlManager; // packageimport | |
21 import dwtx.jface.text.ITextViewerExtension2; // packageimport | |
22 import dwtx.jface.text.IDocumentPartitioner; // packageimport | |
23 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport | |
24 import dwtx.jface.text.ITextSelection; // packageimport | |
25 import dwtx.jface.text.Document; // packageimport | |
26 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport | |
27 import dwtx.jface.text.ITextListener; // packageimport | |
28 import dwtx.jface.text.BadPartitioningException; // packageimport | |
29 import dwtx.jface.text.ITextViewerExtension5; // packageimport | |
30 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport | |
31 import dwtx.jface.text.IUndoManager; // packageimport | |
32 import dwtx.jface.text.ITextHoverExtension2; // packageimport | |
33 import dwtx.jface.text.IRepairableDocument; // packageimport | |
34 import dwtx.jface.text.IRewriteTarget; // packageimport | |
35 import dwtx.jface.text.DefaultPositionUpdater; // packageimport | |
36 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport | |
37 import dwtx.jface.text.TextViewerHoverManager; // packageimport | |
38 import dwtx.jface.text.DocumentRewriteSession; // packageimport | |
39 import dwtx.jface.text.TextViewer; // packageimport | |
40 import dwtx.jface.text.ITextViewerExtension8; // packageimport | |
41 import dwtx.jface.text.RegExMessages; // packageimport | |
42 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport | |
43 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport | |
44 import dwtx.jface.text.IWidgetTokenOwner; // packageimport | |
45 import dwtx.jface.text.IViewportListener; // packageimport | |
46 import dwtx.jface.text.GapTextStore; // packageimport | |
47 import dwtx.jface.text.MarkSelection; // packageimport | |
48 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport | |
49 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport | |
50 import dwtx.jface.text.IInformationControlExtension; // packageimport | |
51 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport | |
52 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport | |
53 import dwtx.jface.text.ITextViewerExtension3; // packageimport | |
54 import dwtx.jface.text.IInformationControlCreator; // packageimport | |
55 import dwtx.jface.text.TypedRegion; // packageimport | |
56 import dwtx.jface.text.ISynchronizable; // packageimport | |
57 import dwtx.jface.text.IMarkRegionTarget; // packageimport | |
58 import dwtx.jface.text.TextViewerUndoManager; // packageimport | |
59 import dwtx.jface.text.IRegion; // packageimport | |
60 import dwtx.jface.text.IInformationControlExtension2; // packageimport | |
61 import dwtx.jface.text.IDocumentExtension4; // packageimport | |
62 import dwtx.jface.text.IDocumentExtension2; // packageimport | |
63 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport | |
64 import dwtx.jface.text.DefaultInformationControl; // packageimport | |
65 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport | |
66 import dwtx.jface.text.DocumentClone; // packageimport | |
67 import dwtx.jface.text.DefaultUndoManager; // packageimport | |
68 import dwtx.jface.text.IFindReplaceTarget; // packageimport | |
69 import dwtx.jface.text.IAutoEditStrategy; // packageimport | |
70 import dwtx.jface.text.ILineTrackerExtension; // packageimport | |
71 import dwtx.jface.text.IUndoManagerExtension; // packageimport | |
72 import dwtx.jface.text.TextSelection; // packageimport | |
73 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport | |
74 import dwtx.jface.text.IAutoIndentStrategy; // packageimport | |
75 import dwtx.jface.text.IPainter; // packageimport | |
76 import dwtx.jface.text.IInformationControl; // packageimport | |
77 import dwtx.jface.text.IInformationControlExtension3; // packageimport | |
78 import dwtx.jface.text.ITextViewerExtension6; // packageimport | |
79 import dwtx.jface.text.IInformationControlExtension4; // packageimport | |
80 import dwtx.jface.text.DefaultLineTracker; // packageimport | |
81 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport | |
82 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport | |
83 import dwtx.jface.text.ITextHover; // packageimport | |
84 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport | |
85 import dwtx.jface.text.ILineTracker; // packageimport | |
86 import dwtx.jface.text.Line; // packageimport | |
87 import dwtx.jface.text.ITextViewerExtension; // packageimport | |
88 import dwtx.jface.text.IDocumentAdapter; // packageimport | |
89 import dwtx.jface.text.TextEvent; // packageimport | |
90 import dwtx.jface.text.BadLocationException; // packageimport | |
91 import dwtx.jface.text.AbstractDocument; // packageimport | |
92 import dwtx.jface.text.AbstractLineTracker; // packageimport | |
93 import dwtx.jface.text.ITextPresentationListener; // packageimport | |
94 import dwtx.jface.text.Region; // packageimport | |
95 import dwtx.jface.text.ITextViewer; // packageimport | |
96 import dwtx.jface.text.IDocumentInformationMapping; // packageimport | |
97 import dwtx.jface.text.MarginPainter; // packageimport | |
98 import dwtx.jface.text.IPaintPositionManager; // packageimport | |
99 import dwtx.jface.text.TextPresentation; // packageimport | |
100 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport | |
101 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport | |
102 import dwtx.jface.text.ISelectionValidator; // packageimport | |
103 import dwtx.jface.text.IDocumentExtension; // packageimport | |
104 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport | |
105 import dwtx.jface.text.ConfigurableLineTracker; // packageimport | |
106 import dwtx.jface.text.SlaveDocumentEvent; // packageimport | |
107 import dwtx.jface.text.IDocumentListener; // packageimport | |
108 import dwtx.jface.text.PaintManager; // packageimport | |
109 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport | |
110 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport | |
111 import dwtx.jface.text.IDocumentExtension3; // packageimport | |
112 import dwtx.jface.text.Position; // packageimport | |
113 import dwtx.jface.text.TextMessages; // packageimport | |
114 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport | |
115 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport | |
116 import dwtx.jface.text.IPositionUpdater; // packageimport | |
117 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport | |
118 import dwtx.jface.text.ListLineTracker; // packageimport | |
119 import dwtx.jface.text.ITextInputListener; // packageimport | |
120 import dwtx.jface.text.BadPositionCategoryException; // packageimport | |
121 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport | |
122 import dwtx.jface.text.IInputChangedListener; // packageimport | |
123 import dwtx.jface.text.ITextOperationTarget; // packageimport | |
124 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport | |
125 import dwtx.jface.text.ITextViewerExtension7; // packageimport | |
126 import dwtx.jface.text.IInformationControlExtension5; // packageimport | |
127 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport | |
128 import dwtx.jface.text.JFaceTextUtil; // packageimport | |
129 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport | |
130 import dwtx.jface.text.TabsToSpacesConverter; // packageimport | |
131 import dwtx.jface.text.CursorLinePainter; // packageimport | |
132 import dwtx.jface.text.ITextHoverExtension; // packageimport | |
133 import dwtx.jface.text.IEventConsumer; // packageimport | |
134 import dwtx.jface.text.IDocument; // packageimport | |
135 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport | |
136 import dwtx.jface.text.DocumentCommand; // packageimport | |
137 import dwtx.jface.text.TypedPosition; // packageimport | |
138 import dwtx.jface.text.IEditingSupportRegistry; // packageimport | |
139 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport | |
140 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport | |
141 import dwtx.jface.text.IEditingSupport; // packageimport | |
142 import dwtx.jface.text.IMarkSelection; // packageimport | |
143 import dwtx.jface.text.ISlaveDocumentManager; // packageimport | |
144 import dwtx.jface.text.DocumentEvent; // packageimport | |
145 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport | |
146 import dwtx.jface.text.ITextStore; // packageimport | |
147 import dwtx.jface.text.JFaceTextMessages; // packageimport | |
148 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport | |
149 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport | |
150 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport | |
151 import dwtx.jface.text.TextAttribute; // packageimport | |
152 import dwtx.jface.text.ITextViewerExtension4; // packageimport | |
153 import dwtx.jface.text.ITypedRegion; // packageimport | |
154 | |
129 | 155 import dwt.dwthelper.utils; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
156 import dwtx.dwtxhelper.Collection; |
162 | 157 import tango.text.convert.Format; |
158 import tango.core.Exception; | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
159 |
129 | 160 import dwtx.core.runtime.Assert; |
158 | 161 import dwtx.jface.text.AbstractLineTracker; |
129 | 162 |
163 /** | |
164 * Abstract implementation of <code>ILineTracker</code>. It lets the definition of line | |
165 * delimiters to subclasses. Assuming that '\n' is the only line delimiter, this abstract | |
166 * implementation defines the following line scheme: | |
167 * <ul> | |
168 * <li> "" -> [0,0] | |
169 * <li> "a" -> [0,1] | |
170 * <li> "\n" -> [0,1], [1,0] | |
171 * <li> "a\n" -> [0,2], [2,0] | |
172 * <li> "a\nb" -> [0,2], [2,1] | |
173 * <li> "a\nbc\n" -> [0,2], [2,3], [5,0] | |
174 * </ul> | |
175 * <p> | |
176 * This class must be subclassed. | |
177 * </p> | |
178 * <p> | |
179 * <strong>Performance:</strong> The query operations perform in <i>O(log n)</i> where <var>n</var> | |
180 * is the number of lines in the document. The modification operations roughly perform in <i>O(l * | |
181 * log n)</i> where <var>n</var> is the number of lines in the document and <var>l</var> is the | |
182 * sum of the number of removed, added or modified lines. | |
183 * </p> | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
184 * |
129 | 185 * @since 3.2 |
186 */ | |
187 abstract class TreeLineTracker : ILineTracker { | |
188 /* | |
189 * Differential Balanced Binary Tree | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
190 * |
129 | 191 * Assumption: lines cannot overlap => there exists a total ordering of the lines by their offset, |
192 * which is the same as the ordering by line number | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
193 * |
129 | 194 * Base idea: store lines in a binary search tree |
195 * - the key is the line number / line offset | |
196 * -> lookup_line is O(log n) | |
197 * -> lookup_offset is O(log n) | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
198 * - a change in a line somewhere will change any succeeding line numbers / line offsets |
129 | 199 * -> replace is O(n) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
200 * |
129 | 201 * Differential tree: instead of storing the key (line number, line offset) directly, every node |
202 * stores the difference between its key and its parent's key | |
203 * - the sort key is still the line number / line offset, but it remains "virtual" | |
204 * - inserting a node (a line) really increases the virtual key of all succeeding nodes (lines), but this | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
205 * fact will not be realized in the key information encoded in the nodes. |
129 | 206 * -> any change only affects the nodes in the node's parent chain, although more bookkeeping |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
207 * has to be done when changing a node or balancing the tree |
129 | 208 * -> replace is O(log n) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
209 * -> line offsets and line numbers have to be computed when walking the tree from the root / |
129 | 210 * from a node |
211 * -> still O(log n) | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
212 * |
129 | 213 * The balancing algorithm chosen does not depend on the differential tree property. An AVL tree |
214 * implementation has been chosen for simplicity. | |
215 */ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
216 |
129 | 217 /* |
218 * Turns assertions on/off. Don't make this a a debug option for performance reasons - this way | |
219 * the compiler can optimize the asserts away. | |
220 */ | |
147 | 221 private static const bool ASSERT= false; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
222 |
129 | 223 /** |
224 * The empty delimiter of the last line. The last line and only the last line must have this | |
225 * zero-length delimiter. | |
226 */ | |
147 | 227 private static const String NO_DELIM= ""; //$NON-NLS-1$ |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
228 |
129 | 229 /** |
230 * A node represents one line. Its character and line offsets are 0-based and relative to the | |
231 * subtree covered by the node. All nodes under the left subtree represent lines before, all | |
232 * nodes under the right subtree lines after the current node. | |
233 */ | |
234 private static final class Node { | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
235 this(int length, String delimiter) { |
129 | 236 this.length= length; |
237 this.delimiter= delimiter; | |
238 } | |
239 /** | |
240 * The line index in this node's line tree, or equivalently, the number of lines in the left | |
241 * subtree. | |
242 */ | |
243 int line; | |
244 /** | |
245 * The line offset in this node's line tree, or equivalently, the number of characters in | |
246 * the left subtree. | |
247 */ | |
248 int offset; | |
249 /** The number of characters in this line. */ | |
250 int length; | |
251 /** The line delimiter of this line, needed to answer the delimiter query. */ | |
252 String delimiter; | |
253 /** The parent node, <code>null</code> if this is the root node. */ | |
254 Node parent; | |
255 /** The left subtree, possibly <code>null</code>. */ | |
256 Node left; | |
257 /** The right subtree, possibly <code>null</code>. */ | |
258 Node right; | |
259 /** The balance factor. */ | |
260 byte balance; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
261 |
129 | 262 /* |
263 * @see java.lang.Object#toString() | |
264 */ | |
160 | 265 public final override String toString() { |
129 | 266 String bal; |
267 switch (balance) { | |
268 case 0: | |
269 bal= "="; //$NON-NLS-1$ | |
270 break; | |
271 case 1: | |
272 bal= "+"; //$NON-NLS-1$ | |
273 break; | |
274 case 2: | |
275 bal= "++"; //$NON-NLS-1$ | |
276 break; | |
277 case -1: | |
278 bal= "-"; //$NON-NLS-1$ | |
279 break; | |
280 case -2: | |
281 bal= "--"; //$NON-NLS-1$ | |
282 break; | |
283 default: | |
284 bal= Byte.toString(balance); | |
285 } | |
162 | 286 return Format("[{}+{}+{}|{}|{}]", offset, pureLength(), delimiter.length, line, bal ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ |
129 | 287 } |
288 | |
289 /** | |
290 * Returns the pure (without the line delimiter) length of this line. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
291 * |
129 | 292 * @return the pure line length |
293 */ | |
294 int pureLength() { | |
162 | 295 return length - delimiter.length; |
129 | 296 } |
297 } | |
298 | |
299 /** | |
300 * The root node of the tree, never <code>null</code>. | |
301 */ | |
162 | 302 private Node fRoot; |
129 | 303 |
304 /** | |
305 * Creates a new line tracker. | |
306 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
307 protected this() { |
162 | 308 fRoot= new Node(0, NO_DELIM); |
129 | 309 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
310 |
129 | 311 /** |
312 * Package visible constructor for creating a tree tracker from a list tracker. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
313 * |
129 | 314 * @param tracker |
315 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
316 this(ListLineTracker tracker) { |
162 | 317 fRoot= new Node(0, NO_DELIM); |
129 | 318 final List lines= tracker.getLines(); |
319 final int n= lines.size(); | |
320 if (n is 0) | |
321 return; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
322 |
134 | 323 Line line= cast(Line) lines.get(0); |
129 | 324 String delim= line.delimiter; |
325 if (delim is null) | |
326 delim= NO_DELIM; | |
327 int length= line.length; | |
328 fRoot= new Node(length, delim); | |
329 Node node= fRoot; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
330 |
129 | 331 for (int i= 1; i < n; i++) { |
134 | 332 line= cast(Line) lines.get(i); |
129 | 333 delim= line.delimiter; |
334 if (delim is null) | |
335 delim= NO_DELIM; | |
336 length= line.length; | |
337 node= insertAfter(node, length, delim); | |
338 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
339 |
129 | 340 if (node.delimiter !is NO_DELIM) |
341 insertAfter(node, 0, NO_DELIM); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
342 |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
343 if (ASSERT) checkTree(); |
129 | 344 } |
345 | |
346 /** | |
347 * Returns the node (line) including a certain offset. If the offset is between two | |
348 * lines, the line starting at <code>offset</code> is returned. | |
349 * <p> | |
350 * This means that for offsets smaller than the length, the following holds: | |
351 * </p> | |
352 * <p> | |
353 * <code>line.offset <= offset < line.offset + offset.length</code>. | |
354 * </p> | |
355 * <p> | |
356 * If <code>offset</code> is the document length, then this is true: | |
357 * </p> | |
358 * <p> | |
359 * <code>offset= line.offset + line.length</code>. | |
360 * </p> | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
361 * |
129 | 362 * @param offset a document offset |
363 * @return the line starting at or containing <code>offset</code> | |
364 * @throws BadLocationException if the offset is invalid | |
365 */ | |
156 | 366 private Node nodeByOffset(int offset) { |
129 | 367 /* |
368 * Works for any binary search tree. | |
369 */ | |
370 int remaining= offset; | |
371 Node node= fRoot; | |
372 int line= 0; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
373 |
129 | 374 while (true) { |
375 if (node is null) | |
376 fail(offset); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
377 |
129 | 378 if (remaining < node.offset) { |
379 node= node.left; | |
380 } else { | |
381 remaining -= node.offset; | |
382 line+= node.line; | |
383 if (remaining < node.length | |
384 || remaining is node.length && node.right is null) { // last line | |
385 break; | |
386 } | |
387 remaining -= node.length; | |
388 line ++; | |
389 node= node.right; | |
390 } | |
391 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
392 |
129 | 393 return node; |
394 } | |
395 /** | |
396 * Returns the line number for the given offset. If the offset is between two lines, the line | |
397 * starting at <code>offset</code> is returned. The last line is returned if | |
398 * <code>offset</code> is equal to the document length. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
399 * |
129 | 400 * @param offset a document offset |
401 * @return the line number starting at or containing <code>offset</code> | |
402 * @throws BadLocationException if the offset is invalid | |
403 */ | |
156 | 404 private int lineByOffset(int offset) { |
129 | 405 /* |
406 * Works for any binary search tree. | |
407 */ | |
408 int remaining= offset; | |
409 Node node= fRoot; | |
410 int line= 0; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
411 |
129 | 412 while (true) { |
413 if (node is null) | |
414 fail(offset); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
415 |
129 | 416 if (remaining < node.offset) { |
417 node= node.left; | |
418 } else { | |
419 remaining -= node.offset; | |
420 line+= node.line; | |
421 if (remaining < node.length || remaining is node.length && node.right is null) // last line | |
422 return line; | |
423 | |
424 remaining -= node.length; | |
425 line ++; | |
426 node= node.right; | |
427 } | |
428 } | |
429 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
430 |
129 | 431 /** |
432 * Returns the node (line) with the given line number. Note that the last line is always | |
433 * incomplete, i.e. has the {@link #NO_DELIM} delimiter. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
434 * |
129 | 435 * @param line a line number |
436 * @return the line with the given line number | |
437 * @throws BadLocationException if the line is invalid | |
438 */ | |
156 | 439 private Node nodeByLine(int line) { |
129 | 440 /* |
441 * Works for any binary search tree. | |
442 */ | |
443 int remaining= line; | |
444 int offset= 0; | |
445 Node node= fRoot; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
446 |
129 | 447 while (true) { |
448 if (node is null) | |
449 fail(line); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
450 |
129 | 451 if (remaining is node.line) |
452 break; | |
453 if (remaining < node.line) { | |
454 node= node.left; | |
455 } else { | |
456 remaining -= node.line + 1; | |
457 offset += node.offset + node.length; | |
458 node= node.right; | |
459 } | |
460 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
461 |
129 | 462 return node; |
463 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
464 |
129 | 465 /** |
466 * Returns the offset for the given line number. Note that the | |
467 * last line is always incomplete, i.e. has the {@link #NO_DELIM} delimiter. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
468 * |
129 | 469 * @param line a line number |
470 * @return the line offset with the given line number | |
471 * @throws BadLocationException if the line is invalid | |
472 */ | |
156 | 473 private int offsetByLine(int line) { |
129 | 474 /* |
475 * Works for any binary search tree. | |
476 */ | |
477 int remaining= line; | |
478 int offset= 0; | |
479 Node node= fRoot; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
480 |
129 | 481 while (true) { |
482 if (node is null) | |
483 fail(line); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
484 |
129 | 485 if (remaining is node.line) |
486 return offset + node.offset; | |
487 | |
488 if (remaining < node.line) { | |
489 node= node.left; | |
490 } else { | |
491 remaining -= node.line + 1; | |
492 offset += node.offset + node.length; | |
493 node= node.right; | |
494 } | |
495 } | |
496 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
497 |
129 | 498 /** |
499 * Left rotation - the given node is rotated down, its right child is rotated up, taking the | |
500 * previous structural position of <code>node</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
501 * |
129 | 502 * @param node the node to rotate around |
503 */ | |
504 private void rotateLeft(Node node) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
505 if (ASSERT) Assert.isNotNull(node); |
129 | 506 Node child= node.right; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
507 if (ASSERT) Assert.isNotNull(child); |
129 | 508 bool leftChild= node.parent is null || node is node.parent.left; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
509 |
129 | 510 // restructure |
511 setChild(node.parent, child, leftChild); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
512 |
129 | 513 setChild(node, child.left, false); |
514 setChild(child, node, true); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
515 |
129 | 516 // update relative info |
517 // child becomes the new parent, its line and offset counts increase as the former parent | |
518 // moves under child's left subtree | |
519 child.line += node.line + 1; | |
520 child.offset += node.offset + node.length; | |
521 } | |
522 | |
523 /** | |
524 * Right rotation - the given node is rotated down, its left child is rotated up, taking the | |
525 * previous structural position of <code>node</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
526 * |
129 | 527 * @param node the node to rotate around |
528 */ | |
529 private void rotateRight(Node node) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
530 if (ASSERT) Assert.isNotNull(node); |
129 | 531 Node child= node.left; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
532 if (ASSERT) Assert.isNotNull(child); |
129 | 533 bool leftChild= node.parent is null || node is node.parent.left; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
534 |
129 | 535 setChild(node.parent, child, leftChild); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
536 |
129 | 537 setChild(node, child.right, true); |
538 setChild(child, node, false); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
539 |
129 | 540 // update relative info |
541 // node loses its left subtree, except for what it keeps in its new subtree | |
542 // this is exactly the amount in child | |
543 node.line -= child.line + 1; | |
544 node.offset -= child.offset + child.length; | |
545 } | |
546 | |
547 /** | |
548 * Helper method for moving a child, ensuring that parent pointers are set correctly. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
549 * |
129 | 550 * @param parent the new parent of <code>child</code>, <code>null</code> to replace the |
551 * root node | |
552 * @param child the new child of <code>parent</code>, may be <code>null</code> | |
553 * @param isLeftChild <code>true</code> if <code>child</code> shall become | |
554 * <code>parent</code>'s left child, <code>false</code> if it shall become | |
555 * <code>parent</code>'s right child | |
556 */ | |
557 private void setChild(Node parent, Node child, bool isLeftChild) { | |
558 if (parent is null) { | |
559 if (child is null) | |
560 fRoot= new Node(0, NO_DELIM); | |
561 else | |
562 fRoot= child; | |
563 } else { | |
564 if (isLeftChild) | |
565 parent.left= child; | |
566 else | |
567 parent.right= child; | |
568 } | |
569 if (child !is null) | |
570 child.parent= parent; | |
571 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
572 |
129 | 573 /** |
574 * A left rotation around <code>parent</code>, whose structural position is replaced by | |
575 * <code>node</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
576 * |
129 | 577 * @param node the node moving up and left |
578 * @param parent the node moving left and down | |
579 */ | |
580 private void singleLeftRotation(Node node, Node parent) { | |
581 rotateLeft(parent); | |
582 node.balance= 0; | |
583 parent.balance= 0; | |
584 } | |
585 | |
586 /** | |
587 * A right rotation around <code>parent</code>, whose structural position is replaced by | |
588 * <code>node</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
589 * |
129 | 590 * @param node the node moving up and right |
591 * @param parent the node moving right and down | |
592 */ | |
593 private void singleRightRotation(Node node, Node parent) { | |
594 rotateRight(parent); | |
595 node.balance= 0; | |
596 parent.balance= 0; | |
597 } | |
598 | |
599 /** | |
600 * A double left rotation, first rotating right around <code>node</code>, then left around | |
601 * <code>parent</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
602 * |
129 | 603 * @param node the node that will be rotated right |
604 * @param parent the node moving left and down | |
605 */ | |
606 private void rightLeftRotation(Node node, Node parent) { | |
607 Node child= node.left; | |
608 rotateRight(node); | |
609 rotateLeft(parent); | |
610 if (child.balance is 1) { | |
611 node.balance= 0; | |
612 parent.balance= -1; | |
613 child.balance= 0; | |
614 } else if (child.balance is 0) { | |
615 node.balance= 0; | |
616 parent.balance= 0; | |
617 } else if (child.balance is -1) { | |
618 node.balance= 1; | |
619 parent.balance= 0; | |
620 child.balance= 0; | |
621 } | |
622 } | |
623 | |
624 /** | |
625 * A double right rotation, first rotating left around <code>node</code>, then right around | |
626 * <code>parent</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
627 * |
129 | 628 * @param node the node that will be rotated left |
629 * @param parent the node moving right and down | |
630 */ | |
631 private void leftRightRotation(Node node, Node parent) { | |
632 Node child= node.right; | |
633 rotateLeft(node); | |
634 rotateRight(parent); | |
635 if (child.balance is -1) { | |
636 node.balance= 0; | |
637 parent.balance= 1; | |
638 child.balance= 0; | |
639 } else if (child.balance is 0) { | |
640 node.balance= 0; | |
641 parent.balance= 0; | |
642 } else if (child.balance is 1) { | |
643 node.balance= -1; | |
644 parent.balance= 0; | |
645 child.balance= 0; | |
646 } | |
647 } | |
648 | |
649 /** | |
650 * Inserts a line with the given length and delimiter after <code>node</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
651 * |
129 | 652 * @param node the predecessor of the inserted node |
653 * @param length the line length of the inserted node | |
654 * @param delimiter the delimiter of the inserted node | |
655 * @return the inserted node | |
656 */ | |
657 private Node insertAfter(Node node, int length, String delimiter) { | |
658 /* | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
659 * An insertion really shifts the key of all succeeding nodes. Hence we insert the added node |
129 | 660 * between node and the successor of node. The added node becomes either the right child |
661 * of the predecessor node, or the left child of the successor node. | |
662 */ | |
663 Node added= new Node(length, delimiter); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
664 |
129 | 665 if (node.right is null) |
666 setChild(node, added, false); | |
667 else | |
668 setChild(successorDown(node.right), added, true); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
669 |
129 | 670 // parent chain update |
671 updateParentChain(added, length, 1); | |
672 updateParentBalanceAfterInsertion(added); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
673 |
129 | 674 return added; |
675 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
676 |
129 | 677 /** |
678 * Updates the balance information in the parent chain of node until it reaches the root or | |
679 * finds a node whose balance violates the AVL constraint, which is the re-balanced. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
680 * |
129 | 681 * @param node the child of the first node that needs balance updating |
682 */ | |
683 private void updateParentBalanceAfterInsertion(Node node) { | |
684 Node parent= node.parent; | |
685 while (parent !is null) { | |
686 if (node is parent.left) | |
687 parent.balance--; | |
688 else | |
689 parent.balance++; | |
690 | |
691 switch (parent.balance) { | |
692 case 1: | |
693 case -1: | |
694 node= parent; | |
695 parent= node.parent; | |
696 continue; | |
697 case -2: | |
698 rebalanceAfterInsertionLeft(node); | |
699 break; | |
700 case 2: | |
701 rebalanceAfterInsertionRight(node); | |
702 break; | |
703 case 0: | |
704 break; | |
705 default: | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
706 if (ASSERT) |
129 | 707 Assert.isTrue(false); |
708 } | |
709 return; | |
710 } | |
711 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
712 |
129 | 713 /** |
714 * Re-balances a node whose parent has a double positive balance. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
715 * |
129 | 716 * @param node the node to re-balance |
717 */ | |
718 private void rebalanceAfterInsertionRight(Node node) { | |
719 Node parent= node.parent; | |
720 if (node.balance is 1) { | |
721 singleLeftRotation(node, parent); | |
722 } else if (node.balance is -1) { | |
723 rightLeftRotation(node, parent); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
724 } else if (ASSERT) { |
129 | 725 Assert.isTrue(false); |
726 } | |
727 } | |
728 | |
729 /** | |
730 * Re-balances a node whose parent has a double negative balance. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
731 * |
129 | 732 * @param node the node to re-balance |
733 */ | |
734 private void rebalanceAfterInsertionLeft(Node node) { | |
735 Node parent= node.parent; | |
736 if (node.balance is -1) { | |
737 singleRightRotation(node, parent); | |
738 } else if (node.balance is 1) { | |
739 leftRightRotation(node, parent); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
740 } else if (ASSERT) { |
129 | 741 Assert.isTrue(false); |
742 } | |
743 } | |
744 | |
745 /* | |
746 * @see dwtx.jface.text.ILineTracker#replace(int, int, java.lang.String) | |
747 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
748 public final void replace(int offset, int length, String text) { |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
749 if (ASSERT) checkTree(); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
750 |
129 | 751 // Inlined nodeByOffset as we need both node and offset |
752 int remaining= offset; | |
753 Node first= fRoot; | |
162 | 754 int firstNodeOffset; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
755 |
129 | 756 while (true) { |
757 if (first is null) | |
758 fail(offset); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
759 |
129 | 760 if (remaining < first.offset) { |
761 first= first.left; | |
762 } else { | |
763 remaining -= first.offset; | |
764 if (remaining < first.length | |
765 || remaining is first.length && first.right is null) { // last line | |
766 firstNodeOffset= offset - remaining; | |
767 break; | |
768 } | |
769 remaining -= first.length; | |
770 first= first.right; | |
771 } | |
772 } | |
773 // Inline nodeByOffset end | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
774 if (ASSERT) Assert.isTrue(first !is null); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
775 |
129 | 776 Node last; |
777 if (offset + length < firstNodeOffset + first.length) | |
778 last= first; | |
779 else | |
780 last= nodeByOffset(offset + length); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
781 if (ASSERT) Assert.isTrue(last !is null); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
782 |
129 | 783 int firstLineDelta= firstNodeOffset + first.length - offset; |
784 if (first is last) | |
785 replaceInternal(first, text, length, firstLineDelta); | |
786 else | |
787 replaceFromTo(first, last, text, length, firstLineDelta); | |
788 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
789 if (ASSERT) checkTree(); |
129 | 790 } |
791 | |
792 /** | |
793 * Replace happening inside a single line. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
794 * |
129 | 795 * @param node the affected node |
796 * @param text the added text | |
797 * @param length the replace length, < <code>firstLineDelta</code> | |
798 * @param firstLineDelta the number of characters from the replacement offset to the end of | |
799 * <code>node</code> > <code>length</code> | |
800 */ | |
801 private void replaceInternal(Node node, String text, int length, int firstLineDelta) { | |
802 // 1) modification on a single line | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
803 |
159 | 804 AbstractLineTracker_DelimiterInfo info= text is null ? null : nextDelimiterInfo(text, 0); |
129 | 805 |
806 if (info is null || info.delimiter is null) { | |
807 // a) trivial case: insert into a single node, no line mangling | |
162 | 808 int added= text is null ? 0 : text.length; |
129 | 809 updateLength(node, added - length); |
810 } else { | |
811 // b) more lines to add between two chunks of the first node | |
812 // remember what we split off the first line | |
813 int remainder= firstLineDelta - length; | |
814 String remDelim= node.delimiter; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
815 |
129 | 816 // join the first line with the first added |
817 int consumed= info.delimiterIndex + info.delimiterLength; | |
818 int delta= consumed - firstLineDelta; | |
819 updateLength(node, delta); | |
820 node.delimiter= info.delimiter; | |
821 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
822 // Inline addlines start |
129 | 823 info= nextDelimiterInfo(text, consumed); |
824 while (info !is null) { | |
825 int lineLen= info.delimiterIndex - consumed + info.delimiterLength; | |
826 node= insertAfter(node, lineLen, info.delimiter); | |
827 consumed += lineLen; | |
828 info= nextDelimiterInfo(text, consumed); | |
829 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
830 // Inline addlines end |
129 | 831 |
832 // add remaining chunk merged with last (incomplete) additional line | |
162 | 833 insertAfter(node, remainder + text.length - consumed, remDelim); |
129 | 834 } |
835 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
836 |
129 | 837 /** |
838 * Replace spanning from one node to another. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
839 * |
129 | 840 * @param node the first affected node |
841 * @param last the last affected node | |
842 * @param text the added text | |
843 * @param length the replace length, >= <code>firstLineDelta</code> | |
844 * @param firstLineDelta the number of characters removed from the replacement offset to the end | |
845 * of <code>node</code>, <= <code>length</code> | |
846 */ | |
847 private void replaceFromTo(Node node, Node last, String text, int length, int firstLineDelta) { | |
848 // 2) modification covers several lines | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
849 |
129 | 850 // delete intermediate nodes |
851 // TODO could be further optimized: replace intermediate lines with intermediate added lines | |
852 // to reduce re-balancing | |
162 | 853 Node successor_= successor(node); |
854 while (successor_ !is last) { | |
855 length -= successor_.length; | |
856 Node toDelete= successor_; | |
857 successor_= successor(successor_); | |
129 | 858 updateLength(toDelete, -toDelete.length); |
859 } | |
860 | |
159 | 861 AbstractLineTracker_DelimiterInfo info= text is null ? null : nextDelimiterInfo(text, 0); |
129 | 862 |
863 if (info is null || info.delimiter is null) { | |
162 | 864 int added= text is null ? 0 : text.length; |
129 | 865 |
866 // join the two lines if there are no lines added | |
867 join(node, last, added - length); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
868 |
129 | 869 } else { |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
870 |
129 | 871 // join the first line with the first added |
872 int consumed= info.delimiterIndex + info.delimiterLength; | |
873 updateLength(node, consumed - firstLineDelta); | |
874 node.delimiter= info.delimiter; | |
875 length -= firstLineDelta; | |
876 | |
877 // Inline addLines start | |
878 info= nextDelimiterInfo(text, consumed); | |
879 while (info !is null) { | |
880 int lineLen= info.delimiterIndex - consumed + info.delimiterLength; | |
881 node= insertAfter(node, lineLen, info.delimiter); | |
882 consumed += lineLen; | |
883 info= nextDelimiterInfo(text, consumed); | |
884 } | |
885 // Inline addLines end | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
886 |
162 | 887 updateLength(last, text.length - consumed - length); |
129 | 888 } |
889 } | |
890 | |
891 /** | |
892 * Joins two consecutive node lines, additionally adjusting the resulting length of the combined | |
893 * line by <code>delta</code>. The first node gets deleted. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
894 * |
129 | 895 * @param one the first node to join |
896 * @param two the second node to join | |
897 * @param delta the delta to apply to the remaining single node | |
898 */ | |
899 private void join(Node one, Node two, int delta) { | |
900 int oneLength= one.length; | |
901 updateLength(one, -oneLength); | |
902 updateLength(two, oneLength + delta); | |
903 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
904 |
129 | 905 /** |
906 * Adjusts the length of a node by <code>delta</code>, also adjusting the parent chain of | |
907 * <code>node</code>. If the node's length becomes zero and is not the last (incomplete) | |
908 * node, it is deleted after the update. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
909 * |
129 | 910 * @param node the node to adjust |
911 * @param delta the character delta to add to the node's length | |
912 */ | |
913 private void updateLength(Node node, int delta) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
914 if (ASSERT) Assert.isTrue(node.length + delta >= 0); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
915 |
129 | 916 // update the node itself |
917 node.length += delta; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
918 |
129 | 919 // check deletion |
162 | 920 int lineDelta; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
921 bool delete__= node.length is 0 && node.delimiter !is NO_DELIM; |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
922 if (delete__) |
129 | 923 lineDelta= -1; |
924 else | |
925 lineDelta= 0; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
926 |
129 | 927 // update parent chain |
928 if (delta !is 0 || lineDelta !is 0) | |
929 updateParentChain(node, delta, lineDelta); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
930 |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
931 if (delete__) |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
932 delete_(node); |
129 | 933 } |
934 | |
935 /** | |
936 * Updates the differential indices following the parent chain. All nodes from | |
937 * <code>from.parent</code> to the root are updated. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
938 * |
129 | 939 * @param node the child of the first node to update |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
940 * @param deltaLength the character delta |
129 | 941 * @param deltaLines the line delta |
942 */ | |
943 private void updateParentChain(Node node, int deltaLength, int deltaLines) { | |
944 updateParentChain(node, null, deltaLength, deltaLines); | |
945 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
946 |
129 | 947 /** |
948 * Updates the differential indices following the parent chain. All nodes from | |
949 * <code>from.parent</code> to <code>to</code> (exclusive) are updated. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
950 * |
129 | 951 * @param from the child of the first node to update |
952 * @param to the first node not to update | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
953 * @param deltaLength the character delta |
129 | 954 * @param deltaLines the line delta |
955 */ | |
956 private void updateParentChain(Node from, Node to, int deltaLength, int deltaLines) { | |
957 Node parent= from.parent; | |
958 while (parent !is to) { | |
959 // only update node if update comes from left subtree | |
960 if (from is parent.left) { | |
961 parent.offset += deltaLength; | |
962 parent.line += deltaLines; | |
963 } | |
964 from= parent; | |
965 parent= from.parent; | |
966 } | |
967 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
968 |
129 | 969 /** |
970 * Deletes a node from the tree, re-balancing it if necessary. The differential indices in the | |
971 * node's parent chain have to be updated in advance to calling this method. Generally, don't | |
972 * call <code>delete</code> directly, but call | |
973 * {@link #updateLength(Node, int) update_length(node, -node.length)} to properly remove a | |
974 * node. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
975 * |
129 | 976 * @param node the node to delete. |
977 */ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
978 private void delete_(Node node) { |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
979 if (ASSERT) Assert.isTrue(node !is null); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
980 if (ASSERT) Assert.isTrue(node.length is 0); |
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
981 |
129 | 982 Node parent= node.parent; |
983 Node toUpdate; // the parent of the node that lost a child | |
984 bool lostLeftChild; | |
985 bool isLeftChild= parent is null || node is parent.left; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
986 |
129 | 987 if (node.left is null || node.right is null) { |
988 // 1) node has one child at max - replace parent's pointer with the only child | |
989 // also handles the trivial case of no children | |
990 Node replacement= node.left is null ? node.right : node.left; | |
991 setChild(parent, replacement, isLeftChild); | |
992 toUpdate= parent; | |
993 lostLeftChild= isLeftChild; | |
994 // no updates to do - subtrees stay as they are | |
995 } else if (node.right.left is null) { | |
996 // 2a) node's right child has no left child - replace node with right child, giving node's | |
997 // left subtree to the right child | |
998 Node replacement= node.right; | |
999 setChild(parent, replacement, isLeftChild); | |
1000 setChild(replacement, node.left, true); | |
1001 replacement.line= node.line; | |
1002 replacement.offset= node.offset; | |
1003 replacement.balance= node.balance; | |
1004 toUpdate= replacement; | |
1005 lostLeftChild= false; | |
1006 // } else if (node.left.right is null) { | |
1007 // // 2b) symmetric case | |
1008 // Node replacement= node.left; | |
1009 // set_child(parent, replacement, isLeftChild); | |
1010 // set_child(replacement, node.right, false); | |
1011 // replacement.balance= node.balance; | |
1012 // toUpdate= replacement; | |
1013 // lostLeftChild= true; | |
1014 } else { | |
1015 // 3) hard case - replace node with its successor | |
162 | 1016 Node successor_= successor(node); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1017 |
129 | 1018 // successor exists (otherwise node would not have right child, case 1) |
162 | 1019 if (ASSERT) Assert.isNotNull(successor_); |
129 | 1020 // successor has no left child (a left child would be the real successor of node) |
162 | 1021 if (ASSERT) Assert.isTrue(successor_.left is null); |
1022 if (ASSERT) Assert.isTrue(successor_.line is 0); | |
129 | 1023 // successor is the left child of its parent (otherwise parent would be smaller and |
1024 // hence the real successor) | |
162 | 1025 if (ASSERT) Assert.isTrue(successor_ is successor_.parent.left); |
129 | 1026 // successor is not a child of node (would have been covered by 2a) |
162 | 1027 if (ASSERT) Assert.isTrue(successor_.parent !is node); |
129 | 1028 |
162 | 1029 toUpdate= successor_.parent; |
129 | 1030 lostLeftChild= true; |
1031 | |
1032 // update relative indices | |
162 | 1033 updateParentChain(successor_, node, -successor_.length, -1); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1034 |
129 | 1035 // delete successor from its current place - like 1) |
162 | 1036 setChild(toUpdate, successor_.right, true); |
129 | 1037 |
1038 // move node's subtrees to its successor | |
162 | 1039 setChild(successor_, node.right, false); |
1040 setChild(successor_, node.left, true); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1041 |
129 | 1042 // replace node by successor in its parent |
162 | 1043 setChild(parent, successor_, isLeftChild); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1044 |
129 | 1045 // update the successor |
162 | 1046 successor_.line= node.line; |
1047 successor_.offset= node.offset; | |
1048 successor_.balance= node.balance; | |
129 | 1049 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1050 |
129 | 1051 updateParentBalanceAfterDeletion(toUpdate, lostLeftChild); |
1052 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1053 |
129 | 1054 /** |
1055 * Updates the balance information in the parent chain of node. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1056 * |
129 | 1057 * @param node the first node that needs balance updating |
1058 * @param wasLeftChild <code>true</code> if the deletion happened on <code>node</code>'s | |
1059 * left subtree, <code>false</code> if it occurred on <code>node</code>'s right | |
1060 * subtree | |
1061 */ | |
1062 private void updateParentBalanceAfterDeletion(Node node, bool wasLeftChild) { | |
1063 while (node !is null) { | |
1064 if (wasLeftChild) | |
1065 node.balance++; | |
1066 else | |
1067 node.balance--; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1068 |
129 | 1069 Node parent= node.parent; |
1070 if (parent !is null) | |
1071 wasLeftChild= node is parent.left; | |
1072 | |
1073 switch (node.balance) { | |
1074 case 1: | |
1075 case -1: | |
1076 return; // done, no tree change | |
1077 case -2: | |
1078 if (rebalanceAfterDeletionRight(node.left)) | |
1079 return; | |
1080 break; // propagate up | |
1081 case 2: | |
1082 if (rebalanceAfterDeletionLeft(node.right)) | |
1083 return; | |
1084 break; // propagate up | |
1085 case 0: | |
1086 break; // propagate up | |
1087 default: | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1088 if (ASSERT) |
129 | 1089 Assert.isTrue(false); |
1090 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1091 |
129 | 1092 node= parent; |
1093 } | |
1094 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1095 |
129 | 1096 /** |
1097 * Re-balances a node whose parent has a double positive balance. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1098 * |
129 | 1099 * @param node the node to re-balance |
1100 * @return <code>true</code> if the re-balancement leaves the height at | |
1101 * <code>node.parent</code> constant, <code>false</code> if the height changed | |
1102 */ | |
1103 private bool rebalanceAfterDeletionLeft(Node node) { | |
1104 Node parent= node.parent; | |
1105 if (node.balance is 1) { | |
1106 singleLeftRotation(node, parent); | |
1107 return false; | |
1108 } else if (node.balance is -1) { | |
1109 rightLeftRotation(node, parent); | |
1110 return false; | |
1111 } else if (node.balance is 0) { | |
1112 rotateLeft(parent); | |
1113 node.balance= -1; | |
1114 parent.balance= 1; | |
1115 return true; | |
1116 } else { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1117 if (ASSERT) Assert.isTrue(false); |
129 | 1118 return true; |
1119 } | |
1120 } | |
1121 | |
1122 /** | |
1123 * Re-balances a node whose parent has a double negative balance. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1124 * |
129 | 1125 * @param node the node to re-balance |
1126 * @return <code>true</code> if the re-balancement leaves the height at | |
1127 * <code>node.parent</code> constant, <code>false</code> if the height changed | |
1128 */ | |
1129 private bool rebalanceAfterDeletionRight(Node node) { | |
1130 Node parent= node.parent; | |
1131 if (node.balance is -1) { | |
1132 singleRightRotation(node, parent); | |
1133 return false; | |
1134 } else if (node.balance is 1) { | |
1135 leftRightRotation(node, parent); | |
1136 return false; | |
1137 } else if (node.balance is 0) { | |
1138 rotateRight(parent); | |
1139 node.balance= 1; | |
1140 parent.balance= -1; | |
1141 return true; | |
1142 } else { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1143 if (ASSERT) Assert.isTrue(false); |
129 | 1144 return true; |
1145 } | |
1146 } | |
1147 | |
1148 /** | |
1149 * Returns the successor of a node, <code>null</code> if node is the last node. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1150 * |
129 | 1151 * @param node a node |
1152 * @return the successor of <code>node</code>, <code>null</code> if there is none | |
1153 */ | |
1154 private Node successor(Node node) { | |
1155 if (node.right !is null) | |
1156 return successorDown(node.right); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1157 |
129 | 1158 return successorUp(node); |
1159 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1160 |
129 | 1161 /** |
1162 * Searches the successor of <code>node</code> in its parent chain. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1163 * |
129 | 1164 * @param node a node |
1165 * @return the first node in <code>node</code>'s parent chain that is reached from its left | |
1166 * subtree, <code>null</code> if there is none | |
1167 */ | |
156 | 1168 private Node successorUp(Node node) { |
129 | 1169 Node child= node; |
1170 Node parent= child.parent; | |
1171 while (parent !is null) { | |
1172 if (child is parent.left) | |
1173 return parent; | |
1174 child= parent; | |
1175 parent= child.parent; | |
1176 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1177 if (ASSERT) Assert.isTrue(node.delimiter is NO_DELIM); |
129 | 1178 return null; |
1179 } | |
1180 | |
1181 /** | |
1182 * Searches the left-most node in a given subtree. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1183 * |
129 | 1184 * @param node a node |
1185 * @return the left-most node in the given subtree | |
1186 */ | |
1187 private Node successorDown(Node node) { | |
1188 Node child= node.left; | |
1189 while (child !is null) { | |
1190 node= child; | |
1191 child= node.left; | |
1192 } | |
1193 return node; | |
1194 } | |
1195 | |
1196 /* miscellaneous */ | |
1197 | |
1198 /** | |
1199 * Throws an exception. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1200 * |
129 | 1201 * @param offset the illegal character or line offset that caused the exception |
1202 * @throws BadLocationException always | |
1203 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1204 private void fail(int offset) { |
129 | 1205 throw new BadLocationException(); |
1206 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1207 |
129 | 1208 /** |
1209 * Returns the information about the first delimiter found in the given | |
1210 * text starting at the given offset. | |
1211 * | |
1212 * @param text the text to be searched | |
1213 * @param offset the offset in the given text | |
1214 * @return the information of the first found delimiter or <code>null</code> | |
1215 */ | |
159 | 1216 protected abstract AbstractLineTracker_DelimiterInfo nextDelimiterInfo(String text, int offset); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1217 |
129 | 1218 /* |
1219 * @see dwtx.jface.text.ILineTracker#getLineDelimiter(int) | |
1220 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1221 public final String getLineDelimiter(int line) { |
129 | 1222 Node node= nodeByLine(line); |
1223 return node.delimiter is NO_DELIM ? null : node.delimiter; | |
1224 } | |
1225 | |
1226 /* | |
1227 * @see dwtx.jface.text.ILineTracker#computeNumberOfLines(java.lang.String) | |
1228 */ | |
1229 public final int computeNumberOfLines(String text) { | |
1230 int count= 0; | |
1231 int start= 0; | |
159 | 1232 AbstractLineTracker_DelimiterInfo delimiterInfo= nextDelimiterInfo(text, start); |
129 | 1233 while (delimiterInfo !is null && delimiterInfo.delimiterIndex > -1) { |
1234 ++count; | |
1235 start= delimiterInfo.delimiterIndex + delimiterInfo.delimiterLength; | |
1236 delimiterInfo= nextDelimiterInfo(text, start); | |
1237 } | |
1238 return count; | |
1239 } | |
1240 | |
1241 /* | |
1242 * @see dwtx.jface.text.ILineTracker#getNumberOfLines() | |
1243 */ | |
1244 public final int getNumberOfLines() { | |
1245 // TODO track separately? | |
1246 Node node= fRoot; | |
1247 int lines= 0; | |
1248 while (node !is null) { | |
1249 lines += node.line + 1; | |
1250 node= node.right; | |
1251 } | |
1252 return lines; | |
1253 } | |
1254 | |
1255 /* | |
1256 * @see dwtx.jface.text.ILineTracker#getNumberOfLines(int, int) | |
1257 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1258 public final int getNumberOfLines(int offset, int length) { |
129 | 1259 if (length is 0) |
1260 return 1; | |
1261 | |
1262 int startLine= lineByOffset(offset); | |
1263 int endLine= lineByOffset(offset + length); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1264 |
129 | 1265 return endLine - startLine + 1; |
1266 } | |
1267 | |
1268 /* | |
1269 * @see dwtx.jface.text.ILineTracker#getLineOffset(int) | |
1270 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1271 public final int getLineOffset(int line) { |
129 | 1272 return offsetByLine(line); |
1273 } | |
1274 | |
1275 /* | |
1276 * @see dwtx.jface.text.ILineTracker#getLineLength(int) | |
1277 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1278 public final int getLineLength(int line) { |
129 | 1279 Node node= nodeByLine(line); |
1280 return node.length; | |
1281 } | |
1282 | |
1283 /* | |
1284 * @see dwtx.jface.text.ILineTracker#getLineNumberOfOffset(int) | |
1285 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1286 public final int getLineNumberOfOffset(int offset) { |
129 | 1287 return lineByOffset(offset); |
1288 } | |
1289 | |
1290 /* | |
1291 * @see dwtx.jface.text.ILineTracker#getLineInformationOfOffset(int) | |
1292 */ | |
156 | 1293 public final IRegion getLineInformationOfOffset(int offset) { |
129 | 1294 // Inline nodeByOffset start as we need both node and offset |
1295 int remaining= offset; | |
1296 Node node= fRoot; | |
162 | 1297 int lineOffset; |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1298 |
129 | 1299 while (true) { |
1300 if (node is null) | |
1301 fail(offset); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1302 |
129 | 1303 if (remaining < node.offset) { |
1304 node= node.left; | |
1305 } else { | |
1306 remaining -= node.offset; | |
1307 if (remaining < node.length | |
1308 || remaining is node.length && node.right is null) { // last line | |
1309 lineOffset= offset - remaining; | |
1310 break; | |
1311 } | |
1312 remaining -= node.length; | |
1313 node= node.right; | |
1314 } | |
1315 } | |
1316 // Inline nodeByOffset end | |
1317 return new Region(lineOffset, node.pureLength()); | |
1318 } | |
1319 | |
1320 /* | |
1321 * @see dwtx.jface.text.ILineTracker#getLineInformation(int) | |
1322 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
1323 public final IRegion getLineInformation(int line) { |
129 | 1324 try { |
1325 // Inline nodeByLine start | |
1326 int remaining= line; | |
1327 int offset= 0; | |
1328 Node node= fRoot; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1329 |
129 | 1330 while (true) { |
1331 if (node is null) | |
1332 fail(line); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1333 |
129 | 1334 if (remaining is node.line) { |
1335 offset += node.offset; | |
1336 break; | |
1337 } | |
1338 if (remaining < node.line) { | |
1339 node= node.left; | |
1340 } else { | |
1341 remaining -= node.line + 1; | |
1342 offset += node.offset + node.length; | |
1343 node= node.right; | |
1344 } | |
1345 } | |
1346 // Inline nodeByLine end | |
1347 return new Region(offset, node.pureLength()); | |
1348 } catch (BadLocationException x) { | |
1349 /* | |
1350 * FIXME: this really strange behavior is mandated by the previous line tracker | |
1351 * implementation and included here for compatibility. See | |
1352 * LineTrackerTest3#testFunnyLastLineCompatibility(). | |
1353 */ | |
1354 if (line > 0 && line is getNumberOfLines()) { | |
1355 line= line - 1; | |
1356 // Inline nodeByLine start | |
1357 int remaining= line; | |
1358 int offset= 0; | |
1359 Node node= fRoot; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1360 |
129 | 1361 while (true) { |
1362 if (node is null) | |
1363 fail(line); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1364 |
129 | 1365 if (remaining is node.line) { |
1366 offset+= node.offset; | |
1367 break; | |
1368 } | |
1369 if (remaining < node.line) { | |
1370 node= node.left; | |
1371 } else { | |
1372 remaining -= node.line + 1; | |
1373 offset += node.offset + node.length; | |
1374 node= node.right; | |
1375 } | |
1376 } | |
1377 Node last= node; | |
1378 // Inline nodeByLine end | |
1379 if (last.length > 0) | |
1380 return new Region(offset + last.length, 0); | |
1381 } | |
1382 throw x; | |
1383 } | |
1384 } | |
1385 | |
1386 /* | |
1387 * @see dwtx.jface.text.ILineTracker#set(java.lang.String) | |
1388 */ | |
1389 public final void set(String text) { | |
1390 fRoot= new Node(0, NO_DELIM); | |
1391 try { | |
1392 replace(0, 0, text); | |
1393 } catch (BadLocationException x) { | |
162 | 1394 throw new AssertException(__FILE__,__LINE__); |
129 | 1395 } |
1396 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1397 |
129 | 1398 /* |
1399 * @see java.lang.Object#toString() | |
1400 */ | |
160 | 1401 public override String toString() { |
129 | 1402 int depth= computeDepth(fRoot); |
1403 int WIDTH= 30; | |
162 | 1404 int leaves= cast(int) Math.pow( cast(real)2, cast(uint) depth - 1); |
129 | 1405 int width= WIDTH * leaves; |
1406 String empty= "."; //$NON-NLS-1$ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1407 |
129 | 1408 List roots= new LinkedList(); |
1409 roots.add(fRoot); | |
1410 StringBuffer buf= new StringBuffer((width + 1) * depth); | |
1411 int nodes= 1; | |
1412 int indents= leaves; | |
1413 char[] space= new char[leaves * WIDTH / 2]; | |
1414 Arrays.fill(space, ' '); | |
1415 for(int d= 0; d < depth; d++) { | |
1416 // compute indent | |
1417 indents /= 2; | |
1418 int spaces= Math.max(0, indents * WIDTH - WIDTH / 2); | |
1419 // print nodes | |
1420 for (ListIterator it= roots.listIterator(); it.hasNext();) { | |
1421 // pad before | |
162 | 1422 buf.append(space[ 0 .. spaces]); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1423 |
134 | 1424 Node node= cast(Node) it.next(); |
129 | 1425 String box; |
1426 // replace the node with its children | |
1427 if (node is null) { | |
162 | 1428 it.add(cast(Object)null); |
129 | 1429 box= empty; |
1430 } else { | |
1431 it.set(node.left); | |
1432 it.add(node.right); | |
1433 box= node.toString(); | |
1434 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1435 |
129 | 1436 // draw the node, pad to WIDTH |
1437 int pad_left= (WIDTH - box.length() + 1) / 2; | |
1438 int pad_right= WIDTH - box.length() - pad_left; | |
162 | 1439 buf.append(space[ 0 .. pad_left]); |
129 | 1440 buf.append(box); |
162 | 1441 buf.append(space[ 0 .. pad_right]); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1442 |
129 | 1443 // pad after |
162 | 1444 buf.append(space[ 0 .. spaces]); |
129 | 1445 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1446 |
129 | 1447 buf.append('\n'); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1448 nodes *= 2; |
129 | 1449 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1450 |
129 | 1451 return buf.toString(); |
1452 } | |
1453 | |
1454 /** | |
1455 * Recursively computes the depth of the tree. Only used by {@link #toString()}. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1456 * |
129 | 1457 * @param root the subtree to compute the depth of, may be <code>null</code> |
1458 * @return the depth of the given tree, 0 if it is <code>null</code> | |
1459 */ | |
1460 private byte computeDepth(Node root) { | |
1461 if (root is null) | |
1462 return 0; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1463 |
134 | 1464 return cast(byte) (Math.max(computeDepth(root.left), computeDepth(root.right)) + 1); |
129 | 1465 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1466 |
129 | 1467 /** |
1468 * Debug-only method that checks the tree structure and the differential offsets. | |
1469 */ | |
1470 private void checkTree() { | |
1471 checkTreeStructure(fRoot); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1472 |
129 | 1473 try { |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1474 checkTreeOffsets(nodeByOffset(0), [0, 0], null); |
129 | 1475 } catch (BadLocationException x) { |
162 | 1476 throw new AssertException(__FILE__,__LINE__); |
129 | 1477 } |
1478 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1479 |
129 | 1480 /** |
1481 * Debug-only method that validates the tree structure below <code>node</code>. I.e. it | |
1482 * checks whether all parent/child pointers are consistent and whether the AVL balance | |
1483 * information is correct. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1484 * |
129 | 1485 * @param node the node to validate |
1486 * @return the depth of the tree under <code>node</code> | |
1487 */ | |
1488 private byte checkTreeStructure(Node node) { | |
1489 if (node is null) | |
1490 return 0; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1491 |
129 | 1492 byte leftDepth= checkTreeStructure(node.left); |
1493 byte rightDepth= checkTreeStructure(node.right); | |
1494 Assert.isTrue(node.balance is rightDepth - leftDepth); | |
1495 Assert.isTrue(node.left is null || node.left.parent is node); | |
1496 Assert.isTrue(node.right is null || node.right.parent is node); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1497 |
134 | 1498 return cast(byte) (Math.max(rightDepth, leftDepth) + 1); |
129 | 1499 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1500 |
129 | 1501 /** |
1502 * Debug-only method that checks the differential offsets of the tree, starting at | |
1503 * <code>node</code> and continuing until <code>last</code>. | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1504 * |
129 | 1505 * @param node the first <code>Node</code> to check, may be <code>null</code> |
1506 * @param offLen an array of length 2, with <code>offLen[0]</code> the expected offset of | |
1507 * <code>node</code> and <code>offLen[1]</code> the expected line of | |
1508 * <code>node</code> | |
1509 * @param last the last <code>Node</code> to check, may be <code>null</code> | |
1510 * @return an <code>int[]</code> of length 2, with the first element being the character | |
1511 * length of <code>node</code>'s subtree, and the second element the number of lines | |
1512 * in <code>node</code>'s subtree | |
1513 */ | |
1514 private int[] checkTreeOffsets(Node node, int[] offLen, Node last) { | |
1515 if (node is last) | |
1516 return offLen; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1517 |
129 | 1518 Assert.isTrue(node.offset is offLen[0]); |
1519 Assert.isTrue(node.line is offLen[1]); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1520 |
129 | 1521 if (node.right !is null) { |
1522 int[] result= checkTreeOffsets(successorDown(node.right), new int[2], node); | |
1523 offLen[0] += result[0]; | |
1524 offLen[1] += result[1]; | |
1525 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
1526 |
129 | 1527 offLen[0] += node.length; |
1528 offLen[1]++; | |
1529 return checkTreeOffsets(node.parent, offLen, last); | |
1530 } | |
1531 } |