Skip to content

Commit

Permalink
Merge branch 'main' into fix-extra-line
Browse files Browse the repository at this point in the history
  • Loading branch information
shocklateboy92 authored Aug 28, 2024
2 parents 829c93f + 4d51407 commit 84d330f
Show file tree
Hide file tree
Showing 25 changed files with 1,264 additions and 1,205 deletions.
3 changes: 2 additions & 1 deletion Src/CSharpier.Rider/.prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
printWidth: 100
tabWidth: 4
plugins:
- prettier-plugin-java
- prettier-plugin-java
4 changes: 4 additions & 0 deletions Src/CSharpier.Rider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# csharpier-rider Changelog

## [1.8.2]
- Possible fix for issue with OSX not being able to run dotnet tool list command
- Better handling of error when validating custom install of csharpier

## [1.8.0]
- Use dotnet tool list to find both local and global installs of csharpier.

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 @@ -3,7 +3,7 @@

pluginGroup = com.intellij.csharpier
pluginName = csharpier
pluginVersion = 1.8.0
pluginVersion = 1.8.2

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier.Rider/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"scripts": {
"prettier": "prettier ./**/*.java --write"
},
"devDependencies": {
"prettier-plugin-java": "^2.6.4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class CSharpierLogger {

private static final Logger logger = Logger.getInstance(CSharpierLogger.class);
private static final Logger logger = Logger.getInstance(CSharpierLogger.class);

public static Logger getInstance() {
return logger;
}
public static Logger getInstance() {
return logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,149 +8,152 @@

public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Disposable {

private final boolean useUtf8;
private final String csharpierPath;
private final DotNetProvider dotNetProvider;
private final String version;
private Logger logger = CSharpierLogger.getInstance();

private Process process = null;
private OutputStreamWriter stdin;
private BufferedReader stdOut;
private boolean processFailedToStart;

public CSharpierProcessPipeMultipleFiles(
String csharpierPath,
boolean useUtf8,
String version,
Project project
) {
this.csharpierPath = csharpierPath;
this.useUtf8 = useUtf8;
this.dotNetProvider = DotNetProvider.getInstance(project);
this.version = version;
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() {
try {
var processBuilder = new ProcessBuilder(this.csharpierPath, "--pipe-multiple-files");
processBuilder.environment().put("DOTNET_NOLOGO", "1");
processBuilder.environment().put("DOTNET_ROOT", this.dotNetProvider.getDotNetRoot());
this.process = processBuilder.start();

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)
);

// 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.warn("Failed to spawn the needed csharpier process. Formatting cannot occur.", e);
this.processFailedToStart = true;
}
}

@Override
public String getVersion() {
return this.version;
}

@Override
public boolean getProcessFailedToStart() {
return this.processFailedToStart;
}

@Override
public String formatFile(String content, String filePath) {
if (this.processFailedToStart) {
this.logger.warn("CSharpier proccess failed to start. Formatting cannot occur.");
return "";
private final boolean useUtf8;
private final String csharpierPath;
private final DotNetProvider dotNetProvider;
private final String version;
private Logger logger = CSharpierLogger.getInstance();

private Process process = null;
private OutputStreamWriter stdin;
private BufferedReader stdOut;
private boolean processFailedToStart;

public CSharpierProcessPipeMultipleFiles(
String csharpierPath,
boolean useUtf8,
String version,
Project project
) {
this.csharpierPath = csharpierPath;
this.useUtf8 = useUtf8;
this.dotNetProvider = DotNetProvider.getInstance(project);
this.version = version;
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");
}

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();
private void startProcess() {
try {
var processBuilder = new ProcessBuilder(this.csharpierPath, "--pipe-multiple-files");
processBuilder.environment().put("DOTNET_NOLOGO", "1");
processBuilder.environment().put("DOTNET_ROOT", this.dotNetProvider.getDotNetRoot());
this.process = processBuilder.start();

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)
);

// 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.warn(
"Failed to spawn the needed csharpier process. Formatting cannot occur.",
e
);
this.processFailedToStart = true;
}
} catch (Exception e) {
this.logger.error(e);
e.printStackTrace();
}
};

// 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 (thread.isAlive()) {
this.logger.warn("CSharpier process appears to be hung, restarting it.");
thread.interrupt();
this.process.destroy();
this.startProcess();
return "";
@Override
public String getVersion() {
return this.version;
}

var result = stringBuilder.toString();

if (result == null || result.isEmpty()) {
this.logger.info("File is ignored by .csharpierignore or there was an error");
return "";
@Override
public boolean getProcessFailedToStart() {
return this.processFailedToStart;
}

return result;
}
@Override
public String formatFile(String content, String filePath) {
if (this.processFailedToStart) {
this.logger.warn("CSharpier proccess failed to start. Formatting cannot occur.");
return "";
}

@Override
public void dispose() {
if (this.process != null) {
this.process.destroy();
}
}
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();
}
} catch (Exception e) {
this.logger.error(e);
e.printStackTrace();
}
};

// 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
}

private class StreamGobbler extends Thread {
if (thread.isAlive()) {
this.logger.warn("CSharpier process appears to be hung, restarting it.");
thread.interrupt();
this.process.destroy();
this.startProcess();
return "";
}

InputStream inputStream;
var result = stringBuilder.toString();

private StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
if (result == null || result.isEmpty()) {
this.logger.info("File is ignored by .csharpierignore or there was an error");
return "";
}

return result;
}

@Override
public void run() {
try {
var streamReader = new InputStreamReader(this.inputStream);
var reader = new BufferedReader(streamReader);
while (reader.readLine() != null) {}
} catch (IOException ioe) {}
public void dispose() {
if (this.process != null) {
this.process.destroy();
}
}

private class StreamGobbler extends Thread {

InputStream inputStream;

private StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
}

@Override
public void run() {
try {
var streamReader = new InputStreamReader(this.inputStream);
var reader = new BufferedReader(streamReader);
while (reader.readLine() != null) {}
} catch (IOException ioe) {}
}
}
}
}
Loading

0 comments on commit 84d330f

Please sign in to comment.