Search in sources :

Example 1 with Difference

use of org.incava.util.diff.Difference in project xwiki-platform by xwiki.

the class CharacterDiffService method getDifferences.

@Override
public Collection<XDelta> getDifferences(String previous, String current) {
    // get differences at character level
    // FIXME: do we want at character level or we'd better get word level, to have it working faster
    Collection<XDelta> deltas = new ArrayList<XDelta>();
    List<Character> previousContent = new ArrayList<Character>();
    for (int i = 0; i < previous.length(); ++i) {
        previousContent.add(previous.charAt(i));
    }
    List<Character> currentContent = new ArrayList<Character>();
    for (int i = 0; i < current.length(); ++i) {
        currentContent.add(current.charAt(i));
    }
    Diff<Character> diff = new Diff<Character>(previousContent, currentContent);
    // prepare the XDeltas for all diffs
    for (Difference it : diff.diff()) {
        XDelta delta = getDelta(previous, current, it);
        if (delta != null) {
            deltas.add(delta);
        }
    }
    return deltas;
}
Also used : Diff(org.incava.util.diff.Diff) XDelta(org.xwiki.annotation.maintainer.XDelta) ArrayList(java.util.ArrayList) Difference(org.incava.util.diff.Difference)

Example 2 with Difference

use of org.incava.util.diff.Difference in project erlide_eclipse by erlang.

the class ChangesetMaker method createEdits.

// @SuppressWarnings("unchecked")
// static public ArrayList<TextEdit> createEdits(File in, File out)
// throws IOException {
// inFile = in;
// outFile = out;
// 
// ArrayList<TextEdit> edits = new ArrayList<TextEdit>();
// inFileCharArray = null;
// outFileCharArray = null;
// 
// inFileCharArray = splitFile(inFile);
// outFileCharArray = splitFile(outFile);
// 
// algorithm = new Diff(inFileCharArray, outFileCharArray);
// 
// differencesList = algorithm.diff();
// for (Difference d : differencesList) {
// edits.add(createEditFromDiff(d));
// }
// 
// return edits;
// }
/**
 * Reads the input file, compares with the given new string, then creates Eclipse's
 * <code>TextEdit</code>-s.
 *
 * @param in
 *            original file
 * @param out
 *            modified file content
 * @return list of edit objects
 * @throws IOException
 *             if the file could not be read
 */
public static List<TextEdit> createEdits(final File in, final String out) throws IOException {
    ChangesetMaker.inFile = in;
    final List<TextEdit> edits = new ArrayList<>();
    ChangesetMaker.inFileCharArray = null;
    ChangesetMaker.outFileCharArray = null;
    ChangesetMaker.inFileCharArray = ChangesetMaker.readFile(ChangesetMaker.inFile);
    ChangesetMaker.outFileCharArray = new ArrayList<>();
    ChangesetMaker.outFileCharArray = ChangesetMaker.convertArrayToArrayList(out.toCharArray());
    ChangesetMaker.algorithm = new Diff<>(ChangesetMaker.inFileCharArray, ChangesetMaker.outFileCharArray);
    ChangesetMaker.differencesList = ChangesetMaker.algorithm.diff();
    for (final Difference d : ChangesetMaker.differencesList) {
        edits.add(ChangesetMaker.createEditFromDiff(d));
    }
    return edits;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ArrayList(java.util.ArrayList) Difference(org.incava.util.diff.Difference)

Aggregations

ArrayList (java.util.ArrayList)2 Difference (org.incava.util.diff.Difference)2 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 TextEdit (org.eclipse.text.edits.TextEdit)1 Diff (org.incava.util.diff.Diff)1 XDelta (org.xwiki.annotation.maintainer.XDelta)1