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