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
ElseIfNewLinePlacement = NewLinePlacement.SameLine,
86
CatchNewLinePlacement = NewLinePlacement.SameLine,
87
FinallyNewLinePlacement = NewLinePlacement.SameLine,
88
WhileNewLinePlacement = NewLinePlacement.SameLine,
89
ArrayInitializerWrapping = Wrapping.WrapIfTooLong,
90
ArrayInitializerBraceStyle = BraceStyle.EndOfLine,
91
AllowOneLinedArrayInitialziers = true,
93
SpaceBeforeMethodCallParentheses = true,
94
SpaceBeforeMethodDeclarationParentheses = true,
95
SpaceBeforeConstructorDeclarationParentheses = true,
96
SpaceBeforeDelegateDeclarationParentheses = true,
97
SpaceAfterMethodCallParameterComma = true,
98
SpaceAfterConstructorDeclarationParameterComma = true,
100
SpaceBeforeNewParentheses = true,
101
SpacesWithinNewParentheses = false,
102
SpacesBetweenEmptyNewParentheses = false,
103
SpaceBeforeNewParameterComma = false,
104
SpaceAfterNewParameterComma = true,
106
SpaceBeforeIfParentheses = true,
107
SpaceBeforeWhileParentheses = true,
108
SpaceBeforeForParentheses = true,
109
SpaceBeforeForeachParentheses = true,
110
SpaceBeforeCatchParentheses = true,
111
SpaceBeforeSwitchParentheses = true,
112
SpaceBeforeLockParentheses = true,
113
SpaceBeforeUsingParentheses = true,
114
SpaceAroundAssignment = true,
115
SpaceAroundLogicalOperator = true,
116
SpaceAroundEqualityOperator = true,
117
SpaceAroundRelationalOperator = true,
118
SpaceAroundBitwiseOperator = true,
119
SpaceAroundAdditiveOperator = true,
120
SpaceAroundMultiplicativeOperator = true,
121
SpaceAroundShiftOperator = true,
122
SpaceAroundNullCoalescingOperator = true,
123
SpacesWithinParentheses = false,
124
SpaceWithinMethodCallParentheses = false,
125
SpaceWithinMethodDeclarationParentheses = false,
126
SpacesWithinIfParentheses = false,
127
SpacesWithinWhileParentheses = false,
128
SpacesWithinForParentheses = false,
129
SpacesWithinForeachParentheses = false,
130
SpacesWithinCatchParentheses = false,
131
SpacesWithinSwitchParentheses = false,
132
SpacesWithinLockParentheses = false,
133
SpacesWithinUsingParentheses = false,
134
SpacesWithinCastParentheses = false,
135
SpacesWithinSizeOfParentheses = false,
136
SpacesWithinTypeOfParentheses = false,
137
SpacesWithinCheckedExpressionParantheses = false,
138
SpaceBeforeConditionalOperatorCondition = true,
139
SpaceAfterConditionalOperatorCondition = true,
140
SpaceBeforeConditionalOperatorSeparator = true,
141
SpaceAfterConditionalOperatorSeparator = true,
143
SpacesWithinBrackets = false,
144
SpacesBeforeBrackets = true,
145
SpaceBeforeBracketComma = false,
146
SpaceAfterBracketComma = true,
148
SpaceBeforeForSemicolon = false,
149
SpaceAfterForSemicolon = true,
150
SpaceAfterTypecast = false,
152
AlignEmbeddedIfStatements = true,
153
AlignEmbeddedUsingStatements = true,
154
PropertyFormatting = PropertyFormatting.AllowOneLine,
155
SpaceBeforeMethodDeclarationParameterComma = false,
156
SpaceAfterMethodDeclarationParameterComma = true,
157
SpaceAfterDelegateDeclarationParameterComma = true,
158
SpaceBeforeFieldDeclarationComma = false,
159
SpaceAfterFieldDeclarationComma = true,
160
SpaceBeforeLocalVariableDeclarationComma = false,
161
SpaceAfterLocalVariableDeclarationComma = true,
163
SpaceBeforeIndexerDeclarationBracket = true,
164
SpaceWithinIndexerDeclarationBracket = false,
165
SpaceBeforeIndexerDeclarationParameterComma = false,
166
SpaceInNamedArgumentAfterDoubleColon = true,
168
SpaceAfterIndexerDeclarationParameterComma = true,
170
BlankLinesBeforeUsings = 0,
171
BlankLinesAfterUsings = 1,
172
UsingPlacement = UsingPlacement.TopOfFile,
174
BlankLinesBeforeFirstDeclaration = 0,
175
BlankLinesBetweenTypes = 1,
176
BlankLinesBetweenFields = 0,
177
BlankLinesBetweenEventFields = 0,
178
BlankLinesBetweenMembers = 1,
179
AlignToFirstIndexerArgument = true,
180
AlignToFirstIndexerDeclarationParameter = true,
181
AlignToFirstMethodCallArgument = true,
182
AlignToFirstMethodDeclarationParameter = true,
183
KeepCommentsAtFirstColumn = true,
184
ChainedMethodCallWrapping = Wrapping.DoNotChange,
185
MethodCallArgumentWrapping = Wrapping.DoNotChange,
186
NewLineAferMethodCallOpenParentheses = NewLinePlacement.DoNotCare,
187
MethodCallClosingParenthesesOnNewLine = NewLinePlacement.DoNotCare,
189
IndexerArgumentWrapping = Wrapping.DoNotChange,
190
NewLineAferIndexerOpenBracket = NewLinePlacement.DoNotCare,
191
IndexerClosingBracketOnNewLine = NewLinePlacement.DoNotCare
196
/// Creates sharp develop indent style CSharpFormatting options.
198
public static CSharpFormattingOptions CreateSharpDevelop()
200
var baseOptions = CreateKRStyle();
205
/// The K&R style, so named because it was used in Kernighan and Ritchie's book The C Programming Language,
206
/// is commonly used in C. It is less common for C++, C#, and others.
208
public static CSharpFormattingOptions CreateKRStyle ()
210
return new CSharpFormattingOptions () {
211
IndentNamespaceBody = true,
212
IndentClassBody = true,
213
IndentInterfaceBody = true,
214
IndentStructBody = true,
215
IndentEnumBody = true,
216
IndentMethodBody = true,
217
IndentPropertyBody = true,
218
IndentEventBody = true,
220
IndentSwitchBody = true,
221
IndentCaseBody = true,
222
IndentBreakStatements = true,
224
NamespaceBraceStyle = BraceStyle.NextLine,
225
ClassBraceStyle = BraceStyle.NextLine,
226
InterfaceBraceStyle = BraceStyle.NextLine,
227
StructBraceStyle = BraceStyle.NextLine,
228
EnumBraceStyle = BraceStyle.NextLine,
229
MethodBraceStyle = BraceStyle.NextLine,
230
ConstructorBraceStyle = BraceStyle.NextLine,
231
DestructorBraceStyle = BraceStyle.NextLine,
232
AnonymousMethodBraceStyle = BraceStyle.EndOfLine,
233
PropertyBraceStyle = BraceStyle.EndOfLine,
234
PropertyGetBraceStyle = BraceStyle.EndOfLine,
235
PropertySetBraceStyle = BraceStyle.EndOfLine,
236
AllowPropertyGetBlockInline = true,
237
AllowPropertySetBlockInline = true,
239
EventBraceStyle = BraceStyle.EndOfLine,
240
EventAddBraceStyle = BraceStyle.EndOfLine,
241
EventRemoveBraceStyle = BraceStyle.EndOfLine,
242
AllowEventAddBlockInline = true,
243
AllowEventRemoveBlockInline = true,
244
StatementBraceStyle = BraceStyle.EndOfLine,
246
ElseNewLinePlacement = NewLinePlacement.SameLine,
247
CatchNewLinePlacement = NewLinePlacement.SameLine,
248
FinallyNewLinePlacement = NewLinePlacement.SameLine,
249
WhileNewLinePlacement = NewLinePlacement.SameLine,
250
ArrayInitializerWrapping = Wrapping.WrapIfTooLong,
251
ArrayInitializerBraceStyle = BraceStyle.EndOfLine,
253
SpaceBeforeMethodCallParentheses = false,
254
SpaceBeforeMethodDeclarationParentheses = false,
255
SpaceBeforeConstructorDeclarationParentheses = false,
256
SpaceBeforeDelegateDeclarationParentheses = false,
257
SpaceBeforeIndexerDeclarationBracket = false,
258
SpaceAfterMethodCallParameterComma = true,
259
SpaceAfterConstructorDeclarationParameterComma = true,
261
SpaceBeforeNewParentheses = false,
262
SpacesWithinNewParentheses = false,
263
SpacesBetweenEmptyNewParentheses = false,
264
SpaceBeforeNewParameterComma = false,
265
SpaceAfterNewParameterComma = true,
267
SpaceBeforeIfParentheses = true,
268
SpaceBeforeWhileParentheses = true,
269
SpaceBeforeForParentheses = true,
270
SpaceBeforeForeachParentheses = true,
271
SpaceBeforeCatchParentheses = true,
272
SpaceBeforeSwitchParentheses = true,
273
SpaceBeforeLockParentheses = true,
274
SpaceBeforeUsingParentheses = true,
276
SpaceAroundAssignment = true,
277
SpaceAroundLogicalOperator = true,
278
SpaceAroundEqualityOperator = true,
279
SpaceAroundRelationalOperator = true,
280
SpaceAroundBitwiseOperator = true,
281
SpaceAroundAdditiveOperator = true,
282
SpaceAroundMultiplicativeOperator = true,
283
SpaceAroundShiftOperator = true,
284
SpaceAroundNullCoalescingOperator = true,
285
SpacesWithinParentheses = false,
286
SpaceWithinMethodCallParentheses = false,
287
SpaceWithinMethodDeclarationParentheses = false,
288
SpacesWithinIfParentheses = false,
289
SpacesWithinWhileParentheses = false,
290
SpacesWithinForParentheses = false,
291
SpacesWithinForeachParentheses = false,
292
SpacesWithinCatchParentheses = false,
293
SpacesWithinSwitchParentheses = false,
294
SpacesWithinLockParentheses = false,
295
SpacesWithinUsingParentheses = false,
296
SpacesWithinCastParentheses = false,
297
SpacesWithinSizeOfParentheses = false,
298
SpacesWithinTypeOfParentheses = false,
299
SpacesWithinCheckedExpressionParantheses = false,
300
SpaceBeforeConditionalOperatorCondition = true,
301
SpaceAfterConditionalOperatorCondition = true,
302
SpaceBeforeConditionalOperatorSeparator = true,
303
SpaceAfterConditionalOperatorSeparator = true,
304
SpaceBeforeArrayDeclarationBrackets = false,
306
SpacesWithinBrackets = false,
307
SpacesBeforeBrackets = false,
308
SpaceBeforeBracketComma = false,
309
SpaceAfterBracketComma = true,
311
SpaceBeforeForSemicolon = false,
312
SpaceAfterForSemicolon = true,
313
SpaceAfterTypecast = false,
315
AlignEmbeddedIfStatements = true,
316
AlignEmbeddedUsingStatements = true,
317
PropertyFormatting = PropertyFormatting.AllowOneLine,
318
SpaceBeforeMethodDeclarationParameterComma = false,
319
SpaceAfterMethodDeclarationParameterComma = true,
320
SpaceAfterDelegateDeclarationParameterComma = true,
321
SpaceBeforeFieldDeclarationComma = false,
322
SpaceAfterFieldDeclarationComma = true,
323
SpaceBeforeLocalVariableDeclarationComma = false,
324
SpaceAfterLocalVariableDeclarationComma = true,
326
SpaceWithinIndexerDeclarationBracket = false,
327
SpaceBeforeIndexerDeclarationParameterComma = false,
328
SpaceInNamedArgumentAfterDoubleColon = true,
330
SpaceAfterIndexerDeclarationParameterComma = true,
332
BlankLinesBeforeUsings = 0,
333
BlankLinesAfterUsings = 1,
335
BlankLinesBeforeFirstDeclaration = 0,
336
BlankLinesBetweenTypes = 1,
337
BlankLinesBetweenFields = 0,
338
BlankLinesBetweenEventFields = 0,
339
BlankLinesBetweenMembers = 1,
341
KeepCommentsAtFirstColumn = true,
342
ChainedMethodCallWrapping = Wrapping.DoNotChange,
343
MethodCallArgumentWrapping = Wrapping.DoNotChange,
344
NewLineAferMethodCallOpenParentheses = NewLinePlacement.DoNotCare,
345
MethodCallClosingParenthesesOnNewLine = NewLinePlacement.DoNotCare,
347
IndexerArgumentWrapping = Wrapping.DoNotChange,
348
NewLineAferIndexerOpenBracket = NewLinePlacement.DoNotCare,
349
IndexerClosingBracketOnNewLine = NewLinePlacement.DoNotCare,
355
/// Creates allman indent style CSharpFormatting options used in Visual Studio.
357
public static CSharpFormattingOptions CreateAllman()
359
var baseOptions = CreateKRStyle();
360
baseOptions.AnonymousMethodBraceStyle = BraceStyle.NextLine;
361
baseOptions.PropertyBraceStyle = BraceStyle.NextLine;
362
baseOptions.PropertyGetBraceStyle = BraceStyle.NextLine;
363
baseOptions.PropertySetBraceStyle = BraceStyle.NextLine;
365
baseOptions.EventBraceStyle = BraceStyle.NextLine;
366
baseOptions.EventAddBraceStyle = BraceStyle.NextLine;
367
baseOptions.EventRemoveBraceStyle = BraceStyle.NextLine;
368
baseOptions.StatementBraceStyle = BraceStyle.NextLine;
369
baseOptions.ArrayInitializerBraceStyle = BraceStyle.NextLine;
371
baseOptions.CatchNewLinePlacement = NewLinePlacement.NewLine;
372
baseOptions.ElseNewLinePlacement = NewLinePlacement.NewLine;
373
baseOptions.FinallyNewLinePlacement = NewLinePlacement.NewLine;
374
baseOptions.WhileNewLinePlacement = NewLinePlacement.DoNotCare;
375
baseOptions.ArrayInitializerWrapping = Wrapping.DoNotChange;
381
/// 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.
383
public static CSharpFormattingOptions CreateWhitesmiths()
385
var baseOptions = CreateKRStyle();
387
baseOptions.NamespaceBraceStyle = BraceStyle.NextLineShifted;
388
baseOptions.ClassBraceStyle = BraceStyle.NextLineShifted;
389
baseOptions.InterfaceBraceStyle = BraceStyle.NextLineShifted;
390
baseOptions.StructBraceStyle = BraceStyle.NextLineShifted;
391
baseOptions.EnumBraceStyle = BraceStyle.NextLineShifted;
392
baseOptions.MethodBraceStyle = BraceStyle.NextLineShifted;
393
baseOptions.ConstructorBraceStyle = BraceStyle.NextLineShifted;
394
baseOptions.DestructorBraceStyle = BraceStyle.NextLineShifted;
395
baseOptions.AnonymousMethodBraceStyle = BraceStyle.NextLineShifted;
396
baseOptions.PropertyBraceStyle = BraceStyle.NextLineShifted;
397
baseOptions.PropertyGetBraceStyle = BraceStyle.NextLineShifted;
398
baseOptions.PropertySetBraceStyle = BraceStyle.NextLineShifted;
400
baseOptions.EventBraceStyle = BraceStyle.NextLineShifted;
401
baseOptions.EventAddBraceStyle = BraceStyle.NextLineShifted;
402
baseOptions.EventRemoveBraceStyle = BraceStyle.NextLineShifted;
403
baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted;
408
/// Like the Allman and Whitesmiths styles, GNU style puts braces on a line by themselves, indented by 2 spaces,
409
/// except when opening a function definition, where they are not indented.
410
/// In either case, the contained code is indented by 2 spaces from the braces.
411
/// Popularised by Richard Stallman, the layout may be influenced by his background of writing Lisp code.
412
/// In Lisp the equivalent to a block (a progn)
413
/// is a first class data entity and giving it its own indent level helps to emphasize that,
414
/// whereas in C a block is just syntax.
415
/// Although not directly related to indentation, GNU coding style also includes a space before the bracketed
416
/// list of arguments to a function.
418
public static CSharpFormattingOptions CreateGNU()
420
var baseOptions = CreateAllman();
421
baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted2;