From 12b4cbce0a33f9fa3e81e574fc7fe34873bc84f0 Mon Sep 17 00:00:00 2001 From: Gayan Perera Date: Mon, 23 Sep 2024 22:50:03 +0200 Subject: [PATCH] Fix #3252 Add support for scoping the search operations (#3253) - supports references, call hierarchy, and workspace symbols - with the following scopes to choose from : - all : includes all classpath entries - main: all classpath entries excluding test --- .../internal/handlers/CodeLensHandler.java | 4 +- .../internal/handlers/ReferencesHandler.java | 4 +- .../handlers/WorkspaceSymbolHandler.java | 5 ++- .../preferences/PreferenceManager.java | 5 +++ .../internal/preferences/Preferences.java | 39 +++++++++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeLensHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeLensHandler.java index 5c7e6da2c0..106df05ec0 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeLensHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeLensHandler.java @@ -45,6 +45,7 @@ import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.ResourceUtils; import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager; +import org.eclipse.jdt.ls.core.internal.preferences.Preferences.SearchScope; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.Region; @@ -261,6 +262,7 @@ private CodeLens getCodeLens(String type, IJavaElement element, ITypeRoot typeRo private IJavaSearchScope createSearchScope() throws JavaModelException { IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); - return SearchEngine.createJavaSearchScope(projects, IJavaSearchScope.SOURCES); + var excludeTestCode = preferenceManager.getPreferences().getSearchScope() == SearchScope.main; + return SearchEngine.createJavaSearchScope(excludeTestCode, projects, IJavaSearchScope.SOURCES); } } 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/handlers/WorkspaceSymbolHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceSymbolHandler.java index 4762241e26..347e82744e 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceSymbolHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceSymbolHandler.java @@ -35,6 +35,7 @@ import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.ProjectUtils; 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.SymbolInformation; import org.eclipse.lsp4j.SymbolKind; @@ -126,8 +127,8 @@ private static IJavaSearchScope createSearchScope(String projectName, boolean so if (!sourceOnly && preferenceManager != null && preferenceManager.isClientSupportsClassFileContent()) { scope |= IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES; } - - return SearchEngine.createJavaSearchScope(targetProjects, scope); + var excludeTestCode = preferenceManager.getPreferences().getSearchScope() == SearchScope.main; + return SearchEngine.createJavaSearchScope(excludeTestCode, targetProjects, scope); } public static class SearchSymbolParams extends WorkspaceSymbolParams { 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; + } }