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

Commit f33ede4

Browse files
committed
Add the /list option
This change adds the /list option which prints out all of the rule names and their description.
1 parent 740d26f commit f33ede4

17 files changed

+126
-20
lines changed

src/CodeFormatter/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ Use ConvertTests to convert MSTest tests to xUnit.
4444
/nounicode - Do not convert unicode strings to escape sequences
4545
/simple - Only run simple formatters (default)
4646
/agressive - Run agressive form
47+
/list - List the available rules
4748
/verbose - Verbose output
4849
");
4950
return -1;
5051
}
5152

53+
var comparer = StringComparer.OrdinalIgnoreCase;
5254
var projectOrSolutionPath = args[0];
55+
if (comparer.Equals(projectOrSolutionPath, "/list"))
56+
{
57+
RunListRules();
58+
return 0;
59+
}
60+
5361
if (!File.Exists(projectOrSolutionPath))
5462
{
5563
Console.Error.WriteLine("Project, solution or response file {0} doesn't exist.", projectOrSolutionPath);
@@ -64,7 +72,6 @@ Use ConvertTests to convert MSTest tests to xUnit.
6472
var convertUnicode = true;
6573
var allowTables = false;
6674
var verbose = false;
67-
var comparer = StringComparer.OrdinalIgnoreCase;
6875
var formattingLevel = FormattingLevel.Simple;
6976

7077
for (int i = 1; i < args.Length; i++)
@@ -167,6 +174,17 @@ Use ConvertTests to convert MSTest tests to xUnit.
167174
}
168175
}
169176

177+
private static void RunListRules()
178+
{
179+
var rules = FormattingEngine.GetFormattingRules();
180+
Console.WriteLine("{0,-20} {1}", "Name", "Description");
181+
Console.WriteLine("==============================================");
182+
foreach (var rule in rules)
183+
{
184+
Console.WriteLine("{0,-20} :{1}", rule.Name, rule.Description);
185+
}
186+
}
187+
170188
private static async Task RunAsync(
171189
string projectSolutionOrRspPath,
172190
ImmutableArray<string> ruleTypes,

src/Microsoft.DotNet.CodeFormatting/FormattingEngine.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,35 @@ public static class FormattingEngine
1616
{
1717
public static IFormattingEngine Create(ImmutableArray<string> ruleTypes)
1818
{
19-
var catalog = new AssemblyCatalog(typeof(FormattingEngine).Assembly);
20-
var container = new CompositionContainer(catalog);
19+
var container = CreateCompositionContainer();
2120
var engine = container.GetExportedValue<IFormattingEngine>();
2221
var consoleFormatLogger = new ConsoleFormatLogger();
2322
return engine;
2423
}
24+
25+
public static List<IRuleMetadata> GetFormattingRules()
26+
{
27+
var container = CreateCompositionContainer();
28+
var list = new List<IRuleMetadata>();
29+
AppendRules<ISyntaxFormattingRule>(list, container);
30+
AppendRules<ILocalSemanticFormattingRule>(list, container);
31+
AppendRules<IGlobalSemanticFormattingRule>(list, container);
32+
return list;
33+
}
34+
35+
private static void AppendRules<T>(List<IRuleMetadata> list, CompositionContainer container)
36+
where T : IFormattingRule
37+
{
38+
foreach (var rule in container.GetExports<T, IRuleMetadata>())
39+
{
40+
list.Add(rule.Metadata);
41+
}
42+
}
43+
44+
private static CompositionContainer CreateCompositionContainer()
45+
{
46+
var catalog = new AssemblyCatalog(typeof(FormattingEngine).Assembly);
47+
return new CompositionContainer(catalog);
48+
}
2549
}
2650
}

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public interface IFormattingEngine
2020
bool Verbose { get; set; }
2121
FormattingLevel FormattingLevel { get; set; }
2222
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
23-
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);
23+
Task FormatProjectAsync(Project project, CancellationToken cancellationToken);
2424
}
2525
}

