Skip to content

Commit

Permalink
fix width
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Aug 18, 2024
1 parent c529570 commit 1cdcf0a
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 282 deletions.
1 change: 1 addition & 0 deletions Src/CSharpier.Rider/.prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
printWidth: 100
plugins:
- prettier-plugin-java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.io.*;
import java.nio.charset.Charset;

public class CSharpierProcessPipeMultipleFiles
implements ICSharpierProcess, Disposable {
public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Disposable {

private final boolean useUtf8;
private final String csharpierPath;
Expand Down Expand Up @@ -40,24 +39,14 @@ public CSharpierProcessPipeMultipleFiles(

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

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

this.stdin = new OutputStreamWriter(
this.process.getOutputStream(),
charset
);
this.stdin = new OutputStreamWriter(this.process.getOutputStream(), charset);
this.stdOut = new BufferedReader(
new InputStreamReader(this.process.getInputStream(), charset)
);
Expand All @@ -66,10 +55,7 @@ private void startProcess() {
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.logger.warn("Failed to spawn the needed csharpier process. Formatting cannot occur.", e);
this.processFailedToStart = true;
}
}
Expand All @@ -87,9 +73,7 @@ public boolean getProcessFailedToStart() {
@Override
public String formatFile(String content, String filePath) {
if (this.processFailedToStart) {
this.logger.warn(
"CSharpier proccess failed to start. Formatting cannot occur."
);
this.logger.warn("CSharpier proccess failed to start. Formatting cannot occur.");
return "";
}

Expand Down Expand Up @@ -138,9 +122,7 @@ public String formatFile(String content, String filePath) {
var result = stringBuilder.toString();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,22 @@
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Node;

public class CSharpierProcessProvider
implements DocumentListener, Disposable, IProcessKiller {
public class CSharpierProcessProvider implements DocumentListener, Disposable, IProcessKiller {

private final CustomPathInstaller customPathInstaller;
private final Logger logger = CSharpierLogger.getInstance();
private final Project project;

private boolean warnedForOldVersion;
private final HashMap<String, Long> lastWarmedByDirectory = new HashMap<>();
private final HashMap<String, String> csharpierVersionByDirectory =
new HashMap<>();
private final HashMap<String, ICSharpierProcess> csharpierProcessesByVersion =
new HashMap<>();
private final HashMap<String, String> csharpierVersionByDirectory = new HashMap<>();
private final HashMap<String, ICSharpierProcess> csharpierProcessesByVersion = new HashMap<>();

public CSharpierProcessProvider(@NotNull Project project) {
this.project = project;
this.customPathInstaller = new CustomPathInstaller(project);

EditorFactory.getInstance()
.getEventMulticaster()
.addDocumentListener(this, this);
EditorFactory.getInstance().getEventMulticaster().addDocumentListener(this, this);
}

@NotNull
Expand All @@ -62,9 +57,7 @@ public void documentChanged(@NotNull DocumentEvent event) {
var file = FileDocumentManager.getInstance().getFile(document);

if (
file == null ||
file.getExtension() == null ||
!file.getExtension().equalsIgnoreCase("cs")
file == null || file.getExtension() == null || !file.getExtension().equalsIgnoreCase("cs")
) {
return;
}
Expand All @@ -79,31 +72,23 @@ private void findAndWarmProcess(String filePath) {
}
var directory = Path.of(filePath).getParent().toString();
var now = Instant.now().toEpochMilli();
var lastWarmed =
this.lastWarmedByDirectory.getOrDefault(directory, Long.valueOf(0));
var lastWarmed = this.lastWarmedByDirectory.getOrDefault(directory, Long.valueOf(0));
if (lastWarmed + 5000 > now) {
return;
}
this.logger.debug("Ensure there is a csharpier process for " + directory);
this.lastWarmedByDirectory.put(directory, now);
var version =
this.csharpierVersionByDirectory.getOrDefault(directory, null);
var version = this.csharpierVersionByDirectory.getOrDefault(directory, null);
if (version == null) {
version = this.getCSharpierVersion(directory);
if (version == null || version.isEmpty()) {
InstallerService.getInstance(this.project).displayInstallNeededMessage(
directory,
this
);
InstallerService.getInstance(this.project).displayInstallNeededMessage(directory, this);
}
this.csharpierVersionByDirectory.put(directory, version);
}

if (!this.csharpierProcessesByVersion.containsKey(version)) {
this.csharpierProcessesByVersion.put(
version,
this.setupCSharpierProcess(directory, version)
);
this.csharpierProcessesByVersion.put(version, this.setupCSharpierProcess(directory, version));
}
}

Expand All @@ -114,16 +99,13 @@ public ICSharpierProcess getProcessFor(String filePath) {
}

var directory = Path.of(filePath).getParent().toString();
var version =
this.csharpierVersionByDirectory.getOrDefault(directory, null);
var version = this.csharpierVersionByDirectory.getOrDefault(directory, null);
if (version == null) {
this.findAndWarmProcess(filePath);
version = this.csharpierVersionByDirectory.get(directory);
}

if (
version == null || !this.csharpierProcessesByVersion.containsKey(version)
) {
if (version == null || !this.csharpierProcessesByVersion.containsKey(version)) {
// this shouldn't really happen, but just in case
return NullCSharpierProcess.Instance;
}
Expand All @@ -133,22 +115,17 @@ public ICSharpierProcess getProcessFor(String filePath) {

private String getCSharpierVersion(String directoryThatContainsFile) {
var csharpierVersion = FunctionRunner.runUntilNonNull(
() ->
this.findVersionInCsProjOfParentsDirectories(directoryThatContainsFile),
() ->
this.findCSharpierVersionInToolOutput(directoryThatContainsFile, false),
() ->
this.findCSharpierVersionInToolOutput(directoryThatContainsFile, true)
() -> this.findVersionInCsProjOfParentsDirectories(directoryThatContainsFile),
() -> this.findCSharpierVersionInToolOutput(directoryThatContainsFile, false),
() -> this.findCSharpierVersionInToolOutput(directoryThatContainsFile, true)
);

if (csharpierVersion == null) {
return "";
}

var versionWithoutHash = csharpierVersion.split(Pattern.quote("+"))[0];
this.logger.debug(
"Using " + versionWithoutHash + " as the version number."
);
this.logger.debug("Using " + versionWithoutHash + " as the version number.");
return versionWithoutHash;
}

Expand All @@ -163,9 +140,7 @@ private String findCSharpierVersionInToolOutput(
);

this.logger.debug(
"Running 'dotnet tool list" +
(isGlobal ? "-g" : "") +
"' to look for version"
"Running 'dotnet tool list" + (isGlobal ? "-g" : "") + "' to look for version"
);
this.logger.debug("Output was: \n " + output);

Expand All @@ -187,9 +162,7 @@ private String findCSharpierVersionInToolOutput(
return null;
}

private String findVersionInCsProjOfParentsDirectories(
String directoryThatContainsFile
) {
private String findVersionInCsProjOfParentsDirectories(String directoryThatContainsFile) {
this.logger.debug(
"Looking for csproj in or above " +
directoryThatContainsFile +
Expand Down Expand Up @@ -233,14 +206,9 @@ private String findVersionInCsProj(Path currentDirectory) {
continue;
}

var versionOfMsBuildPackage = node
.getAttributes()
.getNamedItem("Version")
.getNodeValue();
var versionOfMsBuildPackage = node.getAttributes().getNamedItem("Version").getNodeValue();
if (versionOfMsBuildPackage != null) {
this.logger.debug(
"Found version " + versionOfMsBuildPackage + " in " + pathToCsProj
);
this.logger.debug("Found version " + versionOfMsBuildPackage + " in " + pathToCsProj);
return versionOfMsBuildPackage;
}
} catch (Exception e) {
Expand All @@ -256,10 +224,7 @@ private String findVersionInCsProj(Path currentDirectory) {
return null;
}

private ICSharpierProcess setupCSharpierProcess(
String directory,
String version
) {
private ICSharpierProcess setupCSharpierProcess(String directory, String version) {
if (version == null || version.equals("")) {
return NullCSharpierProcess.Instance;
}
Expand All @@ -272,9 +237,7 @@ private ICSharpierProcess setupCSharpierProcess(

var customPath = this.customPathInstaller.getPathForVersion(version);

this.logger.debug(
"Adding new version " + version + " process for " + directory
);
this.logger.debug("Adding new version " + version + " process for " + directory);

// ComparableVersion was unhappy in rider 2023, this code should probably just go away
// but there are still 0.12 and 0.14 downloads happening
Expand All @@ -287,23 +250,15 @@ private ICSharpierProcess setupCSharpierProcess(
versionWeCareAbout >= serverVersion &&
!CSharpierSettings.getInstance(this.project).getDisableCSharpierServer()
) {
csharpierProcess = new CSharpierProcessServer(
customPath,
version,
this.project
);
csharpierProcess = new CSharpierProcessServer(customPath, version, this.project);
} else if (versionWeCareAbout >= 12) {
var useUtf8 = versionWeCareAbout >= 14;

if (
versionWeCareAbout >= serverVersion &&
CSharpierSettings.getInstance(
this.project
).getDisableCSharpierServer()
CSharpierSettings.getInstance(this.project).getDisableCSharpierServer()
) {
this.logger.debug(
"CSharpier server is disabled, falling back to piping via stdin"
);
this.logger.debug("CSharpier server is disabled, falling back to piping via stdin");
}

csharpierProcess = new CSharpierProcessPipeMultipleFiles(
Expand All @@ -324,11 +279,7 @@ private ICSharpierProcess setupCSharpierProcess(
this.warnedForOldVersion = true;
}

csharpierProcess = new CSharpierProcessSingleFile(
customPath,
version,
this.project
);
csharpierProcess = new CSharpierProcessSingleFile(customPath, version, this.project);
}

if (csharpierProcess.getProcessFailedToStart()) {
Expand All @@ -351,10 +302,7 @@ private void displayFailureMessage() {
.getNotificationGroup("CSharpier")
.createNotification(title, message, NotificationType.WARNING);
notification.addAction(
new OpenUrlAction(
"Read More",
"https://csharpier.com/docs/EditorsTroubleshooting"
)
new OpenUrlAction("Read More", "https://csharpier.com/docs/EditorsTroubleshooting")
);
notification.notify(this.project);
}
Expand All @@ -366,9 +314,7 @@ public void dispose() {

public void killRunningProcesses() {
for (var key : this.csharpierProcessesByVersion.keySet()) {
this.logger.debug(
"disposing of process for version " + (key == "" ? "null" : key)
);
this.logger.debug("disposing of process for version " + (key == "" ? "null" : key));
this.csharpierProcessesByVersion.get(key).dispose();
}
this.lastWarmedByDirectory.clear();
Expand Down
Loading

0 comments on commit 1cdcf0a

Please sign in to comment.