simplify inner structures

This commit is contained in:
Sergey Malenkov
2018-07-25 22:28:59 +03:00
parent 709a51a56b
commit 20210eb004
@@ -6,8 +6,6 @@ import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VFileProperty;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.ui.tree.BaseTreeModel;
@@ -27,6 +25,8 @@ import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import static com.intellij.openapi.vfs.VFileProperty.SYMLINK;
import static com.intellij.openapi.vfs.VfsUtilCore.isInvalidLink;
import static com.intellij.ui.tree.TreePathUtil.pathToCustomNode;
import static com.intellij.ui.tree.project.ProjectFileNode.findArea;
import static java.util.Collections.emptyList;
@@ -43,7 +43,7 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
protected void update(boolean fromRoot, @NotNull Set<VirtualFile> updatedFiles) {
boolean filtered = root.filter != null;
SmartHashSet<Node> nodes = fromRoot || filtered ? null : new SmartHashSet<>();
root.children.forEach(root -> root.invalidateChildren(node -> {
root.children.forEach(child -> child.invalidateChildren(node -> {
if (!updatedFiles.contains(node.file)) return true;
if (filtered) node.resetParentVisibility();
if (nodes == null) return false;
@@ -167,37 +167,35 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
}
private static final class Mapper<N extends FileNode> implements BiFunction<VirtualFile, Object, N> {
private final HashMap<VirtualFile, N> map = new HashMap<>();
private final BiFunction<? super VirtualFile, ? super Object, ? extends N> function;
private static final class Mapper implements BiFunction<VirtualFile, Object, FileNode> {
private final HashMap<VirtualFile, FileNode> map = new HashMap<>();
Mapper(@NotNull List<N> list, @NotNull BiFunction<? super VirtualFile, ? super Object, ? extends N> function) {
Mapper(@NotNull List<FileNode> list) {
list.forEach(node -> map.put(node.file, node));
this.function = function;
}
@NotNull
@Override
public final N apply(VirtualFile file, Object id) {
N node = map.isEmpty() ? null : map.remove(file);
return node != null && node.id.equals(id) ? node : function.apply(file, id);
public FileNode apply(VirtualFile file, Object id) {
FileNode node = map.isEmpty() ? null : map.remove(file);
return node != null && node.id.equals(id) ? node : new FileNode(file, id);
}
}
private static abstract class Node<FN extends FileNode> {
private static abstract class Node {
volatile Node parent;
volatile ThreeState visibility;
volatile List<FN> children = emptyList();
volatile List<FileNode> children = emptyList();
volatile boolean valid;
@NotNull
abstract List<FN> getChildren(@NotNull List<FN> oldList);
abstract List<FileNode> getChildren(@NotNull List<FileNode> oldList);
final List<FN> getChildren() {
List<FN> oldList = children;
final List<FileNode> getChildren() {
List<FileNode> oldList = children;
if (valid) return oldList;
List<FN> newList = getChildren(oldList);
List<FileNode> newList = getChildren(oldList);
oldList.forEach(node -> node.parent = null);
newList.forEach(node -> node.parent = this);
children = newList;
@@ -226,7 +224,7 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
}
private static class ProjectNode extends Node<RootNode> {
private static class ProjectNode extends Node {
volatile VirtualFileFilter filter;
volatile boolean showModules;
final Project project;
@@ -242,9 +240,9 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
@NotNull
@Override
List<RootNode> getChildren(@NotNull List<RootNode> oldList) {
List<RootNode> list = new SmartList<>();
Mapper<RootNode> mapper = new Mapper<>(oldList, RootNode::new);
List<FileNode> getChildren(@NotNull List<FileNode> oldList) {
List<FileNode> list = new SmartList<>();
Mapper mapper = new Mapper(oldList);
TreeCollector<VirtualFile> collector = showModules ? null : TreeCollector.createFileRootsCollector();
VirtualFile ancestor = project.getBaseDir();
if (ancestor != null && project == findArea(ancestor, project)) {
@@ -271,7 +269,7 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
}
private static class FileNode extends Node<FileNode> implements ProjectFileNode {
private static class FileNode extends Node implements ProjectFileNode {
final VirtualFile file;
final Object id;
@@ -293,7 +291,9 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
@Override
public String toString() {
return file.getName();
return parent instanceof ProjectNode
? file.getPath()
: file.getName();
}
@NotNull
@@ -313,9 +313,9 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
if (children == null || children.length == 0) return emptyList();
List<FileNode> list = new SmartList<>();
Mapper<FileNode> mapper = new Mapper<>(oldList, FileNode::new);
Mapper mapper = new Mapper(oldList);
for (VirtualFile child : children) {
if (child.is(VFileProperty.SYMLINK) && VfsUtilCore.isInvalidLink(child)) {
if (child.is(SYMLINK) && isInvalidLink(child)) {
continue; // ignore invalid symlink
}
Object id = getRootID();
@@ -327,7 +327,7 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
return list;
}
final void invalidateChildren(Predicate<FileNode> validator) {
void invalidateChildren(Predicate<FileNode> validator) {
if (valid || !file.isDirectory()) {
if (validator == null || !validator.test(this)) {
validator = null; // all children will be invalid
@@ -339,16 +339,4 @@ public final class ProjectFileTreeModel extends BaseTreeModel<ProjectFileNode> i
}
}
}
private static class RootNode extends FileNode {
RootNode(@NotNull VirtualFile file, @NotNull Object id) {
super(file, id);
}
@Override
public String toString() {
return file.getPath();
}
}
}