Skip to content

Commit

Permalink
Only wait 3 seconds for csharpier to format, otherwise kill it and re…
Browse files Browse the repository at this point in the history
…start it.

closes #926
  • Loading branch information
belav committed Aug 23, 2023
1 parent 81b6789 commit 607299a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Src/CSharpier.Rider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# csharpier-rider Changelog

## [1.3.9]
- Wait at most 3 seconds for csharpier to format otherwise consider it hung and restart it.

## [1.3.8]
- Add displayName attribute to CSharpier options window to speed up Settings dialog.

Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Rider/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pluginGroup = com.intellij.csharpier
pluginName = csharpier
# SemVer format -> https://semver.org
pluginVersion = 1.3.8
pluginVersion = 1.3.9

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
import java.util.concurrent.atomic.AtomicBoolean;

public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Disposable {
private final boolean useUtf8;
private final String csharpierPath;
Logger logger = CSharpierLogger.getInstance();

Process process = null;
OutputStreamWriter stdin;
BufferedReader stdOut;

public CSharpierProcessPipeMultipleFiles(String csharpierPath, boolean useUtf8) {
this.csharpierPath = csharpierPath;
this.useUtf8 = useUtf8;
this.startProcess();
}

private void startProcess() {
try {
var processBuilder = new ProcessBuilder(csharpierPath, "--pipe-multiple-files");
var processBuilder = new ProcessBuilder(this.csharpierPath, "--pipe-multiple-files");
processBuilder.environment().put("DOTNET_NOLOGO", "1");
this.process = processBuilder.start();

var charset = useUtf8 ? "utf-8" : Charset.defaultCharset().toString();
var charset = this.useUtf8 ? "utf-8" : Charset.defaultCharset().toString();

this.stdin = new OutputStreamWriter(this.process.getOutputStream(), charset);
this.stdOut = new BufferedReader(new InputStreamReader(this.process.getInputStream(), charset));
Expand All @@ -43,40 +51,56 @@ public CSharpierProcessPipeMultipleFiles(String csharpierPath, boolean useUtf8)

@Override
public String formatFile(String content, String filePath) {
try {

this.stdin.write(filePath);
this.stdin.write('\u0003');
this.stdin.write(content);
this.stdin.write('\u0003');
this.stdin.flush();

var stringBuilder = new StringBuilder();

var nextCharacter = this.stdOut.read();
while (nextCharacter != -1) {
if (nextCharacter == '\u0003') {
break;
var stringBuilder = new StringBuilder();

Runnable task = () -> {
try {
this.stdin.write(filePath);
this.stdin.write('\u0003');
this.stdin.write(content);
this.stdin.write('\u0003');
this.stdin.flush();

var nextCharacter = this.stdOut.read();
while (nextCharacter != -1) {
if (nextCharacter == '\u0003') {
break;
}
stringBuilder.append((char) nextCharacter);
nextCharacter = this.stdOut.read();
}
stringBuilder.append((char) nextCharacter);
nextCharacter = this.stdOut.read();
} catch (Exception e) {
this.logger.error(e);
e.printStackTrace();
}
};

var result = stringBuilder.toString();
// csharpier will freeze in some instances when "Format on Save" is also installed and the file has compilation errors
// this detects that and recovers from it
var thread = new Thread(task);
thread.start();
try {
thread.join(3000);
} catch (InterruptedException e) {
// if we interrupt it we shouldn't log it
}

if (result == null || result.isEmpty())
{
this.logger.info("File is ignored by .csharpierignore or there was an error");
return "";
}
if (thread.isAlive()) {
this.logger.warn("CSharpier process appears to be hung, restarting it.");
thread.interrupt();
this.process.destroy();
this.startProcess();
return "";
}

return result;
var result = stringBuilder.toString();

} catch (Exception e) {
this.logger.error(e);
e.printStackTrace();
if (result == null || result.isEmpty()) {
this.logger.info("File is ignored by .csharpierignore or there was an error");
return "";
}

return result;
}

@Override
Expand Down

0 comments on commit 607299a

Please sign in to comment.