formatter initial
authoralexey.ivanov <alexey.ivanov@jetbrains.com>
Mon, 1 Nov 2010 19:11:36 +0000 (22:11 +0300)
committeralexey.ivanov <alexey.ivanov@jetbrains.com>
Mon, 1 Nov 2010 19:11:36 +0000 (22:11 +0300)
src/META-INF/plugin.xml
src/org/jetbrains/javafx/formatter/JavaFxFormattingBlock.java [new file with mode: 0644]
src/org/jetbrains/javafx/formatter/JavaFxFormattingModel.java [new file with mode: 0644]
src/org/jetbrains/javafx/formatter/JavaFxFormattingModelBuilder.java [new file with mode: 0644]

index 00cc6f14ee033911b66af07a4336f09254402ad1..a854240582f7dea12e8d61c666828043a2748c8c 100644 (file)
@@ -91,6 +91,8 @@
     <lang.findUsagesProvider language="JavaFx" implementationClass="org.jetbrains.javafx.findUsages.JavaFxFindUsagesProvider"/>
     <lang.namesValidator language="JavaFx" implementationClass="org.jetbrains.javafx.refactoring.rename.JavaFxNamesValidator"/>
 
+    <lang.formatter language="JavaFx" implementationClass="org.jetbrains.javafx.formatter.JavaFxFormattingModelBuilder"/>
+
     <renamePsiElementProcessor implementation="org.jetbrains.javafx.refactoring.rename.JavaFxRenameProcessor" order="last"/>
 
     <frameworkSupport implementation="org.jetbrains.javafx.JavaFxSupportProvider"/>
diff --git a/src/org/jetbrains/javafx/formatter/JavaFxFormattingBlock.java b/src/org/jetbrains/javafx/formatter/JavaFxFormattingBlock.java
new file mode 100644 (file)
index 0000000..ca91ad2
--- /dev/null
@@ -0,0 +1,123 @@
+package org.jetbrains.javafx.formatter;
+
+import com.intellij.formatting.*;
+import com.intellij.lang.ASTNode;
+import com.intellij.lang.FileASTNode;
+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;
+import org.jetbrains.javafx.lang.parser.JavaFxElementTypes;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author Alexey.Ivanov
+ */
+public class JavaFxFormattingBlock extends AbstractBlock {
+  protected final Indent myIndent;
+  protected final Indent myChildIndent;
+
+  public JavaFxFormattingBlock(final FileASTNode node) {
+    this(node, Indent.getAbsoluteNoneIndent(), Indent.getNoneIndent());
+  }
+
+  public JavaFxFormattingBlock(final ASTNode node, final Indent indent, final Indent childIndent) {
+    this(node, null, null, indent, childIndent);
+  }
+
+  protected JavaFxFormattingBlock(@NotNull final ASTNode node,
+                                  @Nullable final Wrap wrap,
+                                  @Nullable final Alignment alignment,
+                                  final Indent indent,
+                                  final Indent childIndent) {
+    super(node, wrap, alignment);
+    myIndent = indent;
+    myChildIndent = childIndent;
+  }
+
+  @Override
+  protected List<Block> buildChildren() {
+    if (isLeaf()) {
+      return EMPTY;
+    }
+
+    final List<Block> result = new ArrayList<Block>();
+    ASTNode child = myNode.getFirstChildNode();
+    while (child != null) {
+      if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
+        processChild(result, child);
+      }
+      child = child.getTreeNext();
+    }
+    return result;
+  }
+
+  private void processChild(final List<Block> blocks, final ASTNode node) {
+    final IElementType type = node.getElementType();
+    final Indent childIndent = calcIndent(node, type);
+
+    if (type == JavaFxElementTypes.BLOCK_EXPRESSION ||
+        type == JavaFxElementTypes.OBJECT_LITERAL ||
+        type == JavaFxElementTypes.SEQUENCE_LITERAL) {
+      blocks.add(new JavaFxBlockFormattingBlock(node, childIndent, Indent.getNormalIndent(false)));
+    }
+    else {
+      blocks.add(new JavaFxFormattingBlock(node, childIndent, Indent.getNoneIndent()));
+    }
+  }
+
+  protected Indent calcIndent(final ASTNode child, final IElementType childType) {
+    return (childType == JavaFxTokenTypes.LBRACE || childType == JavaFxTokenTypes.RBRACE) ? Indent.getNoneIndent() : myChildIndent;
+  }
+
+  @Override
+  public Spacing getSpacing(final Block child1, final Block child2) {
+    return null;
+  }
+
+  @Override
+  public boolean isLeaf() {
+    return (myNode instanceof LeafElement);
+  }
+
+  @Override
+  protected Indent getChildIndent() {
+    return myChildIndent;
+  }
+
+  @Override
+  public Indent getIndent() {
+    return myIndent;
+  }
+
+  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);
+    }
+
+    @Override
+    protected Indent calcIndent(final ASTNode child, final IElementType childType) {
+      if (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);
+    }
+  }
+}
diff --git a/src/org/jetbrains/javafx/formatter/JavaFxFormattingModel.java b/src/org/jetbrains/javafx/formatter/JavaFxFormattingModel.java
new file mode 100644 (file)
index 0000000..932802f
--- /dev/null
@@ -0,0 +1,20 @@
+package org.jetbrains.javafx.formatter;
+
+import com.intellij.formatting.Block;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.formatter.FormattingDocumentModelImpl;
+import com.intellij.psi.formatter.PsiBasedFormattingModel;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author Alexey.Ivanov
+ */
+public class JavaFxFormattingModel extends PsiBasedFormattingModel {
+  public JavaFxFormattingModel(final PsiFile file,
+                               @NotNull final Block rootBlock,
+                               final FormattingDocumentModelImpl documentModel) {
+    super(file, rootBlock, documentModel);
+  }
+}
diff --git a/src/org/jetbrains/javafx/formatter/JavaFxFormattingModelBuilder.java b/src/org/jetbrains/javafx/formatter/JavaFxFormattingModelBuilder.java
new file mode 100644 (file)
index 0000000..341d142
--- /dev/null
@@ -0,0 +1,32 @@
+package org.jetbrains.javafx.formatter;
+
+import com.intellij.formatting.Block;
+import com.intellij.formatting.FormattingModel;
+import com.intellij.formatting.FormattingModelBuilder;
+import com.intellij.lang.ASTNode;
+import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.codeStyle.CodeStyleSettings;
+import com.intellij.psi.formatter.FormattingDocumentModelImpl;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Created by IntelliJ IDEA.
+ *
+ * @author Alexey.Ivanov
+ */
+public class JavaFxFormattingModelBuilder implements FormattingModelBuilder {
+  @NotNull
+  @Override
+  public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
+    PsiFile file = element.getContainingFile();
+    Block block = new JavaFxFormattingBlock(file.getNode());
+    return new JavaFxFormattingModel(file, block, FormattingDocumentModelImpl.createOn(file));
+  }
+
+  @Override
+  public TextRange getRangeAffectingIndent(PsiFile file, int offset, ASTNode elementAtOffset) {
+    return null;
+  }
+}