<commit_before>package org.jetbrains.plugins.scala
package lang
package resolve

import processor.BaseProcessor
import psi.api.base._
import psi.api.expr._
import psi.api.toplevel.ScTypedDefinition
import psi.api.toplevel.templates.{ScTemplateBody, ScExtendsBlock}
import psi.api.toplevel.typedef.{ScObject, ScTrait, ScTypeDefinition, ScClass}
import psi.api.toplevel.packaging.ScPackaging
import psi.ScalaPsiUtil
import psi.types._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports._
import com.intellij.psi._
import com.intellij.psi.PsiElement
import result.TypingContext
import com.intellij.openapi.progress.ProgressManager
import util.{PsiModificationTracker, PsiTreeUtil}
import caches.CachesUtil
import psi.api.{ScPackage, ScalaFile}
import psi.impl.ScalaPsiManager
import scaladoc.psi.api.ScDocResolvableCodeReference
import extensions.{toPsiNamedElementExt, toPsiClassExt}
import psi.api.base.types.ScTypeElement

trait ResolvableStableCodeReferenceElement extends ScStableCodeReferenceElement {
  private object Resolver extends StableCodeReferenceElementResolver(this, false, false, false)
  private object ResolverAllConstructors extends StableCodeReferenceElementResolver(this, false, true, false)
  private object NoConstructorResolver extends StableCodeReferenceElementResolver(this, false, false, true)
  private object ShapesResolver extends StableCodeReferenceElementResolver(this, true, false, false)
  private object ShapesResolverAllConstructors extends StableCodeReferenceElementResolver(this, true, true, false)

  def multiResolve(incomplete: Boolean): Array[ResolveResult] = {
//    ResolveCache.getInstance(getProject).resolveWithCaching(this, Resolver, true, incomplete)
    CachesUtil.getMappedWithRecursionPreventingWithRollback[ResolvableStableCodeReferenceElement, Boolean, Array[ResolveResult]](
      this, incomplete, CachesUtil.RESOLVE_KEY, Resolver.resolve, Array.empty, PsiModificationTracker.MODIFICATION_COUNT)
  }

