Search in sources :

Example 6 with BadPositionCategoryException

use of org.eclipse.che.ide.api.editor.text.BadPositionCategoryException in project che by eclipse.

the class DefaultPartitioner method computePartitioning.

private List<TypedRegion> computePartitioning(final int offset, final int length, final boolean includeZeroLengthPartitions) {
    final List<TypedRegion> result = new ArrayList<>();
    final int contentLength = getContentLength();
    try {
        final int endOffset = offset + length;
        final List<TypedPosition> category = getPositions();
        TypedPosition previous = null;
        TypedPosition current = null;
        int start, end, gapOffset;
        final Position gap = new Position(0);
        final int startIndex = getFirstIndexEndingAfterOffset(category, offset);
        final int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
        for (int i = startIndex; i < endIndex; i++) {
            current = category.get(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());
                result.add(new TypedRegionImpl(start, end - start, DEFAULT_CONTENT_TYPE));
            }
            if (current.overlapsWith(offset, length)) {
                start = Math.max(offset, current.getOffset());
                end = Math.min(endOffset, current.getOffset() + current.getLength());
                result.add(new TypedRegionImpl(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            gapOffset = previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(contentLength - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, contentLength);
                result.add(new TypedRegionImpl(start, end - start, DEFAULT_CONTENT_TYPE));
            }
        }
        if (result.isEmpty()) {
            result.add(new TypedRegionImpl(offset, length, DEFAULT_CONTENT_TYPE));
        }
    } catch (final BadPositionCategoryException ex) {
        Logger.getLogger(DefaultPartitioner.class.getName()).fine("Bad position in computePartitioning.");
    } catch (final RuntimeException ex) {
        Logger.getLogger(DefaultPartitioner.class.getName()).warning("computePartitioning failed.");
        throw ex;
    }
    return result;
}
Also used : TypedRegionImpl(org.eclipse.che.ide.api.editor.text.TypedRegionImpl) Position(org.eclipse.che.ide.api.editor.text.Position) TypedPosition(org.eclipse.che.ide.api.editor.text.TypedPosition) TypedPosition(org.eclipse.che.ide.api.editor.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.che.ide.api.editor.text.BadPositionCategoryException) TypedRegion(org.eclipse.che.ide.api.editor.text.TypedRegion)

Example 7 with BadPositionCategoryException

use of org.eclipse.che.ide.api.editor.text.BadPositionCategoryException in project che by eclipse.

the class DefaultPartitioner method updatePositions.

private void updatePositions() {
    // set before the scan as the scan uses the content length
    this.documentPositionMap.setContentLength(getContentLength());
    this.documentPositionMap.resetPositions();
    Position current = null;
    try {
        Token token = scanner.nextToken();
        while (!token.isEOF()) {
            final String contentType = getTokenContentType(token);
            if (isSupportedContentType(contentType)) {
                final TypedPosition position = new TypedPosition(scanner.getTokenOffset(), scanner.getTokenLength(), contentType);
                current = position;
                this.documentPositionMap.addPosition(this.positionCategory, position);
            }
            token = scanner.nextToken();
        }
    } catch (final BadLocationException x) {
        Log.error(DefaultPartitioner.class, "Invalid position: " + String.valueOf(current) + " (max:" + getContentLength() + ").", x);
    } catch (final BadPositionCategoryException x) {
        Log.error(DefaultPartitioner.class, "Invalid position category: " + this.positionCategory, x);
    }
}
Also used : Position(org.eclipse.che.ide.api.editor.text.Position) TypedPosition(org.eclipse.che.ide.api.editor.text.TypedPosition) TypedPosition(org.eclipse.che.ide.api.editor.text.TypedPosition) BadPositionCategoryException(org.eclipse.che.ide.api.editor.text.BadPositionCategoryException) Token(org.eclipse.che.ide.api.editor.text.rules.Token) BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Example 8 with BadPositionCategoryException

use of org.eclipse.che.ide.api.editor.text.BadPositionCategoryException in project che by eclipse.

the class AbstractDocument method getEndingPositions.

/**
     * A list of positions in the given category with an end position inside the given region. The order of the positions is
     * arbitrary.
     *
     * @param category
     *         the position category
     * @param offset
     *         the offset of the region
     * @param length
     *         the length of the region
     * @return a list of the positions in the region
     * @throws BadPositionCategoryException
     *         if category is undefined in this document
     * @since 3.4
     */
private List<Position> getEndingPositions(String category, int offset, int length) throws BadPositionCategoryException {
    List<Position> positions = fEndPositions.get(category);
    if (positions == null)
        throw new BadPositionCategoryException();
    int indexStart = computeIndexInPositionList(positions, offset, false);
    int indexEnd = computeIndexInPositionList(positions, offset + length, false);
    return positions.subList(indexStart, indexEnd);
}
Also used : Position(org.eclipse.che.ide.api.editor.text.Position) BadPositionCategoryException(org.eclipse.che.ide.api.editor.text.BadPositionCategoryException)

Example 9 with BadPositionCategoryException

use of org.eclipse.che.ide.api.editor.text.BadPositionCategoryException in project che by eclipse.

the class AbstractDocument method removePosition.

/*
     * @see org.eclipse.jface.text.IDocument#removePosition(java.lang.String, org.eclipse.jface.text.Position)
     */
public void removePosition(String category, Position position) throws BadPositionCategoryException {
    if (position == null)
        return;
    if (category == null)
        throw new BadPositionCategoryException();
    List<Position> c = fPositions.get(category);
    if (c == null)
        throw new BadPositionCategoryException();
    removeFromPositionsList(c, position, true);
    List<Position> endPositions = fEndPositions.get(category);
    if (endPositions == null)
        throw new BadPositionCategoryException();
    removeFromPositionsList(endPositions, position, false);
}
Also used : Position(org.eclipse.che.ide.api.editor.text.Position) BadPositionCategoryException(org.eclipse.che.ide.api.editor.text.BadPositionCategoryException)

Aggregations

BadPositionCategoryException (org.eclipse.che.ide.api.editor.text.BadPositionCategoryException)9 Position (org.eclipse.che.ide.api.editor.text.Position)7 BadLocationException (org.eclipse.che.ide.api.editor.text.BadLocationException)3 TypedPosition (org.eclipse.che.ide.api.editor.text.TypedPosition)3 TypedRegionImpl (org.eclipse.che.ide.api.editor.text.TypedRegionImpl)2 ArrayList (java.util.ArrayList)1 TypedRegion (org.eclipse.che.ide.api.editor.text.TypedRegion)1 Token (org.eclipse.che.ide.api.editor.text.rules.Token)1