<commit_before>package org.jetbrains.plugins.scala
package lang
package psi
package stubs
package elements

import com.intellij.psi.impl.java.stubs.index.JavaStubIndexKeys
import com.intellij.psi.stubs.{IndexSink, StubElement, StubInputStream, StubOutputStream}
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.{PsiClass, PsiElement}
import com.intellij.util.io.StringRef.fromString
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScAnnotation
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScObject, ScTemplateDefinition, ScTypeDefinition}
import org.jetbrains.plugins.scala.lang.psi.stubs.impl.ScTemplateDefinitionStubImpl
import org.jetbrains.plugins.scala.lang.psi.stubs.index.ScalaIndexKeys
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil

/**
  * @author ilyas, alefas
  */
abstract class ScTemplateDefinitionElementType[TypeDef <: ScTemplateDefinition](debugName: String)
  extends ScStubElementType[ScTemplateDefinitionStub, ScTemplateDefinition](debugName) {

  override def serialize(stub: ScTemplateDefinitionStub, dataStream: StubOutputStream): Unit = {
    dataStream.writeName(stub.getName)
    dataStream.writeName(stub.getQualifiedName)
    dataStream.writeName(stub.javaQualifiedName)
    dataStream.writeBoolean(stub.isPackageObject)
    dataStream.writeBoolean(stub.isScriptFileClass)
    dataStream.writeName(stub.getSourceFileName)
    dataStream.writeBoolean(stub.isDeprecated)
    dataStream.writeBoolean(stub.isImplicitObject)
    dataStream.writeBoolean(stub.isImplicitClass)
    dataStream.writeName(stub.javaName)
    dataStream.writeNames(stub.additionalJavaNames)
    dataStream.writeBoolean(stub.isLocal)
    dataStream.writeBoolean(stub.isVisibleInJava)
  }


  override def deserialize(dataStream: StubInputStream, parentStub: StubElement[_ <: PsiElement]): ScTemplateDefinitionStub =
    new ScTemplateDefinitionStubImpl(parentStub, this,
      nameRef = dataStream.readName,
      qualifiedNameRef = dataStream.readName,
      javaQualifiedNameRef = dataStream.readName,
      isPackageObject = dataStream.readBoolean,
      isScriptFileClass = dataStream.readBoolean,
      sourceFileNameRef = dataStream.readName,
      isDeprecated = dataStream.readBoolean,
      isImplicitObject = dataStream.readBoolean,
      isImplicitClass = dataStream.readBoolean,
      javaNameRef = dataStream.readName,
      additionalJavaNamesRefs = dataStream.readNames,
      isLocal = dataStream.readBoolean,
      isVisibleInJava = dataStream.readBoolean)

  override def createStub(definition: ScTemplateDefinition, parent: StubElement[_ <: PsiElement]): ScTemplateDefinitionStub = {
    ScTemplateDefinitionElementType.isStubBuilding.set(true)
    val fileName = definition.containingVirtualFile.map {
      _.getName
    }.orNull

    val isPackageObject = definition match {
      case td: ScTypeDefinition => td.isPackageObject
      case _ => false
    }
    val isDeprecated = definition.isInstanceOf[ScTypeDefinition] && definition.getModifierList != null &&
      !definition.getModifierList.getAnnotations.forall {
        case a: ScAnnotation =>
          val typeText = a.constructor.typeElement.getText
          typeText != "deprecated" && typeText != "scala.deprecated"
        case _ => true
      }

    def isOkForJava(elem: ScalaPsiElement): Boolean = {
      var res = true
      var element = elem.getParent
      while (element != null && res) {
        element match {
          case o: ScObject if o.isPackageObject => res = false
          case _ =>
        }
        element = element.getParent
      }
      res
    }

    val isLocal = definition.containingClass == null &&
      PsiTreeUtil.getParentOfType(definition, classOf[ScTemplateDefinition]) != null

    val result = new ScTemplateDefinitionStubImpl(parent, this,
      isDotty = definition.getProject.hasDotty,
      nameRef = fromString(definition.name),
      qualifiedNameRef = fromString(definition.qualifiedName),
      javaQualifiedNameRef = fromString(definition.getQualifiedName),
      isPackageObject = isPackageObject,
      isScriptFileClass = definition.isScriptFileClass,
      sourceFileNameRef = fromString(fileName),
      isDeprecated = isDeprecated,
      isImplicitObject = definition.isInstanceOf[ScObject] && definition.hasModifierProperty("implicit"),
      isImplicitClass = definition.isInstanceOf[ScClass] && definition.hasModifierProperty("implicit"),
      javaNameRef = fromString(definition.getName),
      additionalJavaNamesRefs = definition.additionalJavaNames.asReferences,
      isLocal = isLocal,
      isVisibleInJava = isOkForJava(definition))
    ScTemplateDefinitionElementType.isStubBuilding.set(false)
    result
  }

  override def indexStub(stub: ScTemplateDefinitionStub, sink: IndexSink): Unit = {
    if (stub.isScriptFileClass) return
    val name = ScalaNamesUtil.cleanFqn(stub.getName)<commit_msg>rebase fix<commit_after>package org.jetbrains.plugins.scala
package lang
package psi
package stubs
package elements

import com.intellij.psi.impl.java.stubs.index.JavaStubIndexKeys
import com.intellij.psi.stubs.{IndexSink, StubElement, StubInputStream, StubOutputStream}
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.{PsiClass, PsiElement}
import com.intellij.util.io.StringRef.fromString
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScAnnotation
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScObject, ScTemplateDefinition, ScTypeDefinition}
import org.jetbrains.plugins.scala.lang.psi.stubs.impl.ScTemplateDefinitionStubImpl
import org.jetbrains.plugins.scala.lang.psi.stubs.index.ScalaIndexKeys
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil

/**
  * @author ilyas, alefas
  */
abstract class ScTemplateDefinitionElementType[TypeDef <: ScTemplateDefinition](debugName: String)
  extends ScStubElementType[ScTemplateDefinitionStub, ScTemplateDefinition](debugName) {

  override def serialize(stub: ScTemplateDefinitionStub, dataStream: StubOutputStream): Unit = {
    dataStream.writeName(stub.getName)
    dataStream.writeName(stub.getQualifiedName)
    dataStream.writeName(stub.javaQualifiedName)
    dataStream.writeBoolean(stub.isPackageObject)
    dataStream.writeBoolean(stub.isScriptFileClass)
    dataStream.writeName(stub.getSourceFileName)
    dataStream.writeBoolean(stub.isDeprecated)
    dataStream.writeBoolean(stub.isImplicitObject)
    dataStream.writeBoolean(stub.isImplicitClass)
    dataStream.writeName(stub.javaName)
    dataStream.writeNames(stub.additionalJavaNames)
    dataStream.writeBoolean(stub.isLocal)
    dataStream.writeBoolean(stub.isVisibleInJava)
  }


  override def deserialize(dataStream: StubInputStream, parentStub: StubElement[_ <: PsiElement]): ScTemplateDefinitionStub =
    new ScTemplateDefinitionStubImpl(parentStub, this,
      nameRef = dataStream.readName,
      qualifiedNameRef = dataStream.readName,
      javaQualifiedNameRef = dataStream.readName,
      isPackageObject = dataStream.readBoolean,
      isScriptFileClass = dataStream.readBoolean,
      sourceFileNameRef = dataStream.readName,
      isDeprecated = dataStream.readBoolean,
      isImplicitObject = dataStream.readBoolean,
      isImplicitClass = dataStream.readBoolean,
      javaNameRef = dataStream.readName,
      additionalJavaNamesRefs = dataStream.readNames,
      isLocal = dataStream.readBoolean,
      isVisibleInJava = dataStream.readBoolean)

  override def createStub(definition: ScTemplateDefinition, parent: StubElement[_ <: PsiElement]): ScTemplateDefinitionStub = {
    ScTemplateDefinitionElementType.isStubBuilding.set(true)
    val fileName = definition.containingVirtualFile.map {
      _.getName
    }.orNull

    val isPackageObject = definition match {
      case td: ScTypeDefinition => td.isPackageObject
      case _ => false
    }
    val isDeprecated = definition.isInstanceOf[ScTypeDefinition] && definition.getModifierList != null &&
      !definition.getModifierList.getAnnotations.forall {
        case a: ScAnnotation =>
          val typeText = a.constructor.typeElement.getText
          typeText != "deprecated" && typeText != "scala.deprecated"
        case _ => true
      }

    def isOkForJava(elem: ScalaPsiElement): Boolean = {
      var res = true
      var element = elem.getParent
      while (element != null && res) {
        element match {
          case o: ScObject if o.isPackageObject => res = false
          case _ =>
        }
        element = element.getParent
      }
      res
    }

    val isLocal = definition.containingClass == null &&
      PsiTreeUtil.getParentOfType(definition, classOf[ScTemplateDefinition]) != null

    val result = new ScTemplateDefinitionStubImpl(parent, this,
      nameRef = fromString(definition.name),
      qualifiedNameRef = fromString(definition.qualifiedName),
      javaQualifiedNameRef = fromString(definition.getQualifiedName),
      isPackageObject = isPackageObject,
      isScriptFileClass = definition.isScriptFileClass,
      sourceFileNameRef = fromString(fileName),
      isDeprecated = isDeprecated,
      isImplicitObject = definition.isInstanceOf[ScObject] && definition.hasModifierProperty("implicit"),
      isImplicitClass = definition.isInstanceOf[ScClass] && definition.hasModifierProperty("implicit"),
      javaNameRef = fromString(definition.getName),
      additionalJavaNamesRefs = definition.additionalJavaNames.asReferences,
      isLocal = isLocal,
      isVisibleInJava = isOkForJava(definition))
    ScTemplateDefinitionElementType.isStubBuilding.set(false)
    result
  }

  override def indexStub(stub: ScTemplateDefinitionStub, sink: IndexSink): Unit = {
    if (stub.isScriptFileClass) return
    val name = ScalaNamesUtil.cleanFqn(stub.getName)
{'fraction_non_alphanumeric': 0.08605191394808605, 'fraction_numerical': 0.0, 'mean_word_length': 4.40797378481704, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '2898069', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2808, 'n_words': 464}