Search in sources :

Example 16 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.

the class ContentFormatter method getPartitioning.

/**
 * Returns the partitioning of the given region of the document to be formatted.
 * As one partition after the other will be formatted and formatting will
 * probably change the length of the formatted partition, it must be kept
 * track of the modifications in order to submit the correct partition to all
 * formatting strategies. For this, all partitions are remembered as positions
 * in a dedicated position category. (As formatting strategies might rely on each
 * other, calling them in reversed order is not an option.)
 *
 * @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
 * @since 3.0
 */
private TypedPosition[] getPartitioning(IRegion region) throws BadLocationException {
    ITypedRegion[] regions = TextUtilities.computePartitioning(fDocument, fPartitioning, region.getOffset(), region.getLength(), false);
    TypedPosition[] positions = new TypedPosition[regions.length];
    for (int i = 0; i < regions.length; i++) {
        positions[i] = new TypedPosition(regions[i]);
        try {
            fDocument.addPosition(PARTITIONING, positions[i]);
        } catch (BadPositionCategoryException x) {
        // should not happen
        }
    }
    return positions;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 17 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.

the class PresentationReconciler method getDamageEndOffset.

/**
 * Returns the end offset of the damage. If a partition has been split by
 * the given document event also the second half of the original
 * partition must be considered. This is achieved by using the remembered
 * partition range.
 *
 * @param e the event describing the change
 * @return the damage end offset (excluding)
 * @exception BadLocationException if method accesses invalid offset
 */
private int getDamageEndOffset(DocumentEvent e) throws BadLocationException {
    IDocument d = e.getDocument();
    int length = 0;
    if (e.getText() != null) {
        length = e.getText().length();
        if (length > 0)
            --length;
    }
    ITypedRegion partition = getPartition(d, e.getOffset() + length);
    int endOffset = partition.getOffset() + partition.getLength();
    if (endOffset == e.getOffset())
        return -1;
    int end = fRememberedPosition == null ? -1 : fRememberedPosition.getOffset() + fRememberedPosition.getLength();
    if (endOffset < end && end < d.getLength())
        partition = getPartition(d, end);
    IPresentationDamager damager = getDamager(partition.getType());
    if (damager == null)
        return -1;
    IRegion r = damager.getDamageRegion(partition, e, fDocumentPartitioningChanged);
    return r.getOffset() + r.getLength();
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion)

Example 18 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.

the class Reconciler method initialProcess.

@Override
protected void initialProcess() {
    ITypedRegion[] regions = computePartitioning(0, getDocument().getLength());
    List<String> contentTypes = new ArrayList<>(regions.length);
    for (ITypedRegion region : regions) {
        String contentType = region.getType();
        if (contentTypes.contains(contentType))
            continue;
        contentTypes.add(contentType);
        IReconcilingStrategy s = getReconcilingStrategy(contentType);
        if (s instanceof IReconcilingStrategyExtension) {
            IReconcilingStrategyExtension e = (IReconcilingStrategyExtension) s;
            e.initialReconcile();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 19 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.

the class DefaultDamagerRepairer method getDamageRegion.

/**
 * {@inheritDoc}
 * <p>
 * This implementation damages entire lines unless clipped by the given partition.
 * </p>
 *
 * @return the full lines containing the document changes described by the document event,
 *         clipped by the given partition. If there was a partitioning change then the whole
 *         partition is returned.
 */
@Override
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) {
    if (!documentPartitioningChanged) {
        try {
            IRegion info = fDocument.getLineInformationOfOffset(e.getOffset());
            int start = Math.max(partition.getOffset(), info.getOffset());
            int end = e.getOffset() + (e.getText() == null ? e.getLength() : e.getText().length());
            if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
                // optimize the case of the same line
                end = info.getOffset() + info.getLength();
            } else
                end = endOfLineOf(end);
            end = Math.min(partition.getOffset() + partition.getLength(), end);
            return new Region(start, end - start);
        } catch (BadLocationException x) {
        }
    }
    return partition;
}
Also used : Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 20 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project eclipse.platform.text by eclipse.

the class DefaultPartitioner method computePartitioning.

@Override
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    checkInitialization();
    List<TypedRegion> list = new ArrayList<>();
    try {
        int endOffset = offset + length;
        Position[] category = fDocument.getPositions(fPositionCategory);
        TypedPosition previous = null, current = null;
        int start, end, gapOffset;
        Position gap = new Position(0);
        int startIndex = getFirstIndexEndingAfterOffset(category, offset);
        int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
        for (int i = startIndex; i < endIndex; i++) {
            current = (TypedPosition) category[i];
            gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
            gap.setOffset(gapOffset);
            gap.setLength(current.getOffset() - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
            if (current.overlapsWith(offset, length)) {
                start = Math.max(offset, current.getOffset());
                end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            gapOffset = previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(fDocument.getLength() - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, fDocument.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
        }
        if (list.isEmpty())
            list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
    } catch (BadPositionCategoryException x) {
    }
    TypedRegion[] result = new TypedRegion[list.size()];
    list.toArray(result);
    return result;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

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