X-Git-Url: http://secure.phpeclipse.com 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 b272fbd..f5d3ea3 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 @@ -1,10 +1,11 @@ /******************************************************************************* * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v0.5 + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v05.html - * + * * Contributors: * IBM Corporation - initial API and implementation ******************************************************************************/ @@ -23,15 +24,8 @@ import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; import net.sourceforge.phpdt.core.compiler.InvalidInputException; import net.sourceforge.phpdt.internal.compiler.ConfigurableOption; import net.sourceforge.phpdt.internal.compiler.parser.Scanner; -import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility; -import net.sourceforge.phpdt.internal.corext.util.Strings; import net.sourceforge.phpdt.internal.formatter.impl.FormatterOptions; import net.sourceforge.phpdt.internal.formatter.impl.SplitLine; -import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage; - -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.formatter.IContentFormatterExtension; -import org.eclipse.jface.text.formatter.IFormattingContext; /** *

How to format a piece of code ?

@@ -135,7 +129,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Creates a new instance of Code Formatter using the given settings. - * + * * @deprecated backport 1.0 internal functionality */ public CodeFormatter(ConfigurableOption[] settings) { @@ -144,7 +138,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Creates a new instance of Code Formatter using the FormattingOptions object given as argument - * + * * @deprecated Use CodeFormatter(ConfigurableOption[]) instead */ public CodeFormatter() { @@ -166,15 +160,17 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { , false /* nls */ , false /* assert */ , true, /* tokenizeStrings */ - null, null); // regular scanner for forming lines + null, null, true /*taskCaseSensitive*/); // regular scanner for forming lines scanner.recordLineSeparator = true; + scanner.ignorePHPOneLiner = true; // to remind of the position of the beginning of the line. splitScanner = new Scanner(true /* comment */ , true /* whitespace */ , false /* nls */ , false /* assert */ , true, /* tokenizeStrings */ - null, null); + null, null, true /*taskCaseSensitive*/); + splitScanner.ignorePHPOneLiner = true; // secondary scanner to split long lines formed by primary scanning // initialize current line buffer currentLineBuffer = new StringBuffer(); @@ -275,7 +271,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { outputLine(currentString, false, currentLineIndentationLevel, 0, -1, null, 0); } int scannerSourceLength = scanner.source.length; - if (scannerSourceLength > 2) { + if ((scannerSourceLength > 2) && (scanner.startPosition < scannerSourceLength)) { if (scanner.source[scannerSourceLength - 1] == '\n' && scanner.source[scannerSourceLength - 2] == '\r') { formattedSource.append(options.lineSeparatorSequence); increaseGlobalDelta(options.lineSeparatorSequence.length - 2); @@ -336,6 +332,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { currentLineIndentationLevel += constructionsCount; // An InvalidInputException exception might cause the termination of this // loop. + int arrayDeclarationCount=0; + int[] arrayDeclarationParenthesis=new int[10]; try { while (true) { // Get the next token. Catch invalid input and output it @@ -370,11 +368,23 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } token = 0; } - if (token == Scanner.TokenNameEOF) + if (token == Scanner.TokenNameEOF) { break; - if (token == Scanner.TokenNameHEREDOC || token == Scanner.TokenNameINLINE_HTML) { + } else if (token == Scanner.TokenNameHEREDOC) { // no indentation for heredocs and HTML ! - outputCurrentTokenWithoutIndent(Scanner.TokenNameHEREDOC); + outputCurrentTokenWithoutIndent(Scanner.TokenNameHEREDOC, 0); + continue; + } else if (token == Scanner.TokenNameINLINE_HTML) { + // no indentation for heredocs and HTML ! + int newLineCount = 1; + if (scanner.startPosition==0) { + newLineCount = 0; + } + outputCurrentTokenWithoutIndent(Scanner.TokenNameINLINE_HTML, newLineCount); + int srcLen = scanner.source.length; + if (scanner.currentPosition < srcLen-1) { + newLine(1); + } continue; } /* @@ -501,14 +511,24 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingSpace = false; } } + // don't linebreak empty array declarations + if (token == TokenNameRPAREN && arrayDeclarationCount > 0) { + if (previousCompilableToken == TokenNameLPAREN) { + pendingNewLines = 0; + } + } // Add pending new lines to the formatted source string. // Note: pending new lines are not added if the current token // is a single line comment or whitespace. // if the comment is between parenthesis, there is no blank line // preservation // (if it's a one-line comment, a blank line is added after it). - if (((pendingNewLines > 0 && (!isComment(token))) - || (newLinesInWhitespace > 0 && (openParenthesisCount <= 1 && isComment(token))) || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE)) + if (( + (pendingNewLines > 0 && (!isComment(token))) + || (newLinesInWhitespace > 0 && (openParenthesisCount <= 1 && isComment(token))) + || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE) + || (newLinesInWhitespace > 0 && previousCompilableToken == TokenNameDOT) + ) && token != Scanner.TokenNameWHITESPACE) { // Do not add newline & indent between an adjoining close brace and // close paren. Anonymous inner classes may use this form. @@ -667,9 +687,28 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { else openParenthesis[0]++; pendingSpace = false; + // recognize array declaration for nice output + if (previousCompilableToken == TokenNamearray) { + arrayDeclarationCount++; + arrayDeclarationParenthesis[arrayDeclarationCount]=openParenthesis[openParenthesisCount]; + indentationLevel++; + pendingNewLines=1; + } //S } break; case TokenNameRPAREN: + // check for closing array declaration + if (arrayDeclarationCount>0) { + if (arrayDeclarationParenthesis[arrayDeclarationCount]==openParenthesis[openParenthesisCount]) { + if (previousCompilableToken != TokenNameLPAREN) { + newLine(1); + } + indentationLevel--; + currentLineIndentationLevel = indentationLevel; + pendingNewLines = 0; + arrayDeclarationCount--; + } + } // Decrease the parenthesis count // if there is no more unclosed parenthesis, // a new line and indent may be append (depending on the next @@ -701,6 +740,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // Add new line and increase indentation level after open brace. pendingNewLines = 1; indentationLevel += pushBlock(); + inAssignment = false; } } break; @@ -758,7 +798,13 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingSpace = false; break; case TokenNameCOMMA: + pendingSpace = false; + if (arrayDeclarationCount>0) { + pendingNewLines=1; + } + break; case TokenNameDOT: + space(); pendingSpace = false; break; case TokenNameSEMICOLON: @@ -782,7 +828,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameMINUS_MINUS: // Do not put a space between a post-increment/decrement // and the identifier being modified. - if (previousToken == TokenNameIdentifier || previousToken == TokenNameRBRACKET) { + if (previousToken == TokenNameIdentifier || previousToken == TokenNameRBRACKET || previousToken == TokenNameVariable) { pendingSpace = false; } break; @@ -919,7 +965,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Formats the char array sourceString, and returns a string containing the formatted version. - * + * * @return the formatted ouput. */ public String formatSourceString(String sourceString) { @@ -932,7 +978,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Formats the char array sourceString, and returns a string containing the formatted version. - * + * * @param string * the string to format * @param indentationLevel @@ -946,7 +992,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Formats the char array sourceString, and returns a string containing the formatted version. The positions array * is modified to contain the mapped positions. - * + * * @param string * the string to format * @param indentationLevel @@ -979,7 +1025,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Formats the char array sourceString, and returns a string containing the formatted version. The initial * indentation level is 0. - * + * * @param string * the string to format * @return the formatted ouput. @@ -990,7 +1036,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Formats a given source string, starting indenting it at a particular depth and using the given options - * + * * @deprecated backport 1.0 internal functionality */ public static String format(String sourceString, int initialIndentationLevel, ConfigurableOption[] options) { @@ -1042,7 +1088,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Returns an array of descriptions for the configurable options. The descriptions may be changed and passed back to a different * compiler. - * + * * @deprecated backport 1.0 internal functionality */ public static ConfigurableOption[] getDefaultOptions(Locale locale) { @@ -1071,18 +1117,25 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Returns the array of mapped positions. Returns null is no positions have been set. - * + * * @return int[] * @deprecated There is no need to retrieve the mapped positions anymore. */ public int[] getMappedPositions() { + if (null!=mappedPositions) { + for (int i=0;i=formattedSource.length()) { + mappedPositions[i]=formattedSource.length()-1; + } + } + } return mappedPositions; } /** * Returns the priority of the token given as argument
* The most prioritary the token is, the smallest the return value is. - * + * * @return the priority of token * @param token * the token of which the priority is requested @@ -1220,7 +1273,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameLPAREN: case TokenNameNOT: case TokenNameTWIDDLE: - case TokenNameDOT: case 0: // no token case TokenNameWHITESPACE: @@ -1265,7 +1317,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * If the length of oneLineBuffer exceeds maxLineLength, it is split and the result is dumped in * formattedSource - * + * * @param newLineCount * the number of new lines to append */ @@ -1336,7 +1388,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { return "==="; //$NON-NLS-1$ case TokenNameEQUAL_GREATER: // -= (15.25.2) - return "=>"; //$NON-NLS-1$ + return "=>"; //$NON-NLS-1$ case TokenNameNOT_EQUAL: // != (15.20, 15.20.1, 15.20.2, 15.20.3) return "!="; //$NON-NLS-1$ @@ -1449,8 +1501,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } - private void outputCurrentTokenWithoutIndent(int token) { - newLine(0); + private void outputCurrentTokenWithoutIndent(int token, int newLineCount) { + newLine(newLineCount); formattedSource.append(scanner.source, scanner.startPosition, scanner.currentPosition - scanner.startPosition); } @@ -1565,7 +1617,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { *
  • If its length is < maxLineLength, output *
  • Otherwise it is split. * - * + * * @param currentString * string to output * @param preIndented @@ -1725,7 +1777,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { String currentResult = result[i]; if (currentResult.length() != 0 || splitOperators[i] != 0) { int newDepth = (currentResult.startsWith("/*") //$NON-NLS-1$ - || currentResult.startsWith("//")) //$NON-NLS-1$ + || currentResult.startsWith("//")) //$NON-NLS-1$ ? indentationLevel - 1 : depth; outputLine(currentResult, i == 0 || (i == 1 && emptyFirstSubString) ? preIndented : false, i == 0 ? newDepth : newDepth + 1, splitOperators[i], i, splitLine.startSubstringsIndexes, currentString @@ -1797,7 +1849,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Pops elements until the stack is empty or the top element is token.
    * Does not remove token from the stack. - * + * * @param token * the token to be left as the top of the stack */ @@ -1845,7 +1897,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Pops elements until the stack is empty or the top element is token.
    * Removes token from the stack too. - * + * * @param token * the token to remove from the stack */ @@ -1924,11 +1976,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Set the positions to map. The mapped positions should be retrieved using the getMappedPositions() method. - * + * * @param positions * int[] * @deprecated Set the positions to map using the format(String, int, int[]) method. - * + * * @see #getMappedPositions() */ public void setPositionsToMap(int[] positions) { @@ -1949,7 +2001,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Splits stringToSplit on the top level token
    * If there are several identical token at the same level, the string is cut into many pieces. - * + * * @return an object containing the operator and all the substrings or null if the string cannot be split */ public SplitLine split(String stringToSplit) { @@ -1959,7 +2011,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Splits stringToSplit on the top level token
    * If there are several identical token at the same level, the string is cut into many pieces. - * + * * @return an object containing the operator and all the substrings or null if the string cannot be split */ public SplitLine split(String stringToSplit, int offsetInGlobalLine) { @@ -2495,10 +2547,10 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { /** * Sets the initial indentation level - * + * * @param indentationLevel * new indentation level - * + * * @deprecated */ public void setInitialIndentationLevel(int newIndentationLevel) {