Search in sources :

Example 16 with TypedPosition

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

the class FormattingStrategyJSPJava 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) {
        try {
            JSPTranslationUtil translationUtil = new JSPTranslationUtil(document);
            ICompilationUnit cu = translationUtil.getCompilationUnit();
            if (cu != null) {
                String cuSource = cu.getSource();
                /*
					 * Format the entire compilation unit, but only create
					 * edits for the requested JSP partition's range in the
					 * Java source
					 */
                TextEdit textEdit = formatString(CodeFormatter.K_COMPILATION_UNIT, cuSource, translationUtil.getTranslation().getJavaOffset(partition.getOffset()), partition.getLength(), TextUtilities.getDefaultLineDelimiter(document), getPreferences());
                TextEdit jspEdit = translationUtil.getTranslation().getJspEdit(textEdit);
                if (jspEdit != null && jspEdit.hasChildren())
                    jspEdit.apply(document);
            }
        } catch (MalformedTreeException exception) {
            Logger.logException(exception);
        } catch (BadLocationException exception) {
            // Can only happen on concurrent document modification - log
            // and bail out
            Logger.logException(exception);
        } catch (JavaModelException exception) {
            Logger.logException(exception);
        }
    }
}
Also used : JSPTranslationUtil(org.eclipse.jst.jsp.core.internal.java.JSPTranslationUtil) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) TypedPosition(org.eclipse.jface.text.TypedPosition) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 17 with TypedPosition

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

Example 18 with TypedPosition

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

the class DocumentPartitioner method initialize.

/**
 * Performs the initial partitioning of the partitioner's document.
 * <p>
 * May be extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
protected void initialize() {
    fIsInitialized = true;
    clearPositionCache();
    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 : IToken(org.eclipse.jface.text.rules.IToken) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 19 with TypedPosition

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

the class DocumentPartitioner method documentChanged2.

/**
 * {@inheritDoc}
 * <p>
 * May be extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
@Override
public synchronized IRegion documentChanged2(DocumentEvent e) {
    if (!fIsInitialized)
        return null;
    try {
        Assert.isTrue(e.getDocument() == fDocument);
        Position[] category = getPositions();
        int reparseStart = e.getOffset();
        String contentType = null;
        int newLength = e.getText() == null ? 0 : e.getText().length();
        int oldPosition = fDocument.computeIndexInCategory(fPositionCategory, reparseStart);
        if (oldPosition > 0) {
            Position oldPartition = category[oldPosition - 1];
            if (oldPartition.offset + oldPartition.length > reparseStart)
                reparseStart = category[oldPosition - 1].offset;
        }
        fScanner.setPartialRange(fDocument, reparseStart, fDocument.getLength() - reparseStart, /* ignore contentType */
        null, reparseStart);
        int behindLastScannedPosition = reparseStart;
        IToken token = fScanner.nextToken();
        int actualReparseStart = fScanner.getTokenOffset();
        int partitionStart = -1;
        int first = fDocument.computeIndexInCategory(fPositionCategory, actualReparseStart);
        if (first > 0) {
            TypedPosition partition = (TypedPosition) category[first - 1];
            if (partition.includes(actualReparseStart)) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                if (e.getOffset() == partition.getOffset() + partition.getLength())
                    actualReparseStart = partitionStart;
                --first;
            } else if (actualReparseStart == e.getOffset() && actualReparseStart == partition.getOffset() + partition.getLength()) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                actualReparseStart = partitionStart;
                --first;
            } else {
                partitionStart = partition.getOffset() + partition.getLength();
                contentType = IDocument.DEFAULT_CONTENT_TYPE;
                if (actualReparseStart != partitionStart) {
                    String message = String.format("Detected unexpected state in document partitioner. Please file a bug with the following information attached:%n" + "Document content after the event was applied:%n" + ">>>%s<<<%n" + "Document event: %s", fDocument.get(), String.valueOf(e));
                    log.error(message);
                }
            }
        }
        fScanner.setPartialRange(fDocument, actualReparseStart, fDocument.getLength() - actualReparseStart, contentType, actualReparseStart);
        behindLastScannedPosition = actualReparseStart;
        token = fScanner.nextToken();
        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();
        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 : Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) IToken(org.eclipse.jface.text.rules.IToken) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 20 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project tmdm-studio-se by Talend.

the class XMLDocumentPartitioner method computePartitioning.

public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    List<TypedRegion> list = new ArrayList<TypedRegion>();
    try {
        int endOffset = offset + length;
        Position[] category = document.getPositions(positionCategory);
        TypedPosition previous = null;
        TypedPosition current = null;
        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];
            int gapOffset = previous == null ? 0 : previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(current.getOffset() - gapOffset);
            if (includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length) || gap.getLength() > 0 && gap.overlapsWith(offset, length)) {
                int start = Math.max(offset, gapOffset);
                int end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                // $NON-NLS-1$
                list.add(new TypedRegion(start, end - start, "__dftl_partition_content_type"));
            }
            if (current.overlapsWith(offset, length)) {
                int start = Math.max(offset, current.getOffset());
                int end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            int gapOffset = previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(document.getLength() - gapOffset);
            if (includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length) || gap.getLength() > 0 && gap.overlapsWith(offset, length)) {
                int start = Math.max(offset, gapOffset);
                int end = Math.min(endOffset, document.getLength());
                // $NON-NLS-1$
                list.add(new TypedRegion(start, end - start, "__dftl_partition_content_type"));
            }
        }
        if (list.isEmpty()) {
            // $NON-NLS-1$
            list.add(new TypedRegion(offset, length, "__dftl_partition_content_type"));
        }
    } catch (BadPositionCategoryException _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) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

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