Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the duplication nodes with the outline explorer #235

Merged
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A lightweight extension to provide additional Java project explorer features. It

| Setting Name | Description | Default Value |
|---|---|---|
| `java.dependency.showOutline` | Specify whether to show the outline in the dependency viewer. | `true` |
| `java.dependency.showMembers` | Specify whether to show the members in the dependency viewer. | `false` |
| `java.dependency.syncWithFolderExplorer` | Specify whether to sync the folder with dependency viewer when browsering files. | `true` |
| `java.dependency.autoRefresh` | Specify whether to automatically sync the change from editor to the dependency viewer. | `true` |
| `java.dependency.refreshDelay` | The delay time (ms) the auto refresh is invoked when changes are detected. | `2000ms` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
Expand All @@ -60,7 +61,6 @@
import com.microsoft.jdtls.ext.core.model.NodeKind;
import com.microsoft.jdtls.ext.core.model.PackageNode;
import com.microsoft.jdtls.ext.core.model.PackageRootNode;
import com.microsoft.jdtls.ext.core.model.TypeRootNode;

public class PackageCommand {

Expand Down Expand Up @@ -122,7 +122,7 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
List<PackageNode> result = new ArrayList<>();
URI uri = JDTUtils.toURI(typeRootUri);
ITypeRoot typeRoot = ExtUtils.JDT_SCHEME.equals(uri.getScheme()) ? JDTUtils.resolveClassFile(uri) : JDTUtils.resolveCompilationUnit(uri);
if (typeRoot != null) {
if (typeRoot != null && typeRoot.findPrimaryType() != null) {
// Add project node:
result.add(PackageNode.createNodeForProject(typeRoot));
IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
Expand All @@ -135,10 +135,7 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
}
result.add(PackageNode.createNodeForPackageFragmentRoot(pkgRoot));
result.add(PackageNode.createNodeForPackageFragment(packageFragment));

PackageNode item = new TypeRootNode(typeRoot.getElementName(), typeRoot.getPath().toPortableString(), NodeKind.TYPEROOT, TypeRootNode.K_SOURCE);
item.setUri(JDTUtils.toUri(typeRoot));
result.add(item);
result.add(PackageNode.createNodeForPrimaryType(typeRoot.findPrimaryType()));
} else if (ExtUtils.isJarResourceUri(uri)) {
IJarEntryResource resource = ExtUtils.getJarEntryResource(uri);
IPackageFragmentRoot pkgRoot = resource.getPackageFragmentRoot();
Expand Down Expand Up @@ -349,10 +346,23 @@ private static List<PackageNode> getRootTypes(PackageParams query, IProgressMoni
IPackageFragment packageFragment = packageRoot
.getPackageFragment(PackageNode.DEFAULT_PACKAGE_DISPLAYNAME.equals(query.getPath()) ? "" : query.getPath());
if (packageFragment != null) {
IJavaElement[] types = packageFragment.getChildren();
List<IType> primaryTypes = new ArrayList<>();
for (IJavaElement element : packageFragment.getChildren()) {
if (element instanceof ITypeRoot) {
// Filter out the inner class files
if (element instanceof IClassFile && element.getElementName().contains("$")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageFragment.getChildren() returns the immediate children of this package. Will it contain inner class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the Inner class will become: XXX$Inner.class after compilation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically the java class name itself can use $, but i didn't find a good way to judge whether it's inner class. let's keep the current judge, if there is someone reporting the issue, then fix it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

continue;
}
IType primaryType = ((ITypeRoot) element).findPrimaryType();
if (primaryType != null) {
primaryTypes.add(primaryType);
}
}
}
Object[] nonJavaResources = packageFragment.getNonJavaResources();
List<PackageNode> rootTypeNodes = Arrays.stream(types).filter(typeRoot -> !typeRoot.getElementName().contains("$"))
.map(PackageNode::createNodeForTypeRoot).collect(Collectors.toList());
List<PackageNode> rootTypeNodes = primaryTypes.stream()
.map(PackageNode::createNodeForPrimaryType)
.collect(Collectors.toList());
if (nonJavaResources.length == 0) {
return rootTypeNodes;
}
Expand Down Expand Up @@ -451,7 +461,7 @@ private static List<PackageNode> convertToPackageNode(Object[] rootContent, IPac
result.add(entry);
} else if (root instanceof IClassFile) {
IClassFile classFile = (IClassFile) root;
PackageNode entry = new PackageNode(classFile.getElementName(), null, NodeKind.TYPEROOT);
PackageNode entry = new PackageNode(classFile.getElementName(), null, NodeKind.PRIMARYTYPE);
entry.setUri(JDTUtils.toUri(classFile));
result.add(entry);
} else if (root instanceof JarEntryResource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum NodeKind {

PACKAGE(5),

TYPEROOT(6),
PRIMARYTYPE(6),

FOLDER(7),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
package com.microsoft.jdtls.ext.core.model;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
Expand All @@ -39,15 +40,33 @@
* Represent a PackageNode in the project view.
*/
public class PackageNode {

public final static String K_TYPE_KIND = "TypeKind";

/**
* Kind constant for a class.
*/
public final static int K_CLASS = 1;

/**
* Kind constant for an interface.
*/
public final static int K_INTERFACE = 2;

/**
* Kind constant for an enum.
*/
public final static int K_ENUM = 3;

private static final String REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries";
private static final String IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME = "Referenced Libraries (Read-only)";

public static final String REFERENCED_LIBRARIES_PATH = "REFERENCED_LIBRARIES_PATH";
public static final String DEFAULT_PACKAGE_DISPLAYNAME = "(default package)";
public static final ContainerNode REFERENCED_LIBRARIES_CONTAINER = new ContainerNode(REFERENCED_LIBRARIES_CONTAINER_NAME, REFERENCED_LIBRARIES_PATH,
NodeKind.CONTAINER, IClasspathEntry.CPE_CONTAINER);
public static final ContainerNode IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER = new ContainerNode(IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME, REFERENCED_LIBRARIES_PATH,
NodeKind.CONTAINER, IClasspathEntry.CPE_CONTAINER);
public static final ContainerNode IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER = new ContainerNode(IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER_NAME,
REFERENCED_LIBRARIES_PATH, NodeKind.CONTAINER, IClasspathEntry.CPE_CONTAINER);

/**
* The name of the PackageNode
Expand All @@ -74,6 +93,11 @@ public class PackageNode {
*/
private NodeKind kind;

/**
* PackageNode metaData
*/
private Map<String, Object> metaData;

/**
* PackageNode children list
*/
Expand All @@ -83,6 +107,17 @@ public PackageNode() {

}

public Map<String, Object> getMetaData() {
return metaData;
}

public void setMetaDataValue(String key, Object value) {
if (this.metaData == null) {
this.metaData = new HashMap<>();
}
this.metaData.put(key, value);
}

public PackageNode(String name, String path, NodeKind kind) {
this.name = name;
this.path = path;
Expand Down Expand Up @@ -187,15 +222,23 @@ public static PackageNode createNodeForClasspathEntry(IClasspathEntry classpathE
return null;
}

public static PackageNode createNodeForTypeRoot(IJavaElement typeRoot) {
PackageNode typeRootNode = new TypeRootNode(typeRoot.getElementName(), typeRoot.getPath().toPortableString(), NodeKind.TYPEROOT,
typeRoot instanceof IClassFile ? TypeRootNode.K_BINARY : TypeRootNode.K_SOURCE);
if (typeRoot instanceof ICompilationUnit) {
typeRootNode.setUri(JDTUtils.toURI((ICompilationUnit) typeRoot));
} else if (typeRoot instanceof IClassFile) {
typeRootNode.setUri(JDTUtils.toUri((IClassFile) typeRoot));
public static PackageNode createNodeForPrimaryType(IType type) {
PackageNode primaryTypeNode = new PackageNode(type.getElementName(), type.getPath().toPortableString(), NodeKind.PRIMARYTYPE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why create packageNode for IType?

Copy link
Member Author

@jdneo jdneo Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's some unknown historical reason. The Java side code only has the following kinds of nodes:

  • PackageNode
  • ContainerNode
  • PackageRootNode

I'm also confused about the naming. But the fact is that different kind of nodes are all named as PackageNode in the current implementation. One solution could be renaming the PackageNode to ExplorerNode or something else.


try {
if (type.isEnum()) {
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_ENUM);
} else if (type.isInterface()) {
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_INTERFACE);
} else {
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_CLASS);
}
} catch (JavaModelException e) {
primaryTypeNode.setMetaDataValue(K_TYPE_KIND, K_CLASS);
}
return typeRootNode;

primaryTypeNode.setUri(JDTUtils.toUri(type.getTypeRoot()));
return primaryTypeNode;
}

/**
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
"type": "object",
"title": "%configuration.java.dependency.title%",
"properties": {
"java.dependency.showOutline": {
"java.dependency.showMembers": {
"type": "boolean",
"description": "%configuration.java.dependency.showOutline%",
"default": true
"description": "%configuration.java.dependency.showMembers%",
"default": false
},
"java.dependency.syncWithFolderExplorer": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"contributes.commands.java.view.package.copyFilePath": "Copy Path",
"contributes.commands.java.view.package.copyRelativeFilePath": "Copy Relative Path",
"configuration.java.dependency.title": "Java Dependency Configuration",
"configuration.java.dependency.showOutline": "Enable show outline in the Java Dependency explorer",
"configuration.java.dependency.showMembers": "Show the members in the explorer",
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize dependency viewer selection with folder explorer",
"configuration.java.dependency.autoRefresh": "Synchronize dependency viewer with changes",
"configuration.java.dependency.refreshDelay": "The delay time (ms) the auto refresh is invoked when changes are detected",
Expand Down
2 changes: 1 addition & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"contributes.commands.java.view.package.copyFilePath": "复制路径",
"contributes.commands.java.view.package.copyRelativeFilePath": "复制相对路径",
"configuration.java.dependency.title": "Java 依赖管理配置",
"configuration.java.dependency.showOutline": "在 Java 依赖项资源管理器中显示类成员大纲",
"configuration.java.dependency.showMembers": "在 Java 依赖项资源管理器中显示成员",
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 依赖项资源管理器中同步关联当前打开的文件",
"configuration.java.dependency.autoRefresh": "在 Java 依赖项资源管理器中自动同步修改",
"configuration.java.dependency.refreshDelay": "控制Java 依赖项资源管理器刷新的延迟时间 (毫秒)",
Expand Down
9 changes: 8 additions & 1 deletion src/java/nodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ export enum NodeKind {
Container = 3,
PackageRoot = 4,
Package = 5,
TypeRoot = 6,
PrimaryType = 6,
Folder = 7,
File = 8,
}

export enum TypeKind {
Class = 1,
Interface = 2,
Enum = 3,
}

export interface INodeData {
name: string;
moduleName?: string;
path?: string;
uri?: string;
kind: NodeKind;
children?: any[];
metaData?: Map<string, any>;
}
6 changes: 3 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Settings {
}
}));
this.registerConfigurationListener((updatedConfig, oldConfig) => {
if (updatedConfig.showOutline !== oldConfig.showOutline
if (updatedConfig.showMembers !== oldConfig.showMembers
|| updatedConfig.packagePresentation !== oldConfig.packagePresentation
|| (updatedConfig.syncWithFolderExplorer !== oldConfig.syncWithFolderExplorer
&& updatedConfig.syncWithFolderExplorer)) {
Expand Down Expand Up @@ -93,8 +93,8 @@ export class Settings {
}
}

public static showOutline(): boolean {
return this._dependencyConfig.get("showOutline");
public static showMembers(): boolean {
return this._dependencyConfig.get("showMembers");
}

public static autoRefresh(): boolean {
Expand Down
Loading