Skip to content

Commit

Permalink
Rider changes needed to support #1191
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Mar 12, 2024
1 parent bdba345 commit d674c30
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import com.google.gson.Gson;
import com.intellij.openapi.project.Project;

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

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

var data = new FormatFileDto();
data.fileContents = content;
data.fileName = filePath;

var url = "http://localhost:" + this.port + "/format";


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

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

connection.setConnectTimeout(2000);
connection.setDoOutput(true);
connection.setDoInput(true);

var outputStream = connection.getOutputStream();
var writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(this.gson.toJson(data));
writer.write(this.gson.toJson(parameter));
writer.flush();
writer.close();
outputStream.close();

var responseCode = connection.getResponseCode();
if (responseCode != 200) {
this.logger.warn("Csharpier server returned non-200 status code of " + responseCode);
connection.disconnect();
return "";
return null;
}

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

connection.disconnect();

return result.formattedFile != null ? result.formattedFile : "";
return result;

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

return "";
return null;
}

@Override
public String formatFile(String content, String fileName) {
var parameter = new FormatFileParameter();
parameter.fileName = fileName;
parameter.fileContents = content;

var result = this.formatFile(parameter);
return result == null ? null : result.formattedFile;
}

@Override
Expand All @@ -132,13 +141,4 @@ public void dispose() {
this.process.destroy();
}
}

private class FormatFileDto {
public String fileContents;
public String fileName;
}

private class FormatFileResult {
public String formattedFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ static DotNetProvider getInstance(@NotNull Project project) {
void initialize() {
var foundDotNet = this.findDotNet();
if (!foundDotNet) {

var title = "CSharpier unable to run dotnet commands";
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.";
var notification = NotificationGroupManager.getInstance().getNotificationGroup("CSharpier").createNotification(title, message, NotificationType.WARNING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

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

var currentDocumentText = document.getText();

var cSharpierProcessProvider = CSharpierProcessProvider.getInstance(project);
var csharpierProcess = CSharpierProcessProvider.getInstance(project).getProcessFor(filePath);
this.logger.info("Formatting started for " + filePath + ".");
var start = Instant.now();
var result = cSharpierProcessProvider.getProcessFor(filePath).formatFile(currentDocumentText, filePath);

var end = Instant.now();
this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms");

if (result.length() == 0 || currentDocumentText.equals(result)) {
this.logger.debug("Skipping write because " + (result.length() == 0 ? "result is empty" : "current document equals result"));
} else {
WriteCommandAction.runWriteCommandAction(project, () -> {
var finalResult = result;
if (result.indexOf('\r') >= 0) {
// rider always wants \n in files so remove any \r
finalResult = result.replaceAll("\\r", "");
if (csharpierProcess instanceof ICSharpierProcess2) {
var csharpierProcess2 = (ICSharpierProcess2) csharpierProcess;
var parameter = new FormatFileParameter();
parameter.fileContents = currentDocumentText;
parameter.fileName = filePath;
var result = csharpierProcess2.formatFile(parameter);

var end = Instant.now();
this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms");

if (result != null) {
switch (result.status) {
case Formatted -> updateText(document, project, result.formattedFile, currentDocumentText);
case Ignored -> this.logger.info("File is ignored by csharpier cli.");
case Failed -> this.logger.warn("CSharpier cli failed to format the file and returned the following error: " + result.errorMessage);
}
}
}
else {
var result = csharpierProcess.formatFile(currentDocumentText, filePath);

var end = Instant.now();
this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms");

document.replaceString(0, currentDocumentText.length(), finalResult);
});
if (result.length() == 0 || currentDocumentText.equals(result)) {
this.logger.debug("Skipping write because " + (result.length() == 0 ? "result is empty" : "current document equals result"));
} else {
updateText(document, project, result, currentDocumentText);
}
}
}

private static void updateText(@NotNull Document document, @NotNull Project project, String result, String currentDocumentText) {
WriteCommandAction.runWriteCommandAction(project, () -> {
var finalResult = result;
if (result.indexOf('\r') >= 0) {
// rider always wants \n in files so remove any \r
finalResult = result.replaceAll("\\r", "");
}

document.replaceString(0, currentDocumentText.length(), finalResult);
});
}

public boolean getCanFormat(String filePath, Project project) {
var cSharpierProcess = CSharpierProcessProvider.getInstance(project).getProcessFor(filePath);
return !NullCSharpierProcess.Instance.equals(cSharpierProcess);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package com.intellij.csharpier;

public interface ICSharpierProcess {
public String formatFile(String content, String fileName);
public void dispose();
interface ICSharpierProcess {
String formatFile(String content, String fileName);
void dispose();
}

interface ICSharpierProcess2 extends ICSharpierProcess {
FormatFileResult formatFile(FormatFileParameter parameter);
void dispose();
}

class FormatFileParameter {
public String fileContents;
public String fileName;
}

class FormatFileResult {
public String formattedFile;
public Status status;
public String errorMessage;
}

enum Status
{
Formatted,
Ignored,
Failed
}

0 comments on commit d674c30

Please sign in to comment.