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;
26 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
27 import net.sourceforge.phpdt.internal.corext.util.Strings;
28 import net.sourceforge.phpdt.internal.formatter.impl.FormatterOptions;
29 import net.sourceforge.phpdt.internal.formatter.impl.SplitLine;
30 import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.formatter.IContentFormatterExtension;
34 import org.eclipse.jface.text.formatter.IFormattingContext;
37 * <h2>How to format a piece of code ?</h2>
39 * <li>Create an instance of <code>CodeFormatter</code>
40 * <li>Use the method <code>void format(aString)</code> on this instance to format <code>aString</code>. It will return the
44 public class CodeFormatter implements ITerminalSymbols, ICodeFormatter {
45 // IContentFormatterExtension {
46 public FormatterOptions options;
49 * Represents a block in the <code>constructions</code> stack.
51 public static final int BLOCK = ITerminalSymbols.TokenNameLBRACE;
54 * Represents a block following a control statement in the <code>constructions</code> stack.
56 public static final int NONINDENT_BLOCK = -100;
59 * Contains the formatted output.
61 StringBuffer formattedSource;
64 * Contains the current line. <br>
65 * Will be dumped at the next "newline"
67 StringBuffer currentLineBuffer;
70 * Used during the formatting to get each token.
75 * Contains the tokens responsible for the current indentation level and the blocks not closed yet.
77 private int[] constructions;
80 * Index in the <code>constructions</code> array.
82 private int constructionsCount;
85 * Level of indentation of the current token (number of tab char put in front of it).
87 private int indentationLevel;
90 * Regular level of indentation of all the lines
92 private int initialIndentationLevel;
95 * Used to split a line.
100 * To remember the offset between the beginning of the line and the beginning of the comment.
102 int currentCommentOffset;
104 int currentLineIndentationLevel;
106 int maxLineSize = 30;
108 private boolean containsOpenCloseBraces;
110 private int indentationLevelForOpenCloseBraces;
113 * Collections of positions to map
115 private int[] positionsToMap;
118 * Collections of mapped positions
120 private int[] mappedPositions;
122 private int indexToMap;
124 private int indexInMap;
126 private int globalDelta;
128 private int lineDelta;
130 private int splitDelta;
132 private int beginningOfLineIndex;
134 private int multipleLineCommentCounter;
137 * Creates a new instance of Code Formatter using the given settings.
139 * @deprecated backport 1.0 internal functionality
141 public CodeFormatter(ConfigurableOption[] settings) {
142 this(convertConfigurableOptions(settings));
146 * Creates a new instance of Code Formatter using the FormattingOptions object given as argument
148 * @deprecated Use CodeFormatter(ConfigurableOption[]) instead
150 public CodeFormatter() {
155 * Creates a new instance of Code Formatter using the given settings.
157 public CodeFormatter(Map settings) {
158 // initialize internal state
159 constructionsCount = 0;
160 constructions = new int[10];
161 currentLineIndentationLevel = indentationLevel = initialIndentationLevel;
162 currentCommentOffset = -1;
163 // initialize primary and secondary scanners
164 scanner = new Scanner(true /* comment */
165 , true /* whitespace */
168 , true, /* tokenizeStrings */
169 null, null); // regular scanner for forming lines
170 scanner.recordLineSeparator = true;
171 // to remind of the position of the beginning of the line.
172 splitScanner = new Scanner(true /* comment */
173 , true /* whitespace */
176 , true, /* tokenizeStrings */
178 // secondary scanner to split long lines formed by primary scanning
179 // initialize current line buffer
180 currentLineBuffer = new StringBuffer();
181 this.options = new FormatterOptions(settings);
185 * Returns true if a lineSeparator has to be inserted before <code>operator</code> false otherwise.
187 private static boolean breakLineBeforeOperator(int operator) {
190 case TokenNameSEMICOLON:
199 * @deprecated backport 1.0 internal functionality
201 private static Map convertConfigurableOptions(ConfigurableOption[] settings) {
202 Hashtable options = new Hashtable(10);
203 for (int i = 0; i < settings.length; i++) {
204 if (settings[i].getComponentName().equals(CodeFormatter.class.getName())) {
205 String optionName = settings[i].getOptionName();
206 int valueIndex = settings[i].getCurrentValueIndex();
207 if (optionName.equals("newline.openingBrace")) { //$NON-NLS-1$
208 options.put("net.sourceforge.phpdt.core.formatter.newline.openingBrace", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
209 } else if (optionName.equals("newline.controlStatement")) { //$NON-NLS-1$
211 .put("net.sourceforge.phpdt.core.formatter.newline.controlStatement", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
212 } else if (optionName.equals("newline.clearAll")) { //$NON-NLS-1$
213 options.put("net.sourceforge.phpdt.core.formatter.newline.clearAll", valueIndex == 0 ? "clear all" : "preserve one"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
214 } else if (optionName.equals("newline.elseIf")) { //$NON-NLS-1$
215 options.put("net.sourceforge.phpdt.core.formatter.newline.elseIf", valueIndex == 0 ? "do not insert" : "insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
216 } else if (optionName.equals("newline.emptyBlock")) { //$NON-NLS-1$
217 options.put("net.sourceforge.phpdt.core.formatter.newline.emptyBlock", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
218 } else if (optionName.equals("lineSplit")) { //$NON-NLS-1$
219 options.put("net.sourceforge.phpdt.core.formatter.lineSplit", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$
220 } else if (optionName.equals("style.assignment")) { //$NON-NLS-1$
221 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$
224 } else if (optionName.equals("tabulation.size")) { //$NON-NLS-1$
225 options.put("net.sourceforge.phpdt.core.formatter.tabulation.size", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$
233 * Returns the end of the source code.
235 private final String copyRemainingSource() {
236 char str[] = scanner.source;
237 int startPosition = scanner.startPosition;
238 int length = str.length - startPosition;
239 StringBuffer bufr = new StringBuffer(length);
240 if (startPosition < str.length) {
241 bufr.append(str, startPosition, length);
243 return (bufr.toString());
247 * Inserts <code>tabCount</code> tab character or their equivalent number of spaces.
249 private void dumpTab(int tabCount) {
250 if (options.indentWithTab) {
251 for (int j = 0; j < tabCount; j++) {
252 formattedSource.append('\t');
253 increaseSplitDelta(1);
256 for (int i = 0, max = options.tabSize * tabCount; i < max; i++) {
257 formattedSource.append(' ');
258 increaseSplitDelta(1);
264 * Dumps <code>currentLineBuffer</code> into the formatted string.
266 private void flushBuffer() {
267 String currentString = currentLineBuffer.toString();
269 beginningOfLineIndex = formattedSource.length();
270 if (containsOpenCloseBraces) {
271 containsOpenCloseBraces = false;
272 outputLine(currentString, false, indentationLevelForOpenCloseBraces, 0, -1, null, 0);
273 indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
275 outputLine(currentString, false, currentLineIndentationLevel, 0, -1, null, 0);
277 int scannerSourceLength = scanner.source.length;
278 if (scannerSourceLength > 2) {
279 if (scanner.source[scannerSourceLength - 1] == '\n' && scanner.source[scannerSourceLength - 2] == '\r') {
280 formattedSource.append(options.lineSeparatorSequence);
281 increaseGlobalDelta(options.lineSeparatorSequence.length - 2);
282 } else if (scanner.source[scannerSourceLength - 1] == '\n') {
283 formattedSource.append(options.lineSeparatorSequence);
284 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
285 } else if (scanner.source[scannerSourceLength - 1] == '\r') {
286 formattedSource.append(options.lineSeparatorSequence);
287 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
290 updateMappedPositions(scanner.startPosition);
294 * Formats the input string.
296 private void format() {
298 int previousToken = 0;
299 int previousCompilableToken = 0;
300 int indentationOffset = 0;
301 int newLinesInWhitespace = 0;
302 // number of new lines in the previous whitespace token
303 // (used to leave blank lines before comments)
304 int pendingNewLines = 0;
305 boolean expectingOpenBrace = false;
306 boolean clearNonBlockIndents = false;
307 // true if all indentations till the 1st { (usefull after } or ;)
308 boolean pendingSpace = true;
309 boolean pendingNewlineAfterParen = false;
310 // true when a cr is to be put after a ) (in conditional statements)
311 boolean inAssignment = false;
312 boolean inArrayAssignment = false;
313 boolean inThrowsClause = false;
314 boolean inClassOrInterfaceHeader = false;
315 int dollarBraceCount = 0;
316 // openBracketCount is used to count the number of open brackets not closed
318 int openBracketCount = 0;
319 int unarySignModifier = 0;
320 // openParenthesis[0] is used to count the parenthesis not belonging to a
322 // (eg foo();). parenthesis in for (...) are count elsewhere in the array.
323 int openParenthesisCount = 1;
324 int[] openParenthesis = new int[10];
325 // tokenBeforeColon is used to know what token goes along with the current
327 // it can be case or ?
328 int tokenBeforeColonCount = 0;
329 int[] tokenBeforeColon = new int[10];
330 constructionsCount = 0; // initializes the constructions count.
331 // contains DO if in a DO..WHILE statement, UNITIALIZED otherwise.
333 // fix for 1FF17XY: LFCOM:ALL - Format problem on not matching } and else
334 boolean specialElse = false;
335 // OPTION (IndentationLevel): initial indentation level may be non-zero.
336 currentLineIndentationLevel += constructionsCount;
337 // An InvalidInputException exception might cause the termination of this
341 // Get the next token. Catch invalid input and output it
342 // with minimal formatting, also catch end of input and
345 token = scanner.getNextToken();
347 int currentEndPosition = scanner.getCurrentTokenEndPosition();
348 int currentStartPosition = scanner.getCurrentTokenStartPosition();
349 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
350 System.out.println(scanner.toStringAction(token));
352 // Patch for line comment
353 // See PR http://dev.eclipse.org/bugs/show_bug.cgi?id=23096
354 if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
355 int length = scanner.currentPosition;
356 loop: for (int index = length - 1; index >= 0; index--) {
357 switch (scanner.source[index]) {
360 scanner.currentPosition--;
367 } catch (InvalidInputException e) {
368 if (!handleInvalidToken(e)) {
373 if (token == Scanner.TokenNameEOF)
375 if (token == Scanner.TokenNameHEREDOC || token == Scanner.TokenNameINLINE_HTML) {
376 // no indentation for heredocs and HTML !
377 outputCurrentTokenWithoutIndent(Scanner.TokenNameHEREDOC);
381 * ## MODIFYING the indentation level before generating new lines and indentation in the output string
383 // Removes all the indentations made by statements not followed by a
385 // except if the current token is ELSE, CATCH or if we are in a
387 if (clearNonBlockIndents && (token != Scanner.TokenNameWHITESPACE)) {
390 if (constructionsCount > 0 && constructions[constructionsCount - 1] == TokenNameelse) {
394 indentationLevel += popInclusiveUntil(TokenNameif);
396 // case TokenNamecatch :
397 // indentationLevel += popInclusiveUntil(TokenNamecatch);
399 // case TokenNamefinally :
400 // indentationLevel += popInclusiveUntil(TokenNamecatch);
403 if (nlicsToken == TokenNamedo) {
404 indentationLevel += pop(TokenNamedo);
408 indentationLevel += popExclusiveUntilBlockOrCase();
409 // clear until a CASE, DEFAULT or BLOCK is encountered.
410 // Thus, the indentationLevel is correctly cleared either
411 // in a switch/case statement or in any other situation.
413 clearNonBlockIndents = false;
415 // returns to the indentation level created by the SWITCH keyword
416 // if the current token is a CASE or a DEFAULT
417 if (token == TokenNamecase || token == TokenNamedefault) {
418 indentationLevel += pop(TokenNamecase);
420 // if (token == Scanner.TokenNamethrows) {
421 // inThrowsClause = true;
423 if ((token == Scanner.TokenNameclass || token == Scanner.TokenNameinterface) && previousToken != Scanner.TokenNameDOT) {
424 inClassOrInterfaceHeader = true;
427 * ## APPEND newlines and indentations to the output string
429 // Do not add a new line between ELSE and IF, if the option
430 // elseIfOnSameLine is true.
431 // Fix for 1ETLWPZ: IVJCOM:ALL - incorrect "else if" formatting
432 // if (pendingNewlineAfterParen
433 // && previousCompilableToken == TokenNameelse
434 // && token == TokenNameif
435 // && options.compactElseIfMode) {
436 // pendingNewlineAfterParen = false;
437 // pendingNewLines = 0;
438 // indentationLevel += pop(TokenNameelse);
439 // // because else if is now one single statement,
440 // // the indentation level after it is increased by one and not by 2
441 // // (else = 1 indent, if = 1 indent, but else if = 1 indent, not 2).
443 // Add a newline & indent to the formatted source string if
444 // a for/if-else/while statement was scanned and there is no block
446 pendingNewlineAfterParen = pendingNewlineAfterParen
447 || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE);
448 if (pendingNewlineAfterParen && token != Scanner.TokenNameWHITESPACE) {
449 pendingNewlineAfterParen = false;
450 // Do to add a newline & indent sequence if the current token is an
451 // open brace or a period or if the current token is a semi-colon and
453 // previous token is a close paren.
454 // add a new line if a parenthesis belonging to a for() statement
455 // has been closed and the current token is not an opening brace
456 if (token != TokenNameLBRACE && !isComment(token)
457 // to avoid adding new line between else and a comment
458 && token != TokenNameDOT && !(previousCompilableToken == TokenNameRPAREN && token == TokenNameSEMICOLON)) {
460 currentLineIndentationLevel = indentationLevel;
462 pendingSpace = false;
464 if (token == TokenNameLBRACE && options.newLineBeforeOpeningBraceMode) {
466 if (constructionsCount > 0 && constructions[constructionsCount - 1] != BLOCK
467 && constructions[constructionsCount - 1] != NONINDENT_BLOCK) {
468 currentLineIndentationLevel = indentationLevel - 1;
470 currentLineIndentationLevel = indentationLevel;
473 pendingSpace = false;
477 if (token == TokenNameLBRACE && options.newLineBeforeOpeningBraceMode && constructionsCount > 0
478 && constructions[constructionsCount - 1] == TokenNamedo) {
480 currentLineIndentationLevel = indentationLevel - 1;
482 pendingSpace = false;
485 if (token == TokenNameLBRACE && inThrowsClause) {
486 inThrowsClause = false;
487 if (options.newLineBeforeOpeningBraceMode) {
489 currentLineIndentationLevel = indentationLevel;
491 pendingSpace = false;
495 if (token == TokenNameLBRACE && inClassOrInterfaceHeader) {
496 inClassOrInterfaceHeader = false;
497 if (options.newLineBeforeOpeningBraceMode) {
499 currentLineIndentationLevel = indentationLevel;
501 pendingSpace = false;
504 // Add pending new lines to the formatted source string.
505 // Note: pending new lines are not added if the current token
506 // is a single line comment or whitespace.
507 // if the comment is between parenthesis, there is no blank line
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))) || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE))
512 && token != Scanner.TokenNameWHITESPACE) {
513 // Do not add newline & indent between an adjoining close brace and
514 // close paren. Anonymous inner classes may use this form.
515 boolean closeBraceAndCloseParen = previousToken == TokenNameRBRACE && token == TokenNameRPAREN;
516 // OPTION (NewLineInCompoundStatement): do not add newline & indent
517 // between close brace and else, (do) while, catch, and finally if
518 // newlineInCompoundStatement is true.
519 boolean nlicsOption = previousToken == TokenNameRBRACE
520 && !options.newlineInControlStatementMode
521 && (token == TokenNameelse || (token == TokenNamewhile && nlicsToken == TokenNamedo) || token == TokenNamecatch || token == TokenNamefinally);
522 // Do not add a newline & indent between a close brace and
524 boolean semiColonAndCloseBrace = previousToken == TokenNameRBRACE && token == TokenNameSEMICOLON;
525 // Do not add a new line & indent between a multiline comment and a
527 boolean commentAndOpenBrace = previousToken == Scanner.TokenNameCOMMENT_BLOCK && token == TokenNameLBRACE;
528 // Do not add a newline & indent between a close brace and a colon
529 // (in array assignments, for example).
530 boolean commaAndCloseBrace = previousToken == TokenNameRBRACE && token == TokenNameCOMMA;
531 // Add a newline and indent, if appropriate.
533 || (!commentAndOpenBrace && !closeBraceAndCloseParen && !nlicsOption && !semiColonAndCloseBrace && !commaAndCloseBrace)) {
534 // if clearAllBlankLinesMode=false, leaves the blank lines
535 // inserted by the user
536 // if clearAllBlankLinesMode=true, removes all of then
537 // and insert only blank lines required by the formatting.
538 if (!options.clearAllBlankLinesMode) {
539 // (isComment(token))
540 pendingNewLines = (pendingNewLines < newLinesInWhitespace) ? newLinesInWhitespace : pendingNewLines;
541 pendingNewLines = (pendingNewLines > 2) ? 2 : pendingNewLines;
543 if (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE) {
544 containsOpenCloseBraces = true;
545 indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
546 if (isComment(previousToken)) {
547 newLine(pendingNewLines);
550 * if (!(constructionsCount > 1 && constructions[constructionsCount-1] == NONINDENT_BLOCK &&
551 * (constructions[constructionsCount-2] == TokenNamefor
553 if (options.newLineInEmptyBlockMode) {
554 if (inArrayAssignment) {
555 newLine(1); // array assigment with an empty block
557 newLine(pendingNewLines);
563 // see PR 1FKKC3U: LFCOM:WINNT - Format problem with a comment
565 if (!((previousToken == Scanner.TokenNameCOMMENT_BLOCK || previousToken == Scanner.TokenNameCOMMENT_PHPDOC) && token == TokenNameSEMICOLON)) {
566 newLine(pendingNewLines);
569 if (((previousCompilableToken == TokenNameSEMICOLON) || (previousCompilableToken == TokenNameLBRACE)
570 || (previousCompilableToken == TokenNameRBRACE) || (isComment(previousToken)))
571 && (token == TokenNameRBRACE)) {
572 indentationOffset = -1;
573 indentationLevel += popExclusiveUntilBlock();
575 if (previousToken == Scanner.TokenNameCOMMENT_LINE && inAssignment) {
577 currentLineIndentationLevel++;
579 currentLineIndentationLevel = indentationLevel + indentationOffset;
581 pendingSpace = false;
582 indentationOffset = 0;
585 newLinesInWhitespace = 0;
587 if (nlicsToken == TokenNamedo && token == TokenNamewhile) {
591 boolean phpTagAndWhitespace = previousToken == TokenNameINLINE_HTML && token == TokenNameWHITESPACE;
593 // case TokenNameDOLLAR :
594 // dollarBraceCount++;
597 // case TokenNamefinally :
598 expectingOpenBrace = true;
599 pendingNewlineAfterParen = true;
600 indentationLevel += pushControlStatement(token);
603 case TokenNamedefault:
604 if (tokenBeforeColonCount == tokenBeforeColon.length) {
606 .arraycopy(tokenBeforeColon, 0, (tokenBeforeColon = new int[tokenBeforeColonCount * 2]), 0, tokenBeforeColonCount);
608 tokenBeforeColon[tokenBeforeColonCount++] = TokenNamecase;
609 indentationLevel += pushControlStatement(TokenNamecase);
611 case TokenNameQUESTION:
612 if (tokenBeforeColonCount == tokenBeforeColon.length) {
614 .arraycopy(tokenBeforeColon, 0, (tokenBeforeColon = new int[tokenBeforeColonCount * 2]), 0, tokenBeforeColonCount);
616 tokenBeforeColon[tokenBeforeColonCount++] = token;
618 case TokenNameswitch:
620 case TokenNameforeach:
623 if (openParenthesisCount == openParenthesis.length) {
624 System.arraycopy(openParenthesis, 0, (openParenthesis = new int[openParenthesisCount * 2]), 0, openParenthesisCount);
626 openParenthesis[openParenthesisCount++] = 0;
627 expectingOpenBrace = true;
628 indentationLevel += pushControlStatement(token);
631 pendingNewlineAfterParen = true;
633 // several CATCH statements can be contiguous.
634 // a CATCH is encountered pop until first CATCH (if a CATCH
635 // follows a TRY it works the same way,
636 // as CATCH and TRY are the same token in the stack).
637 expectingOpenBrace = true;
638 indentationLevel += pushControlStatement(TokenNamecatch);
641 expectingOpenBrace = true;
642 indentationLevel += pushControlStatement(token);
647 case TokenNameLPAREN:
648 // if (previousToken == TokenNamesynchronized) {
649 // indentationLevel += pushControlStatement(previousToken);
651 // Put a space between the previous and current token if the
652 // previous token was not a keyword, open paren, logical
653 // compliment (eg: !), semi-colon, open brace, close brace,
655 if (previousCompilableToken != TokenNameLBRACKET && previousToken != TokenNameIdentifier && previousToken != 0
656 && previousToken != TokenNameNOT && previousToken != TokenNameLPAREN && previousToken != TokenNameTWIDDLE
657 && previousToken != TokenNameSEMICOLON && previousToken != TokenNameLBRACE && previousToken != TokenNameRBRACE
658 && previousToken != TokenNamesuper) {
659 // && previousToken != TokenNamethis) {
662 // If in a for/if/while statement, increase the parenthesis count
663 // for the current openParenthesisCount
664 // else increase the count for stand alone parenthesis.
665 if (openParenthesisCount > 0)
666 openParenthesis[openParenthesisCount - 1]++;
668 openParenthesis[0]++;
669 pendingSpace = false;
672 case TokenNameRPAREN:
673 // Decrease the parenthesis count
674 // if there is no more unclosed parenthesis,
675 // a new line and indent may be append (depending on the next
677 if ((openParenthesisCount > 1) && (openParenthesis[openParenthesisCount - 1] > 0)) {
678 openParenthesis[openParenthesisCount - 1]--;
679 if (openParenthesis[openParenthesisCount - 1] <= 0) {
680 pendingNewlineAfterParen = true;
681 inAssignment = false;
682 openParenthesisCount--;
685 openParenthesis[0]--;
687 pendingSpace = false;
689 case TokenNameLBRACE:
690 if (previousCompilableToken == TokenNameDOLLAR) {
693 if ((previousCompilableToken == TokenNameRBRACKET) || (previousCompilableToken == TokenNameEQUAL)) {
694 // if (previousCompilableToken == TokenNameRBRACKET) {
695 inArrayAssignment = true;
696 inAssignment = false;
698 if (inArrayAssignment) {
699 indentationLevel += pushBlock();
701 // Add new line and increase indentation level after open brace.
703 indentationLevel += pushBlock();
707 case TokenNameRBRACE:
708 if (dollarBraceCount > 0) {
712 if (previousCompilableToken == TokenNameRPAREN) {
713 pendingSpace = false;
715 if (inArrayAssignment) {
716 inArrayAssignment = false;
718 indentationLevel += popInclusiveUntilBlock();
721 indentationLevel += popInclusiveUntilBlock();
722 if (previousCompilableToken == TokenNameRPAREN) {
723 // fix for 1FGDDV6: LFCOM:WIN98 - Weird splitting on message
725 currentLineBuffer.append(options.lineSeparatorSequence);
726 increaseLineDelta(options.lineSeparatorSequence.length);
728 if (constructionsCount > 0) {
729 switch (constructions[constructionsCount - 1]) {
731 case TokenNameforeach:
732 //indentationLevel += popExclusiveUntilBlock();
734 case TokenNameswitch:
739 case TokenNamefinally:
742 // case TokenNamesynchronized :
743 clearNonBlockIndents = true;
750 case TokenNameLBRACKET:
752 pendingSpace = false;
754 case TokenNameRBRACKET:
755 openBracketCount -= (openBracketCount > 0) ? 1 : 0;
756 // if there is no left bracket to close, the right bracket is
758 pendingSpace = false;
762 pendingSpace = false;
764 case TokenNameSEMICOLON:
765 // Do not generate line terminators in the definition of
766 // the for statement.
767 // if not in this case, jump a line and reduce indentation after
769 // if the block it closes belongs to a conditional statement (if,
771 if (openParenthesisCount <= 1) {
773 if (expectingOpenBrace) {
774 clearNonBlockIndents = true;
775 expectingOpenBrace = false;
778 inAssignment = false;
779 pendingSpace = false;
781 case TokenNamePLUS_PLUS:
782 case TokenNameMINUS_MINUS:
783 // Do not put a space between a post-increment/decrement
784 // and the identifier being modified.
785 if (previousToken == TokenNameIdentifier || previousToken == TokenNameRBRACKET) {
786 pendingSpace = false;
790 // previously ADDITION
792 // Handle the unary operators plus and minus via a flag
793 if (!isLiteralToken(previousToken) && previousToken != TokenNameIdentifier && previousToken != TokenNameRPAREN
794 && previousToken != TokenNameRBRACKET) {
795 unarySignModifier = 1;
799 // In a switch/case statement, add a newline & indent
800 // when a colon is encountered.
801 if (tokenBeforeColonCount > 0) {
802 if (tokenBeforeColon[tokenBeforeColonCount - 1] == TokenNamecase) {
805 tokenBeforeColonCount--;
811 case Scanner.TokenNameCOMMENT_LINE:
814 currentLineIndentationLevel++;
816 break; // a line is always inserted after a one-line comment
817 case Scanner.TokenNameCOMMENT_PHPDOC:
818 case Scanner.TokenNameCOMMENT_BLOCK:
819 currentCommentOffset = getCurrentCommentOffset();
822 case Scanner.TokenNameWHITESPACE:
823 if (!phpTagAndWhitespace) {
824 // Count the number of line terminators in the whitespace so
825 // line spacing can be preserved near comments.
826 char[] source = scanner.source;
827 newLinesInWhitespace = 0;
828 for (int i = scanner.startPosition, max = scanner.currentPosition; i < max; i++) {
829 if (source[i] == '\r') {
831 if (source[++i] == '\n') {
832 newLinesInWhitespace++;
834 newLinesInWhitespace++;
837 newLinesInWhitespace++;
839 } else if (source[i] == '\n') {
840 newLinesInWhitespace++;
843 increaseLineDelta(scanner.startPosition - scanner.currentPosition);
846 // case TokenNameHTML :
847 // // Add the next token to the formatted source string.
848 // // outputCurrentToken(token);
849 // int startPosition = scanner.startPosition;
851 // for (int i = startPosition, max = scanner.currentPosition; i <
853 // char currentCharacter = scanner.source[i];
854 // updateMappedPositions(i);
855 // currentLineBuffer.append(currentCharacter);
859 if ((token == TokenNameIdentifier) || isLiteralToken(token) || token == TokenNamesuper) {
860 // || token == TokenNamethis) {
861 // Do not put a space between a unary operator
862 // (eg: ++, --, +, -) and the identifier being modified.
863 if (previousToken == TokenNamePLUS_PLUS || previousToken == TokenNameMINUS_MINUS
864 || (previousToken == TokenNameMINUS_GREATER && options.compactDereferencingMode) // ->
865 || (previousToken == TokenNamePLUS && unarySignModifier > 0)
866 || (previousToken == TokenNameMINUS && unarySignModifier > 0)) {
867 pendingSpace = false;
869 unarySignModifier = 0;
873 // Do not output whitespace tokens.
874 if (token != Scanner.TokenNameWHITESPACE || phpTagAndWhitespace) {
876 * Add pending space to the formatted source string. Do not output a space under the following circumstances: 1) this is
877 * the first pass 2) previous token is an open paren 3) previous token is a period 4) previous token is the logical
878 * compliment (eg: !) 5) previous token is the bitwise compliment (eg: ~) 6) previous token is the open bracket (eg: [) 7)
879 * in an assignment statement, if the previous token is an open brace or the current token is a close brace 8) previous
880 * token is a single line comment 9) current token is a '->'
882 if (token == TokenNameMINUS_GREATER && options.compactDereferencingMode)
883 pendingSpace = false;
885 boolean openAndCloseBrace = previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE;
886 if (pendingSpace && insertSpaceAfter(previousToken)
887 && !(inAssignment && (previousToken == TokenNameLBRACE || token == TokenNameRBRACE))
888 && previousToken != Scanner.TokenNameCOMMENT_LINE) {
889 if ((!(options.compactAssignmentMode && token == TokenNameEQUAL)) && !openAndCloseBrace)
892 // Add the next token to the formatted source string.
893 outputCurrentToken(token);
894 if (token == Scanner.TokenNameCOMMENT_LINE && openParenthesisCount > 1) {
896 currentLineBuffer.append(options.lineSeparatorSequence);
897 increaseLineDelta(options.lineSeparatorSequence.length);
901 // Whitespace tokens do not need to be remembered.
902 if (token != Scanner.TokenNameWHITESPACE || phpTagAndWhitespace) {
903 previousToken = token;
904 if (token != Scanner.TokenNameCOMMENT_BLOCK && token != Scanner.TokenNameCOMMENT_LINE
905 && token != Scanner.TokenNameCOMMENT_PHPDOC) {
906 previousCompilableToken = token;
910 output(copyRemainingSource());
912 // dump the last token of the source in the formatted output.
913 } catch (InvalidInputException e) {
914 output(copyRemainingSource());
916 // dump the last token of the source in the formatted output.
921 * Formats the char array <code>sourceString</code>, and returns a string containing the formatted version.
923 * @return the formatted ouput.
925 public String formatSourceString(String sourceString) {
926 char[] sourceChars = sourceString.toCharArray();
927 formattedSource = new StringBuffer(sourceChars.length);
928 scanner.setSource(sourceChars);
930 return formattedSource.toString();
934 * Formats the char array <code>sourceString</code>, and returns a string containing the formatted version.
937 * the string to format
938 * @param indentationLevel
939 * the initial indentation level
940 * @return the formatted ouput.
942 public String format(String string, int indentationLevel) {
943 return format(string, indentationLevel, (int[]) null);
947 * Formats the char array <code>sourceString</code>, and returns a string containing the formatted version. The positions array
948 * is modified to contain the mapped positions.
951 * the string to format
952 * @param indentationLevel
953 * the initial indentation level
955 * the array of positions to map
956 * @return the formatted ouput.
958 public String format(String string, int indentationLevel, int[] positions) {
959 return this.format(string, indentationLevel, positions, null);
962 public String format(String string, int indentationLevel, int[] positions, String lineSeparator) {
963 if (lineSeparator != null) {
964 this.options.setLineSeparator(lineSeparator);
966 if (positions != null) {
967 this.setPositionsToMap(positions);
968 this.setInitialIndentationLevel(indentationLevel);
969 String formattedString = this.formatSourceString(string);
970 int[] mappedPositions = this.getMappedPositions();
971 System.arraycopy(mappedPositions, 0, positions, 0, positions.length);
972 return formattedString;
974 this.setInitialIndentationLevel(indentationLevel);
975 return this.formatSourceString(string);
980 * Formats the char array <code>sourceString</code>, and returns a string containing the formatted version. The initial
981 * indentation level is 0.
984 * the string to format
985 * @return the formatted ouput.
987 public String format(String string) {
988 return this.format(string, 0, (int[]) null);
992 * Formats a given source string, starting indenting it at a particular depth and using the given options
994 * @deprecated backport 1.0 internal functionality
996 public static String format(String sourceString, int initialIndentationLevel, ConfigurableOption[] options) {
997 CodeFormatter formatter = new CodeFormatter(options);
998 formatter.setInitialIndentationLevel(initialIndentationLevel);
999 return formatter.formatSourceString(sourceString);
1003 * Returns the number of characters and tab char between the beginning of the line and the beginning of the comment.
1005 private int getCurrentCommentOffset() {
1006 int linePtr = scanner.linePtr;
1007 // if there is no beginning of line, return 0.
1011 int beginningOfLine = scanner.lineEnds[linePtr];
1012 int currentStartPosition = scanner.startPosition;
1013 char[] source = scanner.source;
1014 // find the position of the beginning of the line containing the comment
1015 while (beginningOfLine > currentStartPosition) {
1017 beginningOfLine = scanner.lineEnds[--linePtr];
1019 beginningOfLine = 0;
1023 for (int i = currentStartPosition - 1; i >= beginningOfLine; i--) {
1024 char currentCharacter = source[i];
1025 switch (currentCharacter) {
1027 offset += options.tabSize;
1043 * Returns an array of descriptions for the configurable options. The descriptions may be changed and passed back to a different
1046 * @deprecated backport 1.0 internal functionality
1048 public static ConfigurableOption[] getDefaultOptions(Locale locale) {
1049 String componentName = CodeFormatter.class.getName();
1050 FormatterOptions options = new FormatterOptions();
1051 return new ConfigurableOption[] {
1052 new ConfigurableOption(componentName, "newline.openingBrace", locale, options.newLineBeforeOpeningBraceMode ? 0 : 1),
1054 new ConfigurableOption(componentName, "newline.controlStatement", locale, options.newlineInControlStatementMode ? 0 : 1),
1056 new ConfigurableOption(componentName, "newline.clearAll", locale, options.clearAllBlankLinesMode ? 0 : 1),
1058 // new ConfigurableOption(componentName, "newline.elseIf", locale,
1059 // options.compactElseIfMode ? 0 : 1), //$NON-NLS-1$
1060 new ConfigurableOption(componentName, "newline.emptyBlock", locale, options.newLineInEmptyBlockMode ? 0 : 1),
1062 new ConfigurableOption(componentName, "line.split", locale, options.maxLineLength),
1064 new ConfigurableOption(componentName, "style.compactAssignment", locale, options.compactAssignmentMode ? 0 : 1),
1066 new ConfigurableOption(componentName, "tabulation.char", locale, options.indentWithTab ? 0 : 1),
1068 new ConfigurableOption(componentName, "tabulation.size", locale, options.tabSize) //$NON-NLS-1$
1073 * Returns the array of mapped positions. Returns null is no positions have been set.
1076 * @deprecated There is no need to retrieve the mapped positions anymore.
1078 public int[] getMappedPositions() {
1079 return mappedPositions;
1083 * Returns the priority of the token given as argument <br>
1084 * The most prioritary the token is, the smallest the return value is.
1086 * @return the priority of <code>token</code>
1088 * the token of which the priority is requested
1090 private static int getTokenPriority(int token) {
1092 case TokenNameextends:
1093 // case TokenNameimplements :
1094 // case TokenNamethrows :
1096 case TokenNameSEMICOLON:
1099 case TokenNameCOMMA:
1102 case TokenNameEQUAL:
1105 case TokenNameAND_AND:
1107 case TokenNameOR_OR:
1110 case TokenNameQUESTION:
1112 case TokenNameCOLON:
1114 return 50; // it's better cutting on ?: than on ;
1115 case TokenNameEQUAL_EQUAL:
1117 case TokenNameEQUAL_EQUAL_EQUAL:
1119 case TokenNameNOT_EQUAL:
1121 case TokenNameNOT_EQUAL_EQUAL:
1126 case TokenNameLESS_EQUAL:
1128 case TokenNameGREATER:
1130 case TokenNameGREATER_EQUAL:
1132 // case TokenNameinstanceof : // instanceof
1136 case TokenNameMINUS:
1139 case TokenNameMULTIPLY:
1141 case TokenNameDIVIDE:
1143 case TokenNameREMAINDER:
1146 case TokenNameLEFT_SHIFT:
1148 case TokenNameRIGHT_SHIFT:
1150 // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>>
1159 case TokenNameMULTIPLY_EQUAL:
1161 case TokenNameDIVIDE_EQUAL:
1163 case TokenNameREMAINDER_EQUAL:
1165 case TokenNamePLUS_EQUAL:
1167 case TokenNameMINUS_EQUAL:
1169 case TokenNameLEFT_SHIFT_EQUAL:
1171 case TokenNameRIGHT_SHIFT_EQUAL:
1173 // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>=
1174 case TokenNameAND_EQUAL:
1176 case TokenNameXOR_EQUAL:
1178 case TokenNameOR_EQUAL:
1180 case TokenNameDOT_EQUAL:
1187 return Integer.MAX_VALUE;
1192 * Handles the exception raised when an invalid token is encountered. Returns true if the exception has been handled, false
1195 private boolean handleInvalidToken(Exception e) {
1196 if (e.getMessage().equals(Scanner.INVALID_CHARACTER_CONSTANT) || e.getMessage().equals(Scanner.INVALID_CHAR_IN_STRING)
1197 || e.getMessage().equals(Scanner.INVALID_ESCAPE)) {
1203 private final void increaseGlobalDelta(int offset) {
1204 globalDelta += offset;
1207 private final void increaseLineDelta(int offset) {
1208 lineDelta += offset;
1211 private final void increaseSplitDelta(int offset) {
1212 splitDelta += offset;
1216 * Returns true if a space has to be inserted after <code>operator</code> false otherwise.
1218 private boolean insertSpaceAfter(int token) {
1220 case TokenNameLPAREN:
1222 case TokenNameTWIDDLE:
1226 case TokenNameWHITESPACE:
1227 case TokenNameLBRACKET:
1228 case TokenNameDOLLAR:
1229 case Scanner.TokenNameCOMMENT_LINE:
1237 * Returns true if a space has to be inserted before <code>operator</code> false otherwise. <br>
1238 * Cannot be static as it uses the code formatter options (to know if the compact assignment mode is on).
1240 private boolean insertSpaceBefore(int token) {
1242 case TokenNameEQUAL:
1243 return (!options.compactAssignmentMode);
1249 private static boolean isComment(int token) {
1250 boolean result = token == Scanner.TokenNameCOMMENT_BLOCK || token == Scanner.TokenNameCOMMENT_LINE
1251 || token == Scanner.TokenNameCOMMENT_PHPDOC;
1255 private static boolean isLiteralToken(int token) {
1256 boolean result = token == TokenNameIntegerLiteral
1257 // || token == TokenNameLongLiteral
1258 // || token == TokenNameFloatingPointLiteral
1259 || token == TokenNameDoubleLiteral
1260 // || token == TokenNameCharacterLiteral
1261 || token == TokenNameStringDoubleQuote;
1266 * If the length of <code>oneLineBuffer</code> exceeds <code>maxLineLength</code>, it is split and the result is dumped in
1267 * <code>formattedSource</code>
1269 * @param newLineCount
1270 * the number of new lines to append
1272 private void newLine(int newLineCount) {
1273 // format current line
1275 beginningOfLineIndex = formattedSource.length();
1276 String currentLine = currentLineBuffer.toString();
1277 if (containsOpenCloseBraces) {
1278 containsOpenCloseBraces = false;
1279 outputLine(currentLine, false, indentationLevelForOpenCloseBraces, 0, -1, null, 0);
1280 indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
1282 outputLine(currentLine, false, currentLineIndentationLevel, 0, -1, null, 0);
1284 // dump line break(s)
1285 for (int i = 0; i < newLineCount; i++) {
1286 formattedSource.append(options.lineSeparatorSequence);
1287 increaseSplitDelta(options.lineSeparatorSequence.length);
1289 // reset formatter for next line
1290 int currentLength = currentLine.length();
1291 currentLineBuffer = new StringBuffer(currentLength > maxLineSize ? maxLineSize = currentLength : maxLineSize);
1292 increaseGlobalDelta(splitDelta);
1293 increaseGlobalDelta(lineDelta);
1295 currentLineIndentationLevel = initialIndentationLevel;
1298 private String operatorString(int operator) {
1300 case TokenNameextends:
1301 return "extends"; //$NON-NLS-1$
1302 // case TokenNameimplements :
1303 // return "implements"; //$NON-NLS-1$
1305 // case TokenNamethrows :
1306 // return "throws"; //$NON-NLS-1$
1307 case TokenNameSEMICOLON:
1309 return ";"; //$NON-NLS-1$
1310 case TokenNameCOMMA:
1312 return ","; //$NON-NLS-1$
1313 case TokenNameEQUAL:
1315 return "="; //$NON-NLS-1$
1316 case TokenNameAND_AND:
1318 return "&&"; //$NON-NLS-1$
1319 case TokenNameOR_OR:
1321 return "||"; //$NON-NLS-1$
1322 case TokenNameQUESTION:
1324 return "?"; //$NON-NLS-1$
1325 case TokenNameCOLON:
1327 return ":"; //$NON-NLS-1$
1328 case TokenNamePAAMAYIM_NEKUDOTAYIM:
1330 return "::"; //$NON-NLS-1$
1331 case TokenNameEQUAL_EQUAL:
1332 // == (15.20, 15.20.1, 15.20.2, 15.20.3)
1333 return "=="; //$NON-NLS-1$
1334 case TokenNameEQUAL_EQUAL_EQUAL:
1335 // == (15.20, 15.20.1, 15.20.2, 15.20.3)
1336 return "==="; //$NON-NLS-1$
1337 case TokenNameEQUAL_GREATER:
1339 return "=>"; //$NON-NLS-1$
1340 case TokenNameNOT_EQUAL:
1341 // != (15.20, 15.20.1, 15.20.2, 15.20.3)
1342 return "!="; //$NON-NLS-1$
1343 case TokenNameNOT_EQUAL_EQUAL:
1344 // != (15.20, 15.20.1, 15.20.2, 15.20.3)
1345 return "!=="; //$NON-NLS-1$
1348 return "<"; //$NON-NLS-1$
1349 case TokenNameLESS_EQUAL:
1351 return "<="; //$NON-NLS-1$
1352 case TokenNameGREATER:
1354 return ">"; //$NON-NLS-1$
1355 case TokenNameGREATER_EQUAL:
1357 return ">="; //$NON-NLS-1$
1358 // case TokenNameinstanceof : // instanceof
1359 // return "instanceof"; //$NON-NLS-1$
1361 // + (15.17, 15.17.2)
1362 return "+"; //$NON-NLS-1$
1363 case TokenNameMINUS:
1365 return "-"; //$NON-NLS-1$
1366 case TokenNameMULTIPLY:
1368 return "*"; //$NON-NLS-1$
1369 case TokenNameDIVIDE:
1371 return "/"; //$NON-NLS-1$
1372 case TokenNameREMAINDER:
1374 return "%"; //$NON-NLS-1$
1375 case TokenNameLEFT_SHIFT:
1377 return "<<"; //$NON-NLS-1$
1378 case TokenNameRIGHT_SHIFT:
1380 return ">>"; //$NON-NLS-1$
1381 // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18)
1382 // return ">>>"; //$NON-NLS-1$
1384 // & (15.21, 15.21.1, 15.21.2)
1385 return "&"; //$NON-NLS-1$
1387 // | (15.21, 15.21.1, 15.21.2)
1388 return "|"; //$NON-NLS-1$
1390 // ^ (15.21, 15.21.1, 15.21.2)
1391 return "^"; //$NON-NLS-1$
1392 case TokenNameMULTIPLY_EQUAL:
1394 return "*="; //$NON-NLS-1$
1395 case TokenNameDIVIDE_EQUAL:
1397 return "/="; //$NON-NLS-1$
1398 case TokenNameREMAINDER_EQUAL:
1400 return "%="; //$NON-NLS-1$
1401 case TokenNamePLUS_EQUAL:
1403 return "+="; //$NON-NLS-1$
1404 case TokenNameMINUS_EQUAL:
1406 return "-="; //$NON-NLS-1$
1407 case TokenNameMINUS_GREATER:
1409 return "->"; //$NON-NLS-1$
1410 case TokenNameLEFT_SHIFT_EQUAL:
1412 return "<<="; //$NON-NLS-1$
1413 case TokenNameRIGHT_SHIFT_EQUAL:
1415 return ">>="; //$NON-NLS-1$
1416 // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2)
1417 // return ">>>="; //$NON-NLS-1$
1418 case TokenNameAND_EQUAL:
1420 return "&="; //$NON-NLS-1$
1421 case TokenNameXOR_EQUAL:
1423 return "^="; //$NON-NLS-1$
1424 case TokenNameOR_EQUAL:
1426 return "|="; //$NON-NLS-1$
1427 case TokenNameDOT_EQUAL:
1429 return ".="; //$NON-NLS-1$
1432 return "."; //$NON-NLS-1$
1434 return ""; //$NON-NLS-1$
1439 * Appends <code>stringToOutput</code> to the formatted output. <br>
1440 * If it contains \n, append a LINE_SEPARATOR and indent after it.
1442 private void output(String stringToOutput) {
1443 char currentCharacter;
1444 for (int i = 0, max = stringToOutput.length(); i < max; i++) {
1445 currentCharacter = stringToOutput.charAt(i);
1446 if (currentCharacter != '\t') {
1447 currentLineBuffer.append(currentCharacter);
1452 private void outputCurrentTokenWithoutIndent(int token) {
1454 formattedSource.append(scanner.source, scanner.startPosition, scanner.currentPosition - scanner.startPosition);
1458 * Appends <code>token</code> to the formatted output. <br>
1459 * If it contains <code>\n</code>, append a LINE_SEPARATOR and indent after it.
1461 private void outputCurrentToken(int token) {
1462 char[] source = scanner.source;
1463 int startPosition = scanner.startPosition;
1465 case Scanner.TokenNameCOMMENT_PHPDOC:
1466 case Scanner.TokenNameCOMMENT_BLOCK:
1467 case Scanner.TokenNameCOMMENT_LINE:
1468 boolean endOfLine = false;
1469 int currentCommentOffset = getCurrentCommentOffset();
1470 int beginningOfLineSpaces = 0;
1472 currentCommentOffset = getCurrentCommentOffset();
1473 beginningOfLineSpaces = 0;
1474 boolean pendingCarriageReturn = false;
1475 for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
1476 char currentCharacter = source[i];
1477 updateMappedPositions(i);
1478 switch (currentCharacter) {
1480 pendingCarriageReturn = true;
1484 if (pendingCarriageReturn) {
1485 increaseGlobalDelta(options.lineSeparatorSequence.length - 2);
1487 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1489 pendingCarriageReturn = false;
1490 currentLineBuffer.append(options.lineSeparatorSequence);
1491 beginningOfLineSpaces = 0;
1495 if (pendingCarriageReturn) {
1496 pendingCarriageReturn = false;
1497 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1498 currentLineBuffer.append(options.lineSeparatorSequence);
1499 beginningOfLineSpaces = 0;
1503 // we remove a maximum of currentCommentOffset characters (tabs
1504 // are converted to space numbers).
1505 beginningOfLineSpaces += options.tabSize;
1506 if (beginningOfLineSpaces > currentCommentOffset) {
1507 currentLineBuffer.append(currentCharacter);
1509 increaseGlobalDelta(-1);
1512 currentLineBuffer.append(currentCharacter);
1516 if (pendingCarriageReturn) {
1517 pendingCarriageReturn = false;
1518 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1519 currentLineBuffer.append(options.lineSeparatorSequence);
1520 beginningOfLineSpaces = 0;
1524 // we remove a maximum of currentCommentOffset characters (tabs
1525 // are converted to space numbers).
1526 beginningOfLineSpaces++;
1527 if (beginningOfLineSpaces > currentCommentOffset) {
1528 currentLineBuffer.append(currentCharacter);
1530 increaseGlobalDelta(-1);
1533 currentLineBuffer.append(currentCharacter);
1537 if (pendingCarriageReturn) {
1538 pendingCarriageReturn = false;
1539 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1540 currentLineBuffer.append(options.lineSeparatorSequence);
1541 beginningOfLineSpaces = 0;
1544 beginningOfLineSpaces = 0;
1545 currentLineBuffer.append(currentCharacter);
1550 updateMappedPositions(scanner.currentPosition - 1);
1551 multipleLineCommentCounter++;
1554 for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
1555 char currentCharacter = source[i];
1556 updateMappedPositions(i);
1557 currentLineBuffer.append(currentCharacter);
1563 * Outputs <code>currentString</code>:<br>
1565 * <li>If its length is < maxLineLength, output
1566 * <li>Otherwise it is split.
1569 * @param currentString
1571 * @param preIndented
1572 * whether the string to output was pre-indented
1574 * number of indentation to put in front of <code>currentString</code>
1576 * value of the operator belonging to <code>currentString</code>.
1578 private void outputLine(String currentString, boolean preIndented, int depth, int operator, int substringIndex,
1579 int[] startSubstringIndexes, int offsetInGlobalLine) {
1580 boolean emptyFirstSubString = false;
1581 String operatorString = operatorString(operator);
1582 boolean placeOperatorBehind = !breakLineBeforeOperator(operator);
1583 boolean placeOperatorAhead = !placeOperatorBehind;
1584 // dump prefix operator?
1585 if (placeOperatorAhead) {
1590 if (operator != 0) {
1591 if (insertSpaceBefore(operator)) {
1592 formattedSource.append(' ');
1593 increaseSplitDelta(1);
1595 formattedSource.append(operatorString);
1596 increaseSplitDelta(operatorString.length());
1597 if (insertSpaceAfter(operator) && operator != TokenNameimplements && operator != TokenNameextends) {
1598 // && operator != TokenNamethrows) {
1599 formattedSource.append(' ');
1600 increaseSplitDelta(1);
1604 SplitLine splitLine = null;
1605 if (options.maxLineLength == 0 || getLength(currentString, depth) < options.maxLineLength
1606 || (splitLine = split(currentString, offsetInGlobalLine)) == null) {
1607 // depending on the type of operator, outputs new line before of after
1609 // indent before postfix operator
1610 // indent also when the line cannot be split
1611 if (operator == TokenNameextends || operator == TokenNameimplements) {
1612 // || operator == TokenNamethrows) {
1613 formattedSource.append(' ');
1614 increaseSplitDelta(1);
1616 if (placeOperatorBehind) {
1621 int max = currentString.length();
1622 if (multipleLineCommentCounter != 0) {
1624 BufferedReader reader = new BufferedReader(new StringReader(currentString));
1625 String line = reader.readLine();
1626 while (line != null) {
1627 updateMappedPositionsWhileSplitting(beginningOfLineIndex, beginningOfLineIndex + line.length()
1628 + options.lineSeparatorSequence.length);
1629 formattedSource.append(line);
1630 beginningOfLineIndex = beginningOfLineIndex + line.length();
1631 if ((line = reader.readLine()) != null) {
1632 formattedSource.append(options.lineSeparatorSequence);
1633 beginningOfLineIndex += options.lineSeparatorSequence.length;
1634 dumpTab(currentLineIndentationLevel);
1638 } catch (IOException e) {
1639 e.printStackTrace();
1642 updateMappedPositionsWhileSplitting(beginningOfLineIndex, beginningOfLineIndex + max);
1643 for (int i = 0; i < max; i++) {
1644 char currentChar = currentString.charAt(i);
1645 switch (currentChar) {
1650 // fix for 1FFYL5C: LFCOM:ALL - Incorrect indentation when
1651 // split with a comment inside a condition
1652 // a substring cannot end with a lineSeparatorSequence,
1653 // except if it has been added by format() after a one-line
1655 formattedSource.append(options.lineSeparatorSequence);
1656 // 1FGDDV6: LFCOM:WIN98 - Weird splitting on message expression
1661 formattedSource.append(currentChar);
1665 // update positions inside the mappedPositions table
1666 if (substringIndex != -1) {
1667 if (multipleLineCommentCounter == 0) {
1668 int startPosition = beginningOfLineIndex + startSubstringIndexes[substringIndex];
1669 updateMappedPositionsWhileSplitting(startPosition, startPosition + max);
1671 // compute the splitDelta resulting with the operator and blank removal
1672 if (substringIndex + 1 != startSubstringIndexes.length) {
1673 increaseSplitDelta(startSubstringIndexes[substringIndex] + max - startSubstringIndexes[substringIndex + 1]);
1676 // dump postfix operator?
1677 if (placeOperatorBehind) {
1678 if (insertSpaceBefore(operator)) {
1679 formattedSource.append(' ');
1680 if (operator != 0) {
1681 increaseSplitDelta(1);
1684 formattedSource.append(operatorString);
1685 if (operator != 0) {
1686 increaseSplitDelta(operatorString.length());
1691 // fix for 1FG0BA3: LFCOM:WIN98 - Weird splitting on interfaces
1692 // extends has to stand alone on a line when currentString has been split.
1693 if (options.maxLineLength != 0 && splitLine != null && (operator == TokenNameextends)) {
1694 // || operator == TokenNameimplements
1695 // || operator == TokenNamethrows)) {
1696 formattedSource.append(options.lineSeparatorSequence);
1697 increaseSplitDelta(options.lineSeparatorSequence.length);
1700 if (operator == TokenNameextends) {
1701 // || operator == TokenNameimplements
1702 // || operator == TokenNamethrows) {
1703 formattedSource.append(' ');
1704 increaseSplitDelta(1);
1707 // perform actual splitting
1708 String result[] = splitLine.substrings;
1709 int[] splitOperators = splitLine.operators;
1710 if (result[0].length() == 0) {
1711 // when the substring 0 is null, the substring 1 is correctly indented.
1713 emptyFirstSubString = true;
1715 // the operator going in front of the result[0] string is the operator
1717 for (int i = 0, max = result.length; i < max; i++) {
1718 // the new depth is the current one if this is the first substring,
1719 // the current one + 1 otherwise.
1720 // if the substring is a comment, use the current indentation Level
1721 // instead of the depth
1722 // (-1 because the ouputline increases depth).
1723 // (fix for 1FFC72R: LFCOM:ALL - Incorrect line split in presence of line
1725 String currentResult = result[i];
1726 if (currentResult.length() != 0 || splitOperators[i] != 0) {
1727 int newDepth = (currentResult.startsWith("/*") //$NON-NLS-1$
1728 || currentResult.startsWith("//")) //$NON-NLS-1$
1729 ? indentationLevel - 1 : depth;
1730 outputLine(currentResult, i == 0 || (i == 1 && emptyFirstSubString) ? preIndented : false,
1731 i == 0 ? newDepth : newDepth + 1, splitOperators[i], i, splitLine.startSubstringsIndexes, currentString
1732 .indexOf(currentResult));
1734 formattedSource.append(options.lineSeparatorSequence);
1735 increaseSplitDelta(options.lineSeparatorSequence.length);
1739 if (result.length == splitOperators.length - 1) {
1740 int lastOperator = splitOperators[result.length];
1741 String lastOperatorString = operatorString(lastOperator);
1742 formattedSource.append(options.lineSeparatorSequence);
1743 increaseSplitDelta(options.lineSeparatorSequence.length);
1744 if (breakLineBeforeOperator(lastOperator)) {
1746 if (lastOperator != 0) {
1747 if (insertSpaceBefore(lastOperator)) {
1748 formattedSource.append(' ');
1749 increaseSplitDelta(1);
1751 formattedSource.append(lastOperatorString);
1752 increaseSplitDelta(lastOperatorString.length());
1753 if (insertSpaceAfter(lastOperator) && lastOperator != TokenNameimplements && lastOperator != TokenNameextends) {
1754 // && lastOperator != TokenNamethrows) {
1755 formattedSource.append(' ');
1756 increaseSplitDelta(1);
1761 if (placeOperatorBehind) {
1762 if (insertSpaceBefore(operator)) {
1763 formattedSource.append(' ');
1764 increaseSplitDelta(1);
1766 formattedSource.append(operatorString);
1767 //increaseSplitDelta(operatorString.length());
1772 * Pops the top statement of the stack if it is <code>token</code>
1774 private int pop(int token) {
1776 if ((constructionsCount > 0) && (constructions[constructionsCount - 1] == token)) {
1778 constructionsCount--;
1784 * Pops the top statement of the stack if it is a <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.
1786 private int popBlock() {
1788 if ((constructionsCount > 0)
1789 && ((constructions[constructionsCount - 1] == BLOCK) || (constructions[constructionsCount - 1] == NONINDENT_BLOCK))) {
1790 if (constructions[constructionsCount - 1] == BLOCK)
1792 constructionsCount--;
1798 * Pops elements until the stack is empty or the top element is <code>token</code>.<br>
1799 * Does not remove <code>token</code> from the stack.
1802 * the token to be left as the top of the stack
1804 private int popExclusiveUntil(int token) {
1806 int startCount = constructionsCount;
1807 for (int i = startCount - 1; i >= 0 && constructions[i] != token; i--) {
1808 if (constructions[i] != NONINDENT_BLOCK)
1810 constructionsCount--;
1816 * Pops elements until the stack is empty or the top element is a <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.<br>
1817 * Does not remove it from the stack.
1819 private int popExclusiveUntilBlock() {
1820 int startCount = constructionsCount;
1822 for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK; i--) {
1823 constructionsCount--;
1830 * Pops elements until the stack is empty or the top element is a <code>BLOCK</code>, a <code>NONINDENT_BLOCK</code> or a
1831 * <code>CASE</code>.<br>
1832 * Does not remove it from the stack.
1834 private int popExclusiveUntilBlockOrCase() {
1835 int startCount = constructionsCount;
1837 for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK
1838 && constructions[i] != TokenNamecase; i--) {
1839 constructionsCount--;
1846 * Pops elements until the stack is empty or the top element is <code>token</code>.<br>
1847 * Removes <code>token</code> from the stack too.
1850 * the token to remove from the stack
1852 private int popInclusiveUntil(int token) {
1853 int startCount = constructionsCount;
1855 for (int i = startCount - 1; i >= 0 && constructions[i] != token; i--) {
1856 if (constructions[i] != NONINDENT_BLOCK)
1858 constructionsCount--;
1860 if (constructionsCount > 0) {
1861 if (constructions[constructionsCount - 1] != NONINDENT_BLOCK)
1863 constructionsCount--;
1869 * Pops elements until the stack is empty or the top element is a <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.<br>
1870 * Does not remove it from the stack.
1872 private int popInclusiveUntilBlock() {
1873 int startCount = constructionsCount;
1875 for (int i = startCount - 1; i >= 0 && (constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK); i--) {
1877 constructionsCount--;
1879 if (constructionsCount > 0) {
1880 if (constructions[constructionsCount - 1] == BLOCK)
1882 constructionsCount--;
1888 * Pushes a block in the stack. <br>
1889 * Pushes a <code>BLOCK</code> if the stack is empty or if the top element is a <code>BLOCK</code>, pushes
1890 * <code>NONINDENT_BLOCK</code> otherwise. Creates a new bigger array if the current one is full.
1892 private int pushBlock() {
1894 if (constructionsCount == constructions.length)
1895 System.arraycopy(constructions, 0, (constructions = new int[constructionsCount * 2]), 0, constructionsCount);
1896 if ((constructionsCount == 0) || (constructions[constructionsCount - 1] == BLOCK)
1897 || (constructions[constructionsCount - 1] == NONINDENT_BLOCK) || (constructions[constructionsCount - 1] == TokenNamecase)) {
1899 constructions[constructionsCount++] = BLOCK;
1901 constructions[constructionsCount++] = NONINDENT_BLOCK;
1907 * Pushes <code>token</code>.<br>
1908 * Creates a new bigger array if the current one is full.
1910 private int pushControlStatement(int token) {
1911 if (constructionsCount == constructions.length)
1912 System.arraycopy(constructions, 0, (constructions = new int[constructionsCount * 2]), 0, constructionsCount);
1913 constructions[constructionsCount++] = token;
1917 private static boolean separateFirstArgumentOn(int currentToken) {
1918 //return (currentToken == TokenNameCOMMA || currentToken ==
1919 // TokenNameSEMICOLON);
1920 return currentToken != TokenNameif && currentToken != TokenNameLPAREN && currentToken != TokenNameNOT
1921 && currentToken != TokenNamewhile && currentToken != TokenNamefor && currentToken != TokenNameforeach
1922 && currentToken != TokenNameswitch;
1926 * Set the positions to map. The mapped positions should be retrieved using the getMappedPositions() method.
1930 * @deprecated Set the positions to map using the format(String, int, int[]) method.
1932 * @see #getMappedPositions()
1934 public void setPositionsToMap(int[] positions) {
1935 positionsToMap = positions;
1938 mappedPositions = new int[positions.length];
1942 * Appends a space character to the current line buffer.
1944 private void space() {
1945 currentLineBuffer.append(' ');
1946 increaseLineDelta(1);
1950 * Splits <code>stringToSplit</code> on the top level token <br>
1951 * If there are several identical token at the same level, the string is cut into many pieces.
1953 * @return an object containing the operator and all the substrings or null if the string cannot be split
1955 public SplitLine split(String stringToSplit) {
1956 return split(stringToSplit, 0);
1960 * Splits <code>stringToSplit</code> on the top level token <br>
1961 * If there are several identical token at the same level, the string is cut into many pieces.
1963 * @return an object containing the operator and all the substrings or null if the string cannot be split
1965 public SplitLine split(String stringToSplit, int offsetInGlobalLine) {
1967 * See http://dev.eclipse.org/bugs/show_bug.cgi?id=12540 and http://dev.eclipse.org/bugs/show_bug.cgi?id=14387
1969 if (stringToSplit.indexOf("//$NON-NLS") != -1) { //$NON-NLS-1$
1972 // split doesn't work correct for PHP
1975 // int currentToken = 0;
1976 // int splitTokenType = 0;
1977 // int splitTokenDepth = Integer.MAX_VALUE;
1978 // int splitTokenPriority = Integer.MAX_VALUE;
1979 // int[] substringsStartPositions = new int[10];
1980 // // contains the start position of substrings
1981 // int[] substringsEndPositions = new int[10];
1982 // // contains the start position of substrings
1983 // int substringsCount = 1; // index in the substringsStartPosition array
1984 // int[] splitOperators = new int[10];
1985 // // contains the start position of substrings
1986 // int splitOperatorsCount = 0; // index in the substringsStartPosition array
1987 // int[] openParenthesisPosition = new int[10];
1988 // int openParenthesisPositionCount = 0;
1989 // int position = 0;
1990 // int lastOpenParenthesisPosition = -1;
1991 // // used to remember the position of the 1st open parenthesis
1992 // // needed for a pattern like: A.B(C); we want formatted like A.B( split C);
1993 // // setup the scanner with a new source
1994 // int lastCommentStartPosition = -1;
1995 // // to remember the start position of the last comment
1996 // int firstTokenOnLine = -1;
1997 // // to remember the first token of the line
1998 // int previousToken = -1;
1999 // // to remember the previous token.
2000 // splitScanner.setSource(stringToSplit.toCharArray());
2002 // // start the loop
2004 // // takes the next token
2006 // if (currentToken != Scanner.TokenNameWHITESPACE)
2007 // previousToken = currentToken;
2008 // currentToken = splitScanner.getNextToken();
2009 // if (Scanner.DEBUG) {
2010 // int currentEndPosition = splitScanner.getCurrentTokenEndPosition();
2011 // int currentStartPosition = splitScanner
2012 // .getCurrentTokenStartPosition();
2013 // System.out.print(currentStartPosition + "," + currentEndPosition
2015 // System.out.println(scanner.toStringAction(currentToken));
2017 // } catch (InvalidInputException e) {
2018 // if (!handleInvalidToken(e))
2020 // currentToken = 0;
2021 // // this value is not modify when an exception is raised.
2023 // if (currentToken == TokenNameEOF)
2025 // if (firstTokenOnLine == -1) {
2026 // firstTokenOnLine = currentToken;
2028 // switch (currentToken) {
2029 // case TokenNameRBRACE :
2030 // case TokenNameRPAREN :
2031 // if (openParenthesisPositionCount > 0) {
2032 // if (openParenthesisPositionCount == 1
2033 // && lastOpenParenthesisPosition < openParenthesisPosition[0]) {
2034 // lastOpenParenthesisPosition = openParenthesisPosition[0];
2035 // } else if ((splitTokenDepth == Integer.MAX_VALUE)
2036 // || (splitTokenDepth > openParenthesisPositionCount && openParenthesisPositionCount == 1)) {
2037 // splitTokenType = 0;
2038 // splitTokenDepth = openParenthesisPositionCount;
2039 // splitTokenPriority = Integer.MAX_VALUE;
2040 // substringsStartPositions[0] = 0;
2041 // // better token means the whole line until now is the first
2043 // substringsCount = 1; // resets the count of substrings
2044 // substringsEndPositions[0] = openParenthesisPosition[0];
2045 // // substring ends on operator start
2046 // position = openParenthesisPosition[0];
2047 // // the string mustn't be cut before the closing parenthesis but
2048 // // after the opening one.
2049 // splitOperatorsCount = 1; // resets the count of split operators
2050 // splitOperators[0] = 0;
2052 // openParenthesisPositionCount--;
2055 // case TokenNameLBRACE :
2056 // case TokenNameLPAREN :
2057 // if (openParenthesisPositionCount == openParenthesisPosition.length) {
2060 // openParenthesisPosition,
2062 // (openParenthesisPosition = new int[openParenthesisPositionCount * 2]),
2063 // 0, openParenthesisPositionCount);
2065 // openParenthesisPosition[openParenthesisPositionCount++] = splitScanner.currentPosition;
2066 // if (currentToken == TokenNameLPAREN
2067 // && previousToken == TokenNameRPAREN) {
2068 // openParenthesisPosition[openParenthesisPositionCount - 1] = splitScanner.startPosition;
2071 // case TokenNameSEMICOLON :
2073 // case TokenNameCOMMA :
2075 // case TokenNameEQUAL :
2077 // if (openParenthesisPositionCount < splitTokenDepth
2078 // || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) {
2079 // // the current token is better than the one we currently have
2080 // // (in level or in priority if same level)
2081 // // reset the substringsCount
2082 // splitTokenDepth = openParenthesisPositionCount;
2083 // splitTokenType = currentToken;
2084 // splitTokenPriority = getTokenPriority(currentToken);
2085 // substringsStartPositions[0] = 0;
2086 // // better token means the whole line until now is the first
2088 // if (separateFirstArgumentOn(firstTokenOnLine)
2089 // && openParenthesisPositionCount > 0) {
2090 // substringsCount = 2; // resets the count of substrings
2091 // substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1];
2092 // substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1];
2093 // substringsEndPositions[1] = splitScanner.startPosition;
2094 // splitOperatorsCount = 2; // resets the count of split operators
2095 // splitOperators[0] = 0;
2096 // splitOperators[1] = currentToken;
2097 // position = splitScanner.currentPosition;
2098 // // next substring will start from operator end
2100 // substringsCount = 1; // resets the count of substrings
2101 // substringsEndPositions[0] = splitScanner.startPosition;
2102 // // substring ends on operator start
2103 // position = splitScanner.currentPosition;
2104 // // next substring will start from operator end
2105 // splitOperatorsCount = 1; // resets the count of split operators
2106 // splitOperators[0] = currentToken;
2109 // if ((openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken))
2110 // && splitTokenType != TokenNameEQUAL
2111 // && currentToken != TokenNameEQUAL) {
2112 // // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after
2114 // // take only the 1st = into account.
2115 // // if another token with the same priority is found,
2116 // // push the start position of the substring and
2117 // // push the token into the stack.
2118 // // create a new array object if the current one is full.
2119 // if (substringsCount == substringsStartPositions.length) {
2122 // substringsStartPositions,
2124 // (substringsStartPositions = new int[substringsCount * 2]),
2125 // 0, substringsCount);
2126 // System.arraycopy(substringsEndPositions, 0,
2127 // (substringsEndPositions = new int[substringsCount * 2]),
2128 // 0, substringsCount);
2130 // if (splitOperatorsCount == splitOperators.length) {
2131 // System.arraycopy(splitOperators, 0,
2132 // (splitOperators = new int[splitOperatorsCount * 2]), 0,
2133 // splitOperatorsCount);
2135 // substringsStartPositions[substringsCount] = position;
2136 // substringsEndPositions[substringsCount++] = splitScanner.startPosition;
2137 // // substring ends on operator start
2138 // position = splitScanner.currentPosition;
2139 // // next substring will start from operator end
2140 // splitOperators[splitOperatorsCount++] = currentToken;
2144 // case TokenNameCOLON :
2146 // // see 1FK7C5R, we only split on a colon, when it is associated
2147 // // with a question-mark.
2148 // // indeed it might appear also behind a case statement, and we do
2149 // // not to break at this point.
2150 // if ((splitOperatorsCount == 0)
2151 // || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) {
2154 // case TokenNameextends :
2155 // case TokenNameimplements :
2156 // //case TokenNamethrows :
2157 // case TokenNameDOT :
2159 // case TokenNameMULTIPLY :
2161 // case TokenNameDIVIDE :
2163 // case TokenNameREMAINDER :
2165 // case TokenNamePLUS :
2166 // // + (15.17, 15.17.2)
2167 // case TokenNameMINUS :
2169 // case TokenNameLEFT_SHIFT :
2171 // case TokenNameRIGHT_SHIFT :
2173 // // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18)
2174 // case TokenNameLESS :
2176 // case TokenNameLESS_EQUAL :
2178 // case TokenNameGREATER :
2180 // case TokenNameGREATER_EQUAL :
2182 // // case TokenNameinstanceof : // instanceof
2183 // case TokenNameEQUAL_EQUAL :
2184 // // == (15.20, 15.20.1, 15.20.2, 15.20.3)
2185 // case TokenNameEQUAL_EQUAL_EQUAL :
2186 // // == (15.20, 15.20.1, 15.20.2, 15.20.3)
2187 // case TokenNameNOT_EQUAL :
2188 // // != (15.20, 15.20.1, 15.20.2, 15.20.3)
2189 // case TokenNameNOT_EQUAL_EQUAL :
2190 // // != (15.20, 15.20.1, 15.20.2, 15.20.3)
2191 // case TokenNameAND :
2192 // // & (15.21, 15.21.1, 15.21.2)
2193 // case TokenNameOR :
2194 // // | (15.21, 15.21.1, 15.21.2)
2195 // case TokenNameXOR :
2196 // // ^ (15.21, 15.21.1, 15.21.2)
2197 // case TokenNameAND_AND :
2199 // case TokenNameOR_OR :
2201 // case TokenNameQUESTION :
2203 // case TokenNameMULTIPLY_EQUAL :
2205 // case TokenNameDIVIDE_EQUAL :
2207 // case TokenNameREMAINDER_EQUAL :
2209 // case TokenNamePLUS_EQUAL :
2211 // case TokenNameMINUS_EQUAL :
2213 // case TokenNameLEFT_SHIFT_EQUAL :
2215 // case TokenNameRIGHT_SHIFT_EQUAL :
2217 // // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2)
2218 // case TokenNameAND_EQUAL :
2220 // case TokenNameXOR_EQUAL :
2222 // case TokenNameOR_EQUAL :
2224 // if ((openParenthesisPositionCount < splitTokenDepth || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority
2225 // > getTokenPriority(currentToken)))
2226 // && !((currentToken == TokenNamePLUS || currentToken == TokenNameMINUS) && (previousToken == TokenNameLBRACE
2227 // || previousToken == TokenNameLBRACKET || splitScanner.startPosition == 0))) {
2228 // // the current token is better than the one we currently have
2229 // // (in level or in priority if same level)
2230 // // reset the substringsCount
2231 // splitTokenDepth = openParenthesisPositionCount;
2232 // splitTokenType = currentToken;
2233 // splitTokenPriority = getTokenPriority(currentToken);
2234 // substringsStartPositions[0] = 0;
2235 // // better token means the whole line until now is the first
2237 // if (separateFirstArgumentOn(firstTokenOnLine)
2238 // && openParenthesisPositionCount > 0) {
2239 // substringsCount = 2; // resets the count of substrings
2240 // substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1];
2241 // substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1];
2242 // substringsEndPositions[1] = splitScanner.startPosition;
2243 // splitOperatorsCount = 3; // resets the count of split operators
2244 // splitOperators[0] = 0;
2245 // splitOperators[1] = 0;
2246 // splitOperators[2] = currentToken;
2247 // position = splitScanner.currentPosition;
2248 // // next substring will start from operator end
2250 // substringsCount = 1; // resets the count of substrings
2251 // substringsEndPositions[0] = splitScanner.startPosition;
2252 // // substring ends on operator start
2253 // position = splitScanner.currentPosition;
2254 // // next substring will start from operator end
2255 // splitOperatorsCount = 2; // resets the count of split operators
2256 // splitOperators[0] = 0;
2257 // // nothing for first operand since operator will be inserted in
2258 // // front of the second operand
2259 // splitOperators[1] = currentToken;
2262 // if (openParenthesisPositionCount == splitTokenDepth
2263 // && splitTokenPriority == getTokenPriority(currentToken)) {
2264 // // if another token with the same priority is found,
2265 // // push the start position of the substring and
2266 // // push the token into the stack.
2267 // // create a new array object if the current one is full.
2268 // if (substringsCount == substringsStartPositions.length) {
2271 // substringsStartPositions,
2273 // (substringsStartPositions = new int[substringsCount * 2]),
2274 // 0, substringsCount);
2275 // System.arraycopy(substringsEndPositions, 0,
2276 // (substringsEndPositions = new int[substringsCount * 2]),
2277 // 0, substringsCount);
2279 // if (splitOperatorsCount == splitOperators.length) {
2280 // System.arraycopy(splitOperators, 0,
2281 // (splitOperators = new int[splitOperatorsCount * 2]), 0,
2282 // splitOperatorsCount);
2284 // substringsStartPositions[substringsCount] = position;
2285 // substringsEndPositions[substringsCount++] = splitScanner.startPosition;
2286 // // substring ends on operator start
2287 // position = splitScanner.currentPosition;
2288 // // next substring will start from operator end
2289 // splitOperators[splitOperatorsCount++] = currentToken;
2295 // if (isComment(currentToken)) {
2296 // lastCommentStartPosition = splitScanner.startPosition;
2298 // lastCommentStartPosition = -1;
2301 // } catch (InvalidInputException e) {
2304 // // if the string cannot be split, return null.
2305 // if (splitOperatorsCount == 0)
2307 // // ## SPECIAL CASES BEGIN
2308 // if (((splitOperatorsCount == 2 && splitOperators[1] == TokenNameDOT
2309 // && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1)
2310 // || (splitOperatorsCount > 2 && splitOperators[1] == TokenNameDOT
2311 // && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1 && lastOpenParenthesisPosition <= options.maxLineLength) ||
2312 // (separateFirstArgumentOn(firstTokenOnLine)
2313 // && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1))
2314 // && (lastOpenParenthesisPosition < splitScanner.source.length && splitScanner.source[lastOpenParenthesisPosition] != ')')) {
2315 // // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis should
2316 // // not be broken on two lines
2317 // // only one split on a top level .
2318 // // or more than one split on . and substring before open parenthesis fits
2320 // // or split inside parenthesis and first token is not a for/while/if
2321 // SplitLine sl = split(
2322 // stringToSplit.substring(lastOpenParenthesisPosition),
2323 // lastOpenParenthesisPosition);
2324 // if (sl == null || sl.operators[0] != TokenNameCOMMA) {
2325 // // trim() is used to remove the extra blanks at the end of the
2326 // // substring. See PR 1FGYPI1
2327 // return new SplitLine(new int[]{0, 0}, new String[]{
2328 // stringToSplit.substring(0, lastOpenParenthesisPosition).trim(),
2329 // stringToSplit.substring(lastOpenParenthesisPosition)}, new int[]{
2330 // offsetInGlobalLine,
2331 // lastOpenParenthesisPosition + offsetInGlobalLine});
2333 // // right substring can be split and is split on comma
2334 // // copy substrings and operators
2335 // // except if the 1st string is empty.
2336 // int startIndex = (sl.substrings[0].length() == 0) ? 1 : 0;
2337 // int subStringsLength = sl.substrings.length + 1 - startIndex;
2338 // String[] result = new String[subStringsLength];
2339 // int[] startIndexes = new int[subStringsLength];
2340 // int operatorsLength = sl.operators.length + 1 - startIndex;
2341 // int[] operators = new int[operatorsLength];
2342 // result[0] = stringToSplit.substring(0, lastOpenParenthesisPosition);
2343 // operators[0] = 0;
2344 // System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes,
2345 // 1, subStringsLength - 1);
2346 // for (int i = subStringsLength - 1; i >= 0; i--) {
2347 // startIndexes[i] += offsetInGlobalLine;
2349 // System.arraycopy(sl.substrings, startIndex, result, 1,
2350 // subStringsLength - 1);
2351 // System.arraycopy(sl.operators, startIndex, operators, 1,
2352 // operatorsLength - 1);
2353 // return new SplitLine(operators, result, startIndexes);
2356 // // if the last token is a comment and the substring before the comment fits
2358 // // split before the comment and return the result.
2359 // if (lastCommentStartPosition > -1
2360 // && lastCommentStartPosition < options.maxLineLength
2361 // && splitTokenPriority > 50) {
2362 // int end = lastCommentStartPosition;
2363 // int start = lastCommentStartPosition;
2364 // if (stringToSplit.charAt(end - 1) == ' ') {
2367 // if (start != end && stringToSplit.charAt(start) == ' ') {
2370 // return new SplitLine(new int[]{0, 0}, new String[]{
2371 // stringToSplit.substring(0, end), stringToSplit.substring(start)},
2372 // new int[]{0, start});
2374 // if (position != stringToSplit.length()) {
2375 // if (substringsCount == substringsStartPositions.length) {
2376 // System.arraycopy(substringsStartPositions, 0,
2377 // (substringsStartPositions = new int[substringsCount * 2]), 0,
2378 // substringsCount);
2379 // System.arraycopy(substringsEndPositions, 0,
2380 // (substringsEndPositions = new int[substringsCount * 2]), 0,
2381 // substringsCount);
2383 // // avoid empty extra substring, e.g. line terminated with a semi-colon
2384 // substringsStartPositions[substringsCount] = position;
2385 // substringsEndPositions[substringsCount++] = stringToSplit.length();
2387 // if (splitOperatorsCount == splitOperators.length) {
2388 // System.arraycopy(splitOperators, 0,
2389 // (splitOperators = new int[splitOperatorsCount * 2]), 0,
2390 // splitOperatorsCount);
2392 // splitOperators[splitOperatorsCount] = 0;
2393 // // the last element of the stack is the position of the end of
2395 // // +1 because the substring method excludes the last character
2396 // String[] result = new String[substringsCount];
2397 // for (int i = 0; i < substringsCount; i++) {
2398 // int start = substringsStartPositions[i];
2399 // int end = substringsEndPositions[i];
2400 // if (stringToSplit.charAt(start) == ' ') {
2402 // substringsStartPositions[i]++;
2404 // if (end != start && stringToSplit.charAt(end - 1) == ' ') {
2407 // result[i] = stringToSplit.substring(start, end);
2408 // substringsStartPositions[i] += offsetInGlobalLine;
2410 // if (splitOperatorsCount > substringsCount) {
2411 // System.arraycopy(substringsStartPositions, 0,
2412 // (substringsStartPositions = new int[splitOperatorsCount]), 0,
2413 // substringsCount);
2414 // System.arraycopy(substringsEndPositions, 0,
2415 // (substringsEndPositions = new int[splitOperatorsCount]), 0,
2416 // substringsCount);
2417 // for (int i = substringsCount; i < splitOperatorsCount; i++) {
2418 // substringsStartPositions[i] = position;
2419 // substringsEndPositions[i] = position;
2421 // System.arraycopy(splitOperators, 0,
2422 // (splitOperators = new int[splitOperatorsCount]), 0,
2423 // splitOperatorsCount);
2425 // System.arraycopy(substringsStartPositions, 0,
2426 // (substringsStartPositions = new int[substringsCount]), 0,
2427 // substringsCount);
2428 // System.arraycopy(substringsEndPositions, 0,
2429 // (substringsEndPositions = new int[substringsCount]), 0,
2430 // substringsCount);
2431 // System.arraycopy(splitOperators, 0,
2432 // (splitOperators = new int[substringsCount]), 0, substringsCount);
2434 // SplitLine splitLine = new SplitLine(splitOperators, result,
2435 // substringsStartPositions);
2436 // return splitLine;
2439 private void updateMappedPositions(int startPosition) {
2440 if (positionsToMap == null) {
2443 char[] source = scanner.source;
2444 int sourceLength = source.length;
2445 while (indexToMap < positionsToMap.length && positionsToMap[indexToMap] <= startPosition) {
2446 int posToMap = positionsToMap[indexToMap];
2447 if (posToMap < 0 || posToMap >= sourceLength) {
2448 // protection against out of bounds position
2449 if (posToMap == sourceLength) {
2450 mappedPositions[indexToMap] = formattedSource.length();
2452 indexToMap = positionsToMap.length; // no more mapping
2455 if (CharOperation.isWhitespace(source[posToMap])) {
2456 mappedPositions[indexToMap] = startPosition + globalDelta + lineDelta;
2458 if (posToMap == sourceLength - 1) {
2459 mappedPositions[indexToMap] = startPosition + globalDelta + lineDelta;
2461 mappedPositions[indexToMap] = posToMap + globalDelta + lineDelta;
2468 private void updateMappedPositionsWhileSplitting(int startPosition, int endPosition) {
2469 if (mappedPositions == null || mappedPositions.length == indexInMap)
2471 while (indexInMap < mappedPositions.length && startPosition <= mappedPositions[indexInMap]
2472 && mappedPositions[indexInMap] < endPosition && indexInMap < indexToMap) {
2473 mappedPositions[indexInMap] += splitDelta;
2478 private int getLength(String s, int tabDepth) {
2480 for (int i = 0; i < tabDepth; i++) {
2481 length += options.tabSize;
2483 for (int i = 0, max = s.length(); i < max; i++) {
2484 char currentChar = s.charAt(i);
2485 switch (currentChar) {
2487 length += options.tabSize;
2497 * Sets the initial indentation level
2499 * @param indentationLevel
2500 * new indentation level
2504 public void setInitialIndentationLevel(int newIndentationLevel) {
2505 this.initialIndentationLevel = currentLineIndentationLevel = indentationLevel = newIndentationLevel;