утренние изменения
удаление эталона и роль студента
This commit is contained in:
@@ -14,8 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.difflib.unifieddiff;
|
||||
|
||||
import com.github.difflib.patch.AbstractDelta;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
@@ -25,15 +25,12 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @todo use an instance to store contextSize and originalLinesProvider.
|
||||
* @author Tobias Warneke (t.warneke@gmx.net)
|
||||
* @todo use an instance to store contextSize and originalLinesProvider.
|
||||
*/
|
||||
public class UnifiedDiffWriter {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(UnifiedDiffWriter.class.getName());
|
||||
|
||||
public static void write(UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Writer writer, int contextSize) throws IOException {
|
||||
Objects.requireNonNull(originalLinesProvider, "original lines provider needs to be specified");
|
||||
write(diff, originalLinesProvider, line -> {
|
||||
@@ -44,12 +41,10 @@ public class UnifiedDiffWriter {
|
||||
}
|
||||
}, contextSize);
|
||||
}
|
||||
|
||||
public static void write(UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Consumer<String> writer, int contextSize) throws IOException {
|
||||
if (diff.getHeader() != null) {
|
||||
writer.accept(diff.getHeader());
|
||||
}
|
||||
|
||||
for (UnifiedDiffFile file : diff.getFiles()) {
|
||||
List<AbstractDelta<String>> patchDeltas = new ArrayList<>(
|
||||
file.getPatch().getDeltas());
|
||||
@@ -58,24 +53,18 @@ public class UnifiedDiffWriter {
|
||||
if (file.getIndex() != null) {
|
||||
writer.accept("index " + file.getIndex());
|
||||
}
|
||||
|
||||
writer.accept("--- " + (file.getFromFile() == null ? "/dev/null" : file.getFromFile()));
|
||||
|
||||
if (file.getToFile() != null) {
|
||||
writer.accept("+++ " + file.getToFile());
|
||||
}
|
||||
|
||||
List<String> originalLines = originalLinesProvider.apply(file.getFromFile());
|
||||
|
||||
List<AbstractDelta<String>> deltas = new ArrayList<>();
|
||||
|
||||
AbstractDelta<String> delta = patchDeltas.get(0);
|
||||
deltas.add(delta); // add the first Delta to the current set
|
||||
// if there's more than 1 Delta, we may need to output them together
|
||||
if (patchDeltas.size() > 1) {
|
||||
for (int i = 1; i < patchDeltas.size(); i++) {
|
||||
int position = delta.getSource().getPosition();
|
||||
|
||||
// Check if the next Delta is too close to the current
|
||||
// position.
|
||||
// And if it is, add it to the current set
|
||||
@@ -93,30 +82,25 @@ public class UnifiedDiffWriter {
|
||||
}
|
||||
delta = nextDelta;
|
||||
}
|
||||
|
||||
}
|
||||
// don't forget to process the last set of Deltas
|
||||
processDeltas(writer, originalLines, deltas, contextSize,
|
||||
patchDeltas.size() == 1 && file.getFromFile() == null);
|
||||
}
|
||||
|
||||
}
|
||||
if (diff.getTail() != null) {
|
||||
writer.accept("--");
|
||||
writer.accept(diff.getTail());
|
||||
}
|
||||
}
|
||||
|
||||
private static void processDeltas(Consumer<String> writer,
|
||||
List<String> origLines, List<AbstractDelta<String>> deltas,
|
||||
int contextSize, boolean newFile) {
|
||||
List<String> origLines, List<AbstractDelta<String>> deltas,
|
||||
int contextSize, boolean newFile) {
|
||||
List<String> buffer = new ArrayList<>();
|
||||
int origTotal = 0; // counter for total lines output from Original
|
||||
int revTotal = 0; // counter for total lines output from Original
|
||||
int line;
|
||||
|
||||
AbstractDelta<String> curDelta = deltas.get(0);
|
||||
|
||||
int origStart;
|
||||
if (newFile) {
|
||||
origStart = 0;
|
||||
@@ -127,18 +111,15 @@ public class UnifiedDiffWriter {
|
||||
origStart = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int revStart = curDelta.getTarget().getPosition() + 1 - contextSize;
|
||||
if (revStart < 1) {
|
||||
revStart = 1;
|
||||
}
|
||||
|
||||
// find the start of the wrapper context code
|
||||
int contextStart = curDelta.getSource().getPosition() - contextSize;
|
||||
if (contextStart < 0) {
|
||||
contextStart = 0; // clamp to the start of the file
|
||||
}
|
||||
|
||||
// output the context before the first Delta
|
||||
for (line = contextStart; line < curDelta.getSource().getPosition()
|
||||
&& line < origLines.size(); line++) { //
|
||||
@@ -150,7 +131,6 @@ public class UnifiedDiffWriter {
|
||||
getDeltaText(txt -> buffer.add(txt), curDelta);
|
||||
origTotal += curDelta.getSource().getLines().size();
|
||||
revTotal += curDelta.getTarget().getLines().size();
|
||||
|
||||
int deltaIndex = 1;
|
||||
while (deltaIndex < deltas.size()) { // for each of the other Deltas
|
||||
AbstractDelta<String> nextDelta = deltas.get(deltaIndex);
|
||||
@@ -169,7 +149,6 @@ public class UnifiedDiffWriter {
|
||||
curDelta = nextDelta;
|
||||
deltaIndex++;
|
||||
}
|
||||
|
||||
// Now output the post-Delta context code, clamping the end of the file
|
||||
contextStart = curDelta.getSource().getPosition()
|
||||
+ curDelta.getSource().getLines().size();
|
||||
@@ -179,7 +158,6 @@ public class UnifiedDiffWriter {
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
|
||||
// Create and insert the block header, conforming to the Unified Diff
|
||||
// standard
|
||||
writer.accept("@@ -" + origStart + "," + origTotal + " +" + revStart + "," + revTotal + " @@");
|
||||
@@ -187,12 +165,11 @@ public class UnifiedDiffWriter {
|
||||
writer.accept(txt);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter.
|
||||
* getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter.
|
||||
*
|
||||
* @param writer consumer for the list of String lines of code
|
||||
* @param delta the Delta to output
|
||||
* @param delta the Delta to output
|
||||
*/
|
||||
private static void getDeltaText(Consumer<String> writer, AbstractDelta<String> delta) {
|
||||
for (String line : delta.getSource().getLines()) {
|
||||
@@ -202,7 +179,6 @@ public class UnifiedDiffWriter {
|
||||
writer.accept("+" + line);
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeOrNothing(Consumer<String> writer, String str) throws IOException {
|
||||
if (str != null) {
|
||||
writer.accept(str);
|
||||
|
||||
Reference in New Issue
Block a user