formatter tests and minor fixes
authoralexey.ivanov <alexey.ivanov@jetbrains.com>
Tue, 2 Nov 2010 19:13:15 +0000 (22:13 +0300)
committeralexey.ivanov <alexey.ivanov@jetbrains.com>
Tue, 2 Nov 2010 19:13:15 +0000 (22:13 +0300)
src/org/jetbrains/javafx/formatter/JavaFxFormattingBlock.java
src/org/jetbrains/javafx/formatter/JavaFxFormattingModelBuilder.java
src/org/jetbrains/javafx/formatter/JavaFxFormattingUtil.java [new file with mode: 0644]
testData/formatter/classIndent.fx [new file with mode: 0644]
testData/formatter/classIndent_after.fx [new file with mode: 0644]
testData/formatter/expressionsIndent.fx [new file with mode: 0644]
testData/formatter/expressionsIndent_after.fx [new file with mode: 0644]
testData/formatter/objectLiteralsIndent.fx [new file with mode: 0644]
testData/formatter/objectLiteralsIndent_after.fx [new file with mode: 0644]
testSrc/org/jetbrains/javafx/JavaFxFormatterTest.java [new file with mode: 0644]

index ca91ad26e4c287672ec1d1034448b95be996e3d0..06171d2d21ba04e55f1a554d6a2c3e5fbac3e18d 100644 (file)
@@ -3,11 +3,11 @@ package org.jetbrains.javafx.formatter;
 import com.intellij.formatting.*;
 import com.intellij.lang.ASTNode;
 import com.intellij.lang.FileASTNode;
+import com.intellij.psi.codeStyle.CodeStyleSettings;
 import com.intellij.psi.formatter.FormatterUtil;
 import com.intellij.psi.formatter.common.AbstractBlock;
 import com.intellij.psi.impl.source.tree.LeafElement;
 import com.intellij.psi.tree.IElementType;
-import com.intellij.psi.tree.TokenSet;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.javafx.lang.lexer.JavaFxTokenTypes;
@@ -21,24 +21,30 @@ import java.util.List;
  *
  * @author Alexey.Ivanov
  */
