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. (#940)

* Only wait 3 seconds for csharpier to format, otherwise kill it and restart it.

closes #926

* Better desc for plugin on jetbrains site

* adding note about deprecation so it can be fixed later
  • Loading branch information
belav authored Aug 27, 2023
1 parent dfab351 commit b91a8bb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 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
4 changes: 2 additions & 2 deletions Src/CSharpier.Rider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ To use it:
- Optionally configure CSharpier to `Run on Save` under Preferences/Settings | Tools | CSharpier

Please report any [issues](https://github.com/belav/csharpier/issues)
<!-- Plugin description end -->

## Installation

Expand All @@ -20,7 +19,6 @@ Please report any [issues](https://github.com/belav/csharpier/issues)
<kbd>Settings/Preferences</kbd> > <kbd>Plugins</kbd> > <kbd>Marketplace</kbd> > <kbd>Search for "CSharpier"</kbd> >
<kbd>Install Plugin</kbd>
---
Plugin based on the [IntelliJ Platform Plugin Template][template].

## Troubleshooting

Expand All @@ -33,3 +31,5 @@ Plugin based on the [IntelliJ Platform Plugin Template][template].
- Add entry for "#com.intellij.csharpier.CSharpierLogger"
- Restart Rider

<!-- Plugin description end -->

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ReformatWithCSharpierOnSave implements AnActionListener {
private final Logger logger = CSharpierLogger.getInstance();

public ReformatWithCSharpierOnSave() {
// TODO this is deprecated and should be switched to https://plugins.jetbrains.com/docs/intellij/messaging-infrastructure.html#subscribing-to-a-topic
Topics.subscribe(AnActionListener.TOPIC, null, this);
}

Expand Down

0 comments on commit b91a8bb

Please sign in to comment.