Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making csharpier server official for VS. #1234

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.5.2" Language="en-US" Publisher="CSharpier" />
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.7.0" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.5.2" Language="en-US" Publisher="CSharpier" />
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.7.0" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier 2019</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,11 @@ public class CSharpierOptions
)]
public string? CustomPath { get; set; }

[Category("CSharpier - Developer")]
[DisplayName("Use CSharpier Server")]
[Description("Use http communication to csharpier - Experimental as of 0.27.2")]
public bool UseServer { get; set; }

protected void LoadFrom(CSharpierOptions newInstance)
{
this.SolutionRunOnSave = newInstance.SolutionRunOnSave;
this.GlobalRunOnSave = newInstance.GlobalRunOnSave;
this.GlobalLogDebugMessages = newInstance.GlobalLogDebugMessages;
this.UseServer = newInstance.UseServer;
this.CustomPath = newInstance.CustomPath;
}

Expand Down Expand Up @@ -124,7 +118,6 @@ await LoadOptionsFromFile(
newInstance.GlobalRunOnSave = o.RunOnSave;
newInstance.GlobalLogDebugMessages = o.LogDebugMessages;
newInstance.CustomPath = o.CustomPath;
newInstance.UseServer = o.UseServer;
}
);

Expand Down Expand Up @@ -179,7 +172,6 @@ await SaveOptions(
RunOnSave = this.GlobalRunOnSave,
LogDebugMessages = this.GlobalLogDebugMessages,
CustomPath = this.CustomPath,
UseServer = this.UseServer
}
);
}
Expand Down Expand Up @@ -214,7 +206,6 @@ private class OptionsDto
public bool? RunOnSave { get; set; }
public bool LogDebugMessages { get; set; }
public string? CustomPath { get; set; }
public bool UseServer { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Text.Editor.OptionsExtensionMethods;
using Process = System.Diagnostics.Process;

namespace CSharpier.VisualStudio
Expand All @@ -16,10 +14,18 @@ public class CSharpierProcessPipeMultipleFiles : ICSharpierProcess
private readonly string csharpierPath;
private StreamWriter standardIn;

public CSharpierProcessPipeMultipleFiles(string csharpierPath, Logger logger)
public string Version { get; }
public bool ProcessFailedToStart { get; private set; }

public CSharpierProcessPipeMultipleFiles(
string csharpierPath,
string version,
Logger logger
)
{
this.logger = logger;
this.csharpierPath = csharpierPath;
this.Version = version;

this.StartProcess();

Expand All @@ -29,8 +35,6 @@ public CSharpierProcessPipeMultipleFiles(string csharpierPath, Logger logger)
this.FormatFile("public class ClassName { }", "Test.cs");
}

public bool ProcessFailedToStart { get; set; }

private void StartProcess()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,22 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version

var installedVersion = new Version(version);
var pipeFilesVersion = new Version("0.12.0");
if (CSharpierOptions.Instance.UseServer)
var serverVersion = new Version("0.28.0");
ICSharpierProcess cSharpierProcess;

if (installedVersion.CompareTo(serverVersion) >= 0)
{
cSharpierProcess = new CSharpierProcessServer(customPath, version, this.logger);
}
else if (installedVersion.CompareTo(pipeFilesVersion) >= 0)
{
return new CSharpierProcessServer(customPath, this.logger);
cSharpierProcess = new CSharpierProcessPipeMultipleFiles(
customPath,
version,
this.logger
);
}
if (installedVersion.CompareTo(pipeFilesVersion) < 0)
else
{
if (!this.warnedForOldVersion)
{
Expand All @@ -231,18 +242,19 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
this.warnedForOldVersion = true;
}

return new CSharpierProcessSingleFile(customPath, this.logger);
cSharpierProcess = new CSharpierProcessSingleFile(
customPath,
version,
this.logger
);
}
var csharpierProcess = new CSharpierProcessPipeMultipleFiles(
customPath,
this.logger
);
if (csharpierProcess.ProcessFailedToStart)

if (cSharpierProcess.ProcessFailedToStart)
{
this.DisplayFailureMessage();
}

return csharpierProcess;
return cSharpierProcess;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using CSharpier.VisualStudio;
using Newtonsoft.Json;

public class CSharpierProcessServer : ICSharpierProcess, IDisposable
public class CSharpierProcessServer : ICSharpierProcess2, IDisposable
{
private readonly string csharpierPath;
private readonly Logger logger;
private int port;
private Process? process;
public bool ProcessFailedToStart;
public bool ProcessFailedToStart { get; private set; }

public CSharpierProcessServer(string csharpierPath, Logger logger)
public string Version { get; }

public CSharpierProcessServer(string csharpierPath, string version, Logger logger)
{
this.logger = logger;
this.csharpierPath = csharpierPath;
this.Version = version;

this.StartProcess();

this.logger.Debug("Warm CSharpier with initial format");
Expand Down Expand Up @@ -79,15 +84,21 @@ private void StartProcess()
}

public string FormatFile(string content, string filePath)
{
var parameter = new FormatFileParameter { fileName = filePath, fileContents = content };

var result = this.formatFile(parameter);
return result?.formattedFile ?? string.Empty;
}

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 { fileContents = content, fileName = filePath };

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

try
Expand All @@ -98,46 +109,38 @@ public string FormatFile(string content, string filePath)

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(JsonConvert.SerializeObject(data));
streamWriter.Write(JsonConvert.SerializeObject(parameter));
}

var response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode != HttpStatusCode.OK)
{
this.logger.Warn(
"Csharpier server returned non-200 status code of " + response.StatusCode
);
response.Close();
return "";
return null;
}

