/*
 * Copyright 2010 Jon S Akhtar (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.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.sylvanaar.idea.Lua.lang.parser.LuaElementTypes;
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.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: Apr 10, 2010
 * Time: 2:54:53 PM
 */
public class LuaFoldingBuilder implements FoldingBuilder {
    @NotNull
    @Override
    public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) {
        List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>();
        appendDescriptors(node, document, descriptors);
        return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
    }


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

                descriptors.add(new FoldingDescriptor(node,
                        new TextRange(stmt.getParameters().getTextRange().getEndOffset() + 1,
                                node.getTextRange().getEndOffset())));
            }

            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;
    }

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


        return "...";
    }

    @Override
    public boolean isCollapsedByDefault(@NotNull ASTNode node) {
        return false;
    }
}

@@ -897,24 +897,61 @@
 TextRange;%0D%0A
+import com.intellij.psi.PsiElement;%0D%0A
 import com.s
@@ -998,16 +998,89 @@
 Types;%0D%0A
+import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaTableConstructor;%0D%0A
 import c
@@ -1254,18 +1254,16 @@
 ist;%0D%0A%0D%0A
-%0D%0A
 import s
@@ -1316,17 +1316,27 @@
 enTypes.
-*
+LONGCOMMENT
 ;%0D%0A%0D%0A/**
@@ -2039,24 +2039,169 @@
 e(node)) %7B%0D%0A
+                final PsiElement psiElement = node.getPsi();%0D%0A%0D%0A                if (psiElement instanceof LuaFunctionDefinitionStatement) %7B%0D%0A    
             
@@ -2275,34 +2275,35 @@
 tement) 
-node.getPsi()
+psiElement
 ;%0D%0A%0D%0A
+    
         
@@ -2379,16 +2379,20 @@
         
+  
+  
 new Text
@@ -2486,16 +2486,20 @@
         
+    
 node.get
@@ -2526,24 +2526,444 @@
 fset())));%0D%0A
+                %7D%0D%0A%0D%0A                if (psiElement instanceof LuaTableConstructor) %7B%0D%0A                    LuaTableConstructor stmt = (LuaTableConstructor) psiElement;%0D%0A%0D%0A                    descriptors.add(new FoldingDescriptor(node,%0D%0A                            new TextRange(stmt.getTextRange().getStartOffset() + 1,%0D%0A                                    node.getTextRange().getEndOffset() - 1)));%0D%0A                %7D%0D%0A
             
@@ -3558,16 +3558,94 @@
 FINITION
+ %7C%7C%0D%0A                node.getElementType() == LuaElementTypes.TABLE_CONSTUCTOR
 ;%0D%0A    %7D

make table constructors foldable