<commit_before>package scalax.rules.scalasig

object ScalaSigPrinter {

  def printSymbol(symbol: Symbol) {printSymbol(0, symbol)}

  def printSymbol(level: Int, symbol: Symbol) {
    def indent() {for (i <- 1 to level) print("  ")}


    symbol match {
      case o: ObjectSymbol => indent(); printObject(level, o)
      case c: ClassSymbol => indent(); printClass(level, c)
      case m: MethodSymbol => indent(); printMethod(level, m)
      case a: AliasSymbol => indent(); printAlias(level, a)
      case t: TypeSymbol => ()
      case s => {}
    }
  }

  def printChildren(level: Int, symbol: Symbol) {
    for (child <- symbol.children) printSymbol(level + 1, child)
  }

  def printWithIndent(level: Int, s: String) {
    def indent() {for (i <- 1 to level) print("  ")}
    indent;
    print(s)
  }

  def printModifiers(symbol: Symbol) {
    if (symbol.isSealed) print("sealed ")
    if (symbol.isPrivate) print("private ")
    else if (symbol.isProtected) print("protected ")
    if (symbol.isAbstract) print("abstract ")
    if (symbol.isCase && !symbol.isMethod) print("case ")
  }

  def printClass(level: Int, c: ClassSymbol) {
    printAttributes(c)
    printModifiers(c)
    if (c.isTrait) print("trait ") else print("class ")
    print(c.name)
    printType(c)
    print("{\n")
    printChildren(level, c)
    printWithIndent(level, "}\n")
  }

  def printObject(level: Int, o: ObjectSymbol) {
    printAttributes(o)
    printModifiers(o)
    print("object ")
    print(o.name)
    val TypeRefType(prefix, symbol: ClassSymbol, typeArgs) = o.infoType
    printType(symbol)
    print("\n")
    printChildren(level, symbol)
    printWithIndent(level, "}\n")
  }

  def printMethod(level: Int, m: MethodSymbol) {
    printAttributes(m)
    printModifiers(m)
    print("def ")
    print(m.name match {case "<init>" => "this" case name => name})
    printType(m.infoType, " : ")
    println()

    printChildren(level, m)
  }

  def printAlias(level: Int, a: AliasSymbol) {
    printAttributes(a)
    print("type ")
    print(a.name)
    printType(a.infoType, " = ")
    println()

    printChildren(level, a)
  }

  def printAttributes(sym: SymbolInfoSymbol) {
    for (attrib <- sym.attributes) printAttribute(attrib)
  }

  def printAttribute(attrib: AttributeInfo) {
    printType(attrib.typeRef, "@")
    if (attrib.value.isDefined) {
      print("(")
      printValue(attrib.value.get)
      print(")")
    }
    if (!attrib.values.isEmpty) {
      print(" {")
      for (name ~ value <- attrib.values) {
        print(" val ")
        print(name)
        print(" = ")
        printValue(value)
      }
      printValue(attrib.value)
      print(" }")
    }
    print(" ")
  }

  def printValue(value: Any): Unit = value match {
    case t: Type => printType(t)
    // TODO string, char, float, etc.
    case _ => print(value)
  }

  def printType(sym: SymbolInfoSymbol): Unit = printType(sym.infoType)

  def printType(t: Type): Unit = print(toString(t))

  def printType(t: Type, sep: String): Unit = print(toString(t, sep))

  def toString(t: Type): String = toString(t, "")

  def genParamName(ts: String) = "0" //todo improve name generation by type

