|
12 | 12 | *******************************************************************************/
|
13 | 13 | package org.eclipse.jdt.ls.core.internal.preferences;
|
14 | 14 |
|
15 |
| -import java.io.File; |
16 | 15 | import java.io.IOException;
|
17 | 16 | import java.io.StringWriter;
|
18 | 17 | import java.io.Writer;
|
19 |
| -import java.net.URI; |
| 18 | +import java.util.ArrayList; |
20 | 19 | import java.util.Collections;
|
21 |
| -import java.util.HashSet; |
22 | 20 | import java.util.Hashtable;
|
23 | 21 | import java.util.LinkedHashMap;
|
24 | 22 | import java.util.List;
|
|
28 | 26 | import java.util.stream.Collectors;
|
29 | 27 |
|
30 | 28 | import org.apache.commons.lang3.StringUtils;
|
31 |
| -import org.eclipse.core.resources.IMarker; |
32 |
| -import org.eclipse.core.resources.IProject; |
33 | 29 | import org.eclipse.core.resources.IResource;
|
34 | 30 | import org.eclipse.core.runtime.CoreException;
|
35 | 31 | import org.eclipse.core.runtime.ISafeRunnable;
|
36 | 32 | import org.eclipse.core.runtime.ListenerList;
|
| 33 | +import org.eclipse.core.runtime.Platform; |
37 | 34 | import org.eclipse.core.runtime.SafeRunner;
|
| 35 | +import org.eclipse.core.runtime.content.IContentType; |
38 | 36 | import org.eclipse.core.runtime.preferences.DefaultScope;
|
39 | 37 | import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
40 | 38 | import org.eclipse.core.runtime.preferences.InstanceScope;
|
|
43 | 41 | import org.eclipse.jdt.core.JavaCore;
|
44 | 42 | import org.eclipse.jdt.core.manipulation.CodeStyleConfiguration;
|
45 | 43 | import org.eclipse.jdt.core.manipulation.JavaManipulation;
|
| 44 | +import org.eclipse.jdt.internal.compiler.util.SuffixConstants; |
46 | 45 | import org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType;
|
47 | 46 | import org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType.CodeTemplateVariableResolver;
|
48 | 47 | import org.eclipse.jdt.internal.core.manipulation.JavaManipulationMessages;
|
|
54 | 53 | import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
|
55 | 54 | import org.eclipse.jdt.ls.core.internal.IConstants;
|
56 | 55 | import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
|
57 |
| -import org.eclipse.jdt.ls.core.internal.ProjectUtils; |
58 | 56 | import org.eclipse.jdt.ls.core.internal.ResourceUtils;
|
59 | 57 | import org.eclipse.jdt.ls.core.internal.StatusFactory;
|
60 | 58 | import org.eclipse.jdt.ls.core.internal.handlers.BaseDiagnosticsHandler;
|
@@ -258,6 +256,49 @@ public void update(Preferences preferences) {
|
258 | 256 | JavaLanguageServerPlugin.getInstance().getClientConnection().publishDiagnostics(diagnostics);
|
259 | 257 | }
|
260 | 258 | }
|
| 259 | + if (!oldPreferences.getFilesAssociations().equals(preferences.getFilesAssociations())) { |
| 260 | + configureContentTypes(preferences); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + // only for test purpose |
| 265 | + public static void configureContentTypes(Preferences preferences) { |
| 266 | + if (preferences != null && preferences.getFilesAssociations() != null) { |
| 267 | + IContentType javaSourceContentType = Platform.getContentTypeManager().getContentType(JavaCore.JAVA_SOURCE_CONTENT_TYPE); |
| 268 | + if (javaSourceContentType != null) { |
| 269 | + List<String> toRemove = new ArrayList<>(); |
| 270 | + String[] specs = javaSourceContentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); |
| 271 | + for (String spec : specs) { |
| 272 | + if (!SuffixConstants.EXTENSION_java.equals(spec)) { |
| 273 | + toRemove.add(spec); |
| 274 | + } |
| 275 | + } |
| 276 | + List<String> toAdd = new ArrayList<>(); |
| 277 | + for (String spec : preferences.getFilesAssociations()) { |
| 278 | + if (toRemove.contains(spec)) { |
| 279 | + toRemove.remove(spec); |
| 280 | + } else { |
| 281 | + toAdd.add(spec); |
| 282 | + } |
| 283 | + } |
| 284 | + for (String spec : toRemove) { |
| 285 | + try { |
| 286 | + javaSourceContentType.removeFileSpec(spec, IContentType.FILE_EXTENSION_SPEC); |
| 287 | + } catch (CoreException e) { |
| 288 | + JavaLanguageServerPlugin.logException(e); |
| 289 | + } |
| 290 | + } |
| 291 | + for (String spec : toAdd) { |
| 292 | + try { |
| 293 | + javaSourceContentType.addFileSpec(spec, IContentType.FILE_EXTENSION_SPEC); |
| 294 | + } catch (CoreException e) { |
| 295 | + JavaLanguageServerPlugin.logException(e); |
| 296 | + } |
| 297 | + } |
| 298 | + } else { |
| 299 | + JavaLanguageServerPlugin.logInfo("There is no java source content type."); |
| 300 | + } |
| 301 | + } |
261 | 302 | }
|
262 | 303 |
|
263 | 304 | private void preferencesChanged(Preferences oldPreferences, Preferences newPreferences) {
|
|
0 commit comments