2
// FormattingOptionsFactory.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
namespace ICSharpCode.NRefactory.CSharp
31
/// The formatting options factory creates pre defined formatting option styles.
33
public static class FormattingOptionsFactory
36
/// Creates empty CSharpFormatting options.
38
public static CSharpFormattingOptions CreateEmpty()
40
return new CSharpFormattingOptions();
44
/// Creates mono indent style CSharpFormatting options.
46
public static CSharpFormattingOptions CreateMono ()
48
return new CSharpFormattingOptions () {
49
IndentNamespaceBody = true,
50
IndentClassBody = true,
51
IndentInterfaceBody = true,
52
IndentStructBody = true,
53
IndentEnumBody = true,
54
IndentMethodBody = true,
55
IndentPropertyBody = true,
56
IndentEventBody = true,
58
IndentSwitchBody = false,
59
IndentCaseBody = true,
60
IndentBreakStatements = true,
61
NamespaceBraceStyle = BraceStyle.NextLine,
62
ClassBraceStyle = BraceStyle.NextLine,
63
InterfaceBraceStyle = BraceStyle.NextLine,
64
StructBraceStyle = BraceStyle.NextLine,
65
EnumBraceStyle = BraceStyle.NextLine,
66
MethodBraceStyle = BraceStyle.NextLine,
67
ConstructorBraceStyle = BraceStyle.NextLine,
68
DestructorBraceStyle = BraceStyle.NextLine,
69
AnonymousMethodBraceStyle = BraceStyle.EndOfLine,
71
PropertyBraceStyle = BraceStyle.EndOfLine,
72
PropertyGetBraceStyle = BraceStyle.EndOfLine,
73
PropertySetBraceStyle = BraceStyle.EndOfLine,
74
AllowPropertyGetBlockInline = true,
75
AllowPropertySetBlockInline = true,
77
EventBraceStyle = BraceStyle.EndOfLine,
78
EventAddBraceStyle = BraceStyle.EndOfLine,
79
EventRemoveBraceStyle = BraceStyle.EndOfLine,
80
AllowEventAddBlockInline = true,
81
AllowEventRemoveBlockInline = true,
82
StatementBraceStyle = BraceStyle.EndOfLine,
84
ElseNewLinePlacement = NewLinePlacement.SameLine,
85
CatchNewLinePlacement = NewLinePlacement.SameLine,
86
FinallyNewLinePlacement = NewLinePlacement.SameLine,
87
WhileNewLinePlacement = NewLinePlacement.SameLine,
88
ArrayInitializerWrapping = Wrapping.WrapIfTooLong,
89
ArrayInitializerBraceStyle = BraceStyle.EndOfLine,
91
SpaceBeforeMethodCallParentheses = true,
92
SpaceBeforeMethodDeclarationParentheses = true,
93
SpaceBeforeConstructorDeclarationParentheses = true,
94
SpaceBeforeDelegateDeclarationParentheses = true,
95
SpaceAfterMethodCallParameterComma = true,
96
SpaceAfterConstructorDeclarationParameterComma = true,
98
SpaceBeforeNewParentheses = true,
99
SpacesWithinNewParentheses = false,
100
SpacesBetweenEmptyNewParentheses = false,
101
SpaceBeforeNewParameterComma = false,
102
SpaceAfterNewParameterComma = true,
104
SpaceBeforeIfParentheses = true,
105
SpaceBeforeWhileParentheses = true,
106
SpaceBeforeForParentheses = true,
107
SpaceBeforeForeachParentheses = true,
108
SpaceBeforeCatchParentheses = true,
109
SpaceBeforeSwitchParentheses = true,
110
SpaceBeforeLockParentheses = true,
111
SpaceBeforeUsingParentheses = true,
112
SpaceAroundAssignment = true,
113
SpaceAroundLogicalOperator = true,
114
SpaceAroundEqualityOperator = true,
115
SpaceAroundRelationalOperator = true,
116
SpaceAroundBitwiseOperator = true,
117
SpaceAroundAdditiveOperator = true,
118
SpaceAroundMultiplicativeOperator = true,
119
SpaceAroundShiftOperator = true,
120
SpaceAroundNullCoalescingOperator = true,
121
SpacesWithinParentheses = false,
122
SpaceWithinMethodCallParentheses = false,
123
SpaceWithinMethodDeclarationParentheses = false,
124
SpacesWithinIfParentheses = false,
125
SpacesWithinWhileParentheses = false,
126
SpacesWithinForParentheses = false,
127
SpacesWithinForeachParentheses = false,
128
SpacesWithinCatchParentheses = false,
129
SpacesWithinSwitchParentheses = false,
130
SpacesWithinLockParentheses = false,
131
SpacesWithinUsingParentheses = false,
132
SpacesWithinCastParentheses = false,
133
SpacesWithinSizeOfParentheses = false,
134
SpacesWithinTypeOfParentheses = false,
135
SpacesWithinCheckedExpressionParantheses = false,
136
SpaceBeforeConditionalOperatorCondition = true,
137
SpaceAfterConditionalOperatorCondition = true,
138
SpaceBeforeConditionalOperatorSeparator = true,
139
SpaceAfterConditionalOperatorSeparator = true,
141
SpacesWithinBrackets = false,
142
SpacesBeforeBrackets = true,
143
SpaceBeforeBracketComma = false,
144
SpaceAfterBracketComma = true,
146
SpaceBeforeForSemicolon = false,
147
SpaceAfterForSemicolon = true,
148
SpaceAfterTypecast = false,
150
AlignEmbeddedIfStatements = true,
151
AlignEmbeddedUsingStatements = true,
152
PropertyFormatting = PropertyFormatting.AllowOneLine,
153
SpaceBeforeMethodDeclarationParameterComma = false,
154
SpaceAfterMethodDeclarationParameterComma = true,
155
SpaceBeforeFieldDeclarationComma = false,
156
SpaceAfterFieldDeclarationComma = true,
157
SpaceBeforeLocalVariableDeclarationComma = false,
158
SpaceAfterLocalVariableDeclarationComma = true,
160
SpaceBeforeIndexerDeclarationBracket = true,
161
SpaceWithinIndexerDeclarationBracket = false,
162
SpaceBeforeIndexerDeclarationParameterComma = false,
163
SpaceInNamedArgumentAfterDoubleColon = true,
165
SpaceAfterIndexerDeclarationParameterComma = true,
167
BlankLinesBeforeUsings = 0,
168
BlankLinesAfterUsings = 1,
171
BlankLinesBeforeFirstDeclaration = 0,
172
BlankLinesBetweenTypes = 1,
173
BlankLinesBetweenFields = 0,
174
BlankLinesBetweenEventFields = 0,
175
BlankLinesBetweenMembers = 1,
177
KeepCommentsAtFirstColumn = true,
178
ChainedMethodCallWrapping = Wrapping.DoNotChange,
179
MethodCallArgumentWrapping = Wrapping.DoNotChange,
180
NewLineAferMethodCallOpenParentheses = true,
181
MethodCallClosingParenthesesOnNewLine = true,
183
IndexerArgumentWrapping = Wrapping.DoNotChange,
184
NewLineAferIndexerOpenBracket = false,
185
IndexerClosingBracketOnNewLine = false,
187
IfElseBraceForcement = BraceForcement.DoNotChange,
188
ForBraceForcement = BraceForcement.DoNotChange,
189
ForEachBraceForcement = BraceForcement.DoNotChange,
190
WhileBraceForcement = BraceForcement.DoNotChange,
191
UsingBraceForcement = BraceForcement.DoNotChange,
192
FixedBraceForcement = BraceForcement.DoNotChange
197
/// Creates sharp develop indent style CSharpFormatting options.
199
public static CSharpFormattingOptions CreateSharpDevelop()
201
var baseOptions = CreateKRStyle();
202
baseOptions.IfElseBraceForcement = BraceForcement.AddBraces;
203
baseOptions.ForBraceForcement = BraceForcement.AddBraces;
204
baseOptions.ForEachBraceForcement = BraceForcement.AddBraces;
205
baseOptions.WhileBraceForcement = BraceForcement.AddBraces;
206
baseOptions.UsingBraceForcement = BraceForcement.AddBraces;
207
baseOptions.FixedBraceForcement = BraceForcement.AddBraces;
212
/// The K&R style, so named because it was used in Kernighan and Ritchie's book The C Programming Language,
213
/// is commonly used in C. It is less common for C++, C#, and others.
215
public static CSharpFormattingOptions CreateKRStyle ()
217
return new CSharpFormattingOptions () {
218
IndentNamespaceBody = true,
219
IndentClassBody = true,
220
IndentInterfaceBody = true,
221
IndentStructBody = true,
222
IndentEnumBody = true,
223
IndentMethodBody = true,
224
IndentPropertyBody = true,
225
IndentEventBody = true,
227
IndentSwitchBody = true,
228
IndentCaseBody = true,
229
IndentBreakStatements = true,
231
NamespaceBraceStyle = BraceStyle.NextLine,
232
ClassBraceStyle = BraceStyle.NextLine,
233
InterfaceBraceStyle = BraceStyle.NextLine,
234
StructBraceStyle = BraceStyle.NextLine,
235
EnumBraceStyle = BraceStyle.NextLine,
236
MethodBraceStyle = BraceStyle.NextLine,
237
ConstructorBraceStyle = BraceStyle.NextLine,
238
DestructorBraceStyle = BraceStyle.NextLine,
239
AnonymousMethodBraceStyle = BraceStyle.EndOfLine,
240
PropertyBraceStyle = BraceStyle.EndOfLine,
241
PropertyGetBraceStyle = BraceStyle.EndOfLine,
242
PropertySetBraceStyle = BraceStyle.EndOfLine,
243
AllowPropertyGetBlockInline = true,
244
AllowPropertySetBlockInline = true,
246
EventBraceStyle = BraceStyle.EndOfLine,
247
EventAddBraceStyle = BraceStyle.EndOfLine,
248
EventRemoveBraceStyle = BraceStyle.EndOfLine,
249
AllowEventAddBlockInline = true,
250
AllowEventRemoveBlockInline = true,
251
StatementBraceStyle = BraceStyle.EndOfLine,
253
ElseNewLinePlacement = NewLinePlacement.SameLine,
254
CatchNewLinePlacement = NewLinePlacement.SameLine,
255
FinallyNewLinePlacement = NewLinePlacement.SameLine,
256
WhileNewLinePlacement = NewLinePlacement.SameLine,
257
ArrayInitializerWrapping = Wrapping.WrapIfTooLong,
258
ArrayInitializerBraceStyle = BraceStyle.EndOfLine,
260
SpaceBeforeMethodCallParentheses = false,
261
SpaceBeforeMethodDeclarationParentheses = false,
262
SpaceBeforeConstructorDeclarationParentheses = false,
263
SpaceBeforeDelegateDeclarationParentheses = false,
264
SpaceAfterMethodCallParameterComma = true,
265
SpaceAfterConstructorDeclarationParameterComma = true,
267
SpaceBeforeNewParentheses = false,
268
SpacesWithinNewParentheses = false,
269
SpacesBetweenEmptyNewParentheses = false,
270
SpaceBeforeNewParameterComma = false,
271
SpaceAfterNewParameterComma = true,
273
SpaceBeforeIfParentheses = true,
274
SpaceBeforeWhileParentheses = true,
275
SpaceBeforeForParentheses = true,
276
SpaceBeforeForeachParentheses = true,
277
SpaceBeforeCatchParentheses = true,
278
SpaceBeforeSwitchParentheses = true,
279
SpaceBeforeLockParentheses = true,
280
SpaceBeforeUsingParentheses = true,
282
SpaceAroundAssignment = true,
283
SpaceAroundLogicalOperator = true,
284
SpaceAroundEqualityOperator = true,
285
SpaceAroundRelationalOperator = true,
286
SpaceAroundBitwiseOperator = true,
287
SpaceAroundAdditiveOperator = true,
288
SpaceAroundMultiplicativeOperator = true,
289
SpaceAroundShiftOperator = true,
290
SpaceAroundNullCoalescingOperator = true,
291
SpacesWithinParentheses = false,
292
SpaceWithinMethodCallParentheses = false,
293
SpaceWithinMethodDeclarationParentheses = false,
294
SpacesWithinIfParentheses = false,
295
SpacesWithinWhileParentheses = false,
296
SpacesWithinForParentheses = false,
297
SpacesWithinForeachParentheses = false,
298
SpacesWithinCatchParentheses = false,
299
SpacesWithinSwitchParentheses = false,
300
SpacesWithinLockParentheses = false,
301
SpacesWithinUsingParentheses = false,
302
SpacesWithinCastParentheses = false,
303
SpacesWithinSizeOfParentheses = false,
304
SpacesWithinTypeOfParentheses = false,
305
SpacesWithinCheckedExpressionParantheses = false,
306
SpaceBeforeConditionalOperatorCondition = true,
307
SpaceAfterConditionalOperatorCondition = true,
308
SpaceBeforeConditionalOperatorSeparator = true,
309
SpaceAfterConditionalOperatorSeparator = true,
311
SpacesWithinBrackets = false,
312
SpacesBeforeBrackets = true,
313
SpaceBeforeBracketComma = false,
314
SpaceAfterBracketComma = true,
316
SpaceBeforeForSemicolon = false,
317
SpaceAfterForSemicolon = true,
318
SpaceAfterTypecast = false,
320
AlignEmbeddedIfStatements = true,
321
AlignEmbeddedUsingStatements = true,
322
PropertyFormatting = PropertyFormatting.AllowOneLine,
323
SpaceBeforeMethodDeclarationParameterComma = false,
324
SpaceAfterMethodDeclarationParameterComma = true,
325
SpaceBeforeFieldDeclarationComma = false,
326
SpaceAfterFieldDeclarationComma = true,
327
SpaceBeforeLocalVariableDeclarationComma = false,
328
SpaceAfterLocalVariableDeclarationComma = true,
330
SpaceBeforeIndexerDeclarationBracket = true,
331
SpaceWithinIndexerDeclarationBracket = false,
332
SpaceBeforeIndexerDeclarationParameterComma = false,
333
SpaceInNamedArgumentAfterDoubleColon = true,
335
SpaceAfterIndexerDeclarationParameterComma = true,
337
BlankLinesBeforeUsings = 0,
338
BlankLinesAfterUsings = 1,
340
BlankLinesBeforeFirstDeclaration = 0,
341
BlankLinesBetweenTypes = 1,
342
BlankLinesBetweenFields = 0,
343
BlankLinesBetweenEventFields = 0,
344
BlankLinesBetweenMembers = 1,
346
KeepCommentsAtFirstColumn = true,
347
ChainedMethodCallWrapping = Wrapping.DoNotChange,
348
MethodCallArgumentWrapping = Wrapping.DoNotChange,
349
NewLineAferMethodCallOpenParentheses = true,
350
MethodCallClosingParenthesesOnNewLine = true,
352
IndexerArgumentWrapping = Wrapping.DoNotChange,
353
NewLineAferIndexerOpenBracket = false,
354
IndexerClosingBracketOnNewLine = false,
356
IfElseBraceForcement = BraceForcement.DoNotChange,
357
ForBraceForcement = BraceForcement.DoNotChange,
358
ForEachBraceForcement = BraceForcement.DoNotChange,
359
WhileBraceForcement = BraceForcement.DoNotChange,
360
UsingBraceForcement = BraceForcement.DoNotChange,
361
FixedBraceForcement = BraceForcement.DoNotChange
366
/// Creates allman indent style CSharpFormatting options used in Visual Studio.
368
public static CSharpFormattingOptions CreateAllman()
370
var baseOptions = CreateKRStyle();
371
baseOptions.AnonymousMethodBraceStyle = BraceStyle.NextLine;
372
baseOptions.PropertyBraceStyle = BraceStyle.NextLine;
373
baseOptions.PropertyGetBraceStyle = BraceStyle.NextLine;
374
baseOptions.PropertySetBraceStyle = BraceStyle.NextLine;
376
baseOptions.EventBraceStyle = BraceStyle.NextLine;
377
baseOptions.EventAddBraceStyle = BraceStyle.NextLine;
378
baseOptions.EventRemoveBraceStyle = BraceStyle.NextLine;
379
baseOptions.StatementBraceStyle = BraceStyle.NextLine;
380
baseOptions.ArrayInitializerBraceStyle = BraceStyle.NextLine;
385
/// The Whitesmiths style, also called Wishart style to a lesser extent, is less common today than the previous three. It was originally used in the documentation for the first commercial C compiler, the Whitesmiths Compiler.
387
public static CSharpFormattingOptions CreateWhitesmiths()
389
var baseOptions = CreateKRStyle();
391
baseOptions.NamespaceBraceStyle = BraceStyle.NextLineShifted;
392
baseOptions.ClassBraceStyle = BraceStyle.NextLineShifted;
393
baseOptions.InterfaceBraceStyle = BraceStyle.NextLineShifted;
394
baseOptions.StructBraceStyle = BraceStyle.NextLineShifted;
395
baseOptions.EnumBraceStyle = BraceStyle.NextLineShifted;
396
baseOptions.MethodBraceStyle = BraceStyle.NextLineShifted;
397
baseOptions.ConstructorBraceStyle = BraceStyle.NextLineShifted;
398
baseOptions.DestructorBraceStyle = BraceStyle.NextLineShifted;
399
baseOptions.AnonymousMethodBraceStyle = BraceStyle.NextLineShifted;
400
baseOptions.PropertyBraceStyle = BraceStyle.NextLineShifted;
401
baseOptions.PropertyGetBraceStyle = BraceStyle.NextLineShifted;
402
baseOptions.PropertySetBraceStyle = BraceStyle.NextLineShifted;
404
baseOptions.EventBraceStyle = BraceStyle.NextLineShifted;
405
baseOptions.EventAddBraceStyle = BraceStyle.NextLineShifted;
406
baseOptions.EventRemoveBraceStyle = BraceStyle.NextLineShifted;
407
baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted;
412
/// Like the Allman and Whitesmiths styles, GNU style puts braces on a line by themselves, indented by 2 spaces,
413
/// except when opening a function definition, where they are not indented.
414
/// In either case, the contained code is indented by 2 spaces from the braces.
415
/// Popularised by Richard Stallman, the layout may be influenced by his background of writing Lisp code.
416
/// In Lisp the equivalent to a block (a progn)
417
/// is a first class data entity and giving it its own indent level helps to emphasize that,
418
/// whereas in C a block is just syntax.
419
/// Although not directly related to indentation, GNU coding style also includes a space before the bracketed
420
/// list of arguments to a function.
422
public static CSharpFormattingOptions CreateGNU()
424
var baseOptions = CreateAllman();
425
baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted2;