1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.formatter;
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.StringReader;
16 import java.util.Hashtable;
17 import java.util.Locale;
20 import net.sourceforge.phpdt.core.ICodeFormatter;
21 import net.sourceforge.phpdt.core.compiler.CharOperation;
22 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
23 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
24 import net.sourceforge.phpdt.internal.compiler.ConfigurableOption;
25 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
27 import net.sourceforge.phpdt.internal.formatter.impl.FormatterOptions;
28 import net.sourceforge.phpdt.internal.formatter.impl.SplitLine;
30 /** <h2>How to format a piece of code ?</h2>
31 * <ul><li>Create an instance of <code>CodeFormatter</code>
32 * <li>Use the method <code>void format(aString)</code>
33 * on this instance to format <code>aString</code>.
34 * It will return the formatted string.</ul>
36 public class CodeFormatter implements ITerminalSymbols, ICodeFormatter {
38 public FormatterOptions options;
41 * Represents a block in the <code>constructions</code> stack.
43 public static final int BLOCK = ITerminalSymbols.TokenNameLBRACE;
46 * Represents a block following a control statement in the <code>constructions</code> stack.
48 public static final int NONINDENT_BLOCK = -100;
51 * Contains the formatted output.
53 StringBuffer formattedSource;
56 * Contains the current line.<br>
57 * Will be dumped at the next "newline"
59 StringBuffer currentLineBuffer;
62 * Used during the formatting to get each token.
67 * Contains the tokens responsible for the current indentation level
68 * and the blocks not closed yet.
70 private int[] constructions;
73 * Index in the <code>constructions</code> array.
75 private int constructionsCount;
78 * Level of indentation of the current token (number of tab char put in front of it).
80 private int indentationLevel;
83 * Regular level of indentation of all the lines
85 private int initialIndentationLevel;
88 * Used to split a line.
93 * To remember the offset between the beginning of the line and the
94 * beginning of the comment.
96 int currentCommentOffset;
97 int currentLineIndentationLevel;
99 private boolean containsOpenCloseBraces;
100 private int indentationLevelForOpenCloseBraces;
103 * Collections of positions to map
105 private int[] positionsToMap;
108 * Collections of mapped positions
110 private int[] mappedPositions;
112 private int indexToMap;
114 private int indexInMap;
116 private int globalDelta;
118 private int lineDelta;
120 private int splitDelta;
122 private int beginningOfLineIndex;
124 private int multipleLineCommentCounter;
127 * Creates a new instance of Code Formatter using the given settings.
129 * @deprecated backport 1.0 internal functionality
131 public CodeFormatter(ConfigurableOption[] settings) {
132 this(convertConfigurableOptions(settings));
136 * Creates a new instance of Code Formatter using the FormattingOptions object
138 * @deprecated Use CodeFormatter(ConfigurableOption[]) instead
140 public CodeFormatter() {
144 * Creates a new instance of Code Formatter using the given settings.
146 public CodeFormatter(Map settings) {
148 // initialize internal state
149 constructionsCount = 0;
150 constructions = new int[10];
151 currentLineIndentationLevel = indentationLevel = initialIndentationLevel;
152 currentCommentOffset = -1;
154 // initialize primary and secondary scanners
155 scanner = new Scanner(true /*comment*/
156 , true /*whitespace*/
159 ); // regular scanner for forming lines
160 scanner.recordLineSeparator = true;
162 // to remind of the position of the beginning of the line.
163 splitScanner = new Scanner(true /*comment*/
164 , true /*whitespace*/
168 // secondary scanner to split long lines formed by primary scanning
170 // initialize current line buffer
171 currentLineBuffer = new StringBuffer();
172 this.options = new FormatterOptions(settings);
176 * Returns true if a lineSeparator has to be inserted before <code>operator</code>
179 private static boolean breakLineBeforeOperator(int operator) {
181 case TokenNameCOMMA :
182 case TokenNameSEMICOLON :
183 case TokenNameEQUAL :
191 * @deprecated backport 1.0 internal functionality
193 private static Map convertConfigurableOptions(ConfigurableOption[] settings) {
194 Hashtable options = new Hashtable(10);
196 for (int i = 0; i < settings.length; i++) {
197 if (settings[i].getComponentName().equals(CodeFormatter.class.getName())) {
198 String optionName = settings[i].getOptionName();
199 int valueIndex = settings[i].getCurrentValueIndex();
201 if (optionName.equals("newline.openingBrace")) { //$NON-NLS-1$
202 options.put("net.sourceforge.phpdt.core.formatter.newline.openingBrace", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
204 } else if (optionName.equals("newline.controlStatement")) { //$NON-NLS-1$
205 options.put("net.sourceforge.phpdt.core.formatter.newline.controlStatement", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
207 } else if (optionName.equals("newline.clearAll")) { //$NON-NLS-1$
208 options.put("net.sourceforge.phpdt.core.formatter.newline.clearAll", valueIndex == 0 ? "clear all" : "preserve one"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
210 } else if (optionName.equals("newline.elseIf")) { //$NON-NLS-1$
211 options.put("net.sourceforge.phpdt.core.formatter.newline.elseIf", valueIndex == 0 ? "do not insert" : "insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
213 } else if (optionName.equals("newline.emptyBlock")) { //$NON-NLS-1$
214 options.put("net.sourceforge.phpdt.core.formatter.newline.emptyBlock", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
216 } else if (optionName.equals("lineSplit")) { //$NON-NLS-1$
217 options.put("net.sourceforge.phpdt.core.formatter.lineSplit", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$
219 } else if (optionName.equals("style.assignment")) { //$NON-NLS-1$
220 options.put("net.sourceforge.phpdt.core.formatter.style.assignment", valueIndex == 0 ? "compact" : "normal"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
222 } else if (optionName.equals("tabulation.char")) { //$NON-NLS-1$
223 options.put("net.sourceforge.phpdt.core.formatter.tabulation.char", valueIndex == 0 ? "tab" : "space"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
225 } else if (optionName.equals("tabulation.size")) { //$NON-NLS-1$
226 options.put("net.sourceforge.phpdt.core.formatter.tabulation.size", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$
235 * Returns the end of the source code.
237 private final String copyRemainingSource() {
238 char str[] = scanner.source;
239 int startPosition = scanner.startPosition;
240 int length = str.length - startPosition;
241 StringBuffer bufr = new StringBuffer(length);
242 if (startPosition < str.length) {
243 bufr.append(str, startPosition, length);
245 return (bufr.toString());
249 * Inserts <code>tabCount</code> tab character or their equivalent number of spaces.
251 private void dumpTab(int tabCount) {
252 if (options.indentWithTab) {
253 for (int j = 0; j < tabCount; j++) {
254 formattedSource.append('\t');
255 increaseSplitDelta(1);
258 for (int i = 0, max = options.tabSize * tabCount; i < max; i++) {
259 formattedSource.append(' ');
260 increaseSplitDelta(1);
266 * Dumps <code>currentLineBuffer</code> into the formatted string.
268 private void flushBuffer() {
269 String currentString = currentLineBuffer.toString();
271 beginningOfLineIndex = formattedSource.length();
272 if (containsOpenCloseBraces) {
273 containsOpenCloseBraces = false;
274 outputLine(currentString, false, indentationLevelForOpenCloseBraces, 0, -1, null, 0);
275 indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
277 outputLine(currentString, false, currentLineIndentationLevel, 0, -1, null, 0);
279 int scannerSourceLength = scanner.source.length;
280 if (scannerSourceLength > 2) {
281 if (scanner.source[scannerSourceLength - 1] == '\n' && scanner.source[scannerSourceLength - 2] == '\r') {
282 formattedSource.append(options.lineSeparatorSequence);
283 increaseGlobalDelta(options.lineSeparatorSequence.length - 2);
284 } else if (scanner.source[scannerSourceLength - 1] == '\n') {
285 formattedSource.append(options.lineSeparatorSequence);
286 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
287 } else if (scanner.source[scannerSourceLength - 1] == '\r') {
288 formattedSource.append(options.lineSeparatorSequence);
289 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
292 updateMappedPositions(scanner.startPosition);
296 * Formats the input string.
298 private void format() {
300 int previousToken = 0;
301 int previousCompilableToken = 0;
302 int indentationOffset = 0;
303 int newLinesInWhitespace = 0;
305 // number of new lines in the previous whitespace token
306 // (used to leave blank lines before comments)
307 int pendingNewLines = 0;
308 boolean expectingOpenBrace = false;
309 boolean clearNonBlockIndents = false;
310 // true if all indentations till the 1st { (usefull after } or ;)
311 boolean pendingSpace = true;
312 boolean pendingNewlineAfterParen = false;
313 // true when a cr is to be put after a ) (in conditional statements)
314 boolean inAssignment = false;
315 boolean inArrayAssignment = false;
316 boolean inThrowsClause = false;
317 boolean inClassOrInterfaceHeader = false;
319 // openBracketCount is used to count the number of open brackets not closed yet.
320 int openBracketCount = 0;
321 int unarySignModifier = 0;
323 // openParenthesis[0] is used to count the parenthesis not belonging to a condition
324 // (eg foo();). parenthesis in for (...) are count elsewhere in the array.
325 int openParenthesisCount = 1;
326 int[] openParenthesis = new int[10];
328 // tokenBeforeColon is used to know what token goes along with the current :
329 // it can be case or ?
330 int tokenBeforeColonCount = 0;
331 int[] tokenBeforeColon = new int[10];
333 constructionsCount = 0; // initializes the constructions count.
335 // contains DO if in a DO..WHILE statement, UNITIALIZED otherwise.
338 // fix for 1FF17XY: LFCOM:ALL - Format problem on not matching } and else
339 boolean specialElse = false;
341 // OPTION (IndentationLevel): initial indentation level may be non-zero.
342 currentLineIndentationLevel += constructionsCount;
344 // An InvalidInputException exception might cause the termination of this loop.
347 // Get the next token. Catch invalid input and output it
348 // with minimal formatting, also catch end of input and
351 token = scanner.getNextToken();
353 // Patch for line comment
354 // See PR http://dev.eclipse.org/bugs/show_bug.cgi?id=23096
355 if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
356 int length = scanner.currentPosition;
357 loop : for (int index = length - 1; index >= 0; index--) {
358 switch (scanner.source[index]) {
361 scanner.currentPosition--;
368 } catch (InvalidInputException e) {
369 if (!handleInvalidToken(e)) {
374 if (token == Scanner.TokenNameEOF)
377 /* ## MODIFYING the indentation level before generating new lines
378 and indentation in the output string
381 // Removes all the indentations made by statements not followed by a block
382 // except if the current token is ELSE, CATCH or if we are in a switch/case
383 if (clearNonBlockIndents && (token != Scanner.TokenNameWHITESPACE)) {
386 if (constructionsCount > 0 && constructions[constructionsCount - 1] == TokenNameelse) {
390 indentationLevel += popInclusiveUntil(TokenNameif);
392 // case TokenNamecatch :
393 // indentationLevel += popInclusiveUntil(TokenNamecatch);
395 // case TokenNamefinally :
396 // indentationLevel += popInclusiveUntil(TokenNamecatch);
398 case TokenNamewhile :
399 if (nlicsToken == TokenNamedo) {
400 indentationLevel += pop(TokenNamedo);
404 indentationLevel += popExclusiveUntilBlockOrCase();
405 // clear until a CASE, DEFAULT or BLOCK is encountered.
406 // Thus, the indentationLevel is correctly cleared either
407 // in a switch/case statement or in any other situation.
409 clearNonBlockIndents = false;
411 // returns to the indentation level created by the SWITCH keyword
412 // if the current token is a CASE or a DEFAULT
413 if (token == TokenNamecase || token == TokenNamedefault) {
414 indentationLevel += pop(TokenNamecase);
416 // if (token == Scanner.TokenNamethrows) {
417 // inThrowsClause = true;
419 if ((token == Scanner.TokenNameclass // || token == Scanner.TokenNameinterface
421 && previousToken != Scanner.TokenNameDOT) {
422 inClassOrInterfaceHeader = true;
425 /* ## APPEND newlines and indentations to the output string
427 // Do not add a new line between ELSE and IF, if the option elseIfOnSameLine is true.
428 // Fix for 1ETLWPZ: IVJCOM:ALL - incorrect "else if" formatting
429 // if (pendingNewlineAfterParen
430 // && previousCompilableToken == TokenNameelse
431 // && token == TokenNameif
432 // && options.compactElseIfMode) {
433 // pendingNewlineAfterParen = false;
434 // pendingNewLines = 0;
435 // indentationLevel += pop(TokenNameelse);
436 // // because else if is now one single statement,
437 // // the indentation level after it is increased by one and not by 2
438 // // (else = 1 indent, if = 1 indent, but else if = 1 indent, not 2).
440 // Add a newline & indent to the formatted source string if
441 // a for/if-else/while statement was scanned and there is no block
443 pendingNewlineAfterParen =
444 pendingNewlineAfterParen || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE);
445 if (pendingNewlineAfterParen && token != Scanner.TokenNameWHITESPACE) {
446 pendingNewlineAfterParen = false;
448 // Do to add a newline & indent sequence if the current token is an
449 // open brace or a period or if the current token is a semi-colon and the
450 // previous token is a close paren.
451 // add a new line if a parenthesis belonging to a for() statement
452 // has been closed and the current token is not an opening brace
453 if (token != TokenNameLBRACE
454 && !isComment(token) // to avoid adding new line between else and a comment
455 && token != TokenNameDOT
456 && !(previousCompilableToken == TokenNameRPAREN && token == TokenNameSEMICOLON)) {
458 currentLineIndentationLevel = indentationLevel;
460 pendingSpace = false;
462 if (token == TokenNameLBRACE && options.newLineBeforeOpeningBraceMode) {
464 if (constructionsCount > 0
465 && constructions[constructionsCount - 1] != BLOCK
466 && constructions[constructionsCount - 1] != NONINDENT_BLOCK) {
467 currentLineIndentationLevel = indentationLevel - 1;
469 currentLineIndentationLevel = indentationLevel;
472 pendingSpace = false;
476 if (token == TokenNameLBRACE
477 && options.newLineBeforeOpeningBraceMode
478 && constructionsCount > 0
479 && constructions[constructionsCount - 1] == TokenNamedo) {
481 currentLineIndentationLevel = indentationLevel - 1;
483 pendingSpace = false;
486 if (token == TokenNameLBRACE && inThrowsClause) {
487 inThrowsClause = false;
488 if (options.newLineBeforeOpeningBraceMode) {
490 currentLineIndentationLevel = indentationLevel;
492 pendingSpace = false;
496 if (token == TokenNameLBRACE && inClassOrInterfaceHeader) {
497 inClassOrInterfaceHeader = false;
498 if (options.newLineBeforeOpeningBraceMode) {
500 currentLineIndentationLevel = indentationLevel;
502 pendingSpace = false;
505 // Add pending new lines to the formatted source string.
506 // Note: pending new lines are not added if the current token
507 // is a single line comment or whitespace.
508 // if the comment is between parenthesis, there is no blank line preservation
509 // (if it's a one-line comment, a blank line is added after it).
510 if (((pendingNewLines > 0 && (!isComment(token)))
511 || (newLinesInWhitespace > 0 && (openParenthesisCount <= 1 && isComment(token)))
512 || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE))
513 && token != Scanner.TokenNameWHITESPACE) {
515 // Do not add newline & indent between an adjoining close brace and
516 // close paren. Anonymous inner classes may use this form.
517 boolean closeBraceAndCloseParen = previousToken == TokenNameRBRACE && token == TokenNameRPAREN;
519 // OPTION (NewLineInCompoundStatement): do not add newline & indent
520 // between close brace and else, (do) while, catch, and finally if
521 // newlineInCompoundStatement is true.
522 boolean nlicsOption =
523 previousToken == TokenNameRBRACE
524 && !options.newlineInControlStatementMode
525 && (token == TokenNameelse || (token == TokenNamewhile && nlicsToken == TokenNamedo));
526 // || token == TokenNamecatch
527 // || token == TokenNamefinally);
529 // Do not add a newline & indent between a close brace and semi-colon.
530 boolean semiColonAndCloseBrace = previousToken == TokenNameRBRACE && token == TokenNameSEMICOLON;
532 // Do not add a new line & indent between a multiline comment and a opening brace
533 boolean commentAndOpenBrace = previousToken == Scanner.TokenNameCOMMENT_BLOCK && token == TokenNameLBRACE;
535 // Do not add a newline & indent between a close brace and a colon (in array assignments, for example).
536 boolean commaAndCloseBrace = previousToken == TokenNameRBRACE && token == TokenNameCOMMA;
538 // Add a newline and indent, if appropriate.
540 || (!commentAndOpenBrace && !closeBraceAndCloseParen && !nlicsOption && !semiColonAndCloseBrace && !commaAndCloseBrace)) {
542 // if clearAllBlankLinesMode=false, leaves the blank lines
543 // inserted by the user
544 // if clearAllBlankLinesMode=true, removes all of then
545 // and insert only blank lines required by the formatting.
546 if (!options.clearAllBlankLinesMode) {
547 // (isComment(token))
548 pendingNewLines = (pendingNewLines < newLinesInWhitespace) ? newLinesInWhitespace : pendingNewLines;
549 pendingNewLines = (pendingNewLines > 2) ? 2 : pendingNewLines;
551 if (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE) {
552 containsOpenCloseBraces = true;
553 indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
554 if (isComment(previousToken)) {
555 newLine(pendingNewLines);
557 /* if (!(constructionsCount > 1
558 && constructions[constructionsCount-1] == NONINDENT_BLOCK
559 && (constructions[constructionsCount-2] == TokenNamefor
560 || constructions[constructionsCount-2] == TokenNamewhile))) {*/
561 if (options.newLineInEmptyBlockMode) {
562 if (inArrayAssignment) {
563 newLine(1); // array assigment with an empty block
565 newLine(pendingNewLines);
571 // see PR 1FKKC3U: LFCOM:WINNT - Format problem with a comment before the ';'
572 if (!((previousToken == Scanner.TokenNameCOMMENT_BLOCK || previousToken == Scanner.TokenNameCOMMENT_PHPDOC)
573 && token == TokenNameSEMICOLON)) {
574 newLine(pendingNewLines);
577 if (((previousCompilableToken == TokenNameSEMICOLON)
578 || (previousCompilableToken == TokenNameLBRACE)
579 || (previousCompilableToken == TokenNameRBRACE)
580 || (isComment(previousToken)))
581 && (token == TokenNameRBRACE)) {
582 indentationOffset = -1;
583 indentationLevel += popExclusiveUntilBlock();
585 if (previousToken == Scanner.TokenNameCOMMENT_LINE && inAssignment) {
587 currentLineIndentationLevel++;
589 currentLineIndentationLevel = indentationLevel + indentationOffset;
591 pendingSpace = false;
592 indentationOffset = 0;
595 newLinesInWhitespace = 0;
598 if (nlicsToken == TokenNamedo && token == TokenNamewhile) {
604 // case TokenNamefinally :
605 expectingOpenBrace = true;
606 pendingNewlineAfterParen = true;
607 indentationLevel += pushControlStatement(token);
610 case TokenNamedefault :
611 if (tokenBeforeColonCount == tokenBeforeColon.length) {
615 (tokenBeforeColon = new int[tokenBeforeColonCount * 2]),
617 tokenBeforeColonCount);
619 tokenBeforeColon[tokenBeforeColonCount++] = TokenNamecase;
620 indentationLevel += pushControlStatement(TokenNamecase);
622 case TokenNameQUESTION :
623 if (tokenBeforeColonCount == tokenBeforeColon.length) {
627 (tokenBeforeColon = new int[tokenBeforeColonCount * 2]),
629 tokenBeforeColonCount);
631 tokenBeforeColon[tokenBeforeColonCount++] = token;
633 case TokenNameswitch :
636 case TokenNamewhile :
637 if (openParenthesisCount == openParenthesis.length) {
638 System.arraycopy(openParenthesis, 0, (openParenthesis = new int[openParenthesisCount * 2]), 0, openParenthesisCount);
640 openParenthesis[openParenthesisCount++] = 0;
641 expectingOpenBrace = true;
643 indentationLevel += pushControlStatement(token);
645 // case TokenNametry :
646 // pendingNewlineAfterParen = true;
647 // case TokenNamecatch :
648 // // several CATCH statements can be contiguous.
649 // // a CATCH is encountered pop until first CATCH (if a CATCH follows a TRY it works the same way,
650 // // as CATCH and TRY are the same token in the stack).
651 // expectingOpenBrace = true;
652 // indentationLevel += pushControlStatement(TokenNamecatch);
656 expectingOpenBrace = true;
657 indentationLevel += pushControlStatement(token);
662 case TokenNameLPAREN :
663 // if (previousToken == TokenNamesynchronized) {
664 // indentationLevel += pushControlStatement(previousToken);
666 // Put a space between the previous and current token if the
667 // previous token was not a keyword, open paren, logical
668 // compliment (eg: !), semi-colon, open brace, close brace,
670 if (previousCompilableToken != TokenNameLBRACKET
671 && previousToken != TokenNameIdentifier
672 && previousToken != 0
673 && previousToken != TokenNameNOT
674 && previousToken != TokenNameLPAREN
675 && previousToken != TokenNameTWIDDLE
676 && previousToken != TokenNameSEMICOLON
677 && previousToken != TokenNameLBRACE
678 && previousToken != TokenNameRBRACE) {
679 // && previousToken != TokenNamesuper
680 // && previousToken != TokenNamethis) {
683 // If in a for/if/while statement, increase the parenthesis count
684 // for the current openParenthesisCount
685 // else increase the count for stand alone parenthesis.
686 if (openParenthesisCount > 0)
687 openParenthesis[openParenthesisCount - 1]++;
689 openParenthesis[0]++;
691 pendingSpace = false;
694 case TokenNameRPAREN :
696 // Decrease the parenthesis count
697 // if there is no more unclosed parenthesis,
698 // a new line and indent may be append (depending on the next token).
699 if ((openParenthesisCount > 1) && (openParenthesis[openParenthesisCount - 1] > 0)) {
700 openParenthesis[openParenthesisCount - 1]--;
701 if (openParenthesis[openParenthesisCount - 1] <= 0) {
702 pendingNewlineAfterParen = true;
703 inAssignment = false;
704 openParenthesisCount--;
707 openParenthesis[0]--;
709 pendingSpace = false;
711 case TokenNameLBRACE :
712 if ((previousCompilableToken == TokenNameRBRACKET) || (previousCompilableToken == TokenNameEQUAL)) {
713 // if (previousCompilableToken == TokenNameRBRACKET) {
714 inArrayAssignment = true;
715 inAssignment = false;
717 if (inArrayAssignment) {
718 indentationLevel += pushBlock();
720 // Add new line and increase indentation level after open brace.
722 indentationLevel += pushBlock();
725 case TokenNameRBRACE :
726 if (previousCompilableToken == TokenNameRPAREN) {
727 pendingSpace = false;
729 if (inArrayAssignment) {
730 inArrayAssignment = false;
732 indentationLevel += popInclusiveUntilBlock();
735 indentationLevel += popInclusiveUntilBlock();
737 if (previousCompilableToken == TokenNameRPAREN) {
738 // fix for 1FGDDV6: LFCOM:WIN98 - Weird splitting on message expression
739 currentLineBuffer.append(options.lineSeparatorSequence);
740 increaseLineDelta(options.lineSeparatorSequence.length);
742 if (constructionsCount > 0) {
743 switch (constructions[constructionsCount - 1]) {
745 //indentationLevel += popExclusiveUntilBlock();
747 case TokenNameswitch :
750 // case TokenNametry :
751 // case TokenNamecatch :
752 // case TokenNamefinally :
753 case TokenNamewhile :
755 // case TokenNamesynchronized :
756 clearNonBlockIndents = true;
763 case TokenNameLBRACKET :
765 pendingSpace = false;
767 case TokenNameRBRACKET :
768 openBracketCount -= (openBracketCount > 0) ? 1 : 0;
769 // if there is no left bracket to close, the right bracket is ignored.
770 pendingSpace = false;
772 case TokenNameCOMMA :
774 pendingSpace = false;
776 case TokenNameSEMICOLON :
778 // Do not generate line terminators in the definition of
779 // the for statement.
780 // if not in this case, jump a line and reduce indentation after the brace
781 // if the block it closes belongs to a conditional statement (if, while, do...).
782 if (openParenthesisCount <= 1) {
784 if (expectingOpenBrace) {
785 clearNonBlockIndents = true;
786 expectingOpenBrace = false;
789 inAssignment = false;
790 pendingSpace = false;
792 case TokenNamePLUS_PLUS :
793 case TokenNameMINUS_MINUS :
795 // Do not put a space between a post-increment/decrement
796 // and the identifier being modified.
797 if (previousToken == TokenNameIdentifier || previousToken == TokenNameRBRACKET) {
798 pendingSpace = false;
801 case TokenNamePLUS : // previously ADDITION
802 case TokenNameMINUS :
804 // Handle the unary operators plus and minus via a flag
805 if (!isLiteralToken(previousToken)
806 && previousToken != TokenNameIdentifier
807 && previousToken != TokenNameRPAREN
808 && previousToken != TokenNameRBRACKET) {
809 unarySignModifier = 1;
812 case TokenNameCOLON :
813 // In a switch/case statement, add a newline & indent
814 // when a colon is encountered.
815 if (tokenBeforeColonCount > 0) {
816 if (tokenBeforeColon[tokenBeforeColonCount - 1] == TokenNamecase) {
819 tokenBeforeColonCount--;
822 case TokenNameEQUAL :
825 case Scanner.TokenNameCOMMENT_LINE :
828 currentLineIndentationLevel++;
830 break; // a line is always inserted after a one-line comment
831 case Scanner.TokenNameCOMMENT_PHPDOC :
832 case Scanner.TokenNameCOMMENT_BLOCK :
833 currentCommentOffset = getCurrentCommentOffset();
836 case Scanner.TokenNameWHITESPACE :
838 // Count the number of line terminators in the whitespace so
839 // line spacing can be preserved near comments.
840 char[] source = scanner.source;
841 newLinesInWhitespace = 0;
842 for (int i = scanner.startPosition, max = scanner.currentPosition; i < max; i++) {
843 if (source[i] == '\r') {
845 if (source[++i] == '\n') {
846 newLinesInWhitespace++;
848 newLinesInWhitespace++;
851 newLinesInWhitespace++;
853 } else if (source[i] == '\n') {
854 newLinesInWhitespace++;
857 increaseLineDelta(scanner.startPosition - scanner.currentPosition);
859 // case TokenNameHTML :
860 // // Add the next token to the formatted source string.
861 // // outputCurrentToken(token);
862 // int startPosition = scanner.startPosition;
864 // for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
865 // char currentCharacter = scanner.source[i];
866 // updateMappedPositions(i);
867 // currentLineBuffer.append(currentCharacter);
871 if ((token == TokenNameIdentifier) || isLiteralToken(token)) {
872 // || token == TokenNamesuper
873 // || token == TokenNamethis) {
875 // Do not put a space between a unary operator
876 // (eg: ++, --, +, -) and the identifier being modified.
877 if (previousToken == TokenNamePLUS_PLUS
878 || previousToken == TokenNameMINUS_MINUS
879 || (previousToken == TokenNamePLUS && unarySignModifier > 0)
880 || (previousToken == TokenNameMINUS && unarySignModifier > 0)) {
881 pendingSpace = false;
883 unarySignModifier = 0;
887 // Do not output whitespace tokens.
888 if (token != Scanner.TokenNameWHITESPACE) {
890 /* Add pending space to the formatted source string.
891 Do not output a space under the following circumstances:
892 1) this is the first pass
893 2) previous token is an open paren
894 3) previous token is a period
895 4) previous token is the logical compliment (eg: !)
896 5) previous token is the bitwise compliment (eg: ~)
897 6) previous token is the open bracket (eg: [)
898 7) in an assignment statement, if the previous token is an
899 open brace or the current token is a close brace
900 8) previous token is a single line comment
902 boolean openAndCloseBrace = previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE;
905 && insertSpaceAfter(previousToken)
906 && !(inAssignment && (previousToken == TokenNameLBRACE || token == TokenNameRBRACE))
907 && previousToken != Scanner.TokenNameCOMMENT_LINE) {
908 if ((!(options.compactAssignmentMode && token == TokenNameEQUAL)) && !openAndCloseBrace)
911 // Add the next token to the formatted source string.
912 outputCurrentToken(token);
913 if (token == Scanner.TokenNameCOMMENT_LINE && openParenthesisCount > 1) {
915 currentLineBuffer.append(options.lineSeparatorSequence);
916 increaseLineDelta(options.lineSeparatorSequence.length);
920 // Whitespace tokens do not need to be remembered.
921 if (token != Scanner.TokenNameWHITESPACE) {
922 previousToken = token;
923 if (token != Scanner.TokenNameCOMMENT_BLOCK
924 && token != Scanner.TokenNameCOMMENT_LINE
925 && token != Scanner.TokenNameCOMMENT_PHPDOC) {
926 previousCompilableToken = token;
930 output(copyRemainingSource());
932 // dump the last token of the source in the formatted output.
933 } catch (InvalidInputException e) {
934 output(copyRemainingSource());
936 // dump the last token of the source in the formatted output.
941 * Formats the char array <code>sourceString</code>,
942 * and returns a string containing the formatted version.
943 * @return the formatted ouput.
945 public String formatSourceString(String sourceString) {
946 char[] sourceChars = sourceString.toCharArray();
947 formattedSource = new StringBuffer(sourceChars.length);
948 scanner.setSource(sourceChars);
950 return formattedSource.toString();
954 * Formats the char array <code>sourceString</code>,
955 * and returns a string containing the formatted version.
956 * @param string the string to format
957 * @param indentationLevel the initial indentation level
958 * @return the formatted ouput.
960 public String format(String string, int indentationLevel) {
961 return format(string, indentationLevel, (int[]) null);
965 * Formats the char array <code>sourceString</code>,
966 * and returns a string containing the formatted version.
967 * The positions array is modified to contain the mapped positions.
968 * @param string the string to format
969 * @param indentationLevel the initial indentation level
970 * @param positions the array of positions to map
971 * @return the formatted ouput.
973 public String format(String string, int indentationLevel, int[] positions) {
974 return this.format(string, indentationLevel, positions, null);
977 public String format(String string, int indentationLevel, int[] positions, String lineSeparator) {
978 if (lineSeparator != null) {
979 this.options.setLineSeparator(lineSeparator);
981 if (positions != null) {
982 this.setPositionsToMap(positions);
983 this.setInitialIndentationLevel(indentationLevel);
984 String formattedString = this.formatSourceString(string);
985 int[] mappedPositions = this.getMappedPositions();
986 System.arraycopy(mappedPositions, 0, positions, 0, positions.length);
987 return formattedString;
989 this.setInitialIndentationLevel(indentationLevel);
990 return this.formatSourceString(string);
994 * Formats the char array <code>sourceString</code>,
995 * and returns a string containing the formatted version. The initial indentation level is 0.
996 * @param string the string to format
997 * @return the formatted ouput.
999 public String format(String string) {
1000 return this.format(string, 0, (int[]) null);
1004 * Formats a given source string, starting indenting it at a particular
1005 * depth and using the given options
1007 * @deprecated backport 1.0 internal functionality
1009 public static String format(String sourceString, int initialIndentationLevel, ConfigurableOption[] options) {
1010 CodeFormatter formatter = new CodeFormatter(options);
1011 formatter.setInitialIndentationLevel(initialIndentationLevel);
1012 return formatter.formatSourceString(sourceString);
1016 * Returns the number of characters and tab char between the beginning of the line
1017 * and the beginning of the comment.
1019 private int getCurrentCommentOffset() {
1020 int linePtr = scanner.linePtr;
1021 // if there is no beginning of line, return 0.
1025 int beginningOfLine = scanner.lineEnds[linePtr];
1026 int currentStartPosition = scanner.startPosition;
1027 char[] source = scanner.source;
1029 // find the position of the beginning of the line containing the comment
1030 while (beginningOfLine > currentStartPosition) {
1032 beginningOfLine = scanner.lineEnds[--linePtr];
1034 beginningOfLine = 0;
1038 for (int i = currentStartPosition - 1; i >= beginningOfLine; i--) {
1039 char currentCharacter = source[i];
1040 switch (currentCharacter) {
1042 offset += options.tabSize;
1058 * Returns an array of descriptions for the configurable options.
1059 * The descriptions may be changed and passed back to a different
1062 * @deprecated backport 1.0 internal functionality
1064 public static ConfigurableOption[] getDefaultOptions(Locale locale) {
1065 String componentName = CodeFormatter.class.getName();
1066 FormatterOptions options = new FormatterOptions();
1067 return new ConfigurableOption[] { new ConfigurableOption(componentName, "newline.openingBrace", locale, options.newLineBeforeOpeningBraceMode ? 0 : 1), //$NON-NLS-1$
1068 new ConfigurableOption(componentName, "newline.controlStatement", locale, options.newlineInControlStatementMode ? 0 : 1), //$NON-NLS-1$
1069 new ConfigurableOption(componentName, "newline.clearAll", locale, options.clearAllBlankLinesMode ? 0 : 1), //$NON-NLS-1$
1070 // new ConfigurableOption(componentName, "newline.elseIf", locale, options.compactElseIfMode ? 0 : 1), //$NON-NLS-1$
1071 new ConfigurableOption(componentName, "newline.emptyBlock", locale, options.newLineInEmptyBlockMode ? 0 : 1), //$NON-NLS-1$
1072 new ConfigurableOption(componentName, "line.split", locale, options.maxLineLength), //$NON-NLS-1$
1073 new ConfigurableOption(componentName, "style.compactAssignment", locale, options.compactAssignmentMode ? 0 : 1), //$NON-NLS-1$
1074 new ConfigurableOption(componentName, "tabulation.char", locale, options.indentWithTab ? 0 : 1), //$NON-NLS-1$
1075 new ConfigurableOption(componentName, "tabulation.size", locale, options.tabSize) //$NON-NLS-1$
1080 * Returns the array of mapped positions.
1081 * Returns null is no positions have been set.
1083 * @deprecated There is no need to retrieve the mapped positions anymore.
1085 public int[] getMappedPositions() {
1086 return mappedPositions;
1090 * Returns the priority of the token given as argument<br>
1091 * The most prioritary the token is, the smallest the return value is.
1092 * @return the priority of <code>token</code>
1093 * @param token the token of which the priority is requested
1095 private static int getTokenPriority(int token) {
1097 case TokenNameextends :
1098 // case TokenNameimplements :
1099 // case TokenNamethrows :
1101 case TokenNameSEMICOLON : // ;
1103 case TokenNameCOMMA : // ,
1105 case TokenNameEQUAL : // =
1107 case TokenNameAND_AND : // &&
1108 case TokenNameOR_OR : // ||
1110 case TokenNameQUESTION : // ?
1111 case TokenNameCOLON : // :
1112 return 50; // it's better cutting on ?: than on ;
1113 case TokenNameEQUAL_EQUAL : // ==
1114 case TokenNameNOT_EQUAL : // !=
1116 case TokenNameLESS : // <
1117 case TokenNameLESS_EQUAL : // <=
1118 case TokenNameGREATER : // >
1119 case TokenNameGREATER_EQUAL : // >=
1120 // case TokenNameinstanceof : // instanceof
1122 case TokenNamePLUS : // +
1123 case TokenNameMINUS : // -
1125 case TokenNameMULTIPLY : // *
1126 case TokenNameDIVIDE : // /
1127 case TokenNameREMAINDER : // %
1129 case TokenNameLEFT_SHIFT : // <<
1130 case TokenNameRIGHT_SHIFT : // >>
1131 // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>>
1133 case TokenNameAND : // &
1134 case TokenNameOR : // |
1135 case TokenNameXOR : // ^
1137 case TokenNameMULTIPLY_EQUAL : // *=
1138 case TokenNameDIVIDE_EQUAL : // /=
1139 case TokenNameREMAINDER_EQUAL : // %=
1140 case TokenNamePLUS_EQUAL : // +=
1141 case TokenNameMINUS_EQUAL : // -=
1142 case TokenNameLEFT_SHIFT_EQUAL : // <<=
1143 case TokenNameRIGHT_SHIFT_EQUAL : // >>=
1144 // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>=
1145 case TokenNameAND_EQUAL : // &=
1146 case TokenNameXOR_EQUAL : // ^=
1147 case TokenNameOR_EQUAL : // |=
1149 case TokenNameDOT : // .
1152 return Integer.MAX_VALUE;
1157 * Handles the exception raised when an invalid token is encountered.
1158 * Returns true if the exception has been handled, false otherwise.
1160 private boolean handleInvalidToken(Exception e) {
1161 if (e.getMessage().equals(Scanner.INVALID_CHARACTER_CONSTANT)
1162 || e.getMessage().equals(Scanner.INVALID_CHAR_IN_STRING)
1163 || e.getMessage().equals(Scanner.INVALID_ESCAPE)) {
1169 private final void increaseGlobalDelta(int offset) {
1170 globalDelta += offset;
1173 private final void increaseLineDelta(int offset) {
1174 lineDelta += offset;
1177 private final void increaseSplitDelta(int offset) {
1178 splitDelta += offset;
1182 * Returns true if a space has to be inserted after <code>operator</code>
1185 private boolean insertSpaceAfter(int token) {
1187 case TokenNameLPAREN :
1189 case TokenNameTWIDDLE :
1191 case 0 : // no token
1192 case TokenNameLBRACKET :
1193 case Scanner.TokenNameCOMMENT_LINE :
1201 * Returns true if a space has to be inserted before <code>operator</code>
1202 * false otherwise.<br>
1203 * Cannot be static as it uses the code formatter options
1204 * (to know if the compact assignment mode is on).
1206 private boolean insertSpaceBefore(int token) {
1208 case TokenNameEQUAL :
1209 return (!options.compactAssignmentMode);
1215 private static boolean isComment(int token) {
1217 token == Scanner.TokenNameCOMMENT_BLOCK || token == Scanner.TokenNameCOMMENT_LINE || token == Scanner.TokenNameCOMMENT_PHPDOC;
1221 private static boolean isLiteralToken(int token) {
1222 boolean result = token == TokenNameIntegerLiteral
1223 // || token == TokenNameLongLiteral
1224 // || token == TokenNameFloatingPointLiteral
1225 || token == TokenNameDoubleLiteral
1226 // || token == TokenNameCharacterLiteral
1227 || token == TokenNameStringLiteral;
1232 * If the length of <code>oneLineBuffer</code> exceeds <code>maxLineLength</code>,
1233 * it is split and the result is dumped in <code>formattedSource</code>
1234 * @param newLineCount the number of new lines to append
1236 private void newLine(int newLineCount) {
1238 // format current line
1240 beginningOfLineIndex = formattedSource.length();
1241 String currentLine = currentLineBuffer.toString();
1242 if (containsOpenCloseBraces) {
1243 containsOpenCloseBraces = false;
1244 outputLine(currentLine, false, indentationLevelForOpenCloseBraces, 0, -1, null, 0);
1245 indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
1247 outputLine(currentLine, false, currentLineIndentationLevel, 0, -1, null, 0);
1249 // dump line break(s)
1250 for (int i = 0; i < newLineCount; i++) {
1251 formattedSource.append(options.lineSeparatorSequence);
1252 increaseSplitDelta(options.lineSeparatorSequence.length);
1254 // reset formatter for next line
1255 int currentLength = currentLine.length();
1256 currentLineBuffer = new StringBuffer(currentLength > maxLineSize ? maxLineSize = currentLength : maxLineSize);
1258 increaseGlobalDelta(splitDelta);
1259 increaseGlobalDelta(lineDelta);
1261 currentLineIndentationLevel = initialIndentationLevel;
1264 private String operatorString(int operator) {
1266 case TokenNameextends :
1267 return "extends"; //$NON-NLS-1$
1269 // case TokenNameimplements :
1270 // return "implements"; //$NON-NLS-1$
1272 // case TokenNamethrows :
1273 // return "throws"; //$NON-NLS-1$
1275 case TokenNameSEMICOLON : // ;
1276 return ";"; //$NON-NLS-1$
1278 case TokenNameCOMMA : // ,
1279 return ","; //$NON-NLS-1$
1281 case TokenNameEQUAL : // =
1282 return "="; //$NON-NLS-1$
1284 case TokenNameAND_AND : // && (15.22)
1285 return "&&"; //$NON-NLS-1$
1287 case TokenNameOR_OR : // || (15.23)
1288 return "||"; //$NON-NLS-1$
1290 case TokenNameQUESTION : // ? (15.24)
1291 return "?"; //$NON-NLS-1$
1293 case TokenNameCOLON : // : (15.24)
1294 return ":"; //$NON-NLS-1$
1296 case TokenNameEQUAL_EQUAL : // == (15.20, 15.20.1, 15.20.2, 15.20.3)
1297 return "=="; //$NON-NLS-1$
1299 case TokenNameNOT_EQUAL : // != (15.20, 15.20.1, 15.20.2, 15.20.3)
1300 return "!="; //$NON-NLS-1$
1302 case TokenNameLESS : // < (15.19.1)
1303 return "<"; //$NON-NLS-1$
1305 case TokenNameLESS_EQUAL : // <= (15.19.1)
1306 return "<="; //$NON-NLS-1$
1308 case TokenNameGREATER : // > (15.19.1)
1309 return ">"; //$NON-NLS-1$
1311 case TokenNameGREATER_EQUAL : // >= (15.19.1)
1312 return ">="; //$NON-NLS-1$
1314 // case TokenNameinstanceof : // instanceof
1315 // return "instanceof"; //$NON-NLS-1$
1317 case TokenNamePLUS : // + (15.17, 15.17.2)
1318 return "+"; //$NON-NLS-1$
1320 case TokenNameMINUS : // - (15.17.2)
1321 return "-"; //$NON-NLS-1$
1323 case TokenNameMULTIPLY : // * (15.16.1)
1324 return "*"; //$NON-NLS-1$
1326 case TokenNameDIVIDE : // / (15.16.2)
1327 return "/"; //$NON-NLS-1$
1329 case TokenNameREMAINDER : // % (15.16.3)
1330 return "%"; //$NON-NLS-1$
1332 case TokenNameLEFT_SHIFT : // << (15.18)
1333 return "<<"; //$NON-NLS-1$
1335 case TokenNameRIGHT_SHIFT : // >> (15.18)
1336 return ">>"; //$NON-NLS-1$
1338 // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18)
1339 // return ">>>"; //$NON-NLS-1$
1341 case TokenNameAND : // & (15.21, 15.21.1, 15.21.2)
1342 return "&"; //$NON-NLS-1$
1344 case TokenNameOR : // | (15.21, 15.21.1, 15.21.2)
1345 return "|"; //$NON-NLS-1$
1347 case TokenNameXOR : // ^ (15.21, 15.21.1, 15.21.2)
1348 return "^"; //$NON-NLS-1$
1350 case TokenNameMULTIPLY_EQUAL : // *= (15.25.2)
1351 return "*="; //$NON-NLS-1$
1353 case TokenNameDIVIDE_EQUAL : // /= (15.25.2)
1354 return "/="; //$NON-NLS-1$
1356 case TokenNameREMAINDER_EQUAL : // %= (15.25.2)
1357 return "%="; //$NON-NLS-1$
1359 case TokenNamePLUS_EQUAL : // += (15.25.2)
1360 return "+="; //$NON-NLS-1$
1362 case TokenNameMINUS_EQUAL : // -= (15.25.2)
1363 return "-="; //$NON-NLS-1$
1365 case TokenNameLEFT_SHIFT_EQUAL : // <<= (15.25.2)
1366 return "<<="; //$NON-NLS-1$
1368 case TokenNameRIGHT_SHIFT_EQUAL : // >>= (15.25.2)
1369 return ">>="; //$NON-NLS-1$
1371 // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2)
1372 // return ">>>="; //$NON-NLS-1$
1374 case TokenNameAND_EQUAL : // &= (15.25.2)
1375 return "&="; //$NON-NLS-1$
1377 case TokenNameXOR_EQUAL : // ^= (15.25.2)
1378 return "^="; //$NON-NLS-1$
1380 case TokenNameOR_EQUAL : // |= (15.25.2)
1381 return "|="; //$NON-NLS-1$
1383 case TokenNameDOT : // .
1384 return "."; //$NON-NLS-1$
1387 return ""; //$NON-NLS-1$
1392 * Appends <code>stringToOutput</code> to the formatted output.<br>
1393 * If it contains \n, append a LINE_SEPARATOR and indent after it.
1395 private void output(String stringToOutput) {
1396 char currentCharacter;
1397 for (int i = 0, max = stringToOutput.length(); i < max; i++) {
1398 currentCharacter = stringToOutput.charAt(i);
1399 if (currentCharacter != '\t') {
1400 currentLineBuffer.append(currentCharacter);
1406 * Appends <code>token</code> to the formatted output.<br>
1407 * If it contains <code>\n</code>, append a LINE_SEPARATOR and indent after it.
1409 private void outputCurrentToken(int token) {
1410 char[] source = scanner.source;
1411 int startPosition = scanner.startPosition;
1414 case Scanner.TokenNameCOMMENT_PHPDOC :
1415 case Scanner.TokenNameCOMMENT_BLOCK :
1416 case Scanner.TokenNameCOMMENT_LINE :
1417 boolean endOfLine = false;
1418 int currentCommentOffset = getCurrentCommentOffset();
1419 int beginningOfLineSpaces = 0;
1421 currentCommentOffset = getCurrentCommentOffset();
1422 beginningOfLineSpaces = 0;
1423 boolean pendingCarriageReturn = false;
1424 for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
1425 char currentCharacter = source[i];
1426 updateMappedPositions(i);
1427 switch (currentCharacter) {
1429 pendingCarriageReturn = true;
1433 if (pendingCarriageReturn) {
1434 increaseGlobalDelta(options.lineSeparatorSequence.length - 2);
1436 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1438 pendingCarriageReturn = false;
1439 currentLineBuffer.append(options.lineSeparatorSequence);
1440 beginningOfLineSpaces = 0;
1444 if (pendingCarriageReturn) {
1445 pendingCarriageReturn = false;
1446 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1447 currentLineBuffer.append(options.lineSeparatorSequence);
1448 beginningOfLineSpaces = 0;
1452 // we remove a maximum of currentCommentOffset characters (tabs are converted to space numbers).
1453 beginningOfLineSpaces += options.tabSize;
1454 if (beginningOfLineSpaces > currentCommentOffset) {
1455 currentLineBuffer.append(currentCharacter);
1457 increaseGlobalDelta(-1);
1460 currentLineBuffer.append(currentCharacter);
1464 if (pendingCarriageReturn) {
1465 pendingCarriageReturn = false;
1466 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1467 currentLineBuffer.append(options.lineSeparatorSequence);
1468 beginningOfLineSpaces = 0;
1472 // we remove a maximum of currentCommentOffset characters (tabs are converted to space numbers).
1473 beginningOfLineSpaces++;
1474 if (beginningOfLineSpaces > currentCommentOffset) {
1475 currentLineBuffer.append(currentCharacter);
1477 increaseGlobalDelta(-1);
1480 currentLineBuffer.append(currentCharacter);
1484 if (pendingCarriageReturn) {
1485 pendingCarriageReturn = false;
1486 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1487 currentLineBuffer.append(options.lineSeparatorSequence);
1488 beginningOfLineSpaces = 0;
1491 beginningOfLineSpaces = 0;
1492 currentLineBuffer.append(currentCharacter);
1497 updateMappedPositions(scanner.currentPosition - 1);
1498 multipleLineCommentCounter++;
1501 for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
1502 char currentCharacter = source[i];
1503 updateMappedPositions(i);
1504 currentLineBuffer.append(currentCharacter);
1510 * Outputs <code>currentString</code>:<br>
1511 * <ul><li>If its length is < maxLineLength, output
1512 * <li>Otherwise it is split.</ul>
1513 * @param currentString string to output
1514 * @param preIndented whether the string to output was pre-indented
1515 * @param depth number of indentation to put in front of <code>currentString</code>
1516 * @param operator value of the operator belonging to <code>currentString</code>.
1518 private void outputLine(
1519 String currentString,
1520 boolean preIndented,
1524 int[] startSubstringIndexes,
1525 int offsetInGlobalLine) {
1527 boolean emptyFirstSubString = false;
1528 String operatorString = operatorString(operator);
1529 boolean placeOperatorBehind = !breakLineBeforeOperator(operator);
1530 boolean placeOperatorAhead = !placeOperatorBehind;
1532 // dump prefix operator?
1533 if (placeOperatorAhead) {
1538 if (operator != 0) {
1539 if (insertSpaceBefore(operator)) {
1540 formattedSource.append(' ');
1541 increaseSplitDelta(1);
1543 formattedSource.append(operatorString);
1544 increaseSplitDelta(operatorString.length());
1546 if (insertSpaceAfter(operator) // && operator != TokenNameimplements
1547 && operator != TokenNameextends) {
1548 // && operator != TokenNamethrows) {
1549 formattedSource.append(' ');
1550 increaseSplitDelta(1);
1554 SplitLine splitLine = null;
1555 if (options.maxLineLength == 0
1556 || getLength(currentString, depth) < options.maxLineLength
1557 || (splitLine = split(currentString, offsetInGlobalLine)) == null) {
1559 // depending on the type of operator, outputs new line before of after dumping it
1560 // indent before postfix operator
1561 // indent also when the line cannot be split
1562 if (operator == TokenNameextends) {
1563 // || operator == TokenNameimplements
1564 // || operator == TokenNamethrows) {
1565 formattedSource.append(' ');
1566 increaseSplitDelta(1);
1568 if (placeOperatorBehind) {
1573 int max = currentString.length();
1574 if (multipleLineCommentCounter != 0) {
1576 BufferedReader reader = new BufferedReader(new StringReader(currentString));
1577 String line = reader.readLine();
1578 while (line != null) {
1579 updateMappedPositionsWhileSplitting(
1580 beginningOfLineIndex,
1581 beginningOfLineIndex + line.length() + options.lineSeparatorSequence.length);
1582 formattedSource.append(line);
1583 beginningOfLineIndex = beginningOfLineIndex + line.length();
1584 if ((line = reader.readLine()) != null) {
1585 formattedSource.append(options.lineSeparatorSequence);
1586 beginningOfLineIndex += options.lineSeparatorSequence.length;
1587 dumpTab(currentLineIndentationLevel);
1591 } catch (IOException e) {
1592 e.printStackTrace();
1595 updateMappedPositionsWhileSplitting(beginningOfLineIndex, beginningOfLineIndex + max);
1596 for (int i = 0; i < max; i++) {
1597 char currentChar = currentString.charAt(i);
1598 switch (currentChar) {
1603 // fix for 1FFYL5C: LFCOM:ALL - Incorrect indentation when split with a comment inside a condition
1604 // a substring cannot end with a lineSeparatorSequence,
1605 // except if it has been added by format() after a one-line comment
1606 formattedSource.append(options.lineSeparatorSequence);
1608 // 1FGDDV6: LFCOM:WIN98 - Weird splitting on message expression
1613 formattedSource.append(currentChar);
1617 // update positions inside the mappedPositions table
1618 if (substringIndex != -1) {
1619 if (multipleLineCommentCounter == 0) {
1620 int startPosition = beginningOfLineIndex + startSubstringIndexes[substringIndex];
1621 updateMappedPositionsWhileSplitting(startPosition, startPosition + max);
1624 // compute the splitDelta resulting with the operator and blank removal
1625 if (substringIndex + 1 != startSubstringIndexes.length) {
1626 increaseSplitDelta(startSubstringIndexes[substringIndex] + max - startSubstringIndexes[substringIndex + 1]);
1629 // dump postfix operator?
1630 if (placeOperatorBehind) {
1631 if (insertSpaceBefore(operator)) {
1632 formattedSource.append(' ');
1633 if (operator != 0) {
1634 increaseSplitDelta(1);
1637 formattedSource.append(operatorString);
1638 if (operator != 0) {
1639 increaseSplitDelta(operatorString.length());
1644 // fix for 1FG0BA3: LFCOM:WIN98 - Weird splitting on interfaces
1645 // extends has to stand alone on a line when currentString has been split.
1646 if (options.maxLineLength != 0 && splitLine != null && (operator == TokenNameextends)) {
1647 // || operator == TokenNameimplements
1648 // || operator == TokenNamethrows)) {
1649 formattedSource.append(options.lineSeparatorSequence);
1650 increaseSplitDelta(options.lineSeparatorSequence.length);
1653 if (operator == TokenNameextends) {
1654 // || operator == TokenNameimplements
1655 // || operator == TokenNamethrows) {
1656 formattedSource.append(' ');
1657 increaseSplitDelta(1);
1660 // perform actual splitting
1661 String result[] = splitLine.substrings;
1662 int[] splitOperators = splitLine.operators;
1664 if (result[0].length() == 0) {
1665 // when the substring 0 is null, the substring 1 is correctly indented.
1667 emptyFirstSubString = true;
1669 // the operator going in front of the result[0] string is the operator parameter
1670 for (int i = 0, max = result.length; i < max; i++) {
1671 // the new depth is the current one if this is the first substring,
1672 // the current one + 1 otherwise.
1673 // if the substring is a comment, use the current indentation Level instead of the depth
1674 // (-1 because the ouputline increases depth).
1675 // (fix for 1FFC72R: LFCOM:ALL - Incorrect line split in presence of line comments)
1676 String currentResult = result[i];
1678 if (currentResult.length() != 0 || splitOperators[i] != 0) {
1679 int newDepth = (currentResult.startsWith("/*") //$NON-NLS-1$
1680 || currentResult.startsWith("//")) //$NON-NLS-1$
1681 ? indentationLevel - 1 : depth;
1684 i == 0 || (i == 1 && emptyFirstSubString) ? preIndented : false,
1685 i == 0 ? newDepth : newDepth + 1,
1688 splitLine.startSubstringsIndexes,
1689 currentString.indexOf(currentResult));
1691 formattedSource.append(options.lineSeparatorSequence);
1692 increaseSplitDelta(options.lineSeparatorSequence.length);
1696 if (result.length == splitOperators.length - 1) {
1697 int lastOperator = splitOperators[result.length];
1698 String lastOperatorString = operatorString(lastOperator);
1699 formattedSource.append(options.lineSeparatorSequence);
1700 increaseSplitDelta(options.lineSeparatorSequence.length);
1702 if (breakLineBeforeOperator(lastOperator)) {
1704 if (lastOperator != 0) {
1705 if (insertSpaceBefore(lastOperator)) {
1706 formattedSource.append(' ');
1707 increaseSplitDelta(1);
1709 formattedSource.append(lastOperatorString);
1710 increaseSplitDelta(lastOperatorString.length());
1712 if (insertSpaceAfter(lastOperator) // && lastOperator != TokenNameimplements
1713 && lastOperator != TokenNameextends) {
1714 // && lastOperator != TokenNamethrows) {
1715 formattedSource.append(' ');
1716 increaseSplitDelta(1);
1721 if (placeOperatorBehind) {
1722 if (insertSpaceBefore(operator)) {
1723 formattedSource.append(' ');
1724 increaseSplitDelta(1);
1726 formattedSource.append(operatorString);
1727 //increaseSplitDelta(operatorString.length());
1732 * Pops the top statement of the stack if it is <code>token</code>
1734 private int pop(int token) {
1736 if ((constructionsCount > 0) && (constructions[constructionsCount - 1] == token)) {
1738 constructionsCount--;
1744 * Pops the top statement of the stack if it is a <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.
1746 private int popBlock() {
1748 if ((constructionsCount > 0)
1749 && ((constructions[constructionsCount - 1] == BLOCK) || (constructions[constructionsCount - 1] == NONINDENT_BLOCK))) {
1750 if (constructions[constructionsCount - 1] == BLOCK)
1752 constructionsCount--;
1758 * Pops elements until the stack is empty or the top element is <code>token</code>.<br>
1759 * Does not remove <code>token</code> from the stack.
1760 * @param token the token to be left as the top of the stack
1762 private int popExclusiveUntil(int token) {
1764 int startCount = constructionsCount;
1765 for (int i = startCount - 1; i >= 0 && constructions[i] != token; i--) {
1766 if (constructions[i] != NONINDENT_BLOCK)
1768 constructionsCount--;
1774 * Pops elements until the stack is empty or the top element is
1775 * a <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.<br>
1776 * Does not remove it from the stack.
1778 private int popExclusiveUntilBlock() {
1779 int startCount = constructionsCount;
1781 for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK; i--) {
1782 constructionsCount--;
1789 * Pops elements until the stack is empty or the top element is
1790 * a <code>BLOCK</code>, a <code>NONINDENT_BLOCK</code> or a <code>CASE</code>.<br>
1791 * Does not remove it from the stack.
1793 private int popExclusiveUntilBlockOrCase() {
1794 int startCount = constructionsCount;
1796 for (int i = startCount - 1;
1797 i >= 0 && constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK && constructions[i] != TokenNamecase;
1799 constructionsCount--;
1806 * Pops elements until the stack is empty or the top element is <code>token</code>.<br>
1807 * Removes <code>token</code> from the stack too.
1808 * @param token the token to remove from the stack
1810 private int popInclusiveUntil(int token) {
1811 int startCount = constructionsCount;
1813 for (int i = startCount - 1; i >= 0 && constructions[i] != token; i--) {
1814 if (constructions[i] != NONINDENT_BLOCK)
1816 constructionsCount--;
1818 if (constructionsCount > 0) {
1819 if (constructions[constructionsCount - 1] != NONINDENT_BLOCK)
1821 constructionsCount--;
1827 * Pops elements until the stack is empty or the top element is
1828 * a <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.<br>
1829 * Does not remove it from the stack.
1831 private int popInclusiveUntilBlock() {
1832 int startCount = constructionsCount;
1834 for (int i = startCount - 1; i >= 0 && (constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK); i--) {
1836 constructionsCount--;
1838 if (constructionsCount > 0) {
1839 if (constructions[constructionsCount - 1] == BLOCK)
1841 constructionsCount--;
1847 * Pushes a block in the stack.<br>
1848 * Pushes a <code>BLOCK</code> if the stack is empty or if the top element is a <code>BLOCK</code>,
1849 * pushes <code>NONINDENT_BLOCK</code> otherwise.
1850 * Creates a new bigger array if the current one is full.
1852 private int pushBlock() {
1854 if (constructionsCount == constructions.length)
1855 System.arraycopy(constructions, 0, (constructions = new int[constructionsCount * 2]), 0, constructionsCount);
1857 if ((constructionsCount == 0)
1858 || (constructions[constructionsCount - 1] == BLOCK)
1859 || (constructions[constructionsCount - 1] == NONINDENT_BLOCK)
1860 || (constructions[constructionsCount - 1] == TokenNamecase)) {
1862 constructions[constructionsCount++] = BLOCK;
1864 constructions[constructionsCount++] = NONINDENT_BLOCK;
1870 * Pushes <code>token</code>.<br>
1871 * Creates a new bigger array if the current one is full.
1873 private int pushControlStatement(int token) {
1874 if (constructionsCount == constructions.length)
1875 System.arraycopy(constructions, 0, (constructions = new int[constructionsCount * 2]), 0, constructionsCount);
1876 constructions[constructionsCount++] = token;
1880 private static boolean separateFirstArgumentOn(int currentToken) {
1881 //return (currentToken == TokenNameCOMMA || currentToken == TokenNameSEMICOLON);
1882 return currentToken != TokenNameif
1883 && currentToken != TokenNameLPAREN
1884 && currentToken != TokenNameNOT
1885 && currentToken != TokenNamewhile
1886 && currentToken != TokenNamefor
1887 && currentToken != TokenNameswitch;
1891 * Set the positions to map. The mapped positions should be retrieved using the
1892 * getMappedPositions() method.
1893 * @param positions int[]
1894 * @deprecated Set the positions to map using the format(String, int, int[]) method.
1896 * @see #getMappedPositions()
1898 public void setPositionsToMap(int[] positions) {
1899 positionsToMap = positions;
1902 mappedPositions = new int[positions.length];
1906 * Appends a space character to the current line buffer.
1908 private void space() {
1909 currentLineBuffer.append(' ');
1910 increaseLineDelta(1);
1914 * Splits <code>stringToSplit</code> on the top level token<br>
1915 * If there are several identical token at the same level,
1916 * the string is cut into many pieces.
1917 * @return an object containing the operator and all the substrings
1918 * or null if the string cannot be split
1920 public SplitLine split(String stringToSplit) {
1921 return split(stringToSplit, 0);
1925 * Splits <code>stringToSplit</code> on the top level token<br>
1926 * If there are several identical token at the same level,
1927 * the string is cut into many pieces.
1928 * @return an object containing the operator and all the substrings
1929 * or null if the string cannot be split
1931 public SplitLine split(String stringToSplit, int offsetInGlobalLine) {
1933 * See http://dev.eclipse.org/bugs/show_bug.cgi?id=12540 and
1934 * http://dev.eclipse.org/bugs/show_bug.cgi?id=14387
1936 if (stringToSplit.indexOf("//$NON-NLS") != -1) { //$NON-NLS-1$
1940 int currentToken = 0;
1941 int splitTokenType = 0;
1942 int splitTokenDepth = Integer.MAX_VALUE;
1943 int splitTokenPriority = Integer.MAX_VALUE;
1945 int[] substringsStartPositions = new int[10];
1946 // contains the start position of substrings
1947 int[] substringsEndPositions = new int[10];
1948 // contains the start position of substrings
1949 int substringsCount = 1; // index in the substringsStartPosition array
1950 int[] splitOperators = new int[10];
1951 // contains the start position of substrings
1952 int splitOperatorsCount = 0; // index in the substringsStartPosition array
1953 int[] openParenthesisPosition = new int[10];
1954 int openParenthesisPositionCount = 0;
1956 int lastOpenParenthesisPosition = -1;
1957 // used to remember the position of the 1st open parenthesis
1958 // needed for a pattern like: A.B(C); we want formatted like A.B( split C);
1959 // setup the scanner with a new source
1960 int lastCommentStartPosition = -1;
1961 // to remember the start position of the last comment
1962 int firstTokenOnLine = -1;
1963 // to remember the first token of the line
1964 int previousToken = -1;
1965 // to remember the previous token.
1966 splitScanner.setSource(stringToSplit.toCharArray());
1971 // takes the next token
1973 if (currentToken != Scanner.TokenNameWHITESPACE)
1974 previousToken = currentToken;
1975 currentToken = splitScanner.getNextToken();
1976 } catch (InvalidInputException e) {
1977 if (!handleInvalidToken(e))
1980 // this value is not modify when an exception is raised.
1982 if (currentToken == TokenNameEOF)
1985 if (firstTokenOnLine == -1) {
1986 firstTokenOnLine = currentToken;
1988 switch (currentToken) {
1989 case TokenNameRBRACE :
1990 case TokenNameRPAREN :
1991 if (openParenthesisPositionCount > 0) {
1992 if (openParenthesisPositionCount == 1 && lastOpenParenthesisPosition < openParenthesisPosition[0]) {
1993 lastOpenParenthesisPosition = openParenthesisPosition[0];
1995 (splitTokenDepth == Integer.MAX_VALUE)
1996 || (splitTokenDepth > openParenthesisPositionCount && openParenthesisPositionCount == 1)) {
1998 splitTokenDepth = openParenthesisPositionCount;
1999 splitTokenPriority = Integer.MAX_VALUE;
2000 substringsStartPositions[0] = 0;
2001 // better token means the whole line until now is the first substring
2002 substringsCount = 1; // resets the count of substrings
2003 substringsEndPositions[0] = openParenthesisPosition[0];
2004 // substring ends on operator start
2005 position = openParenthesisPosition[0];
2006 // the string mustn't be cut before the closing parenthesis but after the opening one.
2007 splitOperatorsCount = 1; // resets the count of split operators
2008 splitOperators[0] = 0;
2010 openParenthesisPositionCount--;
2013 case TokenNameLBRACE :
2014 case TokenNameLPAREN :
2015 if (openParenthesisPositionCount == openParenthesisPosition.length) {
2017 openParenthesisPosition,
2019 (openParenthesisPosition = new int[openParenthesisPositionCount * 2]),
2021 openParenthesisPositionCount);
2023 openParenthesisPosition[openParenthesisPositionCount++] = splitScanner.currentPosition;
2024 if (currentToken == TokenNameLPAREN && previousToken == TokenNameRPAREN) {
2025 openParenthesisPosition[openParenthesisPositionCount - 1] = splitScanner.startPosition;
2028 case TokenNameSEMICOLON : // ;
2029 case TokenNameCOMMA : // ,
2030 case TokenNameEQUAL : // =
2031 if (openParenthesisPositionCount < splitTokenDepth
2032 || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) {
2033 // the current token is better than the one we currently have
2034 // (in level or in priority if same level)
2035 // reset the substringsCount
2036 splitTokenDepth = openParenthesisPositionCount;
2037 splitTokenType = currentToken;
2038 splitTokenPriority = getTokenPriority(currentToken);
2039 substringsStartPositions[0] = 0;
2040 // better token means the whole line until now is the first substring
2042 if (separateFirstArgumentOn(firstTokenOnLine) && openParenthesisPositionCount > 0) {
2043 substringsCount = 2; // resets the count of substrings
2045 substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1];
2046 substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1];
2047 substringsEndPositions[1] = splitScanner.startPosition;
2048 splitOperatorsCount = 2; // resets the count of split operators
2049 splitOperators[0] = 0;
2050 splitOperators[1] = currentToken;
2051 position = splitScanner.currentPosition;
2052 // next substring will start from operator end
2054 substringsCount = 1; // resets the count of substrings
2056 substringsEndPositions[0] = splitScanner.startPosition;
2057 // substring ends on operator start
2058 position = splitScanner.currentPosition;
2059 // next substring will start from operator end
2060 splitOperatorsCount = 1; // resets the count of split operators
2061 splitOperators[0] = currentToken;
2064 if ((openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken))
2065 && splitTokenType != TokenNameEQUAL
2066 && currentToken != TokenNameEQUAL) {
2067 // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after split
2068 // take only the 1st = into account.
2069 // if another token with the same priority is found,
2070 // push the start position of the substring and
2071 // push the token into the stack.
2072 // create a new array object if the current one is full.
2073 if (substringsCount == substringsStartPositions.length) {
2075 substringsStartPositions,
2077 (substringsStartPositions = new int[substringsCount * 2]),
2081 substringsEndPositions,
2083 (substringsEndPositions = new int[substringsCount * 2]),
2087 if (splitOperatorsCount == splitOperators.length) {
2088 System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount * 2]), 0, splitOperatorsCount);
2090 substringsStartPositions[substringsCount] = position;
2091 substringsEndPositions[substringsCount++] = splitScanner.startPosition;
2092 // substring ends on operator start
2093 position = splitScanner.currentPosition;
2094 // next substring will start from operator end
2095 splitOperators[splitOperatorsCount++] = currentToken;
2100 case TokenNameCOLON : // : (15.24)
2101 // see 1FK7C5R, we only split on a colon, when it is associated with a question-mark.
2102 // indeed it might appear also behind a case statement, and we do not to break at this point.
2103 if ((splitOperatorsCount == 0) || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) {
2106 case TokenNameextends :
2107 // case TokenNameimplements :
2108 // case TokenNamethrows :
2110 case TokenNameDOT : // .
2111 case TokenNameMULTIPLY : // * (15.16.1)
2112 case TokenNameDIVIDE : // / (15.16.2)
2113 case TokenNameREMAINDER : // % (15.16.3)
2114 case TokenNamePLUS : // + (15.17, 15.17.2)
2115 case TokenNameMINUS : // - (15.17.2)
2116 case TokenNameLEFT_SHIFT : // << (15.18)
2117 case TokenNameRIGHT_SHIFT : // >> (15.18)
2118 // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18)
2119 case TokenNameLESS : // < (15.19.1)
2120 case TokenNameLESS_EQUAL : // <= (15.19.1)
2121 case TokenNameGREATER : // > (15.19.1)
2122 case TokenNameGREATER_EQUAL : // >= (15.19.1)
2123 // case TokenNameinstanceof : // instanceof
2124 case TokenNameEQUAL_EQUAL : // == (15.20, 15.20.1, 15.20.2, 15.20.3)
2125 case TokenNameNOT_EQUAL : // != (15.20, 15.20.1, 15.20.2, 15.20.3)
2126 case TokenNameAND : // & (15.21, 15.21.1, 15.21.2)
2127 case TokenNameOR : // | (15.21, 15.21.1, 15.21.2)
2128 case TokenNameXOR : // ^ (15.21, 15.21.1, 15.21.2)
2129 case TokenNameAND_AND : // && (15.22)
2130 case TokenNameOR_OR : // || (15.23)
2131 case TokenNameQUESTION : // ? (15.24)
2132 case TokenNameMULTIPLY_EQUAL : // *= (15.25.2)
2133 case TokenNameDIVIDE_EQUAL : // /= (15.25.2)
2134 case TokenNameREMAINDER_EQUAL : // %= (15.25.2)
2135 case TokenNamePLUS_EQUAL : // += (15.25.2)
2136 case TokenNameMINUS_EQUAL : // -= (15.25.2)
2137 case TokenNameLEFT_SHIFT_EQUAL : // <<= (15.25.2)
2138 case TokenNameRIGHT_SHIFT_EQUAL : // >>= (15.25.2)
2139 // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2)
2140 case TokenNameAND_EQUAL : // &= (15.25.2)
2141 case TokenNameXOR_EQUAL : // ^= (15.25.2)
2142 case TokenNameOR_EQUAL : // |= (15.25.2)
2144 if ((openParenthesisPositionCount < splitTokenDepth
2145 || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken)))
2146 && !((currentToken == TokenNamePLUS || currentToken == TokenNameMINUS)
2147 && (previousToken == TokenNameLBRACE || previousToken == TokenNameLBRACKET || splitScanner.startPosition == 0))) {
2148 // the current token is better than the one we currently have
2149 // (in level or in priority if same level)
2150 // reset the substringsCount
2151 splitTokenDepth = openParenthesisPositionCount;
2152 splitTokenType = currentToken;
2153 splitTokenPriority = getTokenPriority(currentToken);
2154 substringsStartPositions[0] = 0;
2155 // better token means the whole line until now is the first substring
2157 if (separateFirstArgumentOn(firstTokenOnLine) && openParenthesisPositionCount > 0) {
2158 substringsCount = 2; // resets the count of substrings
2160 substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1];
2161 substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1];
2162 substringsEndPositions[1] = splitScanner.startPosition;
2163 splitOperatorsCount = 3; // resets the count of split operators
2164 splitOperators[0] = 0;
2165 splitOperators[1] = 0;
2166 splitOperators[2] = currentToken;
2167 position = splitScanner.currentPosition;
2168 // next substring will start from operator end
2170 substringsCount = 1; // resets the count of substrings
2172 substringsEndPositions[0] = splitScanner.startPosition;
2173 // substring ends on operator start
2174 position = splitScanner.currentPosition;
2175 // next substring will start from operator end
2176 splitOperatorsCount = 2; // resets the count of split operators
2177 splitOperators[0] = 0;
2178 // nothing for first operand since operator will be inserted in front of the second operand
2179 splitOperators[1] = currentToken;
2183 if (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken)) {
2184 // if another token with the same priority is found,
2185 // push the start position of the substring and
2186 // push the token into the stack.
2187 // create a new array object if the current one is full.
2188 if (substringsCount == substringsStartPositions.length) {
2190 substringsStartPositions,
2192 (substringsStartPositions = new int[substringsCount * 2]),
2196 substringsEndPositions,
2198 (substringsEndPositions = new int[substringsCount * 2]),
2202 if (splitOperatorsCount == splitOperators.length) {
2203 System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount * 2]), 0, splitOperatorsCount);
2205 substringsStartPositions[substringsCount] = position;
2206 substringsEndPositions[substringsCount++] = splitScanner.startPosition;
2207 // substring ends on operator start
2208 position = splitScanner.currentPosition;
2209 // next substring will start from operator end
2210 splitOperators[splitOperatorsCount++] = currentToken;
2216 if (isComment(currentToken)) {
2217 lastCommentStartPosition = splitScanner.startPosition;
2219 lastCommentStartPosition = -1;
2222 } catch (InvalidInputException e) {
2225 // if the string cannot be split, return null.
2226 if (splitOperatorsCount == 0)
2229 // ## SPECIAL CASES BEGIN
2230 if (((splitOperatorsCount == 2
2231 && splitOperators[1] == TokenNameDOT
2232 && splitTokenDepth == 0
2233 && lastOpenParenthesisPosition > -1)
2234 || (splitOperatorsCount > 2
2235 && splitOperators[1] == TokenNameDOT
2236 && splitTokenDepth == 0
2237 && lastOpenParenthesisPosition > -1
2238 && lastOpenParenthesisPosition <= options.maxLineLength)
2239 || (separateFirstArgumentOn(firstTokenOnLine) && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1))
2240 && (lastOpenParenthesisPosition < splitScanner.source.length && splitScanner.source[lastOpenParenthesisPosition] != ')')) {
2241 // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis should not be broken on two lines
2242 // only one split on a top level .
2243 // or more than one split on . and substring before open parenthesis fits one line.
2244 // or split inside parenthesis and first token is not a for/while/if
2245 SplitLine sl = split(stringToSplit.substring(lastOpenParenthesisPosition), lastOpenParenthesisPosition);
2246 if (sl == null || sl.operators[0] != TokenNameCOMMA) {
2247 // trim() is used to remove the extra blanks at the end of the substring. See PR 1FGYPI1
2248 return new SplitLine(
2251 stringToSplit.substring(0, lastOpenParenthesisPosition).trim(),
2252 stringToSplit.substring(lastOpenParenthesisPosition)},
2253 new int[] { offsetInGlobalLine, lastOpenParenthesisPosition + offsetInGlobalLine });
2255 // right substring can be split and is split on comma
2256 // copy substrings and operators
2257 // except if the 1st string is empty.
2258 int startIndex = (sl.substrings[0].length() == 0) ? 1 : 0;
2259 int subStringsLength = sl.substrings.length + 1 - startIndex;
2260 String[] result = new String[subStringsLength];
2261 int[] startIndexes = new int[subStringsLength];
2262 int operatorsLength = sl.operators.length + 1 - startIndex;
2263 int[] operators = new int[operatorsLength];
2265 result[0] = stringToSplit.substring(0, lastOpenParenthesisPosition);
2268 System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes, 1, subStringsLength - 1);
2269 for (int i = subStringsLength - 1; i >= 0; i--) {
2270 startIndexes[i] += offsetInGlobalLine;
2272 System.arraycopy(sl.substrings, startIndex, result, 1, subStringsLength - 1);
2273 System.arraycopy(sl.operators, startIndex, operators, 1, operatorsLength - 1);
2275 return new SplitLine(operators, result, startIndexes);
2278 // if the last token is a comment and the substring before the comment fits on a line,
2279 // split before the comment and return the result.
2280 if (lastCommentStartPosition > -1 && lastCommentStartPosition < options.maxLineLength && splitTokenPriority > 50) {
2281 int end = lastCommentStartPosition;
2282 int start = lastCommentStartPosition;
2283 if (stringToSplit.charAt(end - 1) == ' ') {
2286 if (start != end && stringToSplit.charAt(start) == ' ') {
2289 return new SplitLine(
2291 new String[] { stringToSplit.substring(0, end), stringToSplit.substring(start)},
2292 new int[] { 0, start });
2294 if (position != stringToSplit.length()) {
2295 if (substringsCount == substringsStartPositions.length) {
2297 substringsStartPositions,
2299 (substringsStartPositions = new int[substringsCount * 2]),
2302 System.arraycopy(substringsEndPositions, 0, (substringsEndPositions = new int[substringsCount * 2]), 0, substringsCount);
2304 // avoid empty extra substring, e.g. line terminated with a semi-colon
2305 substringsStartPositions[substringsCount] = position;
2306 substringsEndPositions[substringsCount++] = stringToSplit.length();
2308 if (splitOperatorsCount == splitOperators.length) {
2309 System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount * 2]), 0, splitOperatorsCount);
2311 splitOperators[splitOperatorsCount] = 0;
2313 // the last element of the stack is the position of the end of StringToSPlit
2314 // +1 because the substring method excludes the last character
2315 String[] result = new String[substringsCount];
2316 for (int i = 0; i < substringsCount; i++) {
2317 int start = substringsStartPositions[i];
2318 int end = substringsEndPositions[i];
2319 if (stringToSplit.charAt(start) == ' ') {
2321 substringsStartPositions[i]++;
2323 if (end != start && stringToSplit.charAt(end - 1) == ' ') {
2326 result[i] = stringToSplit.substring(start, end);
2327 substringsStartPositions[i] += offsetInGlobalLine;
2329 if (splitOperatorsCount > substringsCount) {
2330 System.arraycopy(substringsStartPositions, 0, (substringsStartPositions = new int[splitOperatorsCount]), 0, substringsCount);
2331 System.arraycopy(substringsEndPositions, 0, (substringsEndPositions = new int[splitOperatorsCount]), 0, substringsCount);
2332 for (int i = substringsCount; i < splitOperatorsCount; i++) {
2333 substringsStartPositions[i] = position;
2334 substringsEndPositions[i] = position;
2336 System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount]), 0, splitOperatorsCount);
2338 System.arraycopy(substringsStartPositions, 0, (substringsStartPositions = new int[substringsCount]), 0, substringsCount);
2339 System.arraycopy(substringsEndPositions, 0, (substringsEndPositions = new int[substringsCount]), 0, substringsCount);
2340 System.arraycopy(splitOperators, 0, (splitOperators = new int[substringsCount]), 0, substringsCount);
2342 SplitLine splitLine = new SplitLine(splitOperators, result, substringsStartPositions);
2346 private void updateMappedPositions(int startPosition) {
2347 if (positionsToMap == null) {
2350 char[] source = scanner.source;
2351 int sourceLength = source.length;
2352 while (indexToMap < positionsToMap.length && positionsToMap[indexToMap] <= startPosition) {
2353 int posToMap = positionsToMap[indexToMap];
2354 if (posToMap < 0 || posToMap >= sourceLength) {
2355 // protection against out of bounds position
2356 if (posToMap == sourceLength) {
2357 mappedPositions[indexToMap] = formattedSource.length();
2359 indexToMap = positionsToMap.length; // no more mapping
2362 if (CharOperation.isWhitespace(source[posToMap])) {
2363 mappedPositions[indexToMap] = startPosition + globalDelta + lineDelta;
2365 if (posToMap == sourceLength - 1) {
2366 mappedPositions[indexToMap] = startPosition + globalDelta + lineDelta;
2368 mappedPositions[indexToMap] = posToMap + globalDelta + lineDelta;
2375 private void updateMappedPositionsWhileSplitting(int startPosition, int endPosition) {
2376 if (mappedPositions == null || mappedPositions.length == indexInMap)
2379 while (indexInMap < mappedPositions.length
2380 && startPosition <= mappedPositions[indexInMap]
2381 && mappedPositions[indexInMap] < endPosition
2382 && indexInMap < indexToMap) {
2383 mappedPositions[indexInMap] += splitDelta;
2388 private int getLength(String s, int tabDepth) {
2390 for (int i = 0; i < tabDepth; i++) {
2391 length += options.tabSize;
2393 for (int i = 0, max = s.length(); i < max; i++) {
2394 char currentChar = s.charAt(i);
2395 switch (currentChar) {
2397 length += options.tabSize;
2407 * Sets the initial indentation level
2408 * @param indentationLevel new indentation level
2412 public void setInitialIndentationLevel(int newIndentationLevel) {
2413 this.initialIndentationLevel = currentLineIndentationLevel = indentationLevel = newIndentationLevel;