<commit_before>package org.jetbrains.plugins.scala
package findUsages

import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.util.{CommonProcessors, QueryExecutor, Processor}
import com.intellij.psi.{PsiElement, PsiReference}
import com.intellij.psi.search.{GlobalSearchScope, UsageSearchContext, PsiSearchHelper, TextOccurenceProcessor}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.ScNamedElement
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil
import com.intellij.openapi.project.IndexNotReadyException
import com.intellij.psi.impl.PsiManagerEx
import com.intellij.psi.impl.search.PsiSearchHelperImpl
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.util.Condition
import com.intellij.openapi.roots.FileIndexFacade
import com.intellij.util.containers.ContainerUtil
import com.intellij.openapi.application.ReadActionProcessor
import java.util
import com.intellij.psi.impl.cache.impl.id.{IdIndex, IdIndexEntry}
import com.intellij.util.indexing.FileBasedIndex

/**
 * Nikolay.Tropin
 * 9/10/13
 */
class OperatorAndBacktickedSearcher extends QueryExecutor[PsiReference, ReferencesSearch.SearchParameters] {
  def execute(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor[PsiReference]): Boolean = {
    val scope = queryParameters.getEffectiveSearchScope
    val element = queryParameters.getElementToSearch
    extensions.inReadAction{
      if (!element.isValid) return true
      val toProcess = element match {
        case isBackticked(name) => Seq((element, name), (element, s"`$name`"))
        case named: ScNamedElement if named.name.exists(ScalaNamesUtil.isOpCharacter) => Seq((named, named.name))
        case _ => Nil
      }
      toProcess.foreach { case (elem, name) =>
        val processor = new TextOccurenceProcessor {
          def execute(element: PsiElement, offsetInElement: Int): Boolean = {
            val references = element.getReferences
            for (ref <- references if ref.getRangeInElement.contains(offsetInElement)) {
              if (ref.isReferenceTo(elem) || ref.resolve() == elem) {
                if (!consumer.process(ref)) return false
              }
            }
            true
          }
        }
        val helper: PsiSearchHelper = new ScalaPsiSearchHelper(elem.getManager.asInstanceOf[PsiManagerEx])
        try {
          helper.processElementsWithWord(processor, scope, name, UsageSearchContext.IN_CODE, true)
        }
        catch {
          case ignore: IndexNotReadyException =>
        }
      }
      true
    }
  }

  private val isBackticked = new {
    def unapply(named: ScNamedElement): Option[String] = {
      val name = named.name
      if (name.startsWith("`") && name.endsWith("`")) Some(name.substring(1, name.length - 1))
      else None
    }
  }

  private class ScalaPsiSearchHelper(manager: PsiManagerEx) extends PsiSearchHelperImpl(manager) {
    override def processFilesWithText(scope: GlobalSearchScope,<commit_msg>Small correction<commit_after>package org.jetbrains.plugins.scala
package findUsages

import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.util.{CommonProcessors, QueryExecutor, Processor}
import com.intellij.psi.{PsiElement, PsiReference}
import com.intellij.psi.search.{GlobalSearchScope, UsageSearchContext, PsiSearchHelper, TextOccurenceProcessor}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.ScNamedElement
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil
import com.intellij.openapi.project.IndexNotReadyException
import com.intellij.psi.impl.PsiManagerEx
import com.intellij.psi.impl.search.PsiSearchHelperImpl
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.util.Condition
import com.intellij.openapi.roots.FileIndexFacade
import com.intellij.util.containers.ContainerUtil
import com.intellij.openapi.application.ReadActionProcessor
import java.util
import com.intellij.psi.impl.cache.impl.id.{IdIndex, IdIndexEntry}
import com.intellij.util.indexing.FileBasedIndex

/**
 * Nikolay.Tropin
 * 9/10/13
 */
class OperatorAndBacktickedSearcher extends QueryExecutor[PsiReference, ReferencesSearch.SearchParameters] {
  def execute(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor[PsiReference]): Boolean = {
    val scope = queryParameters.getEffectiveSearchScope
    val element = queryParameters.getElementToSearch
    extensions.inReadAction{
      if (!element.isValid) return true
      val toProcess = element match {
        case isBackticked(name) => Seq((element, name), (element, s"`$name`"))
        case named: ScNamedElement if named.name.exists(ScalaNamesUtil.isOpCharacter) => Seq((named, named.name))
        case _ => Nil
      }
      toProcess.foreach { case (elem, name) =>
        val processor = new TextOccurenceProcessor {
          def execute(element: PsiElement, offsetInElement: Int): Boolean = {
            val references = element.getReferences
            for (ref <- references if ref.getRangeInElement.contains(offsetInElement)) {
              if (ref.isReferenceTo(elem) || ref.resolve() == elem) {
                if (!consumer.process(ref)) return false
              }
            }
            true
          }
        }
        val helper: PsiSearchHelper = new ScalaPsiSearchHelper(elem.getManager.asInstanceOf[PsiManagerEx])
        try {
          helper.processElementsWithWord(processor, scope, name, UsageSearchContext.IN_CODE, true)
        }
        catch {
          case ignore: IndexNotReadyException =>
        }
      }
      true
    }
  }

  private object isBackticked {
    def unapply(named: ScNamedElement): Option[String] = {
      val name = named.name
      if (name.startsWith("`") && name.endsWith("`")) Some(name.substring(1, name.length - 1))
      else None
    }
  }

  private class ScalaPsiSearchHelper(manager: PsiManagerEx) extends PsiSearchHelperImpl(manager) {
    override def processFilesWithText(scope: GlobalSearchScope,
{'fraction_non_alphanumeric': 0.09720837487537388, 'fraction_numerical': 0.002326354270521768, 'mean_word_length': 3.731918238993711, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1466944', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1729, 'n_words': 327}