  def toString(t: Type, sep: String): String = t match {
  //case ThisType(symbol) =><commit_msg>scalacsigprinter modified<commit_after>package scalax.rules.scalasig

object ScalaSigPrinter {

  type Printer = (String) => Unit

  implicit def getPrinter: Printer = print _

  def printSymbol(symbol: Symbol, printer: Printer) {printSymbol(0, symbol)(printer)}

  def printSymbol(symbol: Symbol) {printSymbol(0, symbol)}

  def printSymbol(level: Int, symbol: Symbol)(implicit printer: Printer) {
    def indent() {for (i <- 1 to level) print("  ")}


    symbol match {
      case o: ObjectSymbol => indent(); printObject(level, o)(printer)
      case c: ClassSymbol => indent(); printClass(level, c)(printer)
      case m: MethodSymbol => indent(); printMethod(level, m)(printer)
      case a: AliasSymbol => indent(); printAlias(level, a) (printer)
      case t: TypeSymbol => ()
      case s => {}
    }
  }

  def printChildren(level: Int, symbol: Symbol) {
    for (child <- symbol.children) printSymbol(level + 1, child)
  }

  def printWithIndent(level: Int, s: String)(implicit printer: Printer) {
    def indent() {for (i <- 1 to level) printer("  ")}
    indent;
    printer(s)
  }

  def printModifiers(symbol: Symbol)(implicit printer: Printer) {
    if (symbol.isSealed) printer("sealed ")
    if (symbol.isPrivate) printer("private ")
    else if (symbol.isProtected) printer("protected ")
    if (symbol.isAbstract) printer("abstract ")
    if (symbol.isCase && !symbol.isMethod) printer("case ")
  }

  def printClass(level: Int, c: ClassSymbol)(implicit printer: Printer) {
    printAttributes(c)
    printModifiers(c)(printer)
    if (c.isTrait) printer("trait ") else printer("class ")
    printer(c.name)
    printType(c)
    printer("{\n")
    printChildren(level, c)
    printWithIndent(level, "}\n")(printer)
  }

  def printObject(level: Int, o: ObjectSymbol)(implicit printer: Printer) {
    printAttributes(o)
    printModifiers(o)(printer)
    printer("object ")
    printer(o.name)
    val TypeRefType(prefix, symbol: ClassSymbol, typeArgs) = o.infoType
    printType(symbol)
    printer("\n")
    printChildren(level, symbol)
    printWithIndent(level, "}\n")(printer)
  }

  def printMethod(level: Int, m: MethodSymbol)(implicit printer: Printer) {
    printAttributes(m)
    printModifiers(m)(printer)
    printer("def ")
    printer(m.name match {case "<init>" => "this" case name => name})
    printType(m.infoType, " : ")(printer)
    println()

    printChildren(level, m)
  }

  def printAlias(level: Int, a: AliasSymbol)(implicit printer: Printer) {
    printAttributes(a)
    printer("type ")
    printer(a.name)
    printType(a.infoType, " = ")(printer)
    println()

    printChildren(level, a)
  }

  def printAttributes(sym: SymbolInfoSymbol) {
    for (attrib <- sym.attributes) printAttribute(attrib)
  }

  def printAttribute(attrib: AttributeInfo)(implicit printer: Printer) {
    printType(attrib.typeRef, "@")(printer)
    if (attrib.value.isDefined) {
      printer("(")
      printValue(attrib.value.get)(printer)
      printer(")")
    }
    if (!attrib.values.isEmpty) {
      printer(" {")
      for (name ~ value <- attrib.values) {
        printer(" val ")
        printer(name)
        printer(" = ")
        printValue(value)(printer)
      }
      printValue(attrib.value)(printer)
      printer(" }")
    }
    printer(" ")
  }

  def printValue(value: Any)(implicit printer: Printer): Unit = value match {
    case t: Type => printType(t)(printer)
    // TODO string, char, float, etc.
    case _ => printer(value.toString)
  }

  def printType(sym: SymbolInfoSymbol): Unit = printType(sym.infoType)

  def printType(t: Type)(implicit printer: Printer): Unit = printer(toString(t))

  def printType(t: Type, sep: String)(implicit printer: Printer): Unit = printer(toString(t, sep))

  def toString(t: Type): String = toString(t, "")

  def genParamName(ts: String) = "0" //todo improve name generation by type

  def toString(t: Type, sep: String): String = t match {
  //case ThisType(symbol) =>
{'fraction_non_alphanumeric': 0.14700594661872493, 'fraction_numerical': 0.0015212280459134283, 'mean_word_length': 3.354003612281758, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 28, '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': '978281', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2365, 'n_words': 567}