Search in sources :

Example 1 with ITypedRegion

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

the class FastPartitioner method computePartitioning.

/**
	 * {@inheritDoc}
	 * <p>
	 * May be replaced or extended by subclasses.
	 * </p>
	 */
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    checkInitialization();
    List list = new ArrayList();
    try {
        int endOffset = offset + length;
        Position[] category = getPositions();
        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 ex) {
        // Make sure we clear the cache
        clearPositionCache();
    } catch (RuntimeException ex) {
        // Make sure we clear the cache
        clearPositionCache();
        throw ex;
    }
    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) ArrayList(java.util.ArrayList) List(java.util.List) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 2 with ITypedRegion

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

the class FastPartitioner method getPartition.

/**
	 * {@inheritDoc}
	 * <p>
	 * May be replaced or extended by subclasses.
	 * </p>
	 */
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 3 with ITypedRegion

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

the class SmartSemicolonAutoEditStrategy method isDefaultPartition.

/**
	 * Checks whether <code>position</code> resides in a default (Java) partition of <code>document</code>.
	 *
	 * @param document the document being modified
	 * @param position the position to be checked
	 * @param partitioning the document partitioning
	 * @return <code>true</code> if <code>position</code> is in the default partition of <code>document</code>, <code>false</code> otherwise
	 */
private static boolean isDefaultPartition(IDocument document, int position, String partitioning) {
    Assert.isTrue(position >= 0);
    Assert.isTrue(position <= document.getLength());
    try {
        // don't use getPartition2 since we're interested in the scanned character's partition
        ITypedRegion region = TextUtilities.getPartition(document, partitioning, position, false);
        return region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE);
    } catch (BadLocationException e) {
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 4 with ITypedRegion

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

the class SmartSemicolonAutoEditStrategy method nextPartitionOrLineEnd.

/**
	 * Returns a position in the first java partition after the last non-empty and non-comment partition.
	 * There is no non-whitespace from the returned position to the end of the partition it is contained in.
	 *
	 * @param document the document being modified
	 * @param line the line under investigation
	 * @param offset the caret offset into <code>line</code>
	 * @param partitioning the document partitioning
	 * @return the position of the next Java partition, or the end of <code>line</code>
	 */
private static int nextPartitionOrLineEnd(IDocument document, ITextSelection line, int offset, String partitioning) {
    // run relative to document
    final int docOffset = offset + line.getOffset();
    final int eol = line.getOffset() + line.getLength();
    // init with line end
    int nextPartitionPos = eol;
    int validPosition = docOffset;
    try {
        ITypedRegion partition = TextUtilities.getPartition(document, partitioning, nextPartitionPos, true);
        validPosition = getValidPositionForPartition(document, partition, eol);
        while (validPosition == -1) {
            nextPartitionPos = partition.getOffset() - 1;
            if (nextPartitionPos < docOffset) {
                validPosition = docOffset;
                break;
            }
            partition = TextUtilities.getPartition(document, partitioning, nextPartitionPos, false);
            validPosition = getValidPositionForPartition(document, partition, eol);
        }
    } catch (BadLocationException e) {
    }
    validPosition = Math.max(validPosition, docOffset);
    // make relative to line
    validPosition -= line.getOffset();
    return validPosition;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 5 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.

the class DocumentPartitioner method getPartition.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
@Override
public synchronized 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());
        }
        if (isOpenSingleLineCommentPartition(previous, offset)) {
            return new TypedRegion(previous.getOffset(), previous.getLength() + 1, 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 : Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) 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)

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