Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #175

Merged
merged 3 commits into from
Dec 16, 2024
Merged

Fixes #175

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void onFiltersApplied(JsArray<JSObject> jsResult) {

private void onRefresh(JsArray<JSObject> jsResult) {
var msg = BackendMessage.deserialize(jsResult);
lastSendFrontendMsg.openedFolders.updateWithModel(msg.root);
lastSendFrontendMsg.openedFolders.updateDeepWithModel(msg.root);
}

private void updateNodes() {
Expand All @@ -228,10 +228,13 @@ private void updateNodes(
RemoteFolderDiffModel model,
FrontendTreeNode treeNode
) {
if (model.children == null || treeNode == null || !treeNode.isOpened()) return;
if (model == null || model.children == null ||
treeNode == null || !treeNode.isOpened()
) return;
left.doOpen();
right.doOpen();

if (treeNode.children.length != model.children.length) treeNode.updateWithModel(model);
int lp = 0, rp = 0;
for (int i = 0; i < model.children.length; i++) {
var child = model.child(i);
Expand Down Expand Up @@ -260,6 +263,7 @@ private void updateNode(
if (model.children == null || treeNode == null || !treeNode.isOpened()) return;
node.doOpen();

if (treeNode.children.length != model.children.length) treeNode.updateWithModel(model);
for (int i = 0; i < model.children.length; i++) {
var child = model.child(i);
var childNode = treeNode.child(i, child.path, child.isFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,32 @@ public static void rereadFolder(
leftReader.beginRead();
}

public static int[] makeIntervals(Document document, int fromLine, int toLine) {
public static int[] makeIntervals(Document document, int fromLine, int toLine, boolean cmpOnlyLines) {
ArrayWriter writer = new ArrayWriter();
writer.write(toLine - fromLine);
int offset = 0;
for (int i = fromLine; i < toLine; i++) {
CodeLine line = document.line(i);
int Mi = line.length();
writer.write(Mi);
for (int j = 0; j < Mi; j++) {
var elem = line.get(j);
writer.write(offset, elem.length());
offset += elem.length();
if (cmpOnlyLines) {
writer.write(1);
writer.write(offset, line.totalStrLength);
offset += line.totalStrLength;
} else {
int Mi = line.length();
writer.write(Mi);
for (int j = 0; j < Mi; j++) {
var elem = line.get(j);
writer.write(offset, elem.length());
offset += elem.length();
}
}
offset++;
}
return writer.getInts();
}

public static int[] makeIntervals(Document document) {
return makeIntervals(document, 0, document.length());
public static int[] makeIntervals(Document document, boolean cmpOnlyLines) {
return makeIntervals(document, 0, document.length(), cmpOnlyLines);
}

public static DiffInfo readDiffInfo(int[] ints) {
Expand Down Expand Up @@ -197,8 +203,8 @@ public static void findDiffs(
) {
char[] chars1 = document1.getChars();
char[] chars2 = document2.getChars();
int[] intervals1 = makeIntervals(document1);
int[] intervals2 = makeIntervals(document2);
int[] intervals1 = makeIntervals(document1, cmpOnlyLines);
int[] intervals2 = makeIntervals(document2, cmpOnlyLines);

window.sendToWorker(true,
r -> {
Expand All @@ -219,8 +225,8 @@ public static void findIntervalDiffs(
) {
char[] chars1 = document1.getChars(fromL, toL);
char[] chars2 = document2.getChars(fromR, toR);
int[] intervals1 = makeIntervals(document1, fromL, toL);
int[] intervals2 = makeIntervals(document2, fromR, toR);
int[] intervals1 = makeIntervals(document1, fromL, toL, false);
int[] intervals2 = makeIntervals(document2, fromR, toR, false);
window.sendToWorker(true,
r -> {
int[] reply = ((ArrayView) r[0]).ints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void compareDocuments(boolean printResults) {

DiffModel model = new DiffModel();
char[] charsL = docL.getChars();
int[] intsL = DiffUtils.makeIntervals(docL);
int[] intsL = DiffUtils.makeIntervals(docL, true);
char[] charsR = docR.getChars();
int[] intsR = DiffUtils.makeIntervals(docR);
int[] intsR = DiffUtils.makeIntervals(docR, true);

int[] res = model.findDiffs(charsL, intsL, charsR, intsR);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void updateDeepWithModel(RemoteFolderDiffModel model) {
var nodeChild = child(i, modelChild.path, modelChild.isFile());
if (nodeChild == null) {
nodeChild = new FrontendTreeNode();
nodeChild.name = modelChild.path;
nodeChild.isFile = modelChild.isFile();
} else {
nodeChild.updateDeepWithModel(modelChild);
}
Expand All @@ -53,7 +55,11 @@ public void updateWithModel(RemoteFolderDiffModel model) {
for (int i = 0; i < model.children.length; i++) {
var modelChild = model.child(i);
var nodeChild = child(i, modelChild.path, modelChild.isFile());
if (nodeChild == null) nodeChild = new FrontendTreeNode();
if (nodeChild == null) {
nodeChild = new FrontendTreeNode();
nodeChild.name = modelChild.path;
nodeChild.isFile = modelChild.isFile();
}
newChildren[i] = nodeChild;
}
children = newChildren;
Expand Down Expand Up @@ -93,7 +99,7 @@ public void deleteItem(FrontendTreeNode node) {
}

public FrontendTreeNode child(int i, String path, boolean isFile) {
if (0 < i && i < children.length) {
if (0 <= i && i < children.length) {
var child = children[i];
if (child.name.equals(path) && child.isFile == isFile) return child;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.sudu.experiments.arrays.ArrayWriter;
import org.sudu.experiments.diff.DiffTypes;
import org.sudu.experiments.diff.SizeScanner;
import org.sudu.experiments.diff.folder.FolderDiffModel;
import org.sudu.experiments.diff.folder.ItemFolderDiffModel;
import org.sudu.experiments.diff.folder.RemoteFolderDiffModel;
import org.sudu.experiments.editor.worker.ArgsCast;
Expand Down Expand Up @@ -80,12 +81,11 @@ public void beginCompare() {

public void refresh() {
LoggingJs.info("RemoteCollector.Refresh");
var items = root.items;
firstMessageSent = lastMessageSent = false;
isRefresh = true;
startTime = lastMessageSentTime = Performance.now();
root = new ItemFolderDiffModel(null, "");
root.setItems(items[0], items[1]);
root.setCompared(false);
root.childrenComparedCnt = 0;
sendToWorkerQueue.clear();
beginCompare();
}
Expand Down Expand Up @@ -233,6 +233,21 @@ private void onError(String error) {
LoggingJs.error(error);
}

private ItemFolderDiffModel oldOrNull(
FolderDiffModel[] oldChildren,
ItemFolderDiffModel parent,
FsItem item
) {
String path = item.getName();
boolean isFile = item instanceof FileHandle;
if (oldChildren == null) return new ItemFolderDiffModel(parent, path);
for (var oldChild: oldChildren)
if (oldChild instanceof ItemFolderDiffModel itemChild &&
path.equals(itemChild.path) && isFile == itemChild.isFile()
) return itemChild;
return new ItemFolderDiffModel(parent, path);
}

private void compare(ItemFolderDiffModel model) {
++inComparing;
if (model.left() instanceof DirectoryHandle leftDir &&
Expand Down Expand Up @@ -275,8 +290,9 @@ private void onFoldersCompared(
FsItem[] rightItem = Arrays.copyOfRange(result, 1 + leftLen, 1 + leftLen + rightLen, FsItem[].class);

int len = diffs.length;
model.children = new ItemFolderDiffModel[len];
var oldChildren = model.children;
model.childrenComparedCnt = 0;
model.children = new ItemFolderDiffModel[len];

int lP = 0, rP = 0;
int mP = 0;
Expand All @@ -286,7 +302,8 @@ private void onFoldersCompared(
int kind = kinds[mP];
if (diffs[mP] == DiffTypes.DELETED) {
edited = true;
model.children[mP] = new ItemFolderDiffModel(model, leftItem[lP].getName());
model.children[mP] = oldOrNull(oldChildren, model, leftItem[lP]);
model.child(mP).setCompared(false);
model.child(mP).posInParent = mP;
model.child(mP).setItemKind(kind);
model.child(mP).setDiffType(DiffTypes.DELETED);
Expand All @@ -296,7 +313,8 @@ private void onFoldersCompared(
lP++;
} else if (diffs[mP] == DiffTypes.INSERTED) {
edited = true;
model.children[mP] = new ItemFolderDiffModel(model, rightItem[rP].getName());
model.children[mP] = oldOrNull(oldChildren, model, rightItem[rP]);
model.child(mP).setCompared(false);
model.child(mP).posInParent = mP;
model.child(mP).setItemKind(kind);
model.child(mP).setDiffType(DiffTypes.INSERTED);
Expand All @@ -305,7 +323,8 @@ private void onFoldersCompared(
mP++;
rP++;
} else {
model.children[mP] = new ItemFolderDiffModel(model, leftItem[lP].getName());
model.children[mP] = oldOrNull(oldChildren, model, leftItem[lP]);
model.child(mP).setCompared(false);
model.child(mP).posInParent = mP;
model.child(mP).setItemKind(kind);
model.child(mP).setItems(leftItem[lP], rightItem[rP]);
Expand Down Expand Up @@ -397,7 +416,7 @@ private void onFolderRead(

private void onItemCompared() {
if (--inComparing < 0) throw new IllegalStateException("inComparing cannot be negative");
if (--sentToWorker < 0) throw new IllegalStateException("inComparing cannot be negative");
if (--sentToWorker < 0) throw new IllegalStateException("sentToWorker cannot be negative");
if (isShutdown) shutdown();
else if (inComparing == 0) onComplete();
else {
Expand Down
Loading