Search in sources :

Example 81 with Position

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

the class ChildDocumentTest method testFindPositions.

@Test
public void testFindPositions() {
    try {
        fDocument.addPosition(new Position(21, 13));
        fDocument.addPosition(new Position(0, 19));
        fDocument.addPosition(new Position(21, 14));
        fDocument.addPosition(new Position(21, 16));
        fDocument.addPosition(new Position(0, 0));
        fDocument.addPosition(new Position(104, 1));
        fDocument.addPosition(new Position(120, 1));
        fDocument.addPosition(new Position(119, 1));
    } catch (BadLocationException x) {
        assertTrue("initilization failed", false);
    }
    Position[] positions = new Position[] { new Position(0, 0), new Position(0, 19), new Position(0, 20), new Position(21, 16), new Position(21, 14), new Position(21, 13), new Position(21, 15), new Position(38, 111), new Position(61, 12), new Position(75, 27), new Position(104, 1), new Position(105, 12), new Position(119, 1), new Position(119, 27), new Position(120, 1) };
    checkPositions(positions);
}
Also used : Position(org.eclipse.jface.text.Position) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 82 with Position

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

the class ChildDocumentTest method testEditScript1.

@Test
public void testEditScript1() {
    try {
        fDocument.replace(0, fDocument.getLength(), "");
    } catch (BadLocationException x) {
        assertTrue("BadLocationException thrown", false);
    }
    Position[] positions = new Position[] { new Position(0, 0) };
    checkPositions(positions);
    // 2. step
    try {
        fDocument.replace(0, 0, "\t");
    } catch (BadLocationException x) {
        assertTrue("BadLocationException thrown", false);
    }
    positions = new Position[] { new Position(1, 0) };
    checkPositions(positions);
}
Also used : Position(org.eclipse.jface.text.Position) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 83 with Position

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

the class DocumentTest method testFindPositions.

@Test
public void testFindPositions() {
    try {
        fDocument.addPosition(new Position(21, 13));
        fDocument.addPosition(new Position(0, 19));
        fDocument.addPosition(new Position(21, 14));
        fDocument.addPosition(new Position(21, 16));
        fDocument.addPosition(new Position(0, 0));
        fDocument.addPosition(new Position(104, 1));
        fDocument.addPosition(new Position(120, 1));
        fDocument.addPosition(new Position(119, 1));
    } catch (BadLocationException x) {
        assertTrue("initilization failed", false);
    }
    Position[] positions = new Position[] { new Position(0, 0), new Position(0, 19), new Position(0, 20), new Position(21, 16), new Position(21, 14), new Position(21, 13), new Position(21, 15), new Position(38, 111), new Position(61, 12), new Position(75, 27), new Position(104, 1), new Position(105, 12), new Position(119, 1), new Position(119, 27), new Position(120, 1) };
    checkPositions(positions);
}
Also used : Position(org.eclipse.jface.text.Position) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 84 with Position

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

the class DocumentTest method testEditScript1.

@Test
public void testEditScript1() {
    try {
        fDocument.replace(0, fDocument.getLength(), "");
    } catch (BadLocationException x) {
        assertTrue("BadLocationException thrown", false);
    }
    Position[] positions = new Position[] { new Position(0, 0) };
    checkPositions(positions);
    // 2. step
    try {
        fDocument.replace(0, 0, "\t");
    } catch (BadLocationException x) {
        assertTrue("BadLocationException thrown", false);
    }
    positions = new Position[] { new Position(1, 0) };
    checkPositions(positions);
}
Also used : Position(org.eclipse.jface.text.Position) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 85 with Position

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

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