Search in sources :

Example 6 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project dbeaver by serge-rider.

the class ExclusivePositionUpdater method update.

@Override
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(category);
        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 renderers to the position - adjust length
                position.setLength(length + deltaLength);
            } else if (offset < eventOffset) {
                // event extends over end of position - adjust length
                position.setLength(eventOffset - offset);
            } else if (end > eventOffset + eventOldLength) {
                // event extends from before position into it - adjust offset
                // and length
                // offset becomes end of event, length ajusted acordingly
                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
    //            _log.debug(EditorMessages.error_badLocationException, e);
    }
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException)

Example 7 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)

Example 8 with BadPositionCategoryException

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

the class JavaContext method rewriteImports.

private void rewriteImports() {
    if (fImportRewrite == null)
        return;
    if (isReadOnly())
        return;
    ICompilationUnit cu = getCompilationUnit();
    if (cu == null)
        return;
    try {
        Position position = new Position(getCompletionOffset(), 0);
        IDocument document = getDocument();
        //$NON-NLS-1$
        final String category = "__template_position_importer" + System.currentTimeMillis();
        IPositionUpdater updater = new DefaultPositionUpdater(category);
        document.addPositionCategory(category);
        document.addPositionUpdater(updater);
        document.addPosition(position);
        try {
            JavaModelUtil.applyEdit(cu, fImportRewrite.rewriteImports(null), false, null);
            setCompletionOffset(position.getOffset());
        } catch (CoreException e) {
            handleException(null, e);
        } finally {
            document.removePosition(position);
            document.removePositionUpdater(updater);
            document.removePositionCategory(category);
        }
    } catch (BadLocationException e) {
        handleException(null, e);
    } catch (BadPositionCategoryException e) {
        handleException(null, e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) Position(org.eclipse.jface.text.Position) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) IPositionUpdater(org.eclipse.jface.text.IPositionUpdater) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 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 10 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)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)11 Position (org.eclipse.jface.text.Position)10 BadLocationException (org.eclipse.jface.text.BadLocationException)6 TypedPosition (org.eclipse.jface.text.TypedPosition)4 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 IDocument (org.eclipse.jface.text.IDocument)2 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)2 IRegion (org.eclipse.jface.text.IRegion)2 ITypedRegion (org.eclipse.jface.text.ITypedRegion)2 TypedRegion (org.eclipse.jface.text.TypedRegion)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1 Map (java.util.Map)1 Set (java.util.Set)1 CoreException (org.eclipse.core.runtime.CoreException)1