Package org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer

Examples of org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer.RangeDifference


    if (isSuspended())
      return fLineChangeInfo;

    // try cache first / speeds up linear search
    RangeDifference last= fLastDifference;
    if (last != null && last.rightStart() <= line && last.rightEnd() > line)
      return new DiffRegion(last, line - last.rightStart(), fDifferences, fLeftDocument);

    fLastDifference= getRangeDifferenceForRightLine(line);
    last= fLastDifference;
    if (last != null)
      return new DiffRegion(last, line - last.rightStart(), fDifferences, fLeftDocument);

    return null;
  }
View Full Code Here


    DiffRegion region= (DiffRegion) getLineInfo(line);
    if (region == null || fRightDocument == null || fLeftDocument == null)
      return;

    RangeDifference diff= region.getDifference();
    int rOffset= fRightDocument.getLineOffset(line);
    int rLength= fRightDocument.getLineLength(line);
    int leftLine= diff.leftStart() + region.getOffset();
    String replacement;
    if (leftLine >= diff.leftEnd()) // restoring a deleted line?
      replacement= ""; //$NON-NLS-1$
    else {
      int lOffset= fLeftDocument.getLineOffset(leftLine);
      int lLength= fLeftDocument.getLineLength(leftLine);
      replacement= fLeftDocument.get(lOffset, lLength);
View Full Code Here

    DiffRegion region= (DiffRegion) getLineInfo(line);
    if (region == null || fRightDocument == null || fLeftDocument == null)
      return;

    RangeDifference diff= region.getDifference();
    int rOffset= fRightDocument.getLineOffset(diff.rightStart());
    int rLength= fRightDocument.getLineOffset(diff.rightEnd() - 1) + fRightDocument.getLineLength(diff.rightEnd() - 1) - rOffset;
    int lOffset= fLeftDocument.getLineOffset(diff.leftStart());
    int lLength= fLeftDocument.getLineOffset(diff.leftEnd() - 1) + fLeftDocument.getLineLength(diff.leftEnd() - 1) - lOffset;
    fRightDocument.replace(rOffset, rLength, fLeftDocument.get(lOffset, lLength));
  }
View Full Code Here

    if (fRightDocument == null || fLeftDocument == null)
      return;

    int rOffset= -1, rLength= -1, lOffset= -1, lLength= -1;
    RangeDifference diff= null;
    final List differences= fDifferences;
    synchronized (differences) {
      Iterator it= differences.iterator();

      // get start
      while (it.hasNext()) {
        diff= (RangeDifference) it.next();
        if (line < diff.rightEnd()) {
          rOffset= fRightDocument.getLineOffset(line);
          int leftLine= Math.min(diff.leftStart() + line - diff.rightStart(), diff.leftEnd() - 1);
          lOffset= fLeftDocument.getLineOffset(leftLine);
          break;
        }
      }

      if (rOffset == -1 || lOffset == -1)
        return;

      // get end / length
      int to= line + nLines - 1;
      while (it.hasNext()) {
        diff= (RangeDifference) it.next();
        if (to < diff.rightEnd()) {
          int rEndOffset= fRightDocument.getLineOffset(to) + fRightDocument.getLineLength(to);
          rLength= rEndOffset - rOffset;
          int leftLine= Math.min(diff.leftStart() + to - diff.rightStart(), diff.leftEnd() - 1);
          int lEndOffset= fLeftDocument.getLineOffset(leftLine) + fLeftDocument.getLineLength(leftLine);
          lLength= lEndOffset - lOffset;
          break;
        }
      }
View Full Code Here

      return 0;

    if (region.getRemovedLinesBelow() < 1)
      return 0;

    RangeDifference diff= null;
    final List differences= fDifferences;
    synchronized (differences) {
      for (Iterator it= differences.iterator(); it.hasNext();) {
        diff= (RangeDifference) it.next();
        if (line >= diff.rightStart() && line < diff.rightEnd()) {
          if (diff.kind() == RangeDifference.NOCHANGE && it.hasNext())
            diff= (RangeDifference) it.next();
          break;
        }
      }
    }

    if (diff == null)
      return 0;

    int rOffset= fRightDocument.getLineOffset(diff.rightEnd());
    int rLength= 0;
    int leftLine= diff.leftStart() + diff.rightLength();
    int lOffset= fLeftDocument.getLineOffset(leftLine);
    int lLength= fLeftDocument.getLineOffset(diff.leftEnd() - 1) + fLeftDocument.getLineLength(diff.leftEnd() - 1) - lOffset;
    fRightDocument.replace(rOffset, rLength, fLeftDocument.get(lOffset, lLength));

    return diff.leftLength() - diff.rightLength();
  }
View Full Code Here

    // inform listeners about change
    if (fUpdateNeeded) {
      AnnotationModelEvent ame= new AnnotationModelEvent(this, false);
      for (Iterator it= fAdded.iterator(); it.hasNext(); ) {
        RangeDifference rd= (RangeDifference) it.next();
        ame.annotationAdded(rd.getDiffRegion(fDifferences, fLeftDocument));
      }
      for (Iterator it= fRemoved.iterator(); it.hasNext(); ) {
        RangeDifference rd= (RangeDifference) it.next();
        ame.annotationRemoved(rd.getDiffRegion(fDifferences, fLeftDocument));
      }
      for (Iterator it= fChanged.iterator(); it.hasNext(); ) {
        RangeDifference rd= (RangeDifference) it.next();
        ame.annotationChanged(rd.getDiffRegion(fDifferences, fLeftDocument));
      }
      fireModelChanged(ame);
      fUpdateNeeded= false;
    }
  }
View Full Code Here

    lastLine += repetitionField;


    // get enclosing range: search for a consistent block of at least the size of our
    // change before and after the change.
    final RangeDifference 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 diffs= RangeDifferencer.findRanges(reference, change);
    if (diffs.size() == 0) {
      diffs.add(new RangeDifference(RangeDifference.CHANGE, 0, 0, 0, 0));
    }


    // shift the partial diffs to the absolute document positions
    for (Iterator it= diffs.iterator(); it.hasNext();) {
      RangeDifference d= (RangeDifference) it.next();
      d.shiftLeft(leftStartLine);
      d.shiftRight(rightStartLine);
    }

    // undo optimization shifting
    if (shiftBefore > 0) {
      RangeDifference first= (RangeDifference) diffs.get(0);
      if (first.kind() == RangeDifference.NOCHANGE)
        first.extendStart(-shiftBefore);
      else
        diffs.add(0, new RangeDifference(RangeDifference.NOCHANGE, first.rightStart() - shiftBefore, shiftBefore, first.leftStart() - shiftBefore, shiftBefore));
    }

    RangeDifference last= (RangeDifference) diffs.get(diffs.size() - 1);
    if (shiftAfter > 0) {
      if (last.kind() == RangeDifference.NOCHANGE)
        last.extendEnd(shiftAfter);
      else
        diffs.add(new RangeDifference(RangeDifference.NOCHANGE, last.rightEnd(), shiftAfter , last.leftEnd(), shiftAfter));
    }

    // replace changed diff range
    synchronized (fDifferences) {
      final ListIterator it= fDifferences.listIterator();
      Iterator newIt= diffs.iterator();
      RangeDifference current;
      boolean changed= false;

      // replace regions from consistentBefore to consistentAfter with new diffs

      // search for consistentBefore
      do {
        Assert.isTrue(it.hasNext());
        current= (RangeDifference) it.next();
      } while (current != consistentBefore);
      Assert.isTrue(current == consistentBefore);

      fChanged.clear();
      fRemoved.clear();
      fAdded.clear();

      // replace until consistentAfter
      while (current != consistentAfter) {
        if (newIt.hasNext()) {
          Object 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= (RangeDifference) it.next();
      }

      // replace consistentAfter
      Assert.isTrue(current == consistentAfter);
      if (newIt.hasNext()) {
        Object 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()) {
        Object 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= (RangeDifference) 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;
    }
View Full Code Here

   * @param line the line before which the range has to occur
   * @param size the minimal size of the range
   * @return the first range found, or the first range in the differ if none can be found
   */
  private RangeDifference findConsistentRangeBeforeLeft(int line, int size) {
    RangeDifference found= null;

    for (ListIterator it= fDifferences.listIterator(); it.hasNext();) {
      RangeDifference difference= (RangeDifference) it.next();
      if (found == null || difference.kind() == RangeDifference.NOCHANGE
          && (difference.leftEnd() < line && difference.leftLength() >= size
              || difference.leftEnd() >= line && line - difference.leftStart() >= size))
        found= difference;

      if (difference.leftEnd() >= line)
        break;
    }

    return found;
  }
View Full Code Here

   * @param line the line after which the range has to occur
   * @param size the minimal size of the range
   * @return the first range found, or the last range in the differ if none can be found
   */
  private RangeDifference findConsistentRangeAfterLeft(int line, int size) {
    RangeDifference found= null;

    for (ListIterator it= fDifferences.listIterator(fDifferences.size()); it.hasPrevious();) {
      RangeDifference difference= (RangeDifference) it.previous();
      if (found == null || difference.kind() == RangeDifference.NOCHANGE
          && (difference.leftStart() > line && difference.leftLength() >= size
              || difference.leftStart() <= line && difference.leftEnd() - line >= size))
        found= difference;

      if (difference.leftStart() <= line)
        break;

    }

    return found;
View Full Code Here

TOP

Related Classes of org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer.RangeDifference

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.