package com.jetbrains.python.psi.search;

import com.intellij.openapi.application.QueryExecutorBase;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.UsageSearchContext;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.util.Processor;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
public class PyInitReferenceSearchExecutor extends QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters> {
  public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
    PsiElement element = queryParameters.getElementToSearch();
    if (!(element instanceof PyFunction)) {
      return;
    }
    final PyFunction function = (PyFunction)element;
    if (!PyNames.INIT.equals(function.getName())) {
      return;
    }
    final PyClass pyClass = function.getContainingClass();
    if (pyClass == null) {
      return;
    }
    final String className = pyClass.getName();
    if (className == null) {
      return;
    }

    SearchScope searchScope = queryParameters.getEffectiveSearchScope();
    if (searchScope instanceof GlobalSearchScope) {
      searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes((GlobalSearchScope)searchScope, PythonFileType.INSTANCE);
    }

    queryParameters.getOptimizer().searchWord(className, searchScope, UsageSearchContext.IN_CODE, true, function);
  }
}

@@ -35,16 +35,129 @@
 earch;%0A%0A
+import com.intellij.openapi.application.AccessToken;%0Aimport com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -1130,32 +1130,33 @@
   return;%0A    %7D%0A
+%0A
     final PyFunc
@@ -1149,26 +1149,194 @@
   final 
-PyFunction
+AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();%0A    String className;%0A    SearchScope searchScope;%0A    PyFunction function;%0A    try %7B%0A     
  functio
@@ -1360,16 +1360,18 @@
 lement;%0A
+  
     if (
@@ -1412,32 +1412,34 @@
 ame())) %7B%0A      
+  
 return;%0A    %7D%0A  
@@ -1426,34 +1426,38 @@
     return;%0A    
-%7D%0A
+  %7D%0A  
     final PyClas
@@ -1499,24 +1499,26 @@
 lass();%0A    
+  
 if (pyClass 
@@ -1526,32 +1526,34 @@
 = null) %7B%0A      
+ 
+ 
 return;%0A    %7D%0A  
@@ -1552,26 +1552,17 @@
     
+  
 %7D%0A    
-final String
+ 
  cla
@@ -1589,24 +1589,26 @@
 Name();%0A    
+  
 if (classNam
@@ -1618,32 +1618,34 @@
 = null) %7B%0A      
+ 
+ 
 return;%0A    %7D%0A%0A 
@@ -1644,26 +1644,18 @@
     
+  
 %7D%0A%0A    
-SearchScope
+ 
  sea
@@ -1708,24 +1708,26 @@
 cope();%0A    
+  
 if (searchSc
@@ -1758,24 +1758,26 @@
 rchScope) %7B%0A
+  
       search
@@ -1890,25 +1890,82 @@
 TANCE);%0A    
-%7D
+  %7D%0A    %7D%0A    finally %7B%0A      accessToken.finish();%0A    %7D%0A
 %0A%0A    queryP

read access