Search in sources :

Example 61 with BadPositionCategoryException

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

the class GenericPositionManager method addPosition.

/*
	 * @see org.eclipse.jface.text.IDocument#addPosition(java.lang.String,
	 *      org.eclipse.jface.text.Position)
	 */
public synchronized void addPosition(String category, Position position) throws BadLocationException, BadPositionCategoryException {
    if ((0 > position.offset) || (0 > position.length) || (position.offset + position.length > getDocumentLength()))
        throw new BadLocationException();
    if (category == null)
        throw new BadPositionCategoryException();
    List list = (List) fPositions.get(category);
    if (list == null)
        throw new BadPositionCategoryException();
    list.add(computeIndexInPositionList(list, position.offset), position);
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ArrayList(java.util.ArrayList) List(java.util.List) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 62 with BadPositionCategoryException

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

the class GenericPositionManager method removePosition.

/*
	 * @see org.eclipse.jface.text.IDocument#removePosition(java.lang.String,
	 *      org.eclipse.jface.text.Position)
	 */
public synchronized void removePosition(String category, Position position) throws BadPositionCategoryException {
    if (position == null)
        return;
    if (category == null)
        throw new BadPositionCategoryException();
    List c = (List) fPositions.get(category);
    if (c == null)
        throw new BadPositionCategoryException();
    // remove based on identity not equality
    int size = c.size();
    for (int i = 0; i < size; i++) {
        if (position == c.get(i)) {
            c.remove(i);
            return;
        }
    }
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 63 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException 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 64 with BadPositionCategoryException

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

the class XbaseTemplateContext method evaluate.

@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    IXtextDocument xDocument = xtextDocumentUtil.getXtextDocument(getDocument());
    // Ensure clean state before evaluation starts
    imports.clear();
    TemplateBuffer resolvedContent = super.evaluate(template);
    Position position = new Position(getCompletionOffset(), 0);
    List<ReplaceRegion> rewrite = createImports(imports, xDocument);
    if (rewrite.size() > 0 && !isReadOnly()) {
        // Remember the completion offset before performing doc changes
        // $NON-NLS-1$
        final String category = "__template_position_import_section" + System.currentTimeMillis();
        IPositionUpdater updater = new DefaultPositionUpdater(category);
        xDocument.addPositionCategory(category);
        xDocument.addPositionUpdater(updater);
        xDocument.addPosition(position);
        try {
            replaceConverter.convertToTextEdit(rewrite).apply(xDocument);
            // restore CompletionOffset
            setCompletionOffset(position.getOffset());
        } catch (BadLocationException e) {
            throw new TemplateException(e);
        } finally {
            xDocument.removePosition(position);
            xDocument.removePositionUpdater(updater);
            try {
                xDocument.removePositionCategory(category);
            } catch (BadPositionCategoryException e) {
                throw new TemplateException(e);
            }
        }
    }
    return resolvedContent;
}
Also used : Position(org.eclipse.jface.text.Position) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TemplateException(org.eclipse.jface.text.templates.TemplateException) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) IPositionUpdater(org.eclipse.jface.text.IPositionUpdater) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 65 with BadPositionCategoryException

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

the class HighlightingPresenter method addPositionFromUI.

/**
 * Add a position with the given range and highlighting unconditionally, only from UI thread. The position will also be registered on
 * the document. The text presentation is not invalidated.
 *
 * @param offset
 *            The range offset
 * @param length
 *            The range length
 * @param highlighting
 *            The highlighting attribute.
 */
private void addPositionFromUI(int offset, int length, TextAttribute highlighting) {
    AttributedPosition position = createHighlightedPosition(offset, length, highlighting);
    synchronized (fPositionLock) {
        insertPosition(position);
    }
    IDocument document = fSourceViewer.getDocument();
    if (document == null)
        return;
    String positionCategory = getPositionCategory();
    try {
        document.addPosition(positionCategory, position);
    } catch (BadLocationException e) {
        // Should not happen
        log.debug(e.getMessage(), e);
    } catch (BadPositionCategoryException e) {
        // Should not happen
        log.debug(e.getMessage(), e);
    }
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)66 Position (org.eclipse.jface.text.Position)46 BadLocationException (org.eclipse.jface.text.BadLocationException)38 TypedPosition (org.eclipse.jface.text.TypedPosition)27 IDocument (org.eclipse.jface.text.IDocument)21 ArrayList (java.util.ArrayList)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)14 TypedRegion (org.eclipse.jface.text.TypedRegion)12 IRegion (org.eclipse.jface.text.IRegion)8 List (java.util.List)6 Point (org.eclipse.swt.graphics.Point)5 IToken (org.eclipse.jface.text.rules.IToken)4 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)3 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)3 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 NoChangeEvent (org.eclipse.wst.sse.core.internal.provisional.events.NoChangeEvent)2 HighlightedPosition (org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition)2 XMLNode (com.amalto.workbench.widgets.xmlviewer.model.XMLNode)1