Skip to content

Commit

Permalink
Bug fixing (#159)
Browse files Browse the repository at this point in the history
* Fix

* Fix navigateUp

* Count line diff before parsing

* Fix startFileDiff

* minor

* return validation

* Collector fix

* Fix not opening dirs

* Minor

* Fix open diff from a copied file doesn't work

* Renamed filter to side
  • Loading branch information
pertsevpv authored Oct 17, 2024
1 parent b3b566a commit cca2f82
Show file tree
Hide file tree
Showing 26 changed files with 394 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.sudu.experiments.LoggingJs;
import org.sudu.experiments.diff.folder.FolderDiffModel;
import org.sudu.experiments.Subscribers;
import org.sudu.experiments.diff.folder.ModelFilter;
import org.sudu.experiments.diff.folder.FolderDiffSide;
import org.sudu.experiments.diff.folder.RemoteFolderDiffModel;
import org.sudu.experiments.editor.EditorWindow;
import org.sudu.experiments.editor.ui.colors.EditorColorScheme;
Expand Down Expand Up @@ -197,12 +197,12 @@ private void updateNodes(
if (child.isLeft()) {
if (isFolder) leftFolderCnt++;
leftChildren[lp] = findUpdateNode(left, path, isFolder, isOpened);
leftChildren[lp++].setHandle(getHandle(true, childModel(left.model(), i)));
leftChildren[lp++].setHandle(getHandle(true, childModel(left.getModelSupplier(), i)));
}
if (child.isRight()) {
if (isFolder) rightFolderCnt++;
rightChildren[rp] = findUpdateNode(right, path, isFolder, isOpened);
rightChildren[rp++].setHandle(getHandle(false, childModel(right.model(), i)));
rightChildren[rp++].setHandle(getHandle(false, childModel(right.getModelSupplier(), i)));
}
if (!isOpened) continue;
if (child.isBoth()) {
Expand Down Expand Up @@ -245,7 +245,7 @@ private void updateNode(
boolean isOpened = opened.contains(path);
if (isFolder) folderCnt++;
children[p] = findUpdateNode(node, path, isFolder, isOpened);
children[p++].setHandle(getHandle(left, childModel(node.model(), i)));
children[p++].setHandle(getHandle(left, childModel(node.getModelSupplier(), i)));
if (isOpened) continue;
if (children[p - 1] instanceof RemoteDirectoryNode dirNode) updateNode(dirNode, child, left);
}
Expand All @@ -271,8 +271,8 @@ private static RemoteFileTreeNode findUpdateNode(
}

protected void updateDiffInfo() {
rootView.left.updateModel(rootModel, rightRoot, ModelFilter.LEFT);
rootView.right.updateModel(rootModel, leftRoot, ModelFilter.RIGHT);
rootView.left.updateModel(rootModel, rightRoot, FolderDiffSide.LEFT);
rootView.right.updateModel(rootModel, leftRoot, FolderDiffSide.RIGHT);
rootView.setDiffModel(DiffModelBuilder.getDiffInfo(
rootView.left.model(),
rootView.right.model()
Expand Down Expand Up @@ -305,28 +305,29 @@ public void updateView() {

@Override
public void openDir(RemoteDirectoryNode node) {
var model = getModel();
var model = node.model();
if (model.children == null) return;

int foldersLen = 0;

var children = new RemoteFileTreeNode[1];
int childPtr = 0;

int filter = left ? ModelFilter.LEFT : ModelFilter.RIGHT;
int mP = model.nextInd(0, filter);
int side = left ? FolderDiffSide.LEFT : FolderDiffSide.RIGHT;
int mP = model.nextInd(0, side);
while (mP >= 0) {
RemoteFileTreeNode childNode;
var child = model.child(mP);
var handle = getHandle(left, childModel(modelSupplier, mP));
if (child.isFile()) {
childNode = new RemoteFileNode(child.path, getHandle(left, getModel(mP)), node.depth + 1);
childNode = new RemoteFileNode(child.path, handle, node.depth + 1);
} else {
foldersLen++;
childNode = new RemoteDirectoryNode(child.path, getHandle(left, getModel(mP)), node.depth + 1);
childNode = new RemoteDirectoryNode(child.path, handle, node.depth + 1);
}
childNode.posInParent = childPtr;
children = ArrayOp.addAt(childNode, children, childPtr++);
mP = model.nextInd(mP + 1, filter);
mP = model.nextInd(mP + 1, side);
}
children = Arrays.copyOf(children, childPtr);
node.setChildren(children);
Expand Down Expand Up @@ -366,12 +367,8 @@ public RemoteFileNode getOppositeFile(RemoteFileNode node) {
}

@Override
public RemoteFolderDiffModel getModel() {
return modelSupplier.get();
}

private Supplier<RemoteFolderDiffModel> getModel(int i) {
return childModel(getModel(), i);
public Supplier<RemoteFolderDiffModel> getModelSupplier() {
return modelSupplier;
}

private RemoteDirectoryNode getOppositeDir(RemoteFolderDiffModel model) {
Expand All @@ -396,8 +393,8 @@ private RemoteDirectoryNode getOpposite(RemoteDirectoryNode current, Deque<Strin
};
}

private static Supplier<RemoteFolderDiffModel> childModel(RemoteFolderDiffModel parent, int i) {
return () -> parent.child(i);
private static Supplier<RemoteFolderDiffModel> childModel(Supplier<RemoteFolderDiffModel> parent, int i) {
return () -> parent.get().child(i);
}

private void newEditor(RemoteFileNode node, boolean left) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ public void setFontSize(float fontSize) {

public void setLeftModel(Model m) {
editor1.setModel(m);
sendToDiff(true);
}

public void setRightModel(Model m) {
editor2.setModel(m);
sendToDiff(true);
}

public Model getLeftModel() {
Expand All @@ -91,7 +93,7 @@ private void fullFileParseListener(EditorComponent editor) {
if (editor1 == editor) modelFlags |= 1;
if (editor2 == editor) modelFlags |= 2;
if ((modelFlags & 3) == 3) {
sendToDiff();
sendToDiff(false);
}
}

Expand All @@ -103,8 +105,8 @@ private void iterativeParseFileListener(EditorComponent editor, int start, int s
if (diffModel == null) return;
int startLine = editor.model().document.getLine(start).x;
int stopLine = editor.model().document.getLine(stop).x;
var fromRangeInd = diffModel.leftBS(startLine, isL);
var toRangeInd = diffModel.rightBS(stopLine, isL);
var fromRangeInd = diffModel.leftNotEmptyBS(startLine, isL);
var toRangeInd = diffModel.rightNotEmptyBS(stopLine, isL);

if (fromRangeInd != 0 && diffModel.ranges[fromRangeInd].type != DiffTypes.DEFAULT) fromRangeInd--;
if (toRangeInd != diffModel.rangeCount() - 1 && diffModel.ranges[toRangeInd].type != DiffTypes.DEFAULT) toRangeInd++;
Expand Down Expand Up @@ -188,10 +190,11 @@ public void updateDiffModel(
setDiffModel(diffModel);
}

protected void sendToDiff() {
protected void sendToDiff(boolean cmpOnlyLines) {
DiffUtils.findDiffs(
editor1.model().document,
editor2.model().document,
cmpOnlyLines,
this::setDiffModel,
ui.windowManager.uiContext.window.worker());
}
Expand Down Expand Up @@ -237,7 +240,7 @@ private void revealFirstDiff() {
public boolean canNavigateUp(EditorComponent focused) {
int lineInd = focused.caretLine();
boolean left = focused == editor1;
int rangeInd = diffModel.rightBS(lineInd, left);
int rangeInd = diffModel.leftBS(lineInd, left);
for (int i = rangeInd - 1; i >= 0; i--) {
if (diffModel.ranges[i].type != DiffTypes.DEFAULT) return true;
}
Expand All @@ -247,7 +250,7 @@ public boolean canNavigateUp(EditorComponent focused) {
public void navigateUp(EditorComponent focused) {
int lineInd = focused.caretLine();
boolean left = focused == editor1;
int rangeInd = diffModel.rightBS(lineInd, left);
int rangeInd = diffModel.leftBS(lineInd, left);
for (int i = rangeInd - 1; i >= 0; i--) {
if (diffModel.ranges[i].type != DiffTypes.DEFAULT) {
setPositionsAtRange(diffModel.ranges[i]);
Expand Down Expand Up @@ -290,6 +293,6 @@ private void setPositionsAtRange(DiffRange range) {
}

public void refresh() {
sendToDiff();
sendToDiff(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public void open(String source, String name, boolean left) {
}

private void fireIfModelReady() {
if (leftFile != null && rightFile != null)
if (leftFile != null && rightFile != null) {
rootView.sendToDiff(true);
fireEvent();
}
}

void updateTitle(FileHandle handle, boolean left) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.sudu.experiments.DirectoryHandle;
import org.sudu.experiments.FileHandle;
import org.sudu.experiments.diff.folder.FolderDiffModel;
import org.sudu.experiments.diff.folder.ModelFilter;
import org.sudu.experiments.diff.folder.FolderDiffSide;
import org.sudu.experiments.editor.EditorWindow;
import org.sudu.experiments.editor.test.MergeButtonsModel;
import org.sudu.experiments.editor.ui.colors.EditorColorScheme;
Expand Down Expand Up @@ -253,8 +253,8 @@ protected void updateDiffInfo(int foldersCompared, int filesCompared) {
}

private void updateModel() {
rootView.left.updateModel(root, rightRoot, ModelFilter.LEFT);
rootView.right.updateModel(root, leftRoot, ModelFilter.RIGHT);
rootView.left.updateModel(root, rightRoot, FolderDiffSide.LEFT);
rootView.right.updateModel(root, leftRoot, FolderDiffSide.RIGHT);
rootView.setDiffModel(DiffModelBuilder.getDiffInfo(
rootView.left.model(),
rootView.right.model()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import org.sudu.experiments.FsItem;
import org.sudu.experiments.arrays.ArrayReader;
import org.sudu.experiments.arrays.ArrayWriter;
import org.sudu.experiments.diff.DiffTypes;

import java.util.Deque;
import java.util.List;

public class ItemFolderDiffModel extends RemoteFolderDiffModel {

public FsItem[] items;
public final FsItem[] items = new FsItem[] {null, null};

public ItemFolderDiffModel(FolderDiffModel parent, String path) {
super(parent, path);
Expand All @@ -19,12 +21,43 @@ public ItemFolderDiffModel child(int i) {
return (ItemFolderDiffModel) super.child(i);
}

@Override
public ItemFolderDiffModel parent() {
return (ItemFolderDiffModel) parent;
}

public ItemFolderDiffModel findNeedUpdate(Deque<ItemFolderDiffModel> paths) {
if (parent == null) return this;
int diffType = getDiffType();
if ((diffType == DiffTypes.DEFAULT || diffType == DiffTypes.EDITED) && countItems() < 2) {
paths.addFirst(this);
return parent().findNeedUpdate(paths);
}
return this;
}

public void setItems(FsItem left, FsItem right) {
items[0] = left;
items[1] = right;
}

public void setItem(FsItem item) {
if (isLeftOnly()) items[0] = item;
else if (isRightOnly()) items[1] = item;
}

public void setItem(boolean left, FsItem item) {
items[left ? 0 : 1] = item;
}

public FsItem item() {
return left();
if (isLeftOnly()) return items[0];
if (isRightOnly()) return items[1];
return null;
}

public FsItem item(boolean left) {
return items.length < 2 || left ? left() : right();
return left ? left() : right();
}

public FsItem left() {
Expand All @@ -35,6 +68,10 @@ public FsItem right() {
return items[1];
}

private int countItems() {
return (left() == null ? 0 : 1) + (right() == null ? 0 : 1);
}

public static int[] toInts(
ItemFolderDiffModel model,
List<String> pathList,
Expand All @@ -57,13 +94,11 @@ public static void writeInts(
writer.write(pathList.size());
pathList.add(model.path);

if (model.items == null) writer.write(-1);
else {
writer.write(model.items.length);
for (var fsItem: model.items) {
for (var fsItem: model.items) {
if (fsItem != null) {
writer.write(fsList.size());
fsList.add(fsItem);
}
} else writer.write(-1);
}

if (model.children == null) writer.write(-1);
Expand All @@ -90,13 +125,10 @@ public static ItemFolderDiffModel fromInts(
int pathInd = reader.next();
model.path = paths[pathInd];

var itemsLen = reader.next();
if (itemsLen != -1) {
model.items = new FsItem[itemsLen];
for (int i = 0; i < itemsLen; i++) {
int itemInd = reader.next();
model.items[i] = items[itemInd];
}
for (int i = 0; i < 2; i++) {
int itemInd = reader.next();
if (itemInd == -1) continue;
model.items[i] = items[itemInd];
}

int childrenLen = reader.next();
Expand All @@ -110,4 +142,43 @@ public static ItemFolderDiffModel fromInts(
}
return model;
}

public void updateFsItems(
boolean left,
ArrayReader reader,
Deque<FsItem> items,
Deque<ItemFolderDiffModel> paths
) {
if (isFile()) return;
int len = reader.next();
var item = items.removeFirst();
if (len == -1) {
var child = paths.removeFirst();
child.setItem(left, item);
child.updateFsItems(left, reader, items, paths);
} else {
int side = left ? FolderDiffSide.LEFT : FolderDiffSide.RIGHT;
int mP = 0;
for (int i = 0; i < len; i++) {
mP = nextInd(mP, side);
var child = child(mP);
child.setItem(left, item);
child.updateFsItems(left, reader, items, paths);
mP++;
}
}
}

private ItemFolderDiffModel child(String path, boolean file) {
for (int i = 0; i < children.length; i++) {
var child = child(i);
if (path.equals(child.path) && child.isFile() == file) return this;
}
return null;
}

@Override
public String toString() {
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ static void syncMethod(String method, Object[] a, ArrayList<Object> result) {
case ScopeProxy.RESOLVE_ALL -> ScopeProxy.resolveAll(ArgsCast.array(a, 0).ints(), ArgsCast.array(a, 1).chars(), ArgsCast.array(a, 2).ints(), result);
case DiffUtils.FIND_DIFFS -> DiffUtils.findDiffs(
ArgsCast.array(a, 0).chars(), ArgsCast.array(a, 1).ints(),
ArgsCast.array(a, 2).chars(), ArgsCast.array(a, 3).ints(), result);
ArgsCast.array(a, 2).chars(), ArgsCast.array(a, 3).ints(),
ArgsCast.array(a, 4).ints(), result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ static void asyncMethod(String method, Object[] a, Consumer<Object[]> r) {
case DiffUtils.CMP_FILES -> DiffUtils.compareFiles(ArgsCast.file(a, 0), ArgsCast.file(a, 1), r);
case DiffUtils.CMP_FOLDERS -> DiffUtils.compareFolders(ArgsCast.dir(a, 0), ArgsCast.dir(a, 1), r);
case DiffUtils.READ_FOLDER -> DiffUtils.readFolder(ArgsCast.dir(a, 0), ArgsCast.array(a, 1).ints(), r);
case DiffUtils.REREAD_FOLDER -> DiffUtils.rereadFolder(
ArgsCast.dir(a, 0),
ArgsCast.array(a, 1).ints(),
ArgsCast.array(a, 2).chars(), r
);
default -> System.out.println("asyncMethod = " + method);
}
}
Expand Down
Loading

0 comments on commit cca2f82

Please sign in to comment.