19
19
import java .util .LinkedList ;
20
20
import java .util .List ;
21
21
import java .util .Map ;
22
+ import java .util .Set ;
22
23
import java .util .function .BiFunction ;
23
24
import java .util .stream .Collectors ;
24
25
31
32
import org .eclipse .core .resources .IWorkspaceRoot ;
32
33
import org .eclipse .core .resources .ResourcesPlugin ;
33
34
import org .eclipse .core .runtime .CoreException ;
35
+ import org .eclipse .core .runtime .IPath ;
34
36
import org .eclipse .core .runtime .IProgressMonitor ;
35
37
import org .eclipse .core .runtime .IStatus ;
36
38
import org .eclipse .core .runtime .OperationCanceledException ;
@@ -257,18 +259,22 @@ private static List<PackageNode> getParentAncestorNodes(IResource element) throw
257
259
* Get the class path container list.
258
260
*/
259
261
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 );
266
273
IClasspathEntry [] references = javaProject .getRawClasspath ();
267
274
for (IClasspathEntry entry : references ) {
268
275
int entryKind = entry .getEntryKind ();
269
276
if (entryKind == IClasspathEntry .CPE_SOURCE ) {
270
- IPackageFragmentRoot [] packageFragmentRoots = javaProject .findPackageFragmentRoots (entry );
271
- children .addAll (Arrays .asList (packageFragmentRoots ));
277
+ Collections .addAll (children , javaProject .findPackageFragmentRoots (entry ));
272
278
} else if (entryKind == IClasspathEntry .CPE_CONTAINER ) {
273
279
children .add (entry );
274
280
} else if (entry .getEntryKind () == IClasspathEntry .CPE_LIBRARY || entry .getEntryKind () == IClasspathEntry .CPE_VARIABLE ) {
@@ -278,24 +284,32 @@ private static List<PackageNode> getProjectChildren(PackageParams query, IProgre
278
284
}
279
285
}
280
286
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
+ }
283
296
}
297
+ } catch (CoreException e ) {
298
+ JdtlsExtActivator .logException ("Problem load project library " , e );
299
+ }
284
300
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 ();
289
305
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 );
297
311
}
298
- return Collections . emptyList () ;
312
+ return result ;
299
313
}
300
314
301
315
private static List <PackageNode > getContainerChildren (PackageParams query , IProgressMonitor pm ) {
@@ -567,7 +581,7 @@ private static Object[] findJarDirectoryChildren(JarEntryDirectory directory, St
567
581
return null ;
568
582
}
569
583
570
- public static IJavaProject getJavaProject (String projectUri ) {
584
+ public static IProject getProject (String projectUri ) {
571
585
IWorkspaceRoot root = ResourcesPlugin .getWorkspace ().getRoot ();
572
586
IContainer [] containers = root .findContainersForLocationURI (JDTUtils .toURI (projectUri ));
573
587
@@ -576,8 +590,7 @@ public static IJavaProject getJavaProject(String projectUri) {
576
590
}
577
591
578
592
// 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.
581
594
Arrays .sort (containers , (Comparator <IContainer >) (IContainer a , IContainer b ) -> {
582
595
return a .getFullPath ().toPortableString ().length () - b .getFullPath ().toPortableString ().length ();
583
596
});
@@ -587,11 +600,16 @@ public static IJavaProject getJavaProject(String projectUri) {
587
600
if (!project .exists ()) {
588
601
return null ;
589
602
}
590
- return JavaCore . create ( project ) ;
603
+ return project ;
591
604
}
592
605
return null ;
593
606
}
594
607
608
+ public static IJavaProject getJavaProject (String projectUri ) {
609
+ IProject project = getProject (projectUri );
610
+ return JavaCore .create (project );
611
+ }
612
+
595
613
private static void refreshLocal (IResource resource , IProgressMonitor monitor ) {
596
614
if (resource == null || !resource .exists ()) {
597
615
return ;
0 commit comments