Skip to content

Commit 0e25546

Browse files
authored
Fix hierarchical package view (#132)
* fix issues about hierarchical view * add null check for when reveal projects
1 parent 515fbeb commit 0e25546

8 files changed

+54
-43
lines changed

src/java/hierachicalPackageNodeData.ts renamed to src/java/hierarchicalPackageNodeData.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
import { INodeData, NodeKind } from "./nodeData";
55

6-
export class HierachicalPackageNodeData implements INodeData {
6+
export class HierarchicalPackageNodeData implements INodeData {
77

8-
public static createHierachicalNodeDataByPackageList(packageList: INodeData[]): HierachicalPackageNodeData {
9-
const result = new HierachicalPackageNodeData("", "");
8+
public static createHierarchicalNodeDataByPackageList(packageList: INodeData[]): HierarchicalPackageNodeData {
9+
const result = new HierarchicalPackageNodeData("", "");
1010
packageList.forEach((nodeData) => result.addSubPackage(nodeData.name.split("."), nodeData));
1111
result.compressTree();
1212
return result;
@@ -62,9 +62,9 @@ export class HierachicalPackageNodeData implements INodeData {
6262
const subPackageDisplayName = packages.shift();
6363
const childNode = this.children.find((child) => child.displayName === subPackageDisplayName);
6464
if (childNode) {
65-
childNode.addSubPackage(packages);
65+
childNode.addSubPackage(packages, nodeData);
6666
} else {
67-
const newNode = new HierachicalPackageNodeData(subPackageDisplayName, this.name);
67+
const newNode = new HierarchicalPackageNodeData(subPackageDisplayName, this.name);
6868
newNode.addSubPackage(packages, nodeData);
6969
this.children.push(newNode);
7070
}

src/views/dataNode.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ export abstract class DataNode extends ExplorerNode {
3434

3535
public async revealPaths(paths: INodeData[]): Promise<DataNode> {
3636
const childNodeData = paths.shift();
37-
const childs: ExplorerNode[] = await this.getChildren();
38-
const childNode = <DataNode>childs.find((child: DataNode) => child.nodeData.name === childNodeData.name && child.path === childNodeData.path);
39-
return childNode === null ? null : (paths.length ? childNode.revealPaths(paths) : childNode);
37+
const children: ExplorerNode[] = await this.getChildren();
38+
const childNode = children ? <DataNode>children.find((child: DataNode) =>
39+
child.nodeData.name === childNodeData.name && child.path === childNodeData.path) : undefined;
40+
return (childNode && paths.length) ? childNode.revealPaths(paths) : childNode;
4041
}
4142

4243
public getChildren(): ProviderResult<ExplorerNode[]> {

src/views/dependencyDataProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
7171
public async revealPaths(paths: INodeData[]): Promise<DataNode> {
7272
const projectNodeData = paths.shift();
7373
const projects = await this.getRootProjects();
74-
const correspondProject = <DataNode>projects.find((node: DataNode) =>
75-
node.path === projectNodeData.path && node.nodeData.name === projectNodeData.name);
76-
return correspondProject.revealPaths(paths);
74+
const project = projects ? <DataNode>projects.find((node: DataNode) =>
75+
node.path === projectNodeData.path && node.nodeData.name === projectNodeData.name) : undefined;
76+
return project ? project.revealPaths(paths) : null;
7777
}
7878

7979
private async getRootProjects(): Promise<ExplorerNode[]> {
@@ -82,8 +82,8 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
8282
return rootElements;
8383
} else {
8484
let result = [];
85-
for (const singleworkspace of rootElements) {
86-
const projects = await singleworkspace.getChildren();
85+
for (const rootWorkspace of rootElements) {
86+
const projects = await rootWorkspace.getChildren();
8787
result = result.concat(projects);
8888
}
8989
return result;

src/views/dependencyExplorer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ export class DependencyExplorer {
3333
this._selectionWhenHidden = undefined;
3434
}
3535
});
36+
37+
this._dataProvider.onDidChangeTreeData(() => {
38+
const currentDocument = window.activeTextEditor.document.uri;
39+
if (currentDocument) {
40+
this.reveal(currentDocument);
41+
}
42+
});
3643
}
3744

3845
public dispose(): void {

src/views/fileNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class FileNode extends DataNode {
1717
}
1818

1919
protected loadData(): Thenable<INodeData[]> {
20-
return null;
20+
return Promise.resolve(null);
2121
}
2222

2323
protected createChildNodeList(): ExplorerNode[] {

src/views/hierachicalPackageNode.ts renamed to src/views/hierarchicalPackageNode.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license.
33

44
import { ProviderResult, TreeItem, TreeItemCollapsibleState } from "vscode";
5-
import { HierachicalPackageNodeData } from "../java/hierachicalPackageNodeData";
5+
import { HierarchicalPackageNodeData } from "../java/hierarchicalPackageNodeData";
66
import { INodeData, NodeKind } from "../java/nodeData";
77
import { Telemetry } from "../telemetry";
88
import { DataNode } from "./dataNode";
@@ -12,7 +12,7 @@ import { PackageNode } from "./packageNode";
1212
import { ProjectNode } from "./projectNode";
1313
import { TypeRootNode } from "./typeRootNode";
1414

15-
export class HierachicalPackageNode extends PackageNode {
15+
export class HierarchicalPackageNode extends PackageNode {
1616

1717
constructor(nodeData: INodeData, parent: DataNode, protected _project: ProjectNode, protected _rootNode: DataNode) {
1818
super(nodeData, parent, _project, _rootNode);
@@ -33,25 +33,24 @@ export class HierachicalPackageNode extends PackageNode {
3333
if (!res) {
3434
Telemetry.sendEvent("load data get undefined result", { node_kind: this.nodeData.kind.toString() });
3535
} else {
36-
// Combine hierachical children and normal packagenode children
36+
// Combine hierarchical children and normal package node children
3737
res.forEach((node) => this.nodeData.children.push(node));
3838
}
3939
return this.createChildNodeList();
4040
});
4141
}
4242

4343
public async revealPaths(paths: INodeData[]): Promise<DataNode> {
44-
const hierachicalNodeData = paths[0];
45-
if (hierachicalNodeData.name === this.nodeData.name) {
44+
const hierarchicalNodeData = paths[0];
45+
if (hierarchicalNodeData.name === this.nodeData.name) {
4646
paths.shift();
4747
// reveal as a package node
4848
return super.revealPaths(paths);
4949
} else {
50-
// reveal as a package root node
51-
const childs: ExplorerNode[] = await this.getChildren();
52-
const childNode = <DataNode>childs.find((child: DataNode) =>
53-
child instanceof HierachicalPackageNode && hierachicalNodeData.name.startsWith(child.nodeData.name));
54-
return childNode === null ? null : childNode.revealPaths(paths);
50+
const children: ExplorerNode[] = await this.getChildren();
51+
const childNode = <DataNode>children.find((child: DataNode) =>
52+
hierarchicalNodeData.name.startsWith(child.nodeData.name + ".") || hierarchicalNodeData.name === child.nodeData.name);
53+
return childNode ? childNode.revealPaths(paths) : null;
5554
}
5655
}
5756

@@ -67,8 +66,8 @@ export class HierachicalPackageNode extends PackageNode {
6766
this.nodeData.children.forEach((nodeData) => {
6867
if (nodeData.kind === NodeKind.File) {
6968
result.push(new FileNode(nodeData, this));
70-
} else if (nodeData instanceof HierachicalPackageNodeData) {
71-
result.push(new HierachicalPackageNode(nodeData, this, this._project, this._rootNode));
69+
} else if (nodeData instanceof HierarchicalPackageNodeData) {
70+
result.push(new HierarchicalPackageNode(nodeData, this, this._project, this._rootNode));
7271
} else {
7372
result.push(new TypeRootNode(nodeData, this));
7473
}
@@ -77,7 +76,7 @@ export class HierachicalPackageNode extends PackageNode {
7776
return result;
7877
}
7978

80-
private getHierarchicalNodeData(): HierachicalPackageNodeData {
81-
return <HierachicalPackageNodeData>this.nodeData;
79+
private getHierarchicalNodeData(): HierarchicalPackageNodeData {
80+
return <HierarchicalPackageNodeData>this.nodeData;
8281
}
8382
}

src/views/hierachicalPackageRootNode.ts renamed to src/views/hierarchicalPackageRootNode.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { HierachicalPackageNodeData } from "../java/hierachicalPackageNodeData";
4+
import { HierarchicalPackageNodeData } from "../java/hierarchicalPackageNodeData";
55
import { INodeData, NodeKind } from "../java/nodeData";
66
import { DataNode } from "./dataNode";
77
import { ExplorerNode } from "./explorerNode";
88
import { FileNode } from "./fileNode";
99
import { FolderNode } from "./folderNode";
10-
import { HierachicalPackageNode } from "./hierachicalPackageNode";
10+
import { HierarchicalPackageNode } from "./hierarchicalPackageNode";
1111
import { PackageRootNode } from "./packageRootNode";
1212
import { ProjectNode } from "./projectNode";
1313
import { TypeRootNode } from "./typeRootNode";
1414

15-
export class HierachicalPackageRootNode extends PackageRootNode {
15+
export class HierarchicalPackageRootNode extends PackageRootNode {
1616

1717
constructor(nodeData: INodeData, parent: DataNode, _project: ProjectNode) {
1818
super(nodeData, parent, _project);
1919
}
2020

2121
public async revealPaths(paths: INodeData[]): Promise<DataNode> {
22-
const hierachicalNodeData = paths[0];
23-
const childs: ExplorerNode[] = await this.getChildren();
24-
const childNode = <DataNode>childs.find((child: DataNode) =>
25-
child instanceof HierachicalPackageNode && hierachicalNodeData.name.startsWith(child.nodeData.name));
26-
return childNode === null ? null : childNode.revealPaths(paths);
22+
const hierarchicalNodeData = paths[0];
23+
const children: ExplorerNode[] = await this.getChildren();
24+
const childNode = <DataNode>children.find((child: DataNode) =>
25+
hierarchicalNodeData.name.startsWith(child.nodeData.name + ".") || hierarchicalNodeData.name === child.nodeData.name);
26+
// don't shift when child node is an hierarchical node, or it may lose data of package node
27+
if (!(childNode instanceof HierarchicalPackageNode)) {
28+
paths.shift();
29+
}
30+
return (childNode && paths.length > 0) ? childNode.revealPaths(paths) : childNode;
2731
}
2832

2933
protected createChildNodeList(): ExplorerNode[] {
@@ -44,16 +48,16 @@ export class HierachicalPackageRootNode extends PackageRootNode {
4448
}
4549

4650
protected getHierarchicalPackageNodes(): ExplorerNode[] {
47-
const hierachicalPackageNodeData = this.getHierarchicalPackageNodeData();
48-
return hierachicalPackageNodeData === null ? [] : hierachicalPackageNodeData.children.map((hierachicalChildrenNode) =>
49-
new HierachicalPackageNode(hierachicalChildrenNode, this, this._project, this));
51+
const hierarchicalPackageNodeData = this.getHierarchicalPackageNodeData();
52+
return hierarchicalPackageNodeData === null ? [] : hierarchicalPackageNodeData.children.map((hierarchicalChildrenNode) =>
53+
new HierarchicalPackageNode(hierarchicalChildrenNode, this, this._project, this));
5054
}
5155

52-
private getHierarchicalPackageNodeData(): HierachicalPackageNodeData {
56+
private getHierarchicalPackageNodeData(): HierarchicalPackageNodeData {
5357
if (this.nodeData.children && this.nodeData.children.length) {
5458
const nodeDataList = this.nodeData.children
5559
.filter((child) => child.kind === NodeKind.Package);
56-
return HierachicalPackageNodeData.createHierachicalNodeDataByPackageList(nodeDataList);
60+
return HierarchicalPackageNodeData.createHierarchicalNodeDataByPackageList(nodeDataList);
5761
} else {
5862
return null;
5963
}

src/views/nodeFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import { INodeData } from "../java/nodeData";
55
import { Settings } from "../settings";
66
import { DataNode } from "./dataNode";
7-
import { HierachicalPackageRootNode } from "./hierachicalPackageRootNode";
7+
import { HierarchicalPackageRootNode } from "./hierarchicalPackageRootNode";
88
import { PackageRootNode } from "./packageRootNode";
99
import { ProjectNode } from "./projectNode";
1010

1111
export class NodeFactory {
1212
public static createPackageRootNode(nodeData: INodeData, parent: DataNode, project: ProjectNode): PackageRootNode {
1313
return Settings.isHierarchicalView() ?
14-
new HierachicalPackageRootNode(nodeData, parent, project) : new PackageRootNode(nodeData, parent, project);
14+
new HierarchicalPackageRootNode(nodeData, parent, project) : new PackageRootNode(nodeData, parent, project);
1515
}
1616
}

0 commit comments

Comments
 (0)