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