|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Collection;
|
21 | 21 | import java.util.Collections;
|
| 22 | +import java.util.List; |
22 | 23 | import java.util.Map;
|
23 | 24 |
|
24 | 25 | import org.eclipse.core.resources.ResourcesPlugin;
|
25 | 26 | import org.eclipse.core.runtime.CoreException;
|
26 | 27 | import org.eclipse.core.runtime.IPath;
|
| 28 | +import org.eclipse.core.runtime.Platform; |
| 29 | +import org.eclipse.core.runtime.content.IContentType; |
27 | 30 | import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
28 | 31 | import org.eclipse.core.runtime.preferences.InstanceScope;
|
| 32 | +import org.eclipse.jdt.core.JavaCore; |
| 33 | +import org.eclipse.jdt.internal.compiler.util.SuffixConstants; |
29 | 34 | import org.eclipse.jdt.ls.core.internal.IConstants;
|
30 | 35 | import org.eclipse.jdt.ls.core.internal.JSONUtility;
|
31 | 36 | import org.eclipse.jdt.ls.core.internal.JVMConfigurator;
|
@@ -57,19 +62,59 @@ public BaseInitHandler(ProjectsManager projectsManager, PreferenceManager prefer
|
57 | 62 | this.projectsManager = projectsManager;
|
58 | 63 | }
|
59 | 64 |
|
60 |
| - @SuppressWarnings("unchecked") |
61 | 65 | public InitializeResult initialize(InitializeParams param) {
|
62 | 66 | logInfo("Initializing Java Language Server " + JavaLanguageServerPlugin.getVersion());
|
63 | 67 | InitializeResult result = new InitializeResult();
|
64 | 68 | handleInitializationOptions(param);
|
65 | 69 | registerCapabilities(result);
|
| 70 | + configureContentTypes(); |
66 | 71 |
|
67 | 72 | // At the end of the InitHandler, trigger a job to import the workspace. This is used to ensure ServiceStatus notification
|
68 | 73 | // is not sent before the initialize response. See the bug https://github.com/redhat-developer/vscode-java/issues/1056
|
69 | 74 | triggerInitialization(preferenceManager.getPreferences().getRootPaths());
|
70 | 75 | return result;
|
71 | 76 | }
|
72 | 77 |
|
| 78 | + private void configureContentTypes() { |
| 79 | + if (preferenceManager.getPreferences().getFilesAssociations() != null) { |
| 80 | + IContentType javaSourceContentType = Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE); |
| 81 | + if (javaSourceContentType != null) { |
| 82 | + List<String> toRemove = new ArrayList<>(); |
| 83 | + String[] specs = javaSourceContentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); |
| 84 | + for (String spec : specs) { |
| 85 | + if (!SuffixConstants.EXTENSION_java.equals(spec)) { |
| 86 | + toRemove.add(spec); |
| 87 | + } |
| 88 | + } |
| 89 | + List<String> toAdd = new ArrayList<>(); |
| 90 | + for (String spec : preferenceManager.getPreferences().getFilesAssociations()) { |
| 91 | + if (toRemove.contains(spec)) { |
| 92 | + toRemove.remove(spec); |
| 93 | + } else { |
| 94 | + toAdd.add(spec); |
| 95 | + } |
| 96 | + } |
| 97 | + for (String spec : toRemove) { |
| 98 | + try { |
| 99 | + javaSourceContentType.removeFileSpec(spec, IContentType.FILE_EXTENSION_SPEC); |
| 100 | + } catch (CoreException e) { |
| 101 | + JavaLanguageServerPlugin.logException(e); |
| 102 | + } |
| 103 | + } |
| 104 | + for (String spec : toAdd) { |
| 105 | + try { |
| 106 | + javaSourceContentType.addFileSpec(spec, IContentType.FILE_EXTENSION_SPEC); |
| 107 | + } catch (CoreException e) { |
| 108 | + JavaLanguageServerPlugin.logException(e); |
| 109 | + } |
| 110 | + } |
| 111 | + } else { |
| 112 | + JavaLanguageServerPlugin.logInfo("There is not java source content type"); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @SuppressWarnings("unchecked") |
73 | 118 | public Map<?, ?> handleInitializationOptions(InitializeParams param) {
|
74 | 119 | Map<?, ?> initializationOptions = this.getInitializationOptions(param);
|
75 | 120 | Map<String, Object> extendedClientCapabilities = getInitializationOption(initializationOptions, "extendedClientCapabilities", Map.class);
|
@@ -109,7 +154,6 @@ public InitializeResult initialize(InitializeParams param) {
|
109 | 154 | rootPaths.add(workspaceLocation);
|
110 | 155 | }
|
111 | 156 | if (initializationOptions.get(SETTINGS_KEY) instanceof Map<?, ?> settings) {
|
112 |
| - @SuppressWarnings("unchecked") |
113 | 157 | Preferences prefs = Preferences.createFrom((Map<String, Object>) settings);
|
114 | 158 | prefs.setRootPaths(rootPaths);
|
115 | 159 | preferenceManager.update(prefs);
|
|
0 commit comments