using (var streamReader = new StreamReader(response.GetResponseStream()))
using (var streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
var result = JsonConvert.DeserializeObject<FormatFileResult>(
streamReader.ReadToEnd()
);
return result.formattedFile ?? "";
return result;
}
}
catch (Exception e)
{
this.logger.Warn("Failed posting to the csharpier server. " + e);
}

return "";
return null;
}

public void Dispose()
{
this.process?.Dispose();
}

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

private class FormatFileResult
{
public string? formattedFile;
this.process?.Kill();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ public class CSharpierProcessSingleFile : ICSharpierProcess
private readonly string csharpierPath;
private readonly Logger logger;

public CSharpierProcessSingleFile(string csharpierPath, Logger logger)
public string Version { get; }
public bool ProcessFailedToStart => false;

public CSharpierProcessSingleFile(string csharpierPath, string version, Logger logger)
{
this.csharpierPath = csharpierPath;
this.logger = logger;
this.Version = version;
}

public string FormatFile(string content, string fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,79 @@ public void Format(Document document)
var endPoint = textDocument.EndPoint.CreateEditPoint();
var text = startPoint.GetText(endPoint);

this.logger.Info("Formatting started for " + document.FullName + ".");
var stopwatch = Stopwatch.StartNew();
var csharpierProcess = this.cSharpierProcessProvider.GetProcessFor(document.FullName);

var newText = this
.cSharpierProcessProvider.GetProcessFor(document.FullName)
.FormatFile(text, document.FullName);
this.logger.Info(
"Formatting started for "
+ document.FullName
+ " using CSharpier "
+ csharpierProcess.Version
);
var stopwatch = Stopwatch.StartNew();

this.logger.Info("Formatted in " + stopwatch.ElapsedMilliseconds + "ms");
if (string.IsNullOrEmpty(newText) || newText.Equals(text))
void UpdateText(string formattedText)
{
this.logger.Debug(
"Skipping write because "
+ (
string.IsNullOrEmpty(newText)
? "result is empty"
: "current document equals result"
)
startPoint.ReplaceText(
endPoint,
formattedText,
(int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers
);
return;
}

startPoint.ReplaceText(
endPoint,
newText,
(int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers
);
if (csharpierProcess is ICSharpierProcess2 csharpierProcess2)
{
var parameter = new FormatFileParameter
{
fileContents = text,
fileName = document.FullName
};
var result = csharpierProcess2.formatFile(parameter);

this.logger.Info("Formatted in " + stopwatch.ElapsedMilliseconds + "ms");

if (result == null)
{
return;
}

switch (result.status)
{
case Status.Formatted:
UpdateText(result.formattedFile);
break;
case Status.Ignored:
this.logger.Info("File is ignored by csharpier cli.");
break;
case Status.Failed:
this.logger.Warn(
"CSharpier cli failed to format the file and returned the following error: "
+ result.errorMessage
);
break;
}
}
else
{
var result = csharpierProcess.FormatFile(text, document.FullName);

this.logger.Info("Formatted in " + stopwatch.ElapsedMilliseconds + "ms");

if (string.IsNullOrEmpty(result) || result.Equals(text))
{
this.logger.Debug(
"Skipping write because "
+ (
string.IsNullOrEmpty(result)
? "result is empty"
: "current document equals result"
)
);
}
else
{
UpdateText(result);
}
}
}

public bool ProcessSupportsFormatting(Document document) =>
Expand Down
Loading
Loading