request 1152810, implementing formatting of array initializers
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / formatter / CodeFormatter.java
index fe61f5a..30e0110 100644 (file)
@@ -271,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);
@@ -332,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
@@ -675,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
@@ -766,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;
@@ -790,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;