Search in sources :

Example 81 with ITypedRegion

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;
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 82 with ITypedRegion

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);
    }
}
Also used : Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 83 with ITypedRegion

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);
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 84 with ITypedRegion

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);
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 85 with ITypedRegion

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);
    }
}
Also used : IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) HashMap(java.util.HashMap) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITypedRegion (org.eclipse.jface.text.ITypedRegion)129 BadLocationException (org.eclipse.jface.text.BadLocationException)59 IRegion (org.eclipse.jface.text.IRegion)42 IDocument (org.eclipse.jface.text.IDocument)21 Region (org.eclipse.jface.text.Region)19 ArrayList (java.util.ArrayList)17 TypedRegion (org.eclipse.jface.text.TypedRegion)16 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)14 Position (org.eclipse.jface.text.Position)14 TypedPosition (org.eclipse.jface.text.TypedPosition)13 List (java.util.List)11 StyleRange (org.eclipse.swt.custom.StyleRange)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)6 ITextSelection (org.eclipse.jface.text.ITextSelection)5 BadPartitioningException (org.eclipse.jface.text.BadPartitioningException)4 Document (org.eclipse.jface.text.Document)4 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)4 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)4