Skip to content

Commit

Permalink
chore: update plugin to compile against upstream LSP4e [HEAD-349] (#142)
Browse files Browse the repository at this point in the history
* chore: don't stop language server (set timeout real high)

* fix: update plugin to compile against upstream LSP4e API

* docs: update Changelog
  • Loading branch information
bastiandoetsch authored Jun 6, 2023
1 parent edabcad commit b023ab9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 83 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Snyk Changelog

## [2.0.0] - Unreleased
## [2.1.0] - UNRELEASED
### Changes
- Increase LS version
- update plugin to cater to LSP4e API changes
- cleanup redundant code
- don't shutdown language server after some time of inactivity

## [2.1.0] - UNRELEASED
## [2.1.0] - v20230307.102901
### Changes
- unpin LSP4e dependency - this requires running Eclipse on JDK 17+

Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
id="io.snyk.languageserver"
label="Snyk Language Server"
clientImpl="io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient"
lastDocumentDisconnectedTimeout="3600"
lastDocumentDisconnectedTimeout="3000000"
singleton="true"
makerType="io.snyk.languageserver.marker">
</server>
Expand Down
23 changes: 18 additions & 5 deletions plugin/src/main/java/io/snyk/eclipse/plugin/SnykStartup.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.lsp4e.LanguageServersRegistry;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
Expand Down Expand Up @@ -60,24 +62,35 @@ protected IStatus run(IProgressMonitor monitor) {
monitor.subTask("Starting download of Snyk Language Server");
download(monitor);
}

monitor.subTask("Starting Snyk Language Server...");
SnykLanguageServer.InitializeServer();
} catch (Exception exception) {
logError(exception);
}
downloading = false;


monitor.subTask("Starting Snyk Language Server...");
startLanguageServer();

if (Preferences.getInstance().getAuthToken().isBlank()) {
monitor.subTask("Starting Snyk Wizard to configure initial settings...");
SnykWizard wizard = new SnykWizard();
WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(), wizard);
dialog.setBlockOnOpen(true);
dialog.open();
}

});

return Status.OK_STATUS;
}

private void startLanguageServer() {
var definition = LanguageServersRegistry.getInstance().getDefinition(SnykLanguageServer.LANGUAGE_SERVER_ID);
try {
LanguageServiceAccessor.startLanguageServer(definition);
} catch (IOException e) {
logError(e);
}
}
};
initJob.setPriority(Job.LONG);
initJob.schedule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.snyk.eclipse.plugin.Activator;
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
import io.snyk.eclipse.plugin.utils.SnykLogger;
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;

import java.io.File;
import java.util.Collections;
Expand All @@ -23,27 +24,10 @@ public void configurationChanged() {
var params = new DidChangeConfigurationParams();
params.setSettings(getCurrentSettings());

var definition = LanguageServersRegistry.getInstance().getDefinition(SnykLanguageServer.LANGUAGE_SERVER_ID);

if (definition == null) {
return;
}
for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
if (project.isAccessible()) {
try {
var servers = Collections
.unmodifiableCollection(LanguageServiceAccessor.getLanguageServers(project, null));
for (LanguageServer ls : servers) {
var currentDefinition = LanguageServiceAccessor.resolveServerDefinition(ls);
if (currentDefinition.isEmpty() || !currentDefinition.get().id.equals(definition.id))
continue;
WorkspaceService workspaceService = ls.getWorkspaceService();
workspaceService.didChangeConfiguration(params);
}
} catch (Exception e) {
SnykLogger.logError(e);
}
}
SnykExtendedLanguageClient lc = SnykExtendedLanguageClient.getInstance();
if (lc != null) {
var languageServer = lc.getConnectedLanguageServer();
languageServer.getWorkspaceService().didChangeConfiguration(params);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.snyk.eclipse.plugin.utils.Lists;
import io.snyk.eclipse.plugin.utils.SnykLogger;

@SuppressWarnings("restriction")
public class SnykLanguageServer extends ProcessStreamConnectionProvider implements StreamConnectionProvider {
public static final String LANGUAGE_SERVER_ID = "io.snyk.languageserver";
private final LsRuntimeEnvironment runtimeEnvironment;
Expand All @@ -46,18 +47,6 @@ public void start() throws IOException {
setCommands(commands);
setWorkingDirectory(workingDir);
super.start();
new Job("Snyk: Sending preferences to Language Server") {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
new LsConfigurationUpdater().configurationChanged();
monitor.done();
} catch (RuntimeException e) {
SnykLogger.logError(e);
}
return Status.OK_STATUS;
}
}.schedule();
}

@Override
Expand All @@ -77,19 +66,4 @@ public Object getInitializationOptions(URI rootUri) {
}
return currentSettings;
}

public static void InitializeServer() {
var projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject project : projects) {
if (project.isAccessible()) {
try {
LanguageServiceAccessor.getLSWrapper(project,
LanguageServersRegistry.getInstance().getDefinition(SnykLanguageServer.LANGUAGE_SERVER_ID));
} catch (IOException e) {
SnykLogger.logError(e);
return;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.snyk.languageserver.protocolextension;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -11,23 +10,18 @@
import java.util.stream.Collectors;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageClientImpl;
import org.eclipse.lsp4e.ServerMessageHandler;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.ProgressParams;
import org.eclipse.lsp4j.ShowDocumentParams;
import org.eclipse.lsp4j.ShowDocumentResult;
import org.eclipse.lsp4j.WorkDoneProgressCreateParams;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.jsonrpc.validation.NonNull;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
Expand Down Expand Up @@ -61,6 +55,12 @@ public static SnykExtendedLanguageClient getInstance() {
return instance; // we leave instantiation to LSP4e, no lazy construction here
}

public LanguageServer getConnectedLanguageServer() {
return super.getLanguageServer();
}



public void triggerScan(IWorkbenchWindow window) {
if (Preferences.getInstance().getAuthToken().isBlank()) {
runSnykWizard();
Expand Down Expand Up @@ -165,7 +165,7 @@ private void showAuthenticatedMessage() {
MessageParams messageParams = new MessageParams();
messageParams.setType(MessageType.Info);
messageParams.setMessage("The authentication token has been stored in Snyk Preferences.");
ServerMessageHandler.showMessage("Authentication with Snyk successful", messageParams);
super.showMessage(messageParams);
}

private void runForProject(String projectName) {
Expand All @@ -178,7 +178,7 @@ private void runForProject(String projectName) {
private void executeCommand(@NonNull String command, List<Object> arguments) {
ExecuteCommandParams params = new ExecuteCommandParams(command, arguments);
try {
getLanguageServer().getWorkspaceService().executeCommand(params);
getConnectedLanguageServer().getWorkspaceService().executeCommand(params);
} catch (Exception e) {
SnykLogger.logError(e);
}
Expand All @@ -194,23 +194,6 @@ protected void setAuthenticationMethod(HasAuthenticatedParam param, Preferences
}
}

// TODO: remove once LSP4e supports `showDocument` in its next release (it's
// been merged to it already)
@Override
public CompletableFuture<ShowDocumentResult> showDocument(ShowDocumentParams params) {
return CompletableFuture.supplyAsync(() -> {
PlatformUI.getWorkbench().getDisplay().syncExec(() -> {
var location = new Location(params.getUri(), params.getSelection());
var window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
var page = window.getActivePage();
LSPEclipseUtils.openInEditor(location, page);
}
});
return new ShowDocumentResult(true);
});
}

/**
* Refresh the token using language server. Waits up to 2s for the token change.
* @return true if token has changed, false if not
Expand Down

0 comments on commit b023ab9

Please sign in to comment.