<commit_before>package org.jetbrains.plugins.scala.runner

import com.intellij.openapi.module.Module
import com.intellij.openapi.util.text.StringUtil
import com.intellij.execution.junit.{JavaRuntimeConfigurationProducerBase, RuntimeConfigurationProducer}
import com.intellij.psi.util.PsiMethodUtil
import com.intellij.execution.configurations.ConfigurationUtil
import com.intellij.openapi.project.Project
import com.intellij.execution.application.{ApplicationConfiguration, ApplicationConfigurationType}
import com.intellij.execution.impl.RunManagerImpl
import com.intellij.openapi.util.Comparing
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution._
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject
import com.intellij.psi._
import org.jetbrains.plugins.scala.extensions.toPsiMemberExt

/**
 * @author Alefas
 * @since 02.03.12
 */
class ScalaApplicationConfigurationProducer extends JavaRuntimeConfigurationProduceBaseAdapter(ApplicationConfigurationType.getInstance) with Cloneable {

  def getSourceElement: PsiElement = myPsiElement

  def createConfigurationByElement(_location: Location[_ <: PsiElement], context: ConfigurationContext): RunnerAndConfigurationSettings = {
    val location = JavaExecutionUtil.stepIntoSingleClass(_location)
    if (location == null) return null
    val element: PsiElement = location.getPsiElement
    val containingFile = element.getContainingFile
    if (!containingFile.isInstanceOf[ScalaFile])return null
    if (!element.isPhysical) return null
    var currentElement: PsiElement = element
    var method: PsiMethod = findMain(currentElement)
    while (method != null) {
      val aClass: PsiClass = method.containingClass
      if (ConfigurationUtil.MAIN_CLASS.value(aClass)) {
        myPsiElement = method
        return createConfiguration(aClass, context, location)
      }
      currentElement = method.getParent
      method = findMain(currentElement)
    }
    val aClass: PsiClass = getMainClass(element)
    if (aClass == null) return null
    myPsiElement = aClass
    createConfiguration(aClass, context, location)
  }

  private def getMainClass(_element: PsiElement): PsiClass = {
    var element = _element
    while (element != null) {
      element match {
        case o: ScObject =>
          val aClass = o.fakeCompanionClassOrCompanionClass
          if (PsiMethodUtil.findMainInClass(aClass) != null) {
            return aClass
          }
        case file: ScalaFile =>
          val classes: Array[PsiClass] = file.getClasses<commit_msg>Application configuration hacked to have appropriate main method configuration producer.<commit_after>package org.jetbrains.plugins.scala.runner

import com.intellij.openapi.module.Module
import com.intellij.openapi.util.text.StringUtil
import com.intellij.execution.junit.{JavaRuntimeConfigurationProducerBase, RuntimeConfigurationProducer}
import com.intellij.psi.util.PsiMethodUtil
import com.intellij.execution.configurations.ConfigurationUtil
import com.intellij.openapi.project.Project
import com.intellij.execution.application.{ApplicationConfiguration, ApplicationConfigurationType}
import com.intellij.execution.impl.RunManagerImpl
import com.intellij.openapi.util.Comparing
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution._
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject
import com.intellij.psi._
import org.jetbrains.plugins.scala.extensions.toPsiMemberExt
import org.jetbrains.plugins.scala.lang.psi.light.ScFunctionWrapper

/**
 * @author Alefas
 * @since 02.03.12
 */
class ScalaApplicationConfigurationProducer extends JavaRuntimeConfigurationProduceBaseAdapter(ApplicationConfigurationType.getInstance) with Cloneable {

  def getSourceElement: PsiElement = myPsiElement

  def createConfigurationByElement(_location: Location[_ <: PsiElement], context: ConfigurationContext): RunnerAndConfigurationSettings = {
    val location = JavaExecutionUtil.stepIntoSingleClass(_location)
    if (location == null) return null
    val element: PsiElement = location.getPsiElement
    val containingFile = element.getContainingFile
    if (!containingFile.isInstanceOf[ScalaFile])return null
    if (!element.isPhysical) return null
    var currentElement: PsiElement = element
    var method: PsiMethod = findMain(currentElement)
    while (method != null) {
      val aClass: PsiClass = method.containingClass
      if (ConfigurationUtil.MAIN_CLASS.value(aClass)) {
        myPsiElement = method match {
          case fun: ScFunction => fun.getFirstChild
          case fun: ScFunctionWrapper => fun.function.getFirstChild
          case elem => elem.getFirstChild
        }
        return createConfiguration(aClass, context, location)
      }
      currentElement = method.getParent
      method = findMain(currentElement)
    }
    val aClass: PsiClass = getMainClass(element)
    if (aClass == null) return null
    myPsiElement = aClass
    createConfiguration(aClass, context, location)
  }

  /**
   * This is not for Java only. However it uses getClasses, to have possibility use [[com.intellij.psi.util.PsiMethodUtil.findMainInClass]]
   */
  private def getMainClass(_element: PsiElement): PsiClass = {
    var element = _element
    while (element != null) {
      element match {
        case o: ScObject =>
          val aClass = o.fakeCompanionClassOrCompanionClass
          if (PsiMethodUtil.findMainInClass(aClass) != null) {
            return aClass
          }
        case file: ScalaFile =>
          val classes: Array[PsiClass] = file.getClasses
{'fraction_non_alphanumeric': 0.08258130427379533, 'fraction_numerical': 0.002043248765537204, 'mean_word_length': 4.393939393939394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '7663897', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1595, 'n_words': 339}