Search in sources :

Example 16 with Chunk

use of difflib.Chunk in project Gargoyle by callakrsos.

the class TextSameLineDiffAppController method extractedWrapperedChunk.

/**
	 * chunk객체를 UI로 표현하기 위해 wrapping처리함.
	 *
	 * @param deltaType
	 * @param deltas
	 * @param readLines
	 * @return
	 */
private List<ChunkWrapper> extractedWrapperedChunk(final DeltaType deltaType, final List<Delta> deltas, final List<String> readLines) {
    List<ChunkWrapper> collect = deltas.stream().map(delta -> {
        Chunk chunk = null;
        if (DeltaType.ORIGINAL == deltaType) {
            chunk = delta.getOriginal();
        } else {
            chunk = delta.getRevised();
        }
        int position = chunk.getPosition();
        @SuppressWarnings("unchecked") List<String> lines = (List<String>) chunk.getLines();
        TYPE type = delta.getType();
        ChunkWrapper chunkWrapper = new ChunkWrapper();
        chunkWrapper.setType(type);
        chunkWrapper.setLines(lines);
        chunkWrapper.setChunk(chunk);
        chunkWrapper.setPosition(position);
        chunkWrapper.setDelta(delta);
        return chunkWrapper;
    }).collect(() -> {
        List<ChunkWrapper> newChunk = new ArrayList<>(readLines.size() * 3);
        for (int i = 0; i < readLines.size(); i++) {
            ChunkWrapper chunkWrapper = new ChunkWrapper();
            chunkWrapper.setStr(readLines.get(i));
            chunkWrapper.setPosition(i);
            newChunk.add(chunkWrapper);
        }
        return newChunk;
    }, (collection, item) -> {
        int position = item.getPosition();
        List<String> lines = item.getLines();
        TYPE type = item.getType();
        int tmpPosition = position;
        for (String str : lines) {
            ChunkWrapper chunkWrapper = new ChunkWrapper();
            chunkWrapper.setType(type);
            chunkWrapper.setLines(lines);
            chunkWrapper.setPosition(position);
            chunkWrapper.setChunk(item.getChunk());
            chunkWrapper.setStr(str);
            chunkWrapper.setDelta(item.getDelta());
            try {
                collection.set(tmpPosition++, chunkWrapper);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, (collection1, collection2) -> collection1.addAll(collection2));
    return collect;
}
Also used : Button(javafx.scene.control.Button) ListView(javafx.scene.control.ListView) TextArea(javafx.scene.control.TextArea) CompareResult(com.kyj.fx.voeditor.visual.diff.CompareResult) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) ListCell(javafx.scene.control.ListCell) LoggerFactory(org.slf4j.LoggerFactory) ChunkWrapper(com.kyj.fx.voeditor.visual.diff.ChunkWrapper) SnapshotParameters(javafx.scene.SnapshotParameters) ArrayList(java.util.ArrayList) TableColumn(javafx.scene.control.TableColumn) Insets(javafx.geometry.Insets) Chunk(difflib.Chunk) BackgroundFill(javafx.scene.layout.BackgroundFill) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) TableView(javafx.scene.control.TableView) Callback(javafx.util.Callback) Tooltip(javafx.scene.control.Tooltip) GridPane(javafx.scene.layout.GridPane) HBox(javafx.scene.layout.HBox) Color(javafx.scene.paint.Color) ObjectProperty(javafx.beans.property.ObjectProperty) Logger(org.slf4j.Logger) Label(javafx.scene.control.Label) WritableImage(javafx.scene.image.WritableImage) IOException(java.io.IOException) DiffComparable(com.kyj.fx.voeditor.visual.diff.DiffComparable) ScrollEvent(javafx.scene.input.ScrollEvent) Collectors(java.util.stream.Collectors) Background(javafx.scene.layout.Background) CellDataFeatures(javafx.scene.control.TableColumn.CellDataFeatures) TextBaseComparator(com.kyj.fx.voeditor.visual.diff.TextBaseComparator) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) List(java.util.List) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) AnchorPane(javafx.scene.layout.AnchorPane) ImageView(javafx.scene.image.ImageView) ValueUtil(kyj.Fx.dao.wizard.core.util.ValueUtil) ObservableValue(javafx.beans.value.ObservableValue) TYPE(difflib.Delta.TYPE) BorderPane(javafx.scene.layout.BorderPane) StringProperty(javafx.beans.property.StringProperty) Delta(difflib.Delta) ArrayList(java.util.ArrayList) ChunkWrapper(com.kyj.fx.voeditor.visual.diff.ChunkWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Chunk(difflib.Chunk) TYPE(difflib.Delta.TYPE) IOException(java.io.IOException)

Example 17 with Chunk

use of difflib.Chunk in project Gargoyle by callakrsos.

the class DiffPathText method diffPath.

@Test
public void diffPath() throws IOException, URISyntaxException, PatchFailedException {
    URL test1 = ClassLoader.getSystemClassLoader().getResource("compare/Test1");
    URL test2 = ClassLoader.getSystemClassLoader().getResource("compare/Test2");
    List<String> test1List = Files.readAllLines(new File(test1.toURI()).toPath());
    List<String> test2List = Files.readAllLines(new File(test2.toURI()).toPath());
    Patch diff = DiffUtils.diff(test1List, test2List);
    List<Delta> deltas = diff.getDeltas();
    deltas.stream().forEach(d -> {
        Chunk original = d.getOriginal();
        int position = original.getPosition();
        List<String> lines = (List<String>) original.getLines();
        for (String str : lines) {
            switch(d.getType()) {
                case INSERT:
                    test1List.set(position++, "+".concat(str));
                    break;
                case DELETE:
                    test1List.set(position++, "-".concat(str));
                    break;
                case CHANGE:
                    test1List.set(position++, "*".concat(str));
                    break;
            }
        }
        Chunk revised = d.getRevised();
        position = revised.getPosition();
        lines = (List<String>) revised.getLines();
    });
    test1List.stream().forEach(System.out::println);
// System.out.println(original);
// List<?> patch = DiffUtils.patch(test1List, diff);
// patch.stream().forEach(System.out::println);
//
// List<?> unpatch = DiffUtils.unpatch(test2List, diff);
// unpatch.stream().forEach(System.out::println);
}
Also used : Delta(difflib.Delta) List(java.util.List) LinkedList(java.util.LinkedList) Chunk(difflib.Chunk) File(java.io.File) Patch(difflib.Patch) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Chunk (difflib.Chunk)17 Delta (difflib.Delta)13 IOException (java.io.IOException)11 ChunkWrapper (com.kyj.fx.voeditor.visual.diff.ChunkWrapper)7 CompareResult (com.kyj.fx.voeditor.visual.diff.CompareResult)7 TYPE (difflib.Delta.TYPE)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 FXML (javafx.fxml.FXML)7 ImageView (javafx.scene.image.ImageView)7 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)6 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)6 StringProperty (javafx.beans.property.StringProperty)6 ObservableValue (javafx.beans.value.ObservableValue)6 File (java.io.File)5 CellDataFeatures (javafx.scene.control.TableColumn.CellDataFeatures)5 DiffComparable (com.kyj.fx.voeditor.visual.diff.DiffComparable)4 Patch (difflib.Patch)4 ObjectProperty (javafx.beans.property.ObjectProperty)4 SimpleObjectProperty (javafx.beans.property.SimpleObjectProperty)4