Advertisement
Guest User

FormatBlock

a guest
Jun 1st, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package com.gameforge.idea.Corona.lang.formatter;
  2.  
  3. import com.gameforge.idea.Corona.lang.lexer.CoronaElementType;
  4. import com.gameforge.idea.Corona.lang.lexer.CoronaTokenTypes;
  5. import com.gameforge.idea.Corona.lang.parser.CoronaElementTypes;
  6. import com.intellij.formatting.*;
  7. import com.intellij.lang.ASTNode;
  8. import com.intellij.openapi.util.TextRange;
  9. import com.intellij.psi.TokenType;
  10. import com.intellij.psi.formatter.common.AbstractBlock;
  11. import com.intellij.psi.impl.source.tree.FileElement;
  12. import com.intellij.psi.tree.IElementType;
  13. import org.jetbrains.annotations.NotNull;
  14. import org.jetbrains.annotations.Nullable;
  15. import sun.security.util.ByteArrayLexOrder;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20. /**
  21.  * Created by felix.doerschner on 17.05.2016.
  22.  */
  23. public class CoronaFormatBlock implements Block {
  24.     private Indent indent;
  25.     private Wrap wrap;
  26.     private Alignment alignment;
  27.     private ASTNode node;
  28.     private List<Block> children;
  29.  
  30.     protected CoronaFormatBlock(ASTNode node, Wrap wrap, Alignment alignment, Indent indent) {
  31. //      super(node, wrap, alignment);
  32.         this.indent = indent;
  33.         this.node = node;
  34.         this.wrap = wrap;
  35.         this.alignment = alignment;
  36.     }
  37.  
  38.     @NotNull
  39.     @Override
  40.     public TextRange getTextRange() {
  41.         return node.getTextRange();
  42.     }
  43.  
  44.     @NotNull
  45.     @Override
  46.     public List<Block> getSubBlocks() {
  47.         return buildChildren();
  48.     }
  49.  
  50.     @Nullable
  51.     @Override
  52.     public Wrap getWrap() {
  53.         return wrap;
  54.     }
  55.  
  56.     @Override
  57.     public Indent getIndent() {
  58.         return indent == null ? CoronaIndent.getIndent(this, node) : indent;
  59.     }
  60.  
  61.     @Nullable
  62.     @Override
  63.     public Alignment getAlignment() {
  64.         return alignment;
  65.     }
  66.  
  67.     protected List<Block> buildChildren() {
  68.         children = new ArrayList<>();
  69.         ASTNode child = node.getFirstChildNode();
  70.         String nodeText = node.getText();
  71.         while (child != null) {
  72.             if (!CoronaElementTypes.WHITE_SPACES_SET.contains(child.getElementType())) {
  73.                 Indent indent = CoronaIndent.getIndent(this, child);
  74.                 children.add(new CoronaFormatBlock(child, Wrap.createWrap(WrapType.NONE, false), getAlignment(), indent));
  75.             }
  76.             child = child.getTreeNext();
  77.         }
  78.         return children;
  79.     }
  80.  
  81.     private void print() {
  82.         System.out.println();
  83.     }
  84.  
  85.     @Nullable
  86.     @Override
  87.     public Spacing getSpacing(@Nullable Block block, @NotNull Block block1) {
  88.         CoronaFormatBlock child1 = (CoronaFormatBlock) block;
  89.         CoronaFormatBlock child2 = (CoronaFormatBlock) block1;
  90.  
  91.         return CoronaSpacing.getSpacing(child1, child2);
  92.     }
  93.  
  94.     @NotNull
  95.     @Override
  96.     public ChildAttributes getChildAttributes(int i) {
  97.         Block child = children.get(i);
  98.         String nodeText = node.getText();
  99.         String childText = ((CoronaFormatBlock)child).getNode().getText();
  100.         return new ChildAttributes(child.getIndent(), child.getAlignment());
  101.     }
  102.  
  103.     @Override
  104.     public boolean isIncomplete() {
  105.         return false;
  106.     }
  107.  
  108.     @Override
  109.     public boolean isLeaf() {
  110.         return node.getFirstChildNode() == null;
  111.     }
  112.  
  113.     ASTNode getNode() {
  114.         return node;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement