From 042f4a095666c41dc2130880b2e9508ab84a9f86 Mon Sep 17 00:00:00 2001 From: khartlage Date: Mon, 10 May 2004 20:54:57 +0000 Subject: [PATCH] improved code formatter --- .../phpeditor/php/test/PHPFormatterTest.java | 79 +- .../phpdt/internal/compiler/parser/Scanner.java | 3 + .../phpdt/internal/corext/template/Template.java | 3 + .../phpdt/internal/formatter/CodeFormatter.java | 1027 ++++++++++---------- 4 files changed, 579 insertions(+), 533 deletions(-) diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/PHPFormatterTest.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/PHPFormatterTest.java index b9e7618..90ab589 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/PHPFormatterTest.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/PHPFormatterTest.java @@ -3,29 +3,27 @@ * */ package net.sourceforge.phpeclipse.phpeditor.php.test; - import java.util.Map; - import junit.framework.TestCase; import net.sourceforge.phpdt.core.JavaCore; import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility; import net.sourceforge.phpdt.internal.corext.util.Strings; import net.sourceforge.phpdt.internal.formatter.CodeFormatter; import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage; - import org.eclipse.jface.text.IDocument; - /** * Testcase for the PHP CodeFormatter + * * @author Stefan Langer - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ */ public class PHPFormatterTest extends TestCase { private CodeFormatter fFormatter; private IDocument fDocument; private String fInitialIndentation; - - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { @@ -33,42 +31,73 @@ public class PHPFormatterTest extends TestCase { fFormatter = new CodeFormatter(options); fDocument = new DummyDocument(); } - + public void testFormatter0() { + System.out.println("----- testFormatter0 -----"); + String text = ""; + fDocument.set(text); + String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument); + int indent = 0; + if (fInitialIndentation != null) { + indent = Strings.computeIndent(fInitialIndentation, + CodeFormatterPreferencePage.getTabSize()); + } + String formatedString = fFormatter + .format(text, indent, null, lineDelimiter); + String testResult = ""; + junit.framework.Assert.assertEquals(formatedString, testResult); + } public void testFormatter1() { System.out.println("----- testFormatter1 -----"); - String text = ""; fDocument.set(text); - String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument); - int indent = 0; if (fInitialIndentation != null) { - indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize()); + indent = Strings.computeIndent(fInitialIndentation, + CodeFormatterPreferencePage.getTabSize()); } - String formatedString = fFormatter.format(text, indent, null, lineDelimiter); - - String testResult = ""; - + String formatedString = fFormatter + .format(text, indent, null, lineDelimiter); + String testResult = ""; junit.framework.Assert.assertEquals(formatedString, testResult); } - public void testFormatter2() { // bug 741752 System.out.println("----- testFormatter2 -----"); - String text = ""; + String text = ""; + fDocument.set(text); + String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument); + int indent = 0; + if (fInitialIndentation != null) { + indent = Strings.computeIndent(fInitialIndentation, + CodeFormatterPreferencePage.getTabSize()); + } + String formatedString = fFormatter + .format(text, indent, null, lineDelimiter); + String testResult = ""; + junit.framework.Assert.assertEquals(formatedString, testResult); + } + public void testFormatter3() { + System.out.println("----- testFormatter1 -----"); + String text = ""; fDocument.set(text); - String lineDelimiter = StubUtility.getLineDelimiterFor(fDocument); - int indent = 0; if (fInitialIndentation != null) { - indent = Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize()); + indent = Strings.computeIndent(fInitialIndentation, + CodeFormatterPreferencePage.getTabSize()); } - String formatedString = fFormatter.format(text, indent, null, lineDelimiter); - - String testResult = ""; - + String formatedString = fFormatter + .format(text, indent, null, lineDelimiter); + String testResult = ""; junit.framework.Assert.assertEquals(formatedString, testResult); } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java index 5ff03b1..10a8ea8 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java @@ -1241,6 +1241,7 @@ public class Scanner implements IScanner, ITerminalSymbols { } } // boolean isWhiteSpace; + while ((currentCharacter == ' ') || Character.isWhitespace(currentCharacter)) { startPosition = currentPosition; @@ -3590,6 +3591,8 @@ public class Scanner implements IScanner, ITerminalSymbols { return "endswitch"; //$NON-NLS-1$ case TokenNameendwhile : return "endwhile"; //$NON-NLS-1$ + case TokenNameexit: + return "exit"; case TokenNameextends : return "extends"; //$NON-NLS-1$ // case TokenNamefalse : diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/Template.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/Template.java index 2ae617c..8c64cee 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/Template.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/Template.java @@ -113,6 +113,9 @@ public class Template { * Returns the name of the template. */ public String getName() { + if (fName==null) { + return ""; + } return fName; } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java index e48c07c..7938516 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java @@ -132,6 +132,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { , true /* whitespace */ , false /* nls */ , false /* assert */ + , true /* tokenizeStrings */ ); // regular scanner for forming lines scanner.recordLineSeparator = true; // to remind of the position of the beginning of the line. @@ -139,6 +140,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { , true /* whitespace */ , false /* nls */ , false /* assert */ + , true /* tokenizeStrings */ ); // secondary scanner to split long lines formed by primary scanning // initialize current line buffer @@ -393,9 +395,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // if (token == Scanner.TokenNamethrows) { // inThrowsClause = true; // } - if ((token == Scanner.TokenNameclass // || token == - // Scanner.TokenNameinterface - ) && previousToken != Scanner.TokenNameDOT) { + if ((token == Scanner.TokenNameclass || token == Scanner.TokenNameinterface) + && previousToken != Scanner.TokenNameDOT) { inClassOrInterfaceHeader = true; } /* @@ -499,9 +500,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // newlineInCompoundStatement is true. boolean nlicsOption = previousToken == TokenNameRBRACE && !options.newlineInControlStatementMode - && (token == TokenNameelse || (token == TokenNamewhile && nlicsToken == TokenNamedo)); - // || token == TokenNamecatch - // || token == TokenNamefinally); + && (token == TokenNameelse + || (token == TokenNamewhile && nlicsToken == TokenNamedo) + || token == TokenNamecatch || token == TokenNamefinally); // Do not add a newline & indent between a close brace and // semi-colon. boolean semiColonAndCloseBrace = previousToken == TokenNameRBRACE @@ -581,6 +582,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { nlicsToken = 0; } } + boolean phpTagAndWhitespace = + previousToken == TokenNameINLINE_HTML && token == TokenNameWHITESPACE; switch (token) { // case TokenNameDOLLAR : // dollarBraceCount++; @@ -622,16 +625,16 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { expectingOpenBrace = true; indentationLevel += pushControlStatement(token); break; - // case TokenNametry : - // pendingNewlineAfterParen = true; - // case TokenNamecatch : - // // several CATCH statements can be contiguous. - // // a CATCH is encountered pop until first CATCH (if a CATCH - // follows a TRY it works the same way, - // // as CATCH and TRY are the same token in the stack). - // expectingOpenBrace = true; - // indentationLevel += pushControlStatement(TokenNamecatch); - // break; + case TokenNametry : + pendingNewlineAfterParen = true; + case TokenNamecatch : + // several CATCH statements can be contiguous. + // a CATCH is encountered pop until first CATCH (if a CATCH + // follows a TRY it works the same way, + // as CATCH and TRY are the same token in the stack). + expectingOpenBrace = true; + indentationLevel += pushControlStatement(TokenNamecatch); + break; case TokenNamedo : expectingOpenBrace = true; indentationLevel += pushControlStatement(token); @@ -654,9 +657,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { && previousToken != TokenNameTWIDDLE && previousToken != TokenNameSEMICOLON && previousToken != TokenNameLBRACE - && previousToken != TokenNameRBRACE) { - // && previousToken != TokenNamesuper - // && previousToken != TokenNamethis) { + && previousToken != TokenNameRBRACE + && previousToken != TokenNamesuper) { + // && previousToken != TokenNamethis) { space(); } // If in a for/if/while statement, increase the parenthesis count @@ -690,7 +693,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameLBRACE : if (previousCompilableToken == TokenNameDOLLAR) { dollarBraceCount++; - } + } else { if ((previousCompilableToken == TokenNameRBRACKET) || (previousCompilableToken == TokenNameEQUAL)) { // if (previousCompilableToken == TokenNameRBRACKET) { @@ -701,8 +704,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { indentationLevel += pushBlock(); } else { // Add new line and increase indentation level after open brace. - pendingNewLines = 1; - indentationLevel += pushBlock(); + pendingNewLines = 1; + indentationLevel += pushBlock(); + } } break; case TokenNameRBRACE : @@ -734,9 +738,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameswitch : case TokenNameif : case TokenNameelse : - // case TokenNametry : - // case TokenNamecatch : - // case TokenNamefinally : + case TokenNametry : + case TokenNamecatch : + case TokenNamefinally : case TokenNamewhile : case TokenNamedo : // case TokenNamesynchronized : @@ -823,27 +827,29 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingNewLines = 1; break; case Scanner.TokenNameWHITESPACE : - // Count the number of line terminators in the whitespace so - // line spacing can be preserved near comments. - char[] source = scanner.source; - newLinesInWhitespace = 0; - for (int i = scanner.startPosition, max = scanner.currentPosition; i < max; i++) { - if (source[i] == '\r') { - if (i < max - 1) { - if (source[++i] == '\n') { - newLinesInWhitespace++; + if (!phpTagAndWhitespace) { + // Count the number of line terminators in the whitespace so + // line spacing can be preserved near comments. + char[] source = scanner.source; + newLinesInWhitespace = 0; + for (int i = scanner.startPosition, max = scanner.currentPosition; i < max; i++) { + if (source[i] == '\r') { + if (i < max - 1) { + if (source[++i] == '\n') { + newLinesInWhitespace++; + } else { + newLinesInWhitespace++; + } } else { newLinesInWhitespace++; } - } else { + } else if (source[i] == '\n') { newLinesInWhitespace++; } - } else if (source[i] == '\n') { - newLinesInWhitespace++; } + increaseLineDelta(scanner.startPosition - scanner.currentPosition); + break; } - increaseLineDelta(scanner.startPosition - scanner.currentPosition); - break; // case TokenNameHTML : // // Add the next token to the formatted source string. // // outputCurrentToken(token); @@ -857,8 +863,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // } // break; default : - if ((token == TokenNameIdentifier) || isLiteralToken(token)) { - // || token == TokenNamesuper + if ((token == TokenNameIdentifier) || isLiteralToken(token) + || token == TokenNamesuper) { // || token == TokenNamethis) { // Do not put a space between a unary operator // (eg: ++, --, +, -) and the identifier being modified. @@ -874,7 +880,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { break; } // Do not output whitespace tokens. - if (token != Scanner.TokenNameWHITESPACE) { + if (token != Scanner.TokenNameWHITESPACE || phpTagAndWhitespace) { /* * Add pending space to the formatted source string. Do not output a * space under the following circumstances: 1) this is the first pass 2) @@ -888,6 +894,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (token == TokenNameMINUS_GREATER && options.compactDereferencingMode) pendingSpace = false; + boolean openAndCloseBrace = previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE; if (pendingSpace @@ -909,7 +916,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingSpace = true; } // Whitespace tokens do not need to be remembered. - if (token != Scanner.TokenNameWHITESPACE) { + if (token != Scanner.TokenNameWHITESPACE || phpTagAndWhitespace) { previousToken = token; if (token != Scanner.TokenNameCOMMENT_BLOCK && token != Scanner.TokenNameCOMMENT_LINE @@ -1235,7 +1242,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameDOT : case 0 : // no token + case TokenNameWHITESPACE : case TokenNameLBRACKET : + case TokenNameDOLLAR : case Scanner.TokenNameCOMMENT_LINE : return false; default : @@ -1597,7 +1606,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } formattedSource.append(operatorString); increaseSplitDelta(operatorString.length()); - if (insertSpaceAfter(operator) // && operator != TokenNameimplements + if (insertSpaceAfter(operator) + && operator != TokenNameimplements && operator != TokenNameextends) { // && operator != TokenNamethrows) { formattedSource.append(' '); @@ -1613,8 +1623,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // dumping it // indent before postfix operator // indent also when the line cannot be split - if (operator == TokenNameextends) { - // || operator == TokenNameimplements + if (operator == TokenNameextends + || operator == TokenNameimplements ) { // || operator == TokenNamethrows) { formattedSource.append(' '); increaseSplitDelta(1); @@ -1765,8 +1775,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } formattedSource.append(lastOperatorString); increaseSplitDelta(lastOperatorString.length()); - if (insertSpaceAfter(lastOperator) // && lastOperator != - // TokenNameimplements + if (insertSpaceAfter(lastOperator) && lastOperator != TokenNameimplements && lastOperator != TokenNameextends) { // && lastOperator != TokenNamethrows) { formattedSource.append(' '); @@ -1995,467 +2004,469 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (stringToSplit.indexOf("//$NON-NLS") != -1) { //$NON-NLS-1$ return null; } + // split doesn't work correct for PHP + return null; // local variables - int currentToken = 0; - int splitTokenType = 0; - int splitTokenDepth = Integer.MAX_VALUE; - int splitTokenPriority = Integer.MAX_VALUE; - int[] substringsStartPositions = new int[10]; - // contains the start position of substrings - int[] substringsEndPositions = new int[10]; - // contains the start position of substrings - int substringsCount = 1; // index in the substringsStartPosition array - int[] splitOperators = new int[10]; - // contains the start position of substrings - int splitOperatorsCount = 0; // index in the substringsStartPosition array - int[] openParenthesisPosition = new int[10]; - int openParenthesisPositionCount = 0; - int position = 0; - int lastOpenParenthesisPosition = -1; - // used to remember the position of the 1st open parenthesis - // needed for a pattern like: A.B(C); we want formatted like A.B( split C); - // setup the scanner with a new source - int lastCommentStartPosition = -1; - // to remember the start position of the last comment - int firstTokenOnLine = -1; - // to remember the first token of the line - int previousToken = -1; - // to remember the previous token. - splitScanner.setSource(stringToSplit.toCharArray()); - try { - // start the loop - while (true) { - // takes the next token - try { - if (currentToken != Scanner.TokenNameWHITESPACE) - previousToken = currentToken; - currentToken = splitScanner.getNextToken(); - if (Scanner.DEBUG) { - int currentEndPosition = splitScanner.getCurrentTokenEndPosition(); - int currentStartPosition = splitScanner - .getCurrentTokenStartPosition(); - System.out.print(currentStartPosition + "," + currentEndPosition - + ": "); - System.out.println(scanner.toStringAction(currentToken)); - } - } catch (InvalidInputException e) { - if (!handleInvalidToken(e)) - throw e; - currentToken = 0; - // this value is not modify when an exception is raised. - } - if (currentToken == TokenNameEOF) - break; - if (firstTokenOnLine == -1) { - firstTokenOnLine = currentToken; - } - switch (currentToken) { - case TokenNameRBRACE : - case TokenNameRPAREN : - if (openParenthesisPositionCount > 0) { - if (openParenthesisPositionCount == 1 - && lastOpenParenthesisPosition < openParenthesisPosition[0]) { - lastOpenParenthesisPosition = openParenthesisPosition[0]; - } else if ((splitTokenDepth == Integer.MAX_VALUE) - || (splitTokenDepth > openParenthesisPositionCount && openParenthesisPositionCount == 1)) { - splitTokenType = 0; - splitTokenDepth = openParenthesisPositionCount; - splitTokenPriority = Integer.MAX_VALUE; - substringsStartPositions[0] = 0; - // better token means the whole line until now is the first - // substring - substringsCount = 1; // resets the count of substrings - substringsEndPositions[0] = openParenthesisPosition[0]; - // substring ends on operator start - position = openParenthesisPosition[0]; - // the string mustn't be cut before the closing parenthesis but - // after the opening one. - splitOperatorsCount = 1; // resets the count of split operators - splitOperators[0] = 0; - } - openParenthesisPositionCount--; - } - break; - case TokenNameLBRACE : - case TokenNameLPAREN : - if (openParenthesisPositionCount == openParenthesisPosition.length) { - System - .arraycopy( - openParenthesisPosition, - 0, - (openParenthesisPosition = new int[openParenthesisPositionCount * 2]), - 0, openParenthesisPositionCount); - } - openParenthesisPosition[openParenthesisPositionCount++] = splitScanner.currentPosition; - if (currentToken == TokenNameLPAREN - && previousToken == TokenNameRPAREN) { - openParenthesisPosition[openParenthesisPositionCount - 1] = splitScanner.startPosition; - } - break; - case TokenNameSEMICOLON : - // ; - case TokenNameCOMMA : - // , - case TokenNameEQUAL : - // = - if (openParenthesisPositionCount < splitTokenDepth - || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) { - // the current token is better than the one we currently have - // (in level or in priority if same level) - // reset the substringsCount - splitTokenDepth = openParenthesisPositionCount; - splitTokenType = currentToken; - splitTokenPriority = getTokenPriority(currentToken); - substringsStartPositions[0] = 0; - // better token means the whole line until now is the first - // substring - if (separateFirstArgumentOn(firstTokenOnLine) - && openParenthesisPositionCount > 0) { - substringsCount = 2; // resets the count of substrings - substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1]; - substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1]; - substringsEndPositions[1] = splitScanner.startPosition; - splitOperatorsCount = 2; // resets the count of split operators - splitOperators[0] = 0; - splitOperators[1] = currentToken; - position = splitScanner.currentPosition; - // next substring will start from operator end - } else { - substringsCount = 1; // resets the count of substrings - substringsEndPositions[0] = splitScanner.startPosition; - // substring ends on operator start - position = splitScanner.currentPosition; - // next substring will start from operator end - splitOperatorsCount = 1; // resets the count of split operators - splitOperators[0] = currentToken; - } - } else { - if ((openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken)) - && splitTokenType != TokenNameEQUAL - && currentToken != TokenNameEQUAL) { - // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after - // split - // take only the 1st = into account. - // if another token with the same priority is found, - // push the start position of the substring and - // push the token into the stack. - // create a new array object if the current one is full. - if (substringsCount == substringsStartPositions.length) { - System - .arraycopy( - substringsStartPositions, - 0, - (substringsStartPositions = new int[substringsCount * 2]), - 0, substringsCount); - System.arraycopy(substringsEndPositions, 0, - (substringsEndPositions = new int[substringsCount * 2]), - 0, substringsCount); - } - if (splitOperatorsCount == splitOperators.length) { - System.arraycopy(splitOperators, 0, - (splitOperators = new int[splitOperatorsCount * 2]), 0, - splitOperatorsCount); - } - substringsStartPositions[substringsCount] = position; - substringsEndPositions[substringsCount++] = splitScanner.startPosition; - // substring ends on operator start - position = splitScanner.currentPosition; - // next substring will start from operator end - splitOperators[splitOperatorsCount++] = currentToken; - } - } - break; - case TokenNameCOLON : - // : (15.24) - // see 1FK7C5R, we only split on a colon, when it is associated - // with a question-mark. - // indeed it might appear also behind a case statement, and we do - // not to break at this point. - if ((splitOperatorsCount == 0) - || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) { - break; - } - case TokenNameextends : - // case TokenNameimplements : - // case TokenNamethrows : - case TokenNameDOT : - // . - case TokenNameMULTIPLY : - // * (15.16.1) - case TokenNameDIVIDE : - // / (15.16.2) - case TokenNameREMAINDER : - // % (15.16.3) - case TokenNamePLUS : - // + (15.17, 15.17.2) - case TokenNameMINUS : - // - (15.17.2) - case TokenNameLEFT_SHIFT : - // << (15.18) - case TokenNameRIGHT_SHIFT : - // >> (15.18) - // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18) - case TokenNameLESS : - // < (15.19.1) - case TokenNameLESS_EQUAL : - // <= (15.19.1) - case TokenNameGREATER : - // > (15.19.1) - case TokenNameGREATER_EQUAL : - // >= (15.19.1) - // case TokenNameinstanceof : // instanceof - case TokenNameEQUAL_EQUAL : - // == (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameEQUAL_EQUAL_EQUAL : - // == (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameNOT_EQUAL : - // != (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameNOT_EQUAL_EQUAL : - // != (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameAND : - // & (15.21, 15.21.1, 15.21.2) - case TokenNameOR : - // | (15.21, 15.21.1, 15.21.2) - case TokenNameXOR : - // ^ (15.21, 15.21.1, 15.21.2) - case TokenNameAND_AND : - // && (15.22) - case TokenNameOR_OR : - // || (15.23) - case TokenNameQUESTION : - // ? (15.24) - case TokenNameMULTIPLY_EQUAL : - // *= (15.25.2) - case TokenNameDIVIDE_EQUAL : - // /= (15.25.2) - case TokenNameREMAINDER_EQUAL : - // %= (15.25.2) - case TokenNamePLUS_EQUAL : - // += (15.25.2) - case TokenNameMINUS_EQUAL : - // -= (15.25.2) - case TokenNameLEFT_SHIFT_EQUAL : - // <<= (15.25.2) - case TokenNameRIGHT_SHIFT_EQUAL : - // >>= (15.25.2) - // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2) - case TokenNameAND_EQUAL : - // &= (15.25.2) - case TokenNameXOR_EQUAL : - // ^= (15.25.2) - case TokenNameOR_EQUAL : - // |= (15.25.2) - if ((openParenthesisPositionCount < splitTokenDepth || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) - && !((currentToken == TokenNamePLUS || currentToken == TokenNameMINUS) && (previousToken == TokenNameLBRACE - || previousToken == TokenNameLBRACKET || splitScanner.startPosition == 0))) { - // the current token is better than the one we currently have - // (in level or in priority if same level) - // reset the substringsCount - splitTokenDepth = openParenthesisPositionCount; - splitTokenType = currentToken; - splitTokenPriority = getTokenPriority(currentToken); - substringsStartPositions[0] = 0; - // better token means the whole line until now is the first - // substring - if (separateFirstArgumentOn(firstTokenOnLine) - && openParenthesisPositionCount > 0) { - substringsCount = 2; // resets the count of substrings - substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1]; - substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1]; - substringsEndPositions[1] = splitScanner.startPosition; - splitOperatorsCount = 3; // resets the count of split operators - splitOperators[0] = 0; - splitOperators[1] = 0; - splitOperators[2] = currentToken; - position = splitScanner.currentPosition; - // next substring will start from operator end - } else { - substringsCount = 1; // resets the count of substrings - substringsEndPositions[0] = splitScanner.startPosition; - // substring ends on operator start - position = splitScanner.currentPosition; - // next substring will start from operator end - splitOperatorsCount = 2; // resets the count of split operators - splitOperators[0] = 0; - // nothing for first operand since operator will be inserted in - // front of the second operand - splitOperators[1] = currentToken; - } - } else { - if (openParenthesisPositionCount == splitTokenDepth - && splitTokenPriority == getTokenPriority(currentToken)) { - // if another token with the same priority is found, - // push the start position of the substring and - // push the token into the stack. - // create a new array object if the current one is full. - if (substringsCount == substringsStartPositions.length) { - System - .arraycopy( - substringsStartPositions, - 0, - (substringsStartPositions = new int[substringsCount * 2]), - 0, substringsCount); - System.arraycopy(substringsEndPositions, 0, - (substringsEndPositions = new int[substringsCount * 2]), - 0, substringsCount); - } - if (splitOperatorsCount == splitOperators.length) { - System.arraycopy(splitOperators, 0, - (splitOperators = new int[splitOperatorsCount * 2]), 0, - splitOperatorsCount); - } - substringsStartPositions[substringsCount] = position; - substringsEndPositions[substringsCount++] = splitScanner.startPosition; - // substring ends on operator start - position = splitScanner.currentPosition; - // next substring will start from operator end - splitOperators[splitOperatorsCount++] = currentToken; - } - } - default : - break; - } - if (isComment(currentToken)) { - lastCommentStartPosition = splitScanner.startPosition; - } else { - lastCommentStartPosition = -1; - } - } - } catch (InvalidInputException e) { - return null; - } - // if the string cannot be split, return null. - if (splitOperatorsCount == 0) - return null; - // ## SPECIAL CASES BEGIN - if (((splitOperatorsCount == 2 && splitOperators[1] == TokenNameDOT - && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1) - || (splitOperatorsCount > 2 && splitOperators[1] == TokenNameDOT - && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1 && lastOpenParenthesisPosition <= options.maxLineLength) || (separateFirstArgumentOn(firstTokenOnLine) - && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1)) - && (lastOpenParenthesisPosition < splitScanner.source.length && splitScanner.source[lastOpenParenthesisPosition] != ')')) { - // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis should - // not be broken on two lines - // only one split on a top level . - // or more than one split on . and substring before open parenthesis fits - // one line. - // or split inside parenthesis and first token is not a for/while/if - SplitLine sl = split( - stringToSplit.substring(lastOpenParenthesisPosition), - lastOpenParenthesisPosition); - if (sl == null || sl.operators[0] != TokenNameCOMMA) { - // trim() is used to remove the extra blanks at the end of the - // substring. See PR 1FGYPI1 - return new SplitLine(new int[]{0, 0}, new String[]{ - stringToSplit.substring(0, lastOpenParenthesisPosition).trim(), - stringToSplit.substring(lastOpenParenthesisPosition)}, new int[]{ - offsetInGlobalLine, - lastOpenParenthesisPosition + offsetInGlobalLine}); - } else { - // right substring can be split and is split on comma - // copy substrings and operators - // except if the 1st string is empty. - int startIndex = (sl.substrings[0].length() == 0) ? 1 : 0; - int subStringsLength = sl.substrings.length + 1 - startIndex; - String[] result = new String[subStringsLength]; - int[] startIndexes = new int[subStringsLength]; - int operatorsLength = sl.operators.length + 1 - startIndex; - int[] operators = new int[operatorsLength]; - result[0] = stringToSplit.substring(0, lastOpenParenthesisPosition); - operators[0] = 0; - System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes, - 1, subStringsLength - 1); - for (int i = subStringsLength - 1; i >= 0; i--) { - startIndexes[i] += offsetInGlobalLine; - } - System.arraycopy(sl.substrings, startIndex, result, 1, - subStringsLength - 1); - System.arraycopy(sl.operators, startIndex, operators, 1, - operatorsLength - 1); - return new SplitLine(operators, result, startIndexes); - } - } - // if the last token is a comment and the substring before the comment fits - // on a line, - // split before the comment and return the result. - if (lastCommentStartPosition > -1 - && lastCommentStartPosition < options.maxLineLength - && splitTokenPriority > 50) { - int end = lastCommentStartPosition; - int start = lastCommentStartPosition; - if (stringToSplit.charAt(end - 1) == ' ') { - end--; - } - if (start != end && stringToSplit.charAt(start) == ' ') { - start++; - } - return new SplitLine(new int[]{0, 0}, new String[]{ - stringToSplit.substring(0, end), stringToSplit.substring(start)}, - new int[]{0, start}); - } - if (position != stringToSplit.length()) { - if (substringsCount == substringsStartPositions.length) { - System.arraycopy(substringsStartPositions, 0, - (substringsStartPositions = new int[substringsCount * 2]), 0, - substringsCount); - System.arraycopy(substringsEndPositions, 0, - (substringsEndPositions = new int[substringsCount * 2]), 0, - substringsCount); - } - // avoid empty extra substring, e.g. line terminated with a semi-colon - substringsStartPositions[substringsCount] = position; - substringsEndPositions[substringsCount++] = stringToSplit.length(); - } - if (splitOperatorsCount == splitOperators.length) { - System.arraycopy(splitOperators, 0, - (splitOperators = new int[splitOperatorsCount * 2]), 0, - splitOperatorsCount); - } - splitOperators[splitOperatorsCount] = 0; - // the last element of the stack is the position of the end of - // StringToSPlit - // +1 because the substring method excludes the last character - String[] result = new String[substringsCount]; - for (int i = 0; i < substringsCount; i++) { - int start = substringsStartPositions[i]; - int end = substringsEndPositions[i]; - if (stringToSplit.charAt(start) == ' ') { - start++; - substringsStartPositions[i]++; - } - if (end != start && stringToSplit.charAt(end - 1) == ' ') { - end--; - } - result[i] = stringToSplit.substring(start, end); - substringsStartPositions[i] += offsetInGlobalLine; - } - if (splitOperatorsCount > substringsCount) { - System.arraycopy(substringsStartPositions, 0, - (substringsStartPositions = new int[splitOperatorsCount]), 0, - substringsCount); - System.arraycopy(substringsEndPositions, 0, - (substringsEndPositions = new int[splitOperatorsCount]), 0, - substringsCount); - for (int i = substringsCount; i < splitOperatorsCount; i++) { - substringsStartPositions[i] = position; - substringsEndPositions[i] = position; - } - System.arraycopy(splitOperators, 0, - (splitOperators = new int[splitOperatorsCount]), 0, - splitOperatorsCount); - } else { - System.arraycopy(substringsStartPositions, 0, - (substringsStartPositions = new int[substringsCount]), 0, - substringsCount); - System.arraycopy(substringsEndPositions, 0, - (substringsEndPositions = new int[substringsCount]), 0, - substringsCount); - System.arraycopy(splitOperators, 0, - (splitOperators = new int[substringsCount]), 0, substringsCount); - } - SplitLine splitLine = new SplitLine(splitOperators, result, - substringsStartPositions); - return splitLine; +// int currentToken = 0; +// int splitTokenType = 0; +// int splitTokenDepth = Integer.MAX_VALUE; +// int splitTokenPriority = Integer.MAX_VALUE; +// int[] substringsStartPositions = new int[10]; +// // contains the start position of substrings +// int[] substringsEndPositions = new int[10]; +// // contains the start position of substrings +// int substringsCount = 1; // index in the substringsStartPosition array +// int[] splitOperators = new int[10]; +// // contains the start position of substrings +// int splitOperatorsCount = 0; // index in the substringsStartPosition array +// int[] openParenthesisPosition = new int[10]; +// int openParenthesisPositionCount = 0; +// int position = 0; +// int lastOpenParenthesisPosition = -1; +// // used to remember the position of the 1st open parenthesis +// // needed for a pattern like: A.B(C); we want formatted like A.B( split C); +// // setup the scanner with a new source +// int lastCommentStartPosition = -1; +// // to remember the start position of the last comment +// int firstTokenOnLine = -1; +// // to remember the first token of the line +// int previousToken = -1; +// // to remember the previous token. +// splitScanner.setSource(stringToSplit.toCharArray()); +// try { +// // start the loop +// while (true) { +// // takes the next token +// try { +// if (currentToken != Scanner.TokenNameWHITESPACE) +// previousToken = currentToken; +// currentToken = splitScanner.getNextToken(); +// if (Scanner.DEBUG) { +// int currentEndPosition = splitScanner.getCurrentTokenEndPosition(); +// int currentStartPosition = splitScanner +// .getCurrentTokenStartPosition(); +// System.out.print(currentStartPosition + "," + currentEndPosition +// + ": "); +// System.out.println(scanner.toStringAction(currentToken)); +// } +// } catch (InvalidInputException e) { +// if (!handleInvalidToken(e)) +// throw e; +// currentToken = 0; +// // this value is not modify when an exception is raised. +// } +// if (currentToken == TokenNameEOF) +// break; +// if (firstTokenOnLine == -1) { +// firstTokenOnLine = currentToken; +// } +// switch (currentToken) { +// case TokenNameRBRACE : +// case TokenNameRPAREN : +// if (openParenthesisPositionCount > 0) { +// if (openParenthesisPositionCount == 1 +// && lastOpenParenthesisPosition < openParenthesisPosition[0]) { +// lastOpenParenthesisPosition = openParenthesisPosition[0]; +// } else if ((splitTokenDepth == Integer.MAX_VALUE) +// || (splitTokenDepth > openParenthesisPositionCount && openParenthesisPositionCount == 1)) { +// splitTokenType = 0; +// splitTokenDepth = openParenthesisPositionCount; +// splitTokenPriority = Integer.MAX_VALUE; +// substringsStartPositions[0] = 0; +// // better token means the whole line until now is the first +// // substring +// substringsCount = 1; // resets the count of substrings +// substringsEndPositions[0] = openParenthesisPosition[0]; +// // substring ends on operator start +// position = openParenthesisPosition[0]; +// // the string mustn't be cut before the closing parenthesis but +// // after the opening one. +// splitOperatorsCount = 1; // resets the count of split operators +// splitOperators[0] = 0; +// } +// openParenthesisPositionCount--; +// } +// break; +// case TokenNameLBRACE : +// case TokenNameLPAREN : +// if (openParenthesisPositionCount == openParenthesisPosition.length) { +// System +// .arraycopy( +// openParenthesisPosition, +// 0, +// (openParenthesisPosition = new int[openParenthesisPositionCount * 2]), +// 0, openParenthesisPositionCount); +// } +// openParenthesisPosition[openParenthesisPositionCount++] = splitScanner.currentPosition; +// if (currentToken == TokenNameLPAREN +// && previousToken == TokenNameRPAREN) { +// openParenthesisPosition[openParenthesisPositionCount - 1] = splitScanner.startPosition; +// } +// break; +// case TokenNameSEMICOLON : +// // ; +// case TokenNameCOMMA : +// // , +// case TokenNameEQUAL : +// // = +// if (openParenthesisPositionCount < splitTokenDepth +// || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) { +// // the current token is better than the one we currently have +// // (in level or in priority if same level) +// // reset the substringsCount +// splitTokenDepth = openParenthesisPositionCount; +// splitTokenType = currentToken; +// splitTokenPriority = getTokenPriority(currentToken); +// substringsStartPositions[0] = 0; +// // better token means the whole line until now is the first +// // substring +// if (separateFirstArgumentOn(firstTokenOnLine) +// && openParenthesisPositionCount > 0) { +// substringsCount = 2; // resets the count of substrings +// substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1]; +// substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1]; +// substringsEndPositions[1] = splitScanner.startPosition; +// splitOperatorsCount = 2; // resets the count of split operators +// splitOperators[0] = 0; +// splitOperators[1] = currentToken; +// position = splitScanner.currentPosition; +// // next substring will start from operator end +// } else { +// substringsCount = 1; // resets the count of substrings +// substringsEndPositions[0] = splitScanner.startPosition; +// // substring ends on operator start +// position = splitScanner.currentPosition; +// // next substring will start from operator end +// splitOperatorsCount = 1; // resets the count of split operators +// splitOperators[0] = currentToken; +// } +// } else { +// if ((openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken)) +// && splitTokenType != TokenNameEQUAL +// && currentToken != TokenNameEQUAL) { +// // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after +// // split +// // take only the 1st = into account. +// // if another token with the same priority is found, +// // push the start position of the substring and +// // push the token into the stack. +// // create a new array object if the current one is full. +// if (substringsCount == substringsStartPositions.length) { +// System +// .arraycopy( +// substringsStartPositions, +// 0, +// (substringsStartPositions = new int[substringsCount * 2]), +// 0, substringsCount); +// System.arraycopy(substringsEndPositions, 0, +// (substringsEndPositions = new int[substringsCount * 2]), +// 0, substringsCount); +// } +// if (splitOperatorsCount == splitOperators.length) { +// System.arraycopy(splitOperators, 0, +// (splitOperators = new int[splitOperatorsCount * 2]), 0, +// splitOperatorsCount); +// } +// substringsStartPositions[substringsCount] = position; +// substringsEndPositions[substringsCount++] = splitScanner.startPosition; +// // substring ends on operator start +// position = splitScanner.currentPosition; +// // next substring will start from operator end +// splitOperators[splitOperatorsCount++] = currentToken; +// } +// } +// break; +// case TokenNameCOLON : +// // : (15.24) +// // see 1FK7C5R, we only split on a colon, when it is associated +// // with a question-mark. +// // indeed it might appear also behind a case statement, and we do +// // not to break at this point. +// if ((splitOperatorsCount == 0) +// || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) { +// break; +// } +// case TokenNameextends : +// case TokenNameimplements : +// //case TokenNamethrows : +// case TokenNameDOT : +// // . +// case TokenNameMULTIPLY : +// // * (15.16.1) +// case TokenNameDIVIDE : +// // / (15.16.2) +// case TokenNameREMAINDER : +// // % (15.16.3) +// case TokenNamePLUS : +// // + (15.17, 15.17.2) +// case TokenNameMINUS : +// // - (15.17.2) +// case TokenNameLEFT_SHIFT : +// // << (15.18) +// case TokenNameRIGHT_SHIFT : +// // >> (15.18) +// // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18) +// case TokenNameLESS : +// // < (15.19.1) +// case TokenNameLESS_EQUAL : +// // <= (15.19.1) +// case TokenNameGREATER : +// // > (15.19.1) +// case TokenNameGREATER_EQUAL : +// // >= (15.19.1) +// // case TokenNameinstanceof : // instanceof +// case TokenNameEQUAL_EQUAL : +// // == (15.20, 15.20.1, 15.20.2, 15.20.3) +// case TokenNameEQUAL_EQUAL_EQUAL : +// // == (15.20, 15.20.1, 15.20.2, 15.20.3) +// case TokenNameNOT_EQUAL : +// // != (15.20, 15.20.1, 15.20.2, 15.20.3) +// case TokenNameNOT_EQUAL_EQUAL : +// // != (15.20, 15.20.1, 15.20.2, 15.20.3) +// case TokenNameAND : +// // & (15.21, 15.21.1, 15.21.2) +// case TokenNameOR : +// // | (15.21, 15.21.1, 15.21.2) +// case TokenNameXOR : +// // ^ (15.21, 15.21.1, 15.21.2) +// case TokenNameAND_AND : +// // && (15.22) +// case TokenNameOR_OR : +// // || (15.23) +// case TokenNameQUESTION : +// // ? (15.24) +// case TokenNameMULTIPLY_EQUAL : +// // *= (15.25.2) +// case TokenNameDIVIDE_EQUAL : +// // /= (15.25.2) +// case TokenNameREMAINDER_EQUAL : +// // %= (15.25.2) +// case TokenNamePLUS_EQUAL : +// // += (15.25.2) +// case TokenNameMINUS_EQUAL : +// // -= (15.25.2) +// case TokenNameLEFT_SHIFT_EQUAL : +// // <<= (15.25.2) +// case TokenNameRIGHT_SHIFT_EQUAL : +// // >>= (15.25.2) +// // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2) +// case TokenNameAND_EQUAL : +// // &= (15.25.2) +// case TokenNameXOR_EQUAL : +// // ^= (15.25.2) +// case TokenNameOR_EQUAL : +// // |= (15.25.2) +// if ((openParenthesisPositionCount < splitTokenDepth || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) +// && !((currentToken == TokenNamePLUS || currentToken == TokenNameMINUS) && (previousToken == TokenNameLBRACE +// || previousToken == TokenNameLBRACKET || splitScanner.startPosition == 0))) { +// // the current token is better than the one we currently have +// // (in level or in priority if same level) +// // reset the substringsCount +// splitTokenDepth = openParenthesisPositionCount; +// splitTokenType = currentToken; +// splitTokenPriority = getTokenPriority(currentToken); +// substringsStartPositions[0] = 0; +// // better token means the whole line until now is the first +// // substring +// if (separateFirstArgumentOn(firstTokenOnLine) +// && openParenthesisPositionCount > 0) { +// substringsCount = 2; // resets the count of substrings +// substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1]; +// substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1]; +// substringsEndPositions[1] = splitScanner.startPosition; +// splitOperatorsCount = 3; // resets the count of split operators +// splitOperators[0] = 0; +// splitOperators[1] = 0; +// splitOperators[2] = currentToken; +// position = splitScanner.currentPosition; +// // next substring will start from operator end +// } else { +// substringsCount = 1; // resets the count of substrings +// substringsEndPositions[0] = splitScanner.startPosition; +// // substring ends on operator start +// position = splitScanner.currentPosition; +// // next substring will start from operator end +// splitOperatorsCount = 2; // resets the count of split operators +// splitOperators[0] = 0; +// // nothing for first operand since operator will be inserted in +// // front of the second operand +// splitOperators[1] = currentToken; +// } +// } else { +// if (openParenthesisPositionCount == splitTokenDepth +// && splitTokenPriority == getTokenPriority(currentToken)) { +// // if another token with the same priority is found, +// // push the start position of the substring and +// // push the token into the stack. +// // create a new array object if the current one is full. +// if (substringsCount == substringsStartPositions.length) { +// System +// .arraycopy( +// substringsStartPositions, +// 0, +// (substringsStartPositions = new int[substringsCount * 2]), +// 0, substringsCount); +// System.arraycopy(substringsEndPositions, 0, +// (substringsEndPositions = new int[substringsCount * 2]), +// 0, substringsCount); +// } +// if (splitOperatorsCount == splitOperators.length) { +// System.arraycopy(splitOperators, 0, +// (splitOperators = new int[splitOperatorsCount * 2]), 0, +// splitOperatorsCount); +// } +// substringsStartPositions[substringsCount] = position; +// substringsEndPositions[substringsCount++] = splitScanner.startPosition; +// // substring ends on operator start +// position = splitScanner.currentPosition; +// // next substring will start from operator end +// splitOperators[splitOperatorsCount++] = currentToken; +// } +// } +// default : +// break; +// } +// if (isComment(currentToken)) { +// lastCommentStartPosition = splitScanner.startPosition; +// } else { +// lastCommentStartPosition = -1; +// } +// } +// } catch (InvalidInputException e) { +// return null; +// } +// // if the string cannot be split, return null. +// if (splitOperatorsCount == 0) +// return null; +// // ## SPECIAL CASES BEGIN +// if (((splitOperatorsCount == 2 && splitOperators[1] == TokenNameDOT +// && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1) +// || (splitOperatorsCount > 2 && splitOperators[1] == TokenNameDOT +// && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1 && lastOpenParenthesisPosition <= options.maxLineLength) || (separateFirstArgumentOn(firstTokenOnLine) +// && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1)) +// && (lastOpenParenthesisPosition < splitScanner.source.length && splitScanner.source[lastOpenParenthesisPosition] != ')')) { +// // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis should +// // not be broken on two lines +// // only one split on a top level . +// // or more than one split on . and substring before open parenthesis fits +// // one line. +// // or split inside parenthesis and first token is not a for/while/if +// SplitLine sl = split( +// stringToSplit.substring(lastOpenParenthesisPosition), +// lastOpenParenthesisPosition); +// if (sl == null || sl.operators[0] != TokenNameCOMMA) { +// // trim() is used to remove the extra blanks at the end of the +// // substring. See PR 1FGYPI1 +// return new SplitLine(new int[]{0, 0}, new String[]{ +// stringToSplit.substring(0, lastOpenParenthesisPosition).trim(), +// stringToSplit.substring(lastOpenParenthesisPosition)}, new int[]{ +// offsetInGlobalLine, +// lastOpenParenthesisPosition + offsetInGlobalLine}); +// } else { +// // right substring can be split and is split on comma +// // copy substrings and operators +// // except if the 1st string is empty. +// int startIndex = (sl.substrings[0].length() == 0) ? 1 : 0; +// int subStringsLength = sl.substrings.length + 1 - startIndex; +// String[] result = new String[subStringsLength]; +// int[] startIndexes = new int[subStringsLength]; +// int operatorsLength = sl.operators.length + 1 - startIndex; +// int[] operators = new int[operatorsLength]; +// result[0] = stringToSplit.substring(0, lastOpenParenthesisPosition); +// operators[0] = 0; +// System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes, +// 1, subStringsLength - 1); +// for (int i = subStringsLength - 1; i >= 0; i--) { +// startIndexes[i] += offsetInGlobalLine; +// } +// System.arraycopy(sl.substrings, startIndex, result, 1, +// subStringsLength - 1); +// System.arraycopy(sl.operators, startIndex, operators, 1, +// operatorsLength - 1); +// return new SplitLine(operators, result, startIndexes); +// } +// } +// // if the last token is a comment and the substring before the comment fits +// // on a line, +// // split before the comment and return the result. +// if (lastCommentStartPosition > -1 +// && lastCommentStartPosition < options.maxLineLength +// && splitTokenPriority > 50) { +// int end = lastCommentStartPosition; +// int start = lastCommentStartPosition; +// if (stringToSplit.charAt(end - 1) == ' ') { +// end--; +// } +// if (start != end && stringToSplit.charAt(start) == ' ') { +// start++; +// } +// return new SplitLine(new int[]{0, 0}, new String[]{ +// stringToSplit.substring(0, end), stringToSplit.substring(start)}, +// new int[]{0, start}); +// } +// if (position != stringToSplit.length()) { +// if (substringsCount == substringsStartPositions.length) { +// System.arraycopy(substringsStartPositions, 0, +// (substringsStartPositions = new int[substringsCount * 2]), 0, +// substringsCount); +// System.arraycopy(substringsEndPositions, 0, +// (substringsEndPositions = new int[substringsCount * 2]), 0, +// substringsCount); +// } +// // avoid empty extra substring, e.g. line terminated with a semi-colon +// substringsStartPositions[substringsCount] = position; +// substringsEndPositions[substringsCount++] = stringToSplit.length(); +// } +// if (splitOperatorsCount == splitOperators.length) { +// System.arraycopy(splitOperators, 0, +// (splitOperators = new int[splitOperatorsCount * 2]), 0, +// splitOperatorsCount); +// } +// splitOperators[splitOperatorsCount] = 0; +// // the last element of the stack is the position of the end of +// // StringToSPlit +// // +1 because the substring method excludes the last character +// String[] result = new String[substringsCount]; +// for (int i = 0; i < substringsCount; i++) { +// int start = substringsStartPositions[i]; +// int end = substringsEndPositions[i]; +// if (stringToSplit.charAt(start) == ' ') { +// start++; +// substringsStartPositions[i]++; +// } +// if (end != start && stringToSplit.charAt(end - 1) == ' ') { +// end--; +// } +// result[i] = stringToSplit.substring(start, end); +// substringsStartPositions[i] += offsetInGlobalLine; +// } +// if (splitOperatorsCount > substringsCount) { +// System.arraycopy(substringsStartPositions, 0, +// (substringsStartPositions = new int[splitOperatorsCount]), 0, +// substringsCount); +// System.arraycopy(substringsEndPositions, 0, +// (substringsEndPositions = new int[splitOperatorsCount]), 0, +// substringsCount); +// for (int i = substringsCount; i < splitOperatorsCount; i++) { +// substringsStartPositions[i] = position; +// substringsEndPositions[i] = position; +// } +// System.arraycopy(splitOperators, 0, +// (splitOperators = new int[splitOperatorsCount]), 0, +// splitOperatorsCount); +// } else { +// System.arraycopy(substringsStartPositions, 0, +// (substringsStartPositions = new int[substringsCount]), 0, +// substringsCount); +// System.arraycopy(substringsEndPositions, 0, +// (substringsEndPositions = new int[substringsCount]), 0, +// substringsCount); +// System.arraycopy(splitOperators, 0, +// (splitOperators = new int[substringsCount]), 0, substringsCount); +// } +// SplitLine splitLine = new SplitLine(splitOperators, result, +// substringsStartPositions); +// return splitLine; } private void updateMappedPositions(int startPosition) { if (positionsToMap == null) { -- 1.7.1