From 1f515ee63b4e707606c920cf8761db67f18c7c3e Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Tue, 20 Feb 2018 19:04:42 +0100 Subject: [PATCH] java.import.exclusions preference is ignored during server startup Signed-off-by: Snjezana Peco --- .../core/internal/handlers/InitHandler.java | 11 ++++++- .../internal/handlers/InitHandlerTest.java | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java index 156d76aafe..cd8044c17b 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java @@ -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; @@ -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; @@ -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; @@ -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 javaSettings = new HashMap<>(); + javaSettings.put("java", settings); + Preferences prefs = Preferences.createFrom(javaSettings); + preferenceManager.update(prefs); + } triggerInitialization(rootPaths); addWorkspaceDiagnosticsHandler(); Integer processId = param.getProcessId(); @@ -233,7 +243,6 @@ private Collection getWorkspaceFolders(Map initializationOptions) } return null; } - private Collection getBundleList(Map initializationOptions) { if (initializationOptions != null) { Object bundleObject = initializationOptions.get(BUNDLES_KEY); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java index 9b02a43beb..d01c4ea3ab 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -131,6 +137,31 @@ public void testRegisterDelayedCapability() throws Exception { verify(client, times(7)).registerCapability(any()); } + @Test + public void testJavaImportExclusions() throws Exception { + Map initializationOptions = createInitializationOptions(); + @SuppressWarnings("unchecked") + Preferences prefs = Preferences.createFrom((Map) (initializationOptions.get(InitHandler.SETTINGS_KEY))); + assertEquals(TEST_EXCLUSIONS, prefs.getJavaImportExclusions().get(0)); + } + + private Map createInitializationOptions() { + List javaImportExclusions = new ArrayList<>(); + javaImportExclusions.add(TEST_EXCLUSIONS); + HashMap exclusionsMap = getMap("exclusions", javaImportExclusions); + HashMap importMap = getMap("import", exclusionsMap); + Map javaMap = getMap("java", importMap); + Map initializationOptions = new HashMap<>(); + initializationOptions.put(InitHandler.SETTINGS_KEY, javaMap); + return initializationOptions; + } + + private HashMap getMap(String key, Object obj) { + HashMap 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();