Skip to content

Commit

Permalink
Filters Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pertsevpv committed Oct 21, 2024
1 parent bc2cc7f commit 5894bf2
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class RemoteFolderDiffWindow extends ToolWindow0 {
JsExternalFileOpener opener;
JsDialogProvider dialogProvider;

private int[] lastFilters = null;

public RemoteFolderDiffWindow(
EditorColorScheme theme,
WindowManager wm,
Expand Down Expand Up @@ -144,7 +146,7 @@ private void openFile(JsArray<JSObject> jsResult) {
private void update(JsArray<JSObject> jsResult) {
var msg = BackendMessage.deserialize(jsResult);
rootModel.update(msg.root);

if (isFiltered()) updateNodes(leftRoot, rightRoot, rootModel);
if (!updatedRoots) {
updatedRoots = true;
leftRoot.setLine(replaceSlashes(msg.leftRootName));
Expand All @@ -170,6 +172,13 @@ private void onDiffApplied(JsArray<JSObject> jsResult) {
updateDiffInfo();
}

private void onFiltersApplied(JsArray<JSObject> jsResult) {
var msg = BackendMessage.deserialize(jsResult);
rootModel.update(msg.root);
updateNodes(leftRoot, rightRoot, rootModel);
updateDiffInfo();
}

private void updateNodes(
RemoteDirectoryNode left,
RemoteDirectoryNode right,
Expand All @@ -189,7 +198,7 @@ private void updateNodes(
int lp = 0, rp = 0;
int leftFolderCnt = 0;
int rightFolderCnt = 0;
for (int i = 0; i < model.children.length; i++) {
for (int i = 0; model.children != null && i < model.children.length; i++) {
var child = model.child(i);
String path = child.path;
boolean isFolder = !child.isFile();
Expand Down Expand Up @@ -238,15 +247,15 @@ private void updateNode(
var children = new RemoteFileTreeNode[1];
int p = 0;
int folderCnt = 0;
for (int i = 0; i < model.children.length; i++) {
for (int i = 0; model.children != null && i < model.children.length; i++) {
var child = model.child(i);
String path = child.path;
boolean isFolder = !model.isFile();
boolean isFolder = !child.isFile();
boolean isOpened = opened.contains(path);
if (isFolder) folderCnt++;
children[p] = findUpdateNode(node, path, isFolder, isOpened);
children[p++].setHandle(getHandle(left, childModel(node.getModelSupplier(), i)));
if (isOpened) continue;
if (!isOpened) continue;
if (children[p - 1] instanceof RemoteDirectoryNode dirNode) updateNode(dirNode, child, left);
}
children = Arrays.copyOf(children, p);
Expand All @@ -266,7 +275,7 @@ private static RemoteFileTreeNode findUpdateNode(
: isFolder
? new RemoteDirectoryNode(path, null, node.depth + 1)
: new RemoteFileNode(path, null, node.depth + 1);
if (isOpened) updNode.open();
if (isOpened && isFolder) updNode.open();
return updNode;
}

Expand All @@ -287,6 +296,7 @@ private void onChannelMessage(JsArray<JSObject> jsResult) {
case DiffModelChannelUpdater.FRONTEND_MESSAGE -> update(jsResult);
case DiffModelChannelUpdater.OPEN_FILE -> openFile(jsResult);
case DiffModelChannelUpdater.APPLY_DIFF -> onDiffApplied(jsResult);
case DiffModelChannelUpdater.APPLY_FILTERS -> onFiltersApplied(jsResult);
}
LoggingJs.trace(
"Got message in " + Numbers.iRnd(Performance.now() - startTime) + "ms"
Expand Down Expand Up @@ -488,6 +498,7 @@ private void sendFrontendModel() {
leftRoot,
rightRoot,
rootModel,
isFiltered(),
searchString
);
result.push(DiffModelChannelUpdater.FRONTEND_MESSAGE_ARRAY);
Expand Down Expand Up @@ -679,12 +690,20 @@ private int getNavigateInd(FileTreeView focused, boolean up) {
}

public void applyDiffFilter(int[] filters) {
// TODO
this.lastFilters = filters;
System.out.println("filters = " + Arrays.toString(filters));
JsArray<JSObject> jsArray = JsArray.create();
jsArray.set(0, JsCast.jsInts(filters));
jsArray.push(DiffModelChannelUpdater.APPLY_FILTERS_ARRAY);
channel.sendMessage(jsArray);
}

public int[] getDiffFilter() {
// TODO
return new int[0];
return lastFilters != null ? lastFilters : new int[0];
}

private boolean isFiltered() {
return lastFilters != null && !(lastFilters.length == 0 || lastFilters.length == 4);
}

public void refresh() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static void writeInts(
) {
writer.write(model.flags);
writer.write(model.childrenComparedCnt);
writer.write(model.posInParent);

writer.write(pathList.size());
pathList.add(model.path);
Expand Down Expand Up @@ -121,6 +122,7 @@ public static ItemFolderDiffModel fromInts(
ItemFolderDiffModel model = new ItemFolderDiffModel(parent, null);
model.flags = reader.next();
model.childrenComparedCnt = reader.next();
model.posInParent = reader.next();

int pathInd = reader.next();
model.path = paths[pathInd];
Expand All @@ -136,7 +138,6 @@ public static ItemFolderDiffModel fromInts(
var children = new ItemFolderDiffModel[childrenLen];
for (int i = 0; i < childrenLen; i++) {
children[i] = fromInts(reader, paths, items, model);
children[i].posInParent = i;
}
model.children = children;
}
Expand Down Expand Up @@ -168,17 +169,4 @@ public void updateFsItems(
}
}
}

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
@@ -1,6 +1,8 @@
package org.sudu.experiments.ui;

import org.sudu.experiments.diff.folder.FolderDiffModel;
import org.sudu.experiments.ui.fs.RemoteFileNode;
import org.sudu.experiments.ui.fs.RemoteFileTreeNode;

import java.util.Comparator;

Expand Down Expand Up @@ -150,7 +152,7 @@ private static void setIcon(FileTreeNode node, FolderDiffModel model) {
}

private static void setIcon(FileTreeNode node, boolean compared) {
if (node.childrenLength() == 0 && !compared) node.iconRefresh();
if (node.isClosed() && !compared) node.iconRefresh();
else node.defaultIcon();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public FolderDiffModel(FolderDiffModel parent) {

public void update(FolderDiffModel newModel) {
this.children = newModel.children;
for (var child: children) child.parent = this;
if (children != null) for (var child: children) child.parent = this;
this.childrenComparedCnt = newModel.childrenComparedCnt;
this.flags = newModel.flags;
this.posInParent = newModel.posInParent;
Expand Down Expand Up @@ -101,8 +101,8 @@ public boolean isCompared() {
public boolean isFile() {
int itemKind = getItemKind();
return itemKind == ItemKind.FILE
|| (isLeft() && itemKind == ItemKind.LEFT_ONLY_FILE)
|| (isRight() && itemKind == ItemKind.RIGHT_ONLY_FILE);
|| (isLeftOnly() && itemKind == ItemKind.LEFT_ONLY_FILE)
|| (isRightOnly() && itemKind == ItemKind.RIGHT_ONLY_FILE);
}

public int getPropagation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.sudu.experiments.diff.DiffTypes;
import org.sudu.experiments.diff.ItemKind;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;

public class RemoteFolderDiffModel extends FolderDiffModel {
Expand Down Expand Up @@ -57,6 +59,29 @@ public RemoteFolderDiffModel getByPath(String[] path, int ind, boolean left) {
return null;
}

public RemoteFolderDiffModel applyFilter(BitSet filterSet, RemoteFolderDiffModel parent) {
int diffType = getDiffType();
boolean matchFilter = filterSet.get(diffType);
if (!isFile()) matchFilter &= !(diffType == DiffTypes.DEFAULT || diffType == DiffTypes.EDITED);
if (isFile() || children == null) return matchFilter ? this : null;
List<RemoteFolderDiffModel> filteredChildren = new ArrayList<>();
var filteredNode = new RemoteFolderDiffModel(parent, path);
for (int i = 0; i < children.length; i++) {
var child = child(i);
var filteredChild = child.applyFilter(filterSet, filteredNode);
if (filteredChild == null) continue;
filteredChildren.add(filteredChild);
}
if (matchFilter || !filteredChildren.isEmpty()) {
filteredNode.flags = flags;
filteredNode.childrenComparedCnt = childrenComparedCnt;
filteredNode.children = filteredChildren.toArray(RemoteFolderDiffModel[]::new);
filteredNode.posInParent = posInParent;
return filteredNode;
}
return null;
}

public static int[] toInts(
RemoteFolderDiffModel model,
List<String> pathList
Expand All @@ -73,6 +98,7 @@ public static void writeInts(
) {
writer.write(model.flags);
writer.write(model.childrenComparedCnt);
writer.write(model.posInParent);

writer.write(pathList.size());
pathList.add(model.path);
Expand All @@ -96,6 +122,7 @@ public static RemoteFolderDiffModel fromInts(
RemoteFolderDiffModel model = new RemoteFolderDiffModel(parent, null);
model.flags = reader.next();
model.childrenComparedCnt = reader.next();
model.posInParent = reader.next();

int pathInd = reader.next();
model.path = paths[pathInd];
Expand All @@ -105,7 +132,6 @@ public static RemoteFolderDiffModel fromInts(
var children = new RemoteFolderDiffModel[childrenLen];
for (int i = 0; i < childrenLen; i++) {
children[i] = fromInts(reader, paths, model);
children[i].posInParent = i;
}
model.children = children;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static JsArray<JSObject> serialize(
if (message == null)
RemoteFolderDiffModel.writeInts(root, paths, writer);
else
RemoteModelWriter.writeInts(root, paths, message.openedFolders, writer);
RemoteModelWriter.writeInts(root, paths, message.openedFolders, writer, message.filtered);

writer.writeAtPos(pathLenPtr, paths.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public class FrontendMessage {

public FrontendTreeNode openedFolders;
public String searchQuery;
public boolean filtered;

public static final FrontendMessage EMPTY;

static {
EMPTY = new FrontendMessage();
EMPTY.openedFolders = new FrontendTreeNode();
EMPTY.openedFolders.name = "";
EMPTY.openedFolders.isFile = false;
EMPTY.searchQuery = "";
EMPTY.filtered = false;
}

public FrontendTreeNode findNode(int[] path) {
Expand All @@ -50,6 +53,7 @@ public static JsArray<JSObject> serialize(
RemoteFileTreeNode leftRoot,
RemoteFileTreeNode rightRoot,
RemoteFolderDiffModel root,
boolean filtered,
String searchQuery
) {
JsArray<JSObject> result = JsArray.create();
Expand All @@ -58,6 +62,7 @@ public static JsArray<JSObject> serialize(
ArrayWriter writer = new ArrayWriter();
int pathLenInd = writer.getPointer();
writer.write(-1);
writer.write(filtered ? 1 : 0);

serialize(leftRoot, rightRoot, root, writer, paths);
writer.writeAtPos(pathLenInd, paths.size());
Expand All @@ -81,6 +86,7 @@ private static void serialize(
var pathInd = paths.size();
paths.add(model.path);
writer.write(pathInd);
writer.write(model.isFile() ? 1 : 0);

if (leftNode.isClosed() || rightNode.isClosed() || model.children == null)
writer.write(-1);
Expand Down Expand Up @@ -112,6 +118,7 @@ private static void serialize(
var pathInd = paths.size();
paths.add(model.path);
writer.write(pathInd);
writer.write(model.isFile() ? 1 : 0);

if (node.isClosed() || model.children == null) writer.write(-1);
else {
Expand All @@ -130,17 +137,20 @@ public static FrontendMessage deserialize(JsArray<JSObject> jsArray) {
paths[i] = JsCast.string(jsArray, 1 + i);

FrontendMessage message = new FrontendMessage();
message.filtered = reader.next() == 1;
message.openedFolders = deserialize(reader, paths);
message.searchQuery = JsCast.string(jsArray, 1 + pathsLen);
return message;
}

public static FrontendTreeNode deserialize(ArrayReader reader, String[] paths) {
int ind = reader.next();
boolean isFile = reader.next() == 1;
int childLen = reader.next();

FrontendTreeNode node = new FrontendTreeNode();
node.name = paths[ind];
node.isFile = isFile;
if (childLen != -1) {
node.children = new FrontendTreeNode[childLen];
for (int i = 0; i < childLen; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static JsArray<JSObject> serialize(
JsArray<JSObject> result = JsArray.create();
result.set(p++, jsInts(firstVisibleNode, leftSelectedNode, rightSelectedNode));

JsArray<JSObject> serializedFrontend = FrontendMessage.serialize(leftRoot, rightRoot, modelRoot, searchQuery);
JsArray<JSObject> serializedFrontend = FrontendMessage.serialize(leftRoot, rightRoot, modelRoot, false, searchQuery);
JsArray<JSObject> serializedBackend = BackendMessage.serializeFullModel(modelRoot, leftRoot.name(), rightRoot.name());

result.set(p++, jsInts(serializedFrontend.getLength(), serializedBackend.getLength()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

public class FrontendTreeNode {

public String name;
public boolean isFile;
public FrontendTreeNode[] children;

FrontendTreeNode findNode(int[] path) {
Expand Down Expand Up @@ -54,6 +56,13 @@ public void deleteItem(FrontendTreeNode node) {
this.children = newChildren;
}

public FrontendTreeNode child(String path, boolean isFile) {
for (var child: children) {
if (child.name.equals(path) && child.isFile == isFile) return child;
}
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading

0 comments on commit 5894bf2

Please sign in to comment.