Skip to content

Commit e724bd3

Browse files
committed
fix: update logic to resolve type
1 parent bec1787 commit e724bd3

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/ProjectCommand.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -435,21 +435,10 @@ private static void resolveSingleType(IJavaProject javaProject, String typeName,
435435
String packageName = typeName.substring(0, lastDotIndex);
436436
String simpleName = typeName.substring(lastDotIndex + 1);
437437

438-
// First try to find the type using JDT's built-in type resolution
439-
try {
440-
org.eclipse.jdt.core.IType type = javaProject.findType(typeName);
441-
if (type != null && type.exists() && type.isBinary() == false) {
442-
// This is a local project type (not from a JAR)
443-
extractTypeInfo(type, result);
444-
return;
445-
}
446-
} catch (JavaModelException e) {
447-
JdtlsExtActivator.logException("Error finding type: " + typeName, e);
448-
// Fall back to manual search if findType fails
449-
return;
450-
}
438+
// Strategy: First search in local source packages (fast), then fallback to global search (slow)
439+
// This optimizes for the common case where imports reference local project types
451440

452-
// Fallback: Search for the type in source package fragments manually
441+
// Fast path: Search for the type in source package fragments directly
453442
IPackageFragmentRoot[] packageRoots = javaProject.getPackageFragmentRoots();
454443
for (IPackageFragmentRoot packageRoot : packageRoots) {
455444
if (packageRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
@@ -462,7 +451,7 @@ private static void resolveSingleType(IJavaProject javaProject, String typeName,
462451
org.eclipse.jdt.core.IType primaryType = cu.findPrimaryType();
463452
if (primaryType != null && primaryType.exists() &&
464453
typeName.equals(primaryType.getFullyQualifiedName())) {
465-
// This is a local project type
454+
// Found local project source type - fast path success
466455
extractTypeInfo(primaryType, result);
467456
return;
468457
}
@@ -479,6 +468,23 @@ private static void resolveSingleType(IJavaProject javaProject, String typeName,
479468
}
480469
}
481470
}
471+
472+
// Slow path: Use JDT's global type resolution as fallback for external dependencies
473+
// This is only needed if the type is not found in local source packages
474+
try {
475+
org.eclipse.jdt.core.IType type = javaProject.findType(typeName);
476+
if (type != null && type.exists()) {
477+
// Found type in dependencies/JRE, but we only process local source types
478+
// for this specific use case (Copilot context)
479+
if (!type.isBinary()) {
480+
extractTypeInfo(type, result);
481+
}
482+
// Note: Binary types (from JARs) are intentionally ignored
483+
// as they don't provide useful context for code completion
484+
}
485+
} catch (JavaModelException e) {
486+
JdtlsExtActivator.logException("Error finding type in global search: " + typeName, e);
487+
}
482488
} catch (JavaModelException e) {
483489
// Log but continue processing other types
484490
JdtlsExtActivator.logException("Error resolving type: " + typeName, e);

0 commit comments

Comments
 (0)