-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
186 additions
and
15 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessServer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using CSharpier.VisualStudio; | ||
using Newtonsoft.Json; | ||
|
||
public class CSharpierProcessServer : ICSharpierProcess, IDisposable | ||
{ | ||
private readonly string csharpierPath; | ||
private readonly Logger logger; | ||
private int port; | ||
private Process? process; | ||
public bool ProcessFailedToStart; | ||
|
||
public CSharpierProcessServer(string csharpierPath, Logger logger) | ||
{ | ||
this.logger = logger; | ||
this.csharpierPath = csharpierPath; | ||
this.StartProcess(); | ||
|
||
this.logger.Debug("Warm CSharpier with initial format"); | ||
// warm by formatting a file twice, the 3rd time is when it gets really fast | ||
this.FormatFile("public class ClassName { }", "/Temp/Test.cs"); | ||
this.FormatFile("public class ClassName { }", "/Temp/Test.cs"); | ||
} | ||
|
||
private void StartProcess() | ||
{ | ||
try | ||
{ | ||
var processStartInfo = new ProcessStartInfo(this.csharpierPath, "--server") | ||
{ | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
Environment = { ["DOTNET_NOLOGO"] = "1" } | ||
}; | ||
this.process = Process.Start(processStartInfo); | ||
|
||
var output = string.Empty; | ||
|
||
var task = Task.Run(() => | ||
{ | ||
output = this.process!.StandardOutput.ReadLine(); | ||
}); | ||
|
||
if (!task.Wait(TimeSpan.FromSeconds(2))) | ||
{ | ||
this.logger.Warn( | ||
"Spawning the csharpier server timed out. Formatting cannot occur." | ||
); | ||
this.process!.Kill(); | ||
return; | ||
} | ||
|
||
if (this.process!.HasExited) | ||
{ | ||
this.logger.Warn( | ||
"Spawning the csharpier server failed because it exited. " | ||
+ this.process!.StandardError.ReadToEnd() | ||
); | ||
this.ProcessFailedToStart = true; | ||
return; | ||
} | ||
|
||
var portString = output.Replace("Started on ", ""); | ||
this.port = int.Parse(portString); | ||
|
||
this.logger.Debug("Connecting via port " + portString); | ||
} | ||
catch (Exception e) | ||
{ | ||
this.logger.Warn("Failed to spawn the needed csharpier server." + e); | ||
this.ProcessFailedToStart = true; | ||
} | ||
} | ||
|
||
public string FormatFile(string content, string filePath) | ||
{ | ||
if (this.ProcessFailedToStart) | ||
{ | ||
this.logger.Warn("CSharpier process failed to start. Formatting cannot occur."); | ||
return ""; | ||
} | ||
|
||
var data = new FormatFileDto { fileContents = content, fileName = filePath }; | ||
|
||
var url = "http://localhost:" + this.port + "/format"; | ||
|
||
try | ||
{ | ||
var request = (HttpWebRequest)WebRequest.Create(url); | ||
request.Method = "POST"; | ||
request.ContentType = "application/json; charset=utf-8"; | ||
|
||
using (var streamWriter = new StreamWriter(request.GetRequestStream())) | ||
{ | ||
streamWriter.Write(JsonConvert.SerializeObject(data)); | ||
} | ||
|
||
var response = (HttpWebResponse)request.GetResponse(); | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) | ||
{ | ||
response.Close(); | ||
return ""; | ||
} | ||
|
||
using (var streamReader = new StreamReader(response.GetResponseStream())) | ||
{ | ||
var result = JsonConvert.DeserializeObject<FormatFileResult>( | ||
streamReader.ReadToEnd() | ||
); | ||
return result.formattedFile ?? ""; | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
this.logger.Warn("Failed posting to the csharpier server. " + e); | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.process?.Dispose(); | ||
} | ||
|
||
private class FormatFileDto | ||
{ | ||
public string fileContents; | ||
public string fileName; | ||
} | ||
|
||
private class FormatFileResult | ||
{ | ||
public string? formattedFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
This extension adds support for [CSharpier](https://github.com/belav/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. | ||
This extension makes use of the dotnet tool [CSharpier](https://github.com/belav/csharpier) to format your code and is versioned independently. | ||
|
||
CSharpier is 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 extension determines which version of csharpier is needed to format a given file by looking for a dotnet manifest file. If one is not found it looks for a globally installed version of CSharpier. | ||
|
||
## Usage | ||
|
||
To use it: | ||
- Install csharpier globally with `dotnet tool install -g csharpier` | ||
- Use the `Reformat with CSharpier` right click context menu action. | ||
- Optionally configure a keyboard shortcut for `EditorContextMenus.CodeWindow.ReformatWithCSharpier` | ||
- Optionally configure `Reformat with CSharpier on Save` under Tools | Options | CSharpier | General | ||
- This option can be configured at the solution level or at the global level. | ||
|
||
Please report any [issues](https://github.com/belav/csharpier/issues) | ||
|
||
### Troubleshooting | ||
CSharpier will log messages and errors to Output | Show output from: CSharpier | ||
Debug logging can be turned on under Tools | Options | CSharpier | Log Debug Messages | ||
CSharpier will log messages and errors to Output | Show output from: CSharpier | ||
|
||
Debug logging can be turned on under Tools | Options | CSharpier | Log Debug Messages | ||
|
||
The extension installs CSharpier to `C:\Users\{CurrentUser}\AppData\Local\CSharpier`. Closing the extension and deleting this folder can fix issues with bad installs. | ||
|
||
Please report any [issues](https://github.com/belav/csharpier/issues) |