Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 7570f07

Browse files
committed
Merge pull request #40 from jaredpar/small-fixes
Couple of small fixes
2 parents d24b9ee + 23e3dca commit 7570f07

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

src/CodeFormatter/Program.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
67
using System.IO;
78
using System.Linq;
89
using System.Threading;
@@ -32,13 +33,15 @@ private static int Main(string[] args)
3233
return -1;
3334
}
3435

35-
List<string> ruleTypes = new List<string>();
36-
List<string> filenames = new List<string>();
36+
var ruleTypes = new List<string>();
37+
var filenames = new List<string>();
38+
var disableCopyright = false;
39+
var comparer = StringComparer.OrdinalIgnoreCase;
3740

3841
for (int i = 1; i < args.Length; i++)
3942
{
4043
string arg = args[i];
41-
if (arg.Equals("/file", StringComparison.InvariantCultureIgnoreCase))
44+
if (comparer.Equals(arg, "/file"))
4245
{
4346
if (i + 1 < args.Length)
4447
{
@@ -47,28 +50,35 @@ private static int Main(string[] args)
4750
i++;
4851
}
4952
}
53+
else if (comparer.Equals(arg, "/nocopyright"))
54+
{
55+
disableCopyright = true;
56+
}
5057
else
5158
{
5259
ruleTypes.Add(arg);
5360
}
5461
}
5562

56-
5763
var cts = new CancellationTokenSource();
5864
var ct = cts.Token;
5965

6066
Console.CancelKeyPress += delegate { cts.Cancel(); };
6167

62-
RunAsync(projectOrSolutionPath, ruleTypes, filenames, ct).Wait(ct);
68+
RunAsync(projectOrSolutionPath, ruleTypes, filenames, disableCopyright, ct).Wait(ct);
6369
Console.WriteLine("Completed formatting.");
6470
return 0;
6571
}
6672

67-
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, CancellationToken cancellationToken)
73+
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, bool disableCopright, CancellationToken cancellationToken)
6874
{
6975
var workspace = MSBuildWorkspace.Create();
7076
var engine = FormattingEngine.Create(ruleTypes, filenames);
7177
engine.Verbose = true;
78+
if (disableCopright)
79+
{
80+
engine.CopyrightHeader = ImmutableArray<string>.Empty;
81+
}
7282

7383
string extension = Path.GetExtension(projectOrSolutionPath);
7484
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/PrivateFieldNamingRuleTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,36 @@ class C3
113113

114114
Verify(text, expected, runFormatter: true);
115115
}
116+
117+
/// <summary>
118+
/// If the name is pascal cased make it camel cased during the rewrite. If it is not
119+
/// pascal cased then do not change the casing.
120+
/// </summary>
121+
[Fact]
122+
public void NameCasingField()
123+
{
124+
var text = @"
125+
class C
126+
{
127+
int Field;
128+
static int Other;
129+
int GCField;
130+
static int GCOther;
131+
}
132+
";
133+
134+
var expected = @"
135+
class C
136+
{
137+
int _field;
138+
static int s_other;
139+
int _GCField;
140+
static int s_GCOther;
141+
}
142+
";
143+
144+
Verify(text, expected, runFormatter: false);
145+
146+
}
116147
}
117148
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,12 @@ private async Task SaveChanges(Solution solution, Solution originalSolution, Can
105105
var sourceText = await document.GetTextAsync(cancellationToken);
106106
using (var file = File.Open(document.FilePath, FileMode.Truncate, FileAccess.Write))
107107
{
108-
var encoding = sourceText.Encoding;
108+
// The encoding of the file can change during the rewrite. Make sure to save with the
109+
// original encoding
110+
var originalDocument = originalSolution.GetDocument(documentId);
111+
var originalSourceText = await originalDocument.GetTextAsync(cancellationToken);
109112

110-
// TODO: It seems like a bug that Encoding could change but it is definitely
111-
// happening. Ex: ArrayBuilder.Enumerator.cs
112-
if (encoding == null)
113-
{
114-
var originalDocument = originalSolution.GetDocument(documentId);
115-
var originalSourceText = await originalDocument.GetTextAsync(cancellationToken);
116-
encoding = originalSourceText.Encoding;
117-
}
118-
119-
using (var writer = new StreamWriter(file, encoding))
113+
using (var writer = new StreamWriter(file, originalSourceText.Encoding))
120114
{
121115
sourceText.Write(writer, cancellationToken);
122116
}

src/Microsoft.DotNet.CodeFormatting/Rules/HasCopyrightHeaderFormattingRule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ internal HasCopyrightHeaderFormattingRule(Options options)
2626

2727
public SyntaxNode Process(SyntaxNode syntaxNode)
2828
{
29+
if (_options.CopyrightHeader.IsDefaultOrEmpty)
30+
{
31+
return syntaxNode;
32+
}
33+
2934
if (HasCopyrightHeader(syntaxNode))
3035
return syntaxNode;
3136

src/Microsoft.DotNet.CodeFormatting/Rules/HasNoIllegalHeadersFormattingRule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal sealed class HasNoIllegalHeadersFormattingRule : ILocalSemanticFormatti
2424
// We are going to remove any multiline comments that *only* contain these characters
2525
private const string CommentFormattingCharacters = "*/=-";
2626

27-
public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
27+
public Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
2828
{
2929
var leadingTrivia = syntaxNode.GetLeadingTrivia();
3030
SyntaxTriviaList newTrivia = leadingTrivia;
@@ -71,9 +71,9 @@ public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxN
7171
}
7272

7373
if (leadingTrivia.Equals(newTrivia))
74-
return syntaxNode;
74+
return Task.FromResult(syntaxNode);
7575

76-
return syntaxNode.WithLeadingTrivia(newTrivia);
76+
return Task.FromResult(syntaxNode.WithLeadingTrivia(newTrivia));
7777
}
7878

7979
private string[] GetIllegalHeaders(Document document)

src/Microsoft.DotNet.CodeFormatting/Rules/PrivateFieldNamingRule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ private static string GetNewFieldName(IFieldSymbol fieldSymbol)
215215
return fieldSymbol.Name;
216216
}
217217

218+
if (name.Length > 2 && char.IsUpper(name[0]) && char.IsLower(name[1]))
219+
{
220+
name = char.ToLower(name[0]) + name.Substring(1);
221+
}
222+
218223
if (fieldSymbol.IsStatic)
219224
{
220225
// Check for ThreadStatic private fields.

0 commit comments

Comments
 (0)