/*
 * 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.psi.impl.statements;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.tree.IElementType;
import com.sylvanaar.idea.Lua.lang.parser.LuaElementTypes;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpressionList;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaIdentifierList;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaReferenceExpression;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaAssignmentStatement;
import com.sylvanaar.idea.Lua.lang.psi.visitor.LuaElementVisitor;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: Jun 10, 2010
 * Time: 10:40:55 AM
 */
public class LuaAssignmentStatementImpl extends LuaStatementElementImpl implements LuaAssignmentStatement {
    public LuaAssignmentStatementImpl(ASTNode node) {
        super(node);
    }

    @Override
    public void accept(LuaElementVisitor visitor) {
      visitor.visitAssignment(this);
    }

    @Override
    public void accept(@NotNull PsiElementVisitor visitor) {
        if (visitor instanceof LuaElementVisitor) {
            ((LuaElementVisitor) visitor).visitAssignment(this);
        } else {
            visitor.visitElement(this);
        }
    }

    @Override
    public LuaIdentifierList getLeftExprs() {
        return (LuaIdentifierList) findChildByType(LuaElementTypes.IDENTIFIER_LIST);
    }

    @Override
    public LuaExpressionList getRightExprs() {
        return (LuaExpressionList) findChildByType(LuaElementTypes.EXPR_LIST);
    }

    @NotNull
    @Override
    public IElementType getOperationTokenType() {
        return LuaElementTypes.ASSIGN;
    }


  public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
    if (lastParent != null && lastParent.getParent() == this) {
      return true;
    }

    for (LuaReferenceExpression ref : getLeftExprs().getReferenceExprs())
        if (!processor.execute(ref, ResolveState.initial())) return false;

    return true;
  }


}

@@ -1186,42 +1186,41 @@
 psi.
-expressions.LuaReferenceExpression
+statements.LuaAssignmentStatement
 ;%0D%0Ai
@@ -1271,35 +1271,27 @@
 ents.Lua
-AssignmentStatement
+Declaration
 ;%0D%0Aimpor
@@ -2687,24 +2687,28 @@
 ace) %7B%0D%0A
+    
     if (
 lastPare
@@ -2703,40 +2703,49 @@
 if (
+p
 la
-s
+ce.ge
 tParent
- != null && las
+().getParent().ge
 tParent
+()
 .get
@@ -2757,15 +2757,16 @@
 t() 
-=
+!
 = this
+ 
 ) %7B%0D
@@ -2776,114 +2776,140 @@
     
-return true;%0D%0A    %7D%0D%0A%0D%0A    for (LuaReferenceExpression ref : getLeftExprs().getReferenceExprs())%0D%0A
+      final LuaDeclaration%5B%5D decls = getLeftExprs().getDeclarations();%0D%0A            for (LuaDeclaration decl : decls) %7B%0D%0A   
         
 if (
@@ -2900,24 +2900,29 @@
 %0A           
+     
 if (!process
@@ -2936,55 +2936,67 @@
 ute(
-ref, ResolveState.initial())) return false;%0D%0A%0D%0A
+decl, state)) return false;%0D%0A            %7D%0D%0A        %7D%0D%0A    
     

fix name resolution in non definition assignments