formatter tests and minor fixes
[idea/javafx-plugin.git] / src / org / jetbrains / javafx / formatter / JavaFxFormattingBlock.java
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);
-    }
   }
 }