2
// CSharpFormattingOptions.cs
5
// Mike KrĆ¼ger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.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
using System.Reflection;
31
namespace ICSharpCode.NRefactory.CSharp
33
public enum BraceStyle
37
EndOfLineWithoutSpace,
44
public enum PropertyFormatting
51
public enum Wrapping {
58
public enum NewLinePlacement {
64
public enum UsingPlacement {
69
public class CSharpFormattingOptions
76
public bool IsBuiltIn {
81
public CSharpFormattingOptions Clone ()
83
return (CSharpFormattingOptions)MemberwiseClone ();
87
public bool IndentNamespaceBody { // tested
92
public bool IndentClassBody { // tested
97
public bool IndentInterfaceBody { // tested
102
public bool IndentStructBody { // tested
107
public bool IndentEnumBody { // tested
112
public bool IndentMethodBody { // tested
117
public bool IndentPropertyBody { // tested
122
public bool IndentEventBody { // tested
127
public bool IndentBlocks { // tested
132
public bool IndentSwitchBody { // tested
137
public bool IndentCaseBody { // tested
142
public bool IndentBreakStatements { // tested
147
public bool AlignEmbeddedUsingStatements { // tested
152
public bool AlignEmbeddedIfStatements { // tested
157
public PropertyFormatting PropertyFormatting { // tested
165
public BraceStyle NamespaceBraceStyle { // tested
170
public BraceStyle ClassBraceStyle { // tested
175
public BraceStyle InterfaceBraceStyle { // tested
180
public BraceStyle StructBraceStyle { // tested
185
public BraceStyle EnumBraceStyle { // tested
190
public BraceStyle MethodBraceStyle { // tested
195
public BraceStyle AnonymousMethodBraceStyle {
200
public BraceStyle ConstructorBraceStyle { // tested
205
public BraceStyle DestructorBraceStyle { // tested
210
public BraceStyle PropertyBraceStyle { // tested
215
public BraceStyle PropertyGetBraceStyle { // tested
220
public BraceStyle PropertySetBraceStyle { // tested
225
public bool AllowPropertyGetBlockInline { // tested
230
public bool AllowPropertySetBlockInline { // tested
235
public BraceStyle EventBraceStyle { // tested
240
public BraceStyle EventAddBraceStyle { // tested
245
public BraceStyle EventRemoveBraceStyle { // tested
250
public bool AllowEventAddBlockInline { // tested
255
public bool AllowEventRemoveBlockInline { // tested
260
public BraceStyle StatementBraceStyle { // tested
265
public bool AllowIfBlockInline {
270
bool allowOneLinedArrayInitialziers = true;
271
public bool AllowOneLinedArrayInitialziers {
273
return allowOneLinedArrayInitialziers;
276
allowOneLinedArrayInitialziers = value;
282
public NewLinePlacement ElseNewLinePlacement { // tested
287
public NewLinePlacement ElseIfNewLinePlacement { // tested
292
public NewLinePlacement CatchNewLinePlacement { // tested
297
public NewLinePlacement FinallyNewLinePlacement { // tested
302
public NewLinePlacement WhileNewLinePlacement { // tested
310
public bool SpaceBeforeMethodDeclarationParentheses { // tested
315
public bool SpaceBetweenEmptyMethodDeclarationParentheses {
320
public bool SpaceBeforeMethodDeclarationParameterComma { // tested
325
public bool SpaceAfterMethodDeclarationParameterComma { // tested
330
public bool SpaceWithinMethodDeclarationParentheses { // tested
336
public bool SpaceBeforeMethodCallParentheses { // tested
341
public bool SpaceBetweenEmptyMethodCallParentheses { // tested
346
public bool SpaceBeforeMethodCallParameterComma { // tested
351
public bool SpaceAfterMethodCallParameterComma { // tested
356
public bool SpaceWithinMethodCallParentheses { // tested
363
public bool SpaceBeforeFieldDeclarationComma { // tested
368
public bool SpaceAfterFieldDeclarationComma { // tested
375
public bool SpaceBeforeLocalVariableDeclarationComma { // tested
380
public bool SpaceAfterLocalVariableDeclarationComma { // tested
387
public bool SpaceBeforeConstructorDeclarationParentheses { // tested
392
public bool SpaceBetweenEmptyConstructorDeclarationParentheses { // tested
397
public bool SpaceBeforeConstructorDeclarationParameterComma { // tested
402
public bool SpaceAfterConstructorDeclarationParameterComma { // tested
407
public bool SpaceWithinConstructorDeclarationParentheses { // tested
413
public bool SpaceBeforeIndexerDeclarationBracket { // tested
418
public bool SpaceWithinIndexerDeclarationBracket { // tested
423
public bool SpaceBeforeIndexerDeclarationParameterComma {
428
public bool SpaceAfterIndexerDeclarationParameterComma {
435
public bool SpaceBeforeDelegateDeclarationParentheses {
440
public bool SpaceBetweenEmptyDelegateDeclarationParentheses {
445
public bool SpaceBeforeDelegateDeclarationParameterComma {
450
public bool SpaceAfterDelegateDeclarationParameterComma {
455
public bool SpaceWithinDelegateDeclarationParentheses {
460
public bool SpaceBeforeNewParentheses { // tested
465
public bool SpaceBeforeIfParentheses { // tested
470
public bool SpaceBeforeWhileParentheses { // tested
475
public bool SpaceBeforeForParentheses { // tested
480
public bool SpaceBeforeForeachParentheses { // tested
485
public bool SpaceBeforeCatchParentheses { // tested
490
public bool SpaceBeforeSwitchParentheses { // tested
495
public bool SpaceBeforeLockParentheses { // tested
500
public bool SpaceBeforeUsingParentheses { // tested
505
public bool SpaceAroundAssignment { // tested
510
public bool SpaceAroundLogicalOperator { // tested
515
public bool SpaceAroundEqualityOperator { // tested
520
public bool SpaceAroundRelationalOperator { // tested
525
public bool SpaceAroundBitwiseOperator { // tested
530
public bool SpaceAroundAdditiveOperator { // tested
535
public bool SpaceAroundMultiplicativeOperator { // tested
540
public bool SpaceAroundShiftOperator { // tested
545
public bool SpaceAroundNullCoalescingOperator {
550
public bool SpacesWithinParentheses { // tested
555
public bool SpacesWithinIfParentheses { // tested
560
public bool SpacesWithinWhileParentheses { // tested
565
public bool SpacesWithinForParentheses { // tested
570
public bool SpacesWithinForeachParentheses { // tested
575
public bool SpacesWithinCatchParentheses { // tested
580
public bool SpacesWithinSwitchParentheses { // tested
585
public bool SpacesWithinLockParentheses { // tested
590
public bool SpacesWithinUsingParentheses { // tested
595
public bool SpacesWithinCastParentheses { // tested
600
public bool SpacesWithinSizeOfParentheses { // tested
605
public bool SpaceBeforeSizeOfParentheses { // tested
610
public bool SpacesWithinTypeOfParentheses { // tested
615
public bool SpacesWithinNewParentheses { // tested
620
public bool SpacesBetweenEmptyNewParentheses { // tested
625
public bool SpaceBeforeNewParameterComma { // tested
630
public bool SpaceAfterNewParameterComma { // tested
635
public bool SpaceBeforeTypeOfParentheses { // tested
640
public bool SpacesWithinCheckedExpressionParantheses { // tested
645
public bool SpaceBeforeConditionalOperatorCondition { // tested
650
public bool SpaceAfterConditionalOperatorCondition { // tested
655
public bool SpaceBeforeConditionalOperatorSeparator { // tested
660
public bool SpaceAfterConditionalOperatorSeparator { // tested
666
public bool SpacesWithinBrackets { // tested
671
public bool SpacesBeforeBrackets { // tested
676
public bool SpaceBeforeBracketComma { // tested
681
public bool SpaceAfterBracketComma { // tested
686
public bool SpaceBeforeForSemicolon { // tested
691
public bool SpaceAfterForSemicolon { // tested
696
public bool SpaceAfterTypecast { // tested
701
public bool SpaceBeforeArrayDeclarationBrackets { // tested
706
public bool SpaceInNamedArgumentAfterDoubleColon {
713
public int BlankLinesBeforeUsings {
718
public int BlankLinesAfterUsings {
723
public int BlankLinesBeforeFirstDeclaration {
728
public int BlankLinesBetweenTypes {
733
public int BlankLinesBetweenFields {
738
public int BlankLinesBetweenEventFields {
743
public int BlankLinesBetweenMembers {
751
#region Keep formatting
752
public bool KeepCommentsAtFirstColumn {
760
public Wrapping ArrayInitializerWrapping {
765
public BraceStyle ArrayInitializerBraceStyle {
770
public Wrapping ChainedMethodCallWrapping {
775
public Wrapping MethodCallArgumentWrapping {
780
public NewLinePlacement NewLineAferMethodCallOpenParentheses {
785
public NewLinePlacement MethodCallClosingParenthesesOnNewLine {
790
public Wrapping IndexerArgumentWrapping {
795
public NewLinePlacement NewLineAferIndexerOpenBracket {
800
public NewLinePlacement IndexerClosingBracketOnNewLine {
805
public Wrapping MethodDeclarationParameterWrapping {
810
public NewLinePlacement NewLineAferMethodDeclarationOpenParentheses {
815
public NewLinePlacement MethodDeclarationClosingParenthesesOnNewLine {
820
public Wrapping IndexerDeclarationParameterWrapping {
825
public NewLinePlacement NewLineAferIndexerDeclarationOpenBracket {
830
public NewLinePlacement IndexerDeclarationClosingBracketOnNewLine {
835
public bool AlignToFirstIndexerArgument {
840
public bool AlignToFirstIndexerDeclarationParameter {
845
public bool AlignToFirstMethodCallArgument {
850
public bool AlignToFirstMethodDeclarationParameter {
857
#region Using Declarations
858
public UsingPlacement UsingPlacement {
864
internal CSharpFormattingOptions()
868
/*public static CSharpFormattingOptions Load (FilePath selectedFile)
870
using (var stream = System.IO.File.OpenRead (selectedFile)) {
871
return Load (stream);
875
public static CSharpFormattingOptions Load (System.IO.Stream input)
877
CSharpFormattingOptions result = FormattingOptionsFactory.CreateMonoOptions ();
878
result.Name = "noname";
879
using (XmlTextReader reader = new XmlTextReader (input)) {
880
while (reader.Read ()) {
881
if (reader.NodeType == XmlNodeType.Element) {
882
if (reader.LocalName == "Property") {
883
var info = typeof(CSharpFormattingOptions).GetProperty (reader.GetAttribute ("name"));
884
string valString = reader.GetAttribute ("value");
886
if (info.PropertyType == typeof(bool)) {
887
value = Boolean.Parse (valString);
888
} else if (info.PropertyType == typeof(int)) {
889
value = Int32.Parse (valString);
891
value = Enum.Parse (info.PropertyType, valString);
893
info.SetValue (result, value, null);
894
} else if (reader.LocalName == "FormattingProfile") {
895
result.Name = reader.GetAttribute ("name");
897
} else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "FormattingProfile") {
898
//Console.WriteLine ("result:" + result.Name);
906
public void Save (string fileName)
908
using (var writer = new XmlTextWriter (fileName, Encoding.Default)) {
909
writer.Formatting = System.Xml.Formatting.Indented;
910
writer.Indentation = 1;
911
writer.IndentChar = '\t';
912
writer.WriteStartElement ("FormattingProfile");
913
writer.WriteAttributeString ("name", Name);
914
foreach (PropertyInfo info in typeof (CSharpFormattingOptions).GetProperties ()) {
915
if (info.GetCustomAttributes (false).Any (o => o.GetType () == typeof(ItemPropertyAttribute))) {
916
writer.WriteStartElement ("Property");
917
writer.WriteAttributeString ("name", info.Name);
918
writer.WriteAttributeString ("value", info.GetValue (this, null).ToString ());
919
writer.WriteEndElement ();
922
writer.WriteEndElement ();
926
public bool Equals (CSharpFormattingOptions other)
928
foreach (PropertyInfo info in typeof (CSharpFormattingOptions).GetProperties ()) {
929
if (info.GetCustomAttributes (false).Any (o => o.GetType () == typeof(ItemPropertyAttribute))) {
930
object val = info.GetValue (this, null);
931
object otherVal = info.GetValue (other, null);
933
if (otherVal == null)
937
if (!val.Equals (otherVal)) {
938
//Console.WriteLine ("!equal");
943
//Console.WriteLine ("== equal");