<commit_before>package com.tang.intellij.lua.editor.formatter;

import com.intellij.formatting.*;
import com.intellij.lang.ASTNode;
import com.intellij.psi.TokenType;
import com.intellij.psi.formatter.common.AbstractBlock;
import com.intellij.psi.tree.TokenSet;
import com.tang.intellij.lua.psi.LuaIndentRange;
import com.tang.intellij.lua.psi.LuaTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by tangzx on 2016/12/3.
 */
public class LuaScriptBlock extends AbstractBlock {

    //格式化时
    private TokenSet formattingSet = TokenSet.create(
            LuaTypes.BLOCK,
            LuaTypes.FIELD_LIST
    );

    //回车时
    private TokenSet childAttrSet = TokenSet.orSet(formattingSet, TokenSet.create(
            LuaTypes.IF_STAT,
            LuaTypes.DO_STAT,
            LuaTypes.FUNC_BODY,
            LuaTypes.FOR_A_STAT,
            LuaTypes.FOR_B_STAT,
            LuaTypes.REPEAT_STAT,
            LuaTypes.TABLE_CONSTRUCTOR
    ));

    private SpacingBuilder spacingBuilder;
    private Indent indent;

    LuaScriptBlock(@NotNull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment, Indent indent, SpacingBuilder spacingBuilder) {
        super(node, wrap, alignment);
        this.spacingBuilder = spacingBuilder;
        this.indent = indent;
    }

    private static boolean shouldCreateBlockFor(ASTNode node) {
        return node.getTextRange().getLength() != 0 && node.getElementType() != TokenType.WHITE_SPACE;
    }

    @Override
    protected List<Block> buildChildren() {
        List<Block> blocks = new ArrayList<>();
        ASTNode node = myNode.getFirstChildNode();
        while (node != null) {
            if (shouldCreateBlockFor(node)) {
                Indent childIndent = Indent.getNoneIndent();
                if (formattingSet.contains(myNode.getElementType())) {
                    childIndent = Indent.getNormalIndent();
                }

                blocks.add(new LuaScriptBlock(node, null, null, childIndent, spacingBuilder));
            }

            node = node.getTreeNext();
        }
        return blocks;
    }

    @Nullable
    @Override
    public Spacing getSpacing(@Nullable Block block, @NotNull Block block1) {
        return spacingBuilder.getSpacing(this, block, block1);
    }

    @Override
    public boolean isLeaf() {
        return myNode.getFirstChildNode() == null;
    }

    @Override
    public Indent getIndent() {
        return indent;
    }

    @NotNull
    @Override
    public ChildAttributes getChildAttributes(int newChildIndex) {
        if (myNode.getPsi() instanceof LuaIndentRange)
            return new ChildAttributes(Indent.getNormalIndent(), null);
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
}<commit_msg>indent bug fix<commit_after>package com.tang.intellij.lua.editor.formatter;

import com.intellij.formatting.*;
import com.intellij.lang.ASTNode;
import com.intellij.psi.TokenType;
import com.intellij.psi.formatter.common.AbstractBlock;
import com.intellij.psi.tree.TokenSet;
import com.tang.intellij.lua.psi.LuaIndentRange;
import com.tang.intellij.lua.psi.LuaTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by tangzx on 2016/12/3.
 */
public class LuaScriptBlock extends AbstractBlock {

    //格式化时
    private TokenSet formattingSet = TokenSet.create(
            LuaTypes.BLOCK,
            LuaTypes.FIELD_LIST
    );

    //回车时
    private TokenSet childAttrSet = TokenSet.orSet(formattingSet, TokenSet.create(
            LuaTypes.IF_STAT,
            LuaTypes.DO_STAT,
            LuaTypes.FUNC_BODY,
            LuaTypes.FOR_A_STAT,
            LuaTypes.FOR_B_STAT,
            LuaTypes.REPEAT_STAT,
            LuaTypes.TABLE_CONSTRUCTOR
    ));

    private SpacingBuilder spacingBuilder;
    private Indent indent;

    LuaScriptBlock(@NotNull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment, Indent indent, SpacingBuilder spacingBuilder) {
        super(node, wrap, alignment);
        this.spacingBuilder = spacingBuilder;
        this.indent = indent;
    }

    private static boolean shouldCreateBlockFor(ASTNode node) {
        return node.getTextRange().getLength() != 0 && node.getElementType() != TokenType.WHITE_SPACE;
    }

    @Override
    protected List<Block> buildChildren() {
        List<Block> blocks = new ArrayList<>();
        ASTNode node = myNode.getFirstChildNode();
        while (node != null) {
            if (shouldCreateBlockFor(node)) {
                Indent childIndent = Indent.getNoneIndent();
                if (formattingSet.contains(myNode.getElementType())) {
                    childIndent = Indent.getNormalIndent();
                }

                blocks.add(new LuaScriptBlock(node, null, null, childIndent, spacingBuilder));
            }

            node = node.getTreeNext();
        }
        return blocks;
    }

    @Nullable
    @Override
    public Spacing getSpacing(@Nullable Block block, @NotNull Block block1) {
        return spacingBuilder.getSpacing(this, block, block1);
    }

    @Override
    public boolean isLeaf() {
        return myNode.getFirstChildNode() == null;
    }

    @Override
    public Indent getIndent() {
        return indent;
    }

    @NotNull
    @Override
    public ChildAttributes getChildAttributes(int newChildIndex) {
        if (childAttrSet.contains(myNode.getElementType()))
            return new ChildAttributes(Indent.getNormalIndent(), null);
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
}
{'fraction_non_alphanumeric': 0.09661076170510133, 'fraction_numerical': 0.0034940600978336828, 'mean_word_length': 2.679305912596401, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2894496', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1650, 'n_words': 336}