1919import java .util .LinkedList ;
2020import java .util .List ;
2121import java .util .Map ;
22+ import java .util .Set ;
2223import java .util .function .BiFunction ;
2324import java .util .stream .Collectors ;
2425
3132import org .eclipse .core .resources .IWorkspaceRoot ;
3233import org .eclipse .core .resources .ResourcesPlugin ;
3334import org .eclipse .core .runtime .CoreException ;
35+ import org .eclipse .core .runtime .IPath ;
3436import org .eclipse .core .runtime .IProgressMonitor ;
3537import org .eclipse .core .runtime .IStatus ;
3638import org .eclipse .core .runtime .OperationCanceledException ;
@@ -257,18 +259,22 @@ private static List<PackageNode> getParentAncestorNodes(IResource element) throw
257259 * Get the class path container list.
258260 */
259261 private static List <PackageNode > getProjectChildren (PackageParams query , IProgressMonitor pm ) {
260- IJavaProject javaProject = getJavaProject (query .getProjectUri ());
261- if (javaProject != null ) {
262- refreshLocal (javaProject .getProject (), pm );
263- List <Object > children = new LinkedList <>();
264- boolean hasReferencedLibraries = false ;
265- try {
262+ IProject project = getProject (query .getProjectUri ());
263+ if (project == null ) {
264+ JdtlsExtActivator .logError ("Failed to find project at: " + query .getProjectUri ());
265+ }
266+
267+ List <Object > children = new LinkedList <>();
268+ boolean hasReferencedLibraries = false ;
269+ IJavaProject javaProject = JavaCore .create (project );
270+ try {
271+ if (ProjectUtils .isJavaProject (project ) && javaProject != null ) {
272+ refreshLocal (javaProject .getProject (), pm );
266273 IClasspathEntry [] references = javaProject .getRawClasspath ();
267274 for (IClasspathEntry entry : references ) {
268275 int entryKind = entry .getEntryKind ();
269276 if (entryKind == IClasspathEntry .CPE_SOURCE ) {
270- IPackageFragmentRoot [] packageFragmentRoots = javaProject .findPackageFragmentRoots (entry );
271- children .addAll (Arrays .asList (packageFragmentRoots ));
277+ Collections .addAll (children , javaProject .findPackageFragmentRoots (entry ));
272278 } else if (entryKind == IClasspathEntry .CPE_CONTAINER ) {
273279 children .add (entry );
274280 } else if (entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY || entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE ) {
@@ -278,24 +284,32 @@ private static List<PackageNode> getProjectChildren(PackageParams query, IProgre
278284 }
279285 }
280286 Collections .addAll (children , javaProject .getNonJavaResources ());
281- } catch (CoreException e ) {
282- JdtlsExtActivator .logException ("Problem load project library " , e );
287+ } else {
288+ Set <IPath > projectPaths = Arrays .stream (ProjectUtils .getAllProjects ())
289+ .map (IProject ::getLocation ).collect (Collectors .toSet ());
290+ IResource [] members = project .members ();
291+ for (IResource member : members ) {
292+ if (!projectPaths .contains (member .getLocation ())) {
293+ children .add (member );
294+ }
295+ }
283296 }
297+ } catch (CoreException e ) {
298+ JdtlsExtActivator .logException ("Problem load project library " , e );
299+ }
284300
285- ResourceSet resourceSet = new ResourceSet (children );
286- ResourceVisitor visitor = new JavaResourceVisitor (javaProject );
287- resourceSet .accept (visitor );
288- List <PackageNode > result = visitor .getNodes ();
301+ ResourceSet resourceSet = new ResourceSet (children );
302+ ResourceVisitor visitor = new JavaResourceVisitor (javaProject );
303+ resourceSet .accept (visitor );
304+ List <PackageNode > result = visitor .getNodes ();
289305
290- // Invisible project will always have the referenced libraries entry
291- if (!ProjectUtils .isVisibleProject (javaProject .getProject ())) {
292- result .add (PackageNode .REFERENCED_LIBRARIES_CONTAINER );
293- } else if (hasReferencedLibraries ) {
294- result .add (PackageNode .IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER );
295- }
296- return result ;
306+ // Invisible project will always have the referenced libraries entry
307+ if (!ProjectUtils .isVisibleProject (project )) {
308+ result .add (PackageNode .REFERENCED_LIBRARIES_CONTAINER );
309+ } else if (hasReferencedLibraries ) {
310+ result .add (PackageNode .IMMUTABLE_REFERENCED_LIBRARIES_CONTAINER );
297311 }
298- return Collections . emptyList () ;
312+ return result ;
299313 }
300314
301315 private static List <PackageNode > getContainerChildren (PackageParams query , IProgressMonitor pm ) {
@@ -567,7 +581,7 @@ private static Object[] findJarDirectoryChildren(JarEntryDirectory directory, St
567581 return null ;
568582 }
569583
570- public static IJavaProject getJavaProject (String projectUri ) {
584+ public static IProject getProject (String projectUri ) {
571585 IWorkspaceRoot root = ResourcesPlugin .getWorkspace ().getRoot ();
572586 IContainer [] containers = root .findContainersForLocationURI (JDTUtils .toURI (projectUri ));
573587
@@ -576,8 +590,7 @@ public static IJavaProject getJavaProject(String projectUri) {
576590 }
577591
578592 // For multi-module scenario, findContainersForLocationURI API may return a container array,
579- // need filter out non-Java project and put the result from the nearest project in front.
580- containers = Arrays .stream (containers ).filter (c -> ProjectUtils .isJavaProject (c .getProject ())).toArray (IContainer []::new );
593+ // put the result from the nearest project in front.
581594 Arrays .sort (containers , (Comparator <IContainer >) (IContainer a , IContainer b ) -> {
582595 return a .getFullPath ().toPortableString ().length () - b .getFullPath ().toPortableString ().length ();
583596 });
@@ -587,11 +600,16 @@ public static IJavaProject getJavaProject(String projectUri) {
587600 if (!project .exists ()) {
588601 return null ;
589602 }
590- return JavaCore . create ( project ) ;
603+ return project ;
591604 }
592605 return null ;
593606 }
594607
608+ public static IJavaProject getJavaProject (String projectUri ) {
609+ IProject project = getProject (projectUri );
610+ return JavaCore .create (project );
611+ }
612+
595613 private static void refreshLocal (IResource resource , IProgressMonitor monitor ) {
596614 if (resource == null || !resource .exists ()) {
597615 return ;
0 commit comments