<commit_before>package org.jetbrains.plugins.scala
package testingSupport.scalaTest

import collection.mutable.ListBuffer
import com.intellij.psi.PsiElement
import lang.psi.impl.expr.{ScBlockExprImpl, ScArgumentExprListImpl}
import annotation.tailrec
import lang.psi.api.base.patterns.ScBindingPattern
import lang.psi.api.statements.ScFunctionDefinition
import lang.psi.api.toplevel.templates.ScTemplateBody
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.execution.Location
import lang.psi.api.toplevel.typedef.{ScTrait, ScClass, ScTypeDefinition, ScMember}
import lang.psi.api.expr._
import org.scalatest.finders.{Finder, AstNode, ToStringTarget, Selection}
import lang.psi.api.base.ScLiteral
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.{OrderRootType, ModuleRootManager}
import java.io.File
import java.net.{URLClassLoader, URL}
import lang.psi.impl.toplevel.typedef.MixinNodes
import lang.psi.types.ScType

/**
 * @author cheeseng
 * @since 16.01.2012
 */
class ScalaTestAstTransformer {
  def getFinder(clazz: ScClass, module: Module): Option[Finder] = {
    val clazzWithStyleOpt = MixinNodes.linearization(clazz).flatMap {
      tp => ScType.extractClass(tp, Some(clazz.getProject))
    }.find {
      psiClass =>
        psiClass match {
          case scClass: ScClass => scClass.hasAnnotation("org.scalatest.Style") != None
          case scTrait: ScTrait => scTrait.hasAnnotation("org.scalatest.Style") != None
          case _ => false
        }
    }

    def loadClass(className: String) = {
      val orderEntries = ModuleRootManager.getInstance(module).getOrderEntries
      val loaderUrls = orderEntries.map {
        orderEntry =>
          val rawUrls = orderEntry.getFiles(OrderRootType.CLASSES_AND_OUTPUT).map(_.getPresentableUrl)
          rawUrls.map {
            rawUrl =>
              val cpFile = new File(rawUrl)
              if (cpFile.exists && cpFile.isDirectory && !rawUrl.toString.endsWith(File.separator))
                new URL("file://" + rawUrl + "/")
              else
                new URL("file://" + rawUrl)
          }
      }.flatten
      val loader = new URLClassLoader(loaderUrls.toArray, getClass.getClassLoader)
      loader.loadClass(className)
    }

    clazzWithStyleOpt match {
      case Some(clazzWithStyle) => // Either ScClass or ScTrait, the following should be safe cast
        val typeDef = clazzWithStyle.asInstanceOf[ScTypeDefinition]
        try {
          val finderClassName: String = typeDef.hasAnnotation("org.scalatest.Style") match {
            case Some(styleAnnotation) =>
              val notFound = "NOT FOUND STYLE TEXT"
              styleAnnotation.constructor.args match {
                case Some(args) =>
                  val exprs = args.exprs
                  if (exprs.length > 0) {
                    exprs(0) match {
                      case l: ScLiteral if l.isString =>
                        val value = l.getValue
                        if (value.isInstanceOf[String]) value.asInstanceOf[String]
                        else notFound
                      case a: ScAssignStmt if a.getLExpression.isInstanceOf[ScReferenceExpression] &&
                        a.getLExpression.asInstanceOf[ScReferenceExpression].refName == "value" =>
                        a.getRExpression match {
                          case l: ScLiteral if l.isString =>
                            val value = l.getValue
                            if (value.isInstanceOf[String]) value.asInstanceOf[String]
                            else notFound
                          case _ => notFound
                        }
                      case _ => notFound
                    }
                  } else notFound
                case _ => notFound
              }
          }
          Some(loadClass(finderClassName).newInstance.asInstanceOf[Finder])
        }
        catch {
          case _: Exception =>
            val suiteClassName = typeDef.qualifiedName
            val suiteClass = loadClass(suiteClassName)
            val styleOpt = suiteClass.getAnnotations.find(annt => annt.annotationType.getName == "org.scalatest.Style")
            styleOpt match {
              case Some(style) =>
                val valueMethod = style.annotationType.getMethod("value")
                val finderClassName = valueMethod.invoke(style).asInstanceOf[String]
                if (finderClassName != null) {
                  val finderClass = loadClass(finderClassName)
                  val instance = finderClass.newInstance
                  instance match {
                    case finder: Finder => Some(finder)
                    case _ => None<commit_msg>ScalaTestAstTransformer: suppress "match is not exhaustive" compiler warning<commit_after>package org.jetbrains.plugins.scala
package testingSupport.scalaTest

import collection.mutable.ListBuffer
import com.intellij.psi.PsiElement
import lang.psi.impl.expr.{ScBlockExprImpl, ScArgumentExprListImpl}
import annotation.tailrec
import lang.psi.api.base.patterns.ScBindingPattern
import lang.psi.api.statements.ScFunctionDefinition
import lang.psi.api.toplevel.templates.ScTemplateBody
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.execution.Location
import lang.psi.api.toplevel.typedef.{ScTrait, ScClass, ScTypeDefinition, ScMember}
import lang.psi.api.expr._
import org.scalatest.finders.{Finder, AstNode, ToStringTarget, Selection}
import lang.psi.api.base.ScLiteral
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.{OrderRootType, ModuleRootManager}
import java.io.File
import java.net.{URLClassLoader, URL}
import lang.psi.impl.toplevel.typedef.MixinNodes
import lang.psi.types.ScType

/**
 * @author cheeseng
 * @since 16.01.2012
 */
class ScalaTestAstTransformer {
  def getFinder(clazz: ScClass, module: Module): Option[Finder] = {
    val clazzWithStyleOpt = MixinNodes.linearization(clazz).flatMap {
      tp => ScType.extractClass(tp, Some(clazz.getProject))
    }.find {
      psiClass =>
        psiClass match {
          case scClass: ScClass => scClass.hasAnnotation("org.scalatest.Style") != None
          case scTrait: ScTrait => scTrait.hasAnnotation("org.scalatest.Style") != None
          case _ => false
        }
    }

    def loadClass(className: String) = {
      val orderEntries = ModuleRootManager.getInstance(module).getOrderEntries
      val loaderUrls = orderEntries.map {
        orderEntry =>
          val rawUrls = orderEntry.getFiles(OrderRootType.CLASSES_AND_OUTPUT).map(_.getPresentableUrl)
          rawUrls.map {
            rawUrl =>
              val cpFile = new File(rawUrl)
              if (cpFile.exists && cpFile.isDirectory && !rawUrl.toString.endsWith(File.separator))
                new URL("file://" + rawUrl + "/")
              else
                new URL("file://" + rawUrl)
          }
      }.flatten
      val loader = new URLClassLoader(loaderUrls.toArray, getClass.getClassLoader)
      loader.loadClass(className)
    }

    clazzWithStyleOpt match {
      case Some(clazzWithStyle) => // Either ScClass or ScTrait, the following should be safe cast
        val typeDef = clazzWithStyle.asInstanceOf[ScTypeDefinition]
        try {
          val finderClassName: String = typeDef.hasAnnotation("org.scalatest.Style") match {
            case Some(styleAnnotation) =>
              val notFound = "NOT FOUND STYLE TEXT"
              styleAnnotation.constructor.args match {
                case Some(args) =>
                  val exprs = args.exprs
                  if (exprs.length > 0) {
                    exprs(0) match {
                      case l: ScLiteral if l.isString =>
                        val value = l.getValue
                        if (value.isInstanceOf[String]) value.asInstanceOf[String]
                        else notFound
                      case a: ScAssignStmt if a.getLExpression.isInstanceOf[ScReferenceExpression] &&
                        a.getLExpression.asInstanceOf[ScReferenceExpression].refName == "value" =>
                        a.getRExpression match {
                          case l: ScLiteral if l.isString =>
                            val value = l.getValue
                            if (value.isInstanceOf[String]) value.asInstanceOf[String]
                            else notFound
                          case _ => notFound
                        }
                      case _ => notFound
                    }
                  } else notFound
                case _ => notFound
              }
            case _ => throw new RuntimeException("Match is not exhaustive!")
          }
          Some(loadClass(finderClassName).newInstance.asInstanceOf[Finder])
        }
        catch {
          case _: Exception =>
            val suiteClassName = typeDef.qualifiedName
            val suiteClass = loadClass(suiteClassName)
            val styleOpt = suiteClass.getAnnotations.find(annt => annt.annotationType.getName == "org.scalatest.Style")
            styleOpt match {
              case Some(style) =>
                val valueMethod = style.annotationType.getMethod("value")
                val finderClassName = valueMethod.invoke(style).asInstanceOf[String]
                if (finderClassName != null) {
                  val finderClass = loadClass(finderClassName)
                  val instance = finderClass.newInstance
                  instance match {
                    case finder: Finder => Some(finder)
                    case _ => None
{'fraction_non_alphanumeric': 0.08875367184221569, 'fraction_numerical': 0.00209819555182543, 'mean_word_length': 2.0603531300160514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 48, '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': '7664484', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2485, 'n_words': 513}