<commit_before>package org.jetbrains.plugins.scala.lang.psi.impl.toplevel.typedef

/**
 * @author ilyas
 */

import com.intellij.lang.ASTNode
import com.intellij.psi._
import com.intellij.psi.tree._
import com.intellij.navigation.NavigationItem
import com.intellij.openapi.editor.colors._
import org.jetbrains.plugins.scala.lang.parser._
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement
import org.jetbrains.plugins.scala.lang.lexer._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef._
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.{ScTypeParamClause, ScTypeParam}
import psi.api.toplevel.packaging._
import psi.api.toplevel.templates._
import org.jetbrains.plugins.scala.icons.Icons
import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode
import com.intellij.navigation._
import com.intellij.psi.javadoc.PsiDocComment
import com.intellij.util.IncorrectOperationException
import com.intellij.util.IconUtil
import com.intellij.psi.impl._
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.VisibilityIcons
import com.intellij.openapi.util.Iconable
import javax.swing.Icon
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction

abstract class ScTypeDefinitionImpl(node: ASTNode) extends ScalaPsiElementImpl(node) with ScTypeDefinition with PsiClassFake {
  def nameId() = findChildByType(ScalaTokenTypes.tIDENTIFIER)

  override def getQualifiedName: String = {
    def _packageName(e: PsiElement): String = e.getParent match {
      case t: ScTypeDefinition => _packageName(t) + "." + t.name
      case p: ScPackaging => _packageName(p) + "." + p.getPackageName
      case f: ScalaFile => f.getPackageName
      case null => ""
      case parent => _packageName(parent)
    }
    val packageName = _packageName(this)
    if (packageName.length > 0) packageName + "." + name else name
  }

  override def getPresentation(): ItemPresentation = {
    new ItemPresentation() {
      def getPresentableText(): String = name
      def getTextAttributesKey(): TextAttributesKey = null
      def getLocationString(): String = getPath match {
        case "" => "<default>"
        case p => '(' + p + ')'
      }
      override def getIcon(open: Boolean) = ScTypeDefinitionImpl.this.getIcon(0)
    }
  }

  protected def getIconInner: Icon

  override def getIcon(flags: Int): Icon = {
    if (!isValid) return null
    val icon = getIconInner
    val isLocked = (flags & Iconable.ICON_FLAG_READ_STATUS) != 0 && !isWritable()
    val rowIcon = ElementBase.createLayeredIcon(icon, ElementPresentationUtil.getFlags(this, isLocked))
    if ((flags & Iconable.ICON_FLAG_VISIBILITY) != 0) {
      VisibilityIcons.setVisibilityIcon(getModifierList, rowIcon);
    }
    rowIcon
  }

  override def findMethodsByName(name: String, checkBases: Boolean): Array[PsiMethod] = functions.filter((m: PsiMethod) =>
          m.getName == name // todo check base classes
  ).toArray

  def extendsBlock: ScExtendsBlock = findChildByClass(classOf[ScExtendsBlock])

  def members(): Seq[ScMember] =
    (extendsBlock.templateBody match {
      case None => Seq.empty
      case Some(body) => body.members
    }) ++ findChildrenByClass(classOf[ScMember])

  def functions(): Seq[ScFunction] =
    (extendsBlock.templateBody match {
      case None => Seq.empty
      case Some(body) => body.functions
    }) ++ findChildrenByClass(classOf[ScFunction])

  def typeDefinitions: Seq[ScTypeDefinition] =
    (extendsBlock.templateBody match {
      case None => Seq.empty
      case Some(body) => body.typeDefinitions
    })

  override def delete() = {
    var parent = getParent
    var remove: PsiElement = this
    while (parent.isInstanceOf[ScPackaging]) {
      remove = parent
      parent = parent.getParent
    }
    parent match {
      case f: ScalaFile => {
        if (f.getTypeDefinitions.length == 1) {
          f.delete
        } else {
          f.getNode.removeChild(remove.getNode)
        }
      }
      case e: ScalaPsiElement => e.getNode.removeChild(remove.getNode)
      case _ => throw new IncorrectOperationException("Invalid type definition")
    }
  }

  override def getTypeParameters = typeParameters.toArray

  override def getMethods = functions.toArray

  def superTypes() = extendsBlock.superTypes

  import com.intellij.psi.scope.{PsiScopeProcessor, ElementClassHint}

  override def processDeclarations(processor: PsiScopeProcessor,
                                  state: ResolveState,
                                  lastParent: PsiElement,
                                  place: PsiElement): Boolean = {
    extendsBlock.templateParents match {
      case Some(p) if (PsiTreeUtil.isAncestor(p, place, true)) => true
      case _ =>
        val maps = TypeDefinitionMembers.getMembers(this)
        val classHint = processor.getHint(classOf[ElementClassHint])
        if (classHint == null || classHint.shouldProcess(classOf[PsiVariable])) {
          for ((v, _) <- maps._1) {
            if (!processor.execute(v, state)) return false
          }
        }
        if (classHint == null || classHint.shouldProcess(classOf[PsiMethod])) {
          for ((m, _) <- maps._2) {
            if (!processor.execute(m.method, state)) return false
          }
        }
        if (classHint == null || classHint.shouldProcess(classOf[PsiClass])) {
          for ((t, _) <- maps._3) {
            if (!processor.execute(t, state)) return false
          }
        }
        true
    }
  }
}<commit_msg>temp rollback<commit_after>package org.jetbrains.plugins.scala.lang.psi.impl.toplevel.typedef

/**
 * @author ilyas
 */

