package com.jetbrains.python.psi.search;

import com.intellij.openapi.application.QueryExecutorBase;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDirectory;
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.django.lang.template.psi.impl.DjangoTemplateFileImpl;
import com.jetbrains.django.util.PythonUtil;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.psi.PyElement;
import org.jetbrains.annotations.NotNull;

/**
 * @author traff
 */
public class PyStringReferenceSearch extends QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters> {
  public void processQuery(@NotNull final ReferencesSearch.SearchParameters params,
                           @NotNull final Processor<PsiReference> consumer) {
    final PsiElement element = params.getElementToSearch();
    if (!(element instanceof PyElement) && !(element instanceof PsiDirectory) && !(element instanceof DjangoTemplateFileImpl)) {
      return;
    }

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

    final String name = PythonUtil.computeElementNameForStringSearch(element);

    if (StringUtil.isEmpty(name)) {
      return;
    }
    params.getOptimizer().searchWord(name, searchScope, UsageSearchContext.IN_STRINGS, true, element);
  }
}

@@ -35,16 +35,129 @@
 earch;%0A%0A
+import com.intellij.openapi.application.AccessToken;%0Aimport com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -1373,27 +1373,158 @@
  %7D%0A%0A    
-SearchScope
+AccessToken token = ApplicationManager.getApplication().acquireReadActionLock();%0A    String name;%0A    SearchScope searchScope;%0A    try %7B%0A     
  searchS
@@ -1560,24 +1560,26 @@
 rchScope();%0A
+  
     if (sear
@@ -1614,24 +1614,26 @@
 rchScope) %7B%0A
+  
       search
@@ -1747,18 +1747,13 @@
 ANCE
+);
 %0A  
-    );%0A
     
@@ -1763,20 +1763,9 @@
     
-final String
+ 
  nam
@@ -1822,16 +1822,64 @@
 lement);
+%0A    %7D%0A    finally %7B%0A      token.finish();%0A    %7D
 %0A%0A    if

read action