Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Formatter/GeneratedCodeSettings.cs

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

#2077: created branch and added first version

File size: 6.8 KB
Line 
1//
2// GeneratedCodeSettings.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.
26
27using System;
28using System.Collections.Generic;
29
30namespace ICSharpCode.NRefactory.CSharp
31{
32  public enum GeneratedCodeMember
33  {
34    Unknown,
35
36    StaticFields,
37    InstanceFields,
38    StaticProperties,
39    InstanceProperties,
40    Indexer,
41    Constructors,
42    StaticMethods,
43    InstanceMethods,
44    StaticEvents,
45    InstanceEvents,
46    Operators,
47    NestedTypes
48  }
49
50  public class GeneratedCodeSettings
51  {
52    List<GeneratedCodeMember> codeMemberOrder;
53
54    public List<GeneratedCodeMember> CodeMemberOrder {
55      get {
56        return codeMemberOrder;
57      }
58      set {
59        codeMemberOrder = value;
60      }
61    }
62
63    public bool GenerateCategoryComments {
64      get;
65      set;
66    }
67
68    public bool SubOrderAlphabetical {
69      get;
70      set;
71    }
72
73    public void Apply (AstNode rootNode)
74    {
75      if (rootNode == null)
76        throw new ArgumentNullException ("rootNode");
77      rootNode.AcceptVisitor (new GenerateCodeVisitior (this));
78    }
79
80    public virtual string GetCategoryLabel(GeneratedCodeMember memberCategory)
81    {
82      switch (memberCategory) {
83        case GeneratedCodeMember.StaticFields:
84          return "Static Fields";
85        case GeneratedCodeMember.InstanceFields:
86          return "Fields";
87        case GeneratedCodeMember.StaticProperties:
88          return "Static Properties";
89        case GeneratedCodeMember.InstanceProperties:
90          return "Properties";
91        case GeneratedCodeMember.Indexer:
92          return "Indexer";
93        case GeneratedCodeMember.Constructors:
94          return "Constructors";
95        case GeneratedCodeMember.StaticMethods:
96          return "Static Methods";
97        case GeneratedCodeMember.InstanceMethods:
98          return "Methods";
99        case GeneratedCodeMember.StaticEvents:
100          return "Static Events";
101        case GeneratedCodeMember.InstanceEvents:
102          return "Events";
103        case GeneratedCodeMember.Operators:
104          return "Operators";
105        case GeneratedCodeMember.NestedTypes:
106          return "Nested Types";
107      }
108      return null;
109    }
110
111    class GenerateCodeVisitior : DepthFirstAstVisitor
112    {
113      GeneratedCodeSettings settings;
114
115      public GenerateCodeVisitior(GeneratedCodeSettings settings)
116      {
117        if (settings == null)
118          throw new ArgumentNullException("settings");
119        this.settings = settings;
120      }
121
122      GeneratedCodeMember GetCodeMemberCategory(EntityDeclaration x)
123      {
124        bool isStatic = x.HasModifier(Modifiers.Static) || x.HasModifier(Modifiers.Const);
125        if (x is FieldDeclaration)
126          return isStatic ? GeneratedCodeMember.StaticFields : GeneratedCodeMember.InstanceFields;
127        if (x is IndexerDeclaration)
128          return GeneratedCodeMember.Indexer;
129        if (x is PropertyDeclaration)
130          return isStatic ? GeneratedCodeMember.StaticProperties : GeneratedCodeMember.InstanceProperties;
131        if (x is ConstructorDeclaration || x is DestructorDeclaration)
132          return GeneratedCodeMember.Constructors;
133        if (x is MethodDeclaration)
134          return isStatic ? GeneratedCodeMember.StaticMethods : GeneratedCodeMember.InstanceMethods;
135        if (x is OperatorDeclaration)
136          return GeneratedCodeMember.Operators;
137        if (x is EventDeclaration || x is CustomEventDeclaration)
138          return isStatic ? GeneratedCodeMember.StaticEvents : GeneratedCodeMember.InstanceEvents;
139
140        if (x is TypeDeclaration)
141          return GeneratedCodeMember.NestedTypes;
142
143        return GeneratedCodeMember.Unknown;
144      }
145
146      public override void VisitTypeDeclaration (TypeDeclaration typeDeclaration)
147      {
148        if (typeDeclaration.ClassType == ClassType.Enum)
149          return;
150        var entities = new List<EntityDeclaration> (typeDeclaration.Members);
151        entities.Sort ((x, y) => {
152          int i1 = settings.CodeMemberOrder.IndexOf (GetCodeMemberCategory (x));
153          int i2 = settings.CodeMemberOrder.IndexOf (GetCodeMemberCategory (y));
154          if (i1 != i2)
155            return i1.CompareTo (i2);
156          if (settings.SubOrderAlphabetical)
157            return (x.Name ?? "").CompareTo ((y.Name ?? ""));
158          return entities.IndexOf (x).CompareTo (entities.IndexOf (y));
159        });
160        typeDeclaration.Members.Clear ();
161        typeDeclaration.Members.AddRange (entities);
162
163        if (settings.GenerateCategoryComments) {
164          var curCat = GeneratedCodeMember.Unknown;
165          foreach (var mem in entities) {
166            if (mem.NextSibling is EntityDeclaration)
167              mem.Parent.InsertChildAfter (mem, new NewLineNode (), Roles.NewLine);
168
169            var cat = GetCodeMemberCategory (mem);
170            if (cat == curCat)
171              continue;
172            curCat = cat;
173            var label = settings.GetCategoryLabel (curCat);
174            if (string.IsNullOrEmpty (label))
175              continue;
176
177            var cmt = new Comment ("", CommentType.SingleLine);
178            var cmt2 = new Comment (" " + label, CommentType.SingleLine);
179            var cmt3 = new Comment ("", CommentType.SingleLine);
180            mem.Parent.InsertChildBefore (mem, cmt, Roles.Comment);
181            mem.Parent.InsertChildBefore (mem, cmt2, Roles.Comment);
182            mem.Parent.InsertChildBefore (mem, cmt3, Roles.Comment);
183            if (cmt.PrevSibling is EntityDeclaration)
184              mem.Parent.InsertChildBefore (cmt, new NewLineNode (), Roles.NewLine);
185          }
186        }
187      }
188    }
189
190    static Lazy<GeneratedCodeSettings> defaultSettings = new Lazy<GeneratedCodeSettings>(
191      () => new GeneratedCodeSettings() {
192        CodeMemberOrder = new List<GeneratedCodeMember>() {
193          GeneratedCodeMember.StaticFields,
194          GeneratedCodeMember.InstanceFields,
195          GeneratedCodeMember.StaticProperties,
196          GeneratedCodeMember.InstanceProperties,
197          GeneratedCodeMember.Indexer,
198          GeneratedCodeMember.Constructors,
199          GeneratedCodeMember.StaticMethods,
200          GeneratedCodeMember.InstanceMethods,
201          GeneratedCodeMember.StaticEvents,
202          GeneratedCodeMember.InstanceEvents,
203          GeneratedCodeMember.Operators,
204          GeneratedCodeMember.NestedTypes
205        },
206        GenerateCategoryComments = true,
207        SubOrderAlphabetical = true
208    });
209
210    public static GeneratedCodeSettings Default {
211      get {
212        return defaultSettings.Value;
213      }
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.