Search in sources :

Example 21 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project linuxtools by eclipse.

the class SpecfileContentProvider method inputChanged.

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    if (oldInput != null) {
        IDocument document = documentProvider.getDocument(oldInput);
        if (document != null) {
            try {
                document.removePositionCategory(SECTION_POSITIONS);
            } catch (BadPositionCategoryException x) {
            }
            document.removePositionUpdater(positionUpdater);
        }
    }
    if (newInput != null) {
        IDocument document = documentProvider.getDocument(newInput);
        if (document != null) {
            document.addPositionCategory(SECTION_POSITIONS);
            document.addPositionUpdater(positionUpdater);
            specfile = new SpecfileParser().parse(document);
        }
    }
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) IDocument(org.eclipse.jface.text.IDocument)

Example 22 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project che by eclipse.

the class DocumentCommand method execute.

/**
	 * Executes the document commands on a document.
	 *
	 * @param document the document on which to execute the commands
	 * @throws BadLocationException in case access to the given document fails
	 * @since 2.1
	 */
void execute(IDocument document) throws BadLocationException {
    if (length == 0 && text == null && fCommands.size() == 0)
        return;
    DefaultPositionUpdater updater = new DefaultPositionUpdater(getCategory());
    Position caretPosition = null;
    try {
        if (updateCaret()) {
            document.addPositionCategory(getCategory());
            document.addPositionUpdater(updater);
            caretPosition = new Position(caretOffset);
            document.addPosition(getCategory(), caretPosition);
        }
        final Command originalCommand = new Command(offset, length, text, owner);
        for (final Iterator iterator = new CommandIterator(fCommands, originalCommand, false); iterator.hasNext(); ) ((Command) iterator.next()).execute(document);
    } catch (BadLocationException e) {
    // ignore
    } catch (BadPositionCategoryException e) {
    // ignore
    } finally {
        if (updateCaret()) {
            document.removePositionUpdater(updater);
            try {
                document.removePositionCategory(getCategory());
            } catch (BadPositionCategoryException e) {
                Assert.isTrue(false);
            }
            caretOffset = caretPosition.getOffset();
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 23 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project che by eclipse.

the class FastPartitioner method initialize.

/**
	 * Performs the initial partitioning of the partitioner's document.
	 * <p>
	 * May be extended by subclasses.
	 * </p>
	 */
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 : TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 24 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project che by eclipse.

the class NonDeletingPositionUpdater method update.

/*
	 * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
	 */
public void update(DocumentEvent event) {
    int eventOffset = event.getOffset();
    int eventOldEndOffset = eventOffset + event.getLength();
    int eventNewLength = event.getText() == null ? 0 : event.getText().length();
    int eventNewEndOffset = eventOffset + eventNewLength;
    int deltaLength = eventNewLength - event.getLength();
    try {
        Position[] positions = event.getDocument().getPositions(fCategory);
        for (int i = 0; i != positions.length; i++) {
            Position position = positions[i];
            if (position.isDeleted())
                continue;
            int offset = position.getOffset();
            int length = position.getLength();
            int end = offset + length;
            if (offset > eventOldEndOffset) {
                // position comes way after change - shift
                position.setOffset(offset + deltaLength);
            } else if (end < eventOffset) {
            // position comes way before change - leave alone
            } else if (offset <= eventOffset && end >= eventOldEndOffset) {
                // event completely internal to the position - adjust length
                position.setLength(length + deltaLength);
            } else if (offset < eventOffset) {
                // event extends over end of position - include the
                // replacement text into the position
                position.setLength(eventNewEndOffset - offset);
            } else if (end > eventOldEndOffset) {
                // event extends from before position into it - adjust
                // offset and length, including the replacement text into
                // the position
                position.setOffset(eventOffset);
                int deleted = eventOldEndOffset - offset;
                position.setLength(length - deleted + eventNewLength);
            } else {
                // event comprises the position - keep it at the same
                // position, but always inside the replacement text
                int newOffset = Math.min(offset, eventNewEndOffset);
                int newEndOffset = Math.min(end, eventNewEndOffset);
                position.setOffset(newOffset);
                position.setLength(newEndOffset - newOffset);
            }
        }
    } catch (BadPositionCategoryException e) {
    // ignore and return
    }
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException)

Example 25 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project che by eclipse.

the class ExclusivePositionUpdater method update.

/*
	 * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
	 */
public void update(DocumentEvent event) {
    int eventOffset = event.getOffset();
    int eventOldLength = event.getLength();
    int eventNewLength = event.getText() == null ? 0 : event.getText().length();
    int deltaLength = eventNewLength - eventOldLength;
    try {
        Position[] positions = event.getDocument().getPositions(fCategory);
        for (int i = 0; i != positions.length; i++) {
            Position position = positions[i];
            if (position.isDeleted())
                continue;
            int offset = position.getOffset();
            int length = position.getLength();
            int end = offset + length;
            if (offset >= eventOffset + eventOldLength)
                // position comes
                // after change - shift
                position.setOffset(offset + deltaLength);
            else if (end <= eventOffset) {
            // position comes way before change -
            // leave alone
            } else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
                // event completely internal to the position - adjust length
                position.setLength(length + deltaLength);
            } else if (offset < eventOffset) {
                // event extends over end of position - adjust length
                int newEnd = eventOffset;
                position.setLength(newEnd - offset);
            } else if (end > eventOffset + eventOldLength) {
                // event extends from before position into it - adjust offset
                // and length
                // offset becomes end of event, length adjusted accordingly
                int newOffset = eventOffset + eventNewLength;
                position.setOffset(newOffset);
                position.setLength(end - newOffset);
            } else {
                // event consumes the position - delete it
                position.delete();
            }
        }
    } catch (BadPositionCategoryException e) {
    // ignore and return
    }
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)42 Position (org.eclipse.jface.text.Position)30 BadLocationException (org.eclipse.jface.text.BadLocationException)22 TypedPosition (org.eclipse.jface.text.TypedPosition)18 IDocument (org.eclipse.jface.text.IDocument)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)10 TypedRegion (org.eclipse.jface.text.TypedRegion)8 ArrayList (java.util.ArrayList)7 IRegion (org.eclipse.jface.text.IRegion)7 Point (org.eclipse.swt.graphics.Point)4 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 List (java.util.List)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1