Search in sources :

Example 86 with Position

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

the class AbstractDecoratedTextEditor method gotoMarker.

/**
 * If the editor can be saved all marker ranges have been changed according to
 * the text manipulations. However, those changes are not yet propagated to the
 * marker manager. Thus, when opening a marker, the marker's position in the editor
 * must be determined as it might differ from the position stated in the marker.
 *
 * @param marker the marker to go to
 * @deprecated visibility will be reduced, use <code>getAdapter(IGotoMarker.class) for accessing this method</code>
 */
@Deprecated
public void gotoMarker(IMarker marker) {
    if (fIsUpdatingMarkerViews)
        return;
    if (getSourceViewer() == null)
        return;
    int start = MarkerUtilities.getCharStart(marker);
    int end = MarkerUtilities.getCharEnd(marker);
    boolean selectLine = start < 0 || end < 0;
    // look up the current range of the marker when the document has been edited
    IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput());
    if (model instanceof AbstractMarkerAnnotationModel) {
        AbstractMarkerAnnotationModel markerModel = (AbstractMarkerAnnotationModel) model;
        Position pos = markerModel.getMarkerPosition(marker);
        if (pos != null && !pos.isDeleted()) {
            // use position instead of marker values
            start = pos.getOffset();
            end = pos.getOffset() + pos.getLength();
        }
        if (pos != null && pos.isDeleted()) {
            // do nothing if position has been deleted
            return;
        }
    }
    IDocument document = getDocumentProvider().getDocument(getEditorInput());
    if (selectLine) {
        int line;
        try {
            if (start >= 0)
                line = document.getLineOfOffset(start);
            else {
                line = MarkerUtilities.getLineNumber(marker);
                // Marker line numbers are 1-based
                --line;
                start = document.getLineOffset(line);
            }
            end = start + document.getLineLength(line) - 1;
        } catch (BadLocationException e) {
            return;
        }
    }
    int length = document.getLength();
    if (end <= length && start <= length) {
        fIsComingFromGotoMarker = true;
        selectAndReveal(start, end - start);
    }
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 87 with Position

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

the class AbstractMarkerAnnotationModel method modifyMarkerAnnotation.

/**
 * Updates the annotation corresponding to the given marker which has changed
 * in some way.
 * <p>
 * Subclasses may override.</p>
 *
 * @param marker the marker
 */
protected void modifyMarkerAnnotation(IMarker marker) {
    MarkerAnnotation a = getMarkerAnnotation(marker);
    if (a != null) {
        Position p = createPositionFromMarker(marker);
        if (p != null) {
            a.update();
            modifyAnnotationPosition(a, p, false);
        }
    } else
        addMarkerAnnotation(marker);
}
Also used : Position(org.eclipse.jface.text.Position)

Example 88 with Position

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

the class AbstractMarkerAnnotationModel method updateMarkers.

/**
 * Updates the markers managed by this annotation model by calling
 * all registered marker updaters (<code>IMarkerUpdater</code>).
 *
 * @param document the document to which this model is currently connected
 * @throws CoreException if there is a problem updating the markers
 */
public void updateMarkers(IDocument document) throws CoreException {
    Assert.isTrue(fDocument == document);
    IAnnotationMap annotationMap = getAnnotationMap();
    if (annotationMap.size() == 0 && fDeletedAnnotations.size() == 0)
        return;
    if (fMarkerUpdaterSpecifications == null)
        installMarkerUpdaters();
    listenToMarkerChanges(false);
    try {
        // update all markers with the positions known by the annotation model
        for (Iterator<Annotation> e = getAnnotationIterator(false); e.hasNext(); ) {
            Object o = e.next();
            if (o instanceof MarkerAnnotation) {
                MarkerAnnotation a = (MarkerAnnotation) o;
                IMarker marker = a.getMarker();
                Position position = annotationMap.get(a);
                if (!updateMarker(marker, document, position)) {
                    if (!fDeletedAnnotations.contains(a))
                        fDeletedAnnotations.add(a);
                }
            }
        }
        if (!fDeletedAnnotations.isEmpty()) {
            removeAnnotations(fDeletedAnnotations, true, true);
            fDeletedAnnotations.clear();
        }
    } finally {
        listenToMarkerChanges(true);
    }
}
Also used : Position(org.eclipse.jface.text.Position) IMarker(org.eclipse.core.resources.IMarker) IAnnotationMap(org.eclipse.jface.text.source.IAnnotationMap) Annotation(org.eclipse.jface.text.source.Annotation)

