Skip to content

Commit f646ea6

Browse files
Vigilansjdneo
andauthored
Do not display workspace link '_' for invisible project (#210)
Co-authored-by: Sheng Chen <[email protected]>
1 parent 1409594 commit f646ea6

File tree

10 files changed

+126
-63
lines changed

10 files changed

+126
-63
lines changed

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
134134
if (isClassFile) {
135135
result.add(PackageNode.createNodeForVirtualContainer(pkgRoot));
136136
}
137-
result.add(PackageNode.createNodeForPackageFragmentRoot(pkgRoot));
137+
// for invisible project, removing the '_' link name may cause an empty named package root
138+
// in this case, we will avoid that 'empty' node from displaying
139+
PackageNode pkgRootNode = PackageNode.createNodeForPackageFragmentRoot(pkgRoot);
140+
if (StringUtils.isNotBlank(pkgRootNode.getName())) {
141+
result.add(pkgRootNode);
142+
}
138143
result.add(PackageNode.createNodeForPackageFragment(packageFragment));
139144
result.add(PackageNode.createNodeForPrimaryType(typeRoot.findPrimaryType()));
140145
} else if (ExtUtils.isJarResourceUri(uri)) {
@@ -171,15 +176,16 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
171176
IJavaElement parentJavaElement = JavaCore.create(parent);
172177
if (parent instanceof IFolder && parentJavaElement instanceof IPackageFragment) {
173178
IPackageFragment packageFragment = (IPackageFragment) parentJavaElement;
174-
IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) packageFragment.getParent();
175-
PackageNode rootNode = null;
176-
177-
rootNode = new PackageRootNode(pkgRoot,
178-
ExtUtils.removeProjectSegment(packageFragment.getJavaProject().getElementName(), pkgRoot.getPath()).toPortableString(),
179-
NodeKind.PACKAGEROOT);
180179

181180
result.add(PackageNode.createNodeForProject(packageFragment));
182-
result.add(rootNode);
181+
182+
IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
183+
// for invisible project, removing the '_' link name may cause an empty named package root
184+
// in this case, we will avoid that 'empty' node from displaying
185+
PackageNode pkgRootNode = PackageNode.createNodeForPackageFragmentRoot(pkgRoot);
186+
if (StringUtils.isNotBlank(pkgRootNode.getName())) {
187+
result.add(pkgRootNode);
188+
}
183189
result.add(PackageNode.createNodeForPackageFragment(packageFragment));
184190

185191
PackageNode item = new PackageNode(resource.getName(), resource.getFullPath().toPortableString(), NodeKind.FILE);
@@ -276,24 +282,30 @@ private static List<PackageNode> getPackageFragmentRoots(PackageParams query, IP
276282
if (containerEntry != null) {
277283
IPackageFragmentRoot[] packageFragmentRoots = javaProject.findPackageFragmentRoots(containerEntry);
278284
for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots) {
279-
String displayName = fragmentRoot.getElementName();
280-
if (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
281-
displayName = ExtUtils.removeProjectSegment(javaProject.getElementName(), fragmentRoot.getPath()).toPortableString();
282-
}
283-
PackageRootNode node = new PackageRootNode(fragmentRoot, displayName, NodeKind.PACKAGEROOT);
284-
node.setHandlerIdentifier(fragmentRoot.getHandleIdentifier());
285-
children.add(node);
286-
if (fragmentRoot instanceof JrtPackageFragmentRoot) {
287-
node.setModuleName(fragmentRoot.getModuleDescription().getElementName());
288-
}
289-
290-
IClasspathEntry resolvedClasspathEntry = fragmentRoot.getResolvedClasspathEntry();
291-
if (resolvedClasspathEntry != null) {
292-
Map<String, String> attributes = new HashMap<>();
293-
for (IClasspathAttribute attribute : resolvedClasspathEntry.getExtraAttributes()) {
294-
attributes.put(attribute.getName(), attribute.getValue());
285+
PackageRootNode node = PackageNode.createNodeForPackageFragmentRoot(fragmentRoot);
286+
if (StringUtils.isNotBlank(node.getName())) {
287+
node.setHandlerIdentifier(fragmentRoot.getHandleIdentifier());
288+
if (fragmentRoot instanceof JrtPackageFragmentRoot) {
289+
node.setModuleName(fragmentRoot.getModuleDescription().getElementName());
295290
}
296-
node.setAttributes(attributes);
291+
292+
IClasspathEntry resolvedClasspathEntry = fragmentRoot.getResolvedClasspathEntry();
293+
if (resolvedClasspathEntry != null) {
294+
Map<String, String> attributes = new HashMap<>();
295+
for (IClasspathAttribute attribute : resolvedClasspathEntry.getExtraAttributes()) {
296+
attributes.put(attribute.getName(), attribute.getValue());
297+
}
298+
node.setAttributes(attributes);
299+
}
300+
301+
children.add(node);
302+
} else {
303+
// for invisible project, the package root name may become empty after removing the '_',
304+
// in this case, we skip this root node from showing in the explorer and keep finding its children.
305+
PackageParams subQuery = new PackageParams(NodeKind.PACKAGEROOT, query.getProjectUri(),
306+
query.getPath(), fragmentRoot.getHandleIdentifier());
307+
List<PackageNode> packageNodes = getPackages(subQuery, pm);
308+
children.addAll(packageNodes);
297309
}
298310
}
299311
return children;

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,22 @@ public class PackageParams {
2727

2828
private String handlerIdentifier;
2929

30-
private String rootPath;
31-
3230
public PackageParams() {
3331
}
3432

35-
public String getHandlerIdentifier() {
36-
return handlerIdentifier;
37-
}
38-
39-
public void setHandlerIdentifier(String handlerIdentifier) {
40-
this.handlerIdentifier = handlerIdentifier;
41-
}
42-
4333
public PackageParams(NodeKind kind, String projectUri) {
4434
this.kind = kind;
4535
this.projectUri = projectUri;
4636
}
4737

4838
public PackageParams(NodeKind kind, String projectUri, String path) {
49-
this.kind = kind;
50-
this.projectUri = projectUri;
39+
this(kind, projectUri);
5140
this.path = path;
5241
}
5342

54-
public PackageParams(NodeKind kind, String projectUri, String path, String rootPath) {
55-
this.kind = kind;
56-
this.projectUri = projectUri;
57-
this.path = path;
58-
this.rootPath = rootPath;
43+
public PackageParams(NodeKind kind, String projectUri, String path, String handlerIdentifier) {
44+
this(kind, projectUri, path);
45+
this.handlerIdentifier = handlerIdentifier;
5946
}
6047

6148
public NodeKind getKind() {
@@ -82,11 +69,12 @@ public void setPath(String nodePath) {
8269
this.path = nodePath;
8370
}
8471

85-
public String getRootPath() {
86-
return rootPath;
72+
public String getHandlerIdentifier() {
73+
return handlerIdentifier;
8774
}
8875

89-
public void setRootPath(String rootPath) {
90-
this.rootPath = rootPath;
76+
public void setHandlerIdentifier(String handlerIdentifier) {
77+
this.handlerIdentifier = handlerIdentifier;
9178
}
79+
9280
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.File;
1515
import java.io.FileOutputStream;
1616
import java.util.ArrayList;
17+
import java.util.Collections;
1718
import java.util.HashSet;
1819
import java.util.List;
1920
import java.util.Objects;
@@ -84,13 +85,19 @@ public MainClassInfo(String name, String path) {
8485

8586
public static List<PackageNode> listProjects(List<Object> arguments, IProgressMonitor monitor) {
8687
String workspaceUri = (String) arguments.get(0);
87-
IPath workspacePath = ResourceUtils.canonicalFilePathFromURI(workspaceUri);
88-
String invisibleProjectName = ProjectUtils.getWorkspaceInvisibleProjectName(workspacePath);
88+
IPath workspaceFolderPath = ResourceUtils.canonicalFilePathFromURI(workspaceUri);
89+
String invisibleProjectName = ProjectUtils.getWorkspaceInvisibleProjectName(workspaceFolderPath);
8990

9091
IProject[] projects = getWorkspaceRoot().getProjects();
9192
ArrayList<PackageNode> children = new ArrayList<>();
93+
List<IPath> paths = Collections.singletonList(workspaceFolderPath);
9294
for (IProject project : projects) {
93-
if (!project.isAccessible() || !ProjectUtils.isJavaProject(project) || Objects.equals(project, JavaLanguageServerPlugin.getProjectsManager().getDefaultProject())) {
95+
if (!project.isAccessible() || !ProjectUtils.isJavaProject(project)) {
96+
continue;
97+
}
98+
// Ignore all the projects that's not contained in the workspace folder, except for the invisible project.
99+
// This check is needed in multi-root scenario.
100+
if ((!ResourceUtils.isContainedIn(project.getLocation(), paths) && !Objects.equals(project.getName(), invisibleProjectName))) {
94101
continue;
95102
}
96103
PackageNode projectNode = PackageNode.createNodeForProject(JavaCore.create(project));

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.util.Objects;
1819

1920
import org.eclipse.core.resources.IFile;
2021
import org.eclipse.core.resources.IFolder;
@@ -32,6 +33,7 @@
3233
import org.eclipse.jdt.core.JavaCore;
3334
import org.eclipse.jdt.core.JavaModelException;
3435
import org.eclipse.jdt.ls.core.internal.JDTUtils;
36+
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
3537

3638
import com.microsoft.jdtls.ext.core.ExtUtils;
3739
import com.microsoft.jdtls.ext.core.JdtlsExtActivator;
@@ -194,19 +196,28 @@ public static PackageNode createNodeForVirtualContainer(IPackageFragmentRoot pkg
194196

195197
}
196198

197-
public static PackageNode createNodeForPackageFragmentRoot(IPackageFragmentRoot pkgRoot) throws JavaModelException {
199+
public static PackageRootNode createNodeForPackageFragmentRoot(IPackageFragmentRoot pkgRoot) throws JavaModelException {
200+
String displayName = pkgRoot.getElementName();
198201
boolean isSourcePath = pkgRoot.getKind() == IPackageFragmentRoot.K_SOURCE;
199202
if (!isSourcePath) {
200203
IClasspathEntry entry = pkgRoot.getRawClasspathEntry();
201204
// Process Referenced Variable
202205
if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
203206
return createNodeForClasspathVariable(entry);
204207
} else {
205-
return new PackageRootNode(pkgRoot, pkgRoot.getElementName(), NodeKind.PACKAGEROOT);
208+
return new PackageRootNode(pkgRoot, displayName, NodeKind.PACKAGEROOT);
206209
}
207210
} else {
208-
return new PackageRootNode(pkgRoot,
209-
ExtUtils.removeProjectSegment(pkgRoot.getJavaProject().getElementName(), pkgRoot.getPath()).toPortableString(), NodeKind.PACKAGEROOT);
211+
IJavaProject javaProject = pkgRoot.getJavaProject();
212+
IPath relativePath = pkgRoot.getPath();
213+
if (pkgRoot.getJavaProject().getPath().isPrefixOf(relativePath)) {
214+
relativePath = relativePath.makeRelativeTo(javaProject.getPath());
215+
}
216+
if (Objects.equals(ProjectUtils.WORKSPACE_LINK, relativePath.segment(0))) {
217+
relativePath = relativePath.removeFirstSegments(1); // Remove the '_' prefix
218+
}
219+
displayName = relativePath.toPortableString();
220+
return new PackageRootNode(pkgRoot, displayName, NodeKind.PACKAGEROOT);
210221
}
211222
}
212223

@@ -278,7 +289,7 @@ public static PackageNode createNodeForPrimaryType(IType type) {
278289
* referenced variable's classpath entry
279290
* @return correspond package node
280291
*/
281-
public static PackageNode createNodeForClasspathVariable(IClasspathEntry classpathEntry) {
292+
public static PackageRootNode createNodeForClasspathVariable(IClasspathEntry classpathEntry) {
282293
IClasspathEntry entry = JavaCore.getResolvedClasspathEntry(classpathEntry);
283294
String name = classpathEntry.getPath().toPortableString();
284295
String path = entry.getPath().toPortableString();

src/syncHandler.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import * as path from "path";
55
import { commands, Disposable, FileSystemWatcher, Uri, workspace } from "vscode";
66
import { instrumentOperation } from "vscode-extension-telemetry-wrapper";
77
import { Commands } from "./commands";
8+
import { NodeKind } from "./java/nodeData";
89
import { Settings } from "./settings";
910
import { DataNode } from "./views/dataNode";
1011
import { ExplorerNode } from "./views/explorerNode";
11-
import { HierarchicalPackageRootNode } from "./views/hierarchicalPackageRootNode";
1212
import { explorerNodeCache } from "./views/nodeCache/explorerNodeCache";
13-
import { PackageRootNode } from "./views/packageRootNode";
1413

1514
const ENABLE_AUTO_REFRESH: string = "java.view.package.enableAutoRefresh";
1615
const DISABLE_AUTO_REFRESH: string = "java.view.package.disableAutoRefresh";
@@ -75,7 +74,7 @@ class SyncHandler implements Disposable {
7574
if (Settings.isHierarchicalView()) {
7675
// TODO: has to get the hierarchical package root node due to the java side implementation
7776
// because currently it will only get the types for a package node but no child packages.
78-
while (!(node instanceof HierarchicalPackageRootNode)) {
77+
while (node && node.nodeData.kind !== NodeKind.PackageRoot) {
7978
node = <DataNode>node.getParent();
8079
}
8180
return node;
@@ -88,7 +87,7 @@ class SyncHandler implements Disposable {
8887
} else {
8988
// the direct parent is not rendered in the explorer, the returned node
9089
// is other package fragment, we need to refresh the package fragment root.
91-
while (!(node instanceof PackageRootNode)) {
90+
while (node && node.nodeData.kind !== NodeKind.PackageRoot) {
9291
node = <DataNode>node.getParent();
9392
}
9493
return node;

src/views/dependencyDataProvider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
120120
}
121121

122122
private doRefresh(element?: ExplorerNode): void {
123+
if (!element) {
124+
this._rootItems = undefined;
125+
}
123126
explorerNodeCache.removeNodeChildren(element);
124127
this._onDidChangeTreeData.fire(element);
125128
}

src/views/folderNode.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export class FolderNode extends DataNode {
1919
kind: NodeKind.Folder,
2020
projectUri: this._project.uri,
2121
path: this.path,
22-
rootPath: this._rootNode.path,
2322
handlerIdentifier: this._rootNode.handlerIdentifier,
2423
});
2524
}

src/views/packageNode.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class PackageNode extends DataNode {
2121
kind: NodeKind.Package,
2222
projectUri: this._project.nodeData.uri,
2323
path: this.nodeData.name,
24-
rootPath: this._rootNode.path,
2524
handlerIdentifier: this.nodeData.handlerIdentifier,
2625
});
2726
}
@@ -49,7 +48,7 @@ export class PackageNode extends DataNode {
4948

5049
protected get contextValue(): string {
5150
const parentData = <IPackageRootNodeData> this._rootNode.nodeData;
52-
if (parentData.entryKind === PackageRootKind.K_SOURCE) {
51+
if (parentData.entryKind === PackageRootKind.K_SOURCE || parentData.kind === NodeKind.Project) {
5352
return `${Explorer.ContextValueType.Package}+source`;
5453
} else if (parentData.entryKind === PackageRootKind.K_BINARY) {
5554
return `${Explorer.ContextValueType.Package}+binary`;

src/views/packageRootNode.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class PackageRootNode extends DataNode {
2525
return Jdtls.getPackageData({
2626
kind: NodeKind.PackageRoot,
2727
projectUri: this._project.nodeData.uri,
28-
rootPath: this.nodeData.path,
2928
handlerIdentifier: this.nodeData.handlerIdentifier,
3029
});
3130
}

0 commit comments

Comments
 (0)