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

Commit 67488a7

Browse files
committed
Merge pull request #82 from jaredpar/fix-endings
Disable table formatting by default
2 parents 36f8784 + fa21a4d commit 67488a7

10 files changed

+108
-69
lines changed

src/CodeFormatter/Program.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ private static int Main(string[] args)
2424
{
2525
if (args.Length < 1)
2626
{
27-
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2> [/copyright:file]");
28-
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
29-
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
27+
Console.WriteLine("CodeFormatter <project or solution> [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file] [/verbose]");
3028
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
3129
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
3230
Console.WriteLine(" should run under.");
3331
Console.WriteLine(" <copyright> - Specifies file containing copyright header.");
32+
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
33+
Console.WriteLine(" <tables> - Let tables opt out of formatting by defining DOTNET_FORMATTER");
34+
Console.WriteLine(" <verbose> - Verbose output");
35+
Console.WriteLine(" <nounicode> - Do not convert unicode strings to escape sequences");
36+
Console.WriteLine(" <nocopyright>- Do not update the copyright message.");
3437
return -1;
3538
}
3639

@@ -45,6 +48,9 @@ private static int Main(string[] args)
4548
var ruleTypeBuilder = ImmutableArray.CreateBuilder<string>();
4649
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
4750
var copyrightHeader = FormattingConstants.DefaultCopyrightHeader;
51+
var convertUnicode = true;
52+
var allowTables = false;
53+
var verbose = false;
4854
var comparer = StringComparer.OrdinalIgnoreCase;
4955

5056
for (int i = 1; i < args.Length; i++)
@@ -80,6 +86,18 @@ private static int Main(string[] args)
8086
{
8187
copyrightHeader = ImmutableArray<string>.Empty;
8288
}
89+
else if (comparer.Equals(arg, "/nounicode"))
90+
{
91+
convertUnicode = false;
92+
}
93+
else if (comparer.Equals(arg, "/verbose"))
94+
{
95+
verbose = true;
96+
}
97+
else if (comparer.Equals(arg, "/tables"))
98+
{
99+
allowTables = true;
100+
}
83101
else
84102
{
85103
ruleTypeBuilder.Add(arg);
@@ -99,6 +117,9 @@ private static int Main(string[] args)
99117
fileNamesBuilder.ToImmutableArray(),
100118
configBuilder.ToImmutableArray(),
101119
copyrightHeader,
120+
allowTables,
121+
convertUnicode,
122+
verbose,
102123
ct).Wait(ct);
103124
Console.WriteLine("Completed formatting.");
104125
return 0;
@@ -124,14 +145,21 @@ private static async Task RunAsync(
124145
ImmutableArray<string> fileNames,
125146
ImmutableArray<string[]> preprocessorConfigurations,
126147
ImmutableArray<string> copyrightHeader,
148+
bool allowTables,
149+
bool convertUnicode,
150+
bool verbose,
127151
CancellationToken cancellationToken)
128152
{
129153
var workspace = MSBuildWorkspace.Create();
130154
var engine = FormattingEngine.Create(ruleTypes);
131155
engine.PreprocessorConfigurations = preprocessorConfigurations;
132156
engine.FileNames = fileNames;
133157
engine.CopyrightHeader = copyrightHeader;
158+
engine.AllowTables = allowTables;
159+
engine.ConvertUnicodeCharacters = convertUnicode;
160+
engine.Verbose = verbose;
134161

162+
Console.WriteLine(Path.GetFileName(projectOrSolutionPath));
135163
string extension = Path.GetExtension(projectOrSolutionPath);
136164
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
137165
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.DotNet.CodeFormatting.Tests
1313
/// <summary>
1414
/// A test which runs all rules on a given piece of code
1515
/// </summary>
16-
public sealed class CombinationTest : CodeFormattingTestBase
16+
public sealed class CombinationTest : CodeFormattingTestBase, IDisposable
1717
{
1818
private static FormattingEngineImplementation s_formattingEngine;
1919

@@ -25,10 +25,16 @@ static CombinationTest()
2525
public CombinationTest()
2626
{
2727
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("// header");
28+
s_formattingEngine.AllowTables = true;
2829
s_formattingEngine.FormatLogger = new EmptyFormatLogger();
2930
s_formattingEngine.PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3031
}
3132

33+
public void Dispose()
34+
{
35+
s_formattingEngine.AllowTables = false;
36+
}
37+
3238
protected override async Task<Document> RewriteDocumentAsync(Document document)
3339
{
3440
var solution = await s_formattingEngine.FormatCoreAsync(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NonAsciiCharactersAreEscapedInLiteralsTests : SyntaxRuleTestBase
1010
{
1111
internal override ISyntaxFormattingRule Rule
1212
{
13-
get { return new Rules.NonAsciiCharactersAreEscapedInLiterals(); }
13+
get { return new Rules.NonAsciiCharactersAreEscapedInLiterals(new Options() { ConvertUnicodeCharacters = true }); }
1414
}
1515

1616
[Fact]

src/Microsoft.DotNet.CodeFormatting/FormattingEngine.cs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,9 @@ public static class FormattingEngine
1717
public static IFormattingEngine Create(ImmutableArray<string> ruleTypes)
1818
{
1919
var catalog = new AssemblyCatalog(typeof(FormattingEngine).Assembly);
20-
21-
var ruleTypesHash = new HashSet<string>(ruleTypes, StringComparer.InvariantCultureIgnoreCase);
22-
var notFoundRuleTypes = new HashSet<string>(ruleTypes, StringComparer.InvariantCultureIgnoreCase);
23-
24-
var filteredCatalog = new FilteredCatalog(catalog, cpd =>
25-
{
26-
if (cpd.ExportDefinitions.Any(em =>
27-
em.ContractName == AttributedModelServices.GetContractName(typeof(ISyntaxFormattingRule)) ||
28-
em.ContractName == AttributedModelServices.GetContractName(typeof(ILocalSemanticFormattingRule)) ||
29-
em.ContractName == AttributedModelServices.GetContractName(typeof(IGlobalSemanticFormattingRule)) ||
30-
em.ContractName == AttributedModelServices.GetContractName(typeof(IFormattingFilter))))
31-
{
32-
object ruleType;
33-
if (cpd.Metadata.TryGetValue(RuleTypeConstants.PartMetadataKey, out ruleType))
34-
{
35-
if (ruleType is string)
36-
{
37-
notFoundRuleTypes.Remove((string)ruleType);
38-
if (!ruleTypesHash.Contains((string)ruleType))
39-
{
40-
return false;
41-
}
42-
}
43-
}
44-
}
45-
46-
return true;
47-
});
48-
49-
var container = new CompositionContainer(filteredCatalog);
20+
var container = new CompositionContainer(catalog);
5021
var engine = container.GetExportedValue<IFormattingEngine>();
5122
var consoleFormatLogger = new ConsoleFormatLogger();
52-
53-
// Need to do this after the catalog is queried, otherwise the lambda won't have been run
54-
foreach (var notFoundRuleType in notFoundRuleTypes)
55-
{
56-
consoleFormatLogger.WriteErrorLine("The specified rule type was not found: {0}", notFoundRuleType);
57-
}
58-
5923
return engine;
6024
}
6125
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ internal sealed class FormattingEngineImplementation : IFormattingEngine
3333
private readonly IEnumerable<ILocalSemanticFormattingRule> _localSemanticRules;
3434
private readonly IEnumerable<IGlobalSemanticFormattingRule> _globalSemanticRules;
3535
private readonly Stopwatch _watch = new Stopwatch();
36+
private bool _allowTables;
37+
private bool _verbose;
3638

3739
public ImmutableArray<string> CopyrightHeader
3840
{
@@ -58,6 +60,24 @@ public IFormatLogger FormatLogger
5860
set { _options.FormatLogger = value; }
5961
}
6062

63+
public bool AllowTables
64+
{
65+
get { return _allowTables; }
66+
set { _allowTables = value; }
67+
}
68+
69+
public bool Verbose
70+
{
71+
get { return _verbose; }
72+
set { _verbose = value; }
73+
}
74+
75+
public bool ConvertUnicodeCharacters
76+
{
77+
get { return _options.ConvertUnicodeCharacters; }
78+
set { _options.ConvertUnicodeCharacters = value; }
79+
}
80+
6181
[ImportingConstructor]
6282
internal FormattingEngineImplementation(
6383
Options options,
@@ -142,11 +162,21 @@ private Solution RemoveTablePreprocessorSymbol(Solution newSolution, Solution ol
142162
internal async Task<Solution> FormatCoreAsync(Solution originalSolution, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
143163
{
144164
var solution = originalSolution;
145-
solution = AddTablePreprocessorSymbol(originalSolution);
165+
166+
if (_allowTables)
167+
{
168+
solution = AddTablePreprocessorSymbol(originalSolution);
169+
}
170+
146171
solution = await RunSyntaxPass(solution, documentIds, cancellationToken);
147172
solution = await RunLocalSemanticPass(solution, documentIds, cancellationToken);
148173
solution = await RunGlobalSemanticPass(solution, documentIds, cancellationToken);
149-
solution = RemoveTablePreprocessorSymbol(solution, originalSolution);
174+
175+
if (_allowTables)
176+
{
177+
solution = RemoveTablePreprocessorSymbol(solution, originalSolution);
178+
}
179+
150180
return solution;
151181
}
152182

@@ -190,7 +220,10 @@ private void StartDocument()
190220
private void EndDocument(Document document)
191221
{
192222
_watch.Stop();
193-
FormatLogger.WriteLine(" {0} {1} seconds", document.Name, _watch.Elapsed.TotalSeconds);
223+
if (_verbose)
224+
{
225+
FormatLogger.WriteLine(" {0} {1} seconds", document.Name, _watch.Elapsed.TotalSeconds);
226+
}
194227
}
195228

196229
/// <summary>
@@ -201,7 +234,7 @@ private void EndDocument(Document document)
201234
/// </summary>
202235
private async Task<Solution> RunSyntaxPass(Solution originalSolution, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
203236
{
204-
FormatLogger.WriteLine("Syntax Pass");
237+
FormatLogger.WriteLine("\tSyntax Pass");
205238

206239
var currentSolution = originalSolution;
207240
foreach (var documentId in documentIds)
@@ -241,7 +274,7 @@ private SyntaxNode RunSyntaxPass(SyntaxNode root, string languageName)
241274

242275
private async Task<Solution> RunLocalSemanticPass(Solution solution, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
243276
{
244-
FormatLogger.WriteLine("Local Semantic Pass");
277+
FormatLogger.WriteLine("\tLocal Semantic Pass");
245278
foreach (var localSemanticRule in _localSemanticRules)
246279
{
247280
solution = await RunLocalSemanticPass(solution, documentIds, localSemanticRule, cancellationToken);
@@ -252,7 +285,11 @@ private async Task<Solution> RunLocalSemanticPass(Solution solution, IReadOnlyLi
252285

253286
private async Task<Solution> RunLocalSemanticPass(Solution originalSolution, IReadOnlyList<DocumentId> documentIds, ILocalSemanticFormattingRule localSemanticRule, CancellationToken cancellationToken)
254287
{
255-
FormatLogger.WriteLine(" {0}", localSemanticRule.GetType().Name);
288+
if (_verbose)
289+
{
290+
FormatLogger.WriteLine(" {0}", localSemanticRule.GetType().Name);
291+
}
292+
256293
var currentSolution = originalSolution;
257294
foreach (var documentId in documentIds)
258295
{
@@ -278,7 +315,7 @@ private async Task<Solution> RunLocalSemanticPass(Solution originalSolution, IRe
278315

279316
private async Task<Solution> RunGlobalSemanticPass(Solution solution, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
280317
{
281-
FormatLogger.WriteLine("Global Semantic Pass");
318+
FormatLogger.WriteLine("\tGlobal Semantic Pass");
282319
foreach (var globalSemanticRule in _globalSemanticRules)
283320
{
284321
solution = await RunGlobalSemanticPass(solution, documentIds, globalSemanticRule, cancellationToken);
@@ -289,7 +326,11 @@ private async Task<Solution> RunGlobalSemanticPass(Solution solution, IReadOnlyL
289326

290327
private async Task<Solution> RunGlobalSemanticPass(Solution solution, IReadOnlyList<DocumentId> documentIds, IGlobalSemanticFormattingRule globalSemanticRule, CancellationToken cancellationToken)
291328
{
292-
FormatLogger.WriteLine(" {0}", globalSemanticRule.GetType().Name);
329+
if (_verbose)
330+
{
331+
FormatLogger.WriteLine(" {0}", globalSemanticRule.GetType().Name);
332+
}
333+
293334
foreach (var documentId in documentIds)
294335
{
295336
var document = solution.GetDocument(documentId);

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public interface IFormattingEngine
1515
ImmutableArray<string> CopyrightHeader { get; set; }
1616
ImmutableArray<string[]> PreprocessorConfigurations { get; set; }
1717
ImmutableArray<string> FileNames { get; set; }
18+
bool AllowTables { get; set; }
19+
bool ConvertUnicodeCharacters { get; set; }
20+
bool Verbose { get; set; }
1821
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
1922
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);
2023
}

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRule.cs" />
139139
<Compile Include="Rules\ExplicitThisRule.cs" />
140140
<Compile Include="Rules\RuleOrder.cs" />
141-
<Compile Include="RuleTypeConstants.cs" />
142141
</ItemGroup>
143142
<ItemGroup>
144143
<None Include="app.config" />

src/Microsoft.DotNet.CodeFormatting/Options.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ internal sealed class Options
2424

2525
internal IFormatLogger FormatLogger { get; set; }
2626

27+
internal bool ConvertUnicodeCharacters { get; set; }
28+
2729
[ImportingConstructor]
2830
internal Options()
2931
{
3032
CopyrightHeader = FormattingConstants.DefaultCopyrightHeader;
3133
FileNames = ImmutableArray<string>.Empty;
3234
PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3335
FormatLogger = new ConsoleFormatLogger();
36+
ConvertUnicodeCharacters = true;
3437
}
3538
}
3639
}

src/Microsoft.DotNet.CodeFormatting/RuleTypeConstants.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,21 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
1818
[SyntaxRuleOrder(SyntaxRuleOrder.NonAsciiChractersAreEscapedInLiterals)]
1919
internal sealed class NonAsciiCharactersAreEscapedInLiterals : CSharpOnlyFormattingRule, ISyntaxFormattingRule
2020
{
21+
private readonly Options _options;
22+
23+
[ImportingConstructor]
24+
internal NonAsciiCharactersAreEscapedInLiterals(Options options)
25+
{
26+
_options = options;
27+
}
28+
2129
public SyntaxNode Process(SyntaxNode root, string languageName)
2230
{
31+
if (!_options.ConvertUnicodeCharacters)
32+
{
33+
return root;
34+
}
35+
2336
return UnicodeCharacterEscapingSyntaxRewriter.Rewriter.Visit(root);
2437
}
2538

0 commit comments

Comments
 (0)