Search in sources :

Example 11 with LineRange

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

the class ChangeRegionTest method testHunkCovering.

@Test
public void testHunkCovering() throws Exception {
    ChangeRegion r;
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, 0, 7));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(11, 0, 8));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(11, 0, 9));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, 0, 9));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, -7, 0));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, -8, 0));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(11, -8, 0));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(11, -9, 0));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, -9, 6));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, -4, 8));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, -3, 9));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, -12, 5));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, -12, 20));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, 12, 20));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(8, -3, 8));
    assertTrue(r.getAdjustedRanges().isEmpty());
    RangeUtil.assertEqualRange(new LineRange(12, 0), r.getAdjustedCoverage());
}
Also used : Hunk(org.eclipse.jface.internal.text.revisions.Hunk) ChangeRegion(org.eclipse.jface.internal.text.revisions.ChangeRegion) LineRange(org.eclipse.jface.text.source.LineRange) Test(org.junit.Test)

Example 12 with LineRange

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

the class ChangeRegionTest method testHunkAtStart.

@Test
public void testHunkAtStart() throws Exception {
    ChangeRegion r;
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, 0, 2));
    RangeUtil.assertEqualSingleRange(new LineRange(14, 5), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(14, 5), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, -2, 0));
    RangeUtil.assertEqualSingleRange(new LineRange(12, 5), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(12, 5), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, -2, 1));
    RangeUtil.assertEqualSingleRange(new LineRange(13, 4), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(13, 4), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, 3, 0));
    RangeUtil.assertEqualSingleRange(new LineRange(15, 7), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(15, 7), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 7));
    r.adjustTo(new Hunk(12, 3, 2));
    RangeUtil.assertEqualSingleRange(new LineRange(17, 5), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(17, 5), r.getAdjustedCoverage());
}
Also used : Hunk(org.eclipse.jface.internal.text.revisions.Hunk) ChangeRegion(org.eclipse.jface.internal.text.revisions.ChangeRegion) LineRange(org.eclipse.jface.text.source.LineRange) Test(org.junit.Test)

Example 13 with LineRange

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

the class ChangeRegionTest method testHunkRightBefore.

@Test
public void testHunkRightBefore() throws Exception {
    ChangeRegion r;
    r = new ChangeRegion(fRevision, new LineRange(12, 3));
    r.adjustTo(new Hunk(10, 0, 2));
    RangeUtil.assertEqualSingleRange(new LineRange(12, 3), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(12, 3), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 3));
    r.adjustTo(new Hunk(10, 2, 2));
    RangeUtil.assertEqualSingleRange(new LineRange(14, 3), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(14, 3), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 3));
    r.adjustTo(new Hunk(9, -3, 0));
    RangeUtil.assertEqualSingleRange(new LineRange(9, 3), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(9, 3), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 3));
    r.adjustTo(new Hunk(9, -1, 2));
    RangeUtil.assertEqualSingleRange(new LineRange(11, 3), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(11, 3), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 3));
    r.adjustTo(new Hunk(9, 3, 3));
    RangeUtil.assertEqualSingleRange(new LineRange(15, 3), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(15, 3), r.getAdjustedCoverage());
    r = new ChangeRegion(fRevision, new LineRange(12, 3));
    r.adjustTo(new Hunk(12, 3, 0));
    RangeUtil.assertEqualSingleRange(new LineRange(15, 3), r.getAdjustedRanges());
    RangeUtil.assertEqualRange(new LineRange(15, 3), r.getAdjustedCoverage());
}
Also used : Hunk(org.eclipse.jface.internal.text.revisions.Hunk) ChangeRegion(org.eclipse.jface.internal.text.revisions.ChangeRegion) LineRange(org.eclipse.jface.text.source.LineRange) Test(org.junit.Test)

Example 14 with LineRange

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

the class RangeTest method testLegalOperations.

