request 1152810, implementing formatting of array initializers
authorbananeweizen <bananeweizen>
Wed, 21 Dec 2005 19:15:26 +0000 (19:15 +0000)
committerbananeweizen <bananeweizen>
Wed, 21 Dec 2005 19:15:26 +0000 (19:15 +0000)
net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/PHPFormatterTest.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java

index d8b9d32..c13f010 100644 (file)
@@ -19,85 +19,150 @@ import org.eclipse.jface.text.IDocument;
  * Testcase for the PHP CodeFormatter
  * 
  * @author Stefan Langer
- * @version $Revision: 1.5 $
+ * @version $Revision: 1.6 $
  */
 public class PHPFormatterTest extends TestCase {
-  private CodeFormatter fFormatter;
+       private CodeFormatter fFormatter;
 
-  private IDocument fDocument;
+       private IDocument fDocument;
 
-  private String fInitialIndentation;
+       private String fInitialIndentation;
 
-  /*
-   * (non-Javadoc)
-   * 
-   * @see junit.framework.TestCase#setUp()
-   */
-  protected void setUp() throws Exception {
-    Map options = JavaCore.getOptions();
-    fFormatter = new CodeFormatter(options);
-    fDocument = new DummyDocument();
-  }
+       /*
+        * (non-Javadoc)
+        * 
+        * @see junit.framework.TestCase#setUp()
+        */
+       protected void setUp() throws Exception {
+               Map options = JavaCore.getOptions();
+               fFormatter = new CodeFormatter(options);
+               fDocument = new DummyDocument();
+       }
 
-  public void testFormatter0() {
-    System.out.println("----- testFormatter0 -----");
-    String text = "<?php\nheader(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\"); exit; ?>";
-    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 = "<?php" + lineDelimiter
-        + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");" + lineDelimiter + "exit;"
-        + lineDelimiter + "?>";
-    junit.framework.Assert.assertEquals(formatedString, testResult);
-  }
+       public void testFormatter0() {
+               System.out.println("----- testFormatter0 -----");
+               String text = "<?php\nheader(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\"); exit; ?>";
+               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 = "<?php"
+                               + lineDelimiter
+                               + "header(\"Location: http://www.phpeclipse.de/eclipse/space/eclipse/faq/longlinetester\");"
+                               + lineDelimiter + "exit;" + lineDelimiter + "?>";
+               junit.framework.Assert.assertEquals(formatedString, testResult);
+       }
 
-  public void testFormatter1() {
-    System.out.println("----- testFormatter1 -----");
-    String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
-    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 = "<?php" + lineDelimiter + "function test() {" + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
-        + lineDelimiter + "?>";
-    junit.framework.Assert.assertEquals(formatedString, testResult);
-  }
+       public void testFormatter1() {
+               System.out.println("----- testFormatter1 -----");
+               String text = "<?php\n" + "function test()\n" + "{echo 'Test!';}\n"
+                               + "?>";
+               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 = "<?php" + lineDelimiter + "function test() {"
+                               + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
+                               + lineDelimiter + "?>";
+               junit.framework.Assert.assertEquals(formatedString, testResult);
+       }
 
-  public void testFormatter2() {
-    //  bug 741752
-    System.out.println("----- testFormatter2 -----");
-    String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n" + "?>";
-    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 = "<?php" + lineDelimiter + "if (${ $feldname } == $modellvar)" + lineDelimiter + "\t$i = 0;"
-        + lineDelimiter + "?>";
-    junit.framework.Assert.assertEquals(formatedString, testResult);
-  }
+       public void testFormatter2() {
+               // bug 741752
+               System.out.println("----- testFormatter2 -----");
+               String text = "<?php\n" + "if ( ${ $feldname }== $modellvar) $i=0;\n"
+                               + "?>";
+               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 = "<?php" + lineDelimiter
+                               + "if (${ $feldname } == $modellvar)" + lineDelimiter
+                               + "\t$i = 0;" + lineDelimiter + "?>";
+               junit.framework.Assert.assertEquals(formatedString, testResult);
+       }
 
-  public void testFormatter3() {
-    System.out.println("----- testFormatter1 -----");
-    String text = "<?php\n \r\n \r\n" + "function test()\n" + "{echo 'Test!';}\n" + "?>";
-    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 = "<?php" + lineDelimiter + "function test() {" + lineDelimiter
-        + "\techo 'Test!';" + lineDelimiter + "}" + lineDelimiter + "?>";
-    junit.framework.Assert.assertEquals(formatedString, testResult);
-  }
+       public void testFormatter3() {
+               System.out.println("----- testFormatter1 -----");
+               String text = "<?php\n \r\n \r\n" + "function test()\n"
+                               + "{echo 'Test!';}\n" + "?>";
+               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 = "<?php" + lineDelimiter + "function test() {"
+                               + lineDelimiter + "\techo 'Test!';" + lineDelimiter + "}"
+                               + lineDelimiter + "?>";
+               junit.framework.Assert.assertEquals(formatedString, testResult);
+       }
+
+       public void testArrayFormatting() throws Exception {
+               System.out.println("-----testArrayFormatting-----");
+               String text = "<?php\n"
+                               + "$my_array = array('first' => 'ok','snd' => 'right','3th' => 'super');\n"
+                               + "?>";
+               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 = "<?php" + lineDelimiter + "$my_array = array ("
+                               + lineDelimiter + "\t'first' => 'ok'," + lineDelimiter
+                               + "\t'snd' => 'right'," + lineDelimiter + "\t'3th' => 'super'"
+                               + lineDelimiter + ");" + lineDelimiter + "?>";
+               junit.framework.Assert.assertEquals(formatedString, testResult);
+       }
+       
+       public void testDoubleArrayFormatting() throws Exception {
+               System.out.println("-----testDoubleArrayFormatting-----");
+               String text = "<?php\n"
+                               + "$my_array = array('first' => 'ok', 'snd' => array ('1' => '11','2' => '22'),'3th' => 'super');\n"
+                               + "?>";
+               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 = "<?php" + lineDelimiter
+                       + "$my_array = array (" + lineDelimiter
+                       + "\t'first' => 'ok'," + lineDelimiter
+                       + "\t'snd' => array (" + lineDelimiter
+                       + "\t\t'1' => '11'," + lineDelimiter
+                       + "\t\t'2' => '22'" + lineDelimiter
+                       + "\t)," + lineDelimiter
+                       + "\t'3th' => 'super'" + lineDelimiter
+                       + ");" + lineDelimiter
+                       + "?>";
+               junit.framework.Assert.assertEquals(formatedString, testResult);
+       }
+       
+       
 }
\ No newline at end of file
index 5d8db07..30e0110 100644 (file)
@@ -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;