@@ -20,6 +20,9 @@ internal static class Program
2020 private const string ConfigSwitch = "/c:" ;
2121 private const string CopyrightSwitch = "/copyright:" ;
2222 private const string LanguageSwitch = "/lang:" ;
23+ private const string RuleEnabledSwitch1 = "/rule+:" ;
24+ private const string RuleEnabledSwitch2 = "/rule:" ;
25+ private const string RuleDisabledSwitch = "/rule-:" ;
2326
2427 private static int Main ( string [ ] args )
2528 {
@@ -29,7 +32,8 @@ private static int Main(string[] args)
2932@"CodeFormatter <project, solution or responsefile> [/file:<filename>]
3033 [/lang:<language>] [/c:<config>[,<config>...]>]
3134 [/copyright:<file> | /nocopyright] [/tables] [/nounicode]
32- [/simple|/agressive] [/verbose]
35+ [/rule(+|-):rule1,rule2,...
36+ [/verbose]
3337
3438 /file - Only apply changes to files with specified name.
3539 /lang - Specifies the language to use when a responsefile is
@@ -42,17 +46,16 @@ Use ConvertTests to convert MSTest tests to xUnit.
4246 /tables - Let tables opt out of formatting by defining
4347 DOTNET_FORMATTER
4448 /nounicode - Do not convert unicode strings to escape sequences
45- /simple - Only run simple formatters (default)
46- /agressive - Run agressive form
47- /list - List the available rules
49+ /rule(+|-) - Enable (default) or disable the specified rule
50+ /rules - List the available rules
4851 /verbose - Verbose output
4952" ) ;
5053 return - 1 ;
5154 }
5255
5356 var comparer = StringComparer . OrdinalIgnoreCase ;
5457 var projectOrSolutionPath = args [ 0 ] ;
55- if ( comparer . Equals ( projectOrSolutionPath , "/list " ) )
58+ if ( comparer . Equals ( projectOrSolutionPath , "/rules " ) )
5659 {
5760 RunListRules ( ) ;
5861 return 0 ;
@@ -65,14 +68,13 @@ Use ConvertTests to convert MSTest tests to xUnit.
6568 }
6669
6770 var fileNamesBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
68- var ruleTypeBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
6971 var configBuilder = ImmutableArray . CreateBuilder < string [ ] > ( ) ;
7072 var copyrightHeader = FormattingConstants . DefaultCopyrightHeader ;
73+ var ruleMap = ImmutableDictionary < string , bool > . Empty ;
7174 var language = LanguageNames . CSharp ;
7275 var convertUnicode = true ;
7376 var allowTables = false ;
7477 var verbose = false ;
75- var formattingLevel = FormattingLevel . Simple ;
7678
7779 for ( int i = 1 ; i < args . Length ; i ++ )
7880 {
@@ -119,42 +121,88 @@ Use ConvertTests to convert MSTest tests to xUnit.
119121 {
120122 verbose = true ;
121123 }
122- else if ( comparer . Equals ( arg , "/tables" ) )
124+ else if ( comparer . Equals ( arg , RuleEnabledSwitch1 ) )
123125 {
124- allowTables = true ;
126+ UpdateRuleMap ( ref ruleMap , arg . Substring ( RuleEnabledSwitch1 . Length ) , enabled : true ) ;
125127 }
126- else if ( comparer . Equals ( arg , "/simple" ) )
128+ else if ( comparer . Equals ( arg , RuleEnabledSwitch2 ) )
127129 {
128- formattingLevel = FormattingLevel . Simple ;
130+ UpdateRuleMap ( ref ruleMap , arg . Substring ( RuleEnabledSwitch2 . Length ) , enabled : true ) ;
129131 }
130- else if ( comparer . Equals ( arg , "/aggressive" ) )
132+ else if ( comparer . Equals ( arg , RuleDisabledSwitch ) )
131133 {
132- formattingLevel = FormattingLevel . Agressive ;
134+ UpdateRuleMap ( ref ruleMap , arg . Substring ( RuleDisabledSwitch . Length ) , enabled : false ) ;
135+ }
136+ else if ( comparer . Equals ( arg , "/tables" ) )
137+ {
138+ allowTables = true ;
133139 }
134140 else
135141 {
136- ruleTypeBuilder . Add ( arg ) ;
142+ Console . WriteLine ( "Unrecognized option: {0}" , arg ) ;
143+ return 1 ;
137144 }
138145 }
139146
147+ return RunFormat (
148+ projectOrSolutionPath ,
149+ fileNamesBuilder . ToImmutableArray ( ) ,
150+ configBuilder . ToImmutableArray ( ) ,
151+ copyrightHeader ,
152+ ruleMap ,
153+ language ,
154+ allowTables ,
155+ convertUnicode ,
156+ verbose ) ;
157+ }
158+
159+ private static void RunListRules ( )
160+ {
161+ var rules = FormattingEngine . GetFormattingRules ( ) ;
162+ Console . WriteLine ( "{0,-20} {1}" , "Name" , "Description" ) ;
163+ Console . WriteLine ( "==============================================" ) ;
164+ foreach ( var rule in rules )
165+ {
166+ Console . WriteLine ( "{0,-20} :{1}" , rule . Name , rule . Description ) ;
167+ }
168+ }
169+
170+ private static void UpdateRuleMap ( ref ImmutableDictionary < string , bool > ruleMap , string data , bool enabled )
171+ {
172+ foreach ( var current in data . Split ( ',' ) )
173+ {
174+ ruleMap = ruleMap . SetItem ( current , enabled ) ;
175+ }
176+ }
177+
178+ private static int RunFormat (
179+ string projectSolutionOrRspPath ,
180+ ImmutableArray < string > fileNames ,
181+ ImmutableArray < string [ ] > preprocessorConfigurations ,
182+ ImmutableArray < string > copyrightHeader ,
183+ ImmutableDictionary < string , bool > ruleMap ,
184+ string language ,
185+ bool allowTables ,
186+ bool convertUnicode ,
187+ bool verbose )
188+ {
140189 var cts = new CancellationTokenSource ( ) ;
141190 var ct = cts . Token ;
142191
143192 Console . CancelKeyPress += delegate { cts . Cancel ( ) ; } ;
144193
145194 try
146195 {
147- RunAsync (
148- projectOrSolutionPath ,
149- ruleTypeBuilder . ToImmutableArray ( ) ,
150- fileNamesBuilder . ToImmutableArray ( ) ,
151- configBuilder . ToImmutableArray ( ) ,
196+ RunFormatAsync (
197+ projectSolutionOrRspPath ,
198+ fileNames ,
199+ preprocessorConfigurations ,
152200 copyrightHeader ,
201+ ruleMap ,
153202 language ,
154203 allowTables ,
155204 convertUnicode ,
156205 verbose ,
157- formattingLevel ,
158206 ct ) . Wait ( ct ) ;
159207 Console . WriteLine ( "Completed formatting." ) ;
160208 return 0 ;
@@ -174,38 +222,30 @@ Use ConvertTests to convert MSTest tests to xUnit.
174222 }
175223 }
176224
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-
188- private static async Task RunAsync (
225+ private static async Task < int > RunFormatAsync (
189226 string projectSolutionOrRspPath ,
190- ImmutableArray < string > ruleTypes ,
191227 ImmutableArray < string > fileNames ,
192228 ImmutableArray < string [ ] > preprocessorConfigurations ,
193229 ImmutableArray < string > copyrightHeader ,
230+ ImmutableDictionary < string , bool > ruleMap ,
194231 string language ,
195232 bool allowTables ,
196233 bool convertUnicode ,
197234 bool verbose ,
198- FormattingLevel formattingLevel ,
199235 CancellationToken cancellationToken )
200236 {
201- var engine = FormattingEngine . Create ( ruleTypes ) ;
237+ var engine = FormattingEngine . Create ( ) ;
202238 engine . PreprocessorConfigurations = preprocessorConfigurations ;
203239 engine . FileNames = fileNames ;
204240 engine . CopyrightHeader = copyrightHeader ;
205241 engine . AllowTables = allowTables ;
206242 engine . ConvertUnicodeCharacters = convertUnicode ;
207243 engine . Verbose = verbose ;
208- engine . FormattingLevel = formattingLevel ;
244+
245+ if ( ! SetRuleMap ( engine , ruleMap ) )
246+ {
247+ return 1 ;
248+ }
209249
210250 Console . WriteLine ( Path . GetFileName ( projectSolutionOrRspPath ) ) ;
211251 string extension = Path . GetExtension ( projectSolutionOrRspPath ) ;
@@ -235,6 +275,26 @@ private static async Task RunAsync(
235275 await engine . FormatProjectAsync ( project , cancellationToken ) ;
236276 }
237277 }
278+
279+ return 0 ;
280+ }
281+
282+ private static bool SetRuleMap ( IFormattingEngine engine , ImmutableDictionary < string , bool > ruleMap )
283+ {
284+ var comparer = StringComparer . OrdinalIgnoreCase ;
285+ foreach ( var entry in ruleMap )
286+ {
287+ var rule = engine . AllRules . Where ( x => comparer . Equals ( x . Name , entry . Key ) ) . FirstOrDefault ( ) ;
288+ if ( rule == null )
289+ {
290+ Console . WriteLine ( "Could not find rule with name {0}" , entry . Key ) ;
291+ return false ;
292+ }
293+
294+ engine . ToggleRuleEnabled ( rule , entry . Value ) ;
295+ }
296+
297+ return true ;
238298 }
239299 }
240300}
0 commit comments