Search in sources :

Example 31 with TypedPosition

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

the class StructuredFormattingStrategy 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 && fFormatProcessor != null) {
        try {
            fFormatProcessor.formatDocument(document, fRegion.getOffset(), fRegion.getLength());
        } catch (IOException e) {
            // log for now, unless we find reason not to
            Logger.log(Logger.INFO, e.getMessage());
        } catch (CoreException e) {
            // log for now, unless we find reason not to
            Logger.log(Logger.INFO, e.getMessage());
        }
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) TypedPosition(org.eclipse.jface.text.TypedPosition) IOException(java.io.IOException) IDocument(org.eclipse.jface.text.IDocument)

Example 32 with TypedPosition

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

the class StructuredTextMultiPassContentFormatter method formatSlave.

/*
	 * Overwritten to check for additional slave formatting strategies
	 * contributed via the editorConfiguration extension point.
	 */
protected void formatSlave(IFormattingContext context, IDocument document, int offset, int length, String type) {
    List installedTypes = getInstalledPartitionTypes();
    if (installedTypes.contains(type)) {
        // we've already set the slave formatter strategy so just perform
        // as normal
        super.formatSlave(context, document, offset, length, type);
    } else {
        boolean findExtendedSlaveFormatter = true;
        // need to figure out if there's already a slave formatter set, so
        // just attempt to format as normal
        super.formatSlave(context, document, offset, length, type);
        // now, determine if slave formatter was already set by checking
        // context (it would be set if there's already one)
        Object contextPartition = context.getProperty(FormattingContextProperties.CONTEXT_PARTITION);
        if (contextPartition instanceof TypedPosition) {
            String contextType = ((TypedPosition) contextPartition).getType();
            if (contextType == type) {
                // there's already a slave formatter set, so just add it
                // to the list of installed partition types for future
                // reference
                installedTypes.add(type);
                findExtendedSlaveFormatter = false;
            }
        }
        // via the editorConfiguration extension point
        if (findExtendedSlaveFormatter) {
            Object configuration = ExtendedConfigurationBuilder.getInstance().getConfiguration(SLAVE_FORMATTING_STRATEGY_EXTENDED_ID, type);
            if (configuration instanceof IFormattingStrategy) {
                // found a formatter, so add it in
                setSlaveStrategy((IFormattingStrategy) configuration, type);
                // try to format slave again now that one is set
                super.formatSlave(context, document, offset, length, type);
            }
            // note that we've already checked this partition type for
            // future reference
            installedTypes.add(type);
        }
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) IFormattingStrategy(org.eclipse.jface.text.formatter.IFormattingStrategy) List(java.util.List) ArrayList(java.util.ArrayList)

Example 33 with TypedPosition

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

the class DocumentPartitioner method getContentType.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
@Override
public synchronized String getContentType(int offset) {
    checkInitialization();
    TypedPosition p = findClosestPosition(offset);
    if (p != null && p.includes(offset))
        return p.getType();
    return IDocument.DEFAULT_CONTENT_TYPE;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition)

Example 34 with TypedPosition

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

the class DocumentPartitioner method computePartitioning.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 *
 * @since 2.2
 */
@Override
public synchronized ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    checkInitialization();
    List<ITypedRegion> list = new ArrayList<ITypedRegion>();
    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);
            int gapLength = fDocument.getLength() - gapOffset;
            if (gapLength < 0) {
                clearPositionCache();
                return new TypedRegion[0];
            } else {
                gap.setLength(gapLength);
                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 : Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 35 with TypedPosition

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

the class XMLDocumentPartitioner method findPreviousNonWhiteSpacePosition.

public TypedPosition findPreviousNonWhiteSpacePosition(int offset) {
    try {
        int index = document.computeIndexInCategory(positionCategory, offset);
        Position[] positions = document.getPositions(positionCategory);
        if (positions.length == 0) {
            return null;
        }
        if (index == positions.length) {
            index--;
        }
        for (; index >= 0; index--) {
            TypedPosition position = (TypedPosition) positions[index];
            if (position instanceof XMLNode) {
                XMLNode node = (XMLNode) position;
                if (!node.isEmpty())
                    return node;
            }
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    } catch (BadPositionCategoryException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : XMLNode(com.amalto.workbench.widgets.xmlviewer.model.XMLNode) TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) 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