Search in sources :

Example 26 with TypedPosition

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

the class FastPartitioner method getPartition.

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

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

the class FastPartitioner method documentChanged2.

/**
 * {@inheritDoc}
 * <p>
 * May be extended by subclasses.
 * </p>
 */
@Override
public IRegion documentChanged2(DocumentEvent e) {
    if (!fIsInitialized)
        return null;
    try {
        Assert.isTrue(e.getDocument() == fDocument);
        Position[] category = getPositions();
        IRegion line = fDocument.getLineInformationOfOffset(e.getOffset());
        int reparseStart = line.getOffset();
        int partitionStart = -1;
        String contentType = null;
        int newLength = e.getText() == null ? 0 : e.getText().length();
        int first = fDocument.computeIndexInCategory(fPositionCategory, reparseStart);
        if (first > 0) {
            TypedPosition partition = (TypedPosition) category[first - 1];
            if (partition.includes(reparseStart)) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                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;
            }
        } else {
            partitionStart = 0;
            reparseStart = 0;
        }
        fPositionUpdater.update(e);
        for (int i = first; i < category.length; i++) {
            Position p = category[i];
            if (p.isDeleted) {
                rememberDeletedOffset(e.getOffset());
                break;
            }
        }
        clearPositionCache();
        category = getPositions();
        fScanner.setPartialRange(fDocument, reparseStart, fDocument.getLength() - reparseStart, contentType, partitionStart);
        int behindLastScannedPosition = 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();
            behindLastScannedPosition = start + length;
            int lastScannedPosition = behindLastScannedPosition - 1;
            // remove all affected positions
            while (first < category.length) {
                TypedPosition p = (TypedPosition) category[first];
                if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!fDocument.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
                    rememberRegion(p.offset, p.length);
                    fDocument.removePosition(fPositionCategory, p);
                    ++first;
                } else
                    break;
            }
            // area covered by the event, we are done
            if (fDocument.containsPosition(fPositionCategory, start, length)) {
                if (lastScannedPosition >= e.getOffset() + newLength)
                    return createRegion();
                ++first;
            } else {
                // insert the new type position
                try {
                    fDocument.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
                    rememberRegion(start, length);
                } catch (BadPositionCategoryException x) {
                } catch (BadLocationException x) {
                }
            }
            token = fScanner.nextToken();
        }
        first = fDocument.computeIndexInCategory(fPositionCategory, behindLastScannedPosition);
        clearPositionCache();
        category = getPositions();
        TypedPosition p;
        while (first < category.length) {
            p = (TypedPosition) category[first++];
            fDocument.removePosition(fPositionCategory, p);
            rememberRegion(p.offset, p.length);
        }
    } catch (BadPositionCategoryException x) {
    // should never happen on connected documents
    } catch (BadLocationException x) {
    } finally {
        clearPositionCache();
    }
    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) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 28 with TypedPosition

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

the class RuleBasedPartitioner method initialize.

/**
 * Performs the initial partitioning of the partitioner's document.
 */
protected void initialize() {
    fScanner.setRange(fDocument, 0, fDocument.getLength());
    try {
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            String contentType = getTokenContentType(token);
            if (isSupportedContentType(contentType)) {
                TypedPosition p = new TypedPosition(fScanner.getTokenOffset(), fScanner.getTokenLength(), contentType);
                fDocument.addPosition(fPositionCategory, p);
            }
            token = fScanner.nextToken();
        }
    } catch (BadLocationException x) {
    // cannot happen as offsets come from scanner
    } catch (BadPositionCategoryException x) {
    // cannot happen if document has been connected before
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 29 with TypedPosition

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

the class RuleBasedPartitioner method documentChanged2.

@Override
public IRegion documentChanged2(DocumentEvent e) {
    try {
        IDocument d = e.getDocument();
        Position[] category = d.getPositions(fPositionCategory);
        int first = 0;
        int reparseStart = 0;
        int originalSize = category.length;
        if (originalSize > 0) {
            /*
				 * determine character position at which the scanner starts:
				 * first position behind the last non-default partition the actual position is not involved with
				 */
            first = d.computeIndexInCategory(fPositionCategory, e.getOffset());
            Position p = null;
            do {
                --first;
                if (first < 0)
                    break;
                p = category[first];
            } while (p.overlapsWith(e.getOffset(), e.getLength()) || (e.getOffset() == fPreviousDocumentLength && (p.getOffset() + p.getLength() == fPreviousDocumentLength)));
            fPositionUpdater.update(e);
            for (Position element : category) {
                p = element;
                if (p.isDeleted) {
                    rememberDeletedOffset(e.getOffset());
                    break;
                }
            }
            category = d.getPositions(fPositionCategory);
            if (first >= 0) {
                p = category[first];
                reparseStart = p.getOffset() + p.getLength();
            }
            ++first;
        }
        fScanner.setRange(d, reparseStart, d.getLength() - reparseStart);
        int lastScannedPosition = reparseStart;
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            String 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;
            }
            // if position already exists we are done
            if (d.containsPosition(fPositionCategory, start, length))
                return createRegion();
            // 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 delete
            ++lastScannedPosition;
        }
        first = d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
        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) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 30 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project webtools.sourceediting by eclipse.

the class XMLFormattingStrategy method format.

/*
	 * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
	 */
public void format() {
    super.format();
    final IDocument document = (IDocument) fDocuments.removeFirst();
    final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
    if (document != null && partition != null && fRegion != null) {
        try {
            if (document instanceof IStructuredDocument) {
                IStructuredModel model = StructuredModelManager.getModelManager().getModelForEdit((IStructuredDocument) document);
                if (model != null) {
                    try {
                        TextEdit edit = formatter.format(model, fRegion.getOffset(), fRegion.getLength());
                        if (edit != null) {
                            try {
                                model.aboutToChangeModel();
                                edit.apply(document);
                            } finally {
                                model.changedModel();
                            }
                        }
                    } finally {
                        model.releaseFromEdit();
                    }
                }
            }
        } catch (BadLocationException e) {
            // log for now, unless we find reason not to
            Logger.log(Logger.INFO, e.getMessage());
        }
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) TextEdit(org.eclipse.text.edits.TextEdit) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) 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