@Test
public void testLegalOperations() {
    Range r = Range.createRelative(0, 1);
    assertEquals(0, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r = Range.createAbsolute(5, 6);
    assertEquals(5, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r = Range.copy(new LineRange(5, 1));
    assertEquals(5, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r = Range.createRelative(5, 1);
    assertEquals(5, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r.moveBy(10);
    assertEquals(15, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r.moveBy(-8);
    assertEquals(7, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r.moveTo(12);
    assertEquals(12, r.start());
    assertEquals(1, r.length());
    assertConsistency(r);
    r.resizeBy(4);
    assertEquals(12, r.start());
    assertEquals(5, r.length());
    assertConsistency(r);
    r.resizeAndMoveBy(3);
    assertEquals(15, r.start());
    assertEquals(2, r.length());
    assertConsistency(r);
    r.resizeAndMoveBy(-3);
    assertEquals(12, r.start());
    assertEquals(5, r.length());
    assertConsistency(r);
    r.setLength(3);
    assertEquals(12, r.start());
    assertEquals(3, r.length());
    assertConsistency(r);
    r.resizeBy(13);
    assertEquals(12, r.start());
    assertEquals(16, r.length());
    assertConsistency(r);
    r.resizeBy(-4);
    assertEquals(12, r.start());
    assertEquals(12, r.length());
    assertConsistency(r);
    r.setEnd(18);
    assertEquals(12, r.start());
    assertEquals(6, r.length());
    assertConsistency(r);
    r.moveEndTo(13);
    assertEquals(7, r.start());
    assertEquals(6, r.length());
    assertConsistency(r);
    r.setLengthAndMove(3);
    assertEquals(10, r.start());
    assertEquals(3, r.length());
    assertConsistency(r);
    r.setStart(7);
    assertEquals(7, r.start());
    assertEquals(6, r.length());
    assertConsistency(r);
}
Also used : Range(org.eclipse.jface.internal.text.revisions.Range) LineRange(org.eclipse.jface.text.source.LineRange) LineRange(org.eclipse.jface.text.source.LineRange) Test(org.junit.Test)

Example 15 with LineRange

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

the class DocumentLineDiffer method handleChanged.

/**
 * Implementation of documentChanged, non synchronized.
 *
 * @param event the document event
 * @throws BadLocationException if document access fails somewhere
 */
void handleChanged(DocumentEvent event) throws BadLocationException {
    Assert.isTrue(fThread == null || fThread == Thread.currentThread());
    fThread = null;
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=132125
    IDocument left = fLeftDocument;
    DocumentEquivalenceClass leftEquivalent = fLeftEquivalent;
    DocumentEquivalenceClass rightEquivalent = fRightEquivalent;
    if (left == null || leftEquivalent == null || rightEquivalent == null)
        return;
    // documents: left, right; modified and unchanged are either of both
    // TODO two-side
    IDocument right = event.getDocument();
    IDocument modified = event.getDocument();
    if (modified != left && modified != right)
        Assert.isTrue(false);
    boolean leftToRight = modified == left;
    String insertion = event.getText();
    int added = insertion == null ? 1 : modified.computeNumberOfLines(insertion) + 1;
    // put an upper bound to the delay we can afford
    if (added > 50 || fNLines > 50) {
        initialize();
        return;
    }
    int size = Math.max(fNLines, added) + 1;
    int lineDelta = added - fNLines;
    int lastLine = fFirstLine + fNLines - 1;
    int repetitionField;
    if (leftToRight) {
        int originalLine = getRightLine(lastLine + 1);
        repetitionField = searchForRepetitionField(size - 1, right, originalLine);
    } else {
        int originalLine = getLeftLine(lastLine + 1);
        repetitionField = searchForRepetitionField(size - 1, left, originalLine);
    }
    lastLine += repetitionField;
    // get enclosing range: search for a consistent block of at least the size of our
    // change before and after the change.
    final QuickDiffRangeDifference consistentBefore, consistentAfter;
    if (leftToRight) {
        consistentBefore = findConsistentRangeBeforeLeft(fFirstLine, size);
        consistentAfter = findConsistentRangeAfterLeft(lastLine, size);
    } else {
        consistentBefore = findConsistentRangeBeforeRight(fFirstLine, size);
        consistentAfter = findConsistentRangeAfterRight(lastLine, size);
    }
    // optimize unchanged blocks: if the consistent blocks around the change are larger than
    // size, we redimension them (especially important when there are only few changes.
    int shiftBefore = 0;
    if (consistentBefore.kind() == RangeDifference.NOCHANGE) {
        int unchanged;
        if (leftToRight)
            unchanged = Math.min(fFirstLine, consistentBefore.leftEnd()) - consistentBefore.leftStart();
        else
            unchanged = Math.min(fFirstLine, consistentBefore.rightEnd()) - consistentBefore.rightStart();
        shiftBefore = Math.max(0, unchanged - size);
    }
    int shiftAfter = 0;
    if (consistentAfter.kind() == RangeDifference.NOCHANGE) {
        int unchanged;
        if (leftToRight)
            unchanged = consistentAfter.leftEnd() - Math.max(lastLine + 1, consistentAfter.leftStart());
        else
            unchanged = consistentAfter.rightEnd() - Math.max(lastLine + 1, consistentAfter.rightStart());
        shiftAfter = Math.max(0, unchanged - size);
    }
    // get the document regions that will be rediffed, take into account that on the
    // document, the change has already happened.
    // left (reference) document
    int leftStartLine = consistentBefore.leftStart() + shiftBefore;
    int leftLine = consistentAfter.leftEnd();
    if (leftToRight)
        leftLine += lineDelta;
    int leftEndLine = leftLine - shiftAfter;
    ILineRange leftRange = new LineRange(leftStartLine, leftEndLine - leftStartLine);
    IRangeComparator reference = new DocEquivalenceComparator(leftEquivalent, leftRange);
    // right (actual) document
    int rightStartLine = consistentBefore.rightStart() + shiftBefore;
    int rightLine = consistentAfter.rightEnd();
    if (!leftToRight)
        rightLine += lineDelta;
    int rightEndLine = rightLine - shiftAfter;
    ILineRange rightRange = new LineRange(rightStartLine, rightEndLine - rightStartLine);
    IRangeComparator change = new DocEquivalenceComparator(rightEquivalent, rightRange);
    // put an upper bound to the delay we can afford
    if (leftLine - shiftAfter - leftStartLine > 50 || rightLine - shiftAfter - rightStartLine > 50) {
        initialize();
        return;
    }
    // debug
    // System.out.println("compare window: "+size+"\n\n<" + left.get(leftRegion.getOffset(), leftRegion.getLength()) +  //$NON-NLS-1$//$NON-NLS-2$
    // ">\n\n<" + right.get(rightRegion.getOffset(), rightRegion.getLength()) + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$
    // compare
    List<QuickDiffRangeDifference> diffs = asQuickDiffRangeDifference(RangeDifferencer.findRanges(fRangeDiffFactory, null, reference, change));
    if (diffs.size() == 0) {
        diffs.add(new QuickDiffRangeDifference(RangeDifference.CHANGE, 0, 0, 0, 0));
    }
    // shift the partial diffs to the absolute document positions
    for (Iterator<QuickDiffRangeDifference> it = diffs.iterator(); it.hasNext(); ) {
        QuickDiffRangeDifference d = it.next();
        d.shiftLeft(leftStartLine);
        d.shiftRight(rightStartLine);
    }
    // undo optimization shifting
    if (shiftBefore > 0) {
        QuickDiffRangeDifference first = diffs.get(0);
        if (first.kind() == RangeDifference.NOCHANGE)
            first.extendStart(-shiftBefore);
        else
            diffs.add(0, new QuickDiffRangeDifference(RangeDifference.NOCHANGE, first.rightStart() - shiftBefore, shiftBefore, first.leftStart() - shiftBefore, shiftBefore));
    }
    QuickDiffRangeDifference last = diffs.get(diffs.size() - 1);
    if (shiftAfter > 0) {
        if (last.kind() == RangeDifference.NOCHANGE)
            last.extendEnd(shiftAfter);
        else
            diffs.add(new QuickDiffRangeDifference(RangeDifference.NOCHANGE, last.rightEnd(), shiftAfter, last.leftEnd(), shiftAfter));
    }
    // replace changed diff range
    synchronized (fDifferences) {
        final ListIterator<QuickDiffRangeDifference> it = fDifferences.listIterator();
        Iterator<QuickDiffRangeDifference> newIt = diffs.iterator();
        QuickDiffRangeDifference current;
        boolean changed = false;
        // search for consistentBefore
        do {
            Assert.isTrue(it.hasNext());
            current = it.next();
        } while (current != consistentBefore);
        Assert.isTrue(current == consistentBefore);
        fChanged.clear();
        fRemoved.clear();
        fAdded.clear();
        // replace until consistentAfter
        while (current != consistentAfter) {
            if (newIt.hasNext()) {
                QuickDiffRangeDifference o = newIt.next();
                if (!current.equals(o)) {
                    fRemoved.add(current);
                    fAdded.add(o);
                    changed = true;
                    it.set(o);
                }
            } else {
                fRemoved.add(current);
                it.remove();
                changed = true;
            }
            Assert.isTrue(it.hasNext());
            current = it.next();
        }
        // replace consistentAfter
        Assert.isTrue(current == consistentAfter);
        if (newIt.hasNext()) {
            QuickDiffRangeDifference o = newIt.next();
            if (!current.equals(o)) {
                fRemoved.add(current);
                fAdded.add(o);
                changed = true;
                it.set(o);
            }
        } else {
            fRemoved.add(current);
            it.remove();
            changed = true;
        }
        // add remaining new diffs
        while (newIt.hasNext()) {
            QuickDiffRangeDifference next = newIt.next();
            fAdded.add(next);
            it.add(next);
            changed = true;
        }
        // shift the old remaining diffs
        boolean init = true;
        int leftShift = 0;
        int rightShift = 0;
        while (it.hasNext()) {
            current = it.next();
            if (init) {
                init = false;
                leftShift = last.leftEnd() - current.leftStart();
                rightShift = last.rightEnd() - current.rightStart();
                if (leftShift != 0 || rightShift != 0)
                    changed = true;
                else
                    break;
            }
            // fChanged.add(current); // not needed since positional shifting is not handled by an annotation model
            current.shiftLeft(leftShift);
            current.shiftRight(rightShift);
        }
        fUpdateNeeded = changed;
    }
    fLastDifference = null;
}
Also used : DocumentEquivalenceClass(org.eclipse.ui.internal.texteditor.quickdiff.compare.equivalence.DocumentEquivalenceClass) IRangeComparator(org.eclipse.compare.rangedifferencer.IRangeComparator) LineRange(org.eclipse.jface.text.source.LineRange) ILineRange(org.eclipse.jface.text.source.ILineRange) ILineRange(org.eclipse.jface.text.source.ILineRange) DocEquivalenceComparator(org.eclipse.ui.internal.texteditor.quickdiff.compare.equivalence.DocEquivalenceComparator) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

LineRange (org.eclipse.jface.text.source.LineRange)18 Test (org.junit.Test)12 ChangeRegion (org.eclipse.jface.internal.text.revisions.ChangeRegion)11 Hunk (org.eclipse.jface.internal.text.revisions.Hunk)10 ILineRange (org.eclipse.jface.text.source.ILineRange)5 Range (org.eclipse.jface.internal.text.revisions.Range)3 IDocument (org.eclipse.jface.text.IDocument)3 Point (org.eclipse.swt.graphics.Point)3 IRangeComparator (org.eclipse.compare.rangedifferencer.IRangeComparator)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 IRegion (org.eclipse.jface.text.IRegion)1 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)1 RevisionRange (org.eclipse.jface.text.revisions.RevisionRange)1 DocEquivalenceComparator (org.eclipse.ui.internal.texteditor.quickdiff.compare.equivalence.DocEquivalenceComparator)1 DocumentEquivalenceClass (org.eclipse.ui.internal.texteditor.quickdiff.compare.equivalence.DocumentEquivalenceClass)1