Skip to content

Commit 0bb04c5

Browse files
committed
Recognize custom file extensions
1 parent 494042b commit 0bb04c5

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseInitHandler.java

+46-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
import java.util.ArrayList;
2020
import java.util.Collection;
2121
import java.util.Collections;
22+
import java.util.List;
2223
import java.util.Map;
2324

2425
import org.eclipse.core.resources.ResourcesPlugin;
2526
import org.eclipse.core.runtime.CoreException;
2627
import org.eclipse.core.runtime.IPath;
28+
import org.eclipse.core.runtime.Platform;
29+
import org.eclipse.core.runtime.content.IContentType;
2730
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
2831
import org.eclipse.core.runtime.preferences.InstanceScope;
32+
import org.eclipse.jdt.core.JavaCore;
33+
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
2934
import org.eclipse.jdt.ls.core.internal.IConstants;
3035
import org.eclipse.jdt.ls.core.internal.JSONUtility;
3136
import org.eclipse.jdt.ls.core.internal.JVMConfigurator;
@@ -57,19 +62,59 @@ public BaseInitHandler(ProjectsManager projectsManager, PreferenceManager prefer
5762
this.projectsManager = projectsManager;
5863
}
5964

60-
@SuppressWarnings("unchecked")
6165
public InitializeResult initialize(InitializeParams param) {
6266
logInfo("Initializing Java Language Server " + JavaLanguageServerPlugin.getVersion());
6367
InitializeResult result = new InitializeResult();
6468
handleInitializationOptions(param);
6569
registerCapabilities(result);
70+
configureContentTypes();
6671

6772
// At the end of the InitHandler, trigger a job to import the workspace. This is used to ensure ServiceStatus notification
6873
// is not sent before the initialize response. See the bug https://github.com/redhat-developer/vscode-java/issues/1056
6974
triggerInitialization(preferenceManager.getPreferences().getRootPaths());
7075
return result;
7176
}
7277

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")
73118
public Map<?, ?> handleInitializationOptions(InitializeParams param) {
74119
Map<?, ?> initializationOptions = this.getInitializationOptions(param);
75120
Map<String, Object> extendedClientCapabilities = getInitializationOption(initializationOptions, "extendedClientCapabilities", Map.class);
@@ -109,7 +154,6 @@ public InitializeResult initialize(InitializeParams param) {
109154
rootPaths.add(workspaceLocation);
110155
}
111156
if (initializationOptions.get(SETTINGS_KEY) instanceof Map<?, ?> settings) {
112-
@SuppressWarnings("unchecked")
113157
Preferences prefs = Preferences.createFrom((Map<String, Object>) settings);
114158
prefs.setRootPaths(rootPaths);
115159
preferenceManager.update(prefs);

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java

+30
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public class Preferences {
120120
* Tab Size
121121
*/
122122
public static final String JAVA_CONFIGURATION_TABSIZE = "java.format.tabSize";
123+
124+
/**
125+
* Files associations to languages
126+
*/
127+
public static final String JAVA_CONFIGURATION_ASSOCIATIONS = "java.associations";
123128
/**
124129
* Specifies Java Execution Environments.
125130
*/
@@ -589,6 +594,7 @@ public class Preferences {
589594
private static Map<String, List<String>> nonnullClasspathStorage = new HashMap<>();
590595
private static Map<String, List<String>> nullableClasspathStorage = new HashMap<>();
591596
private static Map<String, List<String>> nonnullbydefaultClasspathStorage = new HashMap<>();
597+
private List<String> filesAssociations = new ArrayList<>();
592598

593599
private Map<String, Object> configuration;
594600
private Severity incompleteClasspathSeverity;
@@ -1330,6 +1336,22 @@ public static Preferences createFrom(Map<String, Object> configuration) {
13301336
prefs.setChainCompletionEnabled(chainCompletionEnabled);
13311337
List<String> diagnosticFilter = getList(configuration, JAVA_DIAGNOSTIC_FILER, Collections.emptyList());
13321338
prefs.setDiagnosticFilter(diagnosticFilter);
1339+
Object associations = getValue(configuration, JAVA_CONFIGURATION_ASSOCIATIONS);
1340+
List<String> filesAssociations = new ArrayList<>();
1341+
if (associations instanceof Map map) {
1342+
try {
1343+
Map<String, String> element = map;
1344+
element.forEach((k, v) -> {
1345+
// Java LS only recognizes *.<extension>
1346+
if ("java".equals(v) && k.startsWith("*.") && k.length() > 2) {
1347+
filesAssociations.add(k.substring(2));
1348+
}
1349+
});
1350+
} catch (Exception e) {
1351+
JavaLanguageServerPlugin.logException(e);
1352+
}
1353+
}
1354+
prefs.setFilesAssociations(filesAssociations);
13331355
return prefs;
13341356
}
13351357

@@ -2582,4 +2604,12 @@ public List<String> getDiagnosticFilter() {
25822604
public void setDiagnosticFilter(List<String> diagnosticFilter) {
25832605
this.diagnosticFilter = diagnosticFilter;
25842606
}
2607+
2608+
public List<String> getFilesAssociations() {
2609+
return filesAssociations;
2610+
}
2611+
2612+
public void setFilesAssociations(List<String> filesAssociations) {
2613+
this.filesAssociations = filesAssociations;
2614+
}
25852615
}

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.eclipse.jdt.core.IJavaProject;
5757
import org.eclipse.jdt.core.JavaCore;
5858
import org.eclipse.jdt.core.JavaModelException;
59+
import org.eclipse.jdt.internal.core.util.Util;
5960
import org.eclipse.jdt.launching.IVMInstall;
6061
import org.eclipse.jdt.launching.IVMInstallChangedListener;
6162
import org.eclipse.jdt.launching.JavaRuntime;
@@ -462,6 +463,24 @@ public void testMissingResourceOperations() throws Exception {
462463
assertFalse(preferences.isResourceOperationSupported());
463464
}
464465

466+
// https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3222
467+
@Test
468+
public void testFilesAssociations() throws Exception {
469+
try {
470+
ClientPreferences mockCapabilies = mock(ClientPreferences.class);
471+
when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
472+
List<String> fileAssociations = new ArrayList<>();
473+
fileAssociations.add("maxj");
474+
preferences.setFilesAssociations(fileAssociations);
475+
initialize(false);
476+
assertTrue(Util.isJavaLikeFileName("Test.maxj"));
477+
} finally {
478+
preferences.getFilesAssociations().clear();
479+
initialize(false);
480+
assertFalse(Util.isJavaLikeFileName("Test.maxj"));
481+
}
482+
}
483+
465484
private void removeExclusionPattern(IJavaProject javaProject) throws JavaModelException {
466485
IClasspathEntry[] classpath = javaProject.getRawClasspath();
467486
for (int i = 0; i < classpath.length; i++) {

0 commit comments

Comments
 (0)