Search in sources :

Example 31 with BadPositionCategoryException

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

the class ProjectionDocument method internalRemoveMasterDocumentRange.

/**
 * Removes the given range of the master document from this projection
 * document.
 *
 * @param offsetInMaster the offset of the range in the master document
 * @param lengthInMaster the length of the range in the master document
 *
 * @throws BadLocationException if the given range is not valid in the
 *             master document
 * @throws IllegalArgumentException if the given range is not projected in
 *             this projection document or is not completely comprised by
 *             an existing fragment
 */
private void internalRemoveMasterDocumentRange(int offsetInMaster, int lengthInMaster) throws BadLocationException {
    try {
        IRegion imageRegion = fMapping.toExactImageRegion(new Region(offsetInMaster, lengthInMaster));
        if (imageRegion == null)
            throw new IllegalArgumentException();
        Fragment fragment = findFragment(offsetInMaster, lengthInMaster);
        if (fragment == null)
            throw new IllegalArgumentException();
        // $NON-NLS-1$
        ProjectionDocumentEvent event = new ProjectionDocumentEvent(this, imageRegion.getOffset(), imageRegion.getLength(), "", offsetInMaster, lengthInMaster);
        super.fireDocumentAboutToBeChanged(event);
        if (fragment.getOffset() == offsetInMaster) {
            fragment.setOffset(offsetInMaster + lengthInMaster);
            fragment.setLength(fragment.getLength() - lengthInMaster);
        } else {
            // split fragment into three fragments, let position updater remove it
            // add fragment for the region to be removed
            Fragment newFragment = new Fragment(offsetInMaster, lengthInMaster);
            Segment segment = new Segment(imageRegion.getOffset(), imageRegion.getLength());
            newFragment.segment = segment;
            segment.fragment = newFragment;
            fMasterDocument.addPosition(fFragmentsCategory, newFragment);
            addPosition(fSegmentsCategory, segment);
            // add fragment for the remainder right of the deleted range in the original fragment
            int offset = offsetInMaster + lengthInMaster;
            newFragment = new Fragment(offset, fragment.getOffset() + fragment.getLength() - offset);
            offset = imageRegion.getOffset() + imageRegion.getLength();
            segment = new Segment(offset, fragment.segment.getOffset() + fragment.segment.getLength() - offset);
            newFragment.segment = segment;
            segment.fragment = newFragment;
            fMasterDocument.addPosition(fFragmentsCategory, newFragment);
            addPosition(fSegmentsCategory, segment);
            // adjust length of initial fragment (the left one)
            fragment.setLength(offsetInMaster - fragment.getOffset());
            fragment.segment.setLength(imageRegion.getOffset() - fragment.segment.getOffset());
        }
        getTracker().replace(event.getOffset(), event.getLength(), event.getText());
        super.fireDocumentChanged(event);
    } catch (BadPositionCategoryException x) {
        internalError();
    }
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 32 with BadPositionCategoryException

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

the class InclusivePositionUpdater 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(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 way
                // 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 + eventNewLength;
                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
                // we want to recycle the overlapping part
                position.setOffset(eventOffset);
                int deleted = eventOffset + eventOldLength - offset;
                position.setLength(length - deleted + eventNewLength);
            } 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 33 with BadPositionCategoryException

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

the class LinkedModeModel method exit.

/**
 * Causes this model to exit. Called either if an illegal document change
 * is detected, or by the UI.
 *
 * @param flags the exit flags as defined in {@link ILinkedModeListener}
 */
public void exit(int flags) {
    if (!fIsActive)
        return;
    fIsActive = false;
    for (IDocument doc : fDocuments) {
        try {
            doc.removePositionCategory(getCategory());
        } catch (BadPositionCategoryException e) {
            // won't happen
            Assert.isTrue(false);
        }
        doc.removePositionUpdater(fUpdater);
        doc.removeDocumentListener(fDocumentListener);
    }
    fDocuments.clear();
    fGroups.clear();
    List<ILinkedModeListener> listeners = new ArrayList<>(fListeners);
    fListeners.clear();
    for (ILinkedModeListener listener : listeners) {
        listener.left(this, flags);
    }
    if (fParentEnvironment != null)
        fParentEnvironment.resume(flags);
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ArrayList(java.util.ArrayList) IDocument(org.eclipse.jface.text.IDocument)

Example 34 with BadPositionCategoryException

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

the class NonDeletingPositionUpdater method update.

@Override
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 35 with BadPositionCategoryException

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

the class LinkedModeUI method leave.

void leave(final int flags) {
    if (!fIsActive)
        return;
    fIsActive = false;
    endCompoundChangeIfNeeded();
    Display display = null;
    if (fCurrentTarget.fWidget != null && !fCurrentTarget.fWidget.isDisposed())
        display = fCurrentTarget.fWidget.getDisplay();
    if (fCurrentTarget.fAnnotationModel != null)
        fCurrentTarget.fAnnotationModel.removeAllAnnotations();
    disconnect();
    for (LinkedModeUITarget fTarget : fTargets) {
        LinkedModeUITarget target = fTarget;
        ITextViewer viewer = target.getViewer();
        if (target.fKeyListener != null) {
            ((ITextViewerExtension) viewer).removeVerifyKeyListener(target.fKeyListener);
            target.fKeyListener = null;
        }
        viewer.removeTextInputListener(fCloser);
    }
    for (int i = 0; i < fTargets.length; i++) {
        if (fTargets[i].fAnnotationModel != null) {
            fTargets[i].fAnnotationModel.removeAllAnnotations();
            fTargets[i].fAnnotationModel.disconnect(fTargets[i].getViewer().getDocument());
            fTargets[i].fAnnotationModel = null;
        }
        uninstallAnnotationModel(fTargets[i]);
    }
    if ((flags & ILinkedModeListener.UPDATE_CARET) != 0 && fExitPosition != null && fFramePosition != fExitPosition && !fExitPosition.isDeleted())
        switchPosition(fExitPosition, true, false);
    final List<IDocument> docs = new ArrayList<>();
    for (LinkedModeUITarget fTarget : fTargets) {
        IDocument doc = fTarget.getViewer().getDocument();
        if (doc != null)
            docs.add(doc);
    }
    fModel.stopForwarding(flags);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (fExitPosition != null)
                fExitPosition.getDocument().removePosition(fExitPosition);
            for (IDocument doc : docs) {
                doc.removePositionUpdater(fPositionUpdater);
                boolean uninstallCat = false;
                String[] cats = doc.getPositionCategories();
                for (String cat : cats) {
                    if (getCategory().equals(cat)) {
                        uninstallCat = true;
                        break;
                    }
                }
                if (uninstallCat)
                    try {
                        doc.removePositionCategory(getCategory());
                    } catch (BadPositionCategoryException e) {
                    // ignore
                    }
            }
            fModel.exit(flags);
        }
    };
    // gets correct document offsets.
    if (display != null)
        display.asyncExec(runnable);
    else
        runnable.run();
}
Also used : ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.jface.text.ITextViewer) ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) Display(org.eclipse.swt.widgets.Display)

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