Skip to content

Commit

Permalink
feat: Investigate using OSProcessHandler
Browse files Browse the repository at this point in the history
Fixes #153

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Sep 18, 2024
1 parent 9d1e0fe commit e6d7463
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
*******************************************************************************/
package com.redhat.devtools.lsp4ij.server;

import java.io.IOException;

/**
* Language server exception when start process cannot be done.
*/
public class CannotStartProcessException extends LanguageServerException {

public CannotStartProcessException(IOException e) {
public CannotStartProcessException(Exception e) {
super(e);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.redhat.devtools.lsp4ij.server;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.impl.ExecutionManagerImpl;
import com.intellij.execution.process.OSProcessHandler;
import org.jetbrains.annotations.Nullable;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

public class OSProcessStreamConnectionProvider implements StreamConnectionProvider {

private GeneralCommandLine commandLine;
private OSProcessHandler processHandler;

public OSProcessStreamConnectionProvider(GeneralCommandLine commandLine) {
this.commandLine = commandLine;
}

public void setCommandLine(GeneralCommandLine commandLine) {
this.commandLine = commandLine;
}

public GeneralCommandLine getCommandLine() {
return commandLine;
}

@Override
public void start() throws CannotStartProcessException {
if (this.commandLine == null) {
throw new CannotStartProcessException("Unable to start language server: " + this);
}
try {
processHandler = new OSProcessHandler(commandLine);
} catch (ExecutionException e) {
throw new CannotStartProcessException(e);
}
}

@Override
public boolean isAlive() {
return processHandler != null && processHandler.isStartNotified() && !processHandler.isProcessTerminated();
}

@Override
public InputStream getInputStream() {
return processHandler.getProcess().getInputStream();
}

@Override
public OutputStream getOutputStream() {
return processHandler.getProcessInput();
}

@Override
public @Nullable InputStream getErrorStream() {
return null;
}

@Override
public void stop() {
if (processHandler != null && !processHandler.isProcessTerminated()) {
ExecutionManagerImpl.stopProcess(processHandler);
}
}

@Override
public int hashCode() {
return Objects.hash(this.getCommandLine());
}

@Override
public String toString() {
return "OSProcessStreamConnectionProvider [commandLine=" + this.getCommandLine() + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
*******************************************************************************/
package com.redhat.devtools.lsp4ij.server.definition.launching;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.EnvironmentUtil;
import com.redhat.devtools.lsp4ij.server.OSProcessStreamConnectionProvider;
import com.redhat.devtools.lsp4ij.server.ProcessStreamConnectionProvider;
import org.jetbrains.annotations.NotNull;

Expand All @@ -23,20 +26,31 @@
* {@link ProcessStreamConnectionProvider} implementation to start a language server with a
* process command defined by the user.
*/
public class UserDefinedStreamConnectionProvider extends ProcessStreamConnectionProvider {
public class UserDefinedStreamConnectionProvider extends OSProcessStreamConnectionProvider {

private final @NotNull UserDefinedLanguageServerDefinition serverDefinition;

public UserDefinedStreamConnectionProvider(@NotNull String commandLine,
@NotNull Map<String, String> userEnvironmentVariables,
boolean includeSystemEnvironmentVariables,
@NotNull UserDefinedLanguageServerDefinition serverDefinition) {
super.setCommands(CommandUtils.createCommands(commandLine));
super.setUserEnvironmentVariables(userEnvironmentVariables);
super.setIncludeSystemEnvironmentVariables(includeSystemEnvironmentVariables);
super(createCommandLine(commandLine, userEnvironmentVariables, includeSystemEnvironmentVariables));
this.serverDefinition = serverDefinition;
}

@NotNull
private static GeneralCommandLine createCommandLine(@NotNull String commandLine,
@NotNull Map<String, String> userEnvironmentVariables,
boolean includeSystemEnvironmentVariables) {
Map<String, String> environmentVariables = userEnvironmentVariables;
// Add System environment variables
if (includeSystemEnvironmentVariables) {
environmentVariables.putAll(EnvironmentUtil.getEnvironmentMap());
}
return new GeneralCommandLine(CommandUtils.createCommands(commandLine))
.withEnvironment(environmentVariables);
}

@Override
public Object getInitializationOptions(VirtualFile rootUri) {
return serverDefinition.getLanguageServerInitializationOptions();
Expand Down

0 comments on commit e6d7463

Please sign in to comment.