|
15 | 15 | */
|
16 | 16 | package difflib;
|
17 | 17 |
|
18 |
| -import java.util.*; |
| 18 | +import difflib.myers.MyersDiff; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.List; |
19 | 24 | import java.util.regex.Matcher;
|
20 | 25 | import java.util.regex.Pattern;
|
21 | 26 |
|
22 |
| -import difflib.myers.*; |
23 |
| - |
24 | 27 | /**
|
25 | 28 | * Implements the difference and patching engine
|
26 | 29 | *
|
@@ -140,6 +143,8 @@ public static Patch parseUnifiedDiff(List<String> diff) {
|
140 | 143 | if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
|
141 | 144 | rawChunk.add(new Object[] { tag, rest });
|
142 | 145 | }
|
| 146 | + } else { |
| 147 | + rawChunk.add(new Object[] {" ", ""}); |
143 | 148 | }
|
144 | 149 | }
|
145 | 150 | }
|
@@ -184,47 +189,48 @@ public static List<String> generateUnifiedDiff(String original, String revised,
|
184 | 189 | List<String> ret = new ArrayList<String>();
|
185 | 190 | ret.add("--- " + original);
|
186 | 191 | ret.add("+++ " + revised);
|
187 |
| - |
188 |
| - // Hmm, I thought the Deltas were sorted already... turns out they're not. |
189 |
| - List<Delta> patchDeltas = new ArrayList<Delta>( patch.getDeltas() ); |
190 |
| - Collections.sort( patchDeltas, new Comparator<Delta>() { |
191 |
| - public int compare( Delta a, Delta b ) { |
192 |
| - return new Integer(a.getOriginal().getPosition()).compareTo( b.getOriginal().getPosition() ); |
193 |
| - } |
194 |
| - }); |
195 |
| - |
196 |
| - // code outside the if block also works for single-delta issues. |
197 |
| - List<Delta> deltas = new ArrayList<Delta>(); // current list of Delta's to process |
198 |
| - Delta delta = patchDeltas.get(0); |
199 |
| - deltas.add(delta); // add the first Delta to the current set |
200 |
| - // if there's more than 1 Delta, we may need to output them together |
201 |
| - if (patchDeltas.size() > 1) { |
202 |
| - for (int i = 1; i < patchDeltas.size(); i++) { |
203 |
| - int position = delta.getOriginal().getPosition(); // store the current position of |
204 |
| - // the first Delta |
205 |
| - |
206 |
| - // Check if the next Delta is too close to the current position. |
207 |
| - // And if it is, add it to the current set |
208 |
| - Delta nextDelta = patchDeltas.get(i); |
209 |
| - if ((position + delta.getOriginal().getSize() + contextSize) >= |
210 |
| - (nextDelta.getOriginal().getPosition() - contextSize)) { |
211 |
| - deltas.add(nextDelta); |
212 |
| - } else { |
213 |
| - // if it isn't, output the current set, |
214 |
| - // then create a new set and add the current Delta to it. |
215 |
| - List<String> curBlock = processDeltas(originalLines, deltas, contextSize); |
216 |
| - ret.addAll(curBlock); |
217 |
| - deltas.clear(); |
218 |
| - deltas.add(nextDelta); |
| 192 | + |
| 193 | + if (!patch.getDeltas().isEmpty()) { |
| 194 | + // Hmm, I thought the Deltas were sorted already... turns out they're not. |
| 195 | + List<Delta> patchDeltas = new ArrayList<Delta>( patch.getDeltas() ); |
| 196 | + Collections.sort( patchDeltas, new Comparator<Delta>() { |
| 197 | + public int compare( Delta a, Delta b ) { |
| 198 | + return new Integer(a.getOriginal().getPosition()).compareTo( b.getOriginal().getPosition() ); |
219 | 199 | }
|
220 |
| - delta = nextDelta; |
| 200 | + }); |
| 201 | + |
| 202 | + // code outside the if block also works for single-delta issues. |
| 203 | + List<Delta> deltas = new ArrayList<Delta>(); // current list of Delta's to process |
| 204 | + Delta delta = patchDeltas.get(0); |
| 205 | + deltas.add(delta); // add the first Delta to the current set |
| 206 | + // if there's more than 1 Delta, we may need to output them together |
| 207 | + if (patchDeltas.size() > 1) { |
| 208 | + for (int i = 1; i < patchDeltas.size(); i++) { |
| 209 | + int position = delta.getOriginal().getPosition(); // store the current position of |
| 210 | + // the first Delta |
| 211 | + |
| 212 | + // Check if the next Delta is too close to the current position. |
| 213 | + // And if it is, add it to the current set |
| 214 | + Delta nextDelta = patchDeltas.get(i); |
| 215 | + if ((position + delta.getOriginal().getSize() + contextSize) >= |
| 216 | + (nextDelta.getOriginal().getPosition() - contextSize)) { |
| 217 | + deltas.add(nextDelta); |
| 218 | + } else { |
| 219 | + // if it isn't, output the current set, |
| 220 | + // then create a new set and add the current Delta to it. |
| 221 | + List<String> curBlock = processDeltas(originalLines, deltas, contextSize); |
| 222 | + ret.addAll(curBlock); |
| 223 | + deltas.clear(); |
| 224 | + deltas.add(nextDelta); |
| 225 | + } |
| 226 | + delta = nextDelta; |
| 227 | + } |
| 228 | + |
221 | 229 | }
|
222 |
| - |
| 230 | + // don't forget to process the last set of Deltas |
| 231 | + List<String> curBlock = processDeltas(originalLines, deltas, contextSize); |
| 232 | + ret.addAll(curBlock); |
223 | 233 | }
|
224 |
| - // don't forget to process the last set of Deltas |
225 |
| - List<String> curBlock = processDeltas(originalLines, deltas, contextSize); |
226 |
| - ret.addAll(curBlock); |
227 |
| - |
228 | 234 | return ret;
|
229 | 235 | }
|
230 | 236 |
|
|
0 commit comments