Search in sources :

Example 6 with TypedPosition

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

the class ContentFormatter method formatRegion.

/**
 * Formats the given region with the strategy registered for the default
 * content type. The strategy is informed about the start, the process, and
 * the termination of the formatting session.
 *
 * @param region the region to be formatted
 * @since 3.0
 */
private void formatRegion(IRegion region) {
    IFormattingStrategy strategy = getFormattingStrategy(IDocument.DEFAULT_CONTENT_TYPE);
    if (strategy != null) {
        strategy.formatterStarts(getIndentation(region.getOffset()));
        format(strategy, new TypedPosition(region.getOffset(), region.getLength(), IDocument.DEFAULT_CONTENT_TYPE));
        strategy.formatterStops();
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition)

Example 7 with TypedPosition

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

the class MultiPassContentFormatter method formatSlave.

/**
 * Formats the document specified in the formatting context with the
 * formatting strategy registered for the content type.
 * <p>
 * For this formatting type only slave strategies are used. The region to be
 * formatted is aligned on partition boundaries of the underlying content
 * type. The exact formatting strategy is determined by the underlying
 * content type of the document partitioning.
 *
 * @param context The formatting context to use
 * @param document The document to operate on
 * @param offset The offset of the region to format
 * @param length The length of the region to format
 * @param type The content type of the region to format
 */
protected void formatSlave(final IFormattingContext context, final IDocument document, final int offset, final int length, final String type) {
    final IFormattingStrategyExtension strategy = (IFormattingStrategyExtension) fSlaves.get(type);
    if (strategy != null) {
        context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, type));
        strategy.formatterStarts(context);
        strategy.format();
        strategy.formatterStops();
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition)

Example 8 with TypedPosition

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

the class MultiPassContentFormatter method formatMaster.

/**
 * Formats the document specified in the formatting context with the master
 * formatting strategy.
 * <p>
 * The master formatting strategy covers all regions of the document. The
 * offset of the region to be formatted is aligned on line start boundaries,
 * whereas the end index of the region remains the same. For this formatting
 * type the document partitioning is not taken into account.
 *
 * @param context The formatting context to use
 * @param document The document to operate on
 * @param offset The offset of the region to format
 * @param length The length of the region to format
 */
protected void formatMaster(final IFormattingContext context, final IDocument document, int offset, int length) {
    try {
        if (length != 0) {
            // Extend the selection to the beginning of line if it is not empty.
            // An empty selection must remain empty since it may be treated in
            // a special way by the formatter.
            final int delta = offset - document.getLineInformationOfOffset(offset).getOffset();
            offset -= delta;
            length += delta;
        }
    } catch (BadLocationException exception) {
    // Do nothing
    }
    if (fMaster != null) {
        context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, fType));
        fMaster.formatterStarts(context);
        fMaster.format();
        fMaster.formatterStops();
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with TypedPosition

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

Example 10 with TypedPosition

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

the class DefaultPartitioner method documentChanged2.

@Override
public IRegion documentChanged2(DocumentEvent e) {
    if (!fIsInitialized)
        return null;
    try {
        IDocument d = e.getDocument();
        Position[] category = d.getPositions(fPositionCategory);
        IRegion line = d.getLineInformationOfOffset(e.getOffset());
        int reparseStart = line.getOffset();
        int partitionStart = -1;
        String contentType = null;
        int newLength = e.getText() == null ? 0 : e.getText().length();
        int first = d.computeIndexInCategory(fPositionCategory, reparseStart);
        if (first > 0) {
            TypedPosition partition = (TypedPosition) category[first - 1];
            if (partition.includes(reparseStart)) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                if (e.getOffset() == partition.getOffset() + partition.getLength())
                    reparseStart = partitionStart;
                --first;
            } else if (reparseStart == e.getOffset() && reparseStart == partition.getOffset() + partition.getLength()) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                reparseStart = partitionStart;
                --first;
            } else {
                partitionStart = partition.getOffset() + partition.getLength();
                contentType = IDocument.DEFAULT_CONTENT_TYPE;
            }
        }
        fPositionUpdater.update(e);
        for (int i = first; i < category.length; i++) {
            Position p = category[i];
            if (p.isDeleted) {
                rememberDeletedOffset(e.getOffset());
                break;
            }
        }
        category = d.getPositions(fPositionCategory);
        fScanner.setPartialRange(d, reparseStart, d.getLength() - reparseStart, contentType, partitionStart);
        int lastScannedPosition = reparseStart;
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            contentType = getTokenContentType(token);
            if (!isSupportedContentType(contentType)) {
                token = fScanner.nextToken();
                continue;
            }
            int start = fScanner.getTokenOffset();
            int length = fScanner.getTokenLength();
            lastScannedPosition = start + length - 1;
            // remove all affected positions
            while (first < category.length) {
                TypedPosition p = (TypedPosition) category[first];
                if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
                    rememberRegion(p.offset, p.length);
                    d.removePosition(fPositionCategory, p);
                    ++first;
                } else
                    break;
            }
            // area covered by the event, we are done
            if (d.containsPosition(fPositionCategory, start, length)) {
                if (lastScannedPosition >= e.getOffset() + newLength)
                    return createRegion();
                ++first;
            } else {
                // insert the new type position
                try {
                    d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
                    rememberRegion(start, length);
                } catch (BadPositionCategoryException x) {
                } catch (BadLocationException x) {
                }
            }
            token = fScanner.nextToken();
        }
        // remove all positions behind lastScannedPosition since there aren't any further types
        if (lastScannedPosition != reparseStart) {
            // if this condition is not met, nothing has been scanned because of a deletion
            ++lastScannedPosition;
        }
        first = d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
        category = d.getPositions(fPositionCategory);
        TypedPosition p;
        while (first < category.length) {
            p = (TypedPosition) category[first++];
            d.removePosition(fPositionCategory, p);
            rememberRegion(p.offset, p.length);
        }
    } catch (BadPositionCategoryException x) {
    // should never happen on connected documents
    } catch (BadLocationException x) {
    }
    return createRegion();
}
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) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

TypedPosition (org.eclipse.jface.text.TypedPosition)38 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)26 BadLocationException (org.eclipse.jface.text.BadLocationException)23 Position (org.eclipse.jface.text.Position)19 ITypedRegion (org.eclipse.jface.text.ITypedRegion)13 TypedRegion (org.eclipse.jface.text.TypedRegion)12 ArrayList (java.util.ArrayList)7 IDocument (org.eclipse.jface.text.IDocument)7 IRegion (org.eclipse.jface.text.IRegion)5 IToken (org.eclipse.jface.text.rules.IToken)4 TextEdit (org.eclipse.text.edits.TextEdit)3 List (java.util.List)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 XMLNode (com.amalto.workbench.widgets.xmlviewer.model.XMLNode)1 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CoreException (org.eclipse.core.runtime.CoreException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1