@@ -440,10 +440,29 @@ private static void resolveSingleType(IJavaProject javaProject, String typeName,
440440 String packageName = typeName .substring (0 , lastDotIndex );
441441 String simpleName = typeName .substring (lastDotIndex + 1 );
442442
443- // Strategy: First search in local source packages (fast ), then fallback to global search (slow)
444- // This optimizes for the common case where imports reference local project types
443+ // Strategy: Use JDT's global type resolution first (comprehensive ),
444+ // then fallback to manual package fragment traversal if needed
445445
446- // Fast path: Search for the type in source package fragments directly
446+ // Primary path: Use JDT's findType which searches all sources and dependencies
447+ try {
448+ org .eclipse .jdt .core .IType type = javaProject .findType (typeName );
449+ if (type != null && type .exists ()) {
450+ // Found type - check if it's a source type we want to process
451+ if (!type .isBinary ()) {
452+ // Source type found - extract information and return
453+ extractTypeInfo (type , result );
454+ return ;
455+ }
456+ // Note: Binary types (from JARs/JRE) are intentionally ignored
457+ // as they don't provide useful context for code completion
458+ }
459+ } catch (JavaModelException e ) {
460+ JdtlsExtActivator .logException ("Error in primary type search: " + typeName , e );
461+ // Continue to fallback method
462+ }
463+
464+ // Fallback path: Manual search in local source package fragments
465+ // This is used when findType() doesn't return results or fails
447466 IPackageFragmentRoot [] packageRoots = javaProject .getPackageFragmentRoots ();
448467 for (IPackageFragmentRoot packageRoot : packageRoots ) {
449468 if (packageRoot .getKind () == IPackageFragmentRoot .K_SOURCE ) {
@@ -456,7 +475,7 @@ private static void resolveSingleType(IJavaProject javaProject, String typeName,
456475 org .eclipse .jdt .core .IType primaryType = cu .findPrimaryType ();
457476 if (primaryType != null && primaryType .exists () &&
458477 typeName .equals (primaryType .getFullyQualifiedName ())) {
459- // Found local project source type - fast path success
478+ // Found local project source type via fallback method
460479 extractTypeInfo (primaryType , result );
461480 return ;
462481 }
@@ -473,23 +492,6 @@ private static void resolveSingleType(IJavaProject javaProject, String typeName,
473492 }
474493 }
475494 }
476-
477- // Slow path: Use JDT's global type resolution as fallback for external dependencies
478- // This is only needed if the type is not found in local source packages
479- try {
480- org .eclipse .jdt .core .IType type = javaProject .findType (typeName );
481- if (type != null && type .exists ()) {
482- // Found type in dependencies/JRE, but we only process local source types
483- // for this specific use case (Copilot context)
484- if (!type .isBinary ()) {
485- extractTypeInfo (type , result );
486- }
487- // Note: Binary types (from JARs) are intentionally ignored
488- // as they don't provide useful context for code completion
489- }
490- } catch (JavaModelException e ) {
491- JdtlsExtActivator .logException ("Error finding type in global search: " + typeName , e );
492- }
493495 } catch (JavaModelException e ) {
494496 // Log but continue processing other types
495497 JdtlsExtActivator .logException ("Error resolving type: " + typeName , e );
0 commit comments