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

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.util.ProcessingContext;
import com.tang.intellij.lua.doc.psi.LuaDocTypes;
import org.jetbrains.annotations.NotNull;

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

/**
 *
 * Created by tangzx on 2016/12/2.
 */
public class LuaCommentCompletionContributor extends CompletionContributor {

    private static final PsiElementPattern.Capture<PsiElement> SHOW_DOC_TAG = psiElement().afterLeaf(
            psiElement().withText("@")
                    .afterSiblingSkipping(psiElement().withElementType(ElementType.WHITE_SPACE), psiElement().withElementType(LuaDocTypes.DASHES))
    );

    public LuaCommentCompletionContributor() {
        extend(CompletionType.BASIC, SHOW_DOC_TAG, new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
                String[] keyWords = new String[]{ "class", "param", "return", "type" };
                for (String keyWord : keyWords) {
                    completionResultSet.addElement(LookupElementBuilder.create(keyWord));
                }
            }
        });

        extend(CompletionType.BASIC, psiElement().afterLeaf(psiElement().withElementType(LuaDocTypes.PARAM_NAME_REF)), new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
                completionResultSet.addElement(LookupElementBuilder.create("sss"));
            }
        });
    }

}

@@ -150,16 +150,97 @@
 uilder;%0A
+import com.intellij.icons.AllIcons;%0Aimport com.intellij.openapi.project.Project;%0A
 import c
@@ -450,65 +450,312 @@
 doc.
-psi.LuaDocTypes;%0Aimport org.jetbrains.annotations.NotNull
+LuaCommentUtil;%0Aimport com.tang.intellij.lua.doc.psi.LuaDocPsiElement;%0Aimport com.tang.intellij.lua.doc.psi.LuaDocTypes;%0Aimport com.tang.intellij.lua.psi.*;%0Aimport com.tang.intellij.lua.psi.index.LuaClassIndex;%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aimport java.util.Collection;%0Aimport java.util.List
 ;%0A%0Ai
@@ -1235,24 +1235,406 @@
 S))%0A    );%0A%0A
+    private static final PsiElementPattern.Capture%3CPsiElement%3E AFTER_PARAM =  psiElement().afterLeaf(%0A            psiElement().withElementType(LuaDocTypes.TAG_PARAM)%0A    );%0A%0A    private static final  PsiElementPattern.Capture%3CPsiElement%3E SHOW_CLASS =  psiElement().afterLeaf(%0A            psiElement().withText(%22#%22).inside(psiElement().withElementType(LuaTypes.DOC_COMMENT))%0A    );%0A%0A
     public L
@@ -2287,425 +2287,2147 @@
 IC, 
-psiElement().afterLeaf(psiElement().withElementType(LuaDocTypes.PARAM_NAME_REF)), new CompletionProvider%3CCompletionParameters%3E() %7B%0A            @Override%0A            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) %7B%0A                completionResultSet.addElement(LookupElementBuilder.create(%22sss%22)
+AFTER_PARAM, new CompletionProvider%3CCompletionParameters%3E() %7B%0A            @Override%0A            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) %7B%0A                PsiElement element = completionParameters.getOriginalFile().findElementAt(completionParameters.getOffset() - 1);%0A                if (element != null && !(element instanceof LuaDocPsiElement))%0A                    element = element.getParent();%0A%0A                if (element instanceof LuaDocPsiElement) %7B%0A                    LuaCommentOwner owner = LuaCommentUtil.findOwner((LuaDocPsiElement) element);%0A                    if (owner instanceof LuaFuncBodyOwner) %7B%0A                        LuaFuncBodyOwner bodyOwner = (LuaFuncBodyOwner) owner;%0A                        LuaFuncBody body = bodyOwner.getFuncBody();%0A                        if (body != null) %7B%0A                            List%3CLuaParDef%3E parDefList = body.getParDefList();%0A                            for (LuaParDef def : parDefList) %7B%0A                                completionResultSet.addElement(%0A                                        LookupElementBuilder.create(def.getText())%0A                                                .withIcon(AllIcons.Nodes.Variable)%0A                                );%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);%0A%0A        extend(CompletionType.BASIC, SHOW_CLASS, new CompletionProvider%3CCompletionParameters%3E() %7B%0A            @Override%0A            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) %7B%0A                Project project = completionParameters.getPosition().getProject();%0A                Collection%3CString%3E collection = LuaClassIndex.getInstance().getAllKeys(project);%0A                collection.forEach(className -%3E %7B%0A                    completionResultSet.addElement(LookupElementBuilder.create(className).withIcon(AllIcons.Nodes.Class));%0A                %7D
 );%0A 

Comment Completion