Skip to content

Commit

Permalink
Fix eclipse-jdtls#3252 Add support for scoping the search operations
Browse files Browse the repository at this point in the history
The current version support scoping the following search operations
- reference search
- call hierarchy search

with the following scopes to choose from
- all : includes all classpath entries
- main: all classpath entries excluding test
  • Loading branch information
gayanper committed Aug 24, 2024
1 parent 3ca99cb commit 99edd96
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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 @@ -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 99edd96

Please sign in to comment.