Skip to content

Commit 31e0868

Browse files
authored
Making csharpier server official for vs (#1234)
closes #1231 references #1109
1 parent 4282d01 commit 31e0868

File tree

12 files changed

+165
-76
lines changed

12 files changed

+165
-76
lines changed

Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
33
<Metadata>
4-
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.5.2" Language="en-US" Publisher="CSharpier" />
4+
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.7.0" Language="en-US" Publisher="CSharpier" />
55
<DisplayName>CSharpier</DisplayName>
66
<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>
77
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>

Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
33
<Metadata>
4-
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.5.2" Language="en-US" Publisher="CSharpier" />
4+
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.7.0" Language="en-US" Publisher="CSharpier" />
55
<DisplayName>CSharpier 2019</DisplayName>
66
<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>
77
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>

Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierOptions.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,11 @@ public class CSharpierOptions
3939
)]
4040
public string? CustomPath { get; set; }
4141

42-
[Category("CSharpier - Developer")]
43-
[DisplayName("Use CSharpier Server")]
44-
[Description("Use http communication to csharpier - Experimental as of 0.27.2")]
45-
public bool UseServer { get; set; }
46-
4742
protected void LoadFrom(CSharpierOptions newInstance)
4843
{
4944
this.SolutionRunOnSave = newInstance.SolutionRunOnSave;
5045
this.GlobalRunOnSave = newInstance.GlobalRunOnSave;
5146
this.GlobalLogDebugMessages = newInstance.GlobalLogDebugMessages;
52-
this.UseServer = newInstance.UseServer;
5347
this.CustomPath = newInstance.CustomPath;
5448
}
5549

@@ -124,7 +118,6 @@ await LoadOptionsFromFile(
124118
newInstance.GlobalRunOnSave = o.RunOnSave;
125119
newInstance.GlobalLogDebugMessages = o.LogDebugMessages;
126120
newInstance.CustomPath = o.CustomPath;
127-
newInstance.UseServer = o.UseServer;
128121
}
129122
);
130123

@@ -179,7 +172,6 @@ await SaveOptions(
179172
RunOnSave = this.GlobalRunOnSave,
180173
LogDebugMessages = this.GlobalLogDebugMessages,
181174
CustomPath = this.CustomPath,
182-
UseServer = this.UseServer
183175
}
184176
);
185177
}
@@ -214,7 +206,6 @@ private class OptionsDto
214206
public bool? RunOnSave { get; set; }
215207
public bool LogDebugMessages { get; set; }
216208
public string? CustomPath { get; set; }
217-
public bool UseServer { get; set; }
218209
}
219210
}
220211
}

Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessPipeMultipleFiles.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Text;
5-
using System.Threading;
65
using System.Threading.Tasks;
7-
using Microsoft.VisualStudio.Text.Editor.OptionsExtensionMethods;
86
using Process = System.Diagnostics.Process;
97

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

