|
10 | 10 | *******************************************************************************/
|
11 | 11 | package com.microsoft.jdtls.ext.core;
|
12 | 12 |
|
| 13 | +import java.net.URI; |
13 | 14 | import java.util.ArrayList;
|
14 | 15 | import java.util.Arrays;
|
15 | 16 | import java.util.Collections;
|
|
42 | 43 | import org.eclipse.jdt.core.IJavaProject;
|
43 | 44 | import org.eclipse.jdt.core.IPackageFragment;
|
44 | 45 | import org.eclipse.jdt.core.IPackageFragmentRoot;
|
| 46 | +import org.eclipse.jdt.core.ITypeRoot; |
45 | 47 | import org.eclipse.jdt.core.JavaCore;
|
46 | 48 | import org.eclipse.jdt.core.JavaModelException;
|
47 | 49 | import org.eclipse.jdt.internal.core.JarEntryDirectory;
|
@@ -122,26 +124,45 @@ public static List<PackageNode> resolvePath(List<Object> arguments, IProgressMon
|
122 | 124 |
|
123 | 125 | List<PackageNode> result = new ArrayList<>();
|
124 | 126 |
|
125 |
| - ICompilationUnit cu = JDTUtils.resolveCompilationUnit(typeRootUri); |
126 |
| - if (cu != null) { |
| 127 | + URI uri = JDTUtils.toURI(typeRootUri); |
| 128 | + ITypeRoot typeRoot = null; |
| 129 | + if ("jdt".equals(uri.getScheme())) { |
| 130 | + typeRoot = JDTUtils.resolveClassFile(uri); |
| 131 | + } else { |
| 132 | + typeRoot = JDTUtils.resolveCompilationUnit(uri); |
| 133 | + } |
| 134 | + if (typeRoot != null) { |
127 | 135 | // Add project node:
|
128 |
| - IProject proj = cu.getJavaProject().getProject(); |
| 136 | + IProject proj = typeRoot.getJavaProject().getProject(); |
129 | 137 | PackageNode projectNode = new PackageNode(proj.getName(), proj.getFullPath().toPortableString(), NodeKind.PROJECT);
|
130 | 138 | projectNode.setUri(proj.getLocationURI().toString());
|
131 | 139 | result.add(projectNode);
|
132 | 140 |
|
133 |
| - IPackageFragment packageFragment = (IPackageFragment) cu.getParent(); |
| 141 | + IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent(); |
134 | 142 | String packageName = packageFragment.isDefaultPackage() ? DEFAULT_PACKAGE_DISPLAYNAME : packageFragment.getElementName();
|
135 | 143 | PackageNode packageNode = new PackageNode(packageName, packageFragment.getPath().toPortableString(), NodeKind.PACKAGE);
|
136 | 144 | IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) packageFragment.getParent();
|
137 |
| - PackageNode rootNode = new PackageRootNode( |
138 |
| - ExtUtils.removeProjectSegment(cu.getJavaProject().getElementName(), pkgRoot.getPath()).toPortableString(), |
139 |
| - pkgRoot.getPath().toPortableString(), NodeKind.PACKAGEROOT, pkgRoot.getKind()); |
| 145 | + PackageNode rootNode = null; |
| 146 | + if (typeRoot instanceof IClassFile) { |
| 147 | + rootNode = new PackageRootNode(pkgRoot.getElementName(), pkgRoot.getPath().toPortableString(), NodeKind.PACKAGEROOT, pkgRoot.getKind()); |
| 148 | + } else { |
| 149 | + rootNode = new PackageRootNode(ExtUtils.removeProjectSegment(typeRoot.getJavaProject().getElementName(), pkgRoot.getPath()).toPortableString(), |
| 150 | + pkgRoot.getPath().toPortableString(), NodeKind.PACKAGEROOT, pkgRoot.getKind()); |
| 151 | + } |
| 152 | + // TODO: Let the client handle the display instead. Server side should always |
| 153 | + // provide the container node. |
| 154 | + if (typeRoot instanceof IClassFile) { |
| 155 | + IClasspathEntry entry = pkgRoot.getRawClasspathEntry(); |
| 156 | + IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), typeRoot.getJavaProject()); |
| 157 | + PackageNode containerNode = new ContainerNode(container.getDescription(), container.getPath().toPortableString(), NodeKind.CONTAINER, |
| 158 | + entry.getEntryKind()); |
| 159 | + result.add(containerNode); |
| 160 | + } |
140 | 161 | result.add(rootNode);
|
141 | 162 | result.add(packageNode);
|
142 | 163 |
|
143 |
| - PackageNode item = new TypeRootNode(cu.getElementName(), cu.getPath().toPortableString(), NodeKind.TYPEROOT, TypeRootNode.K_SOURCE); |
144 |
| - item.setUri(JDTUtils.toURI(cu)); |
| 164 | + PackageNode item = new TypeRootNode(typeRoot.getElementName(), typeRoot.getPath().toPortableString(), NodeKind.TYPEROOT, TypeRootNode.K_SOURCE); |
| 165 | + item.setUri(JDTUtils.toUri(typeRoot)); |
145 | 166 | result.add(item);
|
146 | 167 | }
|
147 | 168 |
|
@@ -267,11 +288,20 @@ private static List<PackageNode> getRootTypes(PackageParams query, IProgressMoni
|
267 | 288 | return rootTypeNodes;
|
268 | 289 | }
|
269 | 290 | // when .java files and other .properties files are mixed up
|
270 |
| - rootTypeNodes.addAll(Arrays.stream(nonJavaResources).filter(resource -> resource instanceof IFile).map(resource -> { |
271 |
| - IFile file = (IFile) resource; |
272 |
| - PackageNode item = new PackageNode(file.getName(), file.getFullPath().toPortableString(), NodeKind.FILE); |
273 |
| - item.setUri(JDTUtils.getFileURI(file)); |
274 |
| - return item; |
| 291 | + rootTypeNodes.addAll( |
| 292 | + Arrays.stream(nonJavaResources).filter(resource -> resource instanceof IFile || resource instanceof JarEntryFile).map(resource -> { |
| 293 | + if (resource instanceof IFile) { |
| 294 | + IFile file = (IFile) resource; |
| 295 | + PackageNode item = new PackageNode(file.getName(), file.getFullPath().toPortableString(), NodeKind.FILE); |
| 296 | + item.setUri(JDTUtils.getFileURI(file)); |
| 297 | + return item; |
| 298 | + } else { |
| 299 | + JarEntryFile file = (JarEntryFile) resource; |
| 300 | + PackageNode entry = new PackageNode(file.getName(), file.getFullPath().toPortableString(), NodeKind.FILE); |
| 301 | + entry.setUri(ExtUtils.toUri((JarEntryFile) resource)); |
| 302 | + return entry; |
| 303 | + } |
| 304 | + |
275 | 305 | }).collect(Collectors.toList()));
|
276 | 306 | return rootTypeNodes;
|
277 | 307 | }
|
@@ -330,7 +360,7 @@ private static Object[] getPackageFragmentRootContent(IPackageFragmentRoot root,
|
330 | 360 | if (fragment.hasChildren()) {
|
331 | 361 | result.add(child);
|
332 | 362 | } else if (fragment.getNonJavaResources().length > 0) { // some package has non-java files
|
333 |
| - result.add(fragment.getResource()); |
| 363 | + result.add(fragment); |
334 | 364 | }
|
335 | 365 | }
|
336 | 366 | Object[] nonJavaResources = root.getNonJavaResources();
|
|
0 commit comments