import com.intellij.lang.ASTNode
import com.intellij.psi._
import com.intellij.psi.tree._
import com.intellij.navigation.NavigationItem
import com.intellij.openapi.editor.colors._
import org.jetbrains.plugins.scala.lang.parser._
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement
import org.jetbrains.plugins.scala.lang.lexer._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef._
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.{ScTypeParamClause, ScTypeParam}
import psi.api.toplevel.packaging._
import psi.api.toplevel.templates._
import org.jetbrains.plugins.scala.icons.Icons
import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode
import com.intellij.navigation._
import com.intellij.psi.javadoc.PsiDocComment
import com.intellij.util.IncorrectOperationException
import com.intellij.util.IconUtil
import com.intellij.psi.impl._
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.VisibilityIcons
import com.intellij.openapi.util.Iconable
import javax.swing.Icon
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction

abstract class ScTypeDefinitionImpl(node: ASTNode) extends ScalaPsiElementImpl(node) with ScTypeDefinition with PsiClassFake {
  def nameId() = findChildByType(ScalaTokenTypes.tIDENTIFIER)

  override def getQualifiedName: String = {
    def _packageName(e: PsiElement): String = e.getParent match {
      case t: ScTypeDefinition => _packageName(t) + "." + t.name
      case p: ScPackaging => _packageName(p) + "." + p.getPackageName
      case f: ScalaFile => f.getPackageName
      case null => ""
      case parent => _packageName(parent)
    }
    val packageName = _packageName(this)
    if (packageName.length > 0) packageName + "." + name else name
  }

  override def getPresentation(): ItemPresentation = {
    new ItemPresentation() {
      def getPresentableText(): String = name
      def getTextAttributesKey(): TextAttributesKey = null
      def getLocationString(): String = getPath match {
        case "" => "<default>"
        case p => '(' + p + ')'
      }
      override def getIcon(open: Boolean) = ScTypeDefinitionImpl.this.getIcon(0)
    }
  }

  protected def getIconInner: Icon

  override def getIcon(flags: Int): Icon = {
    if (!isValid) return null
    val icon = getIconInner
    val isLocked = (flags & Iconable.ICON_FLAG_READ_STATUS) != 0 && !isWritable()
    val rowIcon = ElementBase.createLayeredIcon(icon, ElementPresentationUtil.getFlags(this, isLocked))
    if ((flags & Iconable.ICON_FLAG_VISIBILITY) != 0) {
      VisibilityIcons.setVisibilityIcon(getModifierList, rowIcon);
    }
    rowIcon
  }

  override def findMethodsByName(name: String, checkBases: Boolean): Array[PsiMethod] = functions.filter((m: PsiMethod) =>
          m.getName == name // todo check base classes
  ).toArray

  def extendsBlock: ScExtendsBlock = findChildByClass(classOf[ScExtendsBlock])

  def members(): Seq[ScMember] =
    (extendsBlock.templateBody match {
      case None => Seq.empty
      case Some(body) => body.members
    }) ++ findChildrenByClass(classOf[ScMember])

  def functions(): Seq[ScFunction] =
    (extendsBlock.templateBody match {
      case None => Seq.empty
      case Some(body) => body.functions
    }) ++ findChildrenByClass(classOf[ScFunction])

  def typeDefinitions: Seq[ScTypeDefinition] =
    (extendsBlock.templateBody match {
      case None => Seq.empty
      case Some(body) => body.typeDefinitions
    })

  override def delete() = {
    var parent = getParent
    var remove: PsiElement = this
    while (parent.isInstanceOf[ScPackaging]) {
      remove = parent
      parent = parent.getParent
    }
    parent match {
      case f: ScalaFile => {
        if (f.getTypeDefinitions.length == 1) {
          f.delete
        } else {
          f.getNode.removeChild(remove.getNode)
        }
      }
      case e: ScalaPsiElement => e.getNode.removeChild(remove.getNode)
      case _ => throw new IncorrectOperationException("Invalid type definition")
    }
  }

  override def getTypeParameters = typeParameters.toArray

  override def getMethods = functions.toArray

  def superTypes() = extendsBlock.superTypes

  import com.intellij.psi.scope.{PsiScopeProcessor, ElementClassHint}

/*
  override def processDeclarations(processor: PsiScopeProcessor,
                                  state: ResolveState,
                                  lastParent: PsiElement,
                                  place: PsiElement): Boolean = {
    extendsBlock.templateParents match {
      case Some(p) if (PsiTreeUtil.isAncestor(p, place, true)) => true
      case _ =>
        val maps = TypeDefinitionMembers.getMembers(this)
        val classHint = processor.getHint(classOf[ElementClassHint])
        if (classHint == null || classHint.shouldProcess(classOf[PsiVariable])) {
          for ((v, _) <- maps._1) {
            if (!processor.execute(v, state)) return false
          }
        }
        if (classHint == null || classHint.shouldProcess(classOf[PsiMethod])) {
          for ((m, _) <- maps._2) {
            if (!processor.execute(m.method, state)) return false
          }
        }
        if (classHint == null || classHint.shouldProcess(classOf[PsiClass])) {
          for ((t, _) <- maps._3) {
            if (!processor.execute(t, state)) return false
          }
        }
        true
    }
  }
*/
}
{'fraction_non_alphanumeric': 0.10877447425670776, 'fraction_numerical': 0.0014503263234227702, 'mean_word_length': 3.1415165165165164, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2376412', 'n_tokens_mistral': 3377, 'n_tokens_neox': 3288, 'n_words': 681}