use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class PresentationReconciler method getDamage.
/**
* Checks for the first and the last affected partition affected by a
* document event and calls their damagers. Invalidates everything from the
* start of the damage for the first partition until the end of the damage
* for the last partition.
*
* @param e the event describing the document change
* @param optimize <code>true</code> if partition changes should be
* considered for optimization
* @return the damaged caused by the change or <code>null</code> if
* computing the partitioning failed
* @since 3.0
*/
private IRegion getDamage(DocumentEvent e, boolean optimize) {
int length = e.getText() == null ? 0 : e.getText().length();
if (fDamagers == null || fDamagers.isEmpty()) {
length = Math.max(e.getLength(), length);
length = Math.min(e.getDocument().getLength() - e.getOffset(), length);
return new Region(e.getOffset(), length);
}
boolean isDeletion = length == 0;
IRegion damage = null;
try {
int offset = e.getOffset();
if (isDeletion)
offset = Math.max(0, offset - 1);
ITypedRegion partition = getPartition(e.getDocument(), offset);
IPresentationDamager damager = getDamager(partition.getType());
if (damager == null)
return null;
IRegion r = damager.getDamageRegion(partition, e, fDocumentPartitioningChanged);
if (!fDocumentPartitioningChanged && optimize && !isDeletion) {
damage = r;
} else {
int damageStart = r.getOffset();
int damageEnd = getDamageEndOffset(e);
if (fChangedDocumentPartitions != null) {
damageStart = Math.min(damageStart, fChangedDocumentPartitions.getOffset());
damageEnd = Math.max(damageEnd, fChangedDocumentPartitions.getOffset() + fChangedDocumentPartitions.getLength());
}
damage = damageEnd == -1 ? r : new Region(damageStart, damageEnd - damageStart);
}
} catch (BadLocationException x) {
}
return damage;
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class Reconciler method process.
/**
* Processes a dirty region. If the dirty region is <code>null</code> the whole
* document is consider being dirty. The dirty region is partitioned by the
* document and each partition is handed over to a reconciling strategy registered
* for the partition's content type.
*
* @param dirtyRegion the dirty region to be processed
* @see AbstractReconciler#process(DirtyRegion)
*/
@Override
protected void process(DirtyRegion dirtyRegion) {
IRegion region = dirtyRegion;
if (region == null)
region = new Region(0, getDocument().getLength());
ITypedRegion[] regions = computePartitioning(region.getOffset(), region.getLength());
for (ITypedRegion r : regions) {
IReconcilingStrategy s = getReconcilingStrategy(r.getType());
if (s == null)
continue;
if (dirtyRegion != null)
s.reconcile(dirtyRegion, r);
else
s.reconcile(r);
}
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class DefaultPartitioner method getPartition.
@Override
public ITypedRegion getPartition(int offset) {
checkInitialization();
try {
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(fPositionCategory, offset);
if (index < category.length) {
TypedPosition next = (TypedPosition) category[index];
if (offset == next.offset)
return new TypedRegion(next.getOffset(), next.getLength(), next.getType());
if (index == 0)
return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE);
TypedPosition previous = (TypedPosition) category[index - 1];
if (previous.includes(offset))
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
}
TypedPosition previous = (TypedPosition) category[category.length - 1];
if (previous.includes(offset))
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
}
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class FastPartitioner method getPartition.
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*/
@Override
public ITypedRegion getPartition(int offset) {
checkInitialization();
try {
Position[] category = getPositions();
if (category == null || category.length == 0)
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
int index = fDocument.computeIndexInCategory(fPositionCategory, offset);
if (index < category.length) {
TypedPosition next = (TypedPosition) category[index];
if (offset == next.offset)
return new TypedRegion(next.getOffset(), next.getLength(), next.getType());
if (index == 0)
return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE);
TypedPosition previous = (TypedPosition) category[index - 1];
if (previous.includes(offset))
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
}
TypedPosition previous = (TypedPosition) category[category.length - 1];
if (previous.includes(offset))
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
}
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
}
use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.
the class MultiPassContentFormatter method formatSlaves.
/**
* Formats the document specified in the formatting context with the slave
* formatting strategies.
* <p>
* For each content type of the region to be formatted in the document
* partitioning, the registered slave formatting strategy is used to format
* that particular region. The region to be formatted is aligned on
* partition boundaries of the underlying content type. If the content type
* is the document's default content type, nothing happens.
*
* @param context The formatting context to use
* @param document The document to operate on
* @param offset The offset of the region to format
* @param length The length of the region to format
*/
protected void formatSlaves(final IFormattingContext context, final IDocument document, final int offset, final int length) {
Map<String, IDocumentPartitioner> partitioners = new HashMap<>(0);
try {
final ITypedRegion[] partitions = TextUtilities.computePartitioning(document, fPartitioning, offset, length, false);
if (!fType.equals(partitions[0].getType()))
partitions[0] = TextUtilities.getPartition(document, fPartitioning, partitions[0].getOffset(), false);
if (partitions.length > 1) {
if (!fType.equals(partitions[partitions.length - 1].getType()))
partitions[partitions.length - 1] = TextUtilities.getPartition(document, fPartitioning, partitions[partitions.length - 1].getOffset(), false);
}
String type = null;
ITypedRegion partition = null;
partitioners = TextUtilities.removeDocumentPartitioners(document);
for (int index = partitions.length - 1; index >= 0; index--) {
partition = partitions[index];
type = partition.getType();
if (!fType.equals(type))
formatSlave(context, document, partition.getOffset(), partition.getLength(), type);
}
} catch (BadLocationException exception) {
// Should not happen
} finally {
TextUtilities.addDocumentPartitioners(document, partitioners);
}
}
Aggregations