Skip to content

Commit

Permalink
Rider changes needed to support #1191 & #1109 (#1203)
Browse files Browse the repository at this point in the history
* Rider changes needed to support #1191

* Update CSharpierProcessServer.java

* Always use server when version >= 28

* update readme

* Deal with deprecated warning

closes #1224

* Some cleanup + logging version

* bump version
  • Loading branch information
belav authored Apr 12, 2024
1 parent c70dfa1 commit ea02607
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 75 deletions.
5 changes: 5 additions & 0 deletions Src/CSharpier.Rider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

# csharpier-rider Changelog

## [1.7.0]
- Fix deprecated function warning
- Use CSharpier Http Server for 0.28.0+
- Log version of CSharpier used to format a given file

## [1.6.4]
- Better support for format on save - now works when files are saved via building/running the project.

Expand Down
16 changes: 12 additions & 4 deletions Src/CSharpier.Rider/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# csharpier-rider

<!-- Plugin description -->
This plugin makes use of the dotnet tool [CSharpier](https://github.com/belav/csharpier) to format your code. CSharpier an opinionated code formatter for c#.
It uses Roslyn to parse your code and re-prints it using its own rules.

This plugin makes use of the dotnet tool [CSharpier](https://github.com/belav/csharpier) to format your code. CSharpier an opinionated code formatter for c#.
It uses Roslyn to parse your code and re-prints it using its own rules.
The printing process was ported from [prettier](https://prettier.io/) but has evolved over time.

## CSharpier Version

The plugin determines which version of csharpier is needed to format a give file by looking for a dotnet manifest file. If one is not found it looks for a globally installed version of CSharpier.

### To format files:

- Install csharpier
- as a local tool versioned to your project with `dotnet tool install csharpier`
- globally with `dotnet tool install -g csharpier`
- Use the `Reformat with CSharpier` action.
- Available when right clicking on a file in the editor
- Available via "Search Everywhere"
- Does not have a default keyboard shortcut but can be assigned one.
- Optionally configure CSharpier to `Run on Save` under Preferences/Settings | Tools | CSharpier

Please report any [issues](https://github.com/belav/csharpier/issues)

## Installation

- Using IDE built-in plugin system:

<kbd>Settings/Preferences</kbd> > <kbd>Plugins</kbd> > <kbd>Marketplace</kbd> > <kbd>Search for "CSharpier"</kbd> >
<kbd>Install Plugin</kbd>

---

## Troubleshooting
Expand All @@ -32,13 +39,14 @@ See [Editor Troubleshooting](https://csharpier.com/docs/EditorsTroubleshooting)
**Note** This plugin does not do any formatting and is versioned separately from CSharpier.

### Viewing CSharpier Logs

- Use the action "Show Log in Explorer"
- Look for entries for "CSharpierLogger"

### Enable Debug Logging for CSharpier

- Use the action "Debug Log Settings"
- 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 @@ -3,7 +3,7 @@

pluginGroup = com.intellij.csharpier
pluginName = csharpier
pluginVersion = 1.6.4
pluginVersion = 1.7.0

# 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 @@ -11,17 +11,19 @@ public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Dis
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;
public boolean processFailedToStart;

public CSharpierProcessPipeMultipleFiles(String csharpierPath, boolean useUtf8, Project project) {
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");
Expand Down Expand Up @@ -51,6 +53,11 @@ private void startProcess() {
}
}

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

@Override
public String formatFile(String content, String filePath) {
if (this.processFailedToStart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version
var installedVersion = version.split("\\.");
var versionWeCareAbout = Integer.parseInt(installedVersion[1]);

if (CSharpierSettings.getInstance(this.project).getUseServer()) {
return new CSharpierProcessServer(customPath, this.project);
if (versionWeCareAbout >= 28) {
return new CSharpierProcessServer(customPath, version, this.project);
}

if (versionWeCareAbout < 12) {
Expand All @@ -235,12 +235,12 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version
}


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

var useUtf8 = versionWeCareAbout >= 14;

var csharpierProcess = new CSharpierProcessPipeMultipleFiles(customPath, useUtf8, this.project);
var csharpierProcess = new CSharpierProcessPipeMultipleFiles(customPath, useUtf8, version, this.project);
if (csharpierProcess.processFailedToStart) {
this.displayFailureMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
import com.google.gson.Gson;
import com.intellij.openapi.project.Project;

public class CSharpierProcessServer implements ICSharpierProcess, Disposable {
public class CSharpierProcessServer implements ICSharpierProcess2, Disposable {
private final Gson gson = new Gson();
private final String csharpierPath;
private final DotNetProvider dotNetProvider;
private final String version;
private Logger logger = CSharpierLogger.getInstance();
private int port;
private Process process = null;
public boolean processFailedToStart;

public CSharpierProcessServer(String csharpierPath, Project project) {
public CSharpierProcessServer(String csharpierPath, String version, Project project) {
this.csharpierPath = csharpierPath;
this.dotNetProvider = DotNetProvider.getInstance(project);
this.version = version;
this.startProcess();

this.logger.debug("Warm CSharpier with initial format");
Expand Down Expand Up @@ -73,16 +75,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 +93,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,28 +117,34 @@ 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 void dispose() {
if (this.process != null) {
this.process.destroy();
}
public String getVersion() {
return this.version;
}

private class FormatFileDto {
public String fileContents;
public String fileName;
@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;
}

private class FormatFileResult {
public String formattedFile;
@Override
public void dispose() {
if (this.process != null) {
this.process.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ public class CSharpierProcessSingleFile implements ICSharpierProcess {
private final DotNetProvider dotNetProvider;
private final Logger logger = CSharpierLogger.getInstance();
private final String csharpierPath;
private final String version;

public CSharpierProcessSingleFile(String csharpierPath, Project project) {
public CSharpierProcessSingleFile(String csharpierPath, String version, Project project) {
this.csharpierPath = csharpierPath;
this.dotNetProvider = DotNetProvider.getInstance(project);
this.version = version;
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ public void setCustomPath(String customPath) {
this.customPath = customPath;
}

private boolean useServer;

public boolean getUseServer() {
return this.useServer;
}

public void setUseServer(boolean useServer) {
this.useServer = useServer;
}

@Override
public CSharpierSettings getState() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public class CSharpierSettingsComponent implements SearchableConfigurable {
private final Project project;
private JBCheckBox runOnSaveCheckBox = new JBCheckBox("Run on Save");
private JBCheckBox useServerCheckBox = new JBCheckBox("Use CSharpier Server - experimental support as of 0.27.2");
private JBTextField customPathTextField = new JBTextField();

public CSharpierSettingsComponent(@NotNull Project project) {
Expand Down Expand Up @@ -76,32 +75,28 @@ private JComponent createSectionHeader(String label) {
.addComponent(createSectionHeader("Developer Settings"), 20)
.setFormLeftIndent(leftIndent)
.addLabeledComponent(new JBLabel("Directory of custom dotnet-csharpier:"), this.customPathTextField, topInset, false)
.addComponent(this.useServerCheckBox, topInset)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

@Override
public boolean isModified() {
return CSharpierSettings.getInstance(this.project).getRunOnSave() != this.runOnSaveCheckBox.isSelected()
|| CSharpierSettings.getInstance(this.project).getUseServer() != this.useServerCheckBox.isSelected()
|| CSharpierSettings.getInstance(this.project).getCustomPath() != this.customPathTextField.getText();
}

@Override
public void apply() throws ConfigurationException {
public void apply() {
var settings = CSharpierSettings.getInstance(this.project);

settings.setRunOnSave(this.runOnSaveCheckBox.isSelected());
settings.setCustomPath(this.customPathTextField.getText());
settings.setUseServer(this.useServerCheckBox.isSelected());
}

@Override
public void reset() {
var settings = CSharpierSettings.getInstance(this.project);
this.runOnSaveCheckBox.setSelected(settings.getRunOnSave());
this.useServerCheckBox.setSelected(settings.getUseServer());
this.customPathTextField.setText(settings.getCustomPath());
}
}
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
Loading

0 comments on commit ea02607

Please sign in to comment.