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

Commit eee50a9

Browse files
committed
Merge pull request #112 from ChadNedzlek/agressive-switch-submit
Add a switch to opt-out of slow checks
2 parents 0ac4069 + 8fd3172 commit eee50a9

19 files changed

+96
-38
lines changed

src/CodeFormatter/Program.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,27 @@ private static int Main(string[] args)
2525
{
2626
if (args.Length < 1)
2727
{
28-
Console.WriteLine("CodeFormatter <project, solution or responsefile> [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file] [/lang:<language>] [/verbose]");
29-
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
30-
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
31-
Console.WriteLine(" should run under.");
32-
Console.WriteLine(" <copyright> - Specifies file containing copyright header.");
33-
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
34-
Console.WriteLine(" <tables> - Let tables opt out of formatting by defining DOTNET_FORMATTER");
35-
Console.WriteLine(" <language> - Specifies the language to use when a responsefile is specified.");
36-
Console.WriteLine(" i.e. 'C#', 'Visual Basic', ... (default: 'C#')");
37-
Console.WriteLine(" <verbose> - Verbose output");
38-
Console.WriteLine(" <nounicode> - Do not convert unicode strings to escape sequences");
39-
Console.WriteLine(" <nocopyright>- Do not update the copyright message.");
28+
Console.WriteLine(
29+
@"CodeFormatter <project, solution or responsefile> [/file:<filename>]
30+
[/lang:<language>] [/c:<config>[,<config>...]>]
31+
[/copyright:<file> | /nocopyright] [/tables] [/nounicode]
32+
[/simple|/agressive] [/verbose]
33+
34+
/file - Only apply changes to files with specified name.
35+
/lang - Specifies the language to use when a responsefile is
36+
specified. i.e. 'C#', 'Visual Basic', ... (default: 'C#')
37+
/c - Additional preprocessor configurations the formatter
38+
should run under.
39+
/copyright - Specifies file containing copyright header.
40+
Use ConvertTests to convert MSTest tests to xUnit.
41+
/nocopyright - Do not update the copyright message.
42+
/tables - Let tables opt out of formatting by defining
43+
DOTNET_FORMATTER
44+
/nounicode - Do not convert unicode strings to escape sequences
45+
/simple - Only run simple formatters (default)
46+
/agressive - Run agressive form
47+
/verbose - Verbose output
48+
");
4049
return -1;
4150
}
4251

@@ -56,6 +65,7 @@ private static int Main(string[] args)
5665
var allowTables = false;
5766
var verbose = false;
5867
var comparer = StringComparer.OrdinalIgnoreCase;
68+
var formattingLevel = FormattingLevel.Simple;
5969

6070
for (int i = 1; i < args.Length; i++)
6171
{
@@ -106,6 +116,14 @@ private static int Main(string[] args)
106116
{
107117
allowTables = true;
108118
}
119+
else if (comparer.Equals(arg, "/simple"))
120+
{
121+
formattingLevel = FormattingLevel.Simple;
122+
}
123+
else if (comparer.Equals(arg, "/aggressive"))
124+
{
125+
formattingLevel = FormattingLevel.Agressive;
126+
}
109127
else
110128
{
111129
ruleTypeBuilder.Add(arg);
@@ -129,6 +147,7 @@ private static int Main(string[] args)
129147
allowTables,
130148
convertUnicode,
131149
verbose,
150+
formattingLevel,
132151
ct).Wait(ct);
133152
Console.WriteLine("Completed formatting.");
134153
return 0;
@@ -158,6 +177,7 @@ private static async Task RunAsync(
158177
bool allowTables,
159178
bool convertUnicode,
160179
bool verbose,
180+
FormattingLevel formattingLevel,
161181
CancellationToken cancellationToken)
162182
{
163183
var engine = FormattingEngine.Create(ruleTypes);
@@ -167,6 +187,7 @@ private static async Task RunAsync(
167187
engine.AllowTables = allowTables;
168188
engine.ConvertUnicodeCharacters = convertUnicode;
169189
engine.Verbose = verbose;
190+
engine.FormattingLevel = formattingLevel;
170191

171192
Console.WriteLine(Path.GetFileName(projectSolutionOrRspPath));
172193
string extension = Path.GetExtension(projectSolutionOrRspPath);

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,35 @@ public bool ConvertUnicodeCharacters
7878
set { _options.ConvertUnicodeCharacters = value; }
7979
}
8080

81+
public FormattingLevel FormattingLevel
82+
{
83+
get { return _options.FormattingLevel; }
84+
set { _options.FormattingLevel = value; }
85+
}
86+
8187
[ImportingConstructor]
8288
internal FormattingEngineImplementation(
8389
Options options,
8490
[ImportMany] IEnumerable<IFormattingFilter> filters,
85-
[ImportMany] IEnumerable<Lazy<ISyntaxFormattingRule, IOrderMetadata>> syntaxRules,
86-
[ImportMany] IEnumerable<Lazy<ILocalSemanticFormattingRule, IOrderMetadata>> localSemanticRules,
87-
[ImportMany] IEnumerable<Lazy<IGlobalSemanticFormattingRule, IOrderMetadata>> globalSemanticRules)
91+
[ImportMany] IEnumerable<Lazy<ISyntaxFormattingRule, IRuleMetadata>> syntaxRules,
92+
[ImportMany] IEnumerable<Lazy<ILocalSemanticFormattingRule, IRuleMetadata>> localSemanticRules,
93+
[ImportMany] IEnumerable<Lazy<IGlobalSemanticFormattingRule, IRuleMetadata>> globalSemanticRules)
8894
{
8995
_options = options;
9096
_filters = filters;
91-
_syntaxRules = syntaxRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value).ToList();
92-
_localSemanticRules = localSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value).ToList();
93-
_globalSemanticRules = globalSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value).ToList();
97+
_syntaxRules = GetOrderedRules(syntaxRules);
98+
_localSemanticRules = GetOrderedRules(localSemanticRules);
99+
_globalSemanticRules = GetOrderedRules(globalSemanticRules);
100+
}
101+
102+
private IEnumerable<TRule> GetOrderedRules<TRule>(IEnumerable<Lazy<TRule, IRuleMetadata>> rules)
103+
where TRule : IFormattingRule
104+
{
105+
return rules
106+
.OrderBy(r => r.Metadata.Order)
107+
.Where(r => r.Metadata.FormattingLevel <= FormattingLevel)
108+
.Select(r => r.Value)
109+
.ToList();
94110
}
95111

