Search in sources :

Example 11 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse.platform.text by eclipse.

the class AnnotationModelStressTest method assertAdd.

private void assertAdd(AnnotationData data, ArrayList<AnnotationData> added) {
    Annotation annotation = new Annotation(false);
    Position position = new Position(data.offset, data.length);
    IAnnotationModel model = getModel(data.annotationNumber);
    model.addAnnotation(annotation, position);
    assertTrue(model.getPosition(annotation) == position);
    data.annotation = annotation;
    data.position = position;
    added.add(data);
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 12 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse.platform.text by eclipse.

the class AnnotationModelStressTest method assertRemove.

private void assertRemove(ArrayList<AnnotationData> added) {
    AnnotationData first = added.remove(0);
    IAnnotationModel model = getModel(first.annotationNumber);
    assertTrue(model.getPosition(first.annotation) == first.position);
    model.removeAnnotation(first.annotation);
    assertTrue(model.getPosition(first.annotation) == null);
}
Also used : IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel)

Example 13 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse.platform.text by eclipse.

the class AnnotationModelStressTest method assertExistNew.

private void assertExistNew(ArrayList<AnnotationData> added) {
    for (int i = 0, size = added.size(); i < size; i++) {
        AnnotationData data = added.get(i);
        IAnnotationModel model = getModel(data.annotationNumber);
        assertTrue(model.getPosition(data.annotation) == data.position);
    }
    ArrayList<Annotation> annotations = getAllAnnotationsNew();
    assertEquals(added.size(), annotations.size());
    for (int i = 0, size = annotations.size(); i < size; i++) {
        Annotation annotation = annotations.get(i);
        AnnotationData data = getAnnotationData(added, annotation);
        assertNotNull(data);
        assertTrue(fAnnotationModel.getPosition(annotation) == data.position);
    }
}
Also used : IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 14 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method findAnnotation.

/**
 * Returns the annotation closest to the given range respecting the given
 * direction. If an annotation is found, the annotations current position
 * is copied into the provided annotation position.
 *
 * @param offset the region offset
 * @param length the region length
 * @param forward <code>true</code> for forwards, <code>false</code> for backward
 * @param annotationPosition the position of the found annotation
 * @return the found annotation
 * @since 3.2
 */
protected Annotation findAnnotation(final int offset, final int length, boolean forward, Position annotationPosition) {
    Annotation nextAnnotation = null;
    Position nextAnnotationPosition = null;
    Annotation containingAnnotation = null;
    Position containingAnnotationPosition = null;
    boolean currentAnnotation = false;
    IDocument document = getDocumentProvider().getDocument(getEditorInput());
    int endOfDocument = 0;
    if (document != null) {
        endOfDocument = document.getLength();
    }
    int distance = Integer.MAX_VALUE;
    IAnnotationModel model = getDocumentProvider().getAnnotationModel(getEditorInput());
    Iterator<Annotation> e = model.getAnnotationIterator();
    while (e.hasNext()) {
        Annotation a = e.next();
        if (!isNavigationTarget(a))
            continue;
        Position p = model.getPosition(a);
        if (p == null)
            continue;
        if (forward && p.offset == offset || !forward && p.offset + p.getLength() == offset + length) {
            // || p.includes(offset)) {
            if (containingAnnotation == null || (forward && p.length >= containingAnnotationPosition.length || !forward && p.length >= containingAnnotationPosition.length)) {
                containingAnnotation = a;
                containingAnnotationPosition = p;
                currentAnnotation = p.length == length;
            }
        } else {
            int currentDistance = 0;
            if (forward) {
                currentDistance = p.getOffset() - offset;
                if (currentDistance < 0)
                    currentDistance = endOfDocument + currentDistance;
                if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
                    distance = currentDistance;
                    nextAnnotation = a;
                    nextAnnotationPosition = p;
                }
            } else {
                currentDistance = offset + length - (p.getOffset() + p.length);
                if (currentDistance < 0)
                    currentDistance = endOfDocument + currentDistance;
                if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
                    distance = currentDistance;
                    nextAnnotation = a;
                    nextAnnotationPosition = p;
                }
            }
        }
    }
    if (containingAnnotationPosition != null && (!currentAnnotation || nextAnnotation == null)) {
        annotationPosition.setOffset(containingAnnotationPosition.getOffset());
        annotationPosition.setLength(containingAnnotationPosition.getLength());
        return containingAnnotation;
    }
    if (nextAnnotationPosition != null) {
        annotationPosition.setOffset(nextAnnotationPosition.getOffset());
        annotationPosition.setLength(nextAnnotationPosition.getLength());
    }
    return nextAnnotation;
}
Also used : Position(org.eclipse.jface.text.Position) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point)

Example 15 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel 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)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10