diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandler.java index 47ead81786..83dcac7a87 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandler.java @@ -61,11 +61,15 @@ public ReferencesHandler(PreferenceManager preferenceManager) { private IJavaSearchScope createSearchScope(IJavaElement elementToSearch) throws JavaModelException { IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); - int includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.APPLICATION_LIBRARIES; - if (isInsideJRE(elementToSearch)) { - includeMask |= IJavaSearchScope.SYSTEM_LIBRARIES; + SearchScope searchScope = preferenceManager.getPreferences().getSearchScope(); + int includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS; + if (searchScope != SearchScope.projectOnly) { + includeMask |= IJavaSearchScope.APPLICATION_LIBRARIES; + if (isInsideJRE(elementToSearch)) { + includeMask |= IJavaSearchScope.SYSTEM_LIBRARIES; + } } - var excludeTestCode = preferenceManager.getPreferences().getSearchScope() == SearchScope.main; + var excludeTestCode = searchScope == SearchScope.main; return SearchEngine.createJavaSearchScope(excludeTestCode, projects, includeMask); } @@ -218,4 +222,4 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException { }, monitor); } -} \ No newline at end of file +} diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java index fda1da73c1..c4b1c64d55 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java @@ -577,12 +577,14 @@ public class Preferences { public static final String CHAIN_COMPLETION_KEY = "java.completion.chain.enabled"; /** - * Preference key to set the scope value to use when searching java code. Allowed value are + * Preference key to set the scope value to use when searching Java code. Allowed values are *
main - Scope for main codeall - Scope for both test and main codemain - Search main source code and librariesall - Search main and test source code and librariesprojectOnly - For reference searches, include main and test
+ * source code and referenced projects, excluding application and system librariesall.
+ * Any unknown value will be treated as all.
*/
public static final String JAVA_SEARCH_SCOPE = "java.search.scope";
@@ -848,15 +850,14 @@ static FeatureStatus fromString(String value, FeatureStatus defaultStatus) {
}
public static enum SearchScope {
- all, main;
+ all, main, projectOnly;
static SearchScope fromString(String value, SearchScope defaultScope) {
if (value != null) {
- String val = value.toLowerCase();
- try {
- return valueOf(val);
- } catch(Exception e) {
- //fall back to default severity
+ for (SearchScope scope : values()) {
+ if (scope.name().equalsIgnoreCase(value)) {
+ return scope;
+ }
}
}
return defaultScope;
diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandlerTest.java
index da21d8182c..cc9cbd2c00 100644
--- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandlerTest.java
+++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/ReferencesHandlerTest.java
@@ -13,6 +13,7 @@
package org.eclipse.jdt.ls.core.internal.handlers;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@@ -34,6 +35,7 @@
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
+import org.eclipse.jdt.ls.core.internal.preferences.Preferences.SearchScope;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
@@ -142,6 +144,41 @@ public void testEnumInClassFile() throws Exception {
assertEquals(fileURI, l.getUri());
}
+ @Test
+ public void testProjectOnlySearchScope() throws Exception {
+ when(preferenceManager.isClientSupportsClassFileContent()).thenReturn(true);
+ importProjects("eclipse/reference");
+ IProject referenceProject = WorkspaceHelper.getProject("reference");
+ IJavaProject referenceJavaProject = JavaCore.create(referenceProject);
+ IType element = referenceJavaProject.findType("org.sample.Foo");
+ IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
+ URI uri = JDTUtils.toURI(JDTUtils.toUri(cf));
+ String fileURI = ResourceUtils.fixURI(uri);
+ ReferenceParams param = new ReferenceParams();
+ param.setPosition(new Position(5, 6));
+ param.setContext(new ReferenceContext(false));
+ param.setTextDocument(new TextDocumentIdentifier(fileURI));
+
+ SearchScope searchScope = preferences.getSearchScope();
+ try {
+ preferences.setSearchScope(SearchScope.projectOnly);
+ List