<commit_before>package scalanlp.config

import com.thoughtworks.paranamer.AdaptiveParanamer
import java.lang.reflect.Type
import collection.mutable.ArrayBuffer
import java.io.File
import java.{lang=>jl}

import ReflectionUtils._

/**
 *  Generates a Help message from a case class. If a constructor parameter has a "Help" annotation
 *  present, it will display that along with basic type information.
 * @author dlwh
 */
object GenerateHelp {
  /**
   * Generates help for the given manifest
   */
  def apply[C:Manifest](conf: Configuration = Configuration.empty):String = {
    val man = implicitly[Manifest[C]]
    val reader = new AdaptiveParanamer();

    def recGen(staticManifest: Manifest[_], prefix: String):Seq[Format] = {
      val clss = staticManifest.erasure
      val ann = clss.getAnnotation(classOf[Help])
      val res = new ArrayBuffer[Format]
      if(ann != null) {
        res += Break += Group(prefix,clss.getName,ann.text) += Break
      }

      val dynamicClass: Class[_] = conf.recursiveGetProperty(prefix).map{
        Class.forName(_)
      } getOrElse (clss);
      println((dynamicClass,staticManifest,dynamicClass.getConstructors.mkString(",")))
      if (dynamicClass.getConstructors.isEmpty)
        return res

      val staticTypeVars: Seq[String] = staticManifest.erasure.getTypeParameters.map(_.toString);
      val staticTypeVals: Seq[OptManifest[_]] = staticManifest.typeArguments;
      val staticTypeMap: Map[String, OptManifest[_]] = (staticTypeVars zip staticTypeVals).toMap withDefaultValue (NoManifest);

      val dynamicTypeMap = solveTypes(staticTypeMap, staticManifest.erasure, dynamicClass);

      // Handle ctor parameters
      val toRecurse = ArrayBuffer[(String,Manifest[_])]()

      val ctor = clss.getConstructors apply 0
      val paramNames = reader.lookupParameterNames(ctor);
      val defaults = lookupDefaultValues(dynamicClass, paramNames);
      val anns = ctor.getParameterAnnotations
      val typedParams = ctor.getGenericParameterTypes.map { mkManifest(dynamicTypeMap, _)}

      println(typedParams)
      for( i <- 0 until paramNames.length) {
        val myAnns = anns(i)
        val tpe = typedParams(i)
        val name = paramNames(i)
        val default = try { defaults(i)().toString } catch { case e: Exception => ""}
        val ann = myAnns.collectFirst{case h: Help => h}
        ann match {
          case None if isPrimitive(tpe.erasure) =>
            res += Param(wrap(prefix,name), prettyString(tpe),default, "")
          case Some(help) =>
            res += Param(wrap(prefix,name), prettyString(tpe),default, help.text)
          case _ =>
        }
        if(!isPrimitive(tpe.erasure)) {
          toRecurse += (name -> tpe)
        }
      }
      for( (name,tpe) <- toRecurse) {
        res ++= recGen(tpe,wrap(prefix,name))
      }

      res
    }

    val formats = recGen(man,"")
    val paramSplit = formats.foldLeft(0)(_ max _.paramLength)
    val minLength = formats.foldLeft(0)(_ max _.minLineLength)<commit_msg>removed some debug statements from generatehelp.<commit_after>package scalanlp.config

import com.thoughtworks.paranamer.AdaptiveParanamer
import java.lang.reflect.Type
import collection.mutable.ArrayBuffer
import java.io.File
import java.{lang=>jl}

import ReflectionUtils._

/**
 *  Generates a Help message from a case class. If a constructor parameter has a "Help" annotation
 *  present, it will display that along with basic type information.
 * @author dlwh
 */
object GenerateHelp {
  /**
   * Generates help for the given manifest
   */
  def apply[C:Manifest](conf: Configuration = Configuration.empty):String = {
    val man = implicitly[Manifest[C]]
    val reader = new AdaptiveParanamer();

    def recGen(staticManifest: Manifest[_], prefix: String):Seq[Format] = {
      val clss = staticManifest.erasure
      val ann = clss.getAnnotation(classOf[Help])
      val res = new ArrayBuffer[Format]
      if(ann != null) {
        res += Break += Group(prefix,clss.getName,ann.text) += Break
      }

      val dynamicClass: Class[_] = conf.recursiveGetProperty(prefix).map{
        Class.forName(_)
      } getOrElse (clss);
      if (dynamicClass.getConstructors.isEmpty)
        return res

      val staticTypeVars: Seq[String] = staticManifest.erasure.getTypeParameters.map(_.toString);
      val staticTypeVals: Seq[OptManifest[_]] = staticManifest.typeArguments;
      val staticTypeMap: Map[String, OptManifest[_]] = (staticTypeVars zip staticTypeVals).toMap withDefaultValue (NoManifest);

      val dynamicTypeMap = solveTypes(staticTypeMap, staticManifest.erasure, dynamicClass);

      // Handle ctor parameters
      val toRecurse = ArrayBuffer[(String,Manifest[_])]()

      val ctor = clss.getConstructors apply 0
      val paramNames = reader.lookupParameterNames(ctor);
      val defaults = lookupDefaultValues(dynamicClass, paramNames);
      val anns = ctor.getParameterAnnotations
      val typedParams = ctor.getGenericParameterTypes.map { mkManifest(dynamicTypeMap, _)}

      for( i <- 0 until paramNames.length) {
        val myAnns = anns(i)
        val tpe = typedParams(i)
        val name = paramNames(i)
        val default = try { defaults(i)().toString } catch { case e: Exception => ""}
        val ann = myAnns.collectFirst{case h: Help => h}
        ann match {
          case None if isPrimitive(tpe.erasure) =>
            res += Param(wrap(prefix,name), prettyString(tpe),default, "")
          case Some(help) =>
            res += Param(wrap(prefix,name), prettyString(tpe),default, help.text)
          case _ =>
        }
        if(!isPrimitive(tpe.erasure)) {
          toRecurse += (name -> tpe)
        }
      }
      for( (name,tpe) <- toRecurse) {
        res ++= recGen(tpe,wrap(prefix,name))
      }

      res
    }

    val formats = recGen(man,"")
    val paramSplit = formats.foldLeft(0)(_ max _.paramLength)
    val minLength = formats.foldLeft(0)(_ max _.minLineLength)
{'fraction_non_alphanumeric': 0.11219348337252268, 'fraction_numerical': 0.0013436345314074571, 'mean_word_length': 3.1556175854849964, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '6536753', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1757, 'n_words': 420}