Mercurial > projects > dwt-addons
annotate dwtx/jface/text/rules/FastPartitioner.d @ 199:eb98a5cbfd78
Fix: potential problem with synchronized, thx torhu.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 13 Mar 2009 17:03:26 +0100 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.rules.FastPartitioner; | |
14 | |
131 | 15 import dwtx.jface.text.rules.ITokenScanner; // packageimport |
16 import dwtx.jface.text.rules.Token; // packageimport | |
17 import dwtx.jface.text.rules.RuleBasedScanner; // packageimport | |
18 import dwtx.jface.text.rules.EndOfLineRule; // packageimport | |
19 import dwtx.jface.text.rules.WordRule; // packageimport | |
20 import dwtx.jface.text.rules.WhitespaceRule; // packageimport | |
21 import dwtx.jface.text.rules.WordPatternRule; // packageimport | |
22 import dwtx.jface.text.rules.IPredicateRule; // packageimport | |
23 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport | |
24 import dwtx.jface.text.rules.NumberRule; // packageimport | |
25 import dwtx.jface.text.rules.SingleLineRule; // packageimport | |
26 import dwtx.jface.text.rules.PatternRule; // packageimport | |
27 import dwtx.jface.text.rules.RuleBasedDamagerRepairer; // packageimport | |
28 import dwtx.jface.text.rules.ICharacterScanner; // packageimport | |
29 import dwtx.jface.text.rules.IRule; // packageimport | |
30 import dwtx.jface.text.rules.DefaultDamagerRepairer; // packageimport | |
31 import dwtx.jface.text.rules.IToken; // packageimport | |
32 import dwtx.jface.text.rules.IPartitionTokenScanner; // packageimport | |
33 import dwtx.jface.text.rules.MultiLineRule; // packageimport | |
34 import dwtx.jface.text.rules.RuleBasedPartitioner; // packageimport | |
35 import dwtx.jface.text.rules.RuleBasedPartitionScanner; // packageimport | |
36 import dwtx.jface.text.rules.BufferedRuleBasedScanner; // packageimport | |
37 import dwtx.jface.text.rules.IWhitespaceDetector; // packageimport | |
38 | |
129 | 39 import dwt.dwthelper.utils; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
40 import dwtx.dwtxhelper.Collection; |
162 | 41 import tango.text.convert.Format; |
129 | 42 |
43 import dwtx.core.runtime.Assert; | |
44 import dwtx.core.runtime.Platform; | |
45 import dwtx.jface.text.BadLocationException; | |
46 import dwtx.jface.text.BadPositionCategoryException; | |
47 import dwtx.jface.text.DefaultPositionUpdater; | |
48 import dwtx.jface.text.DocumentEvent; | |
49 import dwtx.jface.text.DocumentRewriteSession; | |
50 import dwtx.jface.text.IDocument; | |
51 import dwtx.jface.text.IDocumentPartitioner; | |
52 import dwtx.jface.text.IDocumentPartitionerExtension; | |
53 import dwtx.jface.text.IDocumentPartitionerExtension2; | |
54 import dwtx.jface.text.IDocumentPartitionerExtension3; | |
55 import dwtx.jface.text.IRegion; | |
56 import dwtx.jface.text.ITypedRegion; | |
57 import dwtx.jface.text.Position; | |
58 import dwtx.jface.text.Region; | |
59 import dwtx.jface.text.TextUtilities; | |
60 import dwtx.jface.text.TypedPosition; | |
61 import dwtx.jface.text.TypedRegion; | |
62 | |
63 | |
64 | |
65 /** | |
66 * A standard implementation of a document partitioner. It uses an | |
67 * {@link IPartitionTokenScanner} to scan the document and to determine the | |
68 * document's partitioning. The tokens returned by the scanner must return the | |
69 * partition type as their data. The partitioner remembers the document's | |
70 * partitions in the document itself rather than maintaining its own data | |
71 * structure. | |
72 * <p> | |
73 * To reduce array creations in {@link IDocument#getPositions(String)}, the | |
74 * positions get cached. The cache is cleared after updating the positions in | |
75 * {@link #documentChanged2(DocumentEvent)}. Subclasses need to call | |
76 * {@link #clearPositionCache()} after modifying the partitioner's positions. | |
77 * The cached positions may be accessed through {@link #getPositions()}. | |
78 * </p> | |
79 * | |
80 * @see IPartitionTokenScanner | |
81 * @since 3.1 | |
82 */ | |
83 public class FastPartitioner : IDocumentPartitioner, IDocumentPartitionerExtension, IDocumentPartitionerExtension2, IDocumentPartitionerExtension3 { | |
84 | |
85 /** | |
86 * The position category this partitioner uses to store the document's partitioning information. | |
87 */ | |
147 | 88 private static const String CONTENT_TYPES_CATEGORY= "__content_types_category"; //$NON-NLS-1$ |
129 | 89 /** The partitioner's scanner */ |
90 protected final IPartitionTokenScanner fScanner; | |
91 /** The legal content types of this partitioner */ | |
92 protected final String[] fLegalContentTypes; | |
93 /** The partitioner's document */ | |
94 protected IDocument fDocument; | |
95 /** The document length before a document change occurred */ | |
96 protected int fPreviousDocumentLength; | |
97 /** The position updater used to for the default updating of partitions */ | |
98 protected final DefaultPositionUpdater fPositionUpdater; | |
99 /** The offset at which the first changed partition starts */ | |
100 protected int fStartOffset; | |
101 /** The offset at which the last changed partition ends */ | |
102 protected int fEndOffset; | |
103 /**The offset at which a partition has been deleted */ | |
104 protected int fDeleteOffset; | |
105 /** | |
106 * The position category this partitioner uses to store the document's partitioning information. | |
107 */ | |
146 | 108 private const String fPositionCategory; |
129 | 109 /** |
110 * The active document rewrite session. | |
111 */ | |
112 private DocumentRewriteSession fActiveRewriteSession; | |
113 /** | |
114 * Flag indicating whether this partitioner has been initialized. | |
115 */ | |
116 private bool fIsInitialized= false; | |
117 /** | |
118 * The cached positions from our document, so we don't create a new array every time | |
119 * someone requests partition information. | |
120 */ | |
121 private Position[] fCachedPositions= null; | |
122 /** Debug option for cache consistency checking. */ | |
162 | 123 private static bool CHECK_CACHE_CONSISTENCY_; |
124 private static bool CHECK_CACHE_CONSISTENCY_init; | |
125 private static bool CHECK_CACHE_CONSISTENCY(){ | |
126 if( !CHECK_CACHE_CONSISTENCY_init ){ | |
127 synchronized(FastPartitioner.classinfo ){ | |
128 if( !CHECK_CACHE_CONSISTENCY_init ){ | |
129 CHECK_CACHE_CONSISTENCY_init = true; | |
130 CHECK_CACHE_CONSISTENCY_ = "true".equalsIgnoreCase(Platform.getDebugOption("dwtx.jface.text/debug/FastPartitioner/PositionCache")); //$NON-NLS-1$//$NON-NLS-2$; | |
131 } | |
132 } | |
133 } | |
134 return CHECK_CACHE_CONSISTENCY_; | |
135 } | |
129 | 136 |
137 /** | |
138 * Creates a new partitioner that uses the given scanner and may return | |
139 * partitions of the given legal content types. | |
140 * | |
141 * @param scanner the scanner this partitioner is supposed to use | |
142 * @param legalContentTypes the legal content types of this partitioner | |
143 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
144 public this(IPartitionTokenScanner scanner, String[] legalContentTypes) { |
129 | 145 fScanner= scanner; |
146 fLegalContentTypes= TextUtilities.copy(legalContentTypes); | |
162 | 147 fPositionCategory= CONTENT_TYPES_CATEGORY ~ Integer.toString(toHash()); |
129 | 148 fPositionUpdater= new DefaultPositionUpdater(fPositionCategory); |
149 } | |
150 | |
151 /* | |
152 * @see dwtx.jface.text.IDocumentPartitionerExtension2#getManagingPositionCategories() | |
153 */ | |
154 public String[] getManagingPositionCategories() { | |
150 | 155 return [ fPositionCategory ]; |
129 | 156 } |
157 | |
158 /* | |
159 * @see dwtx.jface.text.IDocumentPartitioner#connect(dwtx.jface.text.IDocument) | |
160 */ | |
161 public final void connect(IDocument document) { | |
162 connect(document, false); | |
163 } | |
164 | |
165 /** | |
166 * {@inheritDoc} | |
167 * <p> | |
168 * May be extended by subclasses. | |
169 * </p> | |
170 */ | |
171 public void connect(IDocument document, bool delayInitialization) { | |
162 | 172 Assert.isNotNull(cast(Object)document); |
129 | 173 Assert.isTrue(!document.containsPositionCategory(fPositionCategory)); |
174 | |
175 fDocument= document; | |
176 fDocument.addPositionCategory(fPositionCategory); | |
177 | |
178 fIsInitialized= false; | |
179 if (!delayInitialization) | |
180 checkInitialization(); | |
181 } | |
182 | |
183 /** | |
184 * Calls {@link #initialize()} if the receiver is not yet initialized. | |
185 */ | |
186 protected final void checkInitialization() { | |
187 if (!fIsInitialized) | |
188 initialize(); | |
189 } | |
190 | |
191 /** | |
192 * Performs the initial partitioning of the partitioner's document. | |
193 * <p> | |
194 * May be extended by subclasses. | |
195 * </p> | |
196 */ | |
197 protected void initialize() { | |
198 fIsInitialized= true; | |
199 clearPositionCache(); | |
200 fScanner.setRange(fDocument, 0, fDocument.getLength()); | |
201 | |
202 try { | |
203 IToken token= fScanner.nextToken(); | |
204 while (!token.isEOF()) { | |
205 | |
206 String contentType= getTokenContentType(token); | |
207 | |
208 if (isSupportedContentType(contentType)) { | |
209 TypedPosition p= new TypedPosition(fScanner.getTokenOffset(), fScanner.getTokenLength(), contentType); | |
210 fDocument.addPosition(fPositionCategory, p); | |
211 } | |
212 | |
213 token= fScanner.nextToken(); | |
214 } | |
215 } catch (BadLocationException x) { | |
216 // cannot happen as offsets come from scanner | |
217 } catch (BadPositionCategoryException x) { | |
218 // cannot happen if document has been connected before | |
219 } | |
220 } | |
221 | |
222 /** | |
223 * {@inheritDoc} | |
224 * <p> | |
225 * May be extended by subclasses. | |
226 * </p> | |
227 */ | |
228 public void disconnect() { | |
229 | |
230 Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory)); | |
231 | |
232 try { | |
233 fDocument.removePositionCategory(fPositionCategory); | |
234 } catch (BadPositionCategoryException x) { | |
235 // can not happen because of Assert | |
236 } | |
237 } | |
238 | |
239 /** | |
240 * {@inheritDoc} | |
241 * <p> | |
242 * May be extended by subclasses. | |
243 * </p> | |
244 */ | |
245 public void documentAboutToBeChanged(DocumentEvent e) { | |
246 if (fIsInitialized) { | |
247 | |
248 Assert.isTrue(e.getDocument() is fDocument); | |
249 | |
250 fPreviousDocumentLength= e.getDocument().getLength(); | |
251 fStartOffset= -1; | |
252 fEndOffset= -1; | |
253 fDeleteOffset= -1; | |
254 } | |
255 } | |
256 | |
257 /* | |
258 * @see IDocumentPartitioner#documentChanged(DocumentEvent) | |
259 */ | |
260 public final bool documentChanged(DocumentEvent e) { | |
261 if (fIsInitialized) { | |
262 IRegion region= documentChanged2(e); | |
263 return (region !is null); | |
264 } | |
265 return false; | |
266 } | |
267 | |
268 /** | |
269 * Helper method for tracking the minimal region containing all partition changes. | |
270 * If <code>offset</code> is smaller than the remembered offset, <code>offset</code> | |
271 * will from now on be remembered. If <code>offset + length</code> is greater than | |
272 * the remembered end offset, it will be remembered from now on. | |
273 * | |
274 * @param offset the offset | |
275 * @param length the length | |
276 */ | |
277 private void rememberRegion(int offset, int length) { | |
278 // remember start offset | |
279 if (fStartOffset is -1) | |
280 fStartOffset= offset; | |
281 else if (offset < fStartOffset) | |
282 fStartOffset= offset; | |
283 | |
284 // remember end offset | |
285 int endOffset= offset + length; | |
286 if (fEndOffset is -1) | |
287 fEndOffset= endOffset; | |
288 else if (endOffset > fEndOffset) | |
289 fEndOffset= endOffset; | |
290 } | |
291 | |
292 /** | |
293 * Remembers the given offset as the deletion offset. | |
294 * | |
295 * @param offset the offset | |
296 */ | |
297 private void rememberDeletedOffset(int offset) { | |
298 fDeleteOffset= offset; | |
299 } | |
300 | |
301 /** | |
302 * Creates the minimal region containing all partition changes using the | |
303 * remembered offset, end offset, and deletion offset. | |
304 * | |
305 * @return the minimal region containing all the partition changes | |
306 */ | |
307 private IRegion createRegion() { | |
308 if (fDeleteOffset is -1) { | |
309 if (fStartOffset is -1 || fEndOffset is -1) | |
310 return null; | |
311 return new Region(fStartOffset, fEndOffset - fStartOffset); | |
312 } else if (fStartOffset is -1 || fEndOffset is -1) { | |
313 return new Region(fDeleteOffset, 0); | |
314 } else { | |
315 int offset= Math.min(fDeleteOffset, fStartOffset); | |
316 int endOffset= Math.max(fDeleteOffset, fEndOffset); | |
317 return new Region(offset, endOffset - offset); | |
318 } | |
319 } | |
320 | |
321 /** | |
322 * {@inheritDoc} | |
323 * <p> | |
324 * May be extended by subclasses. | |
325 * </p> | |
326 */ | |
327 public IRegion documentChanged2(DocumentEvent e) { | |
328 | |
329 if (!fIsInitialized) | |
330 return null; | |
331 | |
332 try { | |
333 Assert.isTrue(e.getDocument() is fDocument); | |
334 | |
335 Position[] category= getPositions(); | |
336 IRegion line= fDocument.getLineInformationOfOffset(e.getOffset()); | |
337 int reparseStart= line.getOffset(); | |
338 int partitionStart= -1; | |
339 String contentType= null; | |
340 int newLength= e.getText() is null ? 0 : e.getText().length(); | |
341 | |
342 int first= fDocument.computeIndexInCategory(fPositionCategory, reparseStart); | |
343 if (first > 0) { | |
134 | 344 TypedPosition partition= cast(TypedPosition) category[first - 1]; |
129 | 345 if (partition.includes(reparseStart)) { |
346 partitionStart= partition.getOffset(); | |
347 contentType= partition.getType(); | |
348 if (e.getOffset() is partition.getOffset() + partition.getLength()) | |
349 reparseStart= partitionStart; | |
350 -- first; | |
351 } else if (reparseStart is e.getOffset() && reparseStart is partition.getOffset() + partition.getLength()) { | |
352 partitionStart= partition.getOffset(); | |
353 contentType= partition.getType(); | |
354 reparseStart= partitionStart; | |
355 -- first; | |
356 } else { | |
357 partitionStart= partition.getOffset() + partition.getLength(); | |
358 contentType= IDocument.DEFAULT_CONTENT_TYPE; | |
359 } | |
360 } | |
361 | |
362 fPositionUpdater.update(e); | |
363 for (int i= first; i < category.length; i++) { | |
364 Position p= category[i]; | |
365 if (p.isDeleted) { | |
366 rememberDeletedOffset(e.getOffset()); | |
367 break; | |
368 } | |
369 } | |
370 clearPositionCache(); | |
371 category= getPositions(); | |
372 | |
373 fScanner.setPartialRange(fDocument, reparseStart, fDocument.getLength() - reparseStart, contentType, partitionStart); | |
374 | |
375 int behindLastScannedPosition= reparseStart; | |
376 IToken token= fScanner.nextToken(); | |
377 | |
378 while (!token.isEOF()) { | |
379 | |
380 contentType= getTokenContentType(token); | |
381 | |
382 if (!isSupportedContentType(contentType)) { | |
383 token= fScanner.nextToken(); | |
384 continue; | |
385 } | |
386 | |
387 int start= fScanner.getTokenOffset(); | |
388 int length= fScanner.getTokenLength(); | |
389 | |
390 behindLastScannedPosition= start + length; | |
391 int lastScannedPosition= behindLastScannedPosition - 1; | |
392 | |
393 // remove all affected positions | |
394 while (first < category.length) { | |
134 | 395 TypedPosition p= cast(TypedPosition) category[first]; |
129 | 396 if (lastScannedPosition >= p.offset + p.length || |
397 (p.overlapsWith(start, length) && | |
398 (!fDocument.containsPosition(fPositionCategory, start, length) || | |
399 !contentType.equals(p.getType())))) { | |
400 | |
401 rememberRegion(p.offset, p.length); | |
402 fDocument.removePosition(fPositionCategory, p); | |
403 ++ first; | |
404 | |
405 } else | |
406 break; | |
407 } | |
408 | |
409 // if position already exists and we have scanned at least the | |
410 // area covered by the event, we are done | |
411 if (fDocument.containsPosition(fPositionCategory, start, length)) { | |
412 if (lastScannedPosition >= e.getOffset() + newLength) | |
413 return createRegion(); | |
414 ++ first; | |
415 } else { | |
416 // insert the new type position | |
417 try { | |
418 fDocument.addPosition(fPositionCategory, new TypedPosition(start, length, contentType)); | |
419 rememberRegion(start, length); | |
420 } catch (BadPositionCategoryException x) { | |
421 } catch (BadLocationException x) { | |
422 } | |
423 } | |
424 | |
425 token= fScanner.nextToken(); | |
426 } | |
427 | |
428 first= fDocument.computeIndexInCategory(fPositionCategory, behindLastScannedPosition); | |
429 | |
430 clearPositionCache(); | |
431 category= getPositions(); | |
432 TypedPosition p; | |
433 while (first < category.length) { | |
134 | 434 p= cast(TypedPosition) category[first++]; |
129 | 435 fDocument.removePosition(fPositionCategory, p); |
436 rememberRegion(p.offset, p.length); | |
437 } | |
438 | |
439 } catch (BadPositionCategoryException x) { | |
440 // should never happen on connected documents | |
441 } catch (BadLocationException x) { | |
442 } finally { | |
443 clearPositionCache(); | |
444 } | |
445 | |
446 return createRegion(); | |
447 } | |
448 | |
449 /** | |
450 * Returns the position in the partitoner's position category which is | |
451 * close to the given offset. This is, the position has either an offset which | |
452 * is the same as the given offset or an offset which is smaller than the given | |
453 * offset. This method profits from the knowledge that a partitioning is | |
454 * a ordered set of disjoint position. | |
455 * <p> | |
456 * May be extended or replaced by subclasses. | |
457 * </p> | |
458 * @param offset the offset for which to search the closest position | |
459 * @return the closest position in the partitioner's category | |
460 */ | |
461 protected TypedPosition findClosestPosition(int offset) { | |
462 | |
463 try { | |
464 | |
465 int index= fDocument.computeIndexInCategory(fPositionCategory, offset); | |
466 Position[] category= getPositions(); | |
467 | |
468 if (category.length is 0) | |
469 return null; | |
470 | |
471 if (index < category.length) { | |
472 if (offset is category[index].offset) | |
134 | 473 return cast(TypedPosition) category[index]; |
129 | 474 } |
475 | |
476 if (index > 0) | |
477 index--; | |
478 | |
134 | 479 return cast(TypedPosition) category[index]; |
129 | 480 |
481 } catch (BadPositionCategoryException x) { | |
482 } catch (BadLocationException x) { | |
483 } | |
484 | |
485 return null; | |
486 } | |
487 | |
488 | |
489 /** | |
490 * {@inheritDoc} | |
491 * <p> | |
492 * May be replaced or extended by subclasses. | |
493 * </p> | |
494 */ | |
495 public String getContentType(int offset) { | |
496 checkInitialization(); | |
497 | |
498 TypedPosition p= findClosestPosition(offset); | |
499 if (p !is null && p.includes(offset)) | |
500 return p.getType(); | |
501 | |
502 return IDocument.DEFAULT_CONTENT_TYPE; | |
503 } | |
504 | |
505 /** | |
506 * {@inheritDoc} | |
507 * <p> | |
508 * May be replaced or extended by subclasses. | |
509 * </p> | |
510 */ | |
511 public ITypedRegion getPartition(int offset) { | |
512 checkInitialization(); | |
513 | |
514 try { | |
515 | |
516 Position[] category = getPositions(); | |
517 | |
518 if (category is null || category.length is 0) | |
519 return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE); | |
520 | |
521 int index= fDocument.computeIndexInCategory(fPositionCategory, offset); | |
522 | |
523 if (index < category.length) { | |
524 | |
134 | 525 TypedPosition next= cast(TypedPosition) category[index]; |
129 | 526 |
527 if (offset is next.offset) | |
528 return new TypedRegion(next.getOffset(), next.getLength(), next.getType()); | |
529 | |
530 if (index is 0) | |
531 return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE); | |
532 | |
134 | 533 TypedPosition previous= cast(TypedPosition) category[index - 1]; |
129 | 534 if (previous.includes(offset)) |
535 return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType()); | |
536 | |
537 int endOffset= previous.getOffset() + previous.getLength(); | |
538 return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE); | |
539 } | |
540 | |
134 | 541 TypedPosition previous= cast(TypedPosition) category[category.length - 1]; |
129 | 542 if (previous.includes(offset)) |
543 return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType()); | |
544 | |
545 int endOffset= previous.getOffset() + previous.getLength(); | |
546 return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE); | |
547 | |
548 } catch (BadPositionCategoryException x) { | |
549 } catch (BadLocationException x) { | |
550 } | |
551 | |
552 return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE); | |
553 } | |
554 | |
555 /* | |
556 * @see IDocumentPartitioner#computePartitioning(int, int) | |
557 */ | |
558 public final ITypedRegion[] computePartitioning(int offset, int length) { | |
559 return computePartitioning(offset, length, false); | |
560 } | |
561 | |
562 /** | |
563 * {@inheritDoc} | |
564 * <p> | |
565 * May be replaced or extended by subclasses. | |
566 * </p> | |
567 */ | |
568 public String[] getLegalContentTypes() { | |
569 return TextUtilities.copy(fLegalContentTypes); | |
570 } | |
571 | |
572 /** | |
573 * Returns whether the given type is one of the legal content types. | |
574 * <p> | |
575 * May be extended by subclasses. | |
576 * </p> | |
577 * | |
578 * @param contentType the content type to check | |
579 * @return <code>true</code> if the content type is a legal content type | |
580 */ | |
581 protected bool isSupportedContentType(String contentType) { | |
582 if (contentType !is null) { | |
583 for (int i= 0; i < fLegalContentTypes.length; i++) { | |
584 if (fLegalContentTypes[i].equals(contentType)) | |
585 return true; | |
586 } | |
587 } | |
588 | |
589 return false; | |
590 } | |
591 | |
592 /** | |
593 * Returns a content type encoded in the given token. If the token's | |
594 * data is not <code>null</code> and a string it is assumed that | |
595 * it is the encoded content type. | |
596 * <p> | |
597 * May be replaced or extended by subclasses. | |
598 * </p> | |
599 * | |
600 * @param token the token whose content type is to be determined | |
601 * @return the token's content type | |
602 */ | |
603 protected String getTokenContentType(IToken token) { | |
604 Object data= token.getData(); | |
162 | 605 if ( auto str = cast(ArrayWrapperString)data ) |
606 return str.array; | |
129 | 607 return null; |
608 } | |
609 | |
610 /* zero-length partition support */ | |
611 | |
612 /** | |
613 * {@inheritDoc} | |
614 * <p> | |
615 * May be replaced or extended by subclasses. | |
616 * </p> | |
617 */ | |
618 public String getContentType(int offset, bool preferOpenPartitions) { | |
619 return getPartition(offset, preferOpenPartitions).getType(); | |
620 } | |
621 | |
622 /** | |
623 * {@inheritDoc} | |
624 * <p> | |
625 * May be replaced or extended by subclasses. | |
626 * </p> | |
627 */ | |
628 public ITypedRegion getPartition(int offset, bool preferOpenPartitions) { | |
629 ITypedRegion region= getPartition(offset); | |
630 if (preferOpenPartitions) { | |
631 if (region.getOffset() is offset && !region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) { | |
632 if (offset > 0) { | |
633 region= getPartition(offset - 1); | |
634 if (region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) | |
635 return region; | |
636 } | |
637 return new TypedRegion(offset, 0, IDocument.DEFAULT_CONTENT_TYPE); | |
638 } | |
639 } | |
640 return region; | |
641 } | |
642 | |
643 /** | |
644 * {@inheritDoc} | |
645 * <p> | |
646 * May be replaced or extended by subclasses. | |
647 * </p> | |
648 */ | |
649 public ITypedRegion[] computePartitioning(int offset, int length, bool includeZeroLengthPartitions) { | |
650 checkInitialization(); | |
651 List list= new ArrayList(); | |
652 | |
653 try { | |
654 | |
655 int endOffset= offset + length; | |
656 | |
657 Position[] category= getPositions(); | |
658 | |
659 TypedPosition previous= null, current= null; | |
660 int start, end, gapOffset; | |
661 Position gap= new Position(0); | |
662 | |
663 int startIndex= getFirstIndexEndingAfterOffset(category, offset); | |
664 int endIndex= getFirstIndexStartingAfterOffset(category, endOffset); | |
665 for (int i= startIndex; i < endIndex; i++) { | |
666 | |
134 | 667 current= cast(TypedPosition) category[i]; |
129 | 668 |
669 gapOffset= (previous !is null) ? previous.getOffset() + previous.getLength() : 0; | |
670 gap.setOffset(gapOffset); | |
671 gap.setLength(current.getOffset() - gapOffset); | |
672 if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || | |
673 (gap.getLength() > 0 && gap.overlapsWith(offset, length))) { | |
674 start= Math.max(offset, gapOffset); | |
675 end= Math.min(endOffset, gap.getOffset() + gap.getLength()); | |
676 list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE)); | |
677 } | |
678 | |
679 if (current.overlapsWith(offset, length)) { | |
680 start= Math.max(offset, current.getOffset()); | |
681 end= Math.min(endOffset, current.getOffset() + current.getLength()); | |
682 list.add(new TypedRegion(start, end - start, current.getType())); | |
683 } | |
684 | |
685 previous= current; | |
686 } | |
687 | |
688 if (previous !is null) { | |
689 gapOffset= previous.getOffset() + previous.getLength(); | |
690 gap.setOffset(gapOffset); | |
691 gap.setLength(fDocument.getLength() - gapOffset); | |
692 if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || | |
693 (gap.getLength() > 0 && gap.overlapsWith(offset, length))) { | |
694 start= Math.max(offset, gapOffset); | |
695 end= Math.min(endOffset, fDocument.getLength()); | |
696 list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE)); | |
697 } | |
698 } | |
699 | |
700 if (list.isEmpty()) | |
701 list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE)); | |
702 | |
703 } catch (BadPositionCategoryException ex) { | |
704 // Make sure we clear the cache | |
705 clearPositionCache(); | |
706 } catch (RuntimeException ex) { | |
707 // Make sure we clear the cache | |
708 clearPositionCache(); | |
709 throw ex; | |
710 } | |
711 | |
162 | 712 return arraycast!(ITypedRegion)(list.toArray()); |
129 | 713 } |
714 | |
715 /** | |
716 * Returns <code>true</code> if the given ranges overlap with or touch each other. | |
717 * | |
718 * @param gap the first range | |
719 * @param offset the offset of the second range | |
720 * @param length the length of the second range | |
721 * @return <code>true</code> if the given ranges overlap with or touch each other | |
722 */ | |
723 private bool overlapsOrTouches(Position gap, int offset, int length) { | |
724 return gap.getOffset() <= offset + length && offset <= gap.getOffset() + gap.getLength(); | |
725 } | |
726 | |
727 /** | |
728 * Returns the index of the first position which ends after the given offset. | |
729 * | |
730 * @param positions the positions in linear order | |
731 * @param offset the offset | |
732 * @return the index of the first position which ends after the offset | |
733 */ | |
734 private int getFirstIndexEndingAfterOffset(Position[] positions, int offset) { | |
735 int i= -1, j= positions.length; | |
736 while (j - i > 1) { | |
737 int k= (i + j) >> 1; | |
738 Position p= positions[k]; | |
739 if (p.getOffset() + p.getLength() > offset) | |
740 j= k; | |
741 else | |
742 i= k; | |
743 } | |
744 return j; | |
745 } | |
746 | |
747 /** | |
748 * Returns the index of the first position which starts at or after the given offset. | |
749 * | |
750 * @param positions the positions in linear order | |
751 * @param offset the offset | |
752 * @return the index of the first position which starts after the offset | |
753 */ | |
754 private int getFirstIndexStartingAfterOffset(Position[] positions, int offset) { | |
755 int i= -1, j= positions.length; | |
756 while (j - i > 1) { | |
757 int k= (i + j) >> 1; | |
758 Position p= positions[k]; | |
759 if (p.getOffset() >= offset) | |
760 j= k; | |
761 else | |
762 i= k; | |
763 } | |
764 return j; | |
765 } | |
766 | |
767 /* | |
768 * @see dwtx.jface.text.IDocumentPartitionerExtension3#startRewriteSession(dwtx.jface.text.DocumentRewriteSession) | |
769 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
770 public void startRewriteSession(DocumentRewriteSession session) { |
129 | 771 if (fActiveRewriteSession !is null) |
772 throw new IllegalStateException(); | |
773 fActiveRewriteSession= session; | |
774 } | |
775 | |
776 /** | |
777 * {@inheritDoc} | |
778 * <p> | |
779 * May be extended by subclasses. | |
780 * </p> | |
781 */ | |
782 public void stopRewriteSession(DocumentRewriteSession session) { | |
783 if (fActiveRewriteSession is session) | |
784 flushRewriteSession(); | |
785 } | |
786 | |
787 /** | |
788 * {@inheritDoc} | |
789 * <p> | |
790 * May be extended by subclasses. | |
791 * </p> | |
792 */ | |
793 public DocumentRewriteSession getActiveRewriteSession() { | |
794 return fActiveRewriteSession; | |
795 } | |
796 | |
797 /** | |
798 * Flushes the active rewrite session. | |
799 */ | |
800 protected final void flushRewriteSession() { | |
801 fActiveRewriteSession= null; | |
802 | |
803 // remove all position belonging to the partitioner position category | |
804 try { | |
805 fDocument.removePositionCategory(fPositionCategory); | |
806 } catch (BadPositionCategoryException x) { | |
807 } | |
808 fDocument.addPositionCategory(fPositionCategory); | |
809 | |
810 fIsInitialized= false; | |
811 } | |
812 | |
813 /** | |
814 * Clears the position cache. Needs to be called whenever the positions have | |
815 * been updated. | |
816 */ | |
817 protected final void clearPositionCache() { | |
818 if (fCachedPositions !is null) { | |
819 fCachedPositions= null; | |
820 } | |
821 } | |
822 | |
823 /** | |
824 * Returns the partitioners positions. | |
825 * | |
826 * @return the partitioners positions | |
827 * @throws BadPositionCategoryException if getting the positions from the | |
828 * document fails | |
829 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
830 protected final Position[] getPositions() { |
129 | 831 if (fCachedPositions is null) { |
832 fCachedPositions= fDocument.getPositions(fPositionCategory); | |
150 | 833 } else if (CHECK_CACHE_CONSISTENCY) { |
129 | 834 Position[] positions= fDocument.getPositions(fPositionCategory); |
835 int len= Math.min(positions.length, fCachedPositions.length); | |
836 for (int i= 0; i < len; i++) { | |
162 | 837 if (!positions[i].opEquals(fCachedPositions[i])) |
838 System.err.println(Format("FastPartitioner.getPositions(): cached position is not up to date: from document: {} in cache: {}", toString(positions[i]), toString(fCachedPositions[i]))); //$NON-NLS-1$ //$NON-NLS-2$ | |
129 | 839 } |
840 for (int i= len; i < positions.length; i++) | |
162 | 841 System.err.println(Format("FastPartitioner.getPositions(): new position in document: {}", toString(positions[i]))); //$NON-NLS-1$ |
129 | 842 for (int i= len; i < fCachedPositions.length; i++) |
162 | 843 System.err.println(Format("FastPartitioner.getPositions(): stale position in cache: {}", toString(fCachedPositions[i]))); //$NON-NLS-1$ |
129 | 844 } |
845 return fCachedPositions; | |
846 } | |
847 | |
848 /** | |
849 * Pretty print a <code>Position</code>. | |
850 * | |
851 * @param position the position to format | |
852 * @return a formatted string | |
853 */ | |
160 | 854 private override String toString(Position position) { |
162 | 855 return Format("P[{}+{}]", position.getOffset(), position.getLength() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
129 | 856 } |
857 } |