|
63 | 63 | import com.microsoft.jdtls.ext.core.model.NodeKind; |
64 | 64 | import com.microsoft.jdtls.ext.core.model.PackageNode; |
65 | 65 | import com.microsoft.jdtls.ext.core.model.PackageRootNode; |
| 66 | +import com.microsoft.jdtls.ext.core.model.Trie; |
| 67 | +import com.microsoft.jdtls.ext.core.model.TrieNode; |
66 | 68 |
|
67 | 69 | public class PackageCommand { |
68 | 70 |
|
@@ -347,7 +349,7 @@ private static List<PackageNode> getPackages(PackageParams query, IProgressMonit |
347 | 349 | throw new CoreException( |
348 | 350 | new Status(IStatus.ERROR, JdtlsExtActivator.PLUGIN_ID, String.format("No package root found for %s", query.getPath()))); |
349 | 351 | } |
350 | | - Object[] result = getPackageFragmentRootContent(packageRoot, pm); |
| 352 | + Object[] result = getPackageFragmentRootContent(packageRoot, query.isHierarchicalView(), pm); |
351 | 353 | return convertToPackageNode(result, packageRoot, pm); |
352 | 354 | } catch (CoreException e) { |
353 | 355 | JdtlsExtActivator.logException("Problem load project package ", e); |
@@ -461,18 +463,45 @@ private static List<PackageNode> getFolderChildren(PackageParams query, IProgres |
461 | 463 | return Collections.emptyList(); |
462 | 464 | } |
463 | 465 |
|
464 | | - private static Object[] getPackageFragmentRootContent(IPackageFragmentRoot root, IProgressMonitor pm) throws CoreException { |
| 466 | + /** |
| 467 | + * Return the packages of the package root. Note that when the explorer is in hierarchical mode, |
| 468 | + * We also need to return the deepest common parent packages, for example: |
| 469 | + * - com.microsoft.example <-- this common parent package should be returned. |
| 470 | + * +-- model |
| 471 | + * +-- handler |
| 472 | + * Here we use a Trie to find all these packages. |
| 473 | + * |
| 474 | + * @param root the package fragment root |
| 475 | + * @param isHierarchicalView whether the explorer is in hierarchical mode or not |
| 476 | + * @param pm the progress monitor |
| 477 | + */ |
| 478 | + private static Object[] getPackageFragmentRootContent(IPackageFragmentRoot root, boolean isHierarchicalView, IProgressMonitor pm) throws CoreException { |
465 | 479 | ArrayList<Object> result = new ArrayList<>(); |
466 | | - for (IJavaElement child : root.getChildren()) { |
467 | | - IPackageFragment fragment = (IPackageFragment) child; |
468 | | - if (fragment.hasChildren()) { |
469 | | - result.add(child); |
470 | | - } else if (fragment.getNonJavaResources().length > 0) { // some package has non-java files |
471 | | - result.add(fragment); |
472 | | - } else if (!fragment.hasSubpackages()) { |
473 | | - result.add(fragment); |
| 480 | + if (isHierarchicalView) { |
| 481 | + Map<String, IJavaElement> map = new HashMap<>(); |
| 482 | + for (IJavaElement child : root.getChildren()) { |
| 483 | + map.put(child.getElementName(), child); |
| 484 | + } |
| 485 | + Trie<IJavaElement> trie = new Trie<>(map); |
| 486 | + for (TrieNode<IJavaElement> node : trie.getAllNodes()) { |
| 487 | + if (node.value == null) { |
| 488 | + continue; |
| 489 | + } |
| 490 | + IPackageFragment fragment = (IPackageFragment) node.value; |
| 491 | + if (fragment.hasChildren() || fragment.getNonJavaResources().length > 0 |
| 492 | + || !fragment.hasSubpackages() || node.children.size() > 1) { |
| 493 | + result.add(fragment); |
| 494 | + } |
| 495 | + } |
| 496 | + } else { |
| 497 | + for (IJavaElement child : root.getChildren()) { |
| 498 | + IPackageFragment fragment = (IPackageFragment) child; |
| 499 | + if (fragment.hasChildren() || fragment.getNonJavaResources().length > 0 || !fragment.hasSubpackages()) { |
| 500 | + result.add(fragment); |
| 501 | + } |
474 | 502 | } |
475 | 503 | } |
| 504 | + |
476 | 505 | Object[] nonJavaResources = root.getNonJavaResources(); |
477 | 506 | Collections.addAll(result, nonJavaResources); |
478 | 507 |
|
|
0 commit comments