diff --git a/Src/CSharpier.Rider/CHANGELOG.md b/Src/CSharpier.Rider/CHANGELOG.md index 819cf39be..cdf64d05e 100644 --- a/Src/CSharpier.Rider/CHANGELOG.md +++ b/Src/CSharpier.Rider/CHANGELOG.md @@ -2,6 +2,10 @@ # csharpier-rider Changelog +## [1.7.4] +- Only use CSharpier Server for 0.29.0+ +- Add option to bypass csharpier server. + ## [1.7.3] - Fix issue with csharpier server not working when localhost resolved to IPv6 ::1 diff --git a/Src/CSharpier.Rider/gradle.properties b/Src/CSharpier.Rider/gradle.properties index 99a7d1edd..764d2c6c6 100644 --- a/Src/CSharpier.Rider/gradle.properties +++ b/Src/CSharpier.Rider/gradle.properties @@ -3,7 +3,7 @@ pluginGroup = com.intellij.csharpier pluginName = csharpier -pluginVersion = 1.7.3 +pluginVersion = 1.7.4 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. diff --git a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessProvider.java b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessProvider.java index 68dc40d23..54453ced0 100644 --- a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessProvider.java +++ b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessProvider.java @@ -219,16 +219,26 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version // but there are still 0.12 and 0.14 downloads happening var installedVersion = version.split("\\."); var versionWeCareAbout = Integer.parseInt(installedVersion[1]); + var serverVersion = 29; ICSharpierProcess csharpierProcess; - if (versionWeCareAbout >= 28) { + if (versionWeCareAbout >= serverVersion + && !CSharpierSettings.getInstance(this.project).getDisableCSharpierServer() + ) { csharpierProcess = new CSharpierProcessServer(customPath, version, this.project); } else if (versionWeCareAbout >= 12) { var useUtf8 = versionWeCareAbout >= 14; + if (versionWeCareAbout >= serverVersion + && CSharpierSettings.getInstance(this.project).getDisableCSharpierServer() + ) { + this.logger.debug( + "CSharpier server is disabled, falling back to piping via stdin" + ); + } + csharpierProcess = new CSharpierProcessPipeMultipleFiles(customPath, useUtf8, version, this.project); - } - else { + } else { if (!this.warnedForOldVersion) { var content = "Please upgrade to CSharpier >= 0.12.0 for bug fixes and improved formatting speed."; NotificationGroupManager.getInstance().getNotificationGroup("CSharpier") @@ -238,7 +248,7 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version this.warnedForOldVersion = true; } - csharpierProcess = new CSharpierProcessSingleFile(customPath, version, this.project); + csharpierProcess = new CSharpierProcessSingleFile(customPath, version, this.project); } if (csharpierProcess.getProcessFailedToStart()) { diff --git a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessServer.java b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessServer.java index a4c1e0dfb..04ed4471e 100644 --- a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessServer.java +++ b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierProcessServer.java @@ -27,7 +27,11 @@ public CSharpierProcessServer(String csharpierPath, String version, Project proj this.csharpierPath = csharpierPath; this.dotNetProvider = DotNetProvider.getInstance(project); this.version = version; - this.startProcess(); + + if (!this.startProcess()) { + this.processFailedToStart = true; + return; + } this.logger.debug("Warm CSharpier with initial format"); // warm by formatting a file twice, the 3rd time is when it gets really fast @@ -35,7 +39,7 @@ public CSharpierProcessServer(String csharpierPath, String version, Project proj this.formatFile("public class ClassName { }", "/Temp/Test.cs"); } - private void startProcess() { + private boolean startProcess() { try { var processBuilder = new ProcessBuilder(this.csharpierPath, "--server"); processBuilder.redirectErrorStream(true); @@ -54,23 +58,23 @@ private void startProcess() { } catch (TimeoutException e) { this.logger.warn("Spawning the csharpier server timed out. Formatting cannot occur."); this.process.destroy(); - return; + return false; } if (!this.process.isAlive()) { this.logger.warn("Spawning the csharpier server failed because it exited. " + output); - this.processFailedToStart = true; - return; + return false; } var portString = output.replace("Started on ", ""); this.port = Integer.parseInt(portString); this.logger.debug("Connecting via port " + portString); + return true; } catch (Exception e) { this.logger.warn("Failed to spawn the needed csharpier server.", e); - this.processFailedToStart = true; + return false; } } diff --git a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettings.java b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettings.java index 0f6ec61fc..a339250ab 100644 --- a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettings.java +++ b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettings.java @@ -25,8 +25,8 @@ public boolean getRunOnSave() { return this.runOnSave; } - public void setRunOnSave(boolean runOnSave) { - this.runOnSave = runOnSave; + public void setRunOnSave(boolean value) { + this.runOnSave = value; } private String customPath; @@ -35,8 +35,18 @@ public String getCustomPath() { return this.customPath; } - public void setCustomPath(String customPath) { - this.customPath = customPath; + public void setCustomPath(String value) { + this.customPath = value; + } + + private boolean disableCSharpierServer; + + public boolean getDisableCSharpierServer() { + return this.disableCSharpierServer; + } + + public void setDisableCSharpierServer(boolean value) { + this.disableCSharpierServer = value; } @Override diff --git a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettingsComponent.java b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettingsComponent.java index 770431a46..fc9bf1f2e 100644 --- a/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettingsComponent.java +++ b/Src/CSharpier.Rider/src/main/java/com/intellij/csharpier/CSharpierSettingsComponent.java @@ -19,6 +19,7 @@ public class CSharpierSettingsComponent implements SearchableConfigurable { private final Project project; private JBCheckBox runOnSaveCheckBox = new JBCheckBox("Run on Save"); + private JBCheckBox disableCSharpierServerCheckBox = new JBCheckBox("Disable CSharpier Server"); private JBTextField customPathTextField = new JBTextField(); public CSharpierSettingsComponent(@NotNull Project project) { @@ -64,9 +65,6 @@ private JComponent createSectionHeader(String label) { var leftIndent = 20; var topInset = 10; - - - return FormBuilder.createFormBuilder() .addComponent(createSectionHeader("General Settings")) .setFormLeftIndent(leftIndent) @@ -75,6 +73,7 @@ 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.disableCSharpierServerCheckBox, topInset) .addComponentFillVertically(new JPanel(), 0) .getPanel(); } @@ -82,7 +81,8 @@ private JComponent createSectionHeader(String label) { @Override public boolean isModified() { return CSharpierSettings.getInstance(this.project).getRunOnSave() != this.runOnSaveCheckBox.isSelected() - || CSharpierSettings.getInstance(this.project).getCustomPath() != this.customPathTextField.getText(); + || CSharpierSettings.getInstance(this.project).getCustomPath() != this.customPathTextField.getText() + || CSharpierSettings.getInstance(this.project).getDisableCSharpierServer() != this.disableCSharpierServerCheckBox.isSelected(); } @Override @@ -91,6 +91,7 @@ public void apply() { settings.setRunOnSave(this.runOnSaveCheckBox.isSelected()); settings.setCustomPath(this.customPathTextField.getText()); + settings.setDisableCSharpierServer(this.disableCSharpierServerCheckBox.isSelected()); } @Override @@ -98,5 +99,6 @@ public void reset() { var settings = CSharpierSettings.getInstance(this.project); this.runOnSaveCheckBox.setSelected(settings.getRunOnSave()); this.customPathTextField.setText(settings.getCustomPath()); + this.disableCSharpierServerCheckBox.setSelected(settings.getDisableCSharpierServer()); } } diff --git a/Src/CSharpier.VSCode/CHANGELOG.md b/Src/CSharpier.VSCode/CHANGELOG.md index 6a7498e1e..e90d327b7 100644 --- a/Src/CSharpier.VSCode/CHANGELOG.md +++ b/Src/CSharpier.VSCode/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.7.3] +- Only use CSharpier Server on 0.29.0+ +- Add option to bypass csharpier server. + ## [1.7.2] - Fix issue with csharpier server not working when localhost resolved to IPv6 ::1 diff --git a/Src/CSharpier.VSCode/package.json b/Src/CSharpier.VSCode/package.json index 0082bb63c..e7c7efc25 100644 --- a/Src/CSharpier.VSCode/package.json +++ b/Src/CSharpier.VSCode/package.json @@ -2,7 +2,7 @@ "name": "csharpier-vscode", "displayName": "CSharpier - Code formatter", "description": "Code formatter using csharpier", - "version": "1.7.2", + "version": "1.7.3", "publisher": "csharpier", "author": "CSharpier", "homepage": "https://marketplace.visualstudio.com/items?itemName=csharpier.csharpier-vscode", @@ -52,6 +52,11 @@ "type": "string", "default": "", "description": "Path to directory containing dotnet-csharpier - used for testing the extension with new versions of csharpier." + }, + "csharpier.dev.disableCSharpierServer": { + "type": "boolean", + "default": false, + "description": "Disable CSharpier Server." } } } diff --git a/Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts b/Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts index 6861d360e..953c34b75 100644 --- a/Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts +++ b/Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts @@ -25,6 +25,7 @@ export class CSharpierProcessProvider implements Disposable { string, ICSharpierProcess | ICSharpierProcess2 | undefined > = {}; + disableCSharpierServer: boolean; constructor(logger: Logger, extension: Extension) { this.logger = logger; @@ -35,6 +36,9 @@ export class CSharpierProcessProvider implements Disposable { extension, ); + this.disableCSharpierServer = + workspace.getConfiguration("csharpier").get("dev.disableCSharpierServer") ?? false; + window.onDidChangeActiveTextEditor((event: TextEditor | undefined) => { if (event?.document?.languageId !== "csharp") { return; @@ -226,7 +230,9 @@ export class CSharpierProcessProvider implements Disposable { let csharpierProcess: ICSharpierProcess; - if (semver.gte(version, "0.28.0")) { + const serverVersion = "0.29.0"; + + if (semver.gte(version, serverVersion) && !this.disableCSharpierServer) { csharpierProcess = new CSharpierProcessServer( this.logger, customPath, @@ -234,6 +240,16 @@ export class CSharpierProcessProvider implements Disposable { version, ); } else if (semver.gte(version, "0.12.0")) { + if ( + semver.gte(version, serverVersion) + && this.disableCSharpierServer + ) + { + this.logger.debug( + "CSharpier server is disabled, falling back to piping via stdin" + ); + } + csharpierProcess = new CSharpierProcessPipeMultipleFiles( this.logger, customPath, diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/CSharpier.VisualStudio.csproj b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/CSharpier.VisualStudio.csproj index b8db03ef1..af06b7964 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/CSharpier.VisualStudio.csproj +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/CSharpier.VisualStudio.csproj @@ -28,6 +28,7 @@ $(DevEnvDir)devenv.exe /rootsuffix Exp false + false true diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest index 69b7eb991..f24d851f1 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + CSharpier CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules. https://github.com/belav/csharpier diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/CSharpier.VisualStudio2019.csproj b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/CSharpier.VisualStudio2019.csproj index 5dcc6adab..ff49257c7 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/CSharpier.VisualStudio2019.csproj +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/CSharpier.VisualStudio2019.csproj @@ -28,6 +28,7 @@ $(DevEnvDir)devenv.exe /rootsuffix Exp false + false true diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest index 4be956029..87675f36f 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + CSharpier 2019 CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules. https://github.com/belav/csharpier diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessProvider.cs b/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessProvider.cs index a45760d5f..7cdd737e9 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessProvider.cs +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessProvider.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Net; using System.Security.Policy; +using System.Text.RegularExpressions; using System.Windows.Forms; using System.Xml; using Microsoft.VisualStudio.Imaging; @@ -214,9 +215,9 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version var customPath = this.customPathInstaller.GetPathForVersion(version); this.logger.Debug("Adding new version " + version + " process for " + directory); - var installedVersion = GetInstalledVersion(version); + var installedVersion = this.GetInstalledVersion(version); var pipeFilesVersion = new Version("0.12.0"); - var serverVersion = new Version("0.28.0"); + var serverVersion = new Version("0.29.0"); ICSharpierProcess cSharpierProcess; if ( diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessServer.cs b/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessServer.cs index 108f94ade..bd9b34abc 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessServer.cs +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessServer.cs @@ -16,7 +16,7 @@ public class CSharpierProcessServer : ICSharpierProcess2, IDisposable private readonly Logger logger; private int port; private Process? process; - public bool ProcessFailedToStart { get; private set; } + public bool ProcessFailedToStart { get; } public string Version { get; } @@ -26,7 +26,11 @@ public CSharpierProcessServer(string csharpierPath, string version, Logger logge this.csharpierPath = csharpierPath; this.Version = version; - this.StartProcess(); + if (!this.StartProcess()) + { + this.ProcessFailedToStart = true; + return; + } this.logger.Debug("Warm CSharpier with initial format"); // warm by formatting a file twice, the 3rd time is when it gets really fast @@ -34,31 +38,13 @@ public CSharpierProcessServer(string csharpierPath, string version, Logger logge this.FormatFile("public class ClassName { }", "/Temp/Test.cs"); } - private void StartProcess() - { - if (this.ActuallyStartProcess()) - { - return; - } - - var portToUse = this.FindFreePort(); - if (!this.ActuallyStartProcess(portToUse)) - { - this.ProcessFailedToStart = true; - } - } - - private bool ActuallyStartProcess(int portToUse = -1) + private bool StartProcess() { try { - var arguments = "--server"; - if (portToUse > 0) - { - arguments += " --server-port " + portToUse; - } + const string arguments = "--server"; - this.logger.Debug("Running " + this.csharpierPath + " " + arguments); + this.logger.Debug("Running " + this.csharpierPath + " --server"); var processStartInfo = new ProcessStartInfo(this.csharpierPath, arguments) { @@ -110,40 +96,6 @@ private bool ActuallyStartProcess(int portToUse = -1) } } - private int FindFreePort() - { - this.logger.Debug("Trying to find free port in extension"); - const int startPort = 49152; - const int endPort = 65535; - var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); - var tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); - var ipEndPoint = ipGlobalProperties.GetActiveTcpListeners(); - - var usedPorts = ipEndPoint - .Where(o => o.Port >= startPort) - .Select(o => o.Port) - .Concat( - tcpConnInfoArray - .Where(o => o.LocalEndPoint.Port >= startPort) - .Select(o => o.LocalEndPoint.Port) - ) - .ToHashSet(); - - this.logger.Debug($"Found {usedPorts.Count} used ports that could conflict"); - - for (var i = startPort; i < endPort; i++) - { - if (!usedPorts.Contains(i)) - { - return i; - } - } - - throw new InvalidOperationException( - $"Could not find any free TCP port between ports {startPort}-{endPort}" - ); - } - public string FormatFile(string content, string filePath) { var parameter = new FormatFileParameter { fileName = filePath, fileContents = content }; diff --git a/Src/CSharpier.VisualStudio/ChangeLog.md b/Src/CSharpier.VisualStudio/ChangeLog.md index aeb8ca154..c9ed5eee6 100644 --- a/Src/CSharpier.VisualStudio/ChangeLog.md +++ b/Src/CSharpier.VisualStudio/ChangeLog.md @@ -1,4 +1,8 @@ -## [1.7.3] +## [1.7.4] +- Support for semver +- Only use CSharpier Server on 0.29.0+ + +## [1.7.3] - If CSharpier doesn't respond when trying to find a port, then try to find a port in extension - Add option to bypass csharpier server.