  protected def processQualifierResolveResult(res: ResolveResult, processor: BaseProcessor, ref: ScStableCodeReferenceElement) {
    res match {
      case ScalaResolveResult(td: ScTypeDefinition, substitutor) => {
        td match {
          case _: ScObject =>
            td.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, substitutor),
              null, ResolvableStableCodeReferenceElement.this)
          case _: ScClass | _: ScTrait => td.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, substitutor),
            null, ResolvableStableCodeReferenceElement.this)
        }
      }
      case ScalaResolveResult(typed: ScTypedDefinition, s) =>
        processor.processType(s.subst(typed.getType(TypingContext.empty).getOrAny), this)
      case ScalaResolveResult(field: PsiField, s) =>
        processor.processType(s.subst(ScType.create(field.getType, getProject, getResolveScope)), this)
      case ScalaResolveResult(clazz: PsiClass, s) => {
        processor.processType(new ScDesignatorType(clazz, true), this) //static Java import
      }
      case ScalaResolveResult(pack: ScPackage, s) =>
        pack.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, s),
          null, ResolvableStableCodeReferenceElement.this)
      case other: ScalaResolveResult => {
        other.element.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, other.substitutor),
          null, ResolvableStableCodeReferenceElement.this)
      }
      case _ =>
    }
  }

  def doResolve(ref: ScStableCodeReferenceElement, processor: BaseProcessor,
                accessibilityCheck: Boolean = true): Array[ResolveResult] = {
    if (!accessibilityCheck) processor.doNotCheckAccessibility()
    var x = false
    //performance improvement
    ScalaPsiUtil.fileContext(ref) match {
      case s: ScalaFile if s.isCompiled =>
        x = true
        //todo: improve checking for this and super
        val refText: String = ref.getText
        if (refText.contains("this") || refText.contains("super") || !refText.contains(".")) {} //do nothing
        else {
          //so this is full qualified reference => findClass, or findPackage
          val facade = JavaPsiFacade.getInstance(getProject)
          val manager = ScalaPsiManager.instance(getProject)
          val classes = manager.getCachedClasses(ref.getResolveScope, refText)
          val pack = facade.findPackage(refText)
          if (pack != null) processor.execute(pack, ResolveState.initial)
          for (clazz <- classes) processor.execute(clazz, ResolveState.initial)
          val candidates = processor.candidatesS
          val filtered = candidates.filter(candidatesFilter)
<commit_msg>Reduced imports list resolve stack (+Nika)<commit_after>package org.jetbrains.plugins.scala
package lang
package resolve

import processor.BaseProcessor
import psi.api.base._
import psi.api.expr._
import psi.api.toplevel.ScTypedDefinition
import psi.api.toplevel.templates.{ScTemplateBody, ScExtendsBlock}
import psi.api.toplevel.typedef.{ScObject, ScTrait, ScTypeDefinition, ScClass}
import psi.api.toplevel.packaging.ScPackaging
import psi.{ScImportsHolder, ScalaPsiUtil}
import psi.types._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports._
import com.intellij.psi._
import com.intellij.psi.PsiElement
import result.TypingContext
import com.intellij.openapi.progress.ProgressManager
import util.{PsiModificationTracker, PsiTreeUtil}
import caches.CachesUtil
import psi.api.{ScPackage, ScalaFile}
import psi.impl.ScalaPsiManager
import scaladoc.psi.api.ScDocResolvableCodeReference
import extensions.{toPsiNamedElementExt, toPsiClassExt}
import psi.api.base.types.ScTypeElement

trait ResolvableStableCodeReferenceElement extends ScStableCodeReferenceElement {
  private object Resolver extends StableCodeReferenceElementResolver(this, false, false, false)
  private object ResolverAllConstructors extends StableCodeReferenceElementResolver(this, false, true, false)
  private object NoConstructorResolver extends StableCodeReferenceElementResolver(this, false, false, true)
  private object ShapesResolver extends StableCodeReferenceElementResolver(this, true, false, false)
  private object ShapesResolverAllConstructors extends StableCodeReferenceElementResolver(this, true, true, false)

  def multiResolve(incomplete: Boolean): Array[ResolveResult] = {
//    ResolveCache.getInstance(getProject).resolveWithCaching(this, Resolver, true, incomplete)
    CachesUtil.getMappedWithRecursionPreventingWithRollback[ResolvableStableCodeReferenceElement, Boolean, Array[ResolveResult]](
      this, incomplete, CachesUtil.RESOLVE_KEY, Resolver.resolve, Array.empty, PsiModificationTracker.MODIFICATION_COUNT)
  }

  protected def processQualifierResolveResult(res: ResolveResult, processor: BaseProcessor, ref: ScStableCodeReferenceElement) {
    res match {
      case ScalaResolveResult(td: ScTypeDefinition, substitutor) => {
        td match {
          case _: ScObject =>
            td.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, substitutor),
              null, ResolvableStableCodeReferenceElement.this)
          case _: ScClass | _: ScTrait => td.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, substitutor),
            null, ResolvableStableCodeReferenceElement.this)
        }
      }
      case ScalaResolveResult(typed: ScTypedDefinition, s) =>
        processor.processType(s.subst(typed.getType(TypingContext.empty).getOrAny), this)
      case ScalaResolveResult(field: PsiField, s) =>
        processor.processType(s.subst(ScType.create(field.getType, getProject, getResolveScope)), this)
      case ScalaResolveResult(clazz: PsiClass, s) => {
        processor.processType(new ScDesignatorType(clazz, true), this) //static Java import
      }
      case ScalaResolveResult(pack: ScPackage, s) =>
        pack.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, s),
          null, ResolvableStableCodeReferenceElement.this)
      case other: ScalaResolveResult => {
        other.element.processDeclarations(processor, ResolveState.initial.put(ScSubstitutor.key, other.substitutor),
          null, ResolvableStableCodeReferenceElement.this)
      }
      case _ =>
    }
  }

  def doResolve(ref: ScStableCodeReferenceElement, processor: BaseProcessor,
                accessibilityCheck: Boolean = true): Array[ResolveResult] = {
    val importStmt = PsiTreeUtil.getContextOfType(ref, true, classOf[ScImportStmt])
    if (importStmt != null) {
      val importHolder = PsiTreeUtil.getContextOfType(importStmt, true, classOf[ScImportsHolder])
      if (importHolder != null) {
        importHolder.getImportStatements.takeWhile(_ != importStmt).foreach {
          case stmt: ScImportStmt =>
            stmt.importExprs.foreach {
              case expr: ScImportExpr => expr.reference match {
                case Some(ref) => ref.resolve()
                case None => expr.qualifier.resolve()
              }
            }
        }
      }
    }
    if (!accessibilityCheck) processor.doNotCheckAccessibility()
    var x = false
    //performance improvement
    ScalaPsiUtil.fileContext(ref) match {
      case s: ScalaFile if s.isCompiled =>
        x = true
        //todo: improve checking for this and super
        val refText: String = ref.getText
        if (refText.contains("this") || refText.contains("super") || !refText.contains(".")) {} //do nothing
        else {
          //so this is full qualified reference => findClass, or findPackage
          val facade = JavaPsiFacade.getInstance(getProject)
          val manager = ScalaPsiManager.instance(getProject)
          val classes = manager.getCachedClasses(ref.getResolveScope, refText)
          val pack = facade.findPackage(refText)
          if (pack != null) processor.execute(pack, ResolveState.initial)
          for (clazz <- classes) processor.execute(clazz, ResolveState.initial)
          val candidates = processor.candidatesS
          val filtered = candidates.filter(candidatesFilter)

{'fraction_non_alphanumeric': 0.08746787902747578, 'fraction_numerical': 0.0, 'mean_word_length': 4.215979381443299, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '6276672', 'n_tokens_mistral': 2874, 'n_tokens_neox': 2867, 'n_words': 583}