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 ed0742fe74..f12cec421b 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 @@ -45,6 +45,7 @@ import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; 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.ReferenceParams; @@ -62,7 +63,8 @@ private IJavaSearchScope createSearchScope(IJavaElement elementToSearch) throws if (isInsideJRE(elementToSearch)) { includeMask |= IJavaSearchScope.SYSTEM_LIBRARIES; } - return SearchEngine.createJavaSearchScope(projects, includeMask); + var excludeTestCode = preferenceManager.getPreferences().getSearchScope() == SearchScope.main; + return SearchEngine.createJavaSearchScope(excludeTestCode, projects, includeMask); } public List findReferences(ReferenceParams param, IProgressMonitor monitor) { diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java index 8747a2a549..fa5ac9ddc3 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java @@ -57,6 +57,7 @@ import org.eclipse.jdt.ls.core.internal.StatusFactory; import org.eclipse.jdt.ls.core.internal.handlers.BaseDiagnosticsHandler; import org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler; +import org.eclipse.jdt.ls.core.internal.preferences.Preferences.SearchScope; import org.eclipse.jface.text.templates.Template; import org.eclipse.jface.text.templates.TemplateContextType; import org.eclipse.lsp4j.ClientCapabilities; @@ -259,6 +260,10 @@ public void update(Preferences preferences) { if (!oldPreferences.getFilesAssociations().equals(preferences.getFilesAssociations())) { configureContentTypes(preferences); } + + // update call hierachy test code filer + final boolean filterTestCode = this.preferences.getSearchScope() == SearchScope.main; + eclipsePreferences.put("PREF_FILTER_TESTCODE", String.valueOf(filterTestCode)); } // only for test purpose 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 4835a8b82c..5a69fafcd3 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 @@ -537,6 +537,16 @@ 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 + * + * Any other unknown value will be treated as all. + */ + public static final String JAVA_SEARCH_SCOPE = "java.search.scope"; + public static final String TEXT_DOCUMENT_FORMATTING = "textDocument/formatting"; public static final String TEXT_DOCUMENT_RANGE_FORMATTING = "textDocument/rangeFormatting"; public static final String TEXT_DOCUMENT_ON_TYPE_FORMATTING = "textDocument/onTypeFormatting"; @@ -704,6 +714,7 @@ public class Preferences { private boolean validateAllOpenBuffersOnChanges; private boolean chainCompletionEnabled; private List diagnosticFilter; + private SearchScope searchScope; static { JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>(); @@ -781,6 +792,22 @@ static FeatureStatus fromString(String value, FeatureStatus defaultStatus) { } } + public static enum SearchScope { + all, main; + + 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 + } + } + return defaultScope; + } + } + public static class ReferencedLibraries { private Set include; private Set exclude; @@ -1352,6 +1379,10 @@ public static Preferences createFrom(Map configuration) { } } prefs.setFilesAssociations(new ArrayList<>(associations)); + + String searchScope = getString(configuration, JAVA_SEARCH_SCOPE, null); + prefs.setSearchScope(SearchScope.fromString(searchScope, SearchScope.all)); + return prefs; } @@ -2623,4 +2654,12 @@ public List getFilesAssociations() { public void setFilesAssociations(List filesAssociations) { this.filesAssociations = filesAssociations; } + + public void setSearchScope(SearchScope value) { + this.searchScope = value; + } + + public SearchScope getSearchScope() { + return searchScope; + } }