1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.diff.display.internal

File DefaultInlineDiffDisplayer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
26
1
1
86
49
8
0.31
26
1
8

Classes

Class Line # Actions
DefaultInlineDiffDisplayer 42 26 0% 8 2
0.9428571594.3%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Default {@link InlineDiffDisplayer} implementation.
36    *
37    * @version $Id: 2eae169f8a29b13c639be6314d1a879a68bd8faa $
38    * @since 4.1RC1
39    */
40    @Component
41    @Singleton
 
42    public class DefaultInlineDiffDisplayer implements InlineDiffDisplayer
43    {
 
44  25 toggle @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    // Add a chunk with the unmodified elements between the last delta and the current one.
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    // Add changed chunks.
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    // Add the final chunk with the unmodified elements after the last delta.
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    }