<commit_before>package org.jetbrains.sbt
package language

import com.intellij.psi.{PsiClass, PsiElement, ResolveState, FileViewProvider}
import org.jetbrains.plugins.scala.lang.psi.impl.{ScalaPsiElementFactory, ScalaPsiManager, ScalaFileImpl}
import com.intellij.psi.scope.PsiScopeProcessor
import com.intellij.openapi.module.{Module, ModuleManager, ModuleUtilCore}
import org.jetbrains.plugins.scala.lang.psi.ScDeclarationSequenceHolder
import com.intellij.psi.search.searches.ClassInheritorsSearch
import org.jetbrains.plugins.scala.extensions.toPsiClassExt
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.sbt.project.module.SbtModule
import collection.JavaConverters._

/**
 * @author <NAME>
 */
class SbtFileImpl(provider: FileViewProvider) extends ScalaFileImpl(provider, SbtFileType) with ScDeclarationSequenceHolder{
  override def isScriptFile(withCashing: Boolean) = false

  override def immediateTypeDefinitions = Seq.empty

  override def packagings = Seq.empty

  override def processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement, place: PsiElement): Boolean = 
    super[ScalaFileImpl].processDeclarations(processor, state, lastParent, place) && 
    super[ScDeclarationSequenceHolder].processDeclarations(processor, state, lastParent, place) &&
    processImplicitImports(processor, state, lastParent,place)

  private def processImplicitImports(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement, place: PsiElement): Boolean = {
    val expressions = implicitImportExpressions ++ localObjectsWithDefinitions.map(_.qualifiedName + "._")

    expressions.isEmpty || {
      val code = s"import ${expressions.mkString(", ")};"
      val file = ScalaPsiElementFactory.parseFile(code, getManager)
      file.processDeclarations(processor, state, file.lastChild.get, place)
    }
  }

  private def implicitImportExpressions = projectDefinitionModule.orElse(fileModule)
          .fold(Seq.empty[String])(SbtModule.getImportsFrom)

  private def localObjectsWithDefinitions: Seq[PsiClass] = {
    projectDefinitionModule.fold(Seq.empty[PsiClass]) { module =>
      val manager = ScalaPsiManager.instance(getProject)

      val moduleScope = module.getModuleScope
      val moduleWithLibrariesScope = module.getModuleWithLibrariesScope

      Sbt.DefinitionHolderClasses.flatMap(manager.getCachedClasses(moduleWithLibrariesScope, _))
              .flatMap(ClassInheritorsSearch.search(_, moduleScope, true).findAll.asScala)
    }
  }

  override def getFileResolveScope: GlobalSearchScope =
    projectDefinitionModule.fold(super.getFileResolveScope)(_.getModuleWithLibrariesScope)

  private def projectDefinitionModule: Option[Module] = fileModule.flatMap { module =>
    Option(ModuleManager.getInstance(getProject).findModuleByName(module.getName + Sbt.BuildModuleSuffix))
  }<commit_msg>SbtFileImpl: properly resolve implicit imports (workaround) (cherry picked from commit 480c58b)<commit_after>package org.jetbrains.sbt
package language

import com.intellij.psi.{PsiClass, PsiElement, ResolveState, FileViewProvider}
import org.jetbrains.plugins.scala.lang.psi.impl.{ScalaPsiElementFactory, ScalaPsiManager, ScalaFileImpl}
import com.intellij.psi.scope.PsiScopeProcessor
import com.intellij.openapi.module.{Module, ModuleManager, ModuleUtilCore}
import org.jetbrains.plugins.scala.lang.psi.ScDeclarationSequenceHolder
import com.intellij.psi.search.searches.ClassInheritorsSearch
import org.jetbrains.plugins.scala.extensions.toPsiClassExt
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.sbt.project.module.SbtModule
import collection.JavaConverters._

/**
 * @author <NAME>
 */
class SbtFileImpl(provider: FileViewProvider) extends ScalaFileImpl(provider, SbtFileType) with ScDeclarationSequenceHolder{
  override def isScriptFile(withCashing: Boolean) = false

  override def immediateTypeDefinitions = Seq.empty

  override def packagings = Seq.empty

  override def processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement, place: PsiElement): Boolean = 
    super[ScalaFileImpl].processDeclarations(processor, state, lastParent, place) &&
    super[ScDeclarationSequenceHolder].processDeclarations(processor, state, lastParent, place) &&
    processImplicitImports(processor, state, lastParent,place)

  private def processImplicitImports(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement, place: PsiElement): Boolean = {
    val expressions = implicitImportExpressions ++ localObjectsWithDefinitions.map(_.qualifiedName + "._")

    // TODO this is a workaround, we need to find out why references stopped resolving via the chained imports
    val expressions0 = expressions.map {
      case "Keys._" => "sbt.Keys._"
      case "Build._" => "sbt.Build._"
      case it => it
    }

    expressions0.isEmpty || {
      val code = s"import ${expressions0.mkString(", ")};"
      val file = ScalaPsiElementFactory.parseFile(code, getManager)
      file.processDeclarations(processor, state, file.lastChild.get, place)
    }
  }

  private def implicitImportExpressions = projectDefinitionModule.orElse(fileModule)
          .fold(Seq.empty[String])(SbtModule.getImportsFrom)

  private def localObjectsWithDefinitions: Seq[PsiClass] = {
    projectDefinitionModule.fold(Seq.empty[PsiClass]) { module =>
      val manager = ScalaPsiManager.instance(getProject)

      val moduleScope = module.getModuleScope
      val moduleWithLibrariesScope = module.getModuleWithLibrariesScope

      Sbt.DefinitionHolderClasses.flatMap(manager.getCachedClasses(moduleWithLibrariesScope, _))
              .flatMap(ClassInheritorsSearch.search(_, moduleScope, true).findAll.asScala)
    }
  }

  override def getFileResolveScope: GlobalSearchScope =
    projectDefinitionModule.fold(super.getFileResolveScope)(_.getModuleWithLibrariesScope)

  private def projectDefinitionModule: Option[Module] = fileModule.flatMap { module =>
    Option(ModuleManager.getInstance(getProject).findModuleByName(module.getName + Sbt.BuildModuleSuffix))
  }
{'fraction_non_alphanumeric': 0.09093882891723912, 'fraction_numerical': 0.0013084723585214263, 'mean_word_length': 5.712403951701427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, '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': '531418', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1709, 'n_words': 322}