Skip to content

Commit fbac7ea

Browse files
kaloyan-raevVitalii Parfonov
authored andcommitted
Fix eclipse-che#2471: Send json/schemaAssociations notification (eclipse-che#2474)
The VS Code's JSON language server expects a 'json/schemaAssociations' notification to associate JSON files to JSON schemas. This activates capabilities like code completion, validation and hover without the need to add a '$schema' key. This change implements an extension of the JsonBasedLanguageServer that registers as ServerInitializerObserver and sends the 'json/schemaAssociations' notification on the 'onServerInitialized' event. Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>
1 parent 3309540 commit fbac7ea

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

plugins/plugin-json/che-plugin-json-server/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<groupId>com.google.inject.extensions</groupId>
3333
<artifactId>guice-multibindings</artifactId>
3434
</dependency>
35+
<dependency>
36+
<groupId>io.typefox.lsapi</groupId>
37+
<artifactId>io.typefox.lsapi</artifactId>
38+
</dependency>
3539
<dependency>
3640
<groupId>io.typefox.lsapi</groupId>
3741
<artifactId>io.typefox.lsapi.services</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2012-2016 Codenvy, S.A.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Codenvy, S.A. - initial API and implementation
10+
*******************************************************************************/
11+
package org.eclipse.che.plugin.json.languageserver;
12+
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
import org.eclipse.che.api.languageserver.registry.ServerInitializerObserver;
17+
import org.eclipse.che.api.languageserver.shared.model.LanguageDescription;
18+
19+
import io.typefox.lsapi.ServerCapabilities;
20+
import io.typefox.lsapi.services.LanguageServer;
21+
import io.typefox.lsapi.services.json.JsonBasedLanguageServer;
22+
23+
/**
24+
* Implements the specifics related to the JSON language server.
25+
*
26+
* After the Initialize Request the client must send a 'json/schemaAssociations'
27+
* notification in order to associate JSON schemas to popular JSON files. This
28+
* automatically enables code completion, validation, hover, etc. capabilities
29+
* for these files without the need of adding a "$schema" key.
30+
*
31+
* @author Kaloyan Raev
32+
*/
33+
public class JsonLanguageServer extends JsonBasedLanguageServer implements ServerInitializerObserver {
34+
35+
private final static String JSON_SCHEMA_ASSOCIATIONS = "json/schemaAssociations";
36+
37+
@Override
38+
public void onServerInitialized(LanguageServer server, ServerCapabilities capabilities,
39+
LanguageDescription languageDescription, String projectPath) {
40+
registerSchemaAssociations();
41+
}
42+
43+
private void registerSchemaAssociations() {
44+
Map<String, String[]> associations = new HashMap<>();
45+
associations.put("/*.schema.json", new String[] { "http://json-schema.org/draft-04/schema#" });
46+
associations.put("/bower.json", new String[] { "http://json.schemastore.org/bower" });
47+
associations.put("/.bower.json", new String[] { "http://json.schemastore.org/bower" });
48+
associations.put("/.bowerrc", new String[] { "http://json.schemastore.org/bowerrc" });
49+
associations.put("/composer.json", new String[] { "https://getcomposer.org/schema.json" });
50+
associations.put("/package.json", new String[] { "http://json.schemastore.org/package" });
51+
associations.put("/jsconfig.json", new String[] { "http://json.schemastore.org/jsconfig" });
52+
associations.put("/tsconfig.json", new String[] { "http://json.schemastore.org/tsconfig" });
53+
54+
sendNotification(JSON_SCHEMA_ASSOCIATIONS, associations);
55+
}
56+
57+
}

plugins/plugin-json/che-plugin-json-server/src/main/java/org/eclipse/che/plugin/json/languageserver/JsonLanguageServerLauncher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public boolean isAbleToLaunch() {
6464
}
6565

6666
protected JsonBasedLanguageServer connectToLanguageServer(Process languageServerProcess) {
67-
JsonBasedLanguageServer languageServer = new JsonBasedLanguageServer();
67+
JsonBasedLanguageServer languageServer = new JsonLanguageServer();
6868
languageServer.connect(languageServerProcess.getInputStream(), languageServerProcess.getOutputStream());
6969
return languageServer;
7070
}

wsagent/che-core-api-languageserver/src/main/java/org/eclipse/che/api/languageserver/registry/ServerInitializerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ protected void registerCallbacks(LanguageServer server) {
139139
server.getTextDocumentService().onPublishDiagnostics(publishDiagnosticsParamsMessenger::onEvent);
140140
server.getWindowService().onLogMessage(messageParams -> LOG.error(messageParams.getType() + " " + messageParams.getMessage()));
141141
server.onTelemetryEvent(o -> LOG.error(o.toString()));
142+
143+
if (server instanceof ServerInitializerObserver) {
144+
addObserver((ServerInitializerObserver) server);
145+
}
142146
}
143147

144148
protected InitializeParamsImpl prepareInitializeParams(String projectPath) {

0 commit comments

Comments
 (0)