Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.import.exclusions preference is ignored during server startup #560

Closed
wants to merge 1 commit into from
Closed
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 @@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -38,6 +39,7 @@
import org.eclipse.jdt.ls.core.internal.ServiceStatus;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.CompletionOptions;
Expand All @@ -56,6 +58,7 @@ final public class InitHandler {

public static final String JAVA_LS_INITIALIZATION_JOBS = "java-ls-initialization-jobs";
private static final String BUNDLES_KEY = "bundles";
public static final String SETTINGS_KEY = "settings";

private ProjectsManager projectsManager;
private JavaClientConnection connection;
Expand Down Expand Up @@ -108,6 +111,13 @@ InitializeResult initialize(InitializeParams param) {
logInfo("No workspace folders or root uri was defined. Falling back on " + workspaceLocation);
rootPaths.add(workspaceLocation);
}
if (initializationOptions instanceof Map && initializationOptions.get(SETTINGS_KEY) instanceof Map) {
Object settings = initializationOptions.get(SETTINGS_KEY);
Map<String, Object> javaSettings = new HashMap<>();
javaSettings.put("java", settings);
Preferences prefs = Preferences.createFrom(javaSettings);
preferenceManager.update(prefs);
}
triggerInitialization(rootPaths);
addWorkspaceDiagnosticsHandler();
Integer processId = param.getProcessId();
Expand Down Expand Up @@ -233,7 +243,6 @@ private Collection<String> getWorkspaceFolders(Map<?, ?> initializationOptions)
}
return null;
}

private Collection<String> getBundleList(Map<?, ?> initializationOptions) {
if (initializationOptions != null) {
Object bundleObject = initializationOptions.get(BUNDLES_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.handlers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -19,7 +20,10 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

Expand All @@ -28,6 +32,7 @@
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.DidChangeConfigurationCapabilities;
import org.eclipse.lsp4j.DidChangeConfigurationParams;
Expand All @@ -54,6 +59,7 @@
@RunWith(MockitoJUnitRunner.class)
public class InitHandlerTest extends AbstractProjectsManagerBasedTest {

private static final String TEST_EXCLUSIONS = "**/test/**";
protected JDTLanguageServer server;
protected JDTLanguageServer protocol;

Expand Down Expand Up @@ -131,6 +137,31 @@ public void testRegisterDelayedCapability() throws Exception {
verify(client, times(7)).registerCapability(any());
}

@Test
public void testJavaImportExclusions() throws Exception {
Map<String, Object> initializationOptions = createInitializationOptions();
@SuppressWarnings("unchecked")
Preferences prefs = Preferences.createFrom((Map<String, Object>) (initializationOptions.get(InitHandler.SETTINGS_KEY)));
assertEquals(TEST_EXCLUSIONS, prefs.getJavaImportExclusions().get(0));
}

private Map<String, Object> createInitializationOptions() {
List<String> javaImportExclusions = new ArrayList<>();
javaImportExclusions.add(TEST_EXCLUSIONS);
HashMap<String, Object> exclusionsMap = getMap("exclusions", javaImportExclusions);
HashMap<String, Object> importMap = getMap("import", exclusionsMap);
Map<String, Object> javaMap = getMap("java", importMap);
Map<String, Object> initializationOptions = new HashMap<>();
initializationOptions.put(InitHandler.SETTINGS_KEY, javaMap);
return initializationOptions;
}

private HashMap<String, Object> getMap(String key, Object obj) {
HashMap<String, Object> map = new HashMap<>();
map.put(key, obj);
return map;
}

private InitializeResult initialize(boolean dynamicRegistration) throws InterruptedException, ExecutionException {
InitializeParams params = new InitializeParams();
ClientCapabilities capabilities = new ClientCapabilities();
Expand Down