Removed
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2002, 2006 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.internal.formatter; |
12 |
|
13 |
import java.util.List; |
14 |
import java.util.Map; |
15 |
|
16 |
import org.eclipse.jdt.core.JavaCore; |
17 |
import org.eclipse.jdt.core.compiler.InvalidInputException; |
18 |
import org.eclipse.jdt.core.dom.*; |
19 |
import org.eclipse.jdt.core.dom.PrefixExpression.Operator; |
20 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
21 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
22 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
23 |
import org.eclipse.jdt.internal.compiler.parser.Scanner; |
24 |
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; |
25 |
import org.eclipse.jdt.internal.formatter.align.Alignment; |
26 |
import org.eclipse.jdt.internal.formatter.align.Alignment2; |
27 |
import org.eclipse.jdt.internal.formatter.align.AlignmentException; |
28 |
import org.eclipse.text.edits.TextEdit; |
29 |
|
30 |
/** |
31 |
* This class is responsible for formatting a valid java source code. |
32 |
* @since 3.2 |
33 |
*/ |
34 |
public class CodeFormatterVisitor2 extends ASTVisitor { |
35 |
public final static boolean DEBUG = false; |
36 |
|
37 |
private static final int[] CLOSING_GENERICS_EXPECTEDTOKENS = new int[] { |
38 |
TerminalTokens.TokenNameRIGHT_SHIFT, |
39 |
TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT, |
40 |
TerminalTokens.TokenNameGREATER |
41 |
}; |
42 |
private static final int[] NUMBER_LITERALS_EXPECTEDTOKENS = new int[] { |
43 |
TerminalTokens.TokenNameIntegerLiteral, |
44 |
TerminalTokens.TokenNameLongLiteral, |
45 |
TerminalTokens.TokenNameFloatingPointLiteral, |
46 |
TerminalTokens.TokenNameDoubleLiteral |
47 |
}; |
48 |
/* |
49 |
* Set of expected tokens type for a single type reference. |
50 |
* This array needs to be SORTED. |
51 |
*/ |
52 |
private static final int[] PRIMITIVE_TYPE_EXPECTEDTOKENS = new int[] { |
53 |
TerminalTokens.TokenNameboolean, |
54 |
TerminalTokens.TokenNamebyte, |
55 |
TerminalTokens.TokenNamechar, |
56 |
TerminalTokens.TokenNamedouble, |
57 |
TerminalTokens.TokenNamefloat, |
58 |
TerminalTokens.TokenNameint, |
59 |
TerminalTokens.TokenNamelong, |
60 |
TerminalTokens.TokenNameshort, |
61 |
TerminalTokens.TokenNamevoid |
62 |
}; |
63 |
private Scanner localScanner; |
64 |
|
65 |
public DefaultCodeFormatterOptions preferences; |
66 |
|
67 |
public Scribe2 scribe; |
68 |
|
69 |
public CodeFormatterVisitor2(DefaultCodeFormatterOptions preferences, Map settings, int offset, int length, CompilationUnit unit) { |
70 |
long sourceLevel = settings == null |
71 |
? ClassFileConstants.JDK1_3 |
72 |
: CompilerOptions.versionToJdkLevel(settings.get(JavaCore.COMPILER_SOURCE)); |
73 |
this.localScanner = new Scanner(true, false, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
74 |
|
75 |
this.preferences = preferences; |
76 |
this.scribe = new Scribe2(this, sourceLevel, offset, length, unit); |
77 |
} |
78 |
|
79 |
private boolean commentStartsBlock(int start, int end) { |
80 |
this.localScanner.resetTo(start, end); |
81 |
try { |
82 |
if (this.localScanner.getNextToken() == TerminalTokens.TokenNameLBRACE) { |
83 |
switch(this.localScanner.getNextToken()) { |
84 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
85 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
86 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
87 |
return true; |
88 |
} |
89 |
} |
90 |
} catch(InvalidInputException e) { |
91 |
// ignore |
92 |
} |
93 |
return false; |
94 |
} |
95 |
|
96 |
|
97 |
public static int extractInfixExpressionOperator(InfixExpression node) { |
98 |
final InfixExpression.Operator infixOperator = node.getOperator(); |
99 |
if (infixOperator == InfixExpression.Operator.AND) { |
100 |
return TerminalTokens.TokenNameAND; |
101 |
} else if (infixOperator == InfixExpression.Operator.CONDITIONAL_AND) { |
102 |
return TerminalTokens.TokenNameAND_AND; |
103 |
} else if (infixOperator == InfixExpression.Operator.CONDITIONAL_OR) { |
104 |
return TerminalTokens.TokenNameOR_OR; |
105 |
} else if (infixOperator == InfixExpression.Operator.DIVIDE) { |
106 |
return TerminalTokens.TokenNameDIVIDE; |
107 |
} else if (infixOperator == InfixExpression.Operator.EQUALS) { |
108 |
return TerminalTokens.TokenNameEQUAL_EQUAL; |
109 |
} else if (infixOperator == InfixExpression.Operator.GREATER) { |
110 |
return TerminalTokens.TokenNameGREATER; |
111 |
} else if (infixOperator == InfixExpression.Operator.GREATER_EQUALS) { |
112 |
return TerminalTokens.TokenNameGREATER_EQUAL; |
113 |
} else if (infixOperator == InfixExpression.Operator.LEFT_SHIFT) { |
114 |
return TerminalTokens.TokenNameLEFT_SHIFT; |
115 |
} else if (infixOperator == InfixExpression.Operator.LESS) { |
116 |
return TerminalTokens.TokenNameLESS; |
117 |
} else if (infixOperator == InfixExpression.Operator.LESS_EQUALS) { |
118 |
return TerminalTokens.TokenNameLESS_EQUAL; |
119 |
} else if (infixOperator == InfixExpression.Operator.MINUS) { |
120 |
return TerminalTokens.TokenNameMINUS; |
121 |
} else if (infixOperator == InfixExpression.Operator.NOT_EQUALS) { |
122 |
return TerminalTokens.TokenNameNOT_EQUAL; |
123 |
} else if (infixOperator == InfixExpression.Operator.OR) { |
124 |
return TerminalTokens.TokenNameOR; |
125 |
} else if (infixOperator == InfixExpression.Operator.PLUS) { |
126 |
return TerminalTokens.TokenNamePLUS; |
127 |
} else if (infixOperator == InfixExpression.Operator.REMAINDER) { |
128 |
return TerminalTokens.TokenNameREMAINDER; |
129 |
} else if (infixOperator == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) { |
130 |
return TerminalTokens.TokenNameRIGHT_SHIFT; |
131 |
} else if (infixOperator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) { |
132 |
return TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT; |
133 |
} else if (infixOperator == InfixExpression.Operator.TIMES) { |
134 |
return TerminalTokens.TokenNameMULTIPLY; |
135 |
} else { |
136 |
return TerminalTokens.TokenNameXOR; |
137 |
} |
138 |
} |
139 |
|
140 |
private final TextEdit failedToFormat() { |
141 |
if (DEBUG) { |
142 |
System.out.println("COULD NOT FORMAT \n" + this.scribe.scanner); //$NON-NLS-1$ |
143 |
System.out.println(this.scribe); |
144 |
} |
145 |
return null; |
146 |
} |
147 |
|
148 |
/** |
149 |
* @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String) |
150 |
*/ |
151 |
public TextEdit format(String string, AbstractTypeDeclaration typeDeclaration) { |
152 |
// reset the scribe |
153 |
this.scribe.reset(); |
154 |
|
155 |
long startTime = System.currentTimeMillis(); |
156 |
|
157 |
final char[] compilationUnitSource = string.toCharArray(); |
158 |
|
159 |
this.localScanner.setSource(compilationUnitSource); |
160 |
this.scribe.initializeScanner(compilationUnitSource); |
161 |
|
162 |
try { |
163 |
this.scribe.lastNumberOfNewLines = 1; |
164 |
formatTypeMembers(typeDeclaration.bodyDeclarations(), false); |
165 |
} catch(AbortFormatting e){ |
166 |
return failedToFormat(); |
167 |
} |
168 |
if (DEBUG){ |
169 |
System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$ |
170 |
} |
171 |
return this.scribe.getRootEdit(); |
172 |
} |
173 |
|
174 |
/** |
175 |
* @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String) |
176 |
*/ |
177 |
public TextEdit format(String string, Block block) { |
178 |
// reset the scribe |
179 |
this.scribe.reset(); |
180 |
|
181 |
long startTime = System.currentTimeMillis(); |
182 |
|
183 |
final char[] compilationUnitSource = string.toCharArray(); |
184 |
|
185 |
this.localScanner.setSource(compilationUnitSource); |
186 |
this.scribe.initializeScanner(compilationUnitSource); |
187 |
|
188 |
if ((block.getFlags() & ASTNode.MALFORMED) != 0) { |
189 |
return failedToFormat(); |
190 |
} |
191 |
|
192 |
try { |
193 |
formatStatements(block.statements(), false); |
194 |
if (hasComments()) { |
195 |
this.scribe.printNewLine(); |
196 |
} |
197 |
this.scribe.printComment(); |
198 |
} catch(AbortFormatting e){ |
199 |
return failedToFormat(); |
200 |
} |
201 |
if (DEBUG){ |
202 |
System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$ |
203 |
} |
204 |
return this.scribe.getRootEdit(); |
205 |
} |
206 |
|
207 |
/** |
208 |
* @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String) |
209 |
*/ |
210 |
public TextEdit format(String string, CompilationUnit compilationUnit) { |
211 |
// reset the scribe |
212 |
this.scribe.reset(); |
213 |
|
214 |
if ((compilationUnit.getFlags() & ASTNode.MALFORMED) != 0) { |
215 |
return failedToFormat(); |
216 |
} |
217 |
|
218 |
long startTime = System.currentTimeMillis(); |
219 |
|
220 |
final char[] compilationUnitSource = string.toCharArray(); |
221 |
|
222 |
this.localScanner.setSource(compilationUnitSource); |
223 |
this.scribe.initializeScanner(compilationUnitSource); |
224 |
|
225 |
try { |
226 |
compilationUnit.accept(this); |
227 |
} catch(AbortFormatting e){ |
228 |
return failedToFormat(); |
229 |
} |
230 |
if (DEBUG){ |
231 |
System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$ |
232 |
} |
233 |
return this.scribe.getRootEdit(); |
234 |
} |
235 |
|
236 |
/** |
237 |
* @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String) |
238 |
*/ |
239 |
public TextEdit format(String string, Expression expression) { |
240 |
// reset the scribe |
241 |
this.scribe.reset(); |
242 |
|
243 |
long startTime = System.currentTimeMillis(); |
244 |
|
245 |
final char[] compilationUnitSource = string.toCharArray(); |
246 |
|
247 |
this.localScanner.setSource(compilationUnitSource); |
248 |
this.scribe.initializeScanner(compilationUnitSource); |
249 |
|
250 |
if ((expression.getFlags() & ASTNode.MALFORMED) != 0) { |
251 |
return failedToFormat(); |
252 |
} |
253 |
try { |
254 |
expression.accept(this); |
255 |
this.scribe.printComment(); |
256 |
} catch(AbortFormatting e){ |
257 |
return failedToFormat(); |
258 |
} |
259 |
if (DEBUG){ |
260 |
System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$ |
261 |
} |
262 |
return this.scribe.getRootEdit(); |
263 |
} |
264 |
|
265 |
private void format( |
266 |
AbstractTypeDeclaration memberTypeDeclaration, |
267 |
boolean isChunkStart, |
268 |
boolean isFirstClassBodyDeclaration) { |
269 |
|
270 |
if (isFirstClassBodyDeclaration) { |
271 |
int newLinesBeforeFirstClassBodyDeclaration = this.preferences.blank_lines_before_first_class_body_declaration; |
272 |
if (newLinesBeforeFirstClassBodyDeclaration > 0) { |
273 |
this.scribe.printEmptyLines(newLinesBeforeFirstClassBodyDeclaration); |
274 |
} |
275 |
} else { |
276 |
int newLineBeforeChunk = isChunkStart ? this.preferences.blank_lines_before_new_chunk : 0; |
277 |
if (newLineBeforeChunk > 0) { |
278 |
this.scribe.printEmptyLines(newLineBeforeChunk); |
279 |
} |
280 |
final int newLinesBeforeMember = this.preferences.blank_lines_before_member_type; |
281 |
if (newLinesBeforeMember > 0) { |
282 |
this.scribe.printEmptyLines(newLinesBeforeMember); |
283 |
} |
284 |
} |
285 |
memberTypeDeclaration.accept(this); |
286 |
} |
287 |
|
288 |
private void format(FieldDeclaration fieldDeclaration, boolean isChunkStart, boolean isFirstClassBodyDeclaration) { |
289 |
if (isFirstClassBodyDeclaration) { |
290 |
int newLinesBeforeFirstClassBodyDeclaration = this.preferences.blank_lines_before_first_class_body_declaration; |
291 |
if (newLinesBeforeFirstClassBodyDeclaration > 0) { |
292 |
this.scribe.printEmptyLines(newLinesBeforeFirstClassBodyDeclaration); |
293 |
} |
294 |
} else { |
295 |
int newLineBeforeChunk = isChunkStart ? this.preferences.blank_lines_before_new_chunk : 0; |
296 |
if (newLineBeforeChunk > 0) { |
297 |
this.scribe.printEmptyLines(newLineBeforeChunk); |
298 |
} |
299 |
final int newLinesBeforeField = this.preferences.blank_lines_before_field; |
300 |
if (newLinesBeforeField > 0) { |
301 |
this.scribe.printEmptyLines(newLinesBeforeField); |
302 |
} |
303 |
} |
304 |
Alignment2 memberAlignment = this.scribe.getMemberAlignment(); |
305 |
|
306 |
this.scribe.printComment(); |
307 |
final List modifiers = fieldDeclaration.modifiers(); |
308 |
if (modifiers.size() != 0) { |
309 |
this.scribe.printModifiers(modifiers, this); |
310 |
this.scribe.space(); |
311 |
} |
312 |
|
313 |
fieldDeclaration.getType().accept(this); |
314 |
|
315 |
List fragments = fieldDeclaration.fragments(); |
316 |
final int fragmentsLength = fragments.size(); |
317 |
if (fragmentsLength > 1) { |
318 |
// multiple field declaration |
319 |
Alignment2 multiFieldDeclarationsAlignment =this.scribe.createAlignment( |
320 |
"multiple_field",//$NON-NLS-1$ |
321 |
this.preferences.alignment_for_multiple_fields, |
322 |
fragmentsLength - 1, |
323 |
this.scribe.scanner.currentPosition); |
324 |
this.scribe.enterAlignment(multiFieldDeclarationsAlignment); |
325 |
|
326 |
boolean ok = false; |
327 |
do { |
328 |
try { |
329 |
for (int i = 0; i < fragmentsLength; i++) { |
330 |
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(i); |
331 |
/* |
332 |
* Field name |
333 |
*/ |
334 |
if (i == 0) { |
335 |
this.scribe.alignFragment(memberAlignment, 0); |
336 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
337 |
} else { |
338 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, false); |
339 |
} |
340 |
|
341 |
/* |
342 |
* Check for extra dimensions |
343 |
*/ |
344 |
final int extraDimensions = fragment.getExtraDimensions(); |
345 |
if (extraDimensions != 0) { |
346 |
for (int index = 0; index < extraDimensions; index++) { |
347 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
348 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
349 |
} |
350 |
} |
351 |
|
352 |
/* |
353 |
* Field initialization |
354 |
*/ |
355 |
final Expression initialization = fragment.getInitializer(); |
356 |
if (initialization != null) { |
357 |
if (i == 0) { |
358 |
this.scribe.alignFragment(memberAlignment, 1); |
359 |
} |
360 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
361 |
if (this.preferences.insert_space_after_assignment_operator) { |
362 |
this.scribe.space(); |
363 |
} |
364 |
Alignment2 assignmentAlignment = this.scribe.createAlignment("fieldDeclarationAssignmentAlignment", this.preferences.alignment_for_assignment, 1, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
365 |
this.scribe.enterAlignment(assignmentAlignment); |
366 |
boolean ok2 = false; |
367 |
do { |
368 |
try { |
369 |
this.scribe.alignFragment(assignmentAlignment, 0); |
370 |
initialization.accept(this); |
371 |
ok2 = true; |
372 |
} catch(AlignmentException e){ |
373 |
this.scribe.redoAlignment(e); |
374 |
} |
375 |
} while (!ok2); |
376 |
this.scribe.exitAlignment(assignmentAlignment, true); |
377 |
} |
378 |
|
379 |
if (i != fragmentsLength - 1) { |
380 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_multiple_field_declarations); |
381 |
this.scribe.printTrailingComment(); |
382 |
this.scribe.alignFragment(multiFieldDeclarationsAlignment, i); |
383 |
|
384 |
if (this.preferences.insert_space_after_comma_in_multiple_field_declarations) { |
385 |
this.scribe.space(); |
386 |
} |
387 |
} else { |
388 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
389 |
this.scribe.alignFragment(memberAlignment, 2); |
390 |
this.scribe.printTrailingComment(); |
391 |
} |
392 |
} |
393 |
ok = true; |
394 |
} catch (AlignmentException e) { |
395 |
this.scribe.redoAlignment(e); |
396 |
} |
397 |
} while (!ok); |
398 |
this.scribe.exitAlignment(multiFieldDeclarationsAlignment, true); |
399 |
} else { |
400 |
// single field declaration |
401 |
this.scribe.alignFragment(memberAlignment, 0); |
402 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
403 |
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(0); |
404 |
final int extraDimensions = fragment.getExtraDimensions(); |
405 |
if (extraDimensions != 0) { |
406 |
for (int i = 0; i < extraDimensions; i++) { |
407 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
408 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
409 |
} |
410 |
} |
411 |
final Expression initialization = fragment.getInitializer(); |
412 |
if (initialization != null) { |
413 |
this.scribe.alignFragment(memberAlignment, 1); |
414 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
415 |
if (this.preferences.insert_space_after_assignment_operator) { |
416 |
this.scribe.space(); |
417 |
} |
418 |
Alignment2 assignmentAlignment = this.scribe.createAlignment("fieldDeclarationAssignmentAlignment", this.preferences.alignment_for_assignment, 1, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
419 |
this.scribe.enterAlignment(assignmentAlignment); |
420 |
boolean ok = false; |
421 |
do { |
422 |
try { |
423 |
this.scribe.alignFragment(assignmentAlignment, 0); |
424 |
initialization.accept(this); |
425 |
ok = true; |
426 |
} catch(AlignmentException e){ |
427 |
this.scribe.redoAlignment(e); |
428 |
} |
429 |
} while (!ok); |
430 |
this.scribe.exitAlignment(assignmentAlignment, true); |
431 |
} |
432 |
|
433 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
434 |
|
435 |
if (memberAlignment != null) { |
436 |
this.scribe.alignFragment(memberAlignment, 2); |
437 |
this.scribe.printTrailingComment(); |
438 |
} else { |
439 |
this.scribe.space(); |
440 |
this.scribe.printTrailingComment(); |
441 |
} |
442 |
} |
443 |
} |
444 |
private void format( |
445 |
BodyDeclaration bodyDeclaration, |
446 |
boolean isChunkStart, |
447 |
boolean isFirstClassBodyDeclaration) { |
448 |
|
449 |
if (isFirstClassBodyDeclaration) { |
450 |
int newLinesBeforeFirstClassBodyDeclaration = this.preferences.blank_lines_before_first_class_body_declaration; |
451 |
if (newLinesBeforeFirstClassBodyDeclaration > 0) { |
452 |
this.scribe.printEmptyLines(newLinesBeforeFirstClassBodyDeclaration); |
453 |
} |
454 |
} else { |
455 |
final int newLineBeforeChunk = isChunkStart ? this.preferences.blank_lines_before_new_chunk : 0; |
456 |
if (newLineBeforeChunk > 0) { |
457 |
this.scribe.printEmptyLines(newLineBeforeChunk); |
458 |
} |
459 |
} |
460 |
final int newLinesBeforeMethod = this.preferences.blank_lines_before_method; |
461 |
if (newLinesBeforeMethod > 0 && !isFirstClassBodyDeclaration) { |
462 |
this.scribe.printEmptyLines(newLinesBeforeMethod); |
463 |
} else if (this.scribe.line != 0 || this.scribe.column != 1) { |
464 |
this.scribe.printNewLine(); |
465 |
} |
466 |
bodyDeclaration.accept(this); |
467 |
} |
468 |
|
469 |
private void formatAction(final int line, final Statement action, boolean insertLineForSingleStatement) { |
470 |
if (action != null) { |
471 |
switch(action.getNodeType()) { |
472 |
case ASTNode.BLOCK : |
473 |
formatLeftCurlyBrace(line, this.preferences.brace_position_for_block); |
474 |
action.accept(this); |
475 |
break; |
476 |
case ASTNode.EMPTY_STATEMENT : |
477 |
this.scribe.indent(); |
478 |
action.accept(this); |
479 |
this.scribe.unIndent(); |
480 |
break; |
481 |
default : |
482 |
this.scribe.printNewLine(); |
483 |
this.scribe.indent(); |
484 |
action.accept(this); |
485 |
this.scribe.unIndent(); |
486 |
if (insertLineForSingleStatement) { |
487 |
this.scribe.printNewLine(); |
488 |
} |
489 |
} |
490 |
} |
491 |
} |
492 |
|
493 |
private void formatBlock(Block block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace) { |
494 |
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); |
495 |
final List statements = block.statements(); |
496 |
final int statementsLength = statements.size(); |
497 |
if (statementsLength != 0) { |
498 |
this.scribe.printNewLine(); |
499 |
if (this.preferences.indent_statements_compare_to_block) { |
500 |
this.scribe.indent(); |
501 |
} |
502 |
formatStatements(statements, true); |
503 |
this.scribe.printComment(); |
504 |
|
505 |
if (this.preferences.indent_statements_compare_to_block) { |
506 |
this.scribe.unIndent(); |
507 |
} |
508 |
} else { |
509 |
if (this.preferences.insert_new_line_in_empty_block) { |
510 |
this.scribe.printNewLine(); |
511 |
} |
512 |
if (this.preferences.indent_statements_compare_to_block) { |
513 |
this.scribe.indent(); |
514 |
} |
515 |
this.scribe.printComment(); |
516 |
|
517 |
if (this.preferences.indent_statements_compare_to_block) { |
518 |
this.scribe.unIndent(); |
519 |
} |
520 |
} |
521 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
522 |
this.scribe.printTrailingComment(); |
523 |
if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) { |
524 |
this.scribe.unIndent(); |
525 |
} |
526 |
} |
527 |
|
528 |
private void formatEmptyTypeDeclaration(boolean isFirst) { |
529 |
boolean hasSemiColon = isNextToken(TerminalTokens.TokenNameSEMICOLON); |
530 |
while(isNextToken(TerminalTokens.TokenNameSEMICOLON)) { |
531 |
this.scribe.printComment(); |
532 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
533 |
this.scribe.printTrailingComment(); |
534 |
} |
535 |
if (hasSemiColon && isFirst) { |
536 |
this.scribe.printNewLine(); |
537 |
} |
538 |
} |
539 |
|
540 |
|
541 |
private void formatLeftCurlyBrace(final int line, final String bracePosition) { |
542 |
/* |
543 |
* deal with (quite unexpected) comments right before lcurly |
544 |
*/ |
545 |
this.scribe.printComment(); |
546 |
if (DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP.equals(bracePosition) |
547 |
&& (this.scribe.line > line || this.scribe.column >= this.preferences.page_width)) |
548 |
{ |
549 |
this.scribe.printNewLine(); |
550 |
} |
551 |
} |
552 |
|
553 |
private void formatLocalDeclaration(VariableDeclarationExpression declarationExpression, boolean insertSpaceBeforeComma, boolean insertSpaceAfterComma) { |
554 |
final List modifiers = declarationExpression.modifiers(); |
555 |
if (modifiers.size() != 0) { |
556 |
this.scribe.printModifiers(modifiers, this); |
557 |
this.scribe.space(); |
558 |
} |
559 |
|
560 |
declarationExpression.getType().accept(this); |
561 |
|
562 |
formatVariableDeclarationFragments(declarationExpression.fragments(), insertSpaceBeforeComma, insertSpaceAfterComma); |
563 |
} |
564 |
|
565 |
private void formatVariableDeclarationFragments(final List fragments, boolean insertSpaceBeforeComma, boolean insertSpaceAfterComma) { |
566 |
final int fragmentsLength = fragments.size(); |
567 |
if (fragmentsLength > 1) { |
568 |
// multiple field declaration |
569 |
Alignment2 multiFieldDeclarationsAlignment =this.scribe.createAlignment( |
570 |
"multiple_field",//$NON-NLS-1$ |
571 |
this.preferences.alignment_for_multiple_fields, |
572 |
fragmentsLength - 1, |
573 |
this.scribe.scanner.currentPosition); |
574 |
this.scribe.enterAlignment(multiFieldDeclarationsAlignment); |
575 |
|
576 |
boolean ok = false; |
577 |
do { |
578 |
try { |
579 |
for (int i = 0; i < fragmentsLength; i++) { |
580 |
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(i); |
581 |
/* |
582 |
* Field name |
583 |
*/ |
584 |
if (i == 0) { |
585 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
586 |
} else { |
587 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, false); |
588 |
} |
589 |
|
590 |
/* |
591 |
* Check for extra dimensions |
592 |
*/ |
593 |
final int extraDimensions = fragment.getExtraDimensions(); |
594 |
if (extraDimensions != 0) { |
595 |
for (int index = 0; index < extraDimensions; index++) { |
596 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
597 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
598 |
} |
599 |
} |
600 |
|
601 |
/* |
602 |
* Field initialization |
603 |
*/ |
604 |
final Expression initialization = fragment.getInitializer(); |
605 |
if (initialization != null) { |
606 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
607 |
if (this.preferences.insert_space_after_assignment_operator) { |
608 |
this.scribe.space(); |
609 |
} |
610 |
Alignment2 assignmentAlignment = this.scribe.createAlignment("fieldDeclarationAssignmentAlignment", this.preferences.alignment_for_assignment, 1, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
611 |
this.scribe.enterAlignment(assignmentAlignment); |
612 |
boolean ok2 = false; |
613 |
do { |
614 |
try { |
615 |
this.scribe.alignFragment(assignmentAlignment, 0); |
616 |
initialization.accept(this); |
617 |
ok2 = true; |
618 |
} catch(AlignmentException e){ |
619 |
this.scribe.redoAlignment(e); |
620 |
} |
621 |
} while (!ok2); |
622 |
this.scribe.exitAlignment(assignmentAlignment, true); |
623 |
} |
624 |
|
625 |
if (i != fragmentsLength - 1) { |
626 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, insertSpaceBeforeComma); |
627 |
this.scribe.printTrailingComment(); |
628 |
this.scribe.alignFragment(multiFieldDeclarationsAlignment, i); |
629 |
|
630 |
if (insertSpaceAfterComma) { |
631 |
this.scribe.space(); |
632 |
} |
633 |
} |
634 |
} |
635 |
ok = true; |
636 |
} catch (AlignmentException e) { |
637 |
this.scribe.redoAlignment(e); |
638 |
} |
639 |
} while (!ok); |
640 |
this.scribe.exitAlignment(multiFieldDeclarationsAlignment, true); |
641 |
} else { |
642 |
// single field declaration |
643 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
644 |
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(0); |
645 |
final int extraDimensions = fragment.getExtraDimensions(); |
646 |
if (extraDimensions != 0) { |
647 |
for (int i = 0; i < extraDimensions; i++) { |
648 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
649 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
650 |
} |
651 |
} |
652 |
final Expression initialization = fragment.getInitializer(); |
653 |
if (initialization != null) { |
654 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
655 |
if (this.preferences.insert_space_after_assignment_operator) { |
656 |
this.scribe.space(); |
657 |
} |
658 |
Alignment2 assignmentAlignment = this.scribe.createAlignment("localDeclarationAssignmentAlignment", this.preferences.alignment_for_assignment, 1, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
659 |
this.scribe.enterAlignment(assignmentAlignment); |
660 |
boolean ok = false; |
661 |
do { |
662 |
try { |
663 |
this.scribe.alignFragment(assignmentAlignment, 0); |
664 |
initialization.accept(this); |
665 |
ok = true; |
666 |
} catch(AlignmentException e){ |
667 |
this.scribe.redoAlignment(e); |
668 |
} |
669 |
} while (!ok); |
670 |
this.scribe.exitAlignment(assignmentAlignment, true); |
671 |
} |
672 |
} |
673 |
} |
674 |
|
675 |
private void formatOpeningBrace(String bracePosition, boolean insertSpaceBeforeBrace) { |
676 |
if (DefaultCodeFormatterConstants.NEXT_LINE.equals(bracePosition)) { |
677 |
this.scribe.printNewLine(); |
678 |
} else if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(bracePosition)) { |
679 |
this.scribe.printNewLine(); |
680 |
this.scribe.indent(); |
681 |
} |
682 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, insertSpaceBeforeBrace); |
683 |
|
684 |
this.scribe.printTrailingComment(); |
685 |
} |
686 |
|
687 |
private void formatStatements(final List statements, boolean insertNewLineAfterLastStatement) { |
688 |
final int statementsLength = statements.size(); |
689 |
if (statementsLength > 1) { |
690 |
Statement previousStatement = (Statement) statements.get(0); |
691 |
previousStatement.accept(this); |
692 |
final int previousStatementNodeType = previousStatement.getNodeType(); |
693 |
for (int i = 1; i < statementsLength - 1; i++) { |
694 |
final Statement statement = (Statement) statements.get(i); |
695 |
final int statementNodeType = statement.getNodeType(); |
696 |
if ((previousStatementNodeType == ASTNode.EMPTY_STATEMENT |
697 |
&& statementNodeType != ASTNode.EMPTY_STATEMENT) |
698 |
|| (previousStatementNodeType != ASTNode.EMPTY_STATEMENT |
699 |
&& statementNodeType != ASTNode.EMPTY_STATEMENT)) { |
700 |
this.scribe.printNewLine(); |
701 |
} |
702 |
statement.accept(this); |
703 |
previousStatement = statement; |
704 |
} |
705 |
final Statement statement = ((Statement) statements.get(statementsLength - 1)); |
706 |
final int statementNodeType = statement.getNodeType(); |
707 |
if ((previousStatementNodeType == ASTNode.EMPTY_STATEMENT |
708 |
&& statementNodeType != ASTNode.EMPTY_STATEMENT) |
709 |
|| (previousStatementNodeType != ASTNode.EMPTY_STATEMENT |
710 |
&& statementNodeType != ASTNode.EMPTY_STATEMENT)) { |
711 |
this.scribe.printNewLine(); |
712 |
} |
713 |
statement.accept(this); |
714 |
} else { |
715 |
((Statement) statements.get(0)).accept(this); |
716 |
} |
717 |
if (insertNewLineAfterLastStatement) { |
718 |
this.scribe.printNewLine(); |
719 |
} |
720 |
} |
721 |
|
722 |
private void formatTypeMembers(List bodyDeclarations, boolean insertLineAfterLastMember) { |
723 |
Alignment2 memberAlignment = this.scribe.createMemberAlignment("typeMembers", this.preferences.align_type_members_on_columns ? Alignment.M_MULTICOLUMN : Alignment.M_NO_ALIGNMENT, 3, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
724 |
this.scribe.enterMemberAlignment(memberAlignment); |
725 |
boolean isChunkStart = false; |
726 |
boolean ok = false; |
727 |
int startIndex = 0; |
728 |
do { |
729 |
try { |
730 |
for (int i = startIndex, max = bodyDeclarations.size(); i < max; i++) { |
731 |
BodyDeclaration bodyDeclaration = (BodyDeclaration) bodyDeclarations.get(i); |
732 |
switch(bodyDeclaration.getNodeType()) { |
733 |
case ASTNode.FIELD_DECLARATION : |
734 |
isChunkStart = memberAlignment.checkChunkStart(Alignment.CHUNK_FIELD, i, this.scribe.scanner.currentPosition); |
735 |
FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration; |
736 |
format(fieldDeclaration, isChunkStart, i == 0); |
737 |
break; |
738 |
case ASTNode.INITIALIZER : |
739 |
isChunkStart = memberAlignment.checkChunkStart(Alignment.CHUNK_FIELD, i, this.scribe.scanner.currentPosition); |
740 |
int newLineBeforeChunk = isChunkStart ? this.preferences.blank_lines_before_new_chunk : 0; |
741 |
if (newLineBeforeChunk > 0 && i != 0) { |
742 |
this.scribe.printEmptyLines(newLineBeforeChunk); |
743 |
} else if (i == 0) { |
744 |
int newLinesBeforeFirstClassBodyDeclaration = this.preferences.blank_lines_before_first_class_body_declaration; |
745 |
if (newLinesBeforeFirstClassBodyDeclaration > 0) { |
746 |
this.scribe.printEmptyLines(newLinesBeforeFirstClassBodyDeclaration); |
747 |
} |
748 |
} |
749 |
bodyDeclaration.accept(this); |
750 |
break; |
751 |
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION : |
752 |
case ASTNode.METHOD_DECLARATION : |
753 |
isChunkStart = memberAlignment.checkChunkStart(Alignment.CHUNK_METHOD, i, this.scribe.scanner.currentPosition); |
754 |
format(bodyDeclaration, isChunkStart, i == 0); |
755 |
break; |
756 |
case ASTNode.TYPE_DECLARATION : |
757 |
case ASTNode.ENUM_DECLARATION : |
758 |
case ASTNode.ANNOTATION_TYPE_DECLARATION : |
759 |
isChunkStart = memberAlignment.checkChunkStart(Alignment.CHUNK_TYPE, i, this.scribe.scanner.currentPosition); |
760 |
format((AbstractTypeDeclaration)bodyDeclaration, isChunkStart, i == 0); |
761 |
} |
762 |
if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) { |
763 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
764 |
this.scribe.printTrailingComment(); |
765 |
} |
766 |
if (i < max - 1 || insertLineAfterLastMember) { |
767 |
this.scribe.printNewLine(); |
768 |
} |
769 |
// realign to the proper value |
770 |
if (this.scribe.memberAlignment != null) { |
771 |
// select the last alignment |
772 |
this.scribe.indentationLevel = this.scribe.memberAlignment.originalIndentationLevel; |
773 |
} |
774 |
} |
775 |
ok = true; |
776 |
} catch(AlignmentException e){ |
777 |
startIndex = memberAlignment.chunkStartIndex; |
778 |
this.scribe.redoMemberAlignment(e); |
779 |
} |
780 |
} while (!ok); |
781 |
this.scribe.printComment(); |
782 |
this.scribe.exitMemberAlignment(memberAlignment); |
783 |
} |
784 |
|
785 |
private void formatTypeOpeningBrace(String bracePosition, boolean insertSpaceBeforeBrace, boolean insertNewLine, ASTNode node) { |
786 |
formatOpeningBrace(bracePosition, insertSpaceBeforeBrace); |
787 |
if (!insertNewLine) { |
788 |
switch(node.getNodeType()) { |
789 |
case ASTNode.ENUM_DECLARATION : |
790 |
insertNewLine = this.preferences.insert_new_line_in_empty_enum_declaration; |
791 |
break; |
792 |
case ASTNode.ENUM_CONSTANT_DECLARATION : |
793 |
insertNewLine = this.preferences.insert_new_line_in_empty_enum_constant; |
794 |
break; |
795 |
case ASTNode.ANONYMOUS_CLASS_DECLARATION : |
796 |
insertNewLine = this.preferences.insert_new_line_in_empty_anonymous_type_declaration; |
797 |
break; |
798 |
case ASTNode.ANNOTATION_TYPE_DECLARATION : |
799 |
insertNewLine = this.preferences.insert_new_line_in_empty_annotation_declaration; |
800 |
break; |
801 |
default: |
802 |
insertNewLine = this.preferences.insert_new_line_in_empty_type_declaration; |
803 |
} |
804 |
} |
805 |
if (insertNewLine) { |
806 |
this.scribe.printNewLine(); |
807 |
} |
808 |
} |
809 |
|
810 |
|
811 |
private boolean hasComments() { |
812 |
|
813 |
this.localScanner.resetTo(this.scribe.scanner.startPosition, this.scribe.scannerEndPosition - 1); |
814 |
try { |
815 |
switch(this.localScanner.getNextToken()) { |
816 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
817 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
818 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
819 |
return true; |
820 |
} |
821 |
} catch(InvalidInputException e) { |
822 |
// ignore |
823 |
} |
824 |
return false; |
825 |
} |
826 |
|
827 |
private boolean isClosingGenericToken() { |
828 |
this.localScanner.resetTo(this.scribe.scanner.currentPosition, this.scribe.scannerEndPosition - 1); |
829 |
try { |
830 |
int token = this.localScanner.getNextToken(); |
831 |
loop: while(true) { |
832 |
switch(token) { |
833 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
834 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
835 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
836 |
token = this.localScanner.getNextToken(); |
837 |
continue loop; |
838 |
default: |
839 |
break loop; |
840 |
} |
841 |
} |
842 |
switch(token) { |
843 |
case TerminalTokens.TokenNameGREATER : |
844 |
case TerminalTokens.TokenNameRIGHT_SHIFT : |
845 |
case TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT : |
846 |
return true; |
847 |
} |
848 |
} catch(InvalidInputException e) { |
849 |
// ignore |
850 |
} |
851 |
return false; |
852 |
} |
853 |
|
854 |
private boolean isGuardClause(Block block, List statements) { |
855 |
if (commentStartsBlock(block.getStartPosition(), block.getStartPosition() + block.getLength() - 1)) return false; |
856 |
final int statementsLength = statements.size(); |
857 |
if (statementsLength != 1) return false; |
858 |
switch(((Statement) statements.get(0)).getNodeType()) { |
859 |
case ASTNode.RETURN_STATEMENT : |
860 |
case ASTNode.THROW_STATEMENT : |
861 |
return true; |
862 |
} |
863 |
return false; |
864 |
} |
865 |
|
866 |
private boolean isNextToken(int tokenName) { |
867 |
this.localScanner.resetTo(this.scribe.scanner.currentPosition, this.scribe.scannerEndPosition - 1); |
868 |
try { |
869 |
int token = this.localScanner.getNextToken(); |
870 |
loop: while(true) { |
871 |
switch(token) { |
872 |
case TerminalTokens.TokenNameCOMMENT_BLOCK : |
873 |
case TerminalTokens.TokenNameCOMMENT_JAVADOC : |
874 |
case TerminalTokens.TokenNameCOMMENT_LINE : |
875 |
token = this.localScanner.getNextToken(); |
876 |
continue loop; |
877 |
default: |
878 |
break loop; |
879 |
} |
880 |
} |
881 |
return token == tokenName; |
882 |
} catch(InvalidInputException e) { |
883 |
// ignore |
884 |
} |
885 |
return false; |
886 |
} |
887 |
|
888 |
public boolean visit(AnnotationTypeDeclaration node) { |
889 |
this.scribe.printComment(); |
890 |
final int line = this.scribe.line; |
891 |
|
892 |
final List modifiers = node.modifiers(); |
893 |
if (modifiers.size() != 0) { |
894 |
this.scribe.printModifiers(modifiers, this); |
895 |
this.scribe.space(); |
896 |
} |
897 |
this.scribe.printNextToken(TerminalTokens.TokenNameAT, this.preferences.insert_space_before_at_in_annotation_type_declaration); |
898 |
this.scribe.printNextToken(TerminalTokens.TokenNameinterface, this.preferences.insert_space_after_at_in_annotation_type_declaration); |
899 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
900 |
|
901 |
String class_declaration_brace; |
902 |
boolean space_before_opening_brace; |
903 |
class_declaration_brace = this.preferences.brace_position_for_annotation_type_declaration; |
904 |
space_before_opening_brace = this.preferences.insert_space_before_opening_brace_in_annotation_type_declaration; |
905 |
|
906 |
formatLeftCurlyBrace(line, class_declaration_brace); |
907 |
final List bodyDeclarations = node.bodyDeclarations(); |
908 |
formatTypeOpeningBrace(class_declaration_brace, space_before_opening_brace, bodyDeclarations.size() != 0, node); |
909 |
|
910 |
boolean indent_body_declarations_compare_to_header = this.preferences.indent_body_declarations_compare_to_annotation_declaration_header; |
911 |
if (indent_body_declarations_compare_to_header) { |
912 |
this.scribe.indent(); |
913 |
} |
914 |
|
915 |
formatTypeMembers(bodyDeclarations, true); |
916 |
|
917 |
if (indent_body_declarations_compare_to_header) { |
918 |
this.scribe.unIndent(); |
919 |
} |
920 |
|
921 |
if (this.preferences.insert_new_line_in_empty_annotation_declaration) { |
922 |
this.scribe.printNewLine(); |
923 |
} |
924 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
925 |
this.scribe.printTrailingComment(); |
926 |
if (class_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
927 |
this.scribe.unIndent(); |
928 |
} |
929 |
if (hasComments()) { |
930 |
this.scribe.printNewLine(); |
931 |
} |
932 |
return false; |
933 |
} |
934 |
|
935 |
public boolean visit(AnnotationTypeMemberDeclaration node) { |
936 |
/* |
937 |
* Print comments to get proper line number |
938 |
*/ |
939 |
this.scribe.printComment(); |
940 |
List modifiers = node.modifiers(); |
941 |
if (modifiers.size() != 0) { |
942 |
this.scribe.printModifiers(modifiers, this); |
943 |
this.scribe.space(); |
944 |
} |
945 |
/* |
946 |
* Print the method return type |
947 |
*/ |
948 |
node.getType().accept(this); |
949 |
/* |
950 |
* Print the method name |
951 |
*/ |
952 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
953 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_annotation_type_member_declaration); |
954 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_annotation_type_member_declaration); |
955 |
|
956 |
Expression defaultValue = node.getDefault(); |
957 |
if (defaultValue != null) { |
958 |
this.scribe.printNextToken(TerminalTokens.TokenNamedefault, true); |
959 |
this.scribe.space(); |
960 |
defaultValue.accept(this); |
961 |
} |
962 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
963 |
this.scribe.printTrailingComment(); |
964 |
return false; |
965 |
} |
966 |
|
967 |
public boolean visit(AnonymousClassDeclaration node) { |
968 |
/* |
969 |
* Type body |
970 |
*/ |
971 |
String anonymous_type_declaration_brace_position = this.preferences.brace_position_for_anonymous_type_declaration; |
972 |
|
973 |
final List bodyDeclarations = node.bodyDeclarations(); |
974 |
formatTypeOpeningBrace(anonymous_type_declaration_brace_position, this.preferences.insert_space_before_opening_brace_in_anonymous_type_declaration, bodyDeclarations.size() != 0, node); |
975 |
|
976 |
this.scribe.indent(); |
977 |
|
978 |
formatTypeMembers(bodyDeclarations, true); |
979 |
|
980 |
this.scribe.unIndent(); |
981 |
if (this.preferences.insert_new_line_in_empty_anonymous_type_declaration) { |
982 |
this.scribe.printNewLine(); |
983 |
} |
984 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
985 |
if (anonymous_type_declaration_brace_position.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
986 |
this.scribe.unIndent(); |
987 |
} |
988 |
return false; |
989 |
} |
990 |
|
991 |
public boolean visit(ArrayAccess node) { |
992 |
node.getArray().accept(this); |
993 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET, this.preferences.insert_space_before_opening_bracket_in_array_reference); |
994 |
if (this.preferences.insert_space_after_opening_bracket_in_array_reference) { |
995 |
this.scribe.space(); |
996 |
} |
997 |
node.getIndex().accept(this); |
998 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_before_closing_bracket_in_array_reference); |
999 |
return false; |
1000 |
} |
1001 |
|
1002 |
public boolean visit(ArrayCreation node) { |
1003 |
this.scribe.printNextToken(TerminalTokens.TokenNamenew); |
1004 |
this.scribe.space(); |
1005 |
final ArrayType type = node.getType(); |
1006 |
final List dimensions = node.dimensions(); |
1007 |
final int dimensionsLength = dimensions.size(); |
1008 |
|
1009 |
final int arrayTypeDimensions = type.getDimensions(); |
1010 |
type.getElementType().accept(this); |
1011 |
if (dimensionsLength != 0) { |
1012 |
for (int i = 0; i < dimensionsLength; i++) { |
1013 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET, this.preferences.insert_space_before_opening_bracket_in_array_allocation_expression); |
1014 |
Expression dimension = (Expression) dimensions.get(i); |
1015 |
if (dimension != null) { |
1016 |
if (this.preferences.insert_space_after_opening_bracket_in_array_allocation_expression) { |
1017 |
this.scribe.space(); |
1018 |
} |
1019 |
dimension.accept(this); |
1020 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_before_closing_bracket_in_array_allocation_expression); |
1021 |
} else { |
1022 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_between_empty_brackets_in_array_allocation_expression); |
1023 |
} |
1024 |
} |
1025 |
for (int i = 0, max = arrayTypeDimensions - dimensionsLength; i < max; i++) { |
1026 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET, this.preferences.insert_space_before_opening_bracket_in_array_allocation_expression); |
1027 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_between_empty_brackets_in_array_allocation_expression); |
1028 |
} |
1029 |
} else { |
1030 |
for (int i = 0; i < arrayTypeDimensions; i++) { |
1031 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET, this.preferences.insert_space_before_opening_bracket_in_array_allocation_expression); |
1032 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_between_empty_brackets_in_array_allocation_expression); |
1033 |
} |
1034 |
} |
1035 |
|
1036 |
final ArrayInitializer initializer = node.getInitializer(); |
1037 |
if (initializer != null) { |
1038 |
initializer.accept(this); |
1039 |
} |
1040 |
|
1041 |
return false; |
1042 |
} |
1043 |
|
1044 |
public boolean visit(ArrayInitializer node) { |
1045 |
final List expressions = node.expressions(); |
1046 |
final int expressionsLength = expressions.size(); |
1047 |
if (expressionsLength != 0) { |
1048 |
final String array_initializer_brace_position = this.preferences.brace_position_for_array_initializer; |
1049 |
formatOpeningBrace(array_initializer_brace_position, this.preferences.insert_space_before_opening_brace_in_array_initializer); |
1050 |
|
1051 |
final boolean insert_new_line_after_opening_brace = this.preferences.insert_new_line_after_opening_brace_in_array_initializer; |
1052 |
if (expressionsLength > 1) { |
1053 |
if (insert_new_line_after_opening_brace) { |
1054 |
this.scribe.printNewLine(); |
1055 |
} |
1056 |
Alignment2 arrayInitializerAlignment = this.scribe.createAlignment( |
1057 |
"array_initializer",//$NON-NLS-1$ |
1058 |
this.preferences.alignment_for_expressions_in_array_initializer, |
1059 |
Alignment.R_OUTERMOST, |
1060 |
expressionsLength, |
1061 |
this.scribe.scanner.currentPosition, |
1062 |
this.preferences.continuation_indentation_for_array_initializer, |
1063 |
true); |
1064 |
|
1065 |
if (insert_new_line_after_opening_brace) { |
1066 |
arrayInitializerAlignment.fragmentIndentations[0] = arrayInitializerAlignment.breakIndentationLevel; |
1067 |
} |
1068 |
|
1069 |
this.scribe.enterAlignment(arrayInitializerAlignment); |
1070 |
boolean ok = false; |
1071 |
do { |
1072 |
try { |
1073 |
this.scribe.alignFragment(arrayInitializerAlignment, 0); |
1074 |
if (this.preferences.insert_space_after_opening_brace_in_array_initializer) { |
1075 |
this.scribe.space(); |
1076 |
} |
1077 |
((Expression) expressions.get(0)).accept(this); |
1078 |
for (int i = 1; i < expressionsLength; i++) { |
1079 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer); |
1080 |
this.scribe.printTrailingComment(); |
1081 |
this.scribe.alignFragment(arrayInitializerAlignment, i); |
1082 |
if (this.preferences.insert_space_after_comma_in_array_initializer) { |
1083 |
this.scribe.space(); |
1084 |
} |
1085 |
((Expression) expressions.get(i)).accept(this); |
1086 |
if (i == expressionsLength - 1) { |
1087 |
if (isNextToken(TerminalTokens.TokenNameCOMMA)) { |
1088 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer); |
1089 |
this.scribe.printTrailingComment(); |
1090 |
} |
1091 |
} |
1092 |
} |
1093 |
ok = true; |
1094 |
} catch (AlignmentException e) { |
1095 |
this.scribe.redoAlignment(e); |
1096 |
} |
1097 |
} while (!ok); |
1098 |
this.scribe.exitAlignment(arrayInitializerAlignment, true); |
1099 |
} else { |
1100 |
if (insert_new_line_after_opening_brace) { |
1101 |
this.scribe.printNewLine(); |
1102 |
this.scribe.indent(); |
1103 |
} |
1104 |
// we don't need to use an alignment |
1105 |
if (this.preferences.insert_space_after_opening_brace_in_array_initializer) { |
1106 |
this.scribe.space(); |
1107 |
} else { |
1108 |
this.scribe.needSpace = false; |
1109 |
} |
1110 |
((Expression) expressions.get(0)).accept(this); |
1111 |
if (isNextToken(TerminalTokens.TokenNameCOMMA)) { |
1112 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_array_initializer); |
1113 |
this.scribe.printTrailingComment(); |
1114 |
} |
1115 |
if (insert_new_line_after_opening_brace) { |
1116 |
this.scribe.unIndent(); |
1117 |
} |
1118 |
} |
1119 |
if (this.preferences.insert_new_line_before_closing_brace_in_array_initializer) { |
1120 |
this.scribe.printNewLine(); |
1121 |
} else if (this.preferences.insert_space_before_closing_brace_in_array_initializer) { |
1122 |
this.scribe.space(); |
1123 |
} |
1124 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, false); |
1125 |
if (array_initializer_brace_position.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
1126 |
this.scribe.unIndent(); |
1127 |
} |
1128 |
} else { |
1129 |
boolean keepEmptyArrayInitializerOnTheSameLine = this.preferences.keep_empty_array_initializer_on_one_line; |
1130 |
String array_initializer_brace_position = this.preferences.brace_position_for_array_initializer; |
1131 |
if (keepEmptyArrayInitializerOnTheSameLine) { |
1132 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, this.preferences.insert_space_before_opening_brace_in_array_initializer); |
1133 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, this.preferences.insert_space_between_empty_braces_in_array_initializer); |
1134 |
} else { |
1135 |
formatOpeningBrace(array_initializer_brace_position, this.preferences.insert_space_before_opening_brace_in_array_initializer); |
1136 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, false); |
1137 |
if (array_initializer_brace_position.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
1138 |
this.scribe.unIndent(); |
1139 |
} |
1140 |
} |
1141 |
} |
1142 |
return false; |
1143 |
} |
1144 |
|
1145 |
public boolean visit(ArrayType node) { |
1146 |
node.getComponentType().accept(this); |
1147 |
if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) { |
1148 |
this.scribe.space(); |
1149 |
} |
1150 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
1151 |
if (this.preferences.insert_space_between_brackets_in_array_type_reference) { |
1152 |
this.scribe.space(); |
1153 |
} |
1154 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
1155 |
return false; |
1156 |
} |
1157 |
|
1158 |
public boolean visit(AssertStatement node) { |
1159 |
this.scribe.printNextToken(TerminalTokens.TokenNameassert); |
1160 |
this.scribe.space(); |
1161 |
node.getExpression().accept(this); |
1162 |
|
1163 |
Expression message = node.getMessage(); |
1164 |
if (message != null) { |
1165 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_assert); |
1166 |
if (this.preferences.insert_space_after_colon_in_assert) { |
1167 |
this.scribe.space(); |
1168 |
} |
1169 |
message.accept(this); |
1170 |
} |
1171 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1172 |
this.scribe.printTrailingComment(); |
1173 |
return false; |
1174 |
} |
1175 |
|
1176 |
public boolean visit(Assignment node) { |
1177 |
node.getLeftHandSide().accept(this); |
1178 |
Assignment.Operator operator = node.getOperator(); |
1179 |
if (operator == Assignment.Operator.ASSIGN) { |
1180 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
1181 |
} else if (operator == Assignment.Operator.MINUS_ASSIGN) { |
1182 |
this.scribe.printNextToken(TerminalTokens.TokenNameMINUS_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1183 |
} else if (operator == Assignment.Operator.PLUS_ASSIGN) { |
1184 |
this.scribe.printNextToken(TerminalTokens.TokenNamePLUS_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1185 |
} else if (operator == Assignment.Operator.TIMES_ASSIGN) { |
1186 |
this.scribe.printNextToken(TerminalTokens.TokenNameMULTIPLY_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1187 |
} else if (operator == Assignment.Operator.DIVIDE_ASSIGN) { |
1188 |
this.scribe.printNextToken(TerminalTokens.TokenNameDIVIDE_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1189 |
} else if (operator == Assignment.Operator.REMAINDER_ASSIGN) { |
1190 |
this.scribe.printNextToken(TerminalTokens.TokenNameREMAINDER_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1191 |
} else if (operator == Assignment.Operator.LEFT_SHIFT_ASSIGN) { |
1192 |
this.scribe.printNextToken(TerminalTokens.TokenNameLEFT_SHIFT_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1193 |
} else if (operator == Assignment.Operator.RIGHT_SHIFT_SIGNED_ASSIGN) { |
1194 |
this.scribe.printNextToken(TerminalTokens.TokenNameRIGHT_SHIFT_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1195 |
} else if (operator == Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN) { |
1196 |
this.scribe.printNextToken(TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1197 |
} else if (operator == Assignment.Operator.BIT_AND_ASSIGN) { |
1198 |
this.scribe.printNextToken(TerminalTokens.TokenNameAND_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1199 |
} else if (operator == Assignment.Operator.BIT_OR_ASSIGN) { |
1200 |
this.scribe.printNextToken(TerminalTokens.TokenNameOR_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1201 |
} else { |
1202 |
this.scribe.printNextToken(TerminalTokens.TokenNameXOR_EQUAL, this.preferences.insert_space_before_assignment_operator); |
1203 |
} |
1204 |
if (this.preferences.insert_space_after_assignment_operator) { |
1205 |
this.scribe.space(); |
1206 |
} |
1207 |
|
1208 |
Alignment2 assignmentAlignment = this.scribe.createAlignment("assignmentAlignment", this.preferences.alignment_for_assignment, 1, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
1209 |
this.scribe.enterAlignment(assignmentAlignment); |
1210 |
boolean ok = false; |
1211 |
do { |
1212 |
try { |
1213 |
this.scribe.alignFragment(assignmentAlignment, 0); |
1214 |
node.getRightHandSide().accept(this); |
1215 |
ok = true; |
1216 |
} catch(AlignmentException e){ |
1217 |
this.scribe.redoAlignment(e); |
1218 |
} |
1219 |
} while (!ok); |
1220 |
this.scribe.exitAlignment(assignmentAlignment, true); |
1221 |
return false; |
1222 |
} |
1223 |
|
1224 |
public boolean visit(Block node) { |
1225 |
formatBlock(node, this.preferences.brace_position_for_block, this.preferences.insert_space_before_opening_brace_in_block); |
1226 |
return false; |
1227 |
} |
1228 |
|
1229 |
public boolean visit(BooleanLiteral node) { |
1230 |
if (node.booleanValue()) { |
1231 |
this.scribe.printNextToken(TerminalTokens.TokenNametrue); |
1232 |
} else { |
1233 |
this.scribe.printNextToken(TerminalTokens.TokenNamefalse); |
1234 |
} |
1235 |
return false; |
1236 |
} |
1237 |
|
1238 |
public boolean visit(BreakStatement node) { |
1239 |
this.scribe.printNextToken(TerminalTokens.TokenNamebreak); |
1240 |
if (node.getLabel() != null) { |
1241 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
1242 |
} |
1243 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1244 |
this.scribe.printTrailingComment(); |
1245 |
return false; |
1246 |
} |
1247 |
|
1248 |
public boolean visit(CastExpression node) { |
1249 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN); |
1250 |
if (this.preferences.insert_space_after_opening_paren_in_cast) { |
1251 |
this.scribe.space(); |
1252 |
} |
1253 |
node.getType().accept(this); |
1254 |
|
1255 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_cast); |
1256 |
if (this.preferences.insert_space_after_closing_paren_in_cast) { |
1257 |
this.scribe.space(); |
1258 |
} |
1259 |
node.getExpression().accept(this); |
1260 |
return false; |
1261 |
} |
1262 |
|
1263 |
public boolean visit(CharacterLiteral node) { |
1264 |
this.scribe.printNextToken(TerminalTokens.TokenNameCharacterLiteral); |
1265 |
return false; |
1266 |
} |
1267 |
|
1268 |
public boolean visit(ClassInstanceCreation node) { |
1269 |
Expression expression = node.getExpression(); |
1270 |
if (expression != null) { |
1271 |
expression.accept(this); |
1272 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
1273 |
} |
1274 |
this.scribe.printNextToken(TerminalTokens.TokenNamenew); |
1275 |
final List typeArguments = node.typeArguments(); |
1276 |
final int length = typeArguments.size(); |
1277 |
if (length != 0) { |
1278 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments); |
1279 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) { |
1280 |
this.scribe.space(); |
1281 |
} |
1282 |
for (int i = 0; i < length - 1; i++) { |
1283 |
((Type) typeArguments.get(i)).accept(this); |
1284 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments); |
1285 |
if (this.preferences.insert_space_after_comma_in_type_arguments) { |
1286 |
this.scribe.space(); |
1287 |
} |
1288 |
} |
1289 |
((Type) typeArguments.get(length - 1)).accept(this); |
1290 |
if (isClosingGenericToken()) { |
1291 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments); |
1292 |
} |
1293 |
if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) { |
1294 |
this.scribe.space(); |
1295 |
} |
1296 |
} else { |
1297 |
this.scribe.space(); |
1298 |
} |
1299 |
|
1300 |
node.getType().accept(this); |
1301 |
|
1302 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation); |
1303 |
|
1304 |
final List arguments = node.arguments(); |
1305 |
final int argumentsLength = arguments.size(); |
1306 |
|
1307 |
if (argumentsLength != 0) { |
1308 |
if (this.preferences.insert_space_after_opening_paren_in_method_invocation) { |
1309 |
this.scribe.space(); |
1310 |
} |
1311 |
Alignment2 argumentsAlignment =this.scribe.createAlignment( |
1312 |
"allocation",//$NON-NLS-1$ |
1313 |
this.preferences.alignment_for_arguments_in_allocation_expression, |
1314 |
argumentsLength, |
1315 |
this.scribe.scanner.currentPosition); |
1316 |
this.scribe.enterAlignment(argumentsAlignment); |
1317 |
boolean ok = false; |
1318 |
do { |
1319 |
try { |
1320 |
for (int i = 0; i < argumentsLength; i++) { |
1321 |
if (i > 0) { |
1322 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_allocation_expression); |
1323 |
this.scribe.printTrailingComment(); |
1324 |
} |
1325 |
this.scribe.alignFragment(argumentsAlignment, i); |
1326 |
if (i > 0 && this.preferences.insert_space_after_comma_in_allocation_expression) { |
1327 |
this.scribe.space(); |
1328 |
} |
1329 |
((Expression) arguments.get(i)).accept(this); |
1330 |
} |
1331 |
ok = true; |
1332 |
} catch (AlignmentException e) { |
1333 |
this.scribe.redoAlignment(e); |
1334 |
} |
1335 |
} while (!ok); |
1336 |
this.scribe.exitAlignment(argumentsAlignment, true); |
1337 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation); |
1338 |
} else { |
1339 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation); |
1340 |
} |
1341 |
final AnonymousClassDeclaration classDeclaration = node.getAnonymousClassDeclaration(); |
1342 |
if (classDeclaration != null) { |
1343 |
classDeclaration.accept(this); |
1344 |
} |
1345 |
return false; |
1346 |
} |
1347 |
|
1348 |
public boolean visit(CompilationUnit node) { |
1349 |
// fake new line to handle empty lines before package declaration or import declarations |
1350 |
this.scribe.lastNumberOfNewLines = 1; |
1351 |
/* |
1352 |
* Package declaration |
1353 |
*/ |
1354 |
final PackageDeclaration packageDeclaration = node.getPackage(); |
1355 |
final boolean hasPackage = packageDeclaration != null; |
1356 |
if (hasPackage) { |
1357 |
if (hasComments()) { |
1358 |
this.scribe.printComment(); |
1359 |
} |
1360 |
int blankLinesBeforePackage = this.preferences.blank_lines_before_package; |
1361 |
if (blankLinesBeforePackage > 0) { |
1362 |
this.scribe.printEmptyLines(blankLinesBeforePackage); |
1363 |
} |
1364 |
|
1365 |
final List annotations = packageDeclaration.annotations(); |
1366 |
if (annotations.size() != 0) { |
1367 |
this.scribe.printModifiers(annotations, this); |
1368 |
this.scribe.space(); |
1369 |
} |
1370 |
// dump the package keyword |
1371 |
this.scribe.printNextToken(TerminalTokens.TokenNamepackage); |
1372 |
this.scribe.space(); |
1373 |
packageDeclaration.getName().accept(this); |
1374 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1375 |
this.scribe.printTrailingComment(); |
1376 |
int blankLinesAfterPackage = this.preferences.blank_lines_after_package; |
1377 |
if (blankLinesAfterPackage > 0) { |
1378 |
this.scribe.printEmptyLines(blankLinesAfterPackage); |
1379 |
} else { |
1380 |
this.scribe.printNewLine(); |
1381 |
} |
1382 |
} else { |
1383 |
this.scribe.printComment(); |
1384 |
} |
1385 |
|
1386 |
/* |
1387 |
* Import statements |
1388 |
*/ |
1389 |
final List imports = node.imports(); |
1390 |
final int importsLength = imports.size(); |
1391 |
if (importsLength != 0) { |
1392 |
if (hasPackage) { |
1393 |
int blankLinesBeforeImports = this.preferences.blank_lines_before_imports; |
1394 |
if (blankLinesBeforeImports > 0) { |
1395 |
this.scribe.printEmptyLines(blankLinesBeforeImports); |
1396 |
} |
1397 |
} |
1398 |
for (int i = 0; i < importsLength; i++) { |
1399 |
((ImportDeclaration) imports.get(i)).accept(this); |
1400 |
} |
1401 |
|
1402 |
int blankLinesAfterImports = this.preferences.blank_lines_after_imports; |
1403 |
if (blankLinesAfterImports > 0) { |
1404 |
this.scribe.printEmptyLines(blankLinesAfterImports); |
1405 |
} |
1406 |
} |
1407 |
|
1408 |
formatEmptyTypeDeclaration(true); |
1409 |
|
1410 |
int blankLineBetweenTypeDeclarations = this.preferences.blank_lines_between_type_declarations; |
1411 |
/* |
1412 |
* Type declarations |
1413 |
*/ |
1414 |
final List types = node.types(); |
1415 |
final int typesLength = types.size(); |
1416 |
if (typesLength != 0) { |
1417 |
for (int i = 0; i < typesLength - 1; i++) { |
1418 |
((AbstractTypeDeclaration) types.get(i)).accept(this); |
1419 |
formatEmptyTypeDeclaration(false); |
1420 |
if (blankLineBetweenTypeDeclarations != 0) { |
1421 |
this.scribe.printEmptyLines(blankLineBetweenTypeDeclarations); |
1422 |
} else { |
1423 |
this.scribe.printNewLine(); |
1424 |
} |
1425 |
} |
1426 |
((AbstractTypeDeclaration) types.get(typesLength - 1)).accept(this); |
1427 |
} |
1428 |
this.scribe.printEndOfCompilationUnit(); |
1429 |
return false; |
1430 |
} |
1431 |
|
1432 |
public boolean visit(ConditionalExpression node) { |
1433 |
node.getExpression().accept(this); |
1434 |
|
1435 |
Alignment2 conditionalExpressionAlignment =this.scribe.createAlignment( |
1436 |
"conditionalExpression", //$NON-NLS-1$ |
1437 |
this.preferences.alignment_for_conditional_expression, |
1438 |
2, |
1439 |
this.scribe.scanner.currentPosition); |
1440 |
|
1441 |
this.scribe.enterAlignment(conditionalExpressionAlignment); |
1442 |
boolean ok = false; |
1443 |
do { |
1444 |
try { |
1445 |
this.scribe.alignFragment(conditionalExpressionAlignment, 0); |
1446 |
this.scribe.printNextToken(TerminalTokens.TokenNameQUESTION, this.preferences.insert_space_before_question_in_conditional); |
1447 |
|
1448 |
if (this.preferences.insert_space_after_question_in_conditional) { |
1449 |
this.scribe.space(); |
1450 |
} |
1451 |
node.getThenExpression().accept(this); |
1452 |
this.scribe.printTrailingComment(); |
1453 |
this.scribe.alignFragment(conditionalExpressionAlignment, 1); |
1454 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_conditional); |
1455 |
|
1456 |
if (this.preferences.insert_space_after_colon_in_conditional) { |
1457 |
this.scribe.space(); |
1458 |
} |
1459 |
node.getElseExpression().accept(this); |
1460 |
|
1461 |
ok = true; |
1462 |
} catch (AlignmentException e) { |
1463 |
this.scribe.redoAlignment(e); |
1464 |
} |
1465 |
} while (!ok); |
1466 |
this.scribe.exitAlignment(conditionalExpressionAlignment, true); |
1467 |
return false; |
1468 |
} |
1469 |
|
1470 |
public boolean visit(ConstructorInvocation node) { |
1471 |
final List typeArguments = node.typeArguments(); |
1472 |
final int typeArgumentsLength = typeArguments.size(); |
1473 |
if (typeArgumentsLength != 0) { |
1474 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments); |
1475 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) { |
1476 |
this.scribe.space(); |
1477 |
} |
1478 |
for (int i = 0; i < typeArgumentsLength - 1; i++) { |
1479 |
((Type) typeArguments.get(i)).accept(this); |
1480 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments); |
1481 |
if (this.preferences.insert_space_after_comma_in_type_arguments) { |
1482 |
this.scribe.space(); |
1483 |
} |
1484 |
} |
1485 |
((Type) typeArguments.get(typeArgumentsLength - 1)).accept(this); |
1486 |
if (isClosingGenericToken()) { |
1487 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments); |
1488 |
} |
1489 |
if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) { |
1490 |
this.scribe.space(); |
1491 |
} |
1492 |
} |
1493 |
|
1494 |
this.scribe.printNextToken(TerminalTokens.TokenNamethis); |
1495 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation); |
1496 |
|
1497 |
final List arguments = node.arguments(); |
1498 |
final int argumentsLength = arguments.size(); |
1499 |
if (argumentsLength != 0) { |
1500 |
if (this.preferences.insert_space_after_opening_paren_in_method_invocation) { |
1501 |
this.scribe.space(); |
1502 |
} |
1503 |
Alignment2 argumentsAlignment =this.scribe.createAlignment( |
1504 |
"explicit_constructor_call",//$NON-NLS-1$ |
1505 |
this.preferences.alignment_for_arguments_in_explicit_constructor_call, |
1506 |
argumentsLength, |
1507 |
this.scribe.scanner.currentPosition); |
1508 |
this.scribe.enterAlignment(argumentsAlignment); |
1509 |
boolean ok = false; |
1510 |
do { |
1511 |
try { |
1512 |
for (int i = 0; i < argumentsLength; i++) { |
1513 |
if (i > 0) { |
1514 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_explicit_constructor_call_arguments); |
1515 |
this.scribe.printTrailingComment(); |
1516 |
} |
1517 |
this.scribe.alignFragment(argumentsAlignment, i); |
1518 |
if (i > 0 && this.preferences.insert_space_after_comma_in_explicit_constructor_call_arguments) { |
1519 |
this.scribe.space(); |
1520 |
} |
1521 |
((Expression) arguments.get(i)).accept(this); |
1522 |
} |
1523 |
ok = true; |
1524 |
} catch (AlignmentException e) { |
1525 |
this.scribe.redoAlignment(e); |
1526 |
} |
1527 |
} while (!ok); |
1528 |
this.scribe.exitAlignment(argumentsAlignment, true); |
1529 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation); |
1530 |
} else { |
1531 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation); |
1532 |
} |
1533 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1534 |
this.scribe.printTrailingComment(); |
1535 |
return false; |
1536 |
} |
1537 |
|
1538 |
public boolean visit(ContinueStatement node) { |
1539 |
this.scribe.printNextToken(TerminalTokens.TokenNamecontinue); |
1540 |
if (node.getLabel() != null) { |
1541 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
1542 |
} |
1543 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1544 |
this.scribe.printTrailingComment(); |
1545 |
return false; |
1546 |
} |
1547 |
|
1548 |
public boolean visit(DoStatement node) { |
1549 |
this.scribe.printNextToken(TerminalTokens.TokenNamedo); |
1550 |
final int line = this.scribe.line; |
1551 |
|
1552 |
final Statement action = node.getBody(); |
1553 |
if (action != null) { |
1554 |
switch(action.getNodeType()) { |
1555 |
case ASTNode.BLOCK : |
1556 |
formatLeftCurlyBrace(line, this.preferences.brace_position_for_block); |
1557 |
action.accept(this); |
1558 |
break; |
1559 |
case ASTNode.EMPTY_STATEMENT : |
1560 |
action.accept(this); |
1561 |
break; |
1562 |
default : |
1563 |
this.scribe.printNewLine(); |
1564 |
this.scribe.indent(); |
1565 |
action.accept(this); |
1566 |
this.scribe.unIndent(); |
1567 |
this.scribe.printNewLine(); |
1568 |
} |
1569 |
} |
1570 |
|
1571 |
if (this.preferences.insert_new_line_before_while_in_do_statement) { |
1572 |
this.scribe.printNewLine(); |
1573 |
} |
1574 |
this.scribe.printNextToken(TerminalTokens.TokenNamewhile, this.preferences.insert_space_after_closing_brace_in_block); |
1575 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_while); |
1576 |
|
1577 |
if (this.preferences.insert_space_after_opening_paren_in_while) { |
1578 |
this.scribe.space(); |
1579 |
} |
1580 |
|
1581 |
node.getExpression().accept(this); |
1582 |
|
1583 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_while); |
1584 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1585 |
this.scribe.printTrailingComment(); |
1586 |
return false; |
1587 |
} |
1588 |
|
1589 |
public boolean visit(EmptyStatement node) { |
1590 |
if (this.preferences.put_empty_statement_on_new_line) { |
1591 |
this.scribe.printNewLine(); |
1592 |
} |
1593 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1594 |
this.scribe.printTrailingComment(); |
1595 |
return false; |
1596 |
} |
1597 |
|
1598 |
public boolean visit(EnhancedForStatement node) { |
1599 |
this.scribe.printNextToken(TerminalTokens.TokenNamefor); |
1600 |
final int line = this.scribe.line; |
1601 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_for); |
1602 |
|
1603 |
if (this.preferences.insert_space_after_opening_paren_in_for) { |
1604 |
this.scribe.space(); |
1605 |
} |
1606 |
node.getParameter().accept(this); |
1607 |
|
1608 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_for); |
1609 |
if (this.preferences.insert_space_after_colon_in_for) { |
1610 |
this.scribe.space(); |
1611 |
} |
1612 |
node.getExpression().accept(this); |
1613 |
|
1614 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_for); |
1615 |
|
1616 |
formatAction(line, node.getBody(), false); |
1617 |
return false; |
1618 |
} |
1619 |
|
1620 |
public boolean visit(EnumConstantDeclaration node) { |
1621 |
this.scribe.printComment(); |
1622 |
final int line = this.scribe.line; |
1623 |
|
1624 |
final List modifiers = node.modifiers(); |
1625 |
if (modifiers.size() != 0) { |
1626 |
this.scribe.printModifiers(modifiers, this); |
1627 |
this.scribe.space(); |
1628 |
} |
1629 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
1630 |
if (isNextToken(TerminalTokens.TokenNameLPAREN)) { |
1631 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_enum_constant); |
1632 |
final List arguments = node.arguments(); |
1633 |
final int argumentsLength = arguments.size(); |
1634 |
if (argumentsLength != 0) { |
1635 |
Alignment2 argumentsAlignment = this.scribe.createAlignment( |
1636 |
"enumConstantArguments",//$NON-NLS-1$ |
1637 |
this.preferences.alignment_for_arguments_in_enum_constant, |
1638 |
argumentsLength, |
1639 |
this.scribe.scanner.currentPosition); |
1640 |
this.scribe.enterAlignment(argumentsAlignment); |
1641 |
boolean ok = false; |
1642 |
do { |
1643 |
try { |
1644 |
if (this.preferences.insert_space_after_opening_paren_in_enum_constant) { |
1645 |
this.scribe.space(); |
1646 |
} |
1647 |
for (int i = 0; i < argumentsLength; i++) { |
1648 |
if (i > 0) { |
1649 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_enum_constant_arguments); |
1650 |
this.scribe.printTrailingComment(); |
1651 |
} |
1652 |
this.scribe.alignFragment(argumentsAlignment, i); |
1653 |
if (i > 0 && this.preferences.insert_space_after_comma_in_enum_constant_arguments) { |
1654 |
this.scribe.space(); |
1655 |
} |
1656 |
((Expression) arguments.get(i)).accept(this); |
1657 |
} |
1658 |
ok = true; |
1659 |
} catch (AlignmentException e) { |
1660 |
this.scribe.redoAlignment(e); |
1661 |
} |
1662 |
} while (!ok); |
1663 |
this.scribe.exitAlignment(argumentsAlignment, true); |
1664 |
|
1665 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_enum_constant); |
1666 |
} else { |
1667 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_enum_constant); |
1668 |
} |
1669 |
} |
1670 |
|
1671 |
final AnonymousClassDeclaration anonymousClassDeclaration = node.getAnonymousClassDeclaration(); |
1672 |
if (anonymousClassDeclaration != null) { |
1673 |
final List bodyDeclarations = anonymousClassDeclaration.bodyDeclarations(); |
1674 |
String enum_constant_brace = this.preferences.brace_position_for_enum_constant; |
1675 |
|
1676 |
formatLeftCurlyBrace(line, enum_constant_brace); |
1677 |
formatTypeOpeningBrace(enum_constant_brace, this.preferences.insert_space_before_opening_brace_in_enum_constant, bodyDeclarations.size() != 0, node); |
1678 |
|
1679 |
if (this.preferences.indent_body_declarations_compare_to_enum_constant_header) { |
1680 |
this.scribe.indent(); |
1681 |
} |
1682 |
|
1683 |
formatTypeMembers(bodyDeclarations, true); |
1684 |
|
1685 |
if (this.preferences.indent_body_declarations_compare_to_enum_constant_header) { |
1686 |
this.scribe.unIndent(); |
1687 |
} |
1688 |
|
1689 |
if (this.preferences.insert_new_line_in_empty_enum_constant) { |
1690 |
this.scribe.printNewLine(); |
1691 |
} |
1692 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
1693 |
if (enum_constant_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
1694 |
this.scribe.unIndent(); |
1695 |
} |
1696 |
if (hasComments()) { |
1697 |
this.scribe.printNewLine(); |
1698 |
} |
1699 |
} |
1700 |
return false; |
1701 |
} |
1702 |
|
1703 |
|
1704 |
public boolean visit(EnumDeclaration node) { |
1705 |
/* |
1706 |
* Print comments to get proper line number |
1707 |
*/ |
1708 |
this.scribe.printComment(); |
1709 |
final int line = this.scribe.line; |
1710 |
|
1711 |
final List modifiers = node.modifiers(); |
1712 |
if (modifiers.size() != 0) { |
1713 |
this.scribe.printModifiers(modifiers, this); |
1714 |
this.scribe.space(); |
1715 |
} |
1716 |
|
1717 |
this.scribe.printNextToken(TerminalTokens.TokenNameenum, true); |
1718 |
|
1719 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
1720 |
|
1721 |
/* |
1722 |
* Super Interfaces |
1723 |
*/ |
1724 |
final List superInterfaces = node.superInterfaceTypes(); |
1725 |
final int superInterfacesLength = superInterfaces.size(); |
1726 |
if (superInterfacesLength != 0) { |
1727 |
Alignment2 interfaceAlignment =this.scribe.createAlignment( |
1728 |
"superInterfaces",//$NON-NLS-1$ |
1729 |
this.preferences.alignment_for_superinterfaces_in_enum_declaration, |
1730 |
superInterfacesLength+1, // implements token is first fragment |
1731 |
this.scribe.scanner.currentPosition); |
1732 |
this.scribe.enterAlignment(interfaceAlignment); |
1733 |
boolean ok = false; |
1734 |
do { |
1735 |
try { |
1736 |
this.scribe.alignFragment(interfaceAlignment, 0); |
1737 |
this.scribe.printNextToken(TerminalTokens.TokenNameimplements, true); |
1738 |
for (int i = 0; i < superInterfacesLength; i++) { |
1739 |
if (i > 0) { |
1740 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_superinterfaces); |
1741 |
this.scribe.printTrailingComment(); |
1742 |
this.scribe.alignFragment(interfaceAlignment, i+1); |
1743 |
if (this.preferences.insert_space_after_comma_in_superinterfaces) { |
1744 |
this.scribe.space(); |
1745 |
} |
1746 |
((Type) superInterfaces.get(i)).accept(this); |
1747 |
} else { |
1748 |
this.scribe.alignFragment(interfaceAlignment, i+1); |
1749 |
this.scribe.space(); |
1750 |
((Type) superInterfaces.get(i)).accept(this); |
1751 |
} |
1752 |
} |
1753 |
ok = true; |
1754 |
} catch (AlignmentException e) { |
1755 |
this.scribe.redoAlignment(e); |
1756 |
} |
1757 |
} while (!ok); |
1758 |
this.scribe.exitAlignment(interfaceAlignment, true); |
1759 |
} |
1760 |
|
1761 |
final String bracePosition = this.preferences.brace_position_for_enum_declaration; |
1762 |
|
1763 |
final List enumConstants = node.enumConstants(); |
1764 |
final int enumConstantsLength = enumConstants.size(); |
1765 |
|
1766 |
formatLeftCurlyBrace(line, bracePosition); |
1767 |
formatTypeOpeningBrace(bracePosition, this.preferences.insert_space_before_opening_brace_in_enum_declaration, (enumConstantsLength + node.bodyDeclarations().size()) != 0, node); |
1768 |
|
1769 |
final boolean indent_body_declarations_compare_to_header = this.preferences.indent_body_declarations_compare_to_enum_declaration_header; |
1770 |
if (indent_body_declarations_compare_to_header) { |
1771 |
this.scribe.indent(); |
1772 |
} |
1773 |
|
1774 |
if (enumConstantsLength != 0) { |
1775 |
if (enumConstantsLength > 1) { |
1776 |
Alignment2 enumConstantsAlignment = this.scribe.createAlignment( |
1777 |
"enumConstants",//$NON-NLS-1$ |
1778 |
this.preferences.alignment_for_enum_constants, |
1779 |
enumConstantsLength, |
1780 |
this.scribe.scanner.currentPosition, |
1781 |
0, // we don't want to indent enum constants when splitting to a new line |
1782 |
false); |
1783 |
this.scribe.enterAlignment(enumConstantsAlignment); |
1784 |
boolean ok = false; |
1785 |
do { |
1786 |
try { |
1787 |
for (int i = 0; i < enumConstantsLength; i++) { |
1788 |
this.scribe.alignFragment(enumConstantsAlignment, i); |
1789 |
final EnumConstantDeclaration enumConstantDeclaration = ((EnumConstantDeclaration) enumConstants.get(i)); |
1790 |
enumConstantDeclaration.accept(this); |
1791 |
if (isNextToken(TerminalTokens.TokenNameCOMMA)) { |
1792 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_enum_declarations); |
1793 |
if (this.preferences.insert_space_after_comma_in_enum_declarations) { |
1794 |
this.scribe.space(); |
1795 |
} |
1796 |
this.scribe.printTrailingComment(); |
1797 |
if (enumConstantDeclaration.getAnonymousClassDeclaration() != null) { |
1798 |
this.scribe.printNewLine(); |
1799 |
} |
1800 |
} |
1801 |
} |
1802 |
ok = true; |
1803 |
} catch (AlignmentException e) { |
1804 |
this.scribe.redoAlignment(e); |
1805 |
} |
1806 |
} while (!ok); |
1807 |
this.scribe.exitAlignment(enumConstantsAlignment, true); |
1808 |
} else { |
1809 |
final EnumConstantDeclaration enumConstantDeclaration = ((EnumConstantDeclaration) enumConstants.get(0)); |
1810 |
enumConstantDeclaration.accept(this); |
1811 |
if (isNextToken(TerminalTokens.TokenNameCOMMA)) { |
1812 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_enum_declarations); |
1813 |
if (this.preferences.insert_space_after_comma_in_enum_declarations) { |
1814 |
this.scribe.space(); |
1815 |
} |
1816 |
this.scribe.printTrailingComment(); |
1817 |
if (enumConstantDeclaration.getAnonymousClassDeclaration() != null) { |
1818 |
this.scribe.printNewLine(); |
1819 |
} |
1820 |
} |
1821 |
} |
1822 |
} |
1823 |
if (isNextToken(TerminalTokens.TokenNameSEMICOLON)) { |
1824 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1825 |
this.scribe.printTrailingComment(); |
1826 |
} |
1827 |
if (enumConstantsLength != 0) { |
1828 |
this.scribe.printNewLine(); |
1829 |
} |
1830 |
|
1831 |
formatTypeMembers(node.bodyDeclarations(), true); |
1832 |
|
1833 |
if (indent_body_declarations_compare_to_header) { |
1834 |
this.scribe.unIndent(); |
1835 |
} |
1836 |
|
1837 |
if (this.preferences.insert_new_line_in_empty_enum_declaration) { |
1838 |
this.scribe.printNewLine(); |
1839 |
} |
1840 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
1841 |
this.scribe.printTrailingComment(); |
1842 |
if (bracePosition.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
1843 |
this.scribe.unIndent(); |
1844 |
} |
1845 |
if (hasComments()) { |
1846 |
this.scribe.printNewLine(); |
1847 |
} |
1848 |
return false; |
1849 |
} |
1850 |
|
1851 |
public boolean visit(ExpressionStatement node) { |
1852 |
node.getExpression().accept(this); |
1853 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
1854 |
this.scribe.printTrailingComment(); |
1855 |
return false; |
1856 |
} |
1857 |
|
1858 |
public boolean visit(FieldAccess node) { |
1859 |
node.getExpression().accept(this); |
1860 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
1861 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
1862 |
return false; |
1863 |
} |
1864 |
|
1865 |
public boolean visit(ForStatement node) { |
1866 |
this.scribe.printNextToken(TerminalTokens.TokenNamefor); |
1867 |
final int line = this.scribe.line; |
1868 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_for); |
1869 |
|
1870 |
if (this.preferences.insert_space_after_opening_paren_in_for) { |
1871 |
this.scribe.space(); |
1872 |
} |
1873 |
final List initializers = node.initializers(); |
1874 |
final int initializersLength = initializers.size(); |
1875 |
if (initializersLength != 0) { |
1876 |
for (int i = 0; i < initializersLength; i++) { |
1877 |
Expression initializer = (Expression) initializers.get(i); |
1878 |
switch(initializer.getNodeType()) { |
1879 |
case ASTNode.VARIABLE_DECLARATION_EXPRESSION : |
1880 |
formatLocalDeclaration((VariableDeclarationExpression) initializer, this.preferences.insert_space_before_comma_in_for_inits, this.preferences.insert_space_after_comma_in_for_inits); |
1881 |
break; |
1882 |
default: |
1883 |
initializer.accept(this); |
1884 |
if (i >= 0 && (i < initializersLength - 1)) { |
1885 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_for_inits); |
1886 |
if (this.preferences.insert_space_after_comma_in_for_inits) { |
1887 |
this.scribe.space(); |
1888 |
} |
1889 |
this.scribe.printTrailingComment(); |
1890 |
} |
1891 |
} |
1892 |
} |
1893 |
} |
1894 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon_in_for); |
1895 |
final Expression condition = node.getExpression(); |
1896 |
if (condition != null) { |
1897 |
if (this.preferences.insert_space_after_semicolon_in_for) { |
1898 |
this.scribe.space(); |
1899 |
} |
1900 |
condition.accept(this); |
1901 |
} |
1902 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon_in_for); |
1903 |
final List updaters = node.updaters(); |
1904 |
final int updatersLength = updaters.size(); |
1905 |
if (updatersLength != 0) { |
1906 |
if (this.preferences.insert_space_after_semicolon_in_for) { |
1907 |
this.scribe.space(); |
1908 |
} |
1909 |
for (int i = 0; i < updatersLength; i++) { |
1910 |
((Expression) updaters.get(i)).accept(this); |
1911 |
if (i != updatersLength - 1) { |
1912 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_for_increments); |
1913 |
if (this.preferences.insert_space_after_comma_in_for_increments) { |
1914 |
this.scribe.space(); |
1915 |
} |
1916 |
this.scribe.printTrailingComment(); |
1917 |
} |
1918 |
} |
1919 |
} |
1920 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_for); |
1921 |
|
1922 |
formatAction(line, node.getBody(), false); |
1923 |
return false; |
1924 |
} |
1925 |
|
1926 |
public boolean visit(IfStatement node) { |
1927 |
this.scribe.printNextToken(TerminalTokens.TokenNameif); |
1928 |
final int line = this.scribe.line; |
1929 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_if); |
1930 |
if (this.preferences.insert_space_after_opening_paren_in_if) { |
1931 |
this.scribe.space(); |
1932 |
} |
1933 |
node.getExpression().accept(this); |
1934 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_if); |
1935 |
|
1936 |
final Statement thenStatement = node.getThenStatement(); |
1937 |
final Statement elseStatement = node.getElseStatement(); |
1938 |
|
1939 |
boolean thenStatementIsBlock = false; |
1940 |
if (thenStatement != null) { |
1941 |
if (thenStatement instanceof Block) { |
1942 |
final Block block = (Block) thenStatement; |
1943 |
thenStatementIsBlock = true; |
1944 |
final List statements = block.statements(); |
1945 |
if (isGuardClause(block, statements) && elseStatement == null && this.preferences.keep_guardian_clause_on_one_line) { |
1946 |
/* |
1947 |
* Need a specific formatting for guard clauses |
1948 |
* guard clauses are block with a single return or throw |
1949 |
* statement |
1950 |
*/ |
1951 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, this.preferences.insert_space_before_opening_brace_in_block); |
1952 |
this.scribe.space(); |
1953 |
((Statement) statements.get(0)).accept(this); |
1954 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, true); |
1955 |
this.scribe.printTrailingComment(); |
1956 |
} else { |
1957 |
formatLeftCurlyBrace(line, this.preferences.brace_position_for_block); |
1958 |
thenStatement.accept(this); |
1959 |
if (elseStatement != null && (this.preferences.insert_new_line_before_else_in_if_statement)) { |
1960 |
this.scribe.printNewLine(); |
1961 |
} |
1962 |
} |
1963 |
} else if (elseStatement == null && this.preferences.keep_simple_if_on_one_line) { |
1964 |
Alignment2 compactIfAlignment = this.scribe.createAlignment( |
1965 |
"compactIf", //$NON-NLS-1$ |
1966 |
this.preferences.alignment_for_compact_if, |
1967 |
Alignment.R_OUTERMOST, |
1968 |
1, |
1969 |
this.scribe.scanner.currentPosition, |
1970 |
1, |
1971 |
false); |
1972 |
this.scribe.enterAlignment(compactIfAlignment); |
1973 |
boolean ok = false; |
1974 |
do { |
1975 |
try { |
1976 |
this.scribe.alignFragment(compactIfAlignment, 0); |
1977 |
this.scribe.space(); |
1978 |
thenStatement.accept(this); |
1979 |
ok = true; |
1980 |
} catch (AlignmentException e) { |
1981 |
this.scribe.redoAlignment(e); |
1982 |
} |
1983 |
} while (!ok); |
1984 |
this.scribe.exitAlignment(compactIfAlignment, true); |
1985 |
} else if (this.preferences.keep_then_statement_on_same_line) { |
1986 |
this.scribe.space(); |
1987 |
thenStatement.accept(this); |
1988 |
if (elseStatement != null) { |
1989 |
this.scribe.printNewLine(); |
1990 |
} |
1991 |
} else { |
1992 |
this.scribe.printTrailingComment(); |
1993 |
this.scribe.printNewLine(); |
1994 |
this.scribe.indent(); |
1995 |
thenStatement.accept(this); |
1996 |
if (elseStatement != null) { |
1997 |
this.scribe.printNewLine(); |
1998 |
} |
1999 |
this.scribe.unIndent(); |
2000 |
} |
2001 |
} |
2002 |
|
2003 |
if (elseStatement != null) { |
2004 |
if (thenStatementIsBlock) { |
2005 |
this.scribe.printNextToken(TerminalTokens.TokenNameelse, this.preferences.insert_space_after_closing_brace_in_block); |
2006 |
} else { |
2007 |
this.scribe.printNextToken(TerminalTokens.TokenNameelse, true); |
2008 |
} |
2009 |
if (elseStatement instanceof Block) { |
2010 |
elseStatement.accept(this); |
2011 |
} else if (elseStatement instanceof IfStatement) { |
2012 |
if (!this.preferences.compact_else_if) { |
2013 |
this.scribe.printNewLine(); |
2014 |
this.scribe.indent(); |
2015 |
} |
2016 |
this.scribe.space(); |
2017 |
elseStatement.accept(this); |
2018 |
if (!this.preferences.compact_else_if) { |
2019 |
this.scribe.unIndent(); |
2020 |
} |
2021 |
} else if (this.preferences.keep_else_statement_on_same_line) { |
2022 |
this.scribe.space(); |
2023 |
elseStatement.accept(this); |
2024 |
} else { |
2025 |
this.scribe.printNewLine(); |
2026 |
this.scribe.indent(); |
2027 |
elseStatement.accept(this); |
2028 |
this.scribe.unIndent(); |
2029 |
} |
2030 |
} |
2031 |
return false; |
2032 |
} |
2033 |
|
2034 |
public boolean visit(ImportDeclaration node) { |
2035 |
this.scribe.printNextToken(TerminalTokens.TokenNameimport); |
2036 |
this.scribe.space(); |
2037 |
if (node.isStatic()) { |
2038 |
this.scribe.printNextToken(TerminalTokens.TokenNamestatic); |
2039 |
this.scribe.space(); |
2040 |
} |
2041 |
node.getName().accept(this); |
2042 |
if (node.isOnDemand()) { |
2043 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2044 |
this.scribe.printNextToken(TerminalTokens.TokenNameMULTIPLY); |
2045 |
} |
2046 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
2047 |
this.scribe.printTrailingComment(); |
2048 |
this.scribe.printNewLine(); |
2049 |
return false; |
2050 |
} |
2051 |
|
2052 |
public boolean visit(InfixExpression node) { |
2053 |
// active line wrapping |
2054 |
final InfixExpressionWrappingBuilder builder = new InfixExpressionWrappingBuilder(); |
2055 |
node.accept(builder); |
2056 |
final int fragmentsSize = builder.getFragmentsCounter(); |
2057 |
this.scribe.printComment(); |
2058 |
Alignment2 binaryExpressionAlignment = this.scribe.createAlignment("binaryExpressionAlignment", this.preferences.alignment_for_binary_expression, Alignment.R_OUTERMOST, fragmentsSize, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
2059 |
this.scribe.enterAlignment(binaryExpressionAlignment); |
2060 |
boolean ok = false; |
2061 |
List fragments = builder.fragments; |
2062 |
int[] operators = builder.getOperators(); |
2063 |
/* do { |
2064 |
try { |
2065 |
final boolean alignAfterOperator = false; |
2066 |
if (alignAfterOperator) { |
2067 |
for (int i = 0; i < fragmentsSize - 1; i++) { |
2068 |
this.scribe.alignFragment(binaryExpressionAlignment, i); |
2069 |
((Expression) fragments.get(i)).accept(this); |
2070 |
this.scribe.printTrailingComment(); |
2071 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2072 |
// a new line has been inserted by printTrailingComment() |
2073 |
this.scribe.indentationLevel = binaryExpressionAlignment.breakIndentationLevel; |
2074 |
} |
2075 |
this.scribe.printNextToken(operators[i], this.preferences.insert_space_before_binary_operator); |
2076 |
if (operators[i] == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS)) { |
2077 |
// the next character is a minus (unary operator) |
2078 |
this.scribe.space(); |
2079 |
} |
2080 |
if (this.preferences.insert_space_after_binary_operator) { |
2081 |
this.scribe.space(); |
2082 |
} |
2083 |
} |
2084 |
this.scribe.alignFragment(binaryExpressionAlignment, fragmentsSize - 1); |
2085 |
((Expression) fragments.get(fragmentsSize - 1)).accept(this); |
2086 |
this.scribe.printTrailingComment(); |
2087 |
} else { |
2088 |
this.scribe.alignFragment(binaryExpressionAlignment, 0); |
2089 |
((Expression) fragments.get(0)).accept(this); |
2090 |
this.scribe.printTrailingComment(); |
2091 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2092 |
if (binaryExpressionAlignment.couldBreak() && binaryExpressionAlignment.wasSplit) { |
2093 |
binaryExpressionAlignment.performFragmentEffect(); |
2094 |
} |
2095 |
} |
2096 |
for (int i = 1; i < fragmentsSize - 1; i++) { |
2097 |
this.scribe.alignFragment(binaryExpressionAlignment, i); |
2098 |
this.scribe.printNextToken(operators[i - 1], this.preferences.insert_space_before_binary_operator); |
2099 |
if (operators[i] == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS)) { |
2100 |
// the next character is a minus (unary operator) |
2101 |
this.scribe.space(); |
2102 |
} |
2103 |
if (this.preferences.insert_space_after_binary_operator) { |
2104 |
this.scribe.space(); |
2105 |
} |
2106 |
((Expression) fragments.get(i)).accept(this); |
2107 |
this.scribe.printTrailingComment(); |
2108 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2109 |
if (binaryExpressionAlignment.couldBreak() && binaryExpressionAlignment.wasSplit) { |
2110 |
binaryExpressionAlignment.performFragmentEffect(); |
2111 |
} |
2112 |
} |
2113 |
} |
2114 |
this.scribe.alignFragment(binaryExpressionAlignment, fragmentsSize - 1); |
2115 |
this.scribe.printNextToken(operators[fragmentsSize - 2], this.preferences.insert_space_before_binary_operator); |
2116 |
if (operators[fragmentsSize - 2] == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS)) { |
2117 |
// the next character is a minus (unary operator) |
2118 |
this.scribe.space(); |
2119 |
} |
2120 |
if (this.preferences.insert_space_after_binary_operator) { |
2121 |
this.scribe.space(); |
2122 |
} |
2123 |
((Expression) fragments.get(fragmentsSize - 1)).accept(this); |
2124 |
this.scribe.printTrailingComment(); |
2125 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2126 |
if (binaryExpressionAlignment.couldBreak() && binaryExpressionAlignment.wasSplit) { |
2127 |
binaryExpressionAlignment.performFragmentEffect(); |
2128 |
} |
2129 |
} |
2130 |
} |
2131 |
ok = true; |
2132 |
} catch(AlignmentException e){ |
2133 |
this.scribe.redoAlignment(e); |
2134 |
} |
2135 |
} while (!ok); |
2136 |
this.scribe.exitAlignment(binaryExpressionAlignment, true);*/ |
2137 |
do { |
2138 |
try { |
2139 |
for (int i = 0; i < fragmentsSize - 1; i++) { |
2140 |
((Expression) fragments.get(i)).accept(this); |
2141 |
this.scribe.printTrailingComment(); |
2142 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2143 |
if (binaryExpressionAlignment.couldBreak() && binaryExpressionAlignment.wasSplit) { |
2144 |
binaryExpressionAlignment.performFragmentEffect(); |
2145 |
} |
2146 |
} |
2147 |
this.scribe.alignFragment(binaryExpressionAlignment, i); |
2148 |
this.scribe.printNextToken(operators[i], this.preferences.insert_space_before_binary_operator); |
2149 |
this.scribe.printTrailingComment(); |
2150 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2151 |
if (binaryExpressionAlignment.couldBreak() && binaryExpressionAlignment.wasSplit) { |
2152 |
binaryExpressionAlignment.performFragmentEffect(); |
2153 |
} |
2154 |
} |
2155 |
if ( this.preferences.insert_space_after_binary_operator |
2156 |
|| (operators[i] == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS))) { |
2157 |
// the next character is a minus (unary operator) or the preference is set to true |
2158 |
this.scribe.space(); |
2159 |
} |
2160 |
} |
2161 |
((Expression) fragments.get(fragmentsSize - 1)).accept(this); |
2162 |
this.scribe.printTrailingComment(); |
2163 |
if (this.scribe.lastNumberOfNewLines == 1) { |
2164 |
if (binaryExpressionAlignment.couldBreak() && binaryExpressionAlignment.wasSplit) { |
2165 |
binaryExpressionAlignment.performFragmentEffect(); |
2166 |
} |
2167 |
} |
2168 |
ok = true; |
2169 |
} catch(AlignmentException e){ |
2170 |
this.scribe.redoAlignment(e); |
2171 |
} |
2172 |
} while (!ok); |
2173 |
this.scribe.exitAlignment(binaryExpressionAlignment, true); |
2174 |
/* leftOperand.accept(this); |
2175 |
final int operator = extractInfixExpressionOperator(node); |
2176 |
this.scribe.printNextToken(operator, this.preferences.insert_space_before_binary_operator); |
2177 |
if ( this.preferences.insert_space_after_binary_operator |
2178 |
|| (operator == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS))) { |
2179 |
// the next character is a minus (unary operator) or the preference is set to true |
2180 |
this.scribe.space(); |
2181 |
} |
2182 |
rightOperand.accept(this);*/ |
2183 |
return false; |
2184 |
} |
2185 |
|
2186 |
public boolean visit(Initializer node) { |
2187 |
final List modifiers = node.modifiers(); |
2188 |
if (modifiers.size() != 0) { |
2189 |
this.scribe.printModifiers(modifiers, this); |
2190 |
this.scribe.space(); |
2191 |
} |
2192 |
node.getBody().accept(this); |
2193 |
return false; |
2194 |
} |
2195 |
|
2196 |
public boolean visit(InstanceofExpression node) { |
2197 |
node.getLeftOperand().accept(this); |
2198 |
this.scribe.printNextToken(TerminalTokens.TokenNameinstanceof, true); |
2199 |
this.scribe.space(); |
2200 |
node.getRightOperand().accept(this); |
2201 |
return false; |
2202 |
} |
2203 |
|
2204 |
public boolean visit(LabeledStatement node) { |
2205 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
2206 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_labeled_statement); |
2207 |
if (this.preferences.insert_space_after_colon_in_labeled_statement) { |
2208 |
this.scribe.space(); |
2209 |
} |
2210 |
node.getBody().accept(this); |
2211 |
return false; |
2212 |
} |
2213 |
|
2214 |
public boolean visit(MarkerAnnotation node) { |
2215 |
this.scribe.printNextToken(TerminalTokens.TokenNameAT); |
2216 |
if (this.preferences.insert_space_after_at_in_annotation) { |
2217 |
this.scribe.space(); |
2218 |
} |
2219 |
node.getTypeName().accept(this); |
2220 |
return false; |
2221 |
} |
2222 |
|
2223 |
public boolean visit(MemberValuePair node) { |
2224 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
2225 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
2226 |
if (this.preferences.insert_space_after_assignment_operator) { |
2227 |
this.scribe.space(); |
2228 |
} |
2229 |
node.getValue().accept(this); |
2230 |
return false; |
2231 |
} |
2232 |
|
2233 |
public boolean visit(MethodDeclaration node) { |
2234 |
if ((node.getFlags() & ASTNode.MALFORMED) != 0) { |
2235 |
this.scribe.printComment(); |
2236 |
this.scribe.printIndentationIfNecessary(); |
2237 |
this.scribe.scanner.resetTo(node.getStartPosition() + node.getLength(), this.scribe.scannerEndPosition); |
2238 |
this.scribe.needSpace = true; |
2239 |
this.scribe.printTrailingComment(); |
2240 |
switch(this.scribe.scanner.source[this.scribe.scanner.currentPosition]) { |
2241 |
case '\n' : |
2242 |
this.scribe.scanner.currentPosition++; |
2243 |
this.scribe.lastNumberOfNewLines = 1; |
2244 |
break; |
2245 |
case '\r' : |
2246 |
this.scribe.scanner.currentPosition++; |
2247 |
if (this.scribe.scanner.source[this.scribe.scanner.currentPosition] == '\n') { |
2248 |
this.scribe.scanner.currentPosition++; |
2249 |
} |
2250 |
this.scribe.lastNumberOfNewLines = 1; |
2251 |
} |
2252 |
return false; |
2253 |
} |
2254 |
/* |
2255 |
* Print comments to get proper line number |
2256 |
*/ |
2257 |
this.scribe.printComment(); |
2258 |
final int line = this.scribe.line; |
2259 |
|
2260 |
final List modifiers = node.modifiers(); |
2261 |
final int modifiersLength = modifiers.size(); |
2262 |
if (modifiersLength != 0) { |
2263 |
this.scribe.printModifiers(modifiers, this); |
2264 |
this.scribe.space(); |
2265 |
} |
2266 |
|
2267 |
final List typeParameters = node.typeParameters(); |
2268 |
final int typeParametersLength = typeParameters.size(); |
2269 |
if (typeParametersLength != 0) { |
2270 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_parameters); |
2271 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_type_parameters) { |
2272 |
this.scribe.space(); |
2273 |
} |
2274 |
for (int i = 0; i < typeParametersLength - 1; i++) { |
2275 |
((TypeParameter) typeParameters.get(i)).accept(this); |
2276 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_parameters); |
2277 |
if (this.preferences.insert_space_after_comma_in_type_parameters) { |
2278 |
this.scribe.space(); |
2279 |
} |
2280 |
} |
2281 |
((TypeParameter) typeParameters.get(typeParametersLength - 1)).accept(this); |
2282 |
if (isClosingGenericToken()) { |
2283 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_parameters); |
2284 |
} |
2285 |
if (this.preferences.insert_space_after_closing_angle_bracket_in_type_parameters) { |
2286 |
this.scribe.space(); |
2287 |
} |
2288 |
} |
2289 |
|
2290 |
final Type returnType = node.getReturnType2(); |
2291 |
if (returnType != null) { |
2292 |
returnType.accept(this); |
2293 |
} |
2294 |
/* |
2295 |
* Print the method name |
2296 |
*/ |
2297 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
2298 |
|
2299 |
boolean spaceBeforeParen = this.preferences.insert_space_before_opening_paren_in_method_declaration; |
2300 |
if (node.isConstructor()) { |
2301 |
spaceBeforeParen = this.preferences.insert_space_before_opening_paren_in_constructor_declaration; |
2302 |
} |
2303 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, spaceBeforeParen); |
2304 |
|
2305 |
final List parameters = node.parameters(); |
2306 |
final int parametersLength = parameters.size(); |
2307 |
if (parametersLength != 0) { |
2308 |
if (this.preferences.insert_space_after_opening_paren_in_method_declaration) { |
2309 |
this.scribe.space(); |
2310 |
} |
2311 |
Alignment2 parametersAlignment = this.scribe.createAlignment( |
2312 |
"methodParameters",//$NON-NLS-1$ |
2313 |
this.preferences.alignment_for_parameters_in_method_declaration, |
2314 |
parametersLength, |
2315 |
this.scribe.scanner.currentPosition); |
2316 |
this.scribe.enterAlignment(parametersAlignment); |
2317 |
boolean ok = false; |
2318 |
do { |
2319 |
try { |
2320 |
for (int i = 0; i < parametersLength; i++) { |
2321 |
if (i > 0) { |
2322 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_declaration_parameters); |
2323 |
this.scribe.printTrailingComment(); |
2324 |
} |
2325 |
this.scribe.alignFragment(parametersAlignment, i); |
2326 |
if (i > 0 && this.preferences.insert_space_after_comma_in_method_declaration_parameters) { |
2327 |
this.scribe.space(); |
2328 |
} |
2329 |
((SingleVariableDeclaration) parameters.get(i)).accept(this); |
2330 |
} |
2331 |
ok = true; |
2332 |
} catch (AlignmentException e) { |
2333 |
this.scribe.redoAlignment(e); |
2334 |
} |
2335 |
} while (!ok); |
2336 |
this.scribe.exitAlignment(parametersAlignment, true); |
2337 |
|
2338 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_declaration); |
2339 |
} else { |
2340 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_declaration); |
2341 |
} |
2342 |
/* |
2343 |
* Check for extra dimensions |
2344 |
*/ |
2345 |
final int extraDimensions = node.getExtraDimensions(); |
2346 |
if (extraDimensions != 0) { |
2347 |
for (int i = 0; i < extraDimensions; i++) { |
2348 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
2349 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
2350 |
} |
2351 |
} |
2352 |
|
2353 |
final List thrownExceptions = node.thrownExceptions(); |
2354 |
final int thrownExceptionsLength = thrownExceptions.size(); |
2355 |
if (thrownExceptionsLength != 0) { |
2356 |
Alignment2 throwsAlignment = this.scribe.createAlignment( |
2357 |
"throws",//$NON-NLS-1$ |
2358 |
node.isConstructor() |
2359 |
? this.preferences.alignment_for_throws_clause_in_constructor_declaration |
2360 |
: this.preferences.alignment_for_throws_clause_in_method_declaration, |
2361 |
thrownExceptionsLength, // throws is the first token |
2362 |
this.scribe.scanner.currentPosition); |
2363 |
|
2364 |
this.scribe.enterAlignment(throwsAlignment); |
2365 |
boolean ok = false; |
2366 |
do { |
2367 |
try { |
2368 |
this.scribe.alignFragment(throwsAlignment, 0); |
2369 |
this.scribe.printNextToken(TerminalTokens.TokenNamethrows, true); |
2370 |
|
2371 |
for (int i = 0; i < thrownExceptionsLength; i++) { |
2372 |
if (i > 0) { |
2373 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_declaration_throws); |
2374 |
this.scribe.printTrailingComment(); |
2375 |
this.scribe.alignFragment(throwsAlignment, i); |
2376 |
if (this.preferences.insert_space_after_comma_in_method_declaration_throws) { |
2377 |
this.scribe.space(); |
2378 |
} |
2379 |
} else { |
2380 |
this.scribe.space(); |
2381 |
} |
2382 |
((Name) thrownExceptions.get(i)).accept(this); |
2383 |
} |
2384 |
ok = true; |
2385 |
} catch (AlignmentException e) { |
2386 |
this.scribe.redoAlignment(e); |
2387 |
} |
2388 |
} while (!ok); |
2389 |
this.scribe.exitAlignment(throwsAlignment, true); |
2390 |
} |
2391 |
|
2392 |
final Block body = node.getBody(); |
2393 |
if (body != null) { |
2394 |
/* |
2395 |
* Method body |
2396 |
*/ |
2397 |
String method_declaration_brace = this.preferences.brace_position_for_method_declaration; |
2398 |
formatLeftCurlyBrace(line, method_declaration_brace); |
2399 |
formatOpeningBrace(method_declaration_brace, this.preferences.insert_space_before_opening_brace_in_method_declaration); |
2400 |
final int numberOfBlankLinesAtBeginningOfMethodBody = this.preferences.blank_lines_at_beginning_of_method_body; |
2401 |
if (numberOfBlankLinesAtBeginningOfMethodBody > 0) { |
2402 |
this.scribe.printEmptyLines(numberOfBlankLinesAtBeginningOfMethodBody); |
2403 |
} |
2404 |
final List statements = body.statements(); |
2405 |
final int statementsLength = statements.size(); |
2406 |
if (statementsLength != 0) { |
2407 |
this.scribe.printNewLine(); |
2408 |
if (this.preferences.indent_statements_compare_to_body) { |
2409 |
this.scribe.indent(); |
2410 |
} |
2411 |
formatStatements(statements, true); |
2412 |
this.scribe.printComment(); |
2413 |
if (this.preferences.indent_statements_compare_to_body) { |
2414 |
this.scribe.unIndent(); |
2415 |
} |
2416 |
} else if (this.preferences.insert_new_line_in_empty_method_body) { |
2417 |
this.scribe.printNewLine(); |
2418 |
if (this.preferences.indent_statements_compare_to_body) { |
2419 |
this.scribe.indent(); |
2420 |
} |
2421 |
this.scribe.printComment(); |
2422 |
if (this.preferences.indent_statements_compare_to_body) { |
2423 |
this.scribe.unIndent(); |
2424 |
} |
2425 |
} |
2426 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
2427 |
this.scribe.printTrailingComment(); |
2428 |
if (method_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
2429 |
this.scribe.unIndent(); |
2430 |
} |
2431 |
} else { |
2432 |
// no method body |
2433 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
2434 |
this.scribe.printTrailingComment(); |
2435 |
} |
2436 |
return false; |
2437 |
} |
2438 |
|
2439 |
public boolean visit(MethodInvocation node) { |
2440 |
MethodInvocationFragmentBuilder builder = new MethodInvocationFragmentBuilder(); |
2441 |
node.accept(builder); |
2442 |
|
2443 |
final List fragments = builder.fragments(); |
2444 |
final int fragmentsLength = fragments.size(); |
2445 |
if (fragmentsLength >= 3) { |
2446 |
// manage cascading method invocations |
2447 |
// check the first fragment |
2448 |
final Expression firstFragment = (Expression) fragments.get(0); |
2449 |
switch(firstFragment.getNodeType()) { |
2450 |
case ASTNode.METHOD_INVOCATION : |
2451 |
formatSingleMethodInvocation((MethodInvocation) firstFragment); |
2452 |
break; |
2453 |
default: |
2454 |
firstFragment.accept(this); |
2455 |
} |
2456 |
Alignment2 cascadingMessageSendAlignment = |
2457 |
this.scribe.createAlignment( |
2458 |
"cascadingMessageSendAlignment", //$NON-NLS-1$ |
2459 |
this.preferences.alignment_for_selector_in_method_invocation, |
2460 |
Alignment.R_INNERMOST, |
2461 |
fragmentsLength, |
2462 |
this.scribe.scanner.currentPosition); |
2463 |
this.scribe.enterAlignment(cascadingMessageSendAlignment); |
2464 |
boolean ok = false; |
2465 |
do { |
2466 |
try { |
2467 |
this.scribe.alignFragment(cascadingMessageSendAlignment, 0); |
2468 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2469 |
for (int i = 1; i < fragmentsLength - 1; i++) { |
2470 |
MethodInvocation currentMethodInvocation = (MethodInvocation) fragments.get(i); |
2471 |
formatSingleMethodInvocation(currentMethodInvocation); |
2472 |
this.scribe.alignFragment(cascadingMessageSendAlignment, i); |
2473 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2474 |
} |
2475 |
MethodInvocation currentMethodInvocation = (MethodInvocation) fragments.get(fragmentsLength - 1); |
2476 |
formatSingleMethodInvocation(currentMethodInvocation); |
2477 |
ok = true; |
2478 |
} catch(AlignmentException e){ |
2479 |
this.scribe.redoAlignment(e); |
2480 |
} |
2481 |
} while (!ok); |
2482 |
this.scribe.exitAlignment(cascadingMessageSendAlignment, true); |
2483 |
} else { |
2484 |
Expression expression = node.getExpression(); |
2485 |
if (expression != null) { |
2486 |
expression.accept(this); |
2487 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2488 |
} |
2489 |
formatSingleMethodInvocation(node); |
2490 |
} |
2491 |
return false; |
2492 |
} |
2493 |
|
2494 |
private void formatSingleMethodInvocation(MethodInvocation node) { |
2495 |
final List typeArguments = node.typeArguments(); |
2496 |
final int typeArgumentsLength = typeArguments.size(); |
2497 |
if (typeArgumentsLength != 0) { |
2498 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments); |
2499 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) { |
2500 |
this.scribe.space(); |
2501 |
} |
2502 |
for (int i = 0; i < typeArgumentsLength - 1; i++) { |
2503 |
((Type) typeArguments.get(i)).accept(this); |
2504 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments); |
2505 |
if (this.preferences.insert_space_after_comma_in_type_arguments) { |
2506 |
this.scribe.space(); |
2507 |
} |
2508 |
} |
2509 |
((Type) typeArguments.get(typeArgumentsLength - 1)).accept(this); |
2510 |
if (isClosingGenericToken()) { |
2511 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments); |
2512 |
} |
2513 |
if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) { |
2514 |
this.scribe.space(); |
2515 |
} |
2516 |
} |
2517 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); // selector |
2518 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation); |
2519 |
|
2520 |
final List arguments = node.arguments(); |
2521 |
final int argumentsLength = arguments.size(); |
2522 |
if (argumentsLength != 0) { |
2523 |
if (this.preferences.insert_space_after_opening_paren_in_method_invocation) { |
2524 |
this.scribe.space(); |
2525 |
} |
2526 |
if (argumentsLength > 1) { |
2527 |
Alignment2 argumentsAlignment = this.scribe.createAlignment( |
2528 |
"messageArguments", //$NON-NLS-1$ |
2529 |
this.preferences.alignment_for_arguments_in_method_invocation, |
2530 |
argumentsLength, |
2531 |
this.scribe.scanner.currentPosition); |
2532 |
this.scribe.enterAlignment(argumentsAlignment); |
2533 |
boolean ok = false; |
2534 |
do { |
2535 |
try { |
2536 |
for (int i = 0; i < argumentsLength; i++) { |
2537 |
if (i > 0) { |
2538 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments); |
2539 |
this.scribe.printTrailingComment(); |
2540 |
} |
2541 |
this.scribe.alignFragment(argumentsAlignment, i); |
2542 |
if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) { |
2543 |
this.scribe.space(); |
2544 |
} |
2545 |
((Expression) arguments.get(i)).accept(this); |
2546 |
} |
2547 |
ok = true; |
2548 |
} catch (AlignmentException e) { |
2549 |
this.scribe.redoAlignment(e); |
2550 |
} |
2551 |
} while (!ok); |
2552 |
this.scribe.exitAlignment(argumentsAlignment, true); |
2553 |
} else { |
2554 |
for (int i = 0; i < argumentsLength; i++) { |
2555 |
if (i > 0) { |
2556 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments); |
2557 |
this.scribe.printTrailingComment(); |
2558 |
} |
2559 |
if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) { |
2560 |
this.scribe.space(); |
2561 |
} |
2562 |
((Expression) arguments.get(i)).accept(this); |
2563 |
} |
2564 |
} |
2565 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation); |
2566 |
} else { |
2567 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation); |
2568 |
} |
2569 |
} |
2570 |
|
2571 |
public boolean visit(NormalAnnotation node) { |
2572 |
this.scribe.printNextToken(TerminalTokens.TokenNameAT); |
2573 |
if (this.preferences.insert_space_after_at_in_annotation) { |
2574 |
this.scribe.space(); |
2575 |
} |
2576 |
node.getTypeName().accept(this); |
2577 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_annotation); |
2578 |
if (this.preferences.insert_space_after_opening_paren_in_annotation) { |
2579 |
this.scribe.space(); |
2580 |
} |
2581 |
final List memberValuePairs = node.values(); |
2582 |
final int memberValuePairsLength = memberValuePairs.size(); |
2583 |
if (memberValuePairs.size() != 0) { |
2584 |
for (int i = 0; i < memberValuePairsLength - 1; i++) { |
2585 |
((MemberValuePair) memberValuePairs.get(i)).accept(this); |
2586 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_annotation); |
2587 |
if (this.preferences.insert_space_after_comma_in_annotation) { |
2588 |
this.scribe.space(); |
2589 |
} |
2590 |
} |
2591 |
((MemberValuePair) memberValuePairs.get(memberValuePairsLength - 1)).accept(this); |
2592 |
} |
2593 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_annotation); |
2594 |
return false; |
2595 |
} |
2596 |
|
2597 |
public boolean visit(NullLiteral node) { |
2598 |
this.scribe.printNextToken(TerminalTokens.TokenNamenull); |
2599 |
return false; |
2600 |
} |
2601 |
|
2602 |
public boolean visit(NumberLiteral node) { |
2603 |
if (isNextToken(TerminalTokens.TokenNameMINUS)) { |
2604 |
this.scribe.printNextToken(TerminalTokens.TokenNameMINUS, this.preferences.insert_space_before_unary_operator); |
2605 |
if (this.preferences.insert_space_after_unary_operator) { |
2606 |
this.scribe.space(); |
2607 |
} |
2608 |
} |
2609 |
this.scribe.printNextToken(NUMBER_LITERALS_EXPECTEDTOKENS); |
2610 |
return false; |
2611 |
} |
2612 |
|
2613 |
public boolean visit(ParameterizedType node) { |
2614 |
node.getType().accept(this); |
2615 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_parameterized_type_reference); |
2616 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_parameterized_type_reference) { |
2617 |
this.scribe.space(); |
2618 |
} |
2619 |
final List typeArguments = node.typeArguments(); |
2620 |
final int typeArgumentsLength = typeArguments.size(); |
2621 |
for (int i = 0; i < typeArgumentsLength - 1; i++) { |
2622 |
((Type) typeArguments.get(i)).accept(this); |
2623 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_parameterized_type_reference); |
2624 |
if (this.preferences.insert_space_after_comma_in_parameterized_type_reference) { |
2625 |
this.scribe.space(); |
2626 |
} |
2627 |
} |
2628 |
((Type) typeArguments.get(typeArgumentsLength - 1)).accept(this); |
2629 |
if (isClosingGenericToken()) { |
2630 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_parameterized_type_reference); |
2631 |
} |
2632 |
return false; |
2633 |
} |
2634 |
|
2635 |
public boolean visit(ParenthesizedExpression node) { |
2636 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_parenthesized_expression); |
2637 |
if (this.preferences.insert_space_after_opening_paren_in_parenthesized_expression) { |
2638 |
this.scribe.space(); |
2639 |
} |
2640 |
node.getExpression().accept(this); |
2641 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_parenthesized_expression); |
2642 |
return false; |
2643 |
} |
2644 |
|
2645 |
public boolean visit(PostfixExpression node) { |
2646 |
node.getOperand().accept(this); |
2647 |
int operator = node.getOperator() == PostfixExpression.Operator.INCREMENT |
2648 |
? TerminalTokens.TokenNamePLUS_PLUS : TerminalTokens.TokenNameMINUS_MINUS; |
2649 |
this.scribe.printNextToken(operator, this.preferences.insert_space_before_postfix_operator); |
2650 |
if (this.preferences.insert_space_after_postfix_operator) { |
2651 |
this.scribe.space(); |
2652 |
} |
2653 |
return false; |
2654 |
} |
2655 |
|
2656 |
public boolean visit(PrefixExpression node) { |
2657 |
int operator; |
2658 |
boolean insertSpaceBeforeOperator; |
2659 |
boolean insertSpaceAfterOperator; |
2660 |
final Operator operator2 = node.getOperator(); |
2661 |
if (operator2 == PrefixExpression.Operator.INCREMENT) { |
2662 |
operator = TerminalTokens.TokenNamePLUS_PLUS; |
2663 |
insertSpaceBeforeOperator = this.preferences.insert_space_before_prefix_operator; |
2664 |
insertSpaceAfterOperator = this.preferences.insert_space_after_prefix_operator; |
2665 |
} else if (operator2 == PrefixExpression.Operator.DECREMENT) { |
2666 |
operator = TerminalTokens.TokenNameMINUS_MINUS; |
2667 |
insertSpaceBeforeOperator = this.preferences.insert_space_before_prefix_operator; |
2668 |
insertSpaceAfterOperator = this.preferences.insert_space_after_prefix_operator; |
2669 |
} else if (operator2 == PrefixExpression.Operator.COMPLEMENT) { |
2670 |
operator = TerminalTokens.TokenNameTWIDDLE; |
2671 |
insertSpaceBeforeOperator = this.preferences.insert_space_before_unary_operator; |
2672 |
insertSpaceAfterOperator = this.preferences.insert_space_after_unary_operator; |
2673 |
} else if (operator2 == PrefixExpression.Operator.MINUS) { |
2674 |
operator = TerminalTokens.TokenNameMINUS; |
2675 |
insertSpaceBeforeOperator = this.preferences.insert_space_before_unary_operator; |
2676 |
insertSpaceAfterOperator = this.preferences.insert_space_after_unary_operator; |
2677 |
} else if (operator2 == PrefixExpression.Operator.NOT) { |
2678 |
operator = TerminalTokens.TokenNameNOT; |
2679 |
insertSpaceBeforeOperator = this.preferences.insert_space_before_unary_operator; |
2680 |
insertSpaceAfterOperator = this.preferences.insert_space_after_unary_operator; |
2681 |
} else { |
2682 |
operator = TerminalTokens.TokenNamePLUS; |
2683 |
insertSpaceBeforeOperator = this.preferences.insert_space_before_unary_operator; |
2684 |
insertSpaceAfterOperator = this.preferences.insert_space_after_unary_operator; |
2685 |
} |
2686 |
|
2687 |
this.scribe.printNextToken(operator, insertSpaceBeforeOperator); |
2688 |
if (insertSpaceAfterOperator) { |
2689 |
this.scribe.space(); |
2690 |
} |
2691 |
node.getOperand().accept(this); |
2692 |
return false; |
2693 |
} |
2694 |
|
2695 |
public boolean visit(PrimitiveType node) { |
2696 |
this.scribe.printNextToken(PRIMITIVE_TYPE_EXPECTEDTOKENS); |
2697 |
return false; |
2698 |
} |
2699 |
|
2700 |
public boolean visit(QualifiedName node) { |
2701 |
node.getQualifier().accept(this); |
2702 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2703 |
node.getName().accept(this); |
2704 |
return false; |
2705 |
} |
2706 |
|
2707 |
public boolean visit(QualifiedType node) { |
2708 |
node.getQualifier().accept(this); |
2709 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2710 |
node.getName().accept(this); |
2711 |
return false; |
2712 |
} |
2713 |
|
2714 |
public boolean visit(ReturnStatement node) { |
2715 |
this.scribe.printNextToken(TerminalTokens.TokenNamereturn); |
2716 |
final Expression expression = node.getExpression(); |
2717 |
if (expression != null) { |
2718 |
switch(expression.getNodeType()) { |
2719 |
case ASTNode.PARENTHESIZED_EXPRESSION : |
2720 |
if (this.preferences.insert_space_before_parenthesized_expression_in_return) { |
2721 |
this.scribe.space(); |
2722 |
} |
2723 |
break; |
2724 |
default: |
2725 |
this.scribe.space(); |
2726 |
} |
2727 |
expression.accept(this); |
2728 |
} |
2729 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
2730 |
this.scribe.printTrailingComment(); |
2731 |
return false; |
2732 |
} |
2733 |
|
2734 |
public boolean visit(SimpleName node) { |
2735 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
2736 |
return false; |
2737 |
} |
2738 |
|
2739 |
public boolean visit(SimpleType node) { |
2740 |
node.getName().accept(this); |
2741 |
return false; |
2742 |
} |
2743 |
|
2744 |
public boolean visit(SingleMemberAnnotation node) { |
2745 |
this.scribe.printNextToken(TerminalTokens.TokenNameAT); |
2746 |
if (this.preferences.insert_space_after_at_in_annotation) { |
2747 |
this.scribe.space(); |
2748 |
} |
2749 |
node.getTypeName().accept(this); |
2750 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_annotation); |
2751 |
if (this.preferences.insert_space_after_opening_paren_in_annotation) { |
2752 |
this.scribe.space(); |
2753 |
} |
2754 |
node.getValue().accept(this); |
2755 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_annotation); |
2756 |
return false; |
2757 |
} |
2758 |
|
2759 |
public boolean visit(SingleVariableDeclaration node) { |
2760 |
final List modifiers = node.modifiers(); |
2761 |
if (modifiers.size() != 0) { |
2762 |
this.scribe.printModifiers(modifiers, this); |
2763 |
this.scribe.space(); |
2764 |
} |
2765 |
|
2766 |
node.getType().accept(this); |
2767 |
|
2768 |
if (node.isVarargs()) { |
2769 |
this.scribe.printNextToken(TerminalTokens.TokenNameELLIPSIS, this.preferences.insert_space_before_ellipsis); |
2770 |
if (this.preferences.insert_space_after_ellipsis) { |
2771 |
this.scribe.space(); |
2772 |
} |
2773 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, false); |
2774 |
} else { |
2775 |
/* |
2776 |
* Print the argument name |
2777 |
*/ |
2778 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
2779 |
} |
2780 |
|
2781 |
final int extraDimensions = node.getExtraDimensions(); |
2782 |
if (extraDimensions != 0) { |
2783 |
for (int i = 0; i < extraDimensions; i++) { |
2784 |
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET); |
2785 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET); |
2786 |
} |
2787 |
} |
2788 |
final Expression initialization = node.getInitializer(); |
2789 |
if (initialization != null) { |
2790 |
this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator); |
2791 |
if (this.preferences.insert_space_after_assignment_operator) { |
2792 |
this.scribe.space(); |
2793 |
} |
2794 |
Alignment2 assignmentAlignment = this.scribe.createAlignment("localDeclarationAssignmentAlignment", this.preferences.alignment_for_assignment, 1, this.scribe.scanner.currentPosition); //$NON-NLS-1$ |
2795 |
this.scribe.enterAlignment(assignmentAlignment); |
2796 |
boolean ok = false; |
2797 |
do { |
2798 |
try { |
2799 |
this.scribe.alignFragment(assignmentAlignment, 0); |
2800 |
initialization.accept(this); |
2801 |
ok = true; |
2802 |
} catch(AlignmentException e){ |
2803 |
this.scribe.redoAlignment(e); |
2804 |
} |
2805 |
} while (!ok); |
2806 |
this.scribe.exitAlignment(assignmentAlignment, true); |
2807 |
} |
2808 |
return false; |
2809 |
} |
2810 |
|
2811 |
public boolean visit(StringLiteral node) { |
2812 |
this.scribe.checkNLSTag(node.getStartPosition()); |
2813 |
this.scribe.printNextToken(TerminalTokens.TokenNameStringLiteral); |
2814 |
this.scribe.printTrailingComment(); |
2815 |
return false; |
2816 |
} |
2817 |
|
2818 |
public boolean visit(SuperConstructorInvocation node) { |
2819 |
final Expression qualification = node.getExpression(); |
2820 |
if (qualification != null) { |
2821 |
qualification.accept(this); |
2822 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2823 |
} |
2824 |
final List typeArguments = node.typeArguments(); |
2825 |
final int typeArgumentsLength = typeArguments.size(); |
2826 |
if (typeArgumentsLength != 0) { |
2827 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments); |
2828 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) { |
2829 |
this.scribe.space(); |
2830 |
} |
2831 |
for (int i = 0; i < typeArgumentsLength - 1; i++) { |
2832 |
((Type) typeArguments.get(i)).accept(this); |
2833 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments); |
2834 |
if (this.preferences.insert_space_after_comma_in_type_arguments) { |
2835 |
this.scribe.space(); |
2836 |
} |
2837 |
} |
2838 |
((Type) typeArguments.get(typeArgumentsLength - 1)).accept(this); |
2839 |
if (isClosingGenericToken()) { |
2840 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments); |
2841 |
} |
2842 |
if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) { |
2843 |
this.scribe.space(); |
2844 |
} |
2845 |
} |
2846 |
|
2847 |
this.scribe.printNextToken(TerminalTokens.TokenNamesuper); |
2848 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation); |
2849 |
|
2850 |
final List arguments = node.arguments(); |
2851 |
final int argumentsLength = arguments.size(); |
2852 |
if (argumentsLength != 0) { |
2853 |
if (this.preferences.insert_space_after_opening_paren_in_method_invocation) { |
2854 |
this.scribe.space(); |
2855 |
} |
2856 |
Alignment2 argumentsAlignment =this.scribe.createAlignment( |
2857 |
"explicit_constructor_call",//$NON-NLS-1$ |
2858 |
this.preferences.alignment_for_arguments_in_explicit_constructor_call, |
2859 |
argumentsLength, |
2860 |
this.scribe.scanner.currentPosition); |
2861 |
this.scribe.enterAlignment(argumentsAlignment); |
2862 |
boolean ok = false; |
2863 |
do { |
2864 |
try { |
2865 |
for (int i = 0; i < argumentsLength; i++) { |
2866 |
if (i > 0) { |
2867 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_explicit_constructor_call_arguments); |
2868 |
this.scribe.printTrailingComment(); |
2869 |
} |
2870 |
this.scribe.alignFragment(argumentsAlignment, i); |
2871 |
if (i > 0 && this.preferences.insert_space_after_comma_in_explicit_constructor_call_arguments) { |
2872 |
this.scribe.space(); |
2873 |
} |
2874 |
((Expression) arguments.get(i)).accept(this); |
2875 |
} |
2876 |
ok = true; |
2877 |
} catch (AlignmentException e) { |
2878 |
this.scribe.redoAlignment(e); |
2879 |
} |
2880 |
} while (!ok); |
2881 |
this.scribe.exitAlignment(argumentsAlignment, true); |
2882 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation); |
2883 |
} else { |
2884 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation); |
2885 |
} |
2886 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
2887 |
this.scribe.printTrailingComment(); |
2888 |
return false; |
2889 |
} |
2890 |
|
2891 |
public boolean visit(SuperFieldAccess node) { |
2892 |
final Name qualifier = node.getQualifier(); |
2893 |
if (qualifier != null) { |
2894 |
qualifier.accept(this); |
2895 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2896 |
} |
2897 |
this.scribe.printNextToken(TerminalTokens.TokenNamesuper); |
2898 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2899 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
2900 |
return false; |
2901 |
} |
2902 |
|
2903 |
public boolean visit(SuperMethodInvocation node) { |
2904 |
final Name qualifier = node.getQualifier(); |
2905 |
if (qualifier != null) { |
2906 |
qualifier.accept(this); |
2907 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2908 |
} |
2909 |
this.scribe.printNextToken(TerminalTokens.TokenNamesuper); |
2910 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
2911 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); |
2912 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation); |
2913 |
|
2914 |
final List arguments = node.arguments(); |
2915 |
final int argumentsLength = arguments.size(); |
2916 |
if (argumentsLength != 0) { |
2917 |
if (this.preferences.insert_space_after_opening_paren_in_method_invocation) { |
2918 |
this.scribe.space(); |
2919 |
} |
2920 |
if (argumentsLength > 1) { |
2921 |
Alignment2 argumentsAlignment = this.scribe.createAlignment( |
2922 |
"messageArguments", //$NON-NLS-1$ |
2923 |
this.preferences.alignment_for_arguments_in_method_invocation, |
2924 |
argumentsLength, |
2925 |
this.scribe.scanner.currentPosition); |
2926 |
this.scribe.enterAlignment(argumentsAlignment); |
2927 |
boolean ok = false; |
2928 |
do { |
2929 |
try { |
2930 |
for (int i = 0; i < argumentsLength; i++) { |
2931 |
if (i > 0) { |
2932 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments); |
2933 |
this.scribe.printTrailingComment(); |
2934 |
} |
2935 |
this.scribe.alignFragment(argumentsAlignment, i); |
2936 |
if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) { |
2937 |
this.scribe.space(); |
2938 |
} |
2939 |
((Expression) arguments.get(i)).accept(this); |
2940 |
} |
2941 |
ok = true; |
2942 |
} catch (AlignmentException e) { |
2943 |
this.scribe.redoAlignment(e); |
2944 |
} |
2945 |
} while (!ok); |
2946 |
this.scribe.exitAlignment(argumentsAlignment, true); |
2947 |
} else { |
2948 |
for (int i = 0; i < argumentsLength; i++) { |
2949 |
if (i > 0) { |
2950 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments); |
2951 |
this.scribe.printTrailingComment(); |
2952 |
} |
2953 |
if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) { |
2954 |
this.scribe.space(); |
2955 |
} |
2956 |
((Expression) arguments.get(i)).accept(this); |
2957 |
} |
2958 |
} |
2959 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation); |
2960 |
} else { |
2961 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation); |
2962 |
} |
2963 |
return false; |
2964 |
} |
2965 |
|
2966 |
public boolean visit(SwitchCase node) { |
2967 |
Expression constant = node.getExpression(); |
2968 |
if (constant == null) { |
2969 |
this.scribe.printNextToken(TerminalTokens.TokenNamedefault); |
2970 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_default); |
2971 |
} else { |
2972 |
this.scribe.printNextToken(TerminalTokens.TokenNamecase); |
2973 |
this.scribe.space(); |
2974 |
constant.accept(this); |
2975 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_case); |
2976 |
} |
2977 |
return false; |
2978 |
} |
2979 |
|
2980 |
public boolean visit(SwitchStatement node) { |
2981 |
this.scribe.printNextToken(TerminalTokens.TokenNameswitch); |
2982 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_switch); |
2983 |
|
2984 |
if (this.preferences.insert_space_after_opening_paren_in_switch) { |
2985 |
this.scribe.space(); |
2986 |
} |
2987 |
|
2988 |
node.getExpression().accept(this); |
2989 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_switch); |
2990 |
/* |
2991 |
* Type body |
2992 |
*/ |
2993 |
String switch_brace = this.preferences.brace_position_for_switch; |
2994 |
formatOpeningBrace(switch_brace, this.preferences.insert_space_before_opening_brace_in_switch); |
2995 |
this.scribe.printNewLine(); |
2996 |
|
2997 |
if (this.preferences.indent_switchstatements_compare_to_switch) { |
2998 |
this.scribe.indent(); |
2999 |
} |
3000 |
final List statements = node.statements(); |
3001 |
final int statementsLength = statements.size(); |
3002 |
boolean wasACase = false; |
3003 |
boolean wasAStatement = false; |
3004 |
if (statementsLength != 0) { |
3005 |
for (int i = 0; i < statementsLength; i++) { |
3006 |
final Statement statement = (Statement) statements.get(i); |
3007 |
if (statement instanceof SwitchCase) { |
3008 |
if (wasACase) { |
3009 |
this.scribe.printNewLine(); |
3010 |
} |
3011 |
if ((wasACase && this.preferences.indent_switchstatements_compare_to_cases) |
3012 |
|| (wasAStatement && this.preferences.indent_switchstatements_compare_to_cases)) { |
3013 |
this.scribe.unIndent(); |
3014 |
} |
3015 |
statement.accept(this); |
3016 |
this.scribe.printTrailingComment(); |
3017 |
wasACase = true; |
3018 |
wasAStatement = false; |
3019 |
if (this.preferences.indent_switchstatements_compare_to_cases) { |
3020 |
this.scribe.indent(); |
3021 |
} |
3022 |
} else if (statement instanceof BreakStatement) { |
3023 |
if (this.preferences.indent_breaks_compare_to_cases) { |
3024 |
if (wasAStatement && !this.preferences.indent_switchstatements_compare_to_cases) { |
3025 |
this.scribe.indent(); |
3026 |
} |
3027 |
} else { |
3028 |
if (wasAStatement) { |
3029 |
if (this.preferences.indent_switchstatements_compare_to_cases) { |
3030 |
this.scribe.unIndent(); |
3031 |
} |
3032 |
} |
3033 |
if (wasACase && this.preferences.indent_switchstatements_compare_to_cases) { |
3034 |
this.scribe.unIndent(); |
3035 |
} |
3036 |
} |
3037 |
if (wasACase) { |
3038 |
this.scribe.printNewLine(); |
3039 |
} |
3040 |
statement.accept(this); |
3041 |
if (this.preferences.indent_breaks_compare_to_cases) { |
3042 |
this.scribe.unIndent(); |
3043 |
} |
3044 |
wasACase = false; |
3045 |
wasAStatement = false; |
3046 |
} else if (statement instanceof Block) { |
3047 |
String bracePosition; |
3048 |
if (wasACase) { |
3049 |
if (this.preferences.indent_switchstatements_compare_to_cases) { |
3050 |
this.scribe.unIndent(); |
3051 |
} |
3052 |
bracePosition = this.preferences.brace_position_for_block_in_case; |
3053 |
formatBlock((Block) statement, bracePosition, this.preferences.insert_space_after_colon_in_case); |
3054 |
if (this.preferences.indent_switchstatements_compare_to_cases) { |
3055 |
this.scribe.indent(); |
3056 |
} |
3057 |
} else { |
3058 |
bracePosition = this.preferences.brace_position_for_block; |
3059 |
formatBlock((Block) statement, bracePosition, this.preferences.insert_space_before_opening_brace_in_block); |
3060 |
} |
3061 |
wasAStatement = true; |
3062 |
wasACase = false; |
3063 |
} else { |
3064 |
this.scribe.printNewLine(); |
3065 |
statement.accept(this); |
3066 |
wasAStatement = true; |
3067 |
wasACase = false; |
3068 |
} |
3069 |
if (!wasACase) { |
3070 |
this.scribe.printNewLine(); |
3071 |
} |
3072 |
this.scribe.printComment(); |
3073 |
} |
3074 |
} |
3075 |
|
3076 |
if ((wasACase || wasAStatement) && this.preferences.indent_switchstatements_compare_to_cases) { |
3077 |
this.scribe.unIndent(); |
3078 |
} |
3079 |
if (this.preferences.indent_switchstatements_compare_to_switch) { |
3080 |
this.scribe.unIndent(); |
3081 |
} |
3082 |
this.scribe.printNewLine(); |
3083 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
3084 |
this.scribe.printTrailingComment(); |
3085 |
if (switch_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
3086 |
this.scribe.unIndent(); |
3087 |
} |
3088 |
return false; |
3089 |
} |
3090 |
|
3091 |
public boolean visit(SynchronizedStatement node) { |
3092 |
this.scribe.printNextToken(TerminalTokens.TokenNamesynchronized); |
3093 |
|
3094 |
final int line = this.scribe.line; |
3095 |
|
3096 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_synchronized); |
3097 |
|
3098 |
if (this.preferences.insert_space_after_opening_paren_in_synchronized) { |
3099 |
this.scribe.space(); |
3100 |
} |
3101 |
node.getExpression().accept(this); |
3102 |
|
3103 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_synchronized); |
3104 |
|
3105 |
formatLeftCurlyBrace(line, this.preferences.brace_position_for_block); |
3106 |
node.getBody().accept(this); |
3107 |
return false; |
3108 |
} |
3109 |
|
3110 |
public boolean visit(ThisExpression node) { |
3111 |
final Name qualifier = node.getQualifier(); |
3112 |
if (qualifier != null) { |
3113 |
qualifier.accept(this); |
3114 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
3115 |
} |
3116 |
this.scribe.printNextToken(TerminalTokens.TokenNamethis); |
3117 |
return false; |
3118 |
} |
3119 |
|
3120 |
public boolean visit(ThrowStatement node) { |
3121 |
this.scribe.printNextToken(TerminalTokens.TokenNamethrow); |
3122 |
this.scribe.space(); |
3123 |
node.getExpression().accept(this); |
3124 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
3125 |
this.scribe.printTrailingComment(); |
3126 |
return false; |
3127 |
} |
3128 |
|
3129 |
public boolean visit(TryStatement node) { |
3130 |
|
3131 |
this.scribe.printNextToken(TerminalTokens.TokenNametry); |
3132 |
node.getBody().accept(this); |
3133 |
final List catchClauses = node.catchClauses(); |
3134 |
final int catchClausesLength = catchClauses.size(); |
3135 |
if (catchClausesLength != 0) { |
3136 |
for (int i = 0; i < catchClausesLength; i++) { |
3137 |
if (this.preferences.insert_new_line_before_catch_in_try_statement) { |
3138 |
this.scribe.printNewLine(); |
3139 |
} |
3140 |
this.scribe.printNextToken(TerminalTokens.TokenNamecatch, this.preferences.insert_space_after_closing_brace_in_block); |
3141 |
final int line = this.scribe.line; |
3142 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_catch); |
3143 |
|
3144 |
if (this.preferences.insert_space_after_opening_paren_in_catch) { |
3145 |
this.scribe.space(); |
3146 |
} |
3147 |
|
3148 |
final CatchClause catchClause = ((CatchClause) catchClauses.get(i)); |
3149 |
catchClause.getException().accept(this); |
3150 |
|
3151 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_catch); |
3152 |
|
3153 |
formatLeftCurlyBrace(line, this.preferences.brace_position_for_block); |
3154 |
catchClause.getBody().accept(this); |
3155 |
} |
3156 |
} |
3157 |
final Block finallyBlock = node.getFinally(); |
3158 |
if (finallyBlock != null) { |
3159 |
if (this.preferences.insert_new_line_before_finally_in_try_statement) { |
3160 |
this.scribe.printNewLine(); |
3161 |
} |
3162 |
this.scribe.printNextToken(TerminalTokens.TokenNamefinally, this.preferences.insert_space_after_closing_brace_in_block); |
3163 |
finallyBlock.accept(this); |
3164 |
} |
3165 |
return false; |
3166 |
} |
3167 |
|
3168 |
public boolean visit(TypeDeclaration node) { |
3169 |
/* |
3170 |
* Print comments to get proper line number |
3171 |
*/ |
3172 |
this.scribe.printComment(); |
3173 |
final int line = this.scribe.line; |
3174 |
|
3175 |
final List modifiers = node.modifiers(); |
3176 |
if (modifiers.size() != 0) { |
3177 |
this.scribe.printModifiers(modifiers, this); |
3178 |
this.scribe.space(); |
3179 |
} |
3180 |
final boolean isInterface = node.isInterface(); |
3181 |
if (isInterface) { |
3182 |
this.scribe.printNextToken(TerminalTokens.TokenNameinterface, true); |
3183 |
} else { |
3184 |
this.scribe.printNextToken(TerminalTokens.TokenNameclass, true); |
3185 |
} |
3186 |
|
3187 |
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true); |
3188 |
|
3189 |
final List typeParameters = node.typeParameters(); |
3190 |
final int typeParametersLength = typeParameters.size(); |
3191 |
if (typeParametersLength != 0) { |
3192 |
this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_parameters); |
3193 |
if (this.preferences.insert_space_after_opening_angle_bracket_in_type_parameters) { |
3194 |
this.scribe.space(); |
3195 |
} |
3196 |
for (int i = 0; i < typeParametersLength - 1; i++) { |
3197 |
((TypeParameter) typeParameters.get(i)).accept(this); |
3198 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_parameters); |
3199 |
if (this.preferences.insert_space_after_comma_in_type_parameters) { |
3200 |
this.scribe.space(); |
3201 |
} |
3202 |
} |
3203 |
((TypeParameter) typeParameters.get(typeParametersLength - 1)).accept(this); |
3204 |
if (isClosingGenericToken()) { |
3205 |
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_parameters); |
3206 |
} |
3207 |
if (this.preferences.insert_space_after_closing_angle_bracket_in_type_parameters) { |
3208 |
this.scribe.space(); |
3209 |
} |
3210 |
} |
3211 |
/* |
3212 |
* Superclass |
3213 |
*/ |
3214 |
final Type superclass = node.getSuperclassType(); |
3215 |
if (superclass != null) { |
3216 |
Alignment2 superclassAlignment =this.scribe.createAlignment( |
3217 |
"superclass", //$NON-NLS-1$ |
3218 |
this.preferences.alignment_for_superclass_in_type_declaration, |
3219 |
2, |
3220 |
this.scribe.scanner.currentPosition); |
3221 |
this.scribe.enterAlignment(superclassAlignment); |
3222 |
boolean ok = false; |
3223 |
do { |
3224 |
try { |
3225 |
this.scribe.alignFragment(superclassAlignment, 0); |
3226 |
this.scribe.printNextToken(TerminalTokens.TokenNameextends, true); |
3227 |
this.scribe.alignFragment(superclassAlignment, 1); |
3228 |
this.scribe.space(); |
3229 |
superclass.accept(this); |
3230 |
ok = true; |
3231 |
} catch (AlignmentException e) { |
3232 |
this.scribe.redoAlignment(e); |
3233 |
} |
3234 |
} while (!ok); |
3235 |
this.scribe.exitAlignment(superclassAlignment, true); |
3236 |
} |
3237 |
|
3238 |
/* |
3239 |
* Super Interfaces |
3240 |
*/ |
3241 |
final List superInterfaces = node.superInterfaceTypes(); |
3242 |
final int superInterfacesLength = superInterfaces.size(); |
3243 |
if (superInterfacesLength != 0) { |
3244 |
Alignment2 interfaceAlignment = this.scribe.createAlignment( |
3245 |
"superInterfaces",//$NON-NLS-1$ |
3246 |
this.preferences.alignment_for_superinterfaces_in_type_declaration, |
3247 |
superInterfacesLength+1, // implements token is first fragment |
3248 |
this.scribe.scanner.currentPosition); |
3249 |
this.scribe.enterAlignment(interfaceAlignment); |
3250 |
boolean ok = false; |
3251 |
do { |
3252 |
try { |
3253 |
this.scribe.alignFragment(interfaceAlignment, 0); |
3254 |
if (isInterface) { |
3255 |
this.scribe.printNextToken(TerminalTokens.TokenNameextends, true); |
3256 |
} else { |
3257 |
this.scribe.printNextToken(TerminalTokens.TokenNameimplements, true); |
3258 |
} |
3259 |
this.scribe.alignFragment(interfaceAlignment, 1); |
3260 |
this.scribe.space(); |
3261 |
((Type) superInterfaces.get(0)).accept(this); |
3262 |
for (int i = 1; i < superInterfacesLength; i++) { |
3263 |
this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_superinterfaces); |
3264 |
this.scribe.printTrailingComment(); |
3265 |
this.scribe.alignFragment(interfaceAlignment, i+1); |
3266 |
if (this.preferences.insert_space_after_comma_in_superinterfaces) { |
3267 |
this.scribe.space(); |
3268 |
} |
3269 |
((Type) superInterfaces.get(i)).accept(this); |
3270 |
} |
3271 |
ok = true; |
3272 |
} catch (AlignmentException e) { |
3273 |
this.scribe.redoAlignment(e); |
3274 |
} |
3275 |
} while (!ok); |
3276 |
this.scribe.exitAlignment(interfaceAlignment, true); |
3277 |
} |
3278 |
|
3279 |
final String class_declaration_brace = this.preferences.brace_position_for_type_declaration; |
3280 |
final boolean space_before_opening_brace = this.preferences.insert_space_before_opening_brace_in_type_declaration; |
3281 |
|
3282 |
formatLeftCurlyBrace(line, class_declaration_brace); |
3283 |
final List bodyDeclarations = node.bodyDeclarations(); |
3284 |
formatTypeOpeningBrace(class_declaration_brace, space_before_opening_brace, bodyDeclarations.size() != 0, node); |
3285 |
|
3286 |
final boolean indent_body_declarations_compare_to_header = this.preferences.indent_body_declarations_compare_to_type_header; |
3287 |
|
3288 |
if (indent_body_declarations_compare_to_header) { |
3289 |
this.scribe.indent(); |
3290 |
} |
3291 |
|
3292 |
formatTypeMembers(bodyDeclarations, true); |
3293 |
|
3294 |
if (indent_body_declarations_compare_to_header) { |
3295 |
this.scribe.unIndent(); |
3296 |
} |
3297 |
|
3298 |
if (this.preferences.insert_new_line_in_empty_type_declaration) { |
3299 |
this.scribe.printNewLine(); |
3300 |
} |
3301 |
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE); |
3302 |
this.scribe.printTrailingComment(); |
3303 |
if (class_declaration_brace.equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED)) { |
3304 |
this.scribe.unIndent(); |
3305 |
} |
3306 |
if (hasComments()) { |
3307 |
this.scribe.printNewLine(); |
3308 |
} |
3309 |
return false; |
3310 |
} |
3311 |
|
3312 |
public boolean visit(TypeDeclarationStatement node) { |
3313 |
node.getDeclaration().accept(this); |
3314 |
return false; |
3315 |
} |
3316 |
|
3317 |
public boolean visit(TypeLiteral node) { |
3318 |
node.getType().accept(this); |
3319 |
this.scribe.printNextToken(TerminalTokens.TokenNameDOT); |
3320 |
this.scribe.printNextToken(TerminalTokens.TokenNameclass); |
3321 |
return false; |
3322 |
} |
3323 |
|
3324 |
public boolean visit(TypeParameter node) { |
3325 |
node.getName().accept(this); |
3326 |
final List bounds = node.typeBounds(); |
3327 |
final int boundsLength = bounds.size(); |
3328 |
if (boundsLength != 0) { |
3329 |
this.scribe.printNextToken(TerminalTokens.TokenNameextends, true); |
3330 |
this.scribe.space(); |
3331 |
((Type) bounds.get(0)).accept(this); |
3332 |
if (boundsLength > 1) { |
3333 |
this.scribe.printNextToken(TerminalTokens.TokenNameAND, this.preferences.insert_space_before_and_in_type_parameter); |
3334 |
if (this.preferences.insert_space_after_and_in_type_parameter) { |
3335 |
this.scribe.space(); |
3336 |
} |
3337 |
for (int i = 1; i < boundsLength - 1; i++) { |
3338 |
((Type) bounds.get(i)).accept(this); |
3339 |
this.scribe.printNextToken(TerminalTokens.TokenNameAND, this.preferences.insert_space_before_and_in_type_parameter); |
3340 |
if (this.preferences.insert_space_after_and_in_type_parameter) { |
3341 |
this.scribe.space(); |
3342 |
} |
3343 |
} |
3344 |
((Type) bounds.get(boundsLength - 1)).accept(this); |
3345 |
} |
3346 |
} |
3347 |
return false; |
3348 |
} |
3349 |
|
3350 |
public boolean visit(VariableDeclarationExpression node) { |
3351 |
formatLocalDeclaration(node, this.preferences.insert_space_before_comma_in_multiple_local_declarations, this.preferences.insert_space_after_comma_in_multiple_local_declarations); |
3352 |
return false; |
3353 |
} |
3354 |
|
3355 |
public boolean visit(VariableDeclarationStatement node) { |
3356 |
final List modifiers = node.modifiers(); |
3357 |
if (modifiers.size() != 0) { |
3358 |
this.scribe.printModifiers(modifiers, this); |
3359 |
this.scribe.space(); |
3360 |
} |
3361 |
|
3362 |
node.getType().accept(this); |
3363 |
|
3364 |
formatVariableDeclarationFragments(node.fragments(), this.preferences.insert_space_before_comma_in_multiple_local_declarations, this.preferences.insert_space_after_comma_in_multiple_local_declarations); |
3365 |
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); |
3366 |
this.scribe.printTrailingComment(); |
3367 |
return false; |
3368 |
} |
3369 |
|
3370 |
public boolean visit(WhileStatement node) { |
3371 |
this.scribe.printNextToken(TerminalTokens.TokenNamewhile); |
3372 |
final int line = this.scribe.line; |
3373 |
this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_while); |
3374 |
|
3375 |
if (this.preferences.insert_space_after_opening_paren_in_while) { |
3376 |
this.scribe.space(); |
3377 |
} |
3378 |
node.getExpression().accept(this); |
3379 |
|
3380 |
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_while); |
3381 |
|
3382 |
formatAction(line, node.getBody(),false); |
3383 |
return false; |
3384 |
} |
3385 |
|
3386 |
public boolean visit(WildcardType node) { |
3387 |
this.scribe.printNextToken(TerminalTokens.TokenNameQUESTION, this.preferences.insert_space_before_question_in_wilcard); |
3388 |
final Type bound = node.getBound(); |
3389 |
if (bound != null) { |
3390 |
if (node.isUpperBound()) { |
3391 |
this.scribe.printNextToken(TerminalTokens.TokenNameextends, true); |
3392 |
} else { |
3393 |
this.scribe.printNextToken(TerminalTokens.TokenNamesuper, true); |
3394 |
} |
3395 |
this.scribe.space(); |
3396 |
bound.accept(this); |
3397 |
} else if (this.preferences.insert_space_after_question_in_wilcard) { |
3398 |
this.scribe.space(); |
3399 |
} |
3400 |
return false; |
3401 |
} |
3402 |
|
3403 |
} |