-public class JavaFxFormattingBlock extends AbstractBlock {
+class JavaFxFormattingBlock extends AbstractBlock {
   protected final Indent myIndent;
   protected final Indent myChildIndent;
+  protected final CodeStyleSettings myCodeStyleSettings;
 
-  public JavaFxFormattingBlock(final FileASTNode node) {
-    this(node, Indent.getAbsoluteNoneIndent(), Indent.getNoneIndent());
+  public JavaFxFormattingBlock(@NotNull final FileASTNode node, @NotNull final CodeStyleSettings codeStyleSettings) {
+    this(node, codeStyleSettings, Indent.getAbsoluteNoneIndent(), Indent.getNoneIndent());
   }
 
-  public JavaFxFormattingBlock(final ASTNode node, final Indent indent, final Indent childIndent) {
-    this(node, null, null, indent, childIndent);
+  public JavaFxFormattingBlock(@NotNull final ASTNode node,
+                               @NotNull final CodeStyleSettings codeStyleSettings,
+                               final Indent indent,
+                               final Indent childIndent) {
+    this(node, null, null, codeStyleSettings, indent, childIndent);
   }
 
   protected JavaFxFormattingBlock(@NotNull final ASTNode node,
                                   @Nullable final Wrap wrap,
                                   @Nullable final Alignment alignment,
+                                  @NotNull final CodeStyleSettings codeStyleSettings,
                                   final Indent indent,
                                   final Indent childIndent) {
     super(node, wrap, alignment);
+    myCodeStyleSettings = codeStyleSettings;
     myIndent = indent;
     myChildIndent = childIndent;
   }
@@ -66,11 +72,12 @@ public class JavaFxFormattingBlock extends AbstractBlock {
 
     if (type == JavaFxElementTypes.BLOCK_EXPRESSION ||
         type == JavaFxElementTypes.OBJECT_LITERAL ||
-        type == JavaFxElementTypes.SEQUENCE_LITERAL) {
-      blocks.add(new JavaFxBlockFormattingBlock(node, childIndent, Indent.getNormalIndent(false)));
+        type == JavaFxElementTypes.SEQUENCE_LITERAL ||
+        type == JavaFxElementTypes.CLASS_DEFINITION) {
+      blocks.add(new JavaFxBlockFormattingBlock(node, myCodeStyleSettings, childIndent, Indent.getNormalIndent(false)));
     }
     else {
-      blocks.add(new JavaFxFormattingBlock(node, childIndent, Indent.getNoneIndent()));
+      blocks.add(new JavaFxFormattingBlock(node, myCodeStyleSettings, childIndent, Indent.getNoneIndent()));
     }
   }
 
@@ -99,25 +106,22 @@ public class JavaFxFormattingBlock extends AbstractBlock {
   }
 
   private static class JavaFxBlockFormattingBlock extends JavaFxFormattingBlock {
-    private static final TokenSet BRACES =
-      TokenSet.create(JavaFxTokenTypes.LBRACE, JavaFxTokenTypes.RBRACE, JavaFxTokenTypes.LBRACK, JavaFxTokenTypes.RBRACK);
     private boolean myInsideBraces;
 
-    public JavaFxBlockFormattingBlock(final ASTNode node, final Indent indent, final Indent childIndent) {
-      super(node, indent, childIndent);
+    public JavaFxBlockFormattingBlock(@NotNull final ASTNode node,
+                                      @NotNull final CodeStyleSettings codeStyleSettings,
+                                      final Indent indent,
+                                      final Indent childIndent) {
+      super(node, codeStyleSettings, indent, childIndent);
     }
 
     @Override
     protected Indent calcIndent(final ASTNode child, final IElementType childType) {
-      if (isBrace(childType)) {
+      if (JavaFxFormattingUtil.isBrace(childType)) {
         myInsideBraces = (childType == JavaFxTokenTypes.LBRACE || childType == JavaFxTokenTypes.LBRACK);
         return Indent.getNoneIndent();
       }
       return myInsideBraces ? myChildIndent : Indent.getNoneIndent();
     }
-
-    private boolean isBrace(final IElementType childType) {
-      return BRACES.contains(childType);
-    }
   }
 }
index 341d142d96738366c87dea3c1af25731bbe2e373..311dfe1b7b8e301bca7571e117b5131f655d6b87 100644 (file)
@@ -19,9 +19,9 @@ import org.jetbrains.annotations.NotNull;
 public class JavaFxFormattingModelBuilder implements FormattingModelBuilder {
   @NotNull
   @Override
-  public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
+  public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) {
     PsiFile file = element.getContainingFile();
-    Block block = new JavaFxFormattingBlock(file.getNode());
+    Block block = new JavaFxFormattingBlock(file.getNode(), settings);
     return new JavaFxFormattingModel(file, block, FormattingDocumentModelImpl.createOn(file));
   }
 
diff --git a/src/org/jetbrains/javafx/formatter/JavaFxFormattingUtil.java b/src/org/jetbrains/javafx/formatter/JavaFxFormattingUtil.java
new file mode 100644 (file)
index 0000000..77e6fd2
--- /dev/null
@@ -0,0 +1,22 @@
+package org.jetbrains.javafx.formatter;
+
+import com.intellij.psi.tree.IElementType;
+import com.intellij.psi.tree.TokenSet;
+import org.jetbrains.javafx.lang.lexer.JavaFxTokenTypes;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author Alexey.Ivanov
+ */
+class JavaFxFormattingUtil {
+  private static final TokenSet BRACES =
+    TokenSet.create(JavaFxTokenTypes.LBRACE, JavaFxTokenTypes.RBRACE, JavaFxTokenTypes.LBRACK, JavaFxTokenTypes.RBRACK);
+
+  private JavaFxFormattingUtil() {
+  }
+
+  public static boolean isBrace(final IElementType childType) {
+    return BRACES.contains(childType);
+  }
+}
diff --git a/testData/formatter/classIndent.fx b/testData/formatter/classIndent.fx
new file mode 100644 (file)
index 0000000..3963476
--- /dev/null
@@ -0,0 +1,13 @@
+class Foo extends Node {
+function create() {
+Group {
+content: [
+Circle {
+centerX: 150
+centerY: 120
+radius: 80
+}
+]
+}
+}
+}
\ No newline at end of file
diff --git a/testData/formatter/classIndent_after.fx b/testData/formatter/classIndent_after.fx
new file mode 100644 (file)
index 0000000..6c2e562
--- /dev/null
@@ -0,0 +1,13 @@
+class Foo extends Node {
+    function create() {
+        Group {
+            content: [
+                Circle {
+                    centerX: 150
+                    centerY: 120
+                    radius: 80
+                }
+            ]
+        }
+    }
+}
\ No newline at end of file
diff --git a/testData/formatter/expressionsIndent.fx b/testData/formatter/expressionsIndent.fx
new file mode 100644 (file)
index 0000000..ac027b3
--- /dev/null
@@ -0,0 +1,12 @@
+var a = 3;
+var b = 7;
+var c = 6;
+if (a < 4) {
+b = 5
+}
+else {
+c = 17;
+while (a * b > c) {
+if (a > b) a = 1 else b = 1
+}
+}
\ No newline at end of file
diff --git a/testData/formatter/expressionsIndent_after.fx b/testData/formatter/expressionsIndent_after.fx
new file mode 100644 (file)
index 0000000..771823e
--- /dev/null
@@ -0,0 +1,12 @@
+var a = 3;
+var b = 7;
+var c = 6;
+if (a < 4) {
+    b = 5
+}
+else {
+    c = 17;
+    while (a * b > c) {
+        if (a > b) a = 1 else b = 1
+    }
+}
\ No newline at end of file
diff --git a/testData/formatter/objectLiteralsIndent.fx b/testData/formatter/objectLiteralsIndent.fx
new file mode 100644 (file)
index 0000000..e7f37de
--- /dev/null
@@ -0,0 +1,15 @@
+def timeline: Timeline = Timeline {
+repeatCount: Timeline.INDEFINITE
+keyFrames: [
+KeyFrame {
+time: 1s
+action: function () {
+foo();
+if (a > 4) {
+a = 4;
+timeline.stop();
+}
+}
+}
+]
+}
\ No newline at end of file
diff --git a/testData/formatter/objectLiteralsIndent_after.fx b/testData/formatter/objectLiteralsIndent_after.fx
new file mode 100644 (file)
index 0000000..bea74cc
--- /dev/null
@@ -0,0 +1,15 @@
+def timeline: Timeline = Timeline {
+    repeatCount: Timeline.INDEFINITE
+    keyFrames: [
+        KeyFrame {
+            time: 1s
+            action: function () {
+                foo();
+                if (a > 4) {
+                    a = 4;
+                    timeline.stop();
+                }
+            }
+        }
+    ]
+}
\ No newline at end of file
diff --git a/testSrc/org/jetbrains/javafx/JavaFxFormatterTest.java b/testSrc/org/jetbrains/javafx/JavaFxFormatterTest.java
new file mode 100644 (file)
index 0000000..ab67913
--- /dev/null
@@ -0,0 +1,35 @@
+package org.jetbrains.javafx;
+
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.psi.codeStyle.CodeStyleManager;
+import org.jetbrains.javafx.testUtils.JavaFxLightFixtureTestCase;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author Alexey.Ivanov
+ */
+public class JavaFxFormatterTest extends JavaFxLightFixtureTestCase {
+  public void testExpressionsIndent() {
+    doTest();
+  }
+
+  public void testObjectLiteralsIndent() {
+    doTest();
+  }
+
+  public void testClassIndent() {
+    doTest();
+  }
+
+  private void doTest() {
+    myFixture.configureByFile("formatter/" + getTestName(true) + ".fx");
+    ApplicationManager.getApplication().runWriteAction(new Runnable() {
+      @Override
+      public void run() {
+        CodeStyleManager.getInstance(myFixture.getProject()).reformat(myFixture.getFile());
+      }
+    });
+    myFixture.checkResultByFile("formatter/" + getTestName(true) + "_after.fx");
+  }
+}