package com.tang.intellij.lua.editor.completion;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.ProjectAndLibrariesScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import com.tang.intellij.lua.doc.psi.LuaDocClassDef;
import com.tang.intellij.lua.doc.psi.api.LuaComment;
import com.tang.intellij.lua.psi.LuaGlobalFuncDef;
import com.tang.intellij.lua.psi.index.LuaGlobalFuncIndex;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

import static com.intellij.patterns.PlatformPatterns.psiElement;

/**
 *
 * Created by tangzx on 2016/11/27.
 */
public class LuaCompletionContributor extends CompletionContributor {

    private static final PsiElementPattern.Capture<PsiElement> AFTER_DOT = psiElement().afterLeaf(".", ":");

    public LuaCompletionContributor() {
        extend(CompletionType.BASIC, AFTER_DOT, new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
                Project project = completionParameters.getOriginalFile().getProject();
                Collection<LuaGlobalFuncDef> defs = LuaGlobalFuncIndex.getInstance().get("s", project, new ProjectAndLibrariesScope(project));
                for (LuaGlobalFuncDef def : defs) {
                    LuaComment comment = def.getComment();
                    if (comment != null) {
                        LuaDocClassDef classDec = PsiTreeUtil.findChildOfType(comment, LuaDocClassDef.class);
                        if (classDec != null) {
                            completionResultSet.addElement(LookupElementBuilder.create(classDec.getText()));
                        }
                    }
                }

                Collection<String> all = LuaGlobalFuncIndex.getInstance().getAllKeys(completionParameters.getOriginalFile().getProject());
                all.forEach(name -> completionResultSet.addElement(LookupElementBuilder.create(name)));
            }
        });
    }

}

@@ -303,286 +303,84 @@
 lij.
-psi.search.ProjectAndLibrariesScope;%0Aimport com.intellij.psi.util.PsiTreeUtil;%0Aimport com.intellij.util.ProcessingContext;%0Aimport com.tang.intellij.lua.doc.psi.LuaDocClassDef;%0Aimport com.tang.intellij.lua.doc.psi.api.LuaComment;%0Aimport com.tang.intellij.lua.psi.LuaGlobalFuncDef
+util.ProcessingContext;%0Aimport com.tang.intellij.lua.psi.index.LuaClassIndex
 ;%0Aim
@@ -1227,24 +1227,60 @@
 etProject();
+%0A%0A                //global functions
 %0A           
@@ -1299,30 +1299,19 @@
 ion%3C
-LuaGlobalFuncDef%3E defs
+String%3E all
  = L
@@ -1349,550 +1349,198 @@
 .get
-(%22s%22, project, new ProjectAndLibrariesScope(project));%0A                for (LuaGlobalFuncDef def : defs) %7B%0A                    LuaComment comment = def.getComment();%0A                    if (comment != null) %7B%0A                        LuaDocClassDef classDec = PsiTreeUtil.findChildOfType(comment, LuaDocClassDef.class);%0A                        if (classDec != null) %7B%0A                            completionResultSet.addElement(LookupElementBuilder.create(classDec.getText()));%0A                        %7D%0A                    %7D%0A                %7D%0A
+AllKeys(completionParameters.getOriginalFile().getProject());%0A                all.forEach(name -%3E completionResultSet.addElement(LookupElementBuilder.create(name)));%0A%0A                //class
 %0A   
@@ -1574,32 +1574,34 @@
 ing%3E all
- = LuaGlobalFunc
+Classes = LuaClass
 Index.ge
@@ -1627,59 +1627,15 @@
 eys(
-completionParameters.getOriginalFile().getP
+p
 roject
-()
 );%0A 
@@ -1644,32 +1644,39 @@
              all
+Classes
 .forEach(name -%3E
@@ -1660,33 +1660,38 @@
 Classes.forEach(
-n
+classN
 ame -%3E completio
@@ -1732,33 +1732,38 @@
 tBuilder.create(
-n
+classN
 ame)));%0A        

test completion