From 8c2cbcc3ce5965632a32b7b3d69bcb8d2478a2a3 Mon Sep 17 00:00:00 2001 From: Kaloyan Raev Date: Fri, 16 Sep 2016 23:04:40 +0300 Subject: [PATCH] Fix #2471: Send json/schemaAssociations notification 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 --- .../server/launcher/JsonLanguageServer.java | 57 +++++++++++++++++++ .../launcher/JsonLanguageServerLauncher.java | 2 +- .../registry/ServerInitializerImpl.java | 4 ++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServer.java diff --git a/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServer.java b/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServer.java new file mode 100644 index 000000000000..251767063bcd --- /dev/null +++ b/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServer.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2012-2016 Codenvy, S.A. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Codenvy, S.A. - initial API and implementation + *******************************************************************************/ +package org.eclipse.che.plugin.languageserver.server.launcher; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.che.plugin.languageserver.server.registry.ServerInitializerObserver; +import org.eclipse.che.plugin.languageserver.shared.model.LanguageDescription; + +import io.typefox.lsapi.ServerCapabilities; +import io.typefox.lsapi.services.LanguageServer; +import io.typefox.lsapi.services.json.JsonBasedLanguageServer; + +/** + * Implements the specifics related to the JSON language server. + * + * After the Initialize Request the client must send a 'json/schemaAssociations' + * notification in order to associate JSON schemas to popular JSON files. This + * automatically enables code completion, validation, hover, etc. capabilities + * for these files without the need of adding a "$schema" key. + * + * @author Kaloyan Raev + */ +public class JsonLanguageServer extends JsonBasedLanguageServer implements ServerInitializerObserver { + + private final static String JSON_SCHEMA_ASSOCIATIONS = "json/schemaAssociations"; + + @Override + public void onServerInitialized(LanguageServer server, ServerCapabilities capabilities, + LanguageDescription languageDescription, String projectPath) { + registerSchemaAssociations(); + } + + private void registerSchemaAssociations() { + Map associations = new HashMap<>(); + associations.put("/*.schema.json", new String[] { "http://json-schema.org/draft-04/schema#" }); + associations.put("/bower.json", new String[] { "http://json.schemastore.org/bower" }); + associations.put("/.bower.json", new String[] { "http://json.schemastore.org/bower" }); + associations.put("/.bowerrc", new String[] { "http://json.schemastore.org/bowerrc" }); + associations.put("/composer.json", new String[] { "https://getcomposer.org/schema.json" }); + associations.put("/package.json", new String[] { "http://json.schemastore.org/package" }); + associations.put("/jsconfig.json", new String[] { "http://json.schemastore.org/jsconfig" }); + associations.put("/tsconfig.json", new String[] { "http://json.schemastore.org/tsconfig" }); + + sendNotification(JSON_SCHEMA_ASSOCIATIONS, associations); + } + +} diff --git a/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServerLauncher.java b/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServerLauncher.java index d292d97793ae..fc21ec5b95ee 100644 --- a/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServerLauncher.java +++ b/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/launcher/JsonLanguageServerLauncher.java @@ -50,7 +50,7 @@ public LanguageDescription getLanguageDescription() { } protected JsonBasedLanguageServer connectToLanguageServer(Process languageServerProcess) { - JsonBasedLanguageServer languageServer = new JsonBasedLanguageServer(); + JsonBasedLanguageServer languageServer = new JsonLanguageServer(); languageServer.connect(languageServerProcess.getInputStream(), languageServerProcess.getOutputStream()); return languageServer; } diff --git a/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/registry/ServerInitializerImpl.java b/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/registry/ServerInitializerImpl.java index ff83a1c7c1d0..688f03327e6d 100644 --- a/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/registry/ServerInitializerImpl.java +++ b/plugins/plugin-languageserver/che-plugin-languageserver-server/src/main/java/org/eclipse/che/plugin/languageserver/server/registry/ServerInitializerImpl.java @@ -145,6 +145,10 @@ protected LanguageServer doInitialize(LanguageServerLauncher launcher, String pr protected void registerCallbacks(LanguageServer server) { server.getTextDocumentService().onPublishDiagnostics(publishDiagnosticsParamsMessenger::onEvent); server.getWindowService().onLogMessage(messageParams -> LOG.error(messageParams.getType() + " " + messageParams.getMessage())); + + if (server instanceof ServerInitializerObserver) { + addObserver((ServerInitializerObserver) server); + } } protected InitializeParamsImpl prepareInitializeParams(String projectPath) {