Skip to content

Commit d674c30

Browse files
committed
Rider changes needed to support #1191
1 parent bdba345 commit d674c30

File tree

4 files changed

+86
-42
lines changed

4 files changed

+86
-42
lines changed

Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessServer.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import com.google.gson.Gson;
1414
import com.intellij.openapi.project.Project;
1515

16-
public class CSharpierProcessServer implements ICSharpierProcess, Disposable {
16+
// TODO split these changes out into another PR. maybe queue them up with any other rider changes
17+
public class CSharpierProcessServer implements ICSharpierProcess2, Disposable {
1718
private final Gson gson = new Gson();
1819
private final String csharpierPath;
1920
private final DotNetProvider dotNetProvider;
@@ -73,16 +74,12 @@ private void startProcess() {
7374
}
7475

7576
@Override
76-
public String formatFile(String content, String filePath) {
77+
public FormatFileResult formatFile(FormatFileParameter parameter) {
7778
if (this.processFailedToStart) {
7879
this.logger.warn("CSharpier process failed to start. Formatting cannot occur.");
79-
return "";
80+
return null;
8081
}
8182

82-
var data = new FormatFileDto();
83-
data.fileContents = content;
84-
data.fileName = filePath;
85-
8683
var url = "http://localhost:" + this.port + "/format";
8784

8885

@@ -95,20 +92,22 @@ public String formatFile(String content, String filePath) {
9592

9693
connection.setRequestProperty("Content-Type", "application/json; utf-8");
9794

95+
connection.setConnectTimeout(2000);
9896
connection.setDoOutput(true);
9997
connection.setDoInput(true);
10098

10199
var outputStream = connection.getOutputStream();
102100
var writer = new OutputStreamWriter(outputStream, "UTF-8");
103-
writer.write(this.gson.toJson(data));
101+
writer.write(this.gson.toJson(parameter));
104102
writer.flush();
105103
writer.close();
106104
outputStream.close();
107105

108106
var responseCode = connection.getResponseCode();
109107
if (responseCode != 200) {
108+
this.logger.warn("Csharpier server returned non-200 status code of " + responseCode);
110109
connection.disconnect();
111-
return "";
110+
return null;
112111
}
113112

114113
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
@@ -117,13 +116,23 @@ public String formatFile(String content, String filePath) {
117116

118117
connection.disconnect();
119118

120-
return result.formattedFile != null ? result.formattedFile : "";
119+
return result;
121120

122121
} catch (Exception e) {
123122
this.logger.warn("Failed posting to the csharpier server.", e);
124123
}
125124

126-
return "";
125+
return null;
126+
}
127+
128+
@Override
129+
public String formatFile(String content, String fileName) {
130+
var parameter = new FormatFileParameter();
131+
parameter.fileName = fileName;
132+
parameter.fileContents = content;
133+
134+
var result = this.formatFile(parameter);
135+
return result == null ? null : result.formattedFile;
127136
}
128137

129138
@Override
@@ -132,13 +141,4 @@ public void dispose() {
132141
this.process.destroy();
133142
}
134143
}
135-
136-
private class FormatFileDto {
137-
public String fileContents;
138-
public String fileName;
139-
}
140-
141-
private class FormatFileResult {
142-
public String formattedFile;
143-
}
144144
}

Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/DotNetProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ static DotNetProvider getInstance(@NotNull Project project) {
2828
void initialize() {
2929
var foundDotNet = this.findDotNet();
3030
if (!foundDotNet) {
31-
3231
var title = "CSharpier unable to run dotnet commands";
3332
var message = "CSharpier was unable to determine how to run dotnet commands. Ensure that '.NET CLI executable path' is set properly in your settings or dotnet is available on PATH and restart.";
3433
var notification = NotificationGroupManager.getInstance().getNotificationGroup("CSharpier").createNotification(title, message, NotificationType.WARNING);

Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/FormattingService.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import com.intellij.openapi.editor.Document;
66
import com.intellij.openapi.project.Project;
77
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
8-
import com.intellij.openapi.vfs.VirtualFile;
98
import com.intellij.psi.PsiDocumentManager;
10-
import com.intellij.psi.PsiFile;
119
import org.jetbrains.annotations.NotNull;
1210

1311
import java.time.Duration;
@@ -52,29 +50,53 @@ public void format(@NotNull Document document, @NotNull Project project) {
5250

5351
var currentDocumentText = document.getText();
5452

55-
var cSharpierProcessProvider = CSharpierProcessProvider.getInstance(project);
53+
var csharpierProcess = CSharpierProcessProvider.getInstance(project).getProcessFor(filePath);
5654
this.logger.info("Formatting started for " + filePath + ".");
5755
var start = Instant.now();
58-
var result = cSharpierProcessProvider.getProcessFor(filePath).formatFile(currentDocumentText, filePath);
59-
60-
var end = Instant.now();
61-
this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms");
62-
63-
if (result.length() == 0 || currentDocumentText.equals(result)) {
64-
this.logger.debug("Skipping write because " + (result.length() == 0 ? "result is empty" : "current document equals result"));
65-
} else {
66-
WriteCommandAction.runWriteCommandAction(project, () -> {
67-
var finalResult = result;
68-
if (result.indexOf('\r') >= 0) {
69-
// rider always wants \n in files so remove any \r
70-
finalResult = result.replaceAll("\\r", "");
56+
if (csharpierProcess instanceof ICSharpierProcess2) {
57+
var csharpierProcess2 = (ICSharpierProcess2) csharpierProcess;
58+
var parameter = new FormatFileParameter();
59+
parameter.fileContents = currentDocumentText;
60+
parameter.fileName = filePath;
61+
var result = csharpierProcess2.formatFile(parameter);
62+
63+
var end = Instant.now();
64+
this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms");
65+
66+
if (result != null) {
67+
switch (result.status) {
68+
case Formatted -> updateText(document, project, result.formattedFile, currentDocumentText);
69+
case Ignored -> this.logger.info("File is ignored by csharpier cli.");
70+
case Failed -> this.logger.warn("CSharpier cli failed to format the file and returned the following error: " + result.errorMessage);
7171
}
72+
}
73+
}
74+
else {
75+
var result = csharpierProcess.formatFile(currentDocumentText, filePath);
76+
77+
var end = Instant.now();
78+
this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms");
7279

73-
document.replaceString(0, currentDocumentText.length(), finalResult);
74-
});
80+
if (result.length() == 0 || currentDocumentText.equals(result)) {
81+
this.logger.debug("Skipping write because " + (result.length() == 0 ? "result is empty" : "current document equals result"));
82+
} else {
83+
updateText(document, project, result, currentDocumentText);
84+
}
7585
}
7686
}
7787

88+
private static void updateText(@NotNull Document document, @NotNull Project project, String result, String currentDocumentText) {
89+
WriteCommandAction.runWriteCommandAction(project, () -> {
90+
var finalResult = result;
91+
if (result.indexOf('\r') >= 0) {
92+
// rider always wants \n in files so remove any \r
93+
finalResult = result.replaceAll("\\r", "");
94+
}
95+
96+
document.replaceString(0, currentDocumentText.length(), finalResult);
97+
});
98+
}
99+
78100
public boolean getCanFormat(String filePath, Project project) {
79101
var cSharpierProcess = CSharpierProcessProvider.getInstance(project).getProcessFor(filePath);
80102
return !NullCSharpierProcess.Instance.equals(cSharpierProcess);
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
package com.intellij.csharpier;
22

3-
public interface ICSharpierProcess {
4-
public String formatFile(String content, String fileName);
5-
public void dispose();
3+
interface ICSharpierProcess {
4+
String formatFile(String content, String fileName);
5+
void dispose();
6+
}
7+
8+
interface ICSharpierProcess2 extends ICSharpierProcess {
9+
FormatFileResult formatFile(FormatFileParameter parameter);
10+
void dispose();
11+
}
12+
13+
class FormatFileParameter {
14+
public String fileContents;
15+
public String fileName;
16+
}
17+
18+
class FormatFileResult {
19+
public String formattedFile;
20+
public Status status;
21+
public String errorMessage;
22+
}
23+
24+
enum Status
25+
{
26+
Formatted,
27+
Ignored,
28+
Failed
629
}

0 commit comments

Comments
 (0)