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 |
|
import java.util.Stack; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.diff.Chunk; |
31 |
|
import org.xwiki.diff.Delta; |
32 |
|
import org.xwiki.diff.DiffException; |
33 |
|
import org.xwiki.diff.DiffManager; |
34 |
|
import org.xwiki.diff.DiffResult; |
35 |
|
import org.xwiki.diff.display.InlineDiffChunk; |
36 |
|
import org.xwiki.diff.display.InlineDiffDisplayer; |
37 |
|
import org.xwiki.diff.display.UnifiedDiffBlock; |
38 |
|
import org.xwiki.diff.display.UnifiedDiffConfiguration; |
39 |
|
import org.xwiki.diff.display.UnifiedDiffDisplayer; |
40 |
|
import org.xwiki.diff.display.UnifiedDiffElement; |
41 |
|
import org.xwiki.diff.display.UnifiedDiffElement.Type; |
42 |
|
|
43 |
|
|
44 |
|
@link |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Singleton |
|
|
| 97.7% |
Uncovered Elements: 2 (88) |
Complexity: 24 |
Complexity Density: 0.39 |
|
58 |
|
public class DefaultUnifiedDiffDisplayer implements UnifiedDiffDisplayer |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
@param |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 5 |
Complexity Density: 1 |
|
66 |
|
private static class State<E, F> |
67 |
|
{ |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private final Stack<UnifiedDiffBlock<E, F>> blocks = new Stack<UnifiedDiffBlock<E, F>>(); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private final List<E> previous; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private Delta<E> lastDelta; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@param |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
23 |
State(List<E> previous)... |
89 |
|
{ |
90 |
23 |
this.previous = previous; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
@return |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
77 |
public Delta<E> getLastDelta()... |
97 |
|
{ |
98 |
77 |
return this.lastDelta; |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@param |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
24 |
public void setLastDelta(Delta<E> lastDelta)... |
107 |
|
{ |
108 |
24 |
this.lastDelta = lastDelta; |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
@return |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
181 |
public Stack<UnifiedDiffBlock<E, F>> getBlocks()... |
115 |
|
{ |
116 |
181 |
return this.blocks; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
@return |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
66 |
public List<E> getPrevious()... |
123 |
|
{ |
124 |
66 |
return this.previous; |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@Inject |
132 |
|
private DiffManager diffManager; |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
@Inject |
138 |
|
private InlineDiffDisplayer inlineDisplayer; |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
23 |
@Override... |
141 |
|
public <E, F> UnifiedDiffConfiguration<E, F> getDefaultConfiguration() |
142 |
|
{ |
143 |
23 |
return new UnifiedDiffConfiguration<E, F>(); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
9 |
@Override... |
147 |
|
public <E, F> List<UnifiedDiffBlock<E, F>> display(DiffResult<E> diffResult) |
148 |
|
{ |
149 |
9 |
return display(diffResult, this.<E, F>getDefaultConfiguration()); |
150 |
|
} |
151 |
|
|
|
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 4 |
Complexity Density: 0.22 |
|
152 |
23 |
@Override... |
153 |
|
public <E, F> List<UnifiedDiffBlock<E, F>> display(DiffResult<E> diffResult, UnifiedDiffConfiguration<E, F> config) |
154 |
|
{ |
155 |
23 |
State<E, F> state = new State<E, F>(diffResult.getPrevious()); |
156 |
|
|
157 |
23 |
for (Delta<E> delta : diffResult.getPatch()) { |
158 |
|
|
159 |
|
|
160 |
24 |
maybeStartBlock(delta, state, config.getContextSize()); |
161 |
|
|
162 |
|
|
163 |
24 |
switch (delta.getType()) { |
164 |
17 |
case CHANGE: |
165 |
17 |
state.getBlocks().peek().addAll(this.<E, F>getModifiedElements(delta, config)); |
166 |
17 |
break; |
167 |
3 |
case DELETE: |
168 |
3 |
state.getBlocks().peek().addAll(this.<E, F>getElements(delta.getPrevious(), Type.DELETED)); |
169 |
3 |
break; |
170 |
4 |
case INSERT: |
171 |
4 |
state.getBlocks().peek().addAll(this.<E, F>getElements(delta.getNext(), Type.ADDED)); |
172 |
4 |
break; |
173 |
0 |
default: |
174 |
0 |
break; |
175 |
|
} |
176 |
|
|
177 |
24 |
state.setLastDelta(delta); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
23 |
maybeEndBlock(state, config.getContextSize()); |
182 |
|
|
183 |
23 |
return state.getBlocks(); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
@link |
188 |
|
|
189 |
|
|
190 |
|
@param |
191 |
|
@param |
192 |
|
@param |
193 |
|
@param |
194 |
|
@param |
195 |
|
|
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
197 |
24 |
private <E, F> void maybeStartBlock(Delta<E> delta, State<E, F> state, int contextSize)... |
198 |
|
{ |
199 |
24 |
if (state.getLastDelta() == null |
200 |
|
|| state.getLastDelta().getPrevious().getLastIndex() < delta.getPrevious().getIndex() - contextSize * 2) { |
201 |
21 |
maybeEndBlock(state, contextSize); |
202 |
21 |
state.getBlocks().push(new UnifiedDiffBlock<E, F>()); |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
24 |
int count = state.getBlocks().peek().isEmpty() ? contextSize : contextSize * 2; |
207 |
24 |
int lastChangeIndex = state.getLastDelta() == null ? -1 : state.getLastDelta().getPrevious().getLastIndex(); |
208 |
24 |
int end = delta.getPrevious().getIndex(); |
209 |
24 |
int start = Math.max(end - count, lastChangeIndex + 1); |
210 |
24 |
state.getBlocks().peek().addAll(this.<E, F>getUnmodifiedElements(state.getPrevious(), start, end)); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
@param |
221 |
|
@param |
222 |
|
@param |
223 |
|
@param |
224 |
|
|
225 |
|
@return |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
227 |
17 |
private <E, F> List<UnifiedDiffElement<E, F>> getModifiedElements(Delta<E> delta,... |
228 |
|
UnifiedDiffConfiguration<E, F> config) |
229 |
|
{ |
230 |
17 |
List<UnifiedDiffElement<E, F>> elements = new ArrayList<UnifiedDiffElement<E, F>>(); |
231 |
17 |
elements.addAll(this.<E, F>getElements(delta.getPrevious(), Type.DELETED)); |
232 |
17 |
elements.addAll(this.<E, F>getElements(delta.getNext(), Type.ADDED)); |
233 |
|
|
234 |
|
|
235 |
17 |
if (config.getSplitter() != null && delta.getPrevious().size() == delta.getNext().size()) { |
236 |
11 |
int changeSize = delta.getPrevious().size(); |
237 |
24 |
for (int i = 0; i < changeSize; i++) { |
238 |
13 |
displayInlineDiff(elements.get(i), elements.get(changeSize + i), config); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
17 |
return elements; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
@param |
247 |
|
@param |
248 |
|
@param |
249 |
|
@param |
250 |
|
|
251 |
|
@return |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
253 |
41 |
private <E, F> List<UnifiedDiffElement<E, F>> getElements(Chunk<E> chunk, Type changeType)... |
254 |
|
{ |
255 |
41 |
int index = chunk.getIndex(); |
256 |
41 |
List<UnifiedDiffElement<E, F>> elements = new ArrayList<UnifiedDiffElement<E, F>>(); |
257 |
41 |
for (E element : chunk.getElements()) { |
258 |
52 |
elements.add(new UnifiedDiffElement<E, F>(index++, changeType, element)); |
259 |
|
} |
260 |
41 |
return elements; |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
@param |
265 |
|
@param |
266 |
|
@param |
267 |
|
@param |
268 |
|
@param |
269 |
|
|
270 |
|
@return |
271 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
272 |
45 |
private <E, F> List<UnifiedDiffElement<E, F>> getUnmodifiedElements(List<E> previous, int start, int end)... |
273 |
|
{ |
274 |
45 |
List<UnifiedDiffElement<E, F>> unmodifiedElements = new ArrayList<UnifiedDiffElement<E, F>>(); |
275 |
97 |
for (int i = start; i < end; i++) { |
276 |
52 |
unmodifiedElements.add(new UnifiedDiffElement<E, F>(i, Type.CONTEXT, previous.get(i))); |
277 |
|
} |
278 |
45 |
return unmodifiedElements; |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
@link |
283 |
|
|
284 |
|
@param |
285 |
|
@param |
286 |
|
@param |
287 |
|
@param |
288 |
|
|
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
290 |
44 |
private <E, F> void maybeEndBlock(State<E, F> state, int contextSize)... |
291 |
|
{ |
292 |
44 |
if (!state.getBlocks().isEmpty()) { |
293 |
21 |
int start = state.getLastDelta().getPrevious().getLastIndex() + 1; |
294 |
21 |
int end = Math.min(start + contextSize, state.getPrevious().size()); |
295 |
21 |
state.getBlocks().peek().addAll(this.<E, F>getUnmodifiedElements(state.getPrevious(), start, end)); |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
@param |
304 |
|
@param |
305 |
|
@param |
306 |
|
@param |
307 |
|
@param |
308 |
|
|
309 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
310 |
13 |
private <E, F> void displayInlineDiff(UnifiedDiffElement<E, F> previous, UnifiedDiffElement<E, F> next,... |
311 |
|
UnifiedDiffConfiguration<E, F> config) |
312 |
|
{ |
313 |
13 |
try { |
314 |
13 |
List<F> previousSubElements = config.getSplitter().split(previous.getValue()); |
315 |
13 |
List<F> nextSubElements = config.getSplitter().split(next.getValue()); |
316 |
13 |
DiffResult<F> diffResult = this.diffManager.diff(previousSubElements, nextSubElements, config); |
317 |
|
|
318 |
13 |
List<InlineDiffChunk<F>> chunks = this.inlineDisplayer.display(diffResult); |
319 |
13 |
previous.setChunks(new ArrayList<InlineDiffChunk<F>>()); |
320 |
13 |
next.setChunks(new ArrayList<InlineDiffChunk<F>>()); |
321 |
13 |
for (InlineDiffChunk<F> chunk : chunks) { |
322 |
54 |
if (!chunk.isAdded()) { |
323 |
35 |
previous.getChunks().add(chunk); |
324 |
|
} |
325 |
54 |
if (!chunk.isDeleted()) { |
326 |
44 |
next.getChunks().add(chunk); |
327 |
|
} |
328 |
|
} |
329 |
|
} catch (DiffException e) { |
330 |
|
|
331 |
|
} |
332 |
|
} |
333 |
|
} |