X-Git-Url: http://git.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 27e8037..30e0110 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 @@ -2,6 +2,7 @@ * 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 + * * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v05.html * @@ -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 ?

@@ -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,8 +368,25 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } token = 0; } - if (token == Scanner.TokenNameEOF) + if (token == Scanner.TokenNameEOF) { break; + } else if (token == Scanner.TokenNameHEREDOC) { + // no indentation for heredocs and HTML ! + 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; + } /* * ## MODIFYING the indentation level before generating new lines and indentation in the output string */ @@ -662,9 +677,26 @@ 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]) { + 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 @@ -753,6 +785,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingSpace = false; break; case TokenNameCOMMA: + pendingSpace = false; + if (arrayDeclarationCount>0) { + pendingNewLines=1; + } + break; case TokenNameDOT: pendingSpace = false; break; @@ -777,7 +814,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; @@ -1420,7 +1457,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // |= (15.25.2) return "|="; //$NON-NLS-1$ case TokenNameDOT_EQUAL: - // .= + // .= return ".="; //$NON-NLS-1$ case TokenNameDOT: // . @@ -1444,6 +1481,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } + private void outputCurrentTokenWithoutIndent(int token, int newLineCount) { + newLine(newLineCount); + formattedSource.append(scanner.source, scanner.startPosition, scanner.currentPosition - scanner.startPosition); + } + /** * Appends token to the formatted output.
* If it contains \n, append a LINE_SEPARATOR and indent after it. @@ -1908,7 +1950,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { //return (currentToken == TokenNameCOMMA || currentToken == // TokenNameSEMICOLON); return currentToken != TokenNameif && currentToken != TokenNameLPAREN && currentToken != TokenNameNOT - && currentToken != TokenNamewhile && currentToken != TokenNamefor && currentToken != TokenNameforeach + && currentToken != TokenNamewhile && currentToken != TokenNamefor && currentToken != TokenNameforeach && currentToken != TokenNameswitch; }