/*
 * 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.editor.completion;


import com.intellij.codeInsight.completion.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.patterns.ElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaVariable;
import org.jetbrains.annotations.NotNull;

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

public class LuaCompletionContributor extends CompletionContributor {
    private static final Logger log = Logger.getInstance("#Lua.CompletionContributor");

    private static final ElementPattern<PsiElement> AFTER_DOT = psiElement().afterLeaf(".").withParent(LuaVariable.class);

    public LuaCompletionContributor() {
        log.info("Created Lua completion contributor");

        extend(CompletionType.BASIC, psiElement(PsiElement.class), new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
                for (String s : LuaKeywordsManager.getKeywords())
                    result.addElement(new LuaLookupElement(s));
            }
        });

        extend(CompletionType.BASIC, AFTER_DOT, new CompletionProvider<CompletionParameters>() {
          @Override
          protected void addCompletions(@NotNull CompletionParameters parameters,
                                        ProcessingContext context,
                                        @NotNull CompletionResultSet result) {
            final PsiElement position = parameters.getPosition();

            assert position.getParent() instanceof LuaVariable;

            Object[] os = ((LuaVariable) position.getParent()).getVariants();

            for(Object o : os)
                result.addElement(new LuaLookupElement(o.toString()));
          }
        });

    }
}
@@ -929,31 +929,159 @@
 psi.
-expressions.LuaVariable
+LuaPsiFile;%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaIdentifier;%0Aimport com.sylvanaar.idea.Lua.lang.psi.visitor.LuaRecursiveElementVisitor
 ;%0Aim
@@ -1121,16 +1121,65 @@
 tNull;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 import s
@@ -1485,17 +1485,158 @@
 Leaf(%22.%22
-)
+, %22:%22);//.withParent(LuaVariable.class);%0A    private static final ElementPattern%3CPsiElement%3E NOT_AFTER_DOT = psiElement().andNot(AFTER_DOT);//
 .withPar
@@ -1798,36 +1798,21 @@
 IC, 
-psiElement(PsiElement.class)
+NOT_AFTER_DOT
 , ne
@@ -2286,32 +2286,34 @@
 %3E() %7B%0A          
+  
 @Override%0A      
@@ -2298,32 +2298,34 @@
       @Override%0A
+  
           protec
@@ -2390,16 +2390,18 @@
 meters,%0A
+  
         
@@ -2499,16 +2499,18 @@
         
+  
 @NotNull
@@ -2532,32 +2532,36 @@
 ltSet result) %7B%0A
+%0A   
             fina
@@ -2560,333 +2560,833 @@
     
+ 
 fi
-nal PsiElement position = parameters.getPosition();%0A%0A            assert position.getParent() instanceof LuaVariable;%0A%0A            Object%5B%5D os = ((LuaVariable) position.getParent()).getVariants();%0A%0A            for(Object o : os)%0A                result.addElement(new LuaLookupElement(o.toString()));%0A          %7D%0A        %7D);%0A
+eldVisitor.reset();%0A%0A                ((LuaPsiFile)parameters.getOriginalFile()).accept(fieldVisitor);%0A%0A                for (String s : fieldVisitor.getResult())%0A                    result.addElement(new LuaLookupElement(s));%0A            %7D%0A        %7D);%0A%0A    %7D%0A%0A    LuaFieldElementVisitor fieldVisitor = new LuaFieldElementVisitor();%0A%0A    private static class LuaFieldElementVisitor extends LuaRecursiveElementVisitor %7B%0A        Set%3CString%3E result = new HashSet%3CString%3E();%0A%0A        @Override%0A        public void visitIdentifier(LuaIdentifier e) %7B%0A            super.visitIdentifier(e);%0A%0A            if (e.isField() && e.getText().charAt(0) != '%5B')%0A                result.add(e.getName());%0A%0A        %7D%0A%0A        public Set%3CString%3E getResult() %7B%0A            return result;%0A        %7D%0A%0A        public void reset() %7B result.clear(); %7D
 %0A   

allow field completions