<commit_before>/*
 * Copyright 2010 <NAME> (Sylvanaar)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.sylvanaar.idea.Lua.lang;

import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.sylvanaar.idea.Lua.lang.parser.LuaElementTypes;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaTableConstructor;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaFunctionDefinitionStatement;
import org.jetbrains.annotations.NotNull;

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

import static com.sylvanaar.idea.Lua.lang.lexer.LuaTokenTypes.LONGCOMMENT;

/**
 * Created by IntelliJ IDEA.
 * User: <NAME>
 * Date: Apr 10, 2010
 * Time: 2:54:53 PM
 */
public class LuaFoldingBuilder implements FoldingBuilder, DumbAware {
    @NotNull
    @Override
    public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) {
        final List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>();
        final ASTNode fnode = node;
        final Document fdoc = document;
        ApplicationManager.getApplication().runReadAction(new Runnable() {
            @Override
            public void run() {
               appendDescriptors(fnode, fdoc, descriptors);
            }
        });

        return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
    }


    private void appendDescriptors(final ASTNode node, final Document document, final List<FoldingDescriptor> descriptors) {
        ProgressManager.checkCanceled();
        
        try {
            if (isFoldableNode(node)) {
                final PsiElement psiElement = node.getPsi();

                if (psiElement instanceof LuaFunctionDefinitionStatement) {
                    LuaFunctionDefinitionStatement stmt = (LuaFunctionDefinitionStatement) psiElement;

                    if (stmt.getText().indexOf('\n')>0 && stmt.getTextLength()>3)
                    descriptors.add(new FoldingDescriptor(node,
                            new TextRange(stmt.getParameters().getTextRange().getEndOffset() + 1,
                                    node.getTextRange().getEndOffset())));
                }

                if (psiElement instanceof LuaTableConstructor) {
                    LuaTableConstructor stmt = (LuaTableConstructor) psiElement;

                    if (stmt.getText().indexOf('\n')>0 && stmt.getTextLength()>3)
                        descriptors.add(new FoldingDescriptor(node,
                                new TextRange(stmt.getTextRange().getStartOffset() + 1,
                                        node.getTextRange().getEndOffset() - 1)));
                }
            }

            if (node.getElementType() == LONGCOMMENT && node.getTextLength() > 2) {
                descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
            }

            ASTNode child = node.getFirstChildNode();
            while (child != null) {
                appendDescriptors(child, document, descriptors);
                child = child.getTreeNext();
            }
        } catch (Exception ignored) {
        }
    }

    private boolean isFoldableNode(ASTNode node) {
        return node.getElementType() == LuaElementTypes.FUNCTION_DEFINITION ||
                node.getElementType() == LuaElementTypes.TABLE_CONSTUCTOR;
    }

    @Override
    public String getPlaceholderText(@NotNull ASTNode node) {
        if (node.getElementType() == LONGCOMMENT)
            return "comment";


        return "...";
    }

    @Override
    public boolean isCollapsedByDefault(@NotNull ASTNode node) {
        return false;
    }
}<commit_msg>allow all function declarations to fold ( fix #34 )<commit_after>/*
 * Copyright 2010 <NAME> (Sylvanaar)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.sylvanaar.idea.Lua.lang;

import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.sylvanaar.idea.Lua.lang.parser.LuaElementTypes;
import com.sylvanaar.idea.Lua.lang.psi.LuaFunctionDefinition;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaAnonymousFunctionExpression;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaTableConstructor;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaFunctionDefinitionStatement;
import org.jetbrains.annotations.NotNull;

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

import static com.sylvanaar.idea.Lua.lang.lexer.LuaTokenTypes.LONGCOMMENT;

/**
 * Created by IntelliJ IDEA.
 * User: <NAME> Akhtar
 * Date: Apr 10, 2010
 * Time: 2:54:53 PM
 */
public class LuaFoldingBuilder implements FoldingBuilder, DumbAware {
    @NotNull
    @Override
    public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) {
        final List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>();
        final ASTNode fnode = node;
        final Document fdoc = document;
        ApplicationManager.getApplication().runReadAction(new Runnable() {
            @Override
            public void run() {
               appendDescriptors(fnode, fdoc, descriptors);
            }
        });

        return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
    }


    private void appendDescriptors(final ASTNode node, final Document document, final List<FoldingDescriptor> descriptors) {
        ProgressManager.checkCanceled();
        
        try {
            if (isFoldableNode(node)) {
                final PsiElement psiElement = node.getPsi();

                if (psiElement instanceof LuaFunctionDefinition) {
                    LuaFunctionDefinition stmt = (LuaFunctionDefinition) psiElement;

                    if (stmt.getText().indexOf('\n')>0 && stmt.getTextLength()>3)
                    descriptors.add(new FoldingDescriptor(node,
                            new TextRange(stmt.getParameters().getTextRange().getEndOffset() + 1,
                                    node.getTextRange().getEndOffset())));
                }

                if (psiElement instanceof LuaTableConstructor) {
                    LuaTableConstructor stmt = (LuaTableConstructor) psiElement;

                    if (stmt.getText().indexOf('\n')>0 && stmt.getTextLength()>3)
                        descriptors.add(new FoldingDescriptor(node,
                                new TextRange(stmt.getTextRange().getStartOffset() + 1,
                                        node.getTextRange().getEndOffset() - 1)));
                }
            }

            if (node.getElementType() == LONGCOMMENT && node.getTextLength() > 2) {
                descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
            }

            ASTNode child = node.getFirstChildNode();
            while (child != null) {
                appendDescriptors(child, document, descriptors);
                child = child.getTreeNext();
            }
        } catch (Exception ignored) {
        }
    }

    private boolean isFoldableNode(ASTNode node) {
        return node.getElementType() == LuaElementTypes.FUNCTION_DEFINITION || 
                node.getElementType() == LuaElementTypes.LOCAL_FUNCTION ||
                node.getElementType() == LuaElementTypes.ANONYMOUS_FUNCTION_EXPRESSION ||
                node.getElementType() == LuaElementTypes.TABLE_CONSTUCTOR;
    }

    @Override
    public String getPlaceholderText(@NotNull ASTNode node) {
        if (node.getElementType() == LONGCOMMENT)
            return "comment";


        return "...";
    }

    @Override
    public boolean isCollapsedByDefault(@NotNull ASTNode node) {
        return false;
    }
}
{'fraction_non_alphanumeric': 0.09680521423228977, 'fraction_numerical': 0.005983545250560957, 'mean_word_length': 2.606936416184971, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4716253', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2516, 'n_words': 571}