File: FormattingVisitor_Global.cs

package info (click to toggle)
nrefactory 5.3.0%2B20130718.73b6d0f-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 15,720 kB
  • sloc: cs: 296,018; makefile: 24; ansic: 7; sh: 2
file content (302 lines) | stat: -rw-r--r-- 10,261 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// AstFormattingVisitor_Global.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Linq;

namespace ICSharpCode.NRefactory.CSharp
{
	partial class FormattingVisitor : DepthFirstAstVisitor
	{
		int GetGlobalNewLinesFor(AstNode child)
		{
			int newLines = 1;
			var nextSibling = child.GetNextSibling(NoWhitespacePredicate);
			if ((child is UsingDeclaration || child is UsingAliasDeclaration) && !(nextSibling is UsingDeclaration || nextSibling is UsingAliasDeclaration)) {
				newLines += policy.BlankLinesAfterUsings;
			} else if ((child is TypeDeclaration) && (nextSibling is TypeDeclaration)) {
				newLines += policy.BlankLinesBetweenTypes;
			}

			return newLines;
		}

		public override void VisitSyntaxTree(SyntaxTree unit)
		{
			bool first = true;
			VisitChildrenToFormat(unit, child => {
				if (first && (child is UsingDeclaration || child is UsingAliasDeclaration)) {
					EnsureBlankLinesBefore(child, policy.BlankLinesBeforeUsings);
					first = false;
				}
				if (NoWhitespacePredicate(child))
					FixIndentation(child);
				child.AcceptVisitor(this);
				if (NoWhitespacePredicate(child))
					EnsureNewLinesAfter(child, GetGlobalNewLinesFor(child));
			});
		}

		public override void VisitAttributeSection(AttributeSection attributeSection)
		{
			VisitChildrenToFormat(attributeSection, child => {
				child.AcceptVisitor(this);
				if (child.NextSibling != null && child.NextSibling.Role == Roles.RBracket) {
					ForceSpacesAfter(child, false);
				}
			});
		}

		public override void VisitAttribute(Attribute attribute)
		{
			if (attribute.HasArgumentList) {
				ForceSpacesBefore(attribute.LParToken, policy.SpaceBeforeMethodCallParentheses);
				if (attribute.Arguments.Any()) {
					ForceSpacesAfter(attribute.LParToken, policy.SpaceWithinMethodCallParentheses);
				} else {
					ForceSpacesAfter(attribute.LParToken, policy.SpaceBetweenEmptyMethodCallParentheses);
					ForceSpacesBefore(attribute.RParToken, policy.SpaceBetweenEmptyMethodCallParentheses);
				}
				FormatArguments(attribute);
			}
		}

		public override void VisitUsingDeclaration(UsingDeclaration usingDeclaration)
		{
			ForceSpacesAfter(usingDeclaration.UsingToken, true);
			FixSemicolon(usingDeclaration.SemicolonToken);
		}

		public override void VisitUsingAliasDeclaration(UsingAliasDeclaration usingDeclaration)
		{
			ForceSpacesAfter(usingDeclaration.UsingToken, true);
			FixSemicolon(usingDeclaration.SemicolonToken);
		}

		public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration)
		{
			FixOpenBrace(policy.NamespaceBraceStyle, namespaceDeclaration.LBraceToken);
			if (policy.IndentNamespaceBody)
				curIndent.Push(IndentType.Block);

			bool first = true;
			bool startFormat = false;
			VisitChildrenToFormat(namespaceDeclaration, child => {
				if (first) {
					startFormat = child.StartLocation > namespaceDeclaration.LBraceToken.StartLocation;
				}
				if (child.Role == Roles.LBrace) {
					var next = child.GetNextSibling(NoWhitespacePredicate);
					var blankLines = 1;
					if (next is UsingDeclaration || next is UsingAliasDeclaration) {
						blankLines += policy.BlankLinesBeforeUsings;
					} else {
						blankLines += policy.BlankLinesBeforeFirstDeclaration;
					}
					EnsureNewLinesAfter(child, blankLines);
					startFormat = true;
					return;
				}
				if (child.Role == Roles.RBrace) {
					startFormat = false;
					return;
				}
				if (!startFormat || !NoWhitespacePredicate (child))
					return;
				if (first && (child is UsingDeclaration || child is UsingAliasDeclaration)) {
					// TODO: policy.BlankLinesBeforeUsings
					first = false;
				}
				if (NoWhitespacePredicate(child))
					FixIndentationForceNewLine(child);
				child.AcceptVisitor(this);
				if (NoWhitespacePredicate(child))
					EnsureNewLinesAfter(child, GetGlobalNewLinesFor(child));
			});

			if (policy.IndentNamespaceBody)
				curIndent.Pop();

			FixClosingBrace(policy.NamespaceBraceStyle, namespaceDeclaration.RBraceToken);
		}

		void FixAttributesAndDocComment(EntityDeclaration entity)
		{
			var node = entity.FirstChild;
			while (node != null && node.Role == Roles.Comment) {
				node = node.GetNextSibling(NoWhitespacePredicate);
				FixIndentation(node);
			}
			if (entity.Attributes.Count > 0) {
				AstNode n = null;
				foreach (var attr in entity.Attributes.Skip (1)) {
					FixIndentation(attr);
					n = attr;
				}
				if (n != null) {
					FixIndentation(n.GetNextNode(NoWhitespacePredicate));
				} else {
					FixIndentation(entity.Attributes.First().GetNextNode(NoWhitespacePredicate));
				}
			}
		}

