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/CSharpFormattingOptions.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.7 KB
Line 
1//
2// CSharpFormattingOptions.cs
3// 
4// Author:
5//       Mike Krüger <mkrueger@novell.com>
6// 
7// Copyright (c) 2009 Novell, Inc (http://www.novell.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.
26
27using System;
28using System.Reflection;
29using System.Linq;
30
31namespace ICSharpCode.NRefactory.CSharp
32{
33  public enum BraceStyle
34  {
35    DoNotChange,
36    EndOfLine,
37    EndOfLineWithoutSpace,
38    NextLine,
39    NextLineShifted,
40    NextLineShifted2,
41    BannerStyle
42  }
43
44  public enum PropertyFormatting
45  {
46    AllowOneLine,
47    ForceOneLine,
48    ForceNewLine
49  }
50
51  public enum Wrapping {
52    DoNotChange,
53    DoNotWrap,
54    WrapAlways,
55    WrapIfTooLong
56  }
57
58  public enum NewLinePlacement {
59    DoNotCare,
60    NewLine,
61    SameLine
62  }
63
64  public enum UsingPlacement {
65    TopOfFile,
66    InsideNamespace
67  }
68
69  public enum EmptyLineFormatting {
70    DoNotChange,
71    Indent,
72    DoNotIndent
73  }
74
75  public class CSharpFormattingOptions
76  {
77    public string Name {
78      get;
79      set;
80    }
81
82    public bool IsBuiltIn {
83      get;
84      set;
85    }
86
87    public CSharpFormattingOptions Clone ()
88    {
89      return (CSharpFormattingOptions)MemberwiseClone ();
90    }
91
92    #region Indentation
93    public bool IndentNamespaceBody { // tested
94      get;
95      set;
96    }
97
98    public bool IndentClassBody { // tested
99      get;
100      set;
101    }
102
103    public bool IndentInterfaceBody { // tested
104      get;
105      set;
106    }
107
108    public bool IndentStructBody { // tested
109      get;
110      set;
111    }
112
113    public bool IndentEnumBody { // tested
114      get;
115      set;
116    }
117
118    public bool IndentMethodBody { // tested
119      get;
120      set;
121    }
122
123    public bool IndentPropertyBody { // tested
124      get;
125      set;
126    }
127
128    public bool IndentEventBody { // tested
129      get;
130      set;
131    }
132
133    public bool IndentBlocks { // tested
134      get;
135      set;
136    }
137
138    public bool IndentSwitchBody { // tested
139      get;
140      set;
141    }
142
143    public bool IndentCaseBody { // tested
144      get;
145      set;
146    }
147
148    public bool IndentBreakStatements { // tested
149      get;
150      set;
151    }
152
153    public bool AlignEmbeddedStatements { // tested
154      get;
155      set;
156    }
157
158    public bool AlignElseInIfStatements {
159      get;
160      set;
161    }
162
163
164
165    public PropertyFormatting AutoPropertyFormatting { // tested
166      get;
167      set;
168    }
169
170    public PropertyFormatting SimplePropertyFormatting { // tested
171      get;
172      set;
173    }
174
175    public EmptyLineFormatting EmptyLineFormatting {
176      get;
177      set;
178    }
179
180    public bool IndentPreprocessorDirectives { // tested
181      get;
182      set;
183    }
184
185    public bool AlignToMemberReferenceDot { // TODO!
186      get;
187      set;
188    }
189
190    public bool IndentBlocksInsideExpressions {
191      get;
192      set;
193    }
194    #endregion
195   
196    #region Braces
197    public BraceStyle NamespaceBraceStyle { // tested
198      get;
199      set;
200    }
201
202    public BraceStyle ClassBraceStyle { // tested
203      get;
204      set;
205    }
206
207    public BraceStyle InterfaceBraceStyle { // tested
208      get;
209      set;
210    }
211
212    public BraceStyle StructBraceStyle { // tested
213      get;
214      set;
215    }
216
217    public BraceStyle EnumBraceStyle { // tested
218      get;
219      set;
220    }
221
222    public BraceStyle MethodBraceStyle { // tested
223      get;
224      set;
225    }
226
227    public BraceStyle AnonymousMethodBraceStyle {
228      get;
229      set;
230    }
231
232    public BraceStyle ConstructorBraceStyle {  // tested
233      get;
234      set;
235    }
236
237    public BraceStyle DestructorBraceStyle { // tested
238      get;
239      set;
240    }
241
242    public BraceStyle PropertyBraceStyle { // tested
243      get;
244      set;
245    }
246
247    public BraceStyle PropertyGetBraceStyle { // tested
248      get;
249      set;
250    }
251
252    public BraceStyle PropertySetBraceStyle { // tested
253      get;
254      set;
255    }
256
257    public PropertyFormatting SimpleGetBlockFormatting { // tested
258      get;
259      set;
260    }
261
262    public PropertyFormatting SimpleSetBlockFormatting { // tested
263      get;
264      set;
265    }
266
267    public BraceStyle EventBraceStyle { // tested
268      get;
269      set;
270    }
271
272    public BraceStyle EventAddBraceStyle { // tested
273      get;
274      set;
275    }
276
277    public BraceStyle EventRemoveBraceStyle { // tested
278      get;
279      set;
280    }
281
282    public bool AllowEventAddBlockInline { // tested
283      get;
284      set;
285    }
286
287    public bool AllowEventRemoveBlockInline { // tested
288      get;
289      set;
290    }
291
292    public BraceStyle StatementBraceStyle { // tested
293      get;
294      set;
295    }
296
297    public bool AllowIfBlockInline {
298      get;
299      set;
300    }
301
302    bool allowOneLinedArrayInitialziers = true;
303    public bool AllowOneLinedArrayInitialziers {
304      get {
305        return allowOneLinedArrayInitialziers;
306      }
307      set {
308        allowOneLinedArrayInitialziers = value;
309      }
310    }
311    #endregion
312
313    #region NewLines
314    public NewLinePlacement ElseNewLinePlacement { // tested
315      get;
316      set;
317    }
318
319    public NewLinePlacement ElseIfNewLinePlacement { // tested
320      get;
321      set;
322    }
323
324    public NewLinePlacement CatchNewLinePlacement { // tested
325      get;
326      set;
327    }
328
329    public NewLinePlacement FinallyNewLinePlacement { // tested
330      get;
331      set;
332    }
333
334    public NewLinePlacement WhileNewLinePlacement { // tested
335      get;
336      set;
337    }
338
339    NewLinePlacement embeddedStatementPlacement = NewLinePlacement.NewLine;
340    public NewLinePlacement EmbeddedStatementPlacement {
341      get {
342        return embeddedStatementPlacement;
343      }
344      set {
345        embeddedStatementPlacement = value;
346      }
347    }
348    #endregion
349   
350    #region Spaces
351    // Methods
352    public bool SpaceBeforeMethodDeclarationParentheses { // tested
353      get;
354      set;
355    }
356
357    public bool SpaceBetweenEmptyMethodDeclarationParentheses {
358      get;
359      set;
360    }
361
362    public bool SpaceBeforeMethodDeclarationParameterComma { // tested
363      get;
364      set;
365    }
366
367    public bool SpaceAfterMethodDeclarationParameterComma { // tested
368      get;
369      set;
370    }
371
372    public bool SpaceWithinMethodDeclarationParentheses { // tested
373      get;
374      set;
375    }
376   
377    // Method calls
378    public bool SpaceBeforeMethodCallParentheses { // tested
379      get;
380      set;
381    }
382
383    public bool SpaceBetweenEmptyMethodCallParentheses { // tested
384      get;
385      set;
386    }
387
388    public bool SpaceBeforeMethodCallParameterComma { // tested
389      get;
390      set;
391    }
392
393    public bool SpaceAfterMethodCallParameterComma { // tested
394      get;
395      set;
396    }
397
398    public bool SpaceWithinMethodCallParentheses { // tested
399      get;
400      set;
401    }
402   
403    // fields
404   
405    public bool SpaceBeforeFieldDeclarationComma { // tested
406      get;
407      set;
408    }
409
410    public bool SpaceAfterFieldDeclarationComma { // tested
411      get;
412      set;
413    }
414   
415    // local variables
416   
417    public bool SpaceBeforeLocalVariableDeclarationComma { // tested
418      get;
419      set;
420    }
421
422    public bool SpaceAfterLocalVariableDeclarationComma { // tested
423      get;
424      set;
425    }
426   
427    // constructors
428   
429    public bool SpaceBeforeConstructorDeclarationParentheses { // tested
430      get;
431      set;
432    }
433
434    public bool SpaceBetweenEmptyConstructorDeclarationParentheses { // tested
435      get;
436      set;
437    }
438
439    public bool SpaceBeforeConstructorDeclarationParameterComma { // tested
440      get;
441      set;
442    }
443
444    public bool SpaceAfterConstructorDeclarationParameterComma { // tested
445      get;
446      set;
447    }
448
449    public bool SpaceWithinConstructorDeclarationParentheses { // tested
450      get;
451      set;
452    }
453
454    public NewLinePlacement NewLineBeforeConstructorInitializerColon {
455      get;
456      set;
457    }
458
459    public NewLinePlacement NewLineAfterConstructorInitializerColon {
460      get;
461      set;
462    }
463   
464    // indexer
465    public bool SpaceBeforeIndexerDeclarationBracket { // tested
466      get;
467      set;
468    }
469
470    public bool SpaceWithinIndexerDeclarationBracket { // tested
471      get;
472      set;
473    }
474
475    public bool SpaceBeforeIndexerDeclarationParameterComma {
476      get;
477      set;
478    }
479
480    public bool SpaceAfterIndexerDeclarationParameterComma {
481      get;
482      set;
483    }
484   
485    // delegates
486   
487    public bool SpaceBeforeDelegateDeclarationParentheses {
488      get;
489      set;
490    }
491
492    public bool SpaceBetweenEmptyDelegateDeclarationParentheses {
493      get;
494      set;
495    }
496
497    public bool SpaceBeforeDelegateDeclarationParameterComma {
498      get;
499      set;
500    }
501
502    public bool SpaceAfterDelegateDeclarationParameterComma {
503      get;
504      set;
505    }
506
507    public bool SpaceWithinDelegateDeclarationParentheses {
508      get;
509      set;
510    }
511
512    public bool SpaceBeforeNewParentheses { // tested
513      get;
514      set;
515    }
516
517    public bool SpaceBeforeIfParentheses { // tested
518      get;
519      set;
520    }
521
522    public bool SpaceBeforeWhileParentheses { // tested
523      get;
524      set;
525    }
526
527    public bool SpaceBeforeForParentheses { // tested
528      get;
529      set;
530    }
531
532    public bool SpaceBeforeForeachParentheses { // tested
533      get;
534      set;
535    }
536
537    public bool SpaceBeforeCatchParentheses { // tested
538      get;
539      set;
540    }
541
542    public bool SpaceBeforeSwitchParentheses { // tested
543      get;
544      set;
545    }
546
547    public bool SpaceBeforeLockParentheses { // tested
548      get;
549      set;
550    }
551
552    public bool SpaceBeforeUsingParentheses { // tested
553      get;
554      set;
555    }
556
557    public bool SpaceAroundAssignment { // tested
558      get;
559      set;
560    }
561
562    public bool SpaceAroundLogicalOperator { // tested
563      get;
564      set;
565    }
566
567    public bool SpaceAroundEqualityOperator { // tested
568      get;
569      set;
570    }
571
572    public bool SpaceAroundRelationalOperator { // tested
573      get;
574      set;
575    }
576
577    public bool SpaceAroundBitwiseOperator { // tested
578      get;
579      set;
580    }
581
582    public bool SpaceAroundAdditiveOperator { // tested
583      get;
584      set;
585    }
586
587    public bool SpaceAroundMultiplicativeOperator { // tested
588      get;
589      set;
590    }
591
592    public bool SpaceAroundShiftOperator { // tested
593      get;
594      set;
595    }
596
597    public bool SpaceAroundNullCoalescingOperator { // Tested
598      get;
599      set;
600    }
601
602    public bool SpaceAfterUnsafeAddressOfOperator { // Tested
603      get;
604      set;
605    }
606
607    public bool SpaceAfterUnsafeAsteriskOfOperator { // Tested
608      get;
609      set;
610    }
611
612    public bool SpaceAroundUnsafeArrowOperator { // Tested
613      get;
614      set;
615    }
616
617    public bool SpacesWithinParentheses { // tested
618      get;
619      set;
620    }
621
622    public bool SpacesWithinIfParentheses { // tested
623      get;
624      set;
625    }
626
627    public bool SpacesWithinWhileParentheses { // tested
628      get;
629      set;
630    }
631
632    public bool SpacesWithinForParentheses { // tested
633      get;
634      set;
635    }
636
637    public bool SpacesWithinForeachParentheses { // tested
638      get;
639      set;
640    }
641
642    public bool SpacesWithinCatchParentheses { // tested
643      get;
644      set;
645    }
646
647    public bool SpacesWithinSwitchParentheses { // tested
648      get;
649      set;
650    }
651
652    public bool SpacesWithinLockParentheses { // tested
653      get;
654      set;
655    }
656
657    public bool SpacesWithinUsingParentheses { // tested
658      get;
659      set;
660    }
661
662    public bool SpacesWithinCastParentheses { // tested
663      get;
664      set;
665    }
666
667    public bool SpacesWithinSizeOfParentheses { // tested
668      get;
669      set;
670    }
671
672    public bool SpaceBeforeSizeOfParentheses { // tested
673      get;
674      set;
675    }
676
677    public bool SpacesWithinTypeOfParentheses { // tested
678      get;
679      set;
680    }
681
682    public bool SpacesWithinNewParentheses { // tested
683      get;
684      set;
685    }
686
687    public bool SpacesBetweenEmptyNewParentheses { // tested
688      get;
689      set;
690    }
691
692    public bool SpaceBeforeNewParameterComma { // tested
693      get;
694      set;
695    }
696
697    public bool SpaceAfterNewParameterComma { // tested
698      get;
699      set;
700    }
701
702    public bool SpaceBeforeTypeOfParentheses { // tested
703      get;
704      set;
705    }
706
707    public bool SpacesWithinCheckedExpressionParantheses { // tested
708      get;
709      set;
710    }
711
712    public bool SpaceBeforeConditionalOperatorCondition { // tested
713      get;
714      set;
715    }
716
717    public bool SpaceAfterConditionalOperatorCondition { // tested
718      get;
719      set;
720    }
721
722    public bool SpaceBeforeConditionalOperatorSeparator { // tested
723      get;
724      set;
725    }
726
727    public bool SpaceAfterConditionalOperatorSeparator { // tested
728      get;
729      set;
730    }
731   
732    // brackets
733    public bool SpacesWithinBrackets { // tested
734      get;
735      set;
736    }
737
738    public bool SpacesBeforeBrackets { // tested
739      get;
740      set;
741    }
742
743    public bool SpaceBeforeBracketComma { // tested
744      get;
745      set;
746    }
747
748    public bool SpaceAfterBracketComma { // tested
749      get;
750      set;
751    }
752
753    public bool SpaceBeforeForSemicolon { // tested
754      get;
755      set;
756    }
757
758    public bool SpaceAfterForSemicolon { // tested
759      get;
760      set;
761    }
762
763    public bool SpaceAfterTypecast { // tested
764      get;
765      set;
766    }
767
768    public bool SpaceBeforeArrayDeclarationBrackets { // tested
769      get;
770      set;
771    }
772
773    public bool SpaceInNamedArgumentAfterDoubleColon {
774      get;
775      set;
776    }
777
778    public bool RemoveEndOfLineWhiteSpace {
779      get;
780      set;
781    }
782
783    public bool SpaceBeforeSemicolon {
784      get;
785      set;
786    }
787    #endregion
788   
789    #region Blank Lines
790    public int MinimumBlankLinesBeforeUsings {
791      get;
792      set;
793    }
794
795    public int MinimumBlankLinesAfterUsings {
796      get;
797      set;
798    }
799
800    public int MinimumBlankLinesBeforeFirstDeclaration {
801      get;
802      set;
803    }
804
805    public int MinimumBlankLinesBetweenTypes {
806      get;
807      set;
808    }
809
810    public int MinimumBlankLinesBetweenFields {
811      get;
812      set;
813    }
814
815    public int MinimumBlankLinesBetweenEventFields {
816      get;
817      set;
818    }
819
820    public int MinimumBlankLinesBetweenMembers {
821      get;
822      set;
823    }
824
825    public int MinimumBlankLinesAroundRegion {
826      get;
827      set;
828    }
829
830    public int MinimumBlankLinesInsideRegion {
831      get;
832      set;
833    }
834
835    #endregion
836
837
838    #region Keep formatting
839    public bool KeepCommentsAtFirstColumn {
840      get;
841      set;
842    }
843    #endregion
844
845    #region Wrapping
846
847    public Wrapping ArrayInitializerWrapping {
848      get;
849      set;
850    }
851
852    public BraceStyle ArrayInitializerBraceStyle {
853      get;
854      set;
855    }
856
857    public Wrapping ChainedMethodCallWrapping {
858      get;
859      set;
860    }
861
862    public Wrapping MethodCallArgumentWrapping {
863      get;
864      set;
865    }
866
867    public NewLinePlacement NewLineAferMethodCallOpenParentheses {
868      get;
869      set;
870    }
871
872    public NewLinePlacement MethodCallClosingParenthesesOnNewLine {
873      get;
874      set;
875    }
876
877    public Wrapping IndexerArgumentWrapping {
878      get;
879      set;
880    }
881
882    public NewLinePlacement NewLineAferIndexerOpenBracket {
883      get;
884      set;
885    }
886
887    public NewLinePlacement IndexerClosingBracketOnNewLine {
888      get;
889      set;
890    }
891
892    public Wrapping MethodDeclarationParameterWrapping {
893      get;
894      set;
895    }
896
897    public NewLinePlacement NewLineAferMethodDeclarationOpenParentheses {
898      get;
899      set;
900    }
901
902    public NewLinePlacement MethodDeclarationClosingParenthesesOnNewLine {
903      get;
904      set;
905    }
906
907    public Wrapping IndexerDeclarationParameterWrapping {
908      get;
909      set;
910    }
911
912    public NewLinePlacement NewLineAferIndexerDeclarationOpenBracket {
913      get;
914      set;
915    }
916
917    public NewLinePlacement IndexerDeclarationClosingBracketOnNewLine {
918      get;
919      set;
920    }
921
922    public bool AlignToFirstIndexerArgument {
923      get;
924      set;
925    }
926
927    public bool AlignToFirstIndexerDeclarationParameter {
928      get;
929      set;
930    }
931
932    public bool AlignToFirstMethodCallArgument {
933      get;
934      set;
935    }
936
937    public bool AlignToFirstMethodDeclarationParameter {
938      get;
939      set;
940    }
941
942    public NewLinePlacement NewLineBeforeNewQueryClause {
943      get;
944      set;
945    }
946
947    #endregion
948
949    #region Using Declarations
950    public UsingPlacement UsingPlacement {
951      get;
952      set;
953    }
954    #endregion
955
956    internal CSharpFormattingOptions()
957    {
958    }
959
960    /*public static CSharpFormattingOptions Load (FilePath selectedFile)
961    {
962      using (var stream = System.IO.File.OpenRead (selectedFile)) {
963        return Load (stream);
964      }
965    }
966
967    public static CSharpFormattingOptions Load (System.IO.Stream input)
968    {
969      CSharpFormattingOptions result = FormattingOptionsFactory.CreateMonoOptions ();
970      result.Name = "noname";
971      using (XmlTextReader reader = new XmlTextReader (input)) {
972        while (reader.Read ()) {
973          if (reader.NodeType == XmlNodeType.Element) {
974            if (reader.LocalName == "Property") {
975              var info = typeof(CSharpFormattingOptions).GetProperty (reader.GetAttribute ("name"));
976              string valString = reader.GetAttribute ("value");
977              object value;
978              if (info.PropertyType == typeof(bool)) {
979                value = Boolean.Parse (valString);
980              } else if (info.PropertyType == typeof(int)) {
981                value = Int32.Parse (valString);
982              } else {
983                value = Enum.Parse (info.PropertyType, valString);
984              }
985              info.SetValue (result, value, null);
986            } else if (reader.LocalName == "FormattingProfile") {
987              result.Name = reader.GetAttribute ("name");
988            }
989          } else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "FormattingProfile") {
990            //Console.WriteLine ("result:" + result.Name);
991            return result;
992          }
993        }
994      }
995      return result;
996    }
997
998    public void Save (string fileName)
999    {
1000      using (var writer = new XmlTextWriter (fileName, Encoding.Default)) {
1001        writer.Formatting = System.Xml.Formatting.Indented;
1002        writer.Indentation = 1;
1003        writer.IndentChar = '\t';
1004        writer.WriteStartElement ("FormattingProfile");
1005        writer.WriteAttributeString ("name", Name);
1006        foreach (PropertyInfo info in typeof (CSharpFormattingOptions).GetProperties ()) {
1007          if (info.GetCustomAttributes (false).Any (o => o.GetType () == typeof(ItemPropertyAttribute))) {
1008            writer.WriteStartElement ("Property");
1009            writer.WriteAttributeString ("name", info.Name);
1010            writer.WriteAttributeString ("value", info.GetValue (this, null).ToString ());
1011            writer.WriteEndElement ();
1012          }
1013        }
1014        writer.WriteEndElement ();
1015      }
1016    }
1017
1018    public bool Equals (CSharpFormattingOptions other)
1019    {
1020      foreach (PropertyInfo info in typeof (CSharpFormattingOptions).GetProperties ()) {
1021        if (info.GetCustomAttributes (false).Any (o => o.GetType () == typeof(ItemPropertyAttribute))) {
1022          object val = info.GetValue (this, null);
1023          object otherVal = info.GetValue (other, null);
1024          if (val == null) {
1025            if (otherVal == null)
1026              continue;
1027            return false;
1028          }
1029          if (!val.Equals (otherVal)) {
1030            //Console.WriteLine ("!equal");
1031            return false;
1032          }
1033        }
1034      }
1035      //Console.WriteLine ("== equal");
1036      return true;
1037    }*/
1038  }
1039}
Note: See TracBrowser for help on using the repository browser.