Skip to content

Commit 100b677

Browse files
jdneoCsCherrYY
andauthored
fix: Display compilation units which does not have a primary type (#752)
Co-authored-by: Shi Chen <[email protected]>
1 parent 4309418 commit 100b677

File tree

10 files changed

+51
-31
lines changed

10 files changed

+51
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
### Fixed
1616
- Apply `files.exclude` to Java Projects explorer. [#214](https://github.com/microsoft/vscode-java-dependency/issues/214)
1717
- Empty packages will not appear sometimes. [#600](https://github.com/microsoft/vscode-java-dependency/issues/600)
18-
- Show `module-info.java` at source roots. [698](https://github.com/microsoft/vscode-java-dependency/issues/698)
18+
- Show `module-info.java` at source roots. [#698](https://github.com/microsoft/vscode-java-dependency/issues/698)
19+
- Show Java files which does not have a primary type in the Java Projects explorer. [#748](https://github.com/microsoft/vscode-java-dependency/issues/748)
1920

2021
## 0.21.2
2122
### Fixed

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.eclipse.jdt.core.IJarEntryResource;
4444
import org.eclipse.jdt.core.IJavaElement;
4545
import org.eclipse.jdt.core.IJavaProject;
46-
import org.eclipse.jdt.core.IModuleDescription;
4746
import org.eclipse.jdt.core.IPackageFragment;
4847
import org.eclipse.jdt.core.IPackageFragmentRoot;
4948
import org.eclipse.jdt.core.IType;
@@ -230,12 +229,7 @@ private static List<PackageNode> getParentAncestorNodes(IResource element) throw
230229
List<PackageNode> nodeList = new LinkedList<>();
231230
while (element != null && !(element instanceof IWorkspaceRoot)) {
232231
IJavaElement javaElement = JavaCore.create(element);
233-
if (javaElement == null) {
234-
PackageNode entry = PackageNode.createNodeForResource(element);
235-
if (entry != null) {
236-
nodeList.add(0, entry);
237-
}
238-
} else if (javaElement instanceof IJavaProject) {
232+
if (javaElement instanceof IJavaProject) {
239233
nodeList.add(0, PackageNode.createNodeForProject(javaElement));
240234
} else if (javaElement instanceof IPackageFragmentRoot) {
241235
IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) javaElement;
@@ -248,6 +242,11 @@ private static List<PackageNode> getParentAncestorNodes(IResource element) throw
248242
if (packageFragment.containsJavaResources() || packageFragment.getNonJavaResources().length > 0) {
249243
nodeList.add(0, PackageNode.createNodeForPackageFragment(packageFragment));
250244
}
245+
} else if (javaElement == null) {
246+
PackageNode entry = PackageNode.createNodeForResource(element);
247+
if (entry != null) {
248+
nodeList.add(0, entry);
249+
}
251250
}
252251
element = element.getParent();
253252
}
@@ -431,6 +430,8 @@ public static List<Object> getChildrenForPackage(IPackageFragment packageFragmen
431430
IType primaryType = ((ITypeRoot) element).findPrimaryType();
432431
if (primaryType != null) {
433432
children.add(primaryType);
433+
} else {
434+
children.add(element);
434435
}
435436
}
436437
}
@@ -538,14 +539,6 @@ public static List<Object> getPackageFragmentRootContent(IPackageFragmentRoot ro
538539
Object[] nonJavaResources = root.getNonJavaResources();
539540
Collections.addAll(result, nonJavaResources);
540541

541-
IModuleDescription moduleDescription = root.getModuleDescription();
542-
if (moduleDescription != null) {
543-
if (moduleDescription.getClassFile() != null) {
544-
result.add(moduleDescription.getClassFile());
545-
} else if (moduleDescription.getCompilationUnit() != null) {
546-
result.add(moduleDescription.getCompilationUnit());
547-
}
548-
}
549542
return result;
550543
}
551544

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ public enum NodeKind {
2222

2323
PRIMARYTYPE(5),
2424

25-
CONTAINER(6),
25+
COMPILATIONUNIT(6),
2626

27-
FOLDER(7),
27+
CLASSFILE(7),
2828

29-
FILE(8);
29+
CONTAINER(8),
30+
31+
FOLDER(9),
32+
33+
FILE(10);
3034

3135
private final int value;
3236

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ public String getPath() {
363363
return path;
364364
}
365365

366+
public void setPath(String path) {
367+
this.path = path;
368+
}
369+
366370
public NodeKind getKind() {
367371
return kind;
368372
}

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/parser/JavaResourceVisitor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import org.eclipse.core.resources.IFile;
1818
import org.eclipse.core.resources.IFolder;
19+
import org.eclipse.core.resources.IResource;
1920
import org.eclipse.jdt.core.IClassFile;
2021
import org.eclipse.jdt.core.IClasspathEntry;
2122
import org.eclipse.jdt.core.ICompilationUnit;
@@ -54,15 +55,23 @@ public void visit(IType type) {
5455

5556
@Override
5657
public void visit(IClassFile classFile) {
57-
PackageNode node = new PackageNode(classFile.getElementName(), null, NodeKind.FILE);
58+
PackageNode node = new PackageNode(classFile.getElementName(), null, NodeKind.CLASSFILE);
5859
node.setUri(JDTUtils.toUri(classFile));
60+
IResource resource = classFile.getResource();
61+
if (resource != null) {
62+
node.setPath(resource.getFullPath().toPortableString());
63+
}
5964
this.nodes.add(node);
6065
}
6166

6267
@Override
6368
public void visit(ICompilationUnit compilationUnit) {
64-
PackageNode node = new PackageNode(compilationUnit.getElementName(), null, NodeKind.FILE);
69+
PackageNode node = new PackageNode(compilationUnit.getElementName(), null, NodeKind.COMPILATIONUNIT);
6570
node.setUri(JDTUtils.toUri(compilationUnit));
71+
IResource resource = compilationUnit.getResource();
72+
if (resource != null) {
73+
node.setPath(resource.getFullPath().toPortableString());
74+
}
6675
this.nodes.add(node);
6776
}
6877

src/java/nodeData.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export enum NodeKind {
77
PackageRoot = 3,
88
Package = 4,
99
PrimaryType = 5,
10-
Container = 6,
11-
Folder = 7,
12-
File = 8,
10+
CompilationUnit = 6,
11+
ClassFile = 7,
12+
Container = 8,
13+
Folder = 9,
14+
File = 10,
1315
}
1416

1517
export enum TypeKind {

src/views/dataNode.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export abstract class DataNode extends ExplorerNode {
3030
case NodeKind.PackageRoot:
3131
case NodeKind.Package:
3232
case NodeKind.PrimaryType:
33+
case NodeKind.CompilationUnit:
34+
case NodeKind.ClassFile:
3335
case NodeKind.Folder:
3436
case NodeKind.File:
3537
item.resourceUri = Uri.parse(this.uri);

src/views/nodeFactory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export class NodeFactory {
7373
}
7474

7575
return new FolderNode(nodeData, parent, project, rootNode);
76+
case NodeKind.CompilationUnit:
77+
case NodeKind.ClassFile:
7678
case NodeKind.File:
7779
if (!parent) {
7880
throw new Error("Folder node must have parent.");

test/maven-suite/projectView.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ suite("Maven Project View Tests", () => {
5050

5151
// validate innermost layer nodes
5252
const classes = await firstSecondarySubPackage.getChildren();
53-
assert.equal(classes.length, 3, "Number of main classes of first package should be 3");
53+
assert.equal(classes.length, 4, "Number of main classes of first package should be 4");
5454
const firstClass = classes[0] as PrimaryTypeNode;
5555
const secondClass = classes[1] as PrimaryTypeNode;
5656
const thirdClass = classes[2] as PrimaryTypeNode;
5757
assert.equal(firstClass.name, "App", "Name of first class should be \"App\"");
5858
assert.equal(secondClass.name, "AppToDelete", "Name of second class should be \"AppToDelete\"");
5959
assert.equal(thirdClass.name, "AppToRename", "Name of third class should be \"AppToRename\"");
60+
assert.equal((classes[3] as DataNode).name, "package-info.java");
6061
});
6162

6263
test("Can node render correctly in flat view", async function() {
@@ -110,7 +111,7 @@ suite("Maven Project View Tests", () => {
110111
// validate innermost layer nodes
111112
const mainClasses = await firstMainSubPackage.getChildren();
112113
const testClasses = await testSubPackage.getChildren();
113-
assert.equal(mainClasses.length, 3, "Number of main classes of first package should be 3");
114+
assert.equal(mainClasses.length, 4, "Number of main classes of first package should be 4");
114115
assert.equal(testClasses.length, 1, "Number of test classes should be 1");
115116
const firstMainClass = mainClasses[0] as PrimaryTypeNode;
116117
const secondMainClass = mainClasses[1] as PrimaryTypeNode;
@@ -119,6 +120,7 @@ suite("Maven Project View Tests", () => {
119120
assert.equal(firstMainClass.name, "App", "Name of first class should be \"App\"");
120121
assert.equal(secondMainClass.name, "AppToDelete", "Name of second class should be \"AppToDelete\"");
121122
assert.equal(thirdMainClass.name, "AppToRename", "Name of third class should be \"AppToRename\"");
123+
assert.equal((mainClasses[3] as DataNode).name, "package-info.java");
122124
assert.equal(testClass.name, "AppTest", "Name of test class should be \"AppTest\"");
123125
});
124126

@@ -205,11 +207,11 @@ suite("Maven Project View Tests", () => {
205207
projectUri: workspaceFolders![0].uri.toString(),
206208
path: mainPackage.nodeData.name,
207209
handlerIdentifier: mainPackage.nodeData.handlerIdentifier,
208-
});
209-
assert.equal(packages?.length, 3, "packages' length should be 3");
210-
assert.equal(packages![0].name, "com.mycompany.app");
211-
assert.equal(packages![1].name, "com.mycompany.app1");
212-
assert.equal(packages![2].name, "module-info.java");
210+
}) || [];
211+
assert.equal(packages.length, 3, "packages' length should be 3");
212+
assert.ok(packages.filter((p) => {
213+
return !["com.mycompany.app", "com.mycompany.app1", "module-info.java"].includes(p.name);
214+
}).length === 0);
213215
});
214216

215217
test("Can execute command java.resolvePath correctly", async function() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.mycompany.app;

0 commit comments

Comments
 (0)