package com.wishtack.pysynthetic.impl;

import com.intellij.psi.PsiElement;
import com.jetbrains.python.codeInsight.PyCustomMember;
import com.jetbrains.python.psi.types.PyClassMembersProviderBase;
import com.jetbrains.python.psi.types.PyClassType;
import com.jetbrains.python.psi.types.PyClassTypeImpl;
import com.jetbrains.python.psi.types.TypeEvalContext;
import com.wishtack.pysynthetic.SyntheticTypeInfo;
import com.wishtack.pysynthetic.SyntheticTypeInfoReader;
import com.wishtack.pysynthetic.psi.AbstractAccessor;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

/**
 * Created by Jean Hominal on 2016-11-01
 */
public class SyntheticMembersProvider extends PyClassMembersProviderBase {

    // HACK: Inspect stack trace to return empty collection if doing code completion.
    private static boolean isGettingCompletionVariants() {
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        for (int i = 2; i < stackTrace.length; i++) {
            StackTraceElement current = stackTrace[i];
            if (!PyClassTypeImpl.class.getName().equals(current.getClassName())) {
                return false;
            } else if ("getCompletionVariants".equals(current.getMethodName())) {
                return true;
            }
        }
        return false;
    }

    @NotNull
    @Override
    public Collection<PyCustomMember> getMembers(PyClassType clazz, PsiElement location, TypeEvalContext typeEvalContext) {
        if (isGettingCompletionVariants()) {
            return Collections.emptyList();
        } else {
            SyntheticTypeInfo typeInfo = new SyntheticTypeInfoReader(clazz.getPyClass()).read();
            return typeInfo.getResolveMap().entrySet().stream()
                    .map(e -> computeMember(e.getKey(), e.getValue(), clazz))
                    .collect(Collectors.toList());
        }
    }

    private static PyCustomMember computeMember(String name, PsiElement element, PyClassType clazz) {
        PyCustomMember customMember = new PyCustomMember(name, element, clazz.getName());
        if (element instanceof AbstractAccessor) {
            customMember.asFunction();
        }
        return customMember;
    }

    @Override
    public PsiElement resolveMember(PyClassType clazz, String name, PsiElement location, TypeEvalContext context) {
        SyntheticTypeInfo typeInfo = new SyntheticTypeInfoReader(clazz.getPyClass()).read();
        return typeInfo.getResolveMap().get(name);
    }
}

@@ -121,24 +121,82 @@
 stomMember;%0A
+import com.jetbrains.python.psi.resolve.PyResolveContext;%0A
 import com.j
@@ -2383,16 +2383,25 @@
 eMember(
+@NotNull 
 PyClassT
@@ -2404,22 +2404,30 @@
 assType 
-clazz,
+type, @NotNull
  String 
@@ -2457,25 +2457,42 @@
 on, 
-TypeEvalContext c
+@NotNull PyResolveContext resolveC
 onte
@@ -2554,37 +2554,36 @@
 cTypeInfoReader(
-clazz
+type
 .getPyClass()).r

Replace deprecated PyClassMembersProviderBase.resolveMember override.