package com.tang.intellij.lua.folding;

import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.CustomFoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.tang.intellij.lua.psi.LuaFile;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by tangzx
 * Date : 2015/11/16.
 */
public class LuaFoldingBuilder extends CustomFoldingBuilder {
    @Override
    protected void buildLanguageFoldRegions(@NotNull List<FoldingDescriptor> list,
                                            @NotNull PsiElement psiElement,
                                            @NotNull Document document, boolean b) {
        //if (!(psiElement instanceof LuaFile)) return;
    }

    @Override
    protected String getLanguagePlaceholderText(@NotNull ASTNode astNode, @NotNull TextRange textRange) {
        return null;
    }

    @Override
    protected boolean isRegionCollapsedByDefault(@NotNull ASTNode astNode) {
        return false;
    }
}

@@ -100,22 +100,16 @@
 folding.
-Custom
 FoldingB
@@ -114,16 +114,18 @@
 gBuilder
+Ex
 ;%0Aimport
@@ -239,30 +239,22 @@
 lij.
-openapi.util.TextRange
+psi.PsiElement
 ;%0Aim
@@ -271,34 +271,40 @@
 ntellij.psi.
-PsiElement
+util.PsiTreeUtil
 ;%0Aimport com
@@ -329,20 +329,28 @@
 .psi.Lua
-File
+LocalFuncDef
 ;%0Aimport
@@ -385,16 +385,116 @@
 otNull;%0A
+import org.jetbrains.annotations.Nullable;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;
 %0Aimport 
@@ -604,14 +604,8 @@
 nds 
-Custom
 Fold
@@ -614,19 +614,34 @@
 gBuilder
+Ex
  %7B%0A
+    @NotNull%0A
     @Ove
@@ -655,161 +655,497 @@
    p
-rotected void buildLanguageFoldRegions(@NotNull List%3CFoldingDescriptor%3E list,%0A                                            @NotNull PsiElement psiElement,
+ublic FoldingDescriptor%5B%5D buildFoldRegions(@NotNull PsiElement psiElement, @NotNull Document document, boolean b) %7B%0A        List%3CFoldingDescriptor%3E descriptors = new ArrayList%3CFoldingDescriptor%3E();%0A        Collection%3CLuaLocalFuncDef%3E localFuncDefs = PsiTreeUtil.findChildrenOfType(psiElement, LuaLocalFuncDef.class);%0A        for (final LuaLocalFuncDef funcDef : localFuncDefs) %7B%0A            descriptors.add(new FoldingDescriptor(funcDef, funcDef.getTextRange()) %7B%0A                @Nullable
 %0A   
@@ -1149,32 +1149,42 @@
                 
+@Override%0A
                 
@@ -1187,123 +1187,243 @@
     
-            @NotNull Document document, boolean b) %7B%0A        //if (!(psiElement instanceof LuaFile)) return;%0A    %7D%0A
+public String getPlaceholderText() %7B%0A                    return %22local function() ... end%22;%0A                %7D%0A            %7D);%0A        %7D%0A        return descriptors.toArray(new FoldingDescriptor%5Bdescriptors.size()%5D);%0A    %7D%0A%0A    @Nullable
 %0A   
@@ -1438,24 +1438,21 @@
 de%0A    p
-rotected
+ublic
  String 
@@ -1458,16 +1458,8 @@
  get
-Language
 Plac
@@ -1498,38 +1498,8 @@
 Node
-, @NotNull TextRange textRange
 ) %7B%0A
@@ -1549,16 +1549,13 @@
    p
-rotected
+ublic
  boo
@@ -1561,22 +1561,16 @@
 olean is
-Region
 Collapse
@@ -1627,12 +1627,11 @@
 urn 
-fals
+tru
 e;%0A 

test folding