Skip to content

Commit

Permalink
Merge pull request rath#17 from Sheigutn/fix-collapse-bug
Browse files Browse the repository at this point in the history
Fixed bug that made ComponentFileGroup collapse on changes
  • Loading branch information
rath authored Feb 2, 2020
2 parents 3ce35ef + a61db16 commit f45407a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
41 changes: 18 additions & 23 deletions src/com/xrath/plugin/fold/ComponentFileGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,50 @@
import java.util.ArrayList;
import java.util.List;

public class ComponentFileGroup extends ProjectViewNode<PsiFile[]> {
public class ComponentFileGroup extends ProjectViewNode<PsiFile> {
private final String name;
private final String iconType;
private List<AbstractTreeNode> children;
private final List<AbstractTreeNode<?>> children;

protected ComponentFileGroup(Project project, ViewSettings viewSettings,
String name, String iconType) {
super(project, new PsiFile[0], viewSettings);
protected ComponentFileGroup(Project project,
ViewSettings viewSettings,
PsiFile baseFile,
String name,
String iconType) {
super(project, baseFile, viewSettings);
this.name = name;
this.iconType = iconType;
children = new ArrayList<>();
this.children = new ArrayList<>();
}

@Override
public boolean contains(@NotNull VirtualFile file) {
for (AbstractTreeNode childNode : children) {
ProjectViewNode treeNode = (ProjectViewNode) childNode;
for (AbstractTreeNode<?> childNode : children) {
ProjectViewNode<?> treeNode = (ProjectViewNode<?>) childNode;
if (treeNode.contains(file)) {
return true;
}
}
return false;
}

public void addChild(AbstractTreeNode node, String extension) {
public void addChild(AbstractTreeNode<?> node, String extension) {
if (node instanceof PsiFileNode) {
PsiFileNode n = (PsiFileNode) node;
children.add(new NamedFileNode(n.getProject(), n.getValue(), n.getSettings(), "." + extension, n));
}
}

void freezeChildren() {
List<PsiFile> ret = new ArrayList<>();
for (AbstractTreeNode n : children) {
PsiFile file = (PsiFile)n.getValue();
ret.add(file);
}
setValue(ret.toArray(new PsiFile[ret.size()]));
}

@NotNull
@Override
public List<AbstractTreeNode> getChildren() {
public List<AbstractTreeNode<?>> getChildren() {
return children;
}

AbstractTreeNode getOriginalFirstChild() {
AbstractTreeNode<?> getOriginalFirstChild() {
if (children.size() == 0)
return null;
AbstractTreeNode first = children.get(0);
AbstractTreeNode<?> first = children.get(0);
if (first instanceof NamedFileNode) {
return ((NamedFileNode)first).original;
}
Expand All @@ -85,13 +79,14 @@ protected void update(PresentationData presentation) {

static class NamedFileNode extends PsiFileNode {
private final String name;
private final AbstractTreeNode original;
private final AbstractTreeNode<?> original;

public NamedFileNode(Project project, PsiFile psiFile, ViewSettings viewSettings, String name, AbstractTreeNode original) {
public NamedFileNode(Project project, PsiFile psiFile, ViewSettings viewSettings, String name, AbstractTreeNode<?> original) {
super(project, psiFile, viewSettings);
this.name = name;
this.original = original;
}

@Override
public void update(PresentationData presentationData) {
super.update(presentationData);
Expand Down
16 changes: 7 additions & 9 deletions src/com/xrath/plugin/fold/TreeStructureProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class TreeStructureProvider implements com.intellij.ide.projectView.TreeS

@NotNull
@Override
public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent,
@NotNull Collection<AbstractTreeNode> children,
public Collection<AbstractTreeNode<?>> modify(@NotNull AbstractTreeNode<?> parent,
@NotNull Collection<AbstractTreeNode<?>> children,
ViewSettings viewSettings) {
if (!(parent.getValue() instanceof PsiDirectory))
return children;

List<AbstractTreeNode> ret = new ArrayList<>();
List<AbstractTreeNode<?>> ret = new ArrayList<>();
Map<String, ComponentFileGroup> map = new HashMap<>();
for (AbstractTreeNode child : children) {
for (AbstractTreeNode<?> child : children) {
if (!(child.getValue() instanceof PsiFile)) {
ret.add(child);
continue;
Expand All @@ -47,7 +47,7 @@ public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent,
ComponentFileGroup group = map.get(groupKey);
if (group == null) {
group = new ComponentFileGroup(child.getProject(), viewSettings,
prefix + "." + classType, classType);
psiFile, groupKey, classType);
map.put(groupKey, group);
ret.add(group);
}
Expand All @@ -56,13 +56,11 @@ public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent,

// Undo grouping that has only child
for (int i = 0, len = ret.size(); i < len; i++) {
AbstractTreeNode added = ret.get(i);
AbstractTreeNode<?> added = ret.get(i);
if (added instanceof ComponentFileGroup) {
ComponentFileGroup g = (ComponentFileGroup) added;
if (g.getChildrenCount() <= 1) {
ret.set(i, g.getOriginalFirstChild());
} else {
g.freezeChildren();
}
}
}
Expand All @@ -72,7 +70,7 @@ public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent,

@Nullable
@Override
public Object getData(Collection<AbstractTreeNode> selected, String dataName) {
public Object getData(Collection<AbstractTreeNode<?>> selected, String dataName) {
return null;
}
}

0 comments on commit f45407a

Please sign in to comment.