Skip to content

Commit

Permalink
Fix #3252 Add support for scoping the search operations (#3253)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gayanper authored Sep 23, 2024
1 parent ec37ae5 commit 12b4cbc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Location> findReferences(ReferenceParams param, IProgressMonitor monitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <ul>
* <li><code>main</code> - Scope for main code</li>
* <li><code>all</code> - Scope for both test and main code</li>
* </ul>
* Any other unknown value will be treated as <code>all</code>.
*/
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";
Expand Down Expand Up @@ -704,6 +714,7 @@ public class Preferences {
private boolean validateAllOpenBuffersOnChanges;
private boolean chainCompletionEnabled;
private List<String> diagnosticFilter;
private SearchScope searchScope;

static {
JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>();
Expand Down Expand Up @@ -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<String> include;
private Set<String> exclude;
Expand Down Expand Up @@ -1352,6 +1379,10 @@ public static Preferences createFrom(Map<String, Object> configuration) {
}
}
prefs.setFilesAssociations(new ArrayList<>(associations));

String searchScope = getString(configuration, JAVA_SEARCH_SCOPE, null);
prefs.setSearchScope(SearchScope.fromString(searchScope, SearchScope.all));

return prefs;
}

Expand Down Expand Up @@ -2623,4 +2654,12 @@ public List<String> getFilesAssociations() {
public void setFilesAssociations(List<String> filesAssociations) {
this.filesAssociations = filesAssociations;
}

public void setSearchScope(SearchScope value) {
this.searchScope = value;
}

public SearchScope getSearchScope() {
return searchScope;
}
}

0 comments on commit 12b4cbc

Please sign in to comment.