src/Microsoft.DotNet.CodeFormatting/IRuleMetadata.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ namespace Microsoft.DotNet.CodeFormatting
77
{
88
public interface IRuleMetadata
99
{
10+
[DefaultValue("")]
11+
string Name { get; }
12+
13+
[DefaultValue("")]
14+
string Description { get; }
15+
1016
[DefaultValue(int.MaxValue)]
1117
int Order { get; }
1218

src/Microsoft.DotNet.CodeFormatting/RuleAttribute.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ namespace Microsoft.DotNet.CodeFormatting
1515
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
1616
internal sealed class SyntaxRuleAttribute : ExportAttribute, IRuleMetadata
1717
{
18-
public SyntaxRuleAttribute(int order)
18+
public SyntaxRuleAttribute(string name, string description, int order)
1919
: base(typeof(ISyntaxFormattingRule))
2020
{
21+
Name = name;
22+
Description = description;
2123
Order = order;
2224
}
2325

26+
[DefaultValue("")]
27+
public string Name { get; private set; }
28+
29+
[DefaultValue("")]
30+
public string Description { get; private set; }
31+
2432
[DefaultValue(int.MaxValue)]
2533
public int Order { get; private set; }
2634

@@ -32,12 +40,20 @@ public SyntaxRuleAttribute(int order)
3240
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
3341
internal sealed class LocalSemanticRuleAttribute : ExportAttribute, IRuleMetadata
3442
{
35-
public LocalSemanticRuleAttribute(int order)
43+
public LocalSemanticRuleAttribute(string name, string description, int order)
3644
: base(typeof(ILocalSemanticFormattingRule))
3745
{
46+
Name = name;
47+
Description = description;
3848
Order = order;
3949
}
4050

51+
[DefaultValue("")]
52+
public string Name { get; private set; }
53+
54+
[DefaultValue("")]
55+
public string Description { get; private set; }
56+
4157
[DefaultValue(int.MaxValue)]
4258
public int Order { get; private set; }
4359

@@ -49,12 +65,20 @@ public LocalSemanticRuleAttribute(int order)
4965
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
5066
internal sealed class GlobalSemanticRuleAttribute : ExportAttribute, IRuleMetadata
5167
{
52-
public GlobalSemanticRuleAttribute(int order)
68+
public GlobalSemanticRuleAttribute(string name, string description, int order)
5369
: base(typeof(IGlobalSemanticFormattingRule))
5470
{
71+
Name = name;
72+
Description = description;
5573
Order = order;
5674
}
5775

76+
[DefaultValue("")]
77+
public string Name { get; private set; }
78+
79+
[DefaultValue("")]
80+
public string Description { get; private set; }
81+
5882
[DefaultValue(int.MaxValue)]
5983
public int Order { get; private set; }
6084

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

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

1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
16-
[SyntaxRule(SyntaxRuleOrder.BraceNewLineRule)]
16+
[SyntaxRule(BraceNewLineRule.Name, BraceNewLineRule.Description, SyntaxRuleOrder.BraceNewLineRule)]
1717
internal sealed class BraceNewLineRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
1818
{
19+
internal const string Name = "BraceNewLine";
20+
internal const string Description = "Ensure all braces occur on a new line";
21+
1922
private enum NewLineKind
2023
{
2124
WhitespaceAndNewLine,

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

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

1313
namespace Microsoft.DotNet.CodeFormatting.Rules
1414
{
15-
[SyntaxRule(SyntaxRuleOrder.CopyrightHeaderRule)]
15+
[SyntaxRule(CopyrightHeaderRule.Name, CopyrightHeaderRule.Description, SyntaxRuleOrder.CopyrightHeaderRule)]
1616
internal sealed partial class CopyrightHeaderRule : SyntaxFormattingRule, ISyntaxFormattingRule
1717
{
18+
internal const string Name = "Copyright";
19+
internal const string Description = "Insert the copyright header into every file";
20+
1821
private abstract class CommonRule
1922
{
2023
/// <summary>

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

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

1515
namespace Microsoft.DotNet.CodeFormatting.Rules
1616
{
17-
[LocalSemanticRule(LocalSemanticRuleOrder.RemoveExplicitThisRule)]
17+
[LocalSemanticRule(ExplicitThisRule.Name, ExplicitThisRule.Description, LocalSemanticRuleOrder.RemoveExplicitThisRule)]
1818
internal sealed class ExplicitThisRule : CSharpOnlyFormattingRule, ILocalSemanticFormattingRule
1919
{
20+
internal const string Name = "ExplicitThis";
21+
internal const string Description = "Remove explicit this/Me prefixes on expressions except where necessary";
22+
2023
private sealed class ExplicitThisRewriter : CSharpSyntaxRewriter
2124
{
2225
private readonly Document _document;

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

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

1515
namespace Microsoft.DotNet.CodeFormatting.Rules
1616
{
17-
[LocalSemanticRule(LocalSemanticRuleOrder.ExplicitVisibilityRule)]
17+
[LocalSemanticRule(ExplicitVisibilityRule.Name, ExplicitVisibilityRule.Description, LocalSemanticRuleOrder.ExplicitVisibilityRule)]
1818
internal sealed partial class ExplicitVisibilityRule : ILocalSemanticFormattingRule
1919
{
20+
internal const string Name = "ExplicitVisibility";
21+
internal const string Description = "Ensure all members have an explicit visibility modifier";
22+
2023
public bool SupportsLanguage(string languageName)
2124
{
2225
return

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
namespace Microsoft.DotNet.CodeFormatting.Rules
1919
{
20-
[LocalSemanticRule(LocalSemanticRuleOrder.IsFormattedFormattingRule)]
20+
[LocalSemanticRule(FormatDocumentFormattingRule.Name, FormatDocumentFormattingRule.Description, LocalSemanticRuleOrder.IsFormattedFormattingRule)]
2121
internal sealed class FormatDocumentFormattingRule : ILocalSemanticFormattingRule
2222
{
23+
internal const string Name = "FormatDocument";
24+
internal const string Description = "Run the language specific formatter on every document";
2325
private readonly Options _options;
2426

2527
[ImportingConstructor]

0 commit comments

Comments
 (0)