Search in sources :

Example 1 with TypedRegionImpl

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

the class DefaultPartitioner method getPartition.

@Override
public TypedRegion getPartition(final int offset) {
    final int contentLength = getContentLength();
    List<TypedPosition> category = null;
    try {
        category = getPositions();
    } catch (final BadPositionCategoryException e) {
        Log.warn(DefaultPartitioner.class, "Invalid position cateory... with default category! ", e);
        return defaultRegion();
    }
    if (category == null || category.size() == 0) {
        return defaultRegion();
    }
    Integer index = null;
    try {
        index = this.documentPositionMap.computeIndexInCategory(positionCategory, offset);
    } catch (final BadLocationException e) {
        Log.warn(DefaultPartitioner.class, "Invalid location " + offset + " (max=" + contentLength + ").");
        return defaultRegion();
    } catch (final BadPositionCategoryException e) {
        Log.warn(DefaultPartitioner.class, "Invalid position cateory... with default category " + positionCategory + "!", e);
        return defaultRegion();
    }
    if (index == null) {
        return defaultRegion();
    }
    if (index < category.size()) {
        final TypedPosition next = category.get(index);
        if (offset == next.offset) {
            return new TypedRegionImpl(next.getOffset(), next.getLength(), next.getType());
        }
        if (index == 0) {
            return new TypedRegionImpl(0, next.offset, DEFAULT_CONTENT_TYPE);
        }
        final TypedPosition previous = category.get(index - 1);
        if (previous.includes(offset)) {
            return new TypedRegionImpl(previous.getOffset(), previous.getLength(), previous.getType());
        }
        final int endOffset = previous.getOffset() + previous.getLength();
        return new TypedRegionImpl(endOffset, next.getOffset() - endOffset, DEFAULT_CONTENT_TYPE);
    }
    final TypedPosition previous = category.get(category.size() - 1);
    if (previous.includes(offset)) {
        return new TypedRegionImpl(previous.getOffset(), previous.getLength(), previous.getType());
    }
    final int endOffset = previous.getOffset() + previous.getLength();
    return new TypedRegionImpl(endOffset, contentLength - endOffset, DEFAULT_CONTENT_TYPE);
}
Also used : TypedRegionImpl(org.eclipse.che.ide.api.editor.text.TypedRegionImpl) TypedPosition(org.eclipse.che.ide.api.editor.text.TypedPosition) BadPositionCategoryException(org.eclipse.che.ide.api.editor.text.BadPositionCategoryException) BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Example 2 with TypedRegionImpl

use of org.eclipse.che.ide.api.editor.text.TypedRegionImpl 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)

Aggregations

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