Example 89 with Position

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

the class AbstractMarkerAnnotationModel method resetMarkers.

/**
 * Resets all the markers to their original state.
 */
public void resetMarkers() {
    // re-initializes the positions from the markers
    for (Iterator<Annotation> e = getAnnotationIterator(false); e.hasNext(); ) {
        Object o = e.next();
        if (o instanceof MarkerAnnotation) {
            MarkerAnnotation a = (MarkerAnnotation) o;
            Position p = createPositionFromMarker(a.getMarker());
            if (p != null) {
                removeAnnotation(a, false);
                try {
                    addAnnotation(a, p, false);
                } catch (BadLocationException e1) {
                // ignore invalid position
                }
            }
        }
    }
    // add the markers of deleted positions back to the annotation model
    for (Iterator<Annotation> e = fDeletedAnnotations.iterator(); e.hasNext(); ) {
        Object o = e.next();
        if (o instanceof MarkerAnnotation) {
            MarkerAnnotation a = (MarkerAnnotation) o;
            Position p = createPositionFromMarker(a.getMarker());
            if (p != null)
                try {
                    addAnnotation(a, p, false);
                } catch (BadLocationException e1) {
                // ignore invalid position
                }
        }
    }
    fDeletedAnnotations.clear();
    // fire annotation model changed
    fireModelChanged();
}
Also used : Position(org.eclipse.jface.text.Position) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 90 with Position

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

the class ResourceMarkerAnnotationModel method batchedUpdate.

/**
 * Updates this model to the given marker deltas.
 *
 * @param markerDeltas the array of marker deltas
 */
private void batchedUpdate(IMarkerDelta[] markerDeltas) {
    HashSet<IMarker> removedMarkers = new HashSet<>(markerDeltas.length);
    HashSet<IMarker> modifiedMarkers = new HashSet<>(markerDeltas.length);
    for (int i = 0; i < markerDeltas.length; i++) {
        IMarkerDelta delta = markerDeltas[i];
        switch(delta.getKind()) {
            case IResourceDelta.ADDED:
                addMarkerAnnotation(delta.getMarker());
                break;
            case IResourceDelta.REMOVED:
                removedMarkers.add(delta.getMarker());
                break;
            case IResourceDelta.CHANGED:
                modifiedMarkers.add(delta.getMarker());
                break;
        }
    }
    if (modifiedMarkers.isEmpty() && removedMarkers.isEmpty())
        return;
    Iterator<Annotation> e = getAnnotationIterator(false);
    while (e.hasNext()) {
        Object o = e.next();
        if (o instanceof MarkerAnnotation) {
            MarkerAnnotation a = (MarkerAnnotation) o;
            IMarker marker = a.getMarker();
            if (removedMarkers.remove(marker))
                removeAnnotation(a, false);
            if (modifiedMarkers.remove(marker)) {
                Position p = createPositionFromMarker(marker);
                if (p != null) {
                    a.update();
                    modifyAnnotationPosition(a, p, false);
                }
            }
            if (modifiedMarkers.isEmpty() && removedMarkers.isEmpty())
                return;
        }
    }
    Iterator<IMarker> iter = modifiedMarkers.iterator();
    while (iter.hasNext()) addMarkerAnnotation(iter.next());
}
Also used : Position(org.eclipse.jface.text.Position) IMarker(org.eclipse.core.resources.IMarker) IMarkerDelta(org.eclipse.core.resources.IMarkerDelta) Annotation(org.eclipse.jface.text.source.Annotation) HashSet(java.util.HashSet)

Aggregations

Position (org.eclipse.jface.text.Position)421 BadLocationException (org.eclipse.jface.text.BadLocationException)145 Annotation (org.eclipse.jface.text.source.Annotation)110 IDocument (org.eclipse.jface.text.IDocument)80 ArrayList (java.util.ArrayList)75 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)57 IRegion (org.eclipse.jface.text.IRegion)55 Test (org.junit.Test)54 HashMap (java.util.HashMap)49 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)46 Point (org.eclipse.swt.graphics.Point)40 List (java.util.List)39 Iterator (java.util.Iterator)31 TypedPosition (org.eclipse.jface.text.TypedPosition)31 Region (org.eclipse.jface.text.Region)27 IFile (org.eclipse.core.resources.IFile)22 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)21 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)21 Map (java.util.Map)15 CoreException (org.eclipse.core.runtime.CoreException)15