Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKai Maetzel2003-08-08 15:33:45 +0000
committerKai Maetzel2003-08-08 15:33:45 +0000
commit8924e74ea190de9fb127f75fe57a1f56f6b360b5 (patch)
tree9956ef212e159e4617a7c83389021c1445a74d19
parentef0d68e9286050230f5ec76573afa3774aaffd19 (diff)
downloadeclipse.platform.text-8924e74ea190de9fb127f75fe57a1f56f6b360b5.tar.gz
eclipse.platform.text-8924e74ea190de9fb127f75fe57a1f56f6b360b5.tar.xz
eclipse.platform.text-8924e74ea190de9fb127f75fe57a1f56f6b360b5.zip
support for multiple partitions per document
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/ContentFormatter.java167
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/rules/DefaultPartitioner.java66
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/rules/RuleBasedPartitioner.java63
3 files changed, 182 insertions, 114 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/ContentFormatter.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/ContentFormatter.java
index 03e5cd873db..0f354260aaa 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/ContentFormatter.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/ContentFormatter.java
@@ -274,7 +274,21 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* @since 3.0
*/
private String fPartitioning;
-
+ /**
+ * The document this formatter works on.
+ * @since 3.0
+ */
+ private IDocument fDocument;
+ /**
+ * The external partition managing categories.
+ * @since 3.0
+ */
+ private String[] fExternalPartitonManagingCategories;
+ /**
+ * Indicates whether <code>fPartitionManagingCategories</code> must be computed.
+ * @since 3.0
+ */
+ private boolean fNeedsComputation= true;
/**
@@ -314,9 +328,11 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* be ignored when this formatter updates positions.
*
* @param categories the categories to be ignored
+ * @deprecated incompatible with an open set of document partitionings. The provided information is only used
+ * if this formatter can not compute the partition managing position categories.
*/
public void setPartitionManagingPositionCategories(String[] categories) {
- fPartitionManagingCategories= categories;
+ fExternalPartitonManagingCategories= categories;
}
/**
@@ -363,10 +379,19 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* @see IContentFormatter#format(IDocument, IRegion)
*/
public void format(IDocument document, IRegion region) {
- if (fIsPartitionAware)
- formatPartitions(document, region);
- else
- formatRegion(document, region);
+ fNeedsComputation= true;
+ fDocument= document;
+ try {
+
+ if (fIsPartitionAware)
+ formatPartitions(region);
+ else
+ formatRegion(region);
+
+ } finally {
+ fNeedsComputation= true;
+ fDocument= null;
+ }
}
/**
@@ -374,26 +399,25 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* Informs the formatting strategies of each partition about the start,
* the process, and the termination of the formatting session.
*
- * @param document the document to be formatted
* @param region the document region to be formatted
*/
- private void formatPartitions(IDocument document, IRegion region) {
+ private void formatPartitions(IRegion region) {
- addPartitioningUpdater(document);
+ addPartitioningUpdater();
try {
- TypedPosition[] ranges= getPartitioning(document, region);
+ TypedPosition[] ranges= getPartitioning(region);
if (ranges != null) {
- start(ranges, getIndentation(document, region.getOffset()));
- format(document, ranges);
+ start(ranges, getIndentation(region.getOffset()));
+ format(ranges);
stop(ranges);
}
} catch (BadLocationException x) {
}
- removePartitioningUpdater(document);
+ removePartitioningUpdater();
}
/**
@@ -401,15 +425,14 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* content type. The strategy is informed about the start, the process, and
* the termination of the formatting session.
*
- * @param document the document to be formatted
* @param region the region to be formatted
*/
- private void formatRegion(IDocument document, IRegion region) {
+ private void formatRegion(IRegion region) {
IFormattingStrategy strategy= getFormattingStrategy(IDocument.DEFAULT_CONTENT_TYPE);
if (strategy != null) {
- strategy.formatterStarts(getIndentation(document, region.getOffset()));
- format(document, strategy, new TypedPosition(region.getOffset(), region.getLength(), IDocument.DEFAULT_CONTENT_TYPE));
+ strategy.formatterStarts(getIndentation(region.getOffset()));
+ format(strategy, new TypedPosition(region.getOffset(), region.getLength(), IDocument.DEFAULT_CONTENT_TYPE));
strategy.formatterStops();
}
}
@@ -423,20 +446,19 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* in a dedicated position category. (As formatting stratgies might rely on each
* other, calling them in reversed order is not an option.)
*
- * @param document the document
* @param region the region for which the partitioning must be determined
* @return the partitioning of the specified region
* @exception BadLocationException of region is invalid in the document
*/
- private TypedPosition[] getPartitioning(IDocument document, IRegion region) throws BadLocationException {
+ private TypedPosition[] getPartitioning(IRegion region) throws BadLocationException {
- ITypedRegion[] regions= TextUtilities.computePartitioning(document, fPartitioning, region.getOffset(), region.getLength());
+ ITypedRegion[] regions= TextUtilities.computePartitioning(fDocument, fPartitioning, region.getOffset(), region.getLength());
TypedPosition[] positions= new TypedPosition[regions.length];
for (int i= 0; i < regions.length; i++) {
positions[i]= new TypedPosition(regions[i]);
try {
- document.addPosition(PARTITIONING, positions[i]);
+ fDocument.addPosition(PARTITIONING, positions[i]);
} catch (BadPositionCategoryException x) {
// should not happen
}
@@ -464,14 +486,13 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* Formats one partition after the other using the formatter strategy registered for
* the partition's content type.
*
- * @param document to document to be formatted
* @param ranges the partitioning of the document region to be formatted
*/
- private void format(final IDocument document, TypedPosition[] ranges) {
+ private void format(TypedPosition[] ranges) {
for (int i= 0; i < ranges.length; i++) {
IFormattingStrategy s= getFormattingStrategy(ranges[i].getType());
if (s != null) {
- format(document, s, ranges[i]);
+ format(s, ranges[i]);
}
}
}
@@ -486,31 +507,30 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* their categories, right before the first document listener is informed about
* that a change happend.
*
- * @param document the document to be formatted
* @param strategy the strategy to be used
* @param region the region to be formatted
*/
- private void format(final IDocument document, IFormattingStrategy strategy, TypedPosition region) {
+ private void format(IFormattingStrategy strategy, TypedPosition region) {
try {
final int offset= region.getOffset();
int length= region.getLength();
- String content= document.get(offset, length);
- final int[] positions= getAffectedPositions(document, offset, length);
- String formatted= strategy.format(content, isLineStart(document, offset), getIndentation(document, offset), positions);
+ String content= fDocument.get(offset, length);
+ final int[] positions= getAffectedPositions(offset, length);
+ String formatted= strategy.format(content, isLineStart(offset), getIndentation(offset), positions);
if (formatted != null && !formatted.equals(content)) {
IPositionUpdater first= new RemoveAffectedPositions();
- document.insertPositionUpdater(first, 0);
+ fDocument.insertPositionUpdater(first, 0);
IPositionUpdater last= new UpdateAffectedPositions(positions, offset);
- document.addPositionUpdater(last);
+ fDocument.addPositionUpdater(last);
- document.replace(offset, length, formatted);
+ fDocument.replace(offset, length, formatted);
- document.removePositionUpdater(first);
- document.removePositionUpdater(last);
+ fDocument.removePositionUpdater(first);
+ fDocument.removePositionUpdater(last);
}
} catch (BadLocationException x) {
@@ -533,28 +553,23 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
}
/**
- * Installs those updaters which the formatter needs to keep
- * track of the partitions.
- *
- * @param document the document to be formatted
+ * Installs those updaters which the formatter needs to keep track of the partitions.
*/
- private void addPartitioningUpdater(IDocument document) {
+ private void addPartitioningUpdater() {
fPartitioningUpdater= new NonDeletingPositionUpdater(PARTITIONING);
- document.addPositionCategory(PARTITIONING);
- document.addPositionUpdater(fPartitioningUpdater);
+ fDocument.addPositionCategory(PARTITIONING);
+ fDocument.addPositionUpdater(fPartitioningUpdater);
}
/**
* Removes the formatter's internal position updater and category.
- *
- * @param document the document that has been formatted
*/
- private void removePartitioningUpdater(IDocument document) {
+ private void removePartitioningUpdater() {
try {
- document.removePositionUpdater(fPartitioningUpdater);
- document.removePositionCategory(PARTITIONING);
+ fDocument.removePositionUpdater(fPartitioningUpdater);
+ fDocument.removePositionCategory(PARTITIONING);
fPartitioningUpdater= null;
} catch (BadPositionCategoryException x) {
@@ -563,6 +578,22 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
}
/**
+ * Returns the partition managing position categories for the formatted document.
+ *
+ * @return the position managing position categories
+ * @since 3.0
+ */
+ private String[] getPartitionManagingCategories() {
+ if (fNeedsComputation) {
+ fNeedsComputation= false;
+ fPartitionManagingCategories= TextUtilities.computePartitionManagingCategories(fDocument);
+ if (fPartitionManagingCategories == null)
+ fPartitionManagingCategories= fExternalPartitonManagingCategories;
+ }
+ return fPartitionManagingCategories;
+ }
+
+ /**
* Determines whether the given document position category should be ignored
* by this formatter's position updating.
*
@@ -573,10 +604,11 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
if (PARTITIONING.equals(category))
return true;
-
- if (fPartitionManagingCategories != null) {
- for (int i= 0; i < fPartitionManagingCategories.length; i++) {
- if (fPartitionManagingCategories[i].equals(category))
+
+ String[] categories= getPartitionManagingCategories();
+ if (categories != null) {
+ for (int i= 0; i < categories.length; i++) {
+ if (categories[i].equals(category))
return true;
}
}
@@ -588,13 +620,12 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* Determines all embracing, overlapping, and follow up positions
* for the given region of the document.
*
- * @param document the document to be formatted
* @param offset the offset of the document region to be formatted
* @param length the length of the document to be formatted
*/
- private void determinePositionsToUpdate(IDocument document, int offset, int length) {
+ private void determinePositionsToUpdate(int offset, int length) {
- String[] categories= document.getPositionCategories();
+ String[] categories= fDocument.getPositionCategories();
if (categories != null) {
for (int i= 0; i < categories.length; i++) {
@@ -603,7 +634,7 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
try {
- Position[] positions= document.getPositions(categories[i]);
+ Position[] positions= fDocument.getPositions(categories[i]);
for (int j= 0; j < positions.length; j++) {
@@ -629,16 +660,15 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
* Returns all offset and the end offset of all positions overlapping with the
* specified document range.
*
- * @param document the document to be formatted
* @param offset the offset of the document region to be formatted
* @param length the length of the document to be formatted
* @return all character positions of the interleaving positions
*/
- private int[] getAffectedPositions(IDocument document, int offset, int length) {
+ private int[] getAffectedPositions(int offset, int length) {
fOverlappingPositionReferences= new ArrayList();
- determinePositionsToUpdate(document, offset, length);
+ determinePositionsToUpdate(offset, length);
Collections.sort(fOverlappingPositionReferences);
@@ -679,6 +709,9 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
*/
protected void updateAffectedPositions(IDocument document, int[] positions, int offset) {
+ if (document != fDocument)
+ return;
+
if (positions.length == 0)
return;
@@ -740,22 +773,21 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
/**
* Returns the indentation of the line of the given offset.
*
- * @param document the document
* @param offset the offset
* @return the indentation of the line of the offset
*/
- private String getIndentation(IDocument document, int offset) {
+ private String getIndentation(int offset) {
try {
- int start= document.getLineOfOffset(offset);
- start= document.getLineOffset(start);
+ int start= fDocument.getLineOfOffset(offset);
+ start= fDocument.getLineOffset(start);
int end= start;
- char c= document.getChar(end);
+ char c= fDocument.getChar(end);
while ('\t' == c || ' ' == c)
- c= document.getChar(++end);
+ c= fDocument.getChar(++end);
- return document.get(start, end - start);
+ return fDocument.get(start, end - start);
} catch (BadLocationException x) {
}
@@ -765,14 +797,13 @@ public class ContentFormatter implements IContentFormatter, IContentFormatterExt
/**
* Determines whether the offset is the beginning of a line in the given document.
*
- * @param document the document
* @param offset the offset
* @return <code>true</code> if offset is the beginning of a line
* @exception BadLocationException if offset is invalid in document
*/
- private boolean isLineStart(IDocument document, int offset) throws BadLocationException {
- int start= document.getLineOfOffset(offset);
- start= document.getLineOffset(start);
+ private boolean isLineStart(int offset) throws BadLocationException {
+ int start= fDocument.getLineOfOffset(offset);
+ start= fDocument.getLineOffset(start);
return (start == offset);
}
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/DefaultPartitioner.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/DefaultPartitioner.java
index 9c08f72bbcc..03335e4d4de 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/DefaultPartitioner.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/DefaultPartitioner.java
@@ -23,6 +23,7 @@ import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.IDocumentPartitionerExtension;
+import org.eclipse.jface.text.IDocumentPartitionerExtension2;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Position;
@@ -43,9 +44,12 @@ import org.eclipse.jface.text.TypedRegion;
* @see IPartitionTokenScanner
* @since 2.0
*/
-public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartitionerExtension {
+public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartitionerExtension, IDocumentPartitionerExtension2 {
- /** The position category this partitioner uses to store the document's partitioning information */
+ /**
+ * The position category this partitioner uses to store the document's partitioning information.
+ * @deprecated use <code>getManagingPositionCategories()</code>.
+ */
public final static String CONTENT_TYPES_CATEGORY= "__content_types_category"; //$NON-NLS-1$
@@ -58,14 +62,18 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
/** The document length before a document change occured */
protected int fPreviousDocumentLength;
/** The position updater used to for the default updating of partitions */
- protected DefaultPositionUpdater fPositionUpdater= new DefaultPositionUpdater(CONTENT_TYPES_CATEGORY);
+ protected DefaultPositionUpdater fPositionUpdater;
/** The offset at which the first changed partition starts */
protected int fStartOffset;
/** The offset at which the last changed partition ends */
protected int fEndOffset;
/**The offset at which a partition has been deleted */
protected int fDeleteOffset;
-
+ /**
+ * The position category this partitioner uses to store the document's partitioning information.
+ * @since 3.0
+ */
+ private String fPositionCategory;
/**
* Creates a new partitioner that uses the given scanner and may return
@@ -77,17 +85,27 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
public DefaultPartitioner(IPartitionTokenScanner scanner, String[] legalContentTypes) {
fScanner= scanner;
fLegalContentTypes= legalContentTypes;
+ fPositionCategory= CONTENT_TYPES_CATEGORY + hashCode();
+ fPositionUpdater= new DefaultPositionUpdater(fPositionCategory);
}
-
+
+ /*
+ * @see org.eclipse.jface.text.IDocumentPartitionerExtension2#getManagingPositionCategories()
+ * @since 3.0
+ */
+ public String[] getManagingPositionCategories() {
+ return new String[] { fPositionCategory };
+ }
+
/*
* @see IDocumentPartitioner#connect(IDocument)
*/
public void connect(IDocument document) {
Assert.isNotNull(document);
- Assert.isTrue(!document.containsPositionCategory(CONTENT_TYPES_CATEGORY));
+ Assert.isTrue(!document.containsPositionCategory(fPositionCategory));
fDocument= document;
- fDocument.addPositionCategory(CONTENT_TYPES_CATEGORY);
+ fDocument.addPositionCategory(fPositionCategory);
initialize();
}
@@ -107,7 +125,7 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
if (isSupportedContentType(contentType)) {
TypedPosition p= new TypedPosition(fScanner.getTokenOffset(), fScanner.getTokenLength(), contentType);
- fDocument.addPosition(CONTENT_TYPES_CATEGORY, p);
+ fDocument.addPosition(fPositionCategory, p);
}
token= fScanner.nextToken();
@@ -124,10 +142,10 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
*/
public void disconnect() {
- Assert.isTrue(fDocument.containsPositionCategory(CONTENT_TYPES_CATEGORY));
+ Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory));
try {
- fDocument.removePositionCategory(CONTENT_TYPES_CATEGORY);
+ fDocument.removePositionCategory(fPositionCategory);
} catch (BadPositionCategoryException x) {
// can not happen because of Assert
}
@@ -216,13 +234,13 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
try {
IDocument d= e.getDocument();
- Position[] category= d.getPositions(CONTENT_TYPES_CATEGORY);
+ Position[] category= d.getPositions(fPositionCategory);
IRegion line= d.getLineInformationOfOffset(e.getOffset());
int reparseStart= line.getOffset();
int partitionStart= -1;
String contentType= null;
- int first= d.computeIndexInCategory(CONTENT_TYPES_CATEGORY, reparseStart);
+ int first= d.computeIndexInCategory(fPositionCategory, reparseStart);
if (first > 0) {
TypedPosition partition= (TypedPosition) category[first - 1];
if (partition.includes(reparseStart)) {
@@ -250,7 +268,7 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
break;
}
}
- category= d.getPositions(CONTENT_TYPES_CATEGORY);
+ category= d.getPositions(fPositionCategory);
fScanner.setPartialRange(d, reparseStart, d.getLength() - reparseStart, contentType, partitionStart);
@@ -276,11 +294,11 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
TypedPosition p= (TypedPosition) category[first];
if (lastScannedPosition >= p.offset + p.length ||
(p.overlapsWith(start, length) &&
- (!d.containsPosition(CONTENT_TYPES_CATEGORY, start, length) ||
+ (!d.containsPosition(fPositionCategory, start, length) ||
!contentType.equals(p.getType())))) {
rememberRegion(p.offset, p.length);
- d.removePosition(CONTENT_TYPES_CATEGORY, p);
+ d.removePosition(fPositionCategory, p);
++ first;
} else
@@ -288,14 +306,14 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
}
// if position already exists we are done
- if (d.containsPosition(CONTENT_TYPES_CATEGORY, start, length)) {
+ if (d.containsPosition(fPositionCategory, start, length)) {
if (lastScannedPosition > e.getOffset())
return createRegion();
++ first;
} else {
// insert the new type position
try {
- d.addPosition(CONTENT_TYPES_CATEGORY, new TypedPosition(start, length, contentType));
+ d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
rememberRegion(start, length);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
@@ -311,12 +329,12 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
// if this condition is not met, nothing has been scanned because of a deletion
++ lastScannedPosition;
}
- first= d.computeIndexInCategory(CONTENT_TYPES_CATEGORY, lastScannedPosition);
+ first= d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
TypedPosition p;
while (first < category.length) {
p= (TypedPosition) category[first++];
- d.removePosition(CONTENT_TYPES_CATEGORY, p);
+ d.removePosition(fPositionCategory, p);
rememberRegion(p.offset, p.length);
}
@@ -343,8 +361,8 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
try {
- int index= fDocument.computeIndexInCategory(CONTENT_TYPES_CATEGORY, offset);
- Position[] category= fDocument.getPositions(CONTENT_TYPES_CATEGORY);
+ int index= fDocument.computeIndexInCategory(fPositionCategory, offset);
+ Position[] category= fDocument.getPositions(fPositionCategory);
if (category.length == 0)
return null;
@@ -386,12 +404,12 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
try {
- Position[] category = fDocument.getPositions(CONTENT_TYPES_CATEGORY);
+ Position[] category = fDocument.getPositions(fPositionCategory);
if (category == null || category.length == 0)
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
- int index= fDocument.computeIndexInCategory(CONTENT_TYPES_CATEGORY, offset);
+ int index= fDocument.computeIndexInCategory(fPositionCategory, offset);
if (index < category.length) {
@@ -436,7 +454,7 @@ public class DefaultPartitioner implements IDocumentPartitioner, IDocumentPartit
int endOffset= offset + length;
- Position[] category= fDocument.getPositions(CONTENT_TYPES_CATEGORY);
+ Position[] category= fDocument.getPositions(fPositionCategory);
TypedPosition previous= null, current= null;
int start, end, gapOffset;
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/RuleBasedPartitioner.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/RuleBasedPartitioner.java
index 7be6b9a9477..707a67f95de 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/RuleBasedPartitioner.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/rules/RuleBasedPartitioner.java
@@ -23,6 +23,7 @@ import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.IDocumentPartitionerExtension;
+import org.eclipse.jface.text.IDocumentPartitionerExtension2;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Position;
@@ -45,9 +46,12 @@ import org.eclipse.jface.text.TypedRegion;
*
* @deprecated use <code>DefaultPartitioner</code> instead
*/
-public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPartitionerExtension {
+public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPartitionerExtension, IDocumentPartitionerExtension2 {
- /** The position category this partitioner uses to store the document's partitioning information */
+ /**
+ * The position category this partitioner uses to store the document's partitioning information
+ * @deprecated use <code>getManagingPositionCategories()</code>.
+ */
public final static String CONTENT_TYPES_CATEGORY= "__content_types_category"; //$NON-NLS-1$
@@ -60,13 +64,18 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
/** The document length before a document change occured */
protected int fPreviousDocumentLength;
/** The position updater used to for the default updating of partitions */
- protected DefaultPositionUpdater fPositionUpdater= new DefaultPositionUpdater(CONTENT_TYPES_CATEGORY);
+ protected DefaultPositionUpdater fPositionUpdater;
/** The offset at which the first changed partition starts */
protected int fStartOffset;
/** The offset at which the last changed partition ends */
protected int fEndOffset;
/**The offset at which a partition has been deleted */
protected int fDeleteOffset;
+ /**
+ * The position category for managing partitoning information.
+ * @since 3.0
+ */
+ private String fPositionCategory;
/**
@@ -79,6 +88,16 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
public RuleBasedPartitioner(RuleBasedScanner scanner, String[] legalContentTypes) {
fScanner= scanner;
fLegalContentTypes= legalContentTypes;
+ fPositionCategory= CONTENT_TYPES_CATEGORY + hashCode();
+ fPositionUpdater= new DefaultPositionUpdater(fPositionCategory);
+ }
+
+ /*
+ * @see org.eclipse.jface.text.IDocumentPartitionerExtension2#getManagingPositionCategories()
+ * @since 3.0
+ */
+ public String[] getManagingPositionCategories() {
+ return new String[] { fPositionCategory };
}
/*
@@ -86,10 +105,10 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
*/
public void connect(IDocument document) {
Assert.isNotNull(document);
- Assert.isTrue(!document.containsPositionCategory(CONTENT_TYPES_CATEGORY));
+ Assert.isTrue(!document.containsPositionCategory(fPositionCategory));
fDocument= document;
- fDocument.addPositionCategory(CONTENT_TYPES_CATEGORY);
+ fDocument.addPositionCategory(fPositionCategory);
initialize();
}
@@ -109,7 +128,7 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
if (isSupportedContentType(contentType)) {
TypedPosition p= new TypedPosition(fScanner.getTokenOffset(), fScanner.getTokenLength(), contentType);
- fDocument.addPosition(CONTENT_TYPES_CATEGORY, p);
+ fDocument.addPosition(fPositionCategory, p);
}
token= fScanner.nextToken();
@@ -126,10 +145,10 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
*/
public void disconnect() {
- Assert.isTrue(fDocument.containsPositionCategory(CONTENT_TYPES_CATEGORY));
+ Assert.isTrue(fDocument.containsPositionCategory(fPositionCategory));
try {
- fDocument.removePositionCategory(CONTENT_TYPES_CATEGORY);
+ fDocument.removePositionCategory(fPositionCategory);
} catch (BadPositionCategoryException x) {
// can not happen because of Assert
}
@@ -217,7 +236,7 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
try {
IDocument d= e.getDocument();
- Position[] category= d.getPositions(CONTENT_TYPES_CATEGORY);
+ Position[] category= d.getPositions(fPositionCategory);
int first= 0;
int reparseStart= 0;
int originalSize= category.length;
@@ -229,7 +248,7 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
* first position behind the last non-default partition the actual position is not involved with
*/
- first= d.computeIndexInCategory(CONTENT_TYPES_CATEGORY, e.getOffset());
+ first= d.computeIndexInCategory(fPositionCategory, e.getOffset());
Position p= null;
do {
@@ -251,7 +270,7 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
break;
}
}
- category= d.getPositions(CONTENT_TYPES_CATEGORY);
+ category= d.getPositions(fPositionCategory);
if (first >= 0) {
p= category[first];
@@ -286,11 +305,11 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
TypedPosition p= (TypedPosition) category[first];
if (lastScannedPosition >= p.offset + p.length ||
(p.overlapsWith(start, length) &&
- (!d.containsPosition(CONTENT_TYPES_CATEGORY, start, length) ||
+ (!d.containsPosition(fPositionCategory, start, length) ||
!contentType.equals(p.getType())))) {
rememberRegion(p.offset, p.length);
- d.removePosition(CONTENT_TYPES_CATEGORY, p);
+ d.removePosition(fPositionCategory, p);
++ first;
} else
@@ -298,12 +317,12 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
}
// if position already exists we are done
- if (d.containsPosition(CONTENT_TYPES_CATEGORY, start, length))
+ if (d.containsPosition(fPositionCategory, start, length))
return createRegion();
// insert the new type position
try {
- d.addPosition(CONTENT_TYPES_CATEGORY, new TypedPosition(start, length, contentType));
+ d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
rememberRegion(start, length);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
@@ -318,12 +337,12 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
// if this condition is not met, nothing has been scanned because of a delete
++ lastScannedPosition;
}
- first= d.computeIndexInCategory(CONTENT_TYPES_CATEGORY, lastScannedPosition);
+ first= d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
TypedPosition p;
while (first < category.length) {
p= (TypedPosition) category[first++];
- d.removePosition(CONTENT_TYPES_CATEGORY, p);
+ d.removePosition(fPositionCategory, p);
rememberRegion(p.offset, p.length);
}
@@ -350,8 +369,8 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
try {
- int index= fDocument.computeIndexInCategory(CONTENT_TYPES_CATEGORY, offset);
- Position[] category= fDocument.getPositions(CONTENT_TYPES_CATEGORY);
+ int index= fDocument.computeIndexInCategory(fPositionCategory, offset);
+ Position[] category= fDocument.getPositions(fPositionCategory);
if (category.length == 0)
return null;
@@ -393,12 +412,12 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
try {
- Position[] category = fDocument.getPositions(CONTENT_TYPES_CATEGORY);
+ Position[] category = fDocument.getPositions(fPositionCategory);
if (category == null || category.length == 0)
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
- int index= fDocument.computeIndexInCategory(CONTENT_TYPES_CATEGORY, offset);
+ int index= fDocument.computeIndexInCategory(fPositionCategory, offset);
if (index < category.length) {
@@ -443,7 +462,7 @@ public class RuleBasedPartitioner implements IDocumentPartitioner, IDocumentPart
int endOffset= offset + length;
- Position[] category= fDocument.getPositions(CONTENT_TYPES_CATEGORY);
+ Position[] category= fDocument.getPositions(fPositionCategory);
TypedPosition previous= null, current= null;
int start, end, gapOffset;

Back to the top