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 BraceForcement
51
public enum PropertyFormatting
58
public enum Wrapping {
65
public enum NewLinePlacement {
71
public class CSharpFormattingOptions
78
public bool IsBuiltIn {
83
public CSharpFormattingOptions Clone ()
85
return (CSharpFormattingOptions)MemberwiseClone ();
89
public bool IndentNamespaceBody { // tested
94
public bool IndentClassBody { // tested
99
public bool IndentInterfaceBody { // tested
104
public bool IndentStructBody { // tested
109
public bool IndentEnumBody { // tested
114
public bool IndentMethodBody { // tested
119
public bool IndentPropertyBody { // tested
124
public bool IndentEventBody { // tested
129
public bool IndentBlocks { // tested
134
public bool IndentSwitchBody { // tested
139
public bool IndentCaseBody { // tested
144
public bool IndentBreakStatements { // tested
149
public bool AlignEmbeddedUsingStatements { // tested
154
public bool AlignEmbeddedIfStatements { // tested
159
public PropertyFormatting PropertyFormatting { // tested
167
public BraceStyle NamespaceBraceStyle { // tested
172
public BraceStyle ClassBraceStyle { // tested
177
public BraceStyle InterfaceBraceStyle { // tested
182
public BraceStyle StructBraceStyle { // tested
187
public BraceStyle EnumBraceStyle { // tested
192
public BraceStyle MethodBraceStyle { // tested
197
public BraceStyle AnonymousMethodBraceStyle {
202
public BraceStyle ConstructorBraceStyle { // tested
207
public BraceStyle DestructorBraceStyle { // tested
212
public BraceStyle PropertyBraceStyle { // tested
217
public BraceStyle PropertyGetBraceStyle { // tested
222
public BraceStyle PropertySetBraceStyle { // tested
227
public bool AllowPropertyGetBlockInline { // tested
232
public bool AllowPropertySetBlockInline { // tested
237
public BraceStyle EventBraceStyle { // tested
242
public BraceStyle EventAddBraceStyle { // tested
247
public BraceStyle EventRemoveBraceStyle { // tested
252
public bool AllowEventAddBlockInline { // tested
257
public bool AllowEventRemoveBlockInline { // tested
262
public BraceStyle StatementBraceStyle { // tested
267
public bool AllowIfBlockInline {
275
public BraceForcement IfElseBraceForcement { // tested
280
public BraceForcement ForBraceForcement { // tested
285
public BraceForcement ForEachBraceForcement { // tested
290
public BraceForcement WhileBraceForcement { // tested
295
public BraceForcement UsingBraceForcement { // tested
300
public BraceForcement FixedBraceForcement { // tested
308
public NewLinePlacement ElseNewLinePlacement { // tested
313
public NewLinePlacement ElseIfNewLinePlacement { // tested
318
public NewLinePlacement CatchNewLinePlacement { // tested
323
public NewLinePlacement FinallyNewLinePlacement { // tested
328
public NewLinePlacement WhileNewLinePlacement { // tested
336
public bool SpaceBeforeMethodDeclarationParentheses { // tested
341
public bool SpaceBetweenEmptyMethodDeclarationParentheses {
346
public bool SpaceBeforeMethodDeclarationParameterComma { // tested
351
public bool SpaceAfterMethodDeclarationParameterComma { // tested
356
public bool SpaceWithinMethodDeclarationParentheses { // tested
362
public bool SpaceBeforeMethodCallParentheses { // tested
367
public bool SpaceBetweenEmptyMethodCallParentheses { // tested
372
public bool SpaceBeforeMethodCallParameterComma { // tested
377
public bool SpaceAfterMethodCallParameterComma { // tested
382
public bool SpaceWithinMethodCallParentheses { // tested
389
public bool SpaceBeforeFieldDeclarationComma { // tested
394
public bool SpaceAfterFieldDeclarationComma { // tested
401
public bool SpaceBeforeLocalVariableDeclarationComma { // tested
406
public bool SpaceAfterLocalVariableDeclarationComma { // tested
413
public bool SpaceBeforeConstructorDeclarationParentheses { // tested
418
public bool SpaceBetweenEmptyConstructorDeclarationParentheses { // tested
423
public bool SpaceBeforeConstructorDeclarationParameterComma { // tested
428
public bool SpaceAfterConstructorDeclarationParameterComma { // tested
433
public bool SpaceWithinConstructorDeclarationParentheses { // tested
439
public bool SpaceBeforeIndexerDeclarationBracket { // tested
444
public bool SpaceWithinIndexerDeclarationBracket { // tested
449
public bool SpaceBeforeIndexerDeclarationParameterComma {
454
public bool SpaceAfterIndexerDeclarationParameterComma {
461
public bool SpaceBeforeDelegateDeclarationParentheses {
466
public bool SpaceBetweenEmptyDelegateDeclarationParentheses {
471
public bool SpaceBeforeDelegateDeclarationParameterComma {
476
public bool SpaceAfterDelegateDeclarationParameterComma {
481
public bool SpaceWithinDelegateDeclarationParentheses {
486
public bool SpaceBeforeNewParentheses { // tested
491
public bool SpaceBeforeIfParentheses { // tested
496
public bool SpaceBeforeWhileParentheses { // tested
501
public bool SpaceBeforeForParentheses { // tested
506
public bool SpaceBeforeForeachParentheses { // tested
511
public bool SpaceBeforeCatchParentheses { // tested
516
public bool SpaceBeforeSwitchParentheses { // tested
521
public bool SpaceBeforeLockParentheses { // tested
526
public bool SpaceBeforeUsingParentheses { // tested
531
public bool SpaceAroundAssignment { // tested
536
public bool SpaceAroundLogicalOperator { // tested
541
public bool SpaceAroundEqualityOperator { // tested
546
public bool SpaceAroundRelationalOperator { // tested
551
public bool SpaceAroundBitwiseOperator { // tested
556
public bool SpaceAroundAdditiveOperator { // tested
561
public bool SpaceAroundMultiplicativeOperator { // tested
566
public bool SpaceAroundShiftOperator { // tested
571
public bool SpaceAroundNullCoalescingOperator {
576
public bool SpacesWithinParentheses { // tested
581
public bool SpacesWithinIfParentheses { // tested
586
public bool SpacesWithinWhileParentheses { // tested
591
public bool SpacesWithinForParentheses { // tested
596
public bool SpacesWithinForeachParentheses { // tested
601
public bool SpacesWithinCatchParentheses { // tested
606
public bool SpacesWithinSwitchParentheses { // tested
611
public bool SpacesWithinLockParentheses { // tested
616
public bool SpacesWithinUsingParentheses { // tested
621
public bool SpacesWithinCastParentheses { // tested
626
public bool SpacesWithinSizeOfParentheses { // tested
631
public bool SpaceBeforeSizeOfParentheses { // tested
636
public bool SpacesWithinTypeOfParentheses { // tested
641
public bool SpacesWithinNewParentheses { // tested
646
public bool SpacesBetweenEmptyNewParentheses { // tested
651
public bool SpaceBeforeNewParameterComma { // tested
656
public bool SpaceAfterNewParameterComma { // tested
661
public bool SpaceBeforeTypeOfParentheses { // tested
666
public bool SpacesWithinCheckedExpressionParantheses { // tested
671
public bool SpaceBeforeConditionalOperatorCondition { // tested
676
public bool SpaceAfterConditionalOperatorCondition { // tested
681
public bool SpaceBeforeConditionalOperatorSeparator { // tested
686
public bool SpaceAfterConditionalOperatorSeparator { // tested
692
public bool SpacesWithinBrackets { // tested
697
public bool SpacesBeforeBrackets { // tested
702
public bool SpaceBeforeBracketComma { // tested
707
public bool SpaceAfterBracketComma { // tested
712
public bool SpaceBeforeForSemicolon { // tested
717
public bool SpaceAfterForSemicolon { // tested
722
public bool SpaceAfterTypecast { // tested
727
public bool SpaceBeforeArrayDeclarationBrackets { // tested
732
public bool SpaceInNamedArgumentAfterDoubleColon {
739
public int BlankLinesBeforeUsings {
744
public int BlankLinesAfterUsings {
749
public int BlankLinesBeforeFirstDeclaration {
754
public int BlankLinesBetweenTypes {
759
public int BlankLinesBetweenFields {
764
public int BlankLinesBetweenEventFields {
769
public int BlankLinesBetweenMembers {
777
#region Keep formatting
778
public bool KeepCommentsAtFirstColumn {
786
public Wrapping ArrayInitializerWrapping {
791
public BraceStyle ArrayInitializerBraceStyle {
796
public Wrapping ChainedMethodCallWrapping {
801
public Wrapping MethodCallArgumentWrapping {
806
public bool NewLineAferMethodCallOpenParentheses {
811
public bool MethodCallClosingParenthesesOnNewLine {
816
public Wrapping IndexerArgumentWrapping {
821
public bool NewLineAferIndexerOpenBracket {
826
public bool IndexerClosingBracketOnNewLine {
831
public Wrapping MethodDeclarationParameterWrapping {
836
public bool NewLineAferMethodDeclarationOpenParentheses {
841
public bool MethodDeclarationClosingParenthesesOnNewLine {
846
public Wrapping IndexerDeclarationParameterWrapping {
851
public bool NewLineAferIndexerDeclarationOpenBracket {
856
public bool IndexerDeclarationClosingBracketOnNewLine {
862
internal CSharpFormattingOptions()
866
/*public static CSharpFormattingOptions Load (FilePath selectedFile)
868
using (var stream = System.IO.File.OpenRead (selectedFile)) {
869
return Load (stream);
873
public static CSharpFormattingOptions Load (System.IO.Stream input)
875
CSharpFormattingOptions result = FormattingOptionsFactory.CreateMonoOptions ();
876
result.Name = "noname";
877
using (XmlTextReader reader = new XmlTextReader (input)) {
878
while (reader.Read ()) {
879
if (reader.NodeType == XmlNodeType.Element) {
880
if (reader.LocalName == "Property") {
881
var info = typeof(CSharpFormattingOptions).GetProperty (reader.GetAttribute ("name"));
882
string valString = reader.GetAttribute ("value");
884
if (info.PropertyType == typeof(bool)) {
885
value = Boolean.Parse (valString);
886
} else if (info.PropertyType == typeof(int)) {
887
value = Int32.Parse (valString);
889
value = Enum.Parse (info.PropertyType, valString);
891
info.SetValue (result, value, null);
892
} else if (reader.LocalName == "FormattingProfile") {
893
result.Name = reader.GetAttribute ("name");
895
} else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "FormattingProfile") {
896
//Console.WriteLine ("result:" + result.Name);
904
public void Save (string fileName)
906
using (var writer = new XmlTextWriter (fileName, Encoding.Default)) {
907
writer.Formatting = System.Xml.Formatting.Indented;
908
writer.Indentation = 1;
909
writer.IndentChar = '\t';
910
writer.WriteStartElement ("FormattingProfile");
911
writer.WriteAttributeString ("name", Name);
912
foreach (PropertyInfo info in typeof (CSharpFormattingOptions).GetProperties ()) {
913
if (info.GetCustomAttributes (false).Any (o => o.GetType () == typeof(ItemPropertyAttribute))) {
914
writer.WriteStartElement ("Property");
915
writer.WriteAttributeString ("name", info.Name);
916
writer.WriteAttributeString ("value", info.GetValue (this, null).ToString ());
917
writer.WriteEndElement ();
920
writer.WriteEndElement ();
924
public bool Equals (CSharpFormattingOptions other)
926
foreach (PropertyInfo info in typeof (CSharpFormattingOptions).GetProperties ()) {
927
if (info.GetCustomAttributes (false).Any (o => o.GetType () == typeof(ItemPropertyAttribute))) {
928
object val = info.GetValue (this, null);
929
object otherVal = info.GetValue (other, null);
931
if (otherVal == null)
935
if (!val.Equals (otherVal)) {
936
//Console.WriteLine ("!equal");
941
//Console.WriteLine ("== equal");