Search in sources :

Example 11 with BadPositionCategoryException

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

the class ProjectionMapping method toClosestImageLine.

@Override
public int toClosestImageLine(int originLine) throws BadLocationException {
    try {
        int imageLine = toImageLine(originLine);
        if (imageLine > -1)
            return imageLine;
        Position[] fragments = getFragments();
        if (fragments.length == 0)
            return -1;
        IRegion originLineRegion = fMasterDocument.getLineInformation(originLine);
        int index = fMasterDocument.computeIndexInCategory(fFragmentsCategory, originLineRegion.getOffset());
        if (0 < index && index < fragments.length) {
            Fragment left = (Fragment) fragments[index - 1];
            int leftDistance = originLineRegion.getOffset() - (exclusiveEnd(left));
            Fragment right = (Fragment) fragments[index];
            int rightDistance = right.getOffset() - (exclusiveEnd(originLineRegion));
            if (leftDistance <= rightDistance)
                originLine = fMasterDocument.getLineOfOffset(left.getOffset() + Math.max(left.getLength() - 1, 0));
            else
                originLine = fMasterDocument.getLineOfOffset(right.getOffset());
        } else if (index == 0) {
            Fragment right = (Fragment) fragments[index];
            originLine = fMasterDocument.getLineOfOffset(right.getOffset());
        } else if (index == fragments.length) {
            Fragment left = (Fragment) fragments[index - 1];
            originLine = fMasterDocument.getLineOfOffset(exclusiveEnd(left));
        }
        return toImageLine(originLine);
    } catch (BadPositionCategoryException x) {
    }
    return -1;
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IRegion(org.eclipse.jface.text.IRegion)

Example 12 with BadPositionCategoryException

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

the class LinkedModeModel method register.

/**
 * Registers a <code>LinkedPosition</code> with this model. Called
 * by <code>PositionGroup</code>.
 *
 * @param position the position to register
 * @throws BadLocationException if the position cannot be added to its
 *         document
 */
void register(LinkedPosition position) throws BadLocationException {
    Assert.isNotNull(position);
    IDocument document = position.getDocument();
    manageDocument(document);
    try {
        document.addPosition(getCategory(), position);
    } catch (BadPositionCategoryException e) {
        // won't happen as the category has been added by manageDocument()
        Assert.isTrue(false);
    }
    int seqNr = position.getSequenceNumber();
    if (seqNr != LinkedPositionGroup.NO_STOP) {
        fPositionSequence.add(position);
    }
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument)

Example 13 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException 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 14 with BadPositionCategoryException

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

the class ContentFormatter method updateAffectedPositions.

/**
 * Updates all the overlapping positions. Note, all other positions are
 * automatically updated by their document position updaters.
 *
 * @param document the document to has been formatted
 * @param positions the adapted character positions to be used to update the document positions
 * @param offset the offset of the document region that has been formatted
 */
protected void updateAffectedPositions(IDocument document, int[] positions, int offset) {
    if (document != fDocument)
        return;
    if (positions.length == 0)
        return;
    for (int i = 0; i < positions.length; i++) {
        PositionReference r = fOverlappingPositionReferences.get(i);
        if (r.refersToOffset())
            r.setOffset(offset + positions[i]);
        else
            r.setLength((offset + positions[i]) - r.getOffset());
        Position p = r.getPosition();
        String category = r.getCategory();
        if (!document.containsPosition(category, p.offset, p.length)) {
            try {
                if (positionAboutToBeAdded(document, category, p))
                    document.addPosition(r.getCategory(), p);
            } catch (BadPositionCategoryException x) {
            // can not happen
            } catch (BadLocationException x) {
            // should not happen
            }
        }
    }
    fOverlappingPositionReferences = null;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException 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

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)42 Position (org.eclipse.jface.text.Position)30 BadLocationException (org.eclipse.jface.text.BadLocationException)22 TypedPosition (org.eclipse.jface.text.TypedPosition)18 IDocument (org.eclipse.jface.text.IDocument)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)10 TypedRegion (org.eclipse.jface.text.TypedRegion)8 ArrayList (java.util.ArrayList)7 IRegion (org.eclipse.jface.text.IRegion)7 Point (org.eclipse.swt.graphics.Point)4 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 List (java.util.List)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1