Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Formatter/FormattingOptionsFactory.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 17.9 KB
Line 
1//
2// FormattingOptionsFactory.cs
3// 
4// Author:
5//       Mike Krüger <mkrueger@xamarin.com>
6//
7// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
8//
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:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
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
25// THE SOFTWARE.
26using System;
27
28namespace ICSharpCode.NRefactory.CSharp
29{
30  /// <summary>
31  /// The formatting options factory creates pre defined formatting option styles.
32  /// </summary>
33  public static class FormattingOptionsFactory
34  {
35    /// <summary>
36    /// Creates empty CSharpFormatting options.
37    /// </summary>
38    public static CSharpFormattingOptions CreateEmpty()
39    {
40      return new CSharpFormattingOptions();
41    }
42
43    /// <summary>
44    /// Creates mono indent style CSharpFormatting options.
45    /// </summary>
46    public static CSharpFormattingOptions CreateMono()
47    {
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,
57        IndentBlocks = true,
58        IndentSwitchBody = false,
59        IndentCaseBody = true,
60        IndentBreakStatements = true,
61        IndentPreprocessorDirectives = true,
62        IndentBlocksInsideExpressions = false,
63        NamespaceBraceStyle = BraceStyle.NextLine,
64        ClassBraceStyle = BraceStyle.NextLine,
65        InterfaceBraceStyle = BraceStyle.NextLine,
66        StructBraceStyle = BraceStyle.NextLine,
67        EnumBraceStyle = BraceStyle.NextLine,
68        MethodBraceStyle = BraceStyle.NextLine,
69        ConstructorBraceStyle = BraceStyle.NextLine,
70        DestructorBraceStyle = BraceStyle.NextLine,
71        AnonymousMethodBraceStyle = BraceStyle.EndOfLine,
72 
73        PropertyBraceStyle = BraceStyle.EndOfLine,
74        PropertyGetBraceStyle = BraceStyle.EndOfLine,
75        PropertySetBraceStyle = BraceStyle.EndOfLine,
76        SimpleGetBlockFormatting = PropertyFormatting.AllowOneLine,
77        SimpleSetBlockFormatting = PropertyFormatting.AllowOneLine,
78
79        EventBraceStyle = BraceStyle.EndOfLine,
80        EventAddBraceStyle = BraceStyle.EndOfLine,
81        EventRemoveBraceStyle = BraceStyle.EndOfLine,
82        AllowEventAddBlockInline = true,
83        AllowEventRemoveBlockInline = true,
84        StatementBraceStyle = BraceStyle.EndOfLine,
85 
86        ElseNewLinePlacement = NewLinePlacement.SameLine,
87        ElseIfNewLinePlacement = NewLinePlacement.SameLine,
88        CatchNewLinePlacement = NewLinePlacement.SameLine,
89        FinallyNewLinePlacement = NewLinePlacement.SameLine,
90        WhileNewLinePlacement = NewLinePlacement.SameLine,
91        ArrayInitializerWrapping = Wrapping.WrapIfTooLong,
92        ArrayInitializerBraceStyle = BraceStyle.EndOfLine,
93        AllowOneLinedArrayInitialziers = true,
94
95        SpaceBeforeMethodCallParentheses = true,
96        SpaceBeforeMethodDeclarationParentheses = true,
97        SpaceBeforeConstructorDeclarationParentheses = true,
98        SpaceBeforeDelegateDeclarationParentheses = true,
99        SpaceAfterMethodCallParameterComma = true,
100        SpaceAfterConstructorDeclarationParameterComma = true,
101
102        SpaceBeforeNewParentheses = true,
103        SpacesWithinNewParentheses = false,
104        SpacesBetweenEmptyNewParentheses = false,
105        SpaceBeforeNewParameterComma = false,
106        SpaceAfterNewParameterComma = true,
107       
108        SpaceBeforeIfParentheses = true,
109        SpaceBeforeWhileParentheses = true,
110        SpaceBeforeForParentheses = true,
111        SpaceBeforeForeachParentheses = true,
112        SpaceBeforeCatchParentheses = true,
113        SpaceBeforeSwitchParentheses = true,
114        SpaceBeforeLockParentheses = true,
115        SpaceBeforeUsingParentheses = true,
116        SpaceAroundAssignment = true,
117        SpaceAroundLogicalOperator = true,
118        SpaceAroundEqualityOperator = true,
119        SpaceAroundRelationalOperator = true,
120        SpaceAroundBitwiseOperator = true,
121        SpaceAroundAdditiveOperator = true,
122        SpaceAroundMultiplicativeOperator = true,
123        SpaceAroundShiftOperator = true,
124        SpaceAroundNullCoalescingOperator = true,
125        SpacesWithinParentheses = false,
126        SpaceWithinMethodCallParentheses = false,
127        SpaceWithinMethodDeclarationParentheses = false,
128        SpacesWithinIfParentheses = false,
129        SpacesWithinWhileParentheses = false,
130        SpacesWithinForParentheses = false,
131        SpacesWithinForeachParentheses = false,
132        SpacesWithinCatchParentheses = false,
133        SpacesWithinSwitchParentheses = false,
134        SpacesWithinLockParentheses = false,
135        SpacesWithinUsingParentheses = false,
136        SpacesWithinCastParentheses = false,
137        SpacesWithinSizeOfParentheses = false,
138        SpacesWithinTypeOfParentheses = false,
139        SpacesWithinCheckedExpressionParantheses = false,
140        SpaceBeforeConditionalOperatorCondition = true,
141        SpaceAfterConditionalOperatorCondition = true,
142        SpaceBeforeConditionalOperatorSeparator = true,
143        SpaceAfterConditionalOperatorSeparator = true,
144 
145        SpacesWithinBrackets = false,
146        SpacesBeforeBrackets = true,
147        SpaceBeforeBracketComma = false,
148        SpaceAfterBracketComma = true,
149           
150        SpaceBeforeForSemicolon = false,
151        SpaceAfterForSemicolon = true,
152        SpaceAfterTypecast = false,
153       
154        AlignEmbeddedStatements = true,
155        SimplePropertyFormatting = PropertyFormatting.AllowOneLine,
156        AutoPropertyFormatting = PropertyFormatting.AllowOneLine,
157        EmptyLineFormatting = EmptyLineFormatting.DoNotIndent,
158        SpaceBeforeMethodDeclarationParameterComma = false,
159        SpaceAfterMethodDeclarationParameterComma = true,
160        SpaceAfterDelegateDeclarationParameterComma = true,
161        SpaceBeforeFieldDeclarationComma = false,
162        SpaceAfterFieldDeclarationComma = true,
163        SpaceBeforeLocalVariableDeclarationComma = false,
164        SpaceAfterLocalVariableDeclarationComma = true,
165       
166        SpaceBeforeIndexerDeclarationBracket = true,
167        SpaceWithinIndexerDeclarationBracket = false,
168        SpaceBeforeIndexerDeclarationParameterComma = false,
169        SpaceInNamedArgumentAfterDoubleColon = true,
170        RemoveEndOfLineWhiteSpace = true,
171     
172        SpaceAfterIndexerDeclarationParameterComma = true,
173       
174        MinimumBlankLinesBeforeUsings = 0,
175        MinimumBlankLinesAfterUsings = 1,
176        UsingPlacement = UsingPlacement.TopOfFile,
177       
178        MinimumBlankLinesBeforeFirstDeclaration = 0,
179        MinimumBlankLinesBetweenTypes = 1,
180        MinimumBlankLinesBetweenFields = 0,
181        MinimumBlankLinesBetweenEventFields = 0,
182        MinimumBlankLinesBetweenMembers = 1,
183        MinimumBlankLinesAroundRegion = 1,
184        MinimumBlankLinesInsideRegion = 1,
185        AlignToFirstIndexerArgument = false,
186        AlignToFirstIndexerDeclarationParameter = true,
187        AlignToFirstMethodCallArgument = false,
188        AlignToFirstMethodDeclarationParameter = true,
189        KeepCommentsAtFirstColumn = true,
190        ChainedMethodCallWrapping = Wrapping.DoNotChange,
191        MethodCallArgumentWrapping = Wrapping.DoNotChange,
192        NewLineAferMethodCallOpenParentheses = NewLinePlacement.DoNotCare,
193        MethodCallClosingParenthesesOnNewLine = NewLinePlacement.DoNotCare,
194
195        IndexerArgumentWrapping = Wrapping.DoNotChange,
196        NewLineAferIndexerOpenBracket = NewLinePlacement.DoNotCare,
197        IndexerClosingBracketOnNewLine = NewLinePlacement.DoNotCare,
198
199        NewLineBeforeNewQueryClause = NewLinePlacement.NewLine
200      };
201    }
202
203    /// <summary>
204    /// Creates sharp develop indent style CSharpFormatting options.
205    /// </summary>
206    public static CSharpFormattingOptions CreateSharpDevelop()
207    {
208      var baseOptions = CreateKRStyle();
209      return baseOptions;
210    }
211
212    /// <summary>
213    /// The K&R style, so named because it was used in Kernighan and Ritchie's book The C Programming Language,
214    /// is commonly used in C. It is less common for C++, C#, and others.
215    /// </summary>
216    public static CSharpFormattingOptions CreateKRStyle()
217    {
218      return new CSharpFormattingOptions() {
219        IndentNamespaceBody = true,
220        IndentClassBody = true,
221        IndentInterfaceBody = true,
222        IndentStructBody = true,
223        IndentEnumBody = true,
224        IndentMethodBody = true,
225        IndentPropertyBody = true,
226        IndentEventBody = true,
227        IndentBlocks = true,
228        IndentSwitchBody = true,
229        IndentCaseBody = true,
230        IndentBreakStatements = true,
231        IndentPreprocessorDirectives = true,
232        NamespaceBraceStyle = BraceStyle.NextLine,
233        ClassBraceStyle = BraceStyle.NextLine,
234        InterfaceBraceStyle = BraceStyle.NextLine,
235        StructBraceStyle = BraceStyle.NextLine,
236        EnumBraceStyle = BraceStyle.NextLine,
237        MethodBraceStyle = BraceStyle.NextLine,
238        ConstructorBraceStyle = BraceStyle.NextLine,
239        DestructorBraceStyle = BraceStyle.NextLine,
240        AnonymousMethodBraceStyle = BraceStyle.EndOfLine,
241        PropertyBraceStyle = BraceStyle.EndOfLine,
242        PropertyGetBraceStyle = BraceStyle.EndOfLine,
243        PropertySetBraceStyle = BraceStyle.EndOfLine,
244        SimpleGetBlockFormatting = PropertyFormatting.AllowOneLine,
245        SimpleSetBlockFormatting = PropertyFormatting.AllowOneLine,
246 
247        EventBraceStyle = BraceStyle.EndOfLine,
248        EventAddBraceStyle = BraceStyle.EndOfLine,
249        EventRemoveBraceStyle = BraceStyle.EndOfLine,
250        AllowEventAddBlockInline = true,
251        AllowEventRemoveBlockInline = true,
252        StatementBraceStyle = BraceStyle.EndOfLine,
253 
254        ElseNewLinePlacement = NewLinePlacement.SameLine,
255        ElseIfNewLinePlacement = NewLinePlacement.SameLine,
256        CatchNewLinePlacement = NewLinePlacement.SameLine,
257        FinallyNewLinePlacement = NewLinePlacement.SameLine,
258        WhileNewLinePlacement = NewLinePlacement.SameLine,
259        ArrayInitializerWrapping = Wrapping.WrapIfTooLong,
260        ArrayInitializerBraceStyle = BraceStyle.EndOfLine,
261 
262        SpaceBeforeMethodCallParentheses = false,
263        SpaceBeforeMethodDeclarationParentheses = false,
264        SpaceBeforeConstructorDeclarationParentheses = false,
265        SpaceBeforeDelegateDeclarationParentheses = false,
266        SpaceBeforeIndexerDeclarationBracket = false,
267        SpaceAfterMethodCallParameterComma = true,
268        SpaceAfterConstructorDeclarationParameterComma = true,
269        NewLineBeforeConstructorInitializerColon = NewLinePlacement.NewLine,
270        NewLineAfterConstructorInitializerColon = NewLinePlacement.SameLine,
271       
272        SpaceBeforeNewParentheses = false,
273        SpacesWithinNewParentheses = false,
274        SpacesBetweenEmptyNewParentheses = false,
275        SpaceBeforeNewParameterComma = false,
276        SpaceAfterNewParameterComma = true,
277       
278        SpaceBeforeIfParentheses = true,
279        SpaceBeforeWhileParentheses = true,
280        SpaceBeforeForParentheses = true,
281        SpaceBeforeForeachParentheses = true,
282        SpaceBeforeCatchParentheses = true,
283        SpaceBeforeSwitchParentheses = true,
284        SpaceBeforeLockParentheses = true,
285        SpaceBeforeUsingParentheses = true,
286
287        SpaceAroundAssignment = true,
288        SpaceAroundLogicalOperator = true,
289        SpaceAroundEqualityOperator = true,
290        SpaceAroundRelationalOperator = true,
291        SpaceAroundBitwiseOperator = true,
292        SpaceAroundAdditiveOperator = true,
293        SpaceAroundMultiplicativeOperator = true,
294        SpaceAroundShiftOperator = true,
295        SpaceAroundNullCoalescingOperator = true,
296        SpacesWithinParentheses = false,
297        SpaceWithinMethodCallParentheses = false,
298        SpaceWithinMethodDeclarationParentheses = false,
299        SpacesWithinIfParentheses = false,
300        SpacesWithinWhileParentheses = false,
301        SpacesWithinForParentheses = false,
302        SpacesWithinForeachParentheses = false,
303        SpacesWithinCatchParentheses = false,
304        SpacesWithinSwitchParentheses = false,
305        SpacesWithinLockParentheses = false,
306        SpacesWithinUsingParentheses = false,
307        SpacesWithinCastParentheses = false,
308        SpacesWithinSizeOfParentheses = false,
309        SpacesWithinTypeOfParentheses = false,
310        SpacesWithinCheckedExpressionParantheses = false,
311        SpaceBeforeConditionalOperatorCondition = true,
312        SpaceAfterConditionalOperatorCondition = true,
313        SpaceBeforeConditionalOperatorSeparator = true,
314        SpaceAfterConditionalOperatorSeparator = true,
315        SpaceBeforeArrayDeclarationBrackets = false,
316
317        SpacesWithinBrackets = false,
318        SpacesBeforeBrackets = false,
319        SpaceBeforeBracketComma = false,
320        SpaceAfterBracketComma = true,
321           
322        SpaceBeforeForSemicolon = false,
323        SpaceAfterForSemicolon = true,
324        SpaceAfterTypecast = false,
325       
326        AlignEmbeddedStatements = true,
327        SimplePropertyFormatting = PropertyFormatting.AllowOneLine,
328        AutoPropertyFormatting = PropertyFormatting.AllowOneLine,
329        EmptyLineFormatting = EmptyLineFormatting.DoNotIndent,
330        SpaceBeforeMethodDeclarationParameterComma = false,
331        SpaceAfterMethodDeclarationParameterComma = true,
332        SpaceAfterDelegateDeclarationParameterComma = true,
333        SpaceBeforeFieldDeclarationComma = false,
334        SpaceAfterFieldDeclarationComma = true,
335        SpaceBeforeLocalVariableDeclarationComma = false,
336        SpaceAfterLocalVariableDeclarationComma = true,
337       
338        SpaceWithinIndexerDeclarationBracket = false,
339        SpaceBeforeIndexerDeclarationParameterComma = false,
340        SpaceInNamedArgumentAfterDoubleColon = true,
341     
342        SpaceAfterIndexerDeclarationParameterComma = true,
343        RemoveEndOfLineWhiteSpace = true,
344       
345        MinimumBlankLinesBeforeUsings = 0,
346        MinimumBlankLinesAfterUsings = 1,
347
348        MinimumBlankLinesBeforeFirstDeclaration = 0,
349        MinimumBlankLinesBetweenTypes = 1,
350        MinimumBlankLinesBetweenFields = 0,
351        MinimumBlankLinesBetweenEventFields = 0,
352        MinimumBlankLinesBetweenMembers = 1,
353        MinimumBlankLinesAroundRegion = 1,
354        MinimumBlankLinesInsideRegion = 1,
355 
356        KeepCommentsAtFirstColumn = true,
357        ChainedMethodCallWrapping = Wrapping.DoNotChange,
358        MethodCallArgumentWrapping = Wrapping.DoNotChange,
359        NewLineAferMethodCallOpenParentheses = NewLinePlacement.DoNotCare,
360        MethodCallClosingParenthesesOnNewLine = NewLinePlacement.DoNotCare,
361
362        IndexerArgumentWrapping = Wrapping.DoNotChange,
363        NewLineAferIndexerOpenBracket = NewLinePlacement.DoNotCare,
364        IndexerClosingBracketOnNewLine = NewLinePlacement.DoNotCare,
365
366        NewLineBeforeNewQueryClause = NewLinePlacement.NewLine
367      };
368    }
369
370    /// <summary>
371    /// Creates allman indent style CSharpFormatting options used in Visual Studio.
372    /// </summary>
373    public static CSharpFormattingOptions CreateAllman()
374    {
375      var baseOptions = CreateKRStyle();
376      baseOptions.AnonymousMethodBraceStyle = BraceStyle.NextLine;
377      baseOptions.PropertyBraceStyle = BraceStyle.NextLine;
378      baseOptions.PropertyGetBraceStyle = BraceStyle.NextLine;
379      baseOptions.PropertySetBraceStyle = BraceStyle.NextLine;
380
381      baseOptions.EventBraceStyle = BraceStyle.NextLine;
382      baseOptions.EventAddBraceStyle = BraceStyle.NextLine;
383      baseOptions.EventRemoveBraceStyle = BraceStyle.NextLine;
384      baseOptions.StatementBraceStyle = BraceStyle.NextLine;
385      baseOptions.ArrayInitializerBraceStyle = BraceStyle.NextLine;
386
387      baseOptions.CatchNewLinePlacement = NewLinePlacement.NewLine;
388      baseOptions.ElseNewLinePlacement = NewLinePlacement.NewLine;
389      baseOptions.ElseIfNewLinePlacement = NewLinePlacement.SameLine;
390
391      baseOptions.FinallyNewLinePlacement = NewLinePlacement.NewLine;
392      baseOptions.WhileNewLinePlacement = NewLinePlacement.DoNotCare;
393      baseOptions.ArrayInitializerWrapping = Wrapping.DoNotChange;
394      baseOptions.IndentBlocksInsideExpressions = true;
395
396      return baseOptions;
397    }
398
399    /// <summary>
400    /// 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.
401    /// </summary>
402    public static CSharpFormattingOptions CreateWhitesmiths()
403    {
404      var baseOptions = CreateKRStyle();
405       
406      baseOptions.NamespaceBraceStyle = BraceStyle.NextLineShifted;
407      baseOptions.ClassBraceStyle = BraceStyle.NextLineShifted;
408      baseOptions.InterfaceBraceStyle = BraceStyle.NextLineShifted;
409      baseOptions.StructBraceStyle = BraceStyle.NextLineShifted;
410      baseOptions.EnumBraceStyle = BraceStyle.NextLineShifted;
411      baseOptions.MethodBraceStyle = BraceStyle.NextLineShifted;
412      baseOptions.ConstructorBraceStyle = BraceStyle.NextLineShifted;
413      baseOptions.DestructorBraceStyle = BraceStyle.NextLineShifted;
414      baseOptions.AnonymousMethodBraceStyle = BraceStyle.NextLineShifted;
415      baseOptions.PropertyBraceStyle = BraceStyle.NextLineShifted;
416      baseOptions.PropertyGetBraceStyle = BraceStyle.NextLineShifted;
417      baseOptions.PropertySetBraceStyle = BraceStyle.NextLineShifted;
418 
419      baseOptions.EventBraceStyle = BraceStyle.NextLineShifted;
420      baseOptions.EventAddBraceStyle = BraceStyle.NextLineShifted;
421      baseOptions.EventRemoveBraceStyle = BraceStyle.NextLineShifted;
422      baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted;
423      baseOptions.IndentBlocksInsideExpressions = true;
424      return baseOptions;
425    }
426
427    /// <summary>
428    /// Like the Allman and Whitesmiths styles, GNU style puts braces on a line by themselves, indented by 2 spaces,
429    /// except when opening a function definition, where they are not indented.
430    /// In either case, the contained code is indented by 2 spaces from the braces.
431    /// Popularised by Richard Stallman, the layout may be influenced by his background of writing Lisp code.
432    /// In Lisp the equivalent to a block (a progn)
433    /// is a first class data entity and giving it its own indent level helps to emphasize that,
434    /// whereas in C a block is just syntax.
435    /// Although not directly related to indentation, GNU coding style also includes a space before the bracketed
436    /// list of arguments to a function.
437    /// </summary>
438    public static CSharpFormattingOptions CreateGNU()
439    {
440      var baseOptions = CreateAllman();
441      baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted2;
442      return baseOptions;
443    }
444  }
445}
446
Note: See TracBrowser for help on using the repository browser.