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