Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be helpful to also use the preference in WorkspaceSymbolHandler. Workspace symbols are used to fuzzy search for classes. It can be opened with Ctrl+T in VS Code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will fix in a couple of days!

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);
}

Expand Down Expand Up @@ -218,4 +222,4 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
}, monitor);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <ul>
* <li><code>main</code> - Scope for main code</li>
* <li><code>all</code> - Scope for both test and main code</li>
* <li><code>main</code> - Search main source code and libraries</li>
* <li><code>all</code> - Search main and test source code and libraries</li>
* <li><code>projectOnly</code> - For reference searches, include main and test
* source code and referenced projects, excluding application and system libraries</li>
* </ul>
* Any other unknown value will be treated as <code>all</code>.
* Any unknown value will be treated as <code>all</code>.
*/
public static final String JAVA_SEARCH_SCOPE = "java.search.scope";

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Location> references = handler.findReferences(param, monitor);
assertEquals(1, references.size());
String refereeUri = ResourceUtils.fixURI(referenceProject.getFile("src/org/reference/Main.java").getRawLocationURI());
assertEquals(refereeUri, references.get(0).getUri());

IType system = referenceJavaProject.findType("java.lang.System");
IField field = system.getField("out");
assertTrue(field.exists());
references.clear();
handler.search(field, references, monitor, true);
assertFalse(references.isEmpty());
assertTrue(references.stream().allMatch(reference -> reference.getUri().startsWith("file:")));
} finally {
preferences.setSearchScope(searchScope);
}
}

// https://github.com/redhat-developer/vscode-java/issues/2227
@Test
public void testPotentialMatch() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,28 @@
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.handlers.CompletionGuessMethodArgumentsMode;
import org.eclipse.jdt.ls.core.internal.handlers.MapFlattener;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences.SearchScope;
import org.junit.jupiter.api.Test;

public class PreferencesTest {

@Test
public void testProjectOnlySearchScope() {
Map<String, Object> config = new HashMap<>();
MapFlattener.setValue(config, Preferences.JAVA_SEARCH_SCOPE, "projectOnly");

Preferences preferences = Preferences.createFrom(config);
assertEquals(SearchScope.projectOnly, preferences.getSearchScope());

MapFlattener.setValue(config, Preferences.JAVA_SEARCH_SCOPE, "PROJECTONLY");
preferences = Preferences.createFrom(config);
assertEquals(SearchScope.projectOnly, preferences.getSearchScope());

MapFlattener.setValue(config, Preferences.JAVA_SEARCH_SCOPE, "invalid");
preferences = Preferences.createFrom(config);
assertEquals(SearchScope.all, preferences.getSearchScope());
}

@Test
public void testSetImportOnDemandThreshold() throws Exception {
Preferences preferences = new Preferences();
Expand Down
Loading