| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.diff.display.internal; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Singleton; |
| 26 |
|
|
| 27 |
|
import org.xwiki.component.annotation.Component; |
| 28 |
|
import org.xwiki.diff.Delta; |
| 29 |
|
import org.xwiki.diff.DiffResult; |
| 30 |
|
import org.xwiki.diff.display.InlineDiffChunk; |
| 31 |
|
import org.xwiki.diff.display.InlineDiffChunk.Type; |
| 32 |
|
import org.xwiki.diff.display.InlineDiffDisplayer; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
@link |
| 36 |
|
|
| 37 |
|
@version |
| 38 |
|
@since |
| 39 |
|
|
| 40 |
|
@Component |
| 41 |
|
@Singleton |
| |
|
| 94.3% |
Uncovered Elements: 2 (35) |
Complexity: 8 |
Complexity Density: 0.31 |
|
| 42 |
|
public class DefaultInlineDiffDisplayer implements InlineDiffDisplayer |
| 43 |
|
{ |
| |
|
| 94.1% |
Uncovered Elements: 2 (34) |
Complexity: 8 |
Complexity Density: 0.31 |
|
| 44 |
25 |
@Override... |
| 45 |
|
public <E> List<InlineDiffChunk<E>> display(DiffResult<E> diffResult) |
| 46 |
|
{ |
| 47 |
25 |
List<E> previous = diffResult.getPrevious(); |
| 48 |
25 |
List<InlineDiffChunk<E>> chunks = new ArrayList<InlineDiffChunk<E>>(); |
| 49 |
|
|
| 50 |
25 |
Delta<E> lastDelta = null; |
| 51 |
25 |
for (Delta<E> delta : diffResult.getPatch()) { |
| 52 |
|
|
| 53 |
33 |
int contextStart = lastDelta == null ? 0 : lastDelta.getPrevious().getLastIndex() + 1; |
| 54 |
33 |
int contextEnd = delta.getPrevious().getIndex(); |
| 55 |
33 |
if (contextStart < contextEnd) { |
| 56 |
27 |
chunks.add(new InlineDiffChunk<E>(Type.UNMODIFIED, previous.subList(contextStart, contextEnd))); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
|
| 60 |
33 |
switch (delta.getType()) { |
| 61 |
12 |
case CHANGE: |
| 62 |
12 |
chunks.add(new InlineDiffChunk<E>(Type.DELETED, delta.getPrevious().getElements())); |
| 63 |
12 |
chunks.add(new InlineDiffChunk<E>(Type.ADDED, delta.getNext().getElements())); |
| 64 |
12 |
break; |
| 65 |
6 |
case DELETE: |
| 66 |
6 |
chunks.add(new InlineDiffChunk<E>(Type.DELETED, delta.getPrevious().getElements())); |
| 67 |
6 |
break; |
| 68 |
15 |
case INSERT: |
| 69 |
15 |
chunks.add(new InlineDiffChunk<E>(Type.ADDED, delta.getNext().getElements())); |
| 70 |
15 |
break; |
| 71 |
0 |
default: |
| 72 |
0 |
break; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
33 |
lastDelta = delta; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
|
| 79 |
25 |
int contextStart = lastDelta == null ? 0 : lastDelta.getPrevious().getLastIndex() + 1; |
| 80 |
25 |
if (contextStart < previous.size()) { |
| 81 |
13 |
chunks.add(new InlineDiffChunk<E>(Type.UNMODIFIED, previous.subList(contextStart, previous.size()))); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
25 |
return chunks; |
| 85 |
|
} |
| 86 |
|
} |