Skip to content

Commit

Permalink
Try writing to the stream a few times
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Sep 27, 2023
1 parent 8c6a3b1 commit 904bd14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
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.10
pluginVersion = 1.3.11-beta1

# 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 @@ -19,6 +19,11 @@ public CSharpierProcessPipeMultipleFiles(String csharpierPath, boolean useUtf8)
this.csharpierPath = csharpierPath;
this.useUtf8 = useUtf8;
this.startProcess();

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
this.formatFile("public class ClassName { }", "Test.cs");
this.formatFile("public class ClassName { }", "Test.cs");
}

private void startProcess() {
Expand All @@ -35,41 +40,40 @@ private void startProcess() {
// if we don't read the error stream, eventually too much is buffered on it and the plugin hangs
var errorGobbler = new StreamGobbler(this.process.getErrorStream());
errorGobbler.start();


} catch (Exception e) {
this.logger.error("error", e);
}

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
this.formatFile("public class ClassName { }", "Test.cs");
this.formatFile("public class ClassName { }", "Test.cs");
}

@Override
public String formatFile(String content, String filePath) {
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;
var attempt = 1;
while (attempt < 5) {
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();
break;
} catch (IOException e) {
this.logger.warn(e);
this.startProcess();
stringBuilder.setLength(0);
attempt++;
}
} catch (Exception e) {
this.logger.error(e);
e.printStackTrace();
}
};

Expand Down

0 comments on commit 904bd14

Please sign in to comment.