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