96112
public Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken)

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IFormattingEngine
1818
bool AllowTables { get; set; }
1919
bool ConvertUnicodeCharacters { get; set; }
2020
bool Verbose { get; set; }
21+
FormattingLevel FormattingLevel { get; set; }
2122
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
2223
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);
2324
}

src/Microsoft.DotNet.CodeFormatting/IFormattingRule.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
namespace Microsoft.DotNet.CodeFormatting
1111
{
12+
public enum FormattingLevel
13+
{
14+
Simple = 0,
15+
Agressive,
16+
}
17+
1218
/// <summary>
1319
/// Base formatting rule which helps establish which language the rule applies to.
1420
/// </summary>

src/Microsoft.DotNet.CodeFormatting/IOrderMetadata.cs renamed to src/Microsoft.DotNet.CodeFormatting/IRuleMetadata.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
namespace Microsoft.DotNet.CodeFormatting
77
{
8-
public interface IOrderMetadata
8+
public interface IRuleMetadata
99
{
1010
[DefaultValue(int.MaxValue)]
1111
int Order { get; }
12+
13+
[DefaultValue(FormattingLevel.Simple)]
14+
FormattingLevel FormattingLevel { get; }
1215
}
1316
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@
9898
<Compile Include="IFormattingRule.cs" />
9999
<Compile Include="Filters\IgnoreGeneratedFilesFilter.cs" />
100100
<Compile Include="Options.cs" />
101-
<Compile Include="RuleOrderAttribute.cs" />
101+
<Compile Include="RuleAttribute.cs" />
102102
<Compile Include="Properties\AssemblyInfo.cs" />
103-
<Compile Include="IOrderMetadata.cs" />
103+
<Compile Include="IRuleMetadata.cs" />
104104
<Compile Include="Rules\ExplicitVisibilityRule.CSharp.cs" />
105105
<Compile Include="Rules\ExplicitVisibilityRule.VisualBasic.cs" />
106106
<Compile Include="Rules\PrivateFieldNamingRule.CSharp.cs" />

src/Microsoft.DotNet.CodeFormatting/Options.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal sealed class Options
2929

3030
internal bool ConvertUnicodeCharacters { get; set; }
3131

32+
internal FormattingLevel FormattingLevel { get; set; }
33+
3234
[ImportingConstructor]
3335
internal Options()
3436
{

src/Microsoft.DotNet.CodeFormatting/RuleOrderAttribute.cs renamed to src/Microsoft.DotNet.CodeFormatting/RuleAttribute.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,52 @@ namespace Microsoft.DotNet.CodeFormatting
1313
{
1414
[MetadataAttribute]
1515
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
16-
internal sealed class SyntaxRuleOrderAttribute : ExportAttribute
16+
internal sealed class SyntaxRuleAttribute : ExportAttribute, IRuleMetadata
1717
{
18-
public SyntaxRuleOrderAttribute(int order)
18+
public SyntaxRuleAttribute(int order)
1919
: base(typeof(ISyntaxFormattingRule))
2020
{
2121
Order = order;
2222
}
2323

2424
[DefaultValue(int.MaxValue)]
2525
public int Order { get; private set; }
26+
27+
[DefaultValue(FormattingLevel.Simple)]
28+
public FormattingLevel FormattingLevel { get; set; }
2629
}
2730

2831
[MetadataAttribute]
2932
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
30-
internal sealed class LocalSemanticRuleOrderAttribute : ExportAttribute
33+
internal sealed class LocalSemanticRuleAttribute : ExportAttribute, IRuleMetadata
3134
{
32-
public LocalSemanticRuleOrderAttribute(int order)
35+
public LocalSemanticRuleAttribute(int order)
3336
: base(typeof(ILocalSemanticFormattingRule))
3437
{
3538
Order = order;
3639
}
3740

3841
[DefaultValue(int.MaxValue)]
3942
public int Order { get; private set; }
43+
44+
[DefaultValue(FormattingLevel.Simple)]
45+
public FormattingLevel FormattingLevel { get; set; }
4046
}
4147

4248
[MetadataAttribute]
4349
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
44-
internal sealed class GlobalSemanticRuleOrderAttribute : ExportAttribute
50+
internal sealed class GlobalSemanticRuleAttribute : ExportAttribute, IRuleMetadata
4551
{
46-
public GlobalSemanticRuleOrderAttribute(int order)
52+
public GlobalSemanticRuleAttribute(int order)
4753
: base(typeof(IGlobalSemanticFormattingRule))
4854
{
4955
Order = order;
5056
}
5157

5258
[DefaultValue(int.MaxValue)]
5359
public int Order { get; private set; }
60+
61+
[DefaultValue(FormattingLevel.Simple)]
62+
public FormattingLevel FormattingLevel { get; set; }
5463
}
5564
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
16-
[SyntaxRuleOrder(SyntaxRuleOrder.BraceNewLineRule)]
16+
[SyntaxRule(SyntaxRuleOrder.BraceNewLineRule)]
1717
internal sealed class BraceNewLineRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
1818
{
1919
private enum NewLineKind

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.DotNet.CodeFormatting.Rules
1414
{
15-
[SyntaxRuleOrder(SyntaxRuleOrder.CopyrightHeaderRule)]
15+
[SyntaxRule(SyntaxRuleOrder.CopyrightHeaderRule)]
1616
internal sealed partial class CopyrightHeaderRule : SyntaxFormattingRule, ISyntaxFormattingRule
1717
{
1818
private abstract class CommonRule

0 commit comments

Comments
 (0)