generated from redhat-developer/new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Investigate using OSProcessHandler
Fixes #153 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
9d1e0fe
commit 5142610
Showing
12 changed files
with
221 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/redhat/devtools/lsp4ij/server/LSPProcessListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.redhat.devtools.lsp4ij.server; | ||
|
||
import com.intellij.execution.impl.ExecutionManagerImpl; | ||
import com.intellij.execution.process.ProcessEvent; | ||
import com.intellij.execution.process.ProcessListener; | ||
import com.intellij.execution.process.ProcessOutputType; | ||
import com.intellij.openapi.util.Key; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
public class LSPProcessListener implements ProcessListener { | ||
|
||
private final PipedOutputStream outputStream; | ||
private final OutputStreamWriter outputStreamWriter; | ||
private final PipedInputStream inputStream; | ||
private final @NotNull List<LanguageServerLogErrorHandler> errorHandlers; | ||
|
||
public LSPProcessListener(List<LanguageServerLogErrorHandler> errorHandlers) throws IOException { | ||
this.outputStream = new PipedOutputStream(); | ||
this.outputStreamWriter = new OutputStreamWriter(this.outputStream, StandardCharsets.UTF_8); | ||
this.inputStream = new PipedInputStream(this.outputStream); | ||
this.errorHandlers = errorHandlers; | ||
} | ||
|
||
@NotNull | ||
public final InputStream getInputStream() { | ||
return this.inputStream; | ||
} | ||
|
||
@Override | ||
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) { | ||
ProcessListener.super.onTextAvailable(event, outputType); | ||
|
||
if (ProcessOutputType.isStdout(outputType)) { | ||
try { | ||
this.outputStreamWriter.write(event.getText()); | ||
this.outputStreamWriter.flush(); | ||
} catch (IOException e) { | ||
ExecutionManagerImpl.stopProcess(event.getProcessHandler()); | ||
} | ||
} else if (ProcessOutputType.isStderr(outputType)) { | ||
for (var handler : errorHandlers) { | ||
handler.logError(event.getText()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void processTerminated(@NotNull ProcessEvent event) { | ||
try { | ||
outputStreamWriter.close(); | ||
outputStream.close(); | ||
} catch (IOException e) { | ||
// Do nothing | ||
} | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/redhat/devtools/lsp4ij/server/LanguageServerLogErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.redhat.devtools.lsp4ij.server; | ||
|
||
public interface LanguageServerLogErrorHandler { | ||
|
||
void logError(String error); | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/com/redhat/devtools/lsp4ij/server/OSProcessStreamConnectionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.redhat.devtools.lsp4ij.server; | ||
|
||
import com.intellij.execution.configurations.GeneralCommandLine; | ||
import com.intellij.execution.impl.ExecutionManagerImpl; | ||
import com.intellij.execution.process.OSProcessHandler; | ||
import com.intellij.util.io.BaseOutputReader; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class OSProcessStreamConnectionProvider implements StreamConnectionProvider { | ||
|
||
private GeneralCommandLine commandLine; | ||
private OSProcessHandler processHandler; | ||
private @NotNull InputStream inputStream; | ||
private List<LanguageServerLogErrorHandler> handlers; | ||
|
||
public OSProcessStreamConnectionProvider(@Nullable GeneralCommandLine commandLine) { | ||
this.commandLine = commandLine; | ||
this.handlers = new ArrayList<>(); | ||
} | ||
|
||
public void setCommandLine(GeneralCommandLine commandLine) { | ||
this.commandLine = commandLine; | ||
} | ||
|
||
public GeneralCommandLine getCommandLine() { | ||
return commandLine; | ||
} | ||
|
||
@Override | ||
public void addLogErrorHandler(LanguageServerLogErrorHandler handler) { | ||
handlers.add(handler); | ||
} | ||
|
||
@Override | ||
public void start() throws CannotStartProcessException { | ||
if (this.commandLine == null) { | ||
throw new CannotStartProcessException("Unable to start language server: " + this); | ||
} | ||
try { | ||
processHandler = new OSProcessHandler(commandLine) { | ||
@Override | ||
protected BaseOutputReader.@NotNull Options readerOptions() { | ||
// If it's a long-running mostly idle daemon process, | ||
// consider overriding OSProcessHandler#readerOptions | ||
// with 'BaseOutputReader.Options.forMostlySilentProcess()' | ||
// to reduce CPU usage. | ||
return BaseOutputReader.Options.forMostlySilentProcess(); | ||
} | ||
}; | ||
LSPProcessListener processListener = new LSPProcessListener(handlers); | ||
processHandler.addProcessListener(processListener); | ||
inputStream = processListener.getInputStream(); | ||
processHandler.startNotify(); | ||
} catch (Exception e) { | ||
throw new CannotStartProcessException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isAlive() { | ||
return processHandler != null && processHandler.isStartNotified() && !processHandler.isProcessTerminated(); | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() { | ||
return inputStream; | ||
} | ||
|
||
@Override | ||
public OutputStream getOutputStream() { | ||
return processHandler.getProcessInput(); | ||
} | ||
|
||
@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() + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
...lorSchemes/SemanticTokensHighConstrat.xml → ...lorSchemes/SemanticTokensHighContrast.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?xml version='1.0'?> | ||
<list> | ||
<option name="LSP_TYPE_PARAMETER"> | ||
<value> | ||
<option name="FOREGROUND" value="37cccc" /> | ||
<option name="FONT_TYPE" value="1" /> | ||
</value> | ||
</option> | ||
<?xml version='1.0'?> | ||
<list> | ||
<option name="LSP_TYPE_PARAMETER"> | ||
<value> | ||
<option name="FOREGROUND" value="37cccc" /> | ||
<option name="FONT_TYPE" value="1" /> | ||
</value> | ||
</option> | ||
</list> |