package com.sylvanaar.idea.Lua.intentions.stdlib;

import com.intellij.psi.PsiElement;
import com.sylvanaar.idea.Lua.lang.psi.LuaReferenceElement;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaFunctionCallExpression;
import com.sylvanaar.idea.Lua.lang.psi.impl.symbols.LuaGlobalUsageImpl;
import com.sylvanaar.idea.Lua.lang.psi.symbols.LuaCompoundIdentifier;
import com.sylvanaar.idea.Lua.lang.psi.symbols.LuaGlobalIdentifier;
import com.sylvanaar.idea.Lua.lang.psi.types.LuaPrimitiveType;

public class StdTypeStaticInstanceMethod {
    private final String typeName;
    private final String methodName;
    private final StdLibraryType stdType;

    /**
     * Method
     *
     * @param typeName   Type name
     * @param methodName Method name
     */
    private StdTypeStaticInstanceMethod(String typeName, String methodName, StdLibraryType stdType) {
        this.stdType = stdType;
        this.typeName = typeName;
        this.methodName = methodName;
    }

    /**
     * Factory for type
     *
     * @param call Call element
     * @return new instance or null
     */
    public static StdTypeStaticInstanceMethod create(LuaFunctionCallExpression call) {
        StdTypeStaticInstanceMethod oResult = null;
        LuaReferenceElement methodRef, typeRef = call.getFunctionNameElement();
        if (typeRef != null) {
            PsiElement typeElement = typeRef.getFirstChild();
            if(typeElement instanceof LuaGlobalIdentifier) {
                //LuaGlobalIdentifier globalId = (LuaGlobalIdentifier) typeRef.getFirstChild();
                //LuaPrimitiveType luaPrimitiveType = (LuaPrimitiveType)globalId.getLuaType();
                return null;
            } else if(typeElement instanceof LuaCompoundIdentifier) {
                LuaCompoundIdentifier compoundId = (LuaCompoundIdentifier) typeRef.getFirstChild();
                String operator = compoundId.getOperator();
                if(operator == null || !operator.equals(".")) return null;
                typeRef = (LuaReferenceElement) compoundId.getLeftSymbol();
                methodRef = (LuaReferenceElement) compoundId.getLeftSymbol();
                String typeName = typeRef != null ? typeRef.getName() : null,
                       methodName = methodRef != null ? methodRef.getName() : null;
                if (typeName != null && methodName != null) {
                    StdLibraryType stdType = StdLibraryTypes.getInstance().getStdType(typeName);
                    if (stdType != null && stdType.hasStaticInstanceMethod(methodName)) {
                        oResult = new StdTypeStaticInstanceMethod(typeName, methodName, stdType);
                    }
                }
            }
        }
        return oResult;
    }

    /**
     * @return StdType instance
     */
    public final StdLibraryType getStdType() {
        return stdType;
    }

    /**
     * @return Name of type
     */
    public final String getTypeName() {
        return typeName;
    }

    /**
     * @return Name of method
     */
    public final String getMethodName() {
        return methodName;
    }
}

@@ -136,32 +136,171 @@
 erenceElement;%0D%0A
+import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpression;%0D%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaFieldIdentifier;%0D%0A
 import com.sylva
@@ -638,16 +638,97 @@
 veType;%0D
+%0Aimport com.sylvanaar.idea.Lua.lang.psi.types.LuaType;%0D%0A%0D%0Aimport java.util.List;%0D
 %0A%0D%0Apubli
@@ -1512,19 +1512,8 @@
 ment
- methodRef,
  typ
@@ -1606,20 +1606,24 @@
 Element 
-type
+compound
 Element 
@@ -1669,304 +1669,16 @@
  if(
-typeElement instanceof LuaGlobalIdentifier) %7B%0D%0A                //LuaGlobalIdentifier globalId = (LuaGlobalIdentifier) typeRef.getFirstChild();%0D%0A                //LuaPrimitiveType luaPrimitiveType = (LuaPrimitiveType)globalId.getLuaType();%0D%0A                return null;%0D%0A            %7D else if(type
+compound
 Elem
@@ -1793,39 +1793,31 @@
 tifier) 
-typeRef.getFirstChild()
+compoundElement
 ;%0D%0A     
@@ -2045,68 +2045,285 @@
     
-methodRef = (LuaReferenceElement) compoundId.getLeftSymbol()
+LuaExpression fieldExpression = compoundId.getRightSymbol();%0D%0A                if(fieldExpression == null %7C%7C !(fieldExpression instanceof LuaFieldIdentifier))%0D%0A                    return null;%0D%0A%0D%0A                //LuaFieldIdentifier fieldId = (LuaFieldIdentifier)fieldExpression
 ;%0D%0A 
@@ -2423,29 +2423,28 @@
         
-metho
+fiel
 dName = 
 methodRe
@@ -2439,37 +2439,23 @@
 e = 
-methodRef != null ? methodRef
+fieldExpression
 .get
@@ -2460,23 +2460,16 @@
 etName()
- : null
 ;%0D%0A     
@@ -2503,21 +2503,20 @@
 null && 
-metho
+fiel
 dName !=
@@ -2698,21 +2698,20 @@
 eMethod(
-metho
+fiel
 dName)) 
@@ -2789,21 +2789,20 @@
 peName, 
-metho
+fiel
 dName, s

Fix UseInstanceMethodCallIntention