		public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
		{
			FixAttributesAndDocComment(typeDeclaration);
			BraceStyle braceStyle;
			bool indentBody = false;
			switch (typeDeclaration.ClassType) {
				case ClassType.Class:
					braceStyle = policy.ClassBraceStyle;
					indentBody = policy.IndentClassBody;
					break;
				case ClassType.Struct:
					braceStyle = policy.StructBraceStyle;
					indentBody = policy.IndentStructBody;
					break;
				case ClassType.Interface:
					braceStyle = policy.InterfaceBraceStyle;
					indentBody = policy.IndentInterfaceBody;
					break;
				case ClassType.Enum:
					braceStyle = policy.EnumBraceStyle;
					indentBody = policy.IndentEnumBody;
					break;
				default:
					throw new InvalidOperationException("unsupported class type : " + typeDeclaration.ClassType);
			}
			
			foreach (var constraint in typeDeclaration.Constraints)
				constraint.AcceptVisitor (this);

			FixOpenBrace(braceStyle, typeDeclaration.LBraceToken);

			if (indentBody)
				curIndent.Push(IndentType.Block);
			bool startFormat = true;
			bool first = true;
			VisitChildrenToFormat(typeDeclaration, child => {
				if (first) {
					startFormat = child.StartLocation > typeDeclaration.LBraceToken.StartLocation;
					first = false;
				}
				if (child.Role == Roles.LBrace) {
					startFormat = true;
					return;
				}
				if (child.Role == Roles.RBrace) {
					startFormat = false;
					return;
				}
				if (!startFormat || !NoWhitespacePredicate (child))
					return;
				if (child.Role == Roles.Comma) {
					ForceSpacesBeforeRemoveNewLines (child, false);
					EnsureNewLinesAfter(child, 1);
					return;
				} 
				if (NoWhitespacePredicate(child))
					FixIndentationForceNewLine(child);
				child.AcceptVisitor(this);
				if (NoWhitespacePredicate(child) && child.GetNextSibling (NoWhitespacePredicate).Role != Roles.Comma)
					EnsureNewLinesAfter(child, GetTypeLevelNewLinesFor(child));
			});

			if (indentBody)
				curIndent.Pop();

			FixClosingBrace(braceStyle, typeDeclaration.RBraceToken);
		}

		int GetTypeLevelNewLinesFor(AstNode child)
		{
			var blankLines = 1;
			var nextSibling = child.GetNextSibling(NoWhitespacePredicate);
			if (child is PreProcessorDirective || child is Comment)
				return 1;
			if (child is EventDeclaration) {
				if (nextSibling is EventDeclaration) {
					blankLines += policy.BlankLinesBetweenEventFields;
					return blankLines;
				}
			}

			if (child is FieldDeclaration || child is FixedFieldDeclaration) {
				if (nextSibling is FieldDeclaration || nextSibling is FixedFieldDeclaration) {
					blankLines += policy.BlankLinesBetweenFields;
					return blankLines;
				}
			}
			
			if (child is TypeDeclaration) {
				if (nextSibling is TypeDeclaration) {
					blankLines += policy.BlankLinesBetweenTypes;
					return blankLines;
				}
			}

			if (nextSibling.Role == Roles.TypeMemberRole)
				blankLines += policy.BlankLinesBetweenMembers;
			return blankLines;
		}

		public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration)
		{
			ForceSpacesBefore(delegateDeclaration.LParToken, policy.SpaceBeforeDelegateDeclarationParentheses);
			if (delegateDeclaration.Parameters.Any()) {
				ForceSpacesAfter(delegateDeclaration.LParToken, policy.SpaceWithinDelegateDeclarationParentheses);
				ForceSpacesBefore(delegateDeclaration.RParToken, policy.SpaceWithinDelegateDeclarationParentheses);
			} else {
				ForceSpacesAfter(delegateDeclaration.LParToken, policy.SpaceBetweenEmptyDelegateDeclarationParentheses);
				ForceSpacesBefore(delegateDeclaration.RParToken, policy.SpaceBetweenEmptyDelegateDeclarationParentheses);
			}
			FormatCommas(delegateDeclaration, policy.SpaceBeforeDelegateDeclarationParameterComma, policy.SpaceAfterDelegateDeclarationParameterComma);

			base.VisitDelegateDeclaration(delegateDeclaration);
		}

		public override void VisitComment(Comment comment)
		{
			if (comment.StartsLine && !HadErrors && (!policy.KeepCommentsAtFirstColumn || comment.StartLocation.Column > 1))
				FixIndentation(comment);
		}

		public override void VisitConstraint(Constraint constraint)
		{
			VisitChildrenToFormat (constraint, node => {
				if (node is AstType) {
					node.AcceptVisitor (this);
				} else if (node.Role == Roles.LPar) {
					ForceSpacesBefore (node, false);
					ForceSpacesAfter (node, false);
				} else if (node.Role ==Roles.Comma) {
					ForceSpacesBefore (node, false);
					ForceSpacesAfter (node, true);
				}
			});
		}
	}
}