19-
public CSharpierProcessPipeMultipleFiles(string csharpierPath, Logger logger)
17+
public string Version { get; }
18+
public bool ProcessFailedToStart { get; private set; }
19+
20+
public CSharpierProcessPipeMultipleFiles(
21+
string csharpierPath,
22+
string version,
23+
Logger logger
24+
)
2025
{
2126
this.logger = logger;
2227
this.csharpierPath = csharpierPath;
28+
this.Version = version;
2329

2430
this.StartProcess();
2531

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

32-
public bool ProcessFailedToStart { get; set; }
33-
3438
private void StartProcess()
3539
{
3640
try

Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessProvider.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,22 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
217217

218218
var installedVersion = new Version(version);
219219
var pipeFilesVersion = new Version("0.12.0");
220-
if (CSharpierOptions.Instance.UseServer)
220+
var serverVersion = new Version("0.28.0");
221+
ICSharpierProcess cSharpierProcess;
222+
223+
if (installedVersion.CompareTo(serverVersion) >= 0)
224+
{
225+
cSharpierProcess = new CSharpierProcessServer(customPath, version, this.logger);
226+
}
227+
else if (installedVersion.CompareTo(pipeFilesVersion) >= 0)
221228
{
222-
return new CSharpierProcessServer(customPath, this.logger);
229+
cSharpierProcess = new CSharpierProcessPipeMultipleFiles(
230+
customPath,
231+
version,
232+
this.logger
233+
);
223234
}
224-
if (installedVersion.CompareTo(pipeFilesVersion) < 0)
235+
else
225236
{
226237
if (!this.warnedForOldVersion)
227238
{
@@ -231,18 +242,19 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
231242
this.warnedForOldVersion = true;
232243
}
233244

234-
return new CSharpierProcessSingleFile(customPath, this.logger);
245+
cSharpierProcess = new CSharpierProcessSingleFile(
246+
customPath,
247+
version,
248+
this.logger
249+
);
235250
}
236-
var csharpierProcess = new CSharpierProcessPipeMultipleFiles(
237-
customPath,
238-
this.logger
239-
);
240-
if (csharpierProcess.ProcessFailedToStart)
251+
252+
if (cSharpierProcess.ProcessFailedToStart)
241253
{
242254
this.DisplayFailureMessage();
243255
}
244256

245-
return csharpierProcess;
257+
return cSharpierProcess;
246258
}
247259
catch (Exception ex)
248260
{

Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessServer.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Net;
5+
using System.Text;
56
using System.Threading.Tasks;
67
using CSharpier.VisualStudio;
78
using Newtonsoft.Json;
89

9-
public class CSharpierProcessServer : ICSharpierProcess, IDisposable
10+
public class CSharpierProcessServer : ICSharpierProcess2, IDisposable
1011
{
1112
private readonly string csharpierPath;
1213
private readonly Logger logger;
1314
private int port;
1415
private Process? process;
15-
public bool ProcessFailedToStart;
16+
public bool ProcessFailedToStart { get; private set; }
1617

17-
public CSharpierProcessServer(string csharpierPath, Logger logger)
18+
public string Version { get; }
19+
20+
public CSharpierProcessServer(string csharpierPath, string version, Logger logger)
1821
{
1922
this.logger = logger;
2023
this.csharpierPath = csharpierPath;
24+
this.Version = version;
25+
2126
this.StartProcess();
2227

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

8186
public string FormatFile(string content, string filePath)
87+
{
88+
var parameter = new FormatFileParameter { fileName = filePath, fileContents = content };
89+
90+
var result = this.formatFile(parameter);
91+
return result?.formattedFile ?? string.Empty;
92+
}
93+
94+
public FormatFileResult? formatFile(FormatFileParameter parameter)
8295
{
8396
if (this.ProcessFailedToStart)
8497
{
8598
this.logger.Warn("CSharpier process failed to start. Formatting cannot occur.");
86-
return "";
99+
return null;
87100
}
88101

89-
var data = new FormatFileDto { fileContents = content, fileName = filePath };
90-
91102
var url = "http://localhost:" + this.port + "/format";
92103

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

99110
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
100111
{
101-
streamWriter.Write(JsonConvert.SerializeObject(data));
112+
streamWriter.Write(JsonConvert.SerializeObject(parameter));
102113
}
103114

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

106117
if (response.StatusCode != HttpStatusCode.OK)
107118
{
119+
this.logger.Warn(
120+
"Csharpier server returned non-200 status code of " + response.StatusCode
121+
);
108122
response.Close();
109-
return "";
123+
return null;
110124
}
111125

112-
using (var streamReader = new StreamReader(response.GetResponseStream()))
126+
using (var streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
113127
{
114128
var result = JsonConvert.DeserializeObject<FormatFileResult>(
115129
streamReader.ReadToEnd()
116130
);
117-
return result.formattedFile ?? "";
131+
return result;
118132
}
119133
}
120134
catch (Exception e)
121135
{
122136
this.logger.Warn("Failed posting to the csharpier server. " + e);
123137
}
124138

125-
return "";
139+
return null;
126140
}
127141

128142
public void Dispose()
129143
{
130-
this.process?.Dispose();
131-
}
132-
133-
private class FormatFileDto
134-
{
135-
public string fileContents;
136-
public string fileName;
137-
}
138-
139-
private class FormatFileResult
140-
{
141-
public string? formattedFile;
144+
this.process?.Kill();
142145
}
143146
}

Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierProcessSingleFile.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ public class CSharpierProcessSingleFile : ICSharpierProcess
88
private readonly string csharpierPath;
99
private readonly Logger logger;
1010

11-
public CSharpierProcessSingleFile(string csharpierPath, Logger logger)
11+
public string Version { get; }
12+
public bool ProcessFailedToStart => false;
13+
14+
public CSharpierProcessSingleFile(string csharpierPath, string version, Logger logger)
1215
{
1316
this.csharpierPath = csharpierPath;
1417
this.logger = logger;
18+
this.Version = version;
1519
}
1620

1721
public string FormatFile(string content, string fileName)

Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/FormattingService.cs

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,79 @@ public void Format(Document document)
4949
var endPoint = textDocument.EndPoint.CreateEditPoint();
5050
var text = startPoint.GetText(endPoint);
5151

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

55-
var newText = this
56-
.cSharpierProcessProvider.GetProcessFor(document.FullName)
57-
.FormatFile(text, document.FullName);
54+
this.logger.Info(
55+
"Formatting started for "
56+
+ document.FullName
57+
+ " using CSharpier "
58+
+ csharpierProcess.Version
59+
);
60+
var stopwatch = Stopwatch.StartNew();
5861

59-
this.logger.Info("Formatted in " + stopwatch.ElapsedMilliseconds + "ms");
60-
if (string.IsNullOrEmpty(newText) || newText.Equals(text))
62+
void UpdateText(string formattedText)
6163
{
62-
this.logger.Debug(
63-
"Skipping write because "
64-
+ (
65-
string.IsNullOrEmpty(newText)
66-
? "result is empty"
67-
: "current document equals result"
68-
)
64+
startPoint.ReplaceText(
65+
endPoint,
66+
formattedText,
67+
(int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers
6968
);
70-
return;
7169
}
7270

73-
startPoint.ReplaceText(
74-
endPoint,
75-
newText,
76-
(int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers
77-
);
71+
if (csharpierProcess is ICSharpierProcess2 csharpierProcess2)
72+
{
73+
var parameter = new FormatFileParameter
74+
{
75+
fileContents = text,
76+
fileName = document.FullName
77+
};
78+
var result = csharpierProcess2.formatFile(parameter);
79+
80+
this.logger.Info("Formatted in " + stopwatch.ElapsedMilliseconds + "ms");
81+
82+
if (result == null)
83+
{
84+
return;
85+
}
86+
87+
switch (result.status)
88+
{
89+
case Status.Formatted:
90+
UpdateText(result.formattedFile);
91+
break;
92+
case Status.Ignored:
93+
this.logger.Info("File is ignored by csharpier cli.");
94+
break;
95+
case Status.Failed:
96+
this.logger.Warn(
97+
"CSharpier cli failed to format the file and returned the following error: "
98+
+ result.errorMessage
99+
);
100+
break;
101+
}
102+
}
103+
else
104+
{
105+
var result = csharpierProcess.FormatFile(text, document.FullName);
106+
107+
this.logger.Info("Formatted in " + stopwatch.ElapsedMilliseconds + "ms");
108+
109+
if (string.IsNullOrEmpty(result) || result.Equals(text))
110+
{
111+
this.logger.Debug(
112+
"Skipping write because "
113+
+ (
114+
string.IsNullOrEmpty(result)
115+
? "result is empty"
116+
: "current document equals result"
117+
)
118+
);
119+
}
120+
else
121+
{
122+
UpdateText(result);
123+
}
124+
}
78125
}
79126

80127
public bool ProcessSupportsFormatting(Document document) =>

0 commit comments

Comments
 (0)