1 package org.jetbrains.javafx.formatter;
3 import com.intellij.formatting.*;
4 import com.intellij.lang.ASTNode;
5 import com.intellij.lang.FileASTNode;
6 import com.intellij.psi.formatter.FormatterUtil;
7 import com.intellij.psi.formatter.common.AbstractBlock;
8 import com.intellij.psi.impl.source.tree.LeafElement;
9 import com.intellij.psi.tree.IElementType;
10 import com.intellij.psi.tree.TokenSet;
11 import org.jetbrains.annotations.NotNull;
12 import org.jetbrains.annotations.Nullable;
13 import org.jetbrains.javafx.lang.lexer.JavaFxTokenTypes;
14 import org.jetbrains.javafx.lang.parser.JavaFxElementTypes;
16 import java.util.ArrayList;
17 import java.util.List;
20 * Created by IntelliJ IDEA.
22 * @author Alexey.Ivanov
24 public class JavaFxFormattingBlock extends AbstractBlock {
25 protected final Indent myIndent;
26 protected final Indent myChildIndent;
28 public JavaFxFormattingBlock(final FileASTNode node) {
29 this(node, Indent.getAbsoluteNoneIndent(), Indent.getNoneIndent());
32 public JavaFxFormattingBlock(final ASTNode node, final Indent indent, final Indent childIndent) {
33 this(node, null, null, indent, childIndent);
36 protected JavaFxFormattingBlock(@NotNull final ASTNode node,
37 @Nullable final Wrap wrap,
38 @Nullable final Alignment alignment,
40 final Indent childIndent) {
41 super(node, wrap, alignment);
43 myChildIndent = childIndent;
47 protected List<Block> buildChildren() {
52 final List<Block> result = new ArrayList<Block>();
53 ASTNode child = myNode.getFirstChildNode();
54 while (child != null) {
55 if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
56 processChild(result, child);
58 child = child.getTreeNext();
63 private void processChild(final List<Block> blocks, final ASTNode node) {
64 final IElementType type = node.getElementType();
65 final Indent childIndent = calcIndent(node, type);
67 if (type == JavaFxElementTypes.BLOCK_EXPRESSION ||
68 type == JavaFxElementTypes.OBJECT_LITERAL ||
69 type == JavaFxElementTypes.SEQUENCE_LITERAL) {
70 blocks.add(new JavaFxBlockFormattingBlock(node, childIndent, Indent.getNormalIndent(false)));
73 blocks.add(new JavaFxFormattingBlock(node, childIndent, Indent.getNoneIndent()));
77 protected Indent calcIndent(final ASTNode child, final IElementType childType) {
78 return (childType == JavaFxTokenTypes.LBRACE || childType == JavaFxTokenTypes.RBRACE) ? Indent.getNoneIndent() : myChildIndent;
82 public Spacing getSpacing(final Block child1, final Block child2) {
87 public boolean isLeaf() {
88 return (myNode instanceof LeafElement);
92 protected Indent getChildIndent() {
97 public Indent getIndent() {
101 private static class JavaFxBlockFormattingBlock extends JavaFxFormattingBlock {
102 private static final TokenSet BRACES =
103 TokenSet.create(JavaFxTokenTypes.LBRACE, JavaFxTokenTypes.RBRACE, JavaFxTokenTypes.LBRACK, JavaFxTokenTypes.RBRACK);
104 private boolean myInsideBraces;
106 public JavaFxBlockFormattingBlock(final ASTNode node, final Indent indent, final Indent childIndent) {
107 super(node, indent, childIndent);
111 protected Indent calcIndent(final ASTNode child, final IElementType childType) {
112 if (isBrace(childType)) {
113 myInsideBraces = (childType == JavaFxTokenTypes.LBRACE || childType == JavaFxTokenTypes.LBRACK);
114 return Indent.getNoneIndent();
116 return myInsideBraces ? myChildIndent : Indent.getNoneIndent();
119 private boolean isBrace(final IElementType childType) {
120 return BRACES.contains(childType);