<commit_before>package org.jetbrains.plugins.scala
package lang
package psi
package compiled

import com.intellij.ide.highlighter.JavaClassFileType
import com.intellij.lang.{Language, LanguageParserDefinitions}
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.{FileViewProvider, PsiFile, PsiFileFactory, PsiManager, SingleRootFileViewProvider, compiled, stubs}
import com.intellij.util.indexing.FileContent
import org.jetbrains.plugins.scala.tasty.TastyFileType

final class ScClassFileDecompiler extends compiled.ClassFileDecompilers.Full {

  import ScClassFileDecompiler._

  override def accepts(file: VirtualFile): Boolean =
    isTasty(file) ||
      hasTasty(file) || // Accept .class files that have .tasty to subsequently hide them (to avoid .tasty + .class duplication).
      topLevelScalaClassFor(file).nonEmpty

  override def getStubBuilder: ScClsStubBuilder.type = ScClsStubBuilder

  override def createFileViewProvider(file: VirtualFile,
                                      manager: PsiManager,
                                      eventSystemEnabled: Boolean): FileViewProvider = {
    if (!isTasty(file) && hasTasty(file)) {
      new NonScalaClassFileViewProvider(manager, file, eventSystemEnabled, ScalaLanguage.INSTANCE)
    } else {
      createFileViewProviderImpl(manager, file, eventSystemEnabled, ScalaLanguage.INSTANCE)
    }
  }
}

object ScClassFileDecompiler {

  import DecompilationResult._

  object ScClsStubBuilder extends compiled.ClsStubBuilder {

    override val getStubVersion = 371

    override def buildFileStub(content: FileContent): stubs.PsiFileStubImpl[_ <: PsiFile] =
      decompiledScalaFile(content)
        .map((if (isTasty(content.getFile)) stub3Builder else stub2Builder).buildStubTree)
        .orNull

    private def stub2Builder =
      LanguageParserDefinitions.INSTANCE
        .forLanguage(ScalaLanguage.INSTANCE)
        .asInstanceOf[parser.ScalaParserDefinition]
        .getFileNodeType
        .getBuilder

    private def stub3Builder =
      LanguageParserDefinitions.INSTANCE
        .forLanguage(Scala3Language.INSTANCE)
        .asInstanceOf[parser.Scala3ParserDefinition]
        .getFileNodeType
        .getBuilder
  }

  private def decompiledScalaFile(content: FileContent): Option[PsiFile] = content.getFile match {
    case original if isTasty(content.getFile) || isTopLevelScalaClass(original) =>
      sourceNameAndText(original, content.getContent).map {
        case (sourceName, sourceText) => PsiFileFactory.getInstance(content.getProject).createFileFromText(
          sourceName,<commit_msg>getStubVersion = 372<commit_after>package org.jetbrains.plugins.scala
package lang
package psi
package compiled

import com.intellij.ide.highlighter.JavaClassFileType
import com.intellij.lang.{Language, LanguageParserDefinitions}
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.{FileViewProvider, PsiFile, PsiFileFactory, PsiManager, SingleRootFileViewProvider, compiled, stubs}
import com.intellij.util.indexing.FileContent
import org.jetbrains.plugins.scala.tasty.TastyFileType

final class ScClassFileDecompiler extends compiled.ClassFileDecompilers.Full {

  import ScClassFileDecompiler._

  override def accepts(file: VirtualFile): Boolean =
    isTasty(file) ||
      hasTasty(file) || // Accept .class files that have .tasty to subsequently hide them (to avoid .tasty + .class duplication).
      topLevelScalaClassFor(file).nonEmpty

  override def getStubBuilder: ScClsStubBuilder.type = ScClsStubBuilder

  override def createFileViewProvider(file: VirtualFile,
                                      manager: PsiManager,
                                      eventSystemEnabled: Boolean): FileViewProvider = {
    if (!isTasty(file) && hasTasty(file)) {
      new NonScalaClassFileViewProvider(manager, file, eventSystemEnabled, ScalaLanguage.INSTANCE)
    } else {
      createFileViewProviderImpl(manager, file, eventSystemEnabled, ScalaLanguage.INSTANCE)
    }
  }
}

object ScClassFileDecompiler {

  import DecompilationResult._

  object ScClsStubBuilder extends compiled.ClsStubBuilder {

    override val getStubVersion = 372

    override def buildFileStub(content: FileContent): stubs.PsiFileStubImpl[_ <: PsiFile] =
      decompiledScalaFile(content)
        .map((if (isTasty(content.getFile)) stub3Builder else stub2Builder).buildStubTree)
        .orNull

    private def stub2Builder =
      LanguageParserDefinitions.INSTANCE
        .forLanguage(ScalaLanguage.INSTANCE)
        .asInstanceOf[parser.ScalaParserDefinition]
        .getFileNodeType
        .getBuilder

    private def stub3Builder =
      LanguageParserDefinitions.INSTANCE
        .forLanguage(Scala3Language.INSTANCE)
        .asInstanceOf[parser.Scala3ParserDefinition]
        .getFileNodeType
        .getBuilder
  }

  private def decompiledScalaFile(content: FileContent): Option[PsiFile] = content.getFile match {
    case original if isTasty(content.getFile) || isTopLevelScalaClass(original) =>
      sourceNameAndText(original, content.getContent).map {
        case (sourceName, sourceText) => PsiFileFactory.getInstance(content.getProject).createFileFromText(
          sourceName,
{'fraction_non_alphanumeric': 0.0773511407902059, 'fraction_numerical': 0.0038953811908736783, 'mean_word_length': 3.9513314967860422, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1924024', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1509, 'n_words': 296}