1 11 package org.eclipse.jface.internal.text.revisions; 12 13 import java.util.LinkedList ; 14 import java.util.List ; 15 import java.util.ListIterator ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.jface.text.revisions.Revision; 20 import org.eclipse.jface.text.source.ILineRange; 21 import org.eclipse.jface.text.source.LineRange; 22 23 30 public final class ChangeRegion { 31 private final Revision fRevision; 32 private final ILineRange fLines; 33 private final List fAdjusted= new LinkedList (); 34 35 42 public ChangeRegion(Revision revision, ILineRange lines) throws IndexOutOfBoundsException { 43 Assert.isLegal(revision != null); 44 Assert.isLegal(lines != null); 45 fLines= Range.copy(lines); 46 fRevision=revision; 47 clearDiff(); 48 } 49 50 55 public Revision getRevision() { 56 return fRevision; 57 } 58 59 64 public ILineRange getOriginalRange() { 65 return fLines; 66 } 67 68 74 public List getAdjustedRanges() { 75 return fAdjusted; 76 } 77 78 83 public ILineRange getAdjustedCoverage() { 84 if (fAdjusted.isEmpty()) 85 return new LineRange(fLines.getStartLine(), 0); 86 87 Range first= (Range) fAdjusted.get(0); 88 Range last= (Range) fAdjusted.get(fAdjusted.size() - 1); 89 90 return Range.createAbsolute(first.start(), last.end()); 91 } 92 93 96 public void clearDiff() { 97 fAdjusted.clear(); 98 fAdjusted.add(Range.copy(fLines)); 99 } 100 101 106 public void adjustTo(Hunk hunk) { 107 for (ListIterator it= fAdjusted.listIterator(); it.hasNext();) { 108 Range range= (Range) it.next(); 109 110 int unchanged= getUnchanged(hunk, range.start()); 112 if (unchanged > 0) { 113 if (unchanged >= range.length()) 114 continue; 115 range= range.split(unchanged); 116 it.add(range); 117 it.previous(); it.next(); } 119 120 int line= range.start(); 121 Assert.isTrue(hunk.line <= line); 122 123 int overlap= getOverlap(hunk, line); 125 if (overlap >= range.length()) { 126 it.remove(); 127 continue; 128 } 129 130 range.moveBy(hunk.delta + overlap); 132 range.resizeBy(-overlap); 133 } 134 135 } 136 137 private int getUnchanged(Hunk hunk, int line) { 138 return Math.max(0, hunk.line - line); 139 } 140 141 144 private int getOverlap(Hunk hunk, int line) { 145 146 int deltaLine= hunk.line + hunk.changed; 147 if (hunk.delta >= 0) { 148 if (deltaLine <= line) 149 return 0; 150 return deltaLine - line; 151 } 152 153 int hunkEnd= deltaLine - hunk.delta; 155 int cutCount= hunkEnd - line; 156 return Math.max(0, cutCount); 157 } 158 159 162 public String toString() { 163 return "ChangeRegion [" + fRevision.toString() + ", [" + fLines.getStartLine() + "+" + fLines.getNumberOfLines() + ")]"; } 165 } 166 | Popular Tags |