Search in sources :

Example 56 with Position

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

the class InclusivePositionUpdaterTest method setUp.

@Before
public void setUp() throws Exception {
    fUpdater = new InclusivePositionUpdater(CATEGORY);
    fDoc = new Document("ccccccccccccccccccccccccccccccccccccccccccccc");
    fPos = new Position(5, 5);
    fDoc.addPositionUpdater(fUpdater);
    fDoc.addPositionCategory(CATEGORY);
    fDoc.addPosition(CATEGORY, fPos);
}
Also used : InclusivePositionUpdater(org.eclipse.jface.text.link.InclusivePositionUpdater) Position(org.eclipse.jface.text.Position) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Before(org.junit.Before)

Example 57 with Position

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

the class LinkedPositionTest method testNotEqualsPosition.

@Test
public void testNotEqualsPosition() {
    LinkedPosition p1 = new LinkedPosition(fDoc, 1, 9);
    Position p2 = new Position(1, 9);
    assertFalse(p1.equals(p2));
}
Also used : LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) Position(org.eclipse.jface.text.Position) Test(org.junit.Test)

Example 58 with Position

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

the class FragmentUpdater method affectsPositions.

/**
 * Returns whether this updater considers any position affected by the given document event. A
 * position is affected if <code>event</code> {@link Position#overlapsWith(int, int) overlaps}
 * with it but not if the position is only shifted.
 *
 * @param event the event
 * @return <code>true</code> if there is any affected position, <code>false</code> otherwise
 */
public boolean affectsPositions(DocumentEvent event) {
    IDocument document = event.getDocument();
    try {
        int index = document.computeIndexInCategory(getCategory(), event.getOffset());
        Position[] fragments = document.getPositions(getCategory());
        if (0 < index) {
            Position fragment = fragments[index - 1];
            if (fragment.overlapsWith(event.getOffset(), event.getLength()))
                return true;
            if (index == fragments.length && fragment.offset + fragment.length == event.getOffset())
                return true;
        }
        if (index < fragments.length) {
            Position fragment = fragments[index];
            return fragment.overlapsWith(event.getOffset(), event.getLength());
        }
    } catch (BadLocationException x) {
    } catch (BadPositionCategoryException x) {
    }
    return false;
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 59 with Position

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

the class ProjectionMapping method toClosestImageLine.

@Override
public int toClosestImageLine(int originLine) throws BadLocationException {
    try {
        int imageLine = toImageLine(originLine);
        if (imageLine > -1)
            return imageLine;
        Position[] fragments = getFragments();
        if (fragments.length == 0)
            return -1;
        IRegion originLineRegion = fMasterDocument.getLineInformation(originLine);
        int index = fMasterDocument.computeIndexInCategory(fFragmentsCategory, originLineRegion.getOffset());
        if (0 < index && index < fragments.length) {
            Fragment left = (Fragment) fragments[index - 1];
            int leftDistance = originLineRegion.getOffset() - (exclusiveEnd(left));
            Fragment right = (Fragment) fragments[index];
            int rightDistance = right.getOffset() - (exclusiveEnd(originLineRegion));
            if (leftDistance <= rightDistance)
                originLine = fMasterDocument.getLineOfOffset(left.getOffset() + Math.max(left.getLength() - 1, 0));
            else
                originLine = fMasterDocument.getLineOfOffset(right.getOffset());
        } else if (index == 0) {
            Fragment right = (Fragment) fragments[index];
            originLine = fMasterDocument.getLineOfOffset(right.getOffset());
        } else if (index == fragments.length) {
            Fragment left = (Fragment) fragments[index - 1];
            originLine = fMasterDocument.getLineOfOffset(exclusiveEnd(left));
        }
        return toImageLine(originLine);
    } catch (BadPositionCategoryException x) {
    }
    return -1;
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IRegion(org.eclipse.jface.text.IRegion)

Example 60 with Position

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

the class ProjectionMapping method getExactCoverage.

@Override
public IRegion[] getExactCoverage(IRegion originRegion) throws BadLocationException {
    int originOffset = originRegion.getOffset();
    int originLength = originRegion.getLength();
    if (originLength == 0) {
        int imageOffset = toImageOffset(originOffset);
        return imageOffset > -1 ? new IRegion[] { new Region(originOffset, 0) } : null;
    }
    int endOffset = originOffset + originLength;
    Position[] fragments = getFragments();
    int firstIndex = findFragmentIndex(originOffset, RIGHT);
    int lastIndex = findFragmentIndex(endOffset - 1, LEFT);
    if (firstIndex == -1 || firstIndex > lastIndex)
        return null;
    int resultLength = lastIndex - firstIndex + 1;
    IRegion[] result = new IRegion[resultLength];
    // first
    result[0] = createOriginStartRegion((Fragment) fragments[firstIndex], originOffset - fragments[firstIndex].getOffset());
    // middles
    for (int i = 1; i < resultLength - 1; i++) result[i] = createOriginRegion((Fragment) fragments[firstIndex + i]);
    // last
    Fragment last = (Fragment) fragments[lastIndex];
    int fragmentEndOffset = exclusiveEnd(last);
    IRegion lastRegion = createOriginEndRegion(last, fragmentEndOffset - endOffset);
    if (resultLength > 1) {
        // first != last
        result[resultLength - 1] = lastRegion;
    } else {
        // merge first and last
        IRegion intersection = getIntersectingRegion(result[0], lastRegion);
        if (intersection == null)
            return null;
        result[0] = intersection;
    }
    return result;
}
Also used : Position(org.eclipse.jface.text.Position) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion)

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