Skip to content

Commit 652790c

Browse files
authored
Remove the duplication nodes with the outline explorer (#235)
1 parent aae6ce8 commit 652790c

19 files changed

+215
-197
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A lightweight extension to provide additional Java project explorer features. It
2727

2828
| Setting Name | Description | Default Value |
2929
|---|---|---|
30-
| `java.dependency.showOutline` | Specify whether to show the outline in the dependency viewer. | `true` |
30+
| `java.dependency.showMembers` | Specify whether to show the members in the dependency viewer. | `false` |
3131
| `java.dependency.syncWithFolderExplorer` | Specify whether to sync the folder with dependency viewer when browsering files. | `true` |
3232
| `java.dependency.autoRefresh` | Specify whether to automatically sync the change from editor to the dependency viewer. | `true` |
3333
| `java.dependency.refreshDelay` | The delay time (ms) the auto refresh is invoked when changes are detected. | `2000ms` |

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/PackageCommand.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.eclipse.jdt.core.IJavaProject;
4444
import org.eclipse.jdt.core.IPackageFragment;
4545
import org.eclipse.jdt.core.IPackageFragmentRoot;
46+
import org.eclipse.jdt.core.IType;
4647
import org.eclipse.jdt.core.ITypeRoot;
4748
import org.eclipse.jdt.core.JavaCore;
4849
import org.eclipse.jdt.core.JavaModelException;
@@ -60,7 +61,6 @@
6061
import com.microsoft.jdtls.ext.core.model.NodeKind;
6162
import com.microsoft.jdtls.ext.core.model.PackageNode;
6263
import com.microsoft.jdtls.ext.core.model.PackageRootNode;
63-
import com.microsoft.jdtls.ext.core.model.TypeRootNode;
6464

6565
public class PackageCommand {
6666

@@ -122,7 +122,7 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
122122
List<PackageNode> result = new ArrayList<>();
123123
URI uri = JDTUtils.toURI(typeRootUri);
124124
ITypeRoot typeRoot = ExtUtils.JDT_SCHEME.equals(uri.getScheme()) ? JDTUtils.resolveClassFile(uri) : JDTUtils.resolveCompilationUnit(uri);
125-
if (typeRoot != null) {
125+
if (typeRoot != null && typeRoot.findPrimaryType() != null) {
126126
// Add project node:
127127
result.add(PackageNode.createNodeForProject(typeRoot));
128128
IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
@@ -135,10 +135,7 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
135135
}
136136
result.add(PackageNode.createNodeForPackageFragmentRoot(pkgRoot));
137137
result.add(PackageNode.createNodeForPackageFragment(packageFragment));
138-
139-
PackageNode item = new TypeRootNode(typeRoot.getElementName(), typeRoot.getPath().toPortableString(), NodeKind.TYPEROOT, TypeRootNode.K_SOURCE);
140-
item.setUri(JDTUtils.toUri(typeRoot));
141-
result.add(item);
138+
result.add(PackageNode.createNodeForPrimaryType(typeRoot.findPrimaryType()));
142139
} else if (ExtUtils.isJarResourceUri(uri)) {
143140
IJarEntryResource resource = ExtUtils.getJarEntryResource(uri);
144141
IPackageFragmentRoot pkgRoot = resource.getPackageFragmentRoot();
@@ -349,10 +346,23 @@ private static List<PackageNode> getRootTypes(PackageParams query, IProgressMoni
349346
IPackageFragment packageFragment = packageRoot
350347
.getPackageFragment(PackageNode.DEFAULT_PACKAGE_DISPLAYNAME.equals(query.getPath()) ? "" : query.getPath());
351348
if (packageFragment != null) {
352-
IJavaElement[] types = packageFragment.getChildren();
349+
List<IType> primaryTypes = new ArrayList<>();
350+
for (IJavaElement element : packageFragment.getChildren()) {
351+
if (element instanceof ITypeRoot) {
352+
// Filter out the inner class files
353+
if (element instanceof IClassFile && element.getElementName().contains("$")) {
354+
continue;
355+
}
356+
IType primaryType = ((ITypeRoot) element).findPrimaryType();
357+
if (primaryType != null) {
358+
primaryTypes.add(primaryType);
359+
}
360+
}
361+
}
353362
Object[] nonJavaResources = packageFragment.getNonJavaResources();
354-
List<PackageNode> rootTypeNodes = Arrays.stream(types).filter(typeRoot -> !typeRoot.getElementName().contains("$"))
355-
.map(PackageNode::createNodeForTypeRoot).collect(Collectors.toList());
363+
List<PackageNode> rootTypeNodes = primaryTypes.stream()
364+
.map(PackageNode::createNodeForPrimaryType)
365+
.collect(Collectors.toList());
356366
if (nonJavaResources.length == 0) {
357367
return rootTypeNodes;
358368
}
@@ -451,7 +461,7 @@ private static List<PackageNode> convertToPackageNode(Object[] rootContent, IPac
451461
result.add(entry);
452462
} else if (root instanceof IClassFile) {
453463
IClassFile classFile = (IClassFile) root;
454-
PackageNode entry = new PackageNode(classFile.getElementName(), null, NodeKind.TYPEROOT);
464+
PackageNode entry = new PackageNode(classFile.getElementName(), null, NodeKind.PRIMARYTYPE);
455465
entry.setUri(JDTUtils.toUri(classFile));
456466
result.add(entry);
457467
} else if (root instanceof JarEntryResource) {

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/NodeKind.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public enum NodeKind {
2222

2323
PACKAGE(5),
2424

25-
TYPEROOT(6),
25+
PRIMARYTYPE(6),
2626

2727
FOLDER(7),
2828

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/PackageNode.java

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212
package com.microsoft.jdtls.ext.core.model;
1313

1414
import java.net.URI;
15+
import java.util.HashMap;
1516
import java.util.List;
17+
import java.util.Map;
1618

1719
import org.eclipse.core.resources.IFile;
1820
import org.eclipse.core.resources.IFolder;
1921
import org.eclipse.core.resources.IProject;
2022
import org.eclipse.core.resources.IResource;
2123
import org.eclipse.core.runtime.CoreException;
2224
import org.eclipse.core.runtime.IPath;
23-
import org.eclipse.jdt.core.IClassFile;
2425
import org.eclipse.jdt.core.IClasspathContainer;
2526
import org.eclipse.jdt.core.IClasspathEntry;
26-
import org.eclipse.jdt.core.ICompilationUnit;
2727
import org.eclipse.jdt.core.IJavaElement;
2828
import org.eclipse.jdt.core.IJavaProject;
2929
import org.eclipse.jdt.core.IPackageFragment;
3030
import org.eclipse.jdt.core.IPackageFragmentRoot;
31+
import org.eclipse.jdt.core.IType;
3132
import org.eclipse.jdt.core.JavaCore;
3233
import org.eclipse.jdt.core.JavaModelException;
3334
import org.eclipse.jdt.ls.core.internal.JDTUtils;
@@ -39,15 +40,33 @@
3940
* Represent a PackageNode in the project view.
4041
*/
4142
public class PackageNode {
43+
44+
public final static String K_TYPE_KIND = "TypeKind";
45+
46+
/**
47+
* Kind constant for a class.
48+
*/
49+
public final static int K_CLASS = 1;
50+
51+
/**
52+
* Kind constant for an interface.
53+
*/
54+
public final static int K_INTERFACE = 2;
55+
56+
/**
57+
* Kind constant for an enum.
58+
*/
59+
public final static int K_ENUM = 3;
60+
4261
private static final String REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries";
4362
private static final String IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries (Read-only)";
4463

4564
public static final String REFERENCED_LIBRARIES_PATH = "REFERENCED_LIBRARIES_PATH";
4665
public static final String DEFAULT_PACKAGE_DISPLAYNAME = "(default package)";
4766
public static final ContainerNode REFERENCED_LIBRARIES_CONTAINER = new ContainerNode(REFERENCED_LIBRARIES_CONTAINER_NAME, REFERENCED_LIBRARIES_PATH,
4867
NodeKind.CONTAINER, IClasspathEntry.CPE_CONTAINER);
49-
public static final ContainerNode IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER = new ContainerNode(IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME, REFERENCED_LIBRARIES_PATH,
50-
NodeKind.CONTAINER, IClasspathEntry.CPE_CONTAINER);
68+
public static final ContainerNode IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER = new ContainerNode(IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME,
69+
REFERENCED_LIBRARIES_PATH, NodeKind.CONTAINER, IClasspathEntry.CPE_CONTAINER);
5170

5271
/**
5372
* The name of the PackageNode
@@ -74,6 +93,11 @@ public class PackageNode {
7493
*/
7594
private NodeKind kind;
7695

96+
/**
97+
* PackageNode metaData
98+
*/
99+
private Map<String, Object> metaData;
100+
77101
/**
78102
* PackageNode children list
79103
*/
@@ -83,6 +107,17 @@ public PackageNode() {
83107

84108
}
85109

110+
public Map<String, Object> getMetaData() {
111+
return metaData;
112+
}
113+
114+
public void setMetaDataValue(String key, Object value) {
115+
if (this.metaData == null) {
116+
this.metaData = new HashMap<>();
117+
}
118+
this.metaData.put(key, value);
119+
}
120+
86121
public PackageNode(String name, String path, NodeKind kind) {
87122
this.name = name;
88123
this.path = path;
@@ -187,15 +222,23 @@ public static PackageNode createNodeForClasspathEntry(IClasspathEntry classpathE
187222
return null;
188223
}
189224

190-
public static PackageNode createNodeForTypeRoot(IJavaElement typeRoot) {
191-
PackageNode typeRootNode = new TypeRootNode(typeRoot.getElementName(), typeRoot.getPath().toPortableString(), NodeKind.TYPEROOT,
192-
typeRoot instanceof IClassFile ? TypeRootNode.K_BINARY : TypeRootNode.K_SOURCE);
193-
if (typeRoot instanceof ICompilationUnit) {
194-
typeRootNode.setUri(JDTUtils.toURI((ICompilationUnit) typeRoot));
195-
} else if (typeRoot instanceof IClassFile) {
196-
typeRootNode.setUri(JDTUtils.toUri((IClassFile) typeRoot));
225+
public static PackageNode createNodeForPrimaryType(IType type) {
226+
PackageNode primaryTypeNode = new PackageNode(type.getElementName(), type.getPath().toPortableString(), NodeKind.PRIMARYTYPE);
227+
228+
try {
229+
if (type.isEnum()) {
230+
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_ENUM);
231+
} else if (type.isInterface()) {
232+
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_INTERFACE);
233+
} else {
234+
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_CLASS);
235+
}
236+
} catch (JavaModelException e) {
237+
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_CLASS);
197238
}
198-
return typeRootNode;
239+
240+
primaryTypeNode.setUri(JDTUtils.toUri(type.getTypeRoot()));
241+
return primaryTypeNode;
199242
}
200243

201244
/**

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/model/TypeRootNode.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@
119119
"type": "object",
120120
"title": "%configuration.java.dependency.title%",
121121
"properties": {
122-
"java.dependency.showOutline": {
122+
"java.dependency.showMembers": {
123123
"type": "boolean",
124-
"description": "%configuration.java.dependency.showOutline%",
125-
"default": true
124+
"description": "%configuration.java.dependency.showMembers%",
125+
"default": false
126126
},
127127
"java.dependency.syncWithFolderExplorer": {
128128
"type": "boolean",

package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"contributes.commands.java.view.package.copyFilePath": "Copy Path",
1515
"contributes.commands.java.view.package.copyRelativeFilePath": "Copy Relative Path",
1616
"configuration.java.dependency.title": "Java Dependency Configuration",
17-
"configuration.java.dependency.showOutline": "Enable show outline in the Java Dependency explorer",
17+
"configuration.java.dependency.showMembers": "Show the members in the explorer",
1818
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize dependency viewer selection with folder explorer",
1919
"configuration.java.dependency.autoRefresh": "Synchronize dependency viewer with changes",
2020
"configuration.java.dependency.refreshDelay": "The delay time (ms) the auto refresh is invoked when changes are detected",

package.nls.zh.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"contributes.commands.java.view.package.copyFilePath": "复制路径",
1515
"contributes.commands.java.view.package.copyRelativeFilePath": "复制相对路径",
1616
"configuration.java.dependency.title": "Java 依赖管理配置",
17-
"configuration.java.dependency.showOutline": "在 Java 依赖项资源管理器中显示类成员大纲",
17+
"configuration.java.dependency.showMembers": "在 Java 依赖项资源管理器中显示成员",
1818
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 依赖项资源管理器中同步关联当前打开的文件",
1919
"configuration.java.dependency.autoRefresh": "在 Java 依赖项资源管理器中自动同步修改",
2020
"configuration.java.dependency.refreshDelay": "控制Java 依赖项资源管理器刷新的延迟时间 (毫秒)",

src/java/nodeData.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ export enum NodeKind {
77
Container = 3,
88
PackageRoot = 4,
99
Package = 5,
10-
TypeRoot = 6,
10+
PrimaryType = 6,
1111
Folder = 7,
1212
File = 8,
1313
}
1414

15+
export enum TypeKind {
16+
Class = 1,
17+
Interface = 2,
18+
Enum = 3,
19+
}
20+
1521
export interface INodeData {
1622
name: string;
1723
moduleName?: string;
1824
path?: string;
1925
uri?: string;
2026
kind: NodeKind;
2127
children?: any[];
28+
metaData?: Map<string, any>;
2229
}

src/settings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class Settings {
2323
}
2424
}));
2525
this.registerConfigurationListener((updatedConfig, oldConfig) => {
26-
if (updatedConfig.showOutline !== oldConfig.showOutline
26+
if (updatedConfig.showMembers !== oldConfig.showMembers
2727
|| updatedConfig.packagePresentation !== oldConfig.packagePresentation
2828
|| (updatedConfig.syncWithFolderExplorer !== oldConfig.syncWithFolderExplorer
2929
&& updatedConfig.syncWithFolderExplorer)) {
@@ -93,8 +93,8 @@ export class Settings {
9393
}
9494
}
9595

96-
public static showOutline(): boolean {
97-
return this._dependencyConfig.get("showOutline");
96+
public static showMembers(): boolean {
97+
return this._dependencyConfig.get("showMembers");
9898
}
9999

100100
public static autoRefresh(): boolean {

0 commit comments

Comments
 (0)