Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom gson config #1191

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions org.eclipse.lsp4e/schema/languageServer.exsd
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
</appinfo>
</annotation>
</attribute>
<attribute name="configureGson" type="string">
<annotation>
<documentation>
An optional gson builder to configure the gson instance used by the language server. If undefined, the default gson builder is used.
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="java.util.function.Consumer:" />
</appinfo>
</annotation>
</attribute>
<attribute name="singleton" type="boolean">
<annotation>
<documentation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private synchronized void start(boolean forceRestart) {
.setInput(lspStreamProvider.getInputStream())//
.setOutput(lspStreamProvider.getOutputStream())//
.setExecutorService(listener)//
.configureGson(serverDefinition.getConfigureGson())
.wrapMessages(wrapper)//
.create();
final var languageServer = workingContext.languageServer = launcher.getRemoteProxy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import org.eclipse.ui.statushandlers.StatusManager;
import org.osgi.framework.Bundle;

import com.google.gson.GsonBuilder;

/**
* This registry aims at providing a good language server connection (as {@link StreamConnectionProvider}
* for a given input.
Expand Down Expand Up @@ -82,6 +84,7 @@ public class LanguageServersRegistry {
private static final String MAKER_TYPE_ELEMENT = "makerType"; //$NON-NLS-1$
private static final String MARKER_TYPE_ELEMENT = "markerType"; //$NON-NLS-1$
private static final String MARKER_ATTR_COMPUTER_ELEMENT = "markerAttributeComputer"; //$NON-NLS-1$
private static final String CONFIGURE_GSON_ATTRIBUTE = "configureGson"; //$NON-NLS-1$
private static final String SERVER_INTERFACE_ATTRIBUTE = "serverInterface"; //$NON-NLS-1$
private static final String LAUNCHER_BUILDER_ATTRIBUTE = "launcherBuilder"; //$NON-NLS-1$
private static final String LABEL_ATTRIBUTE = "label"; //$NON-NLS-1$
Expand Down Expand Up @@ -121,6 +124,10 @@ public <S extends LanguageServer> Launcher.Builder<S> createLauncherBuilder() {
return new Launcher.Builder<>();
}

public Consumer<GsonBuilder> getConfigureGson() {
return builder -> {};
}

}

static class ExtensionLanguageServerDefinition extends LanguageServerDefinition {
Expand Down Expand Up @@ -208,6 +215,24 @@ public Class<? extends LanguageServer> getServerInterface() {
return super.getServerInterface();
}

@SuppressWarnings("unchecked")
@Override
public Consumer<GsonBuilder> getConfigureGson() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use the method createLauncherBuilder as a template

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I think you can create a helper method that can be reused by both methods, just passing CONFIGURE_GSON_ATTRIBUTE or LAUNCHER_BUILDER_ATTRIBUTE

String configGson = extension.getAttribute(CONFIGURE_GSON_ATTRIBUTE);
if (configGson != null && !configGson.isEmpty()) {
Bundle bundle = Platform.getBundle(extension.getContributor().getName());
if (bundle != null) {
try {
return (Consumer<GsonBuilder>) extension.createExecutableExtension(CONFIGURE_GSON_ATTRIBUTE);
} catch (ClassNotFoundException exception) {
StatusManager.getManager().handle(new Status(IStatus.ERROR, LanguageServerPlugin.PLUGIN_ID,
exception.getMessage(), exception));
}
}
}
return super.getConfigureGson();
}

@SuppressWarnings("unchecked")
@Override
public <S extends LanguageServer> Launcher.Builder<S> createLauncherBuilder() {
Expand Down
Loading