<commit_before>package scala
package org.scalameta.reflection

import scala.tools.nsc.Global
import scala.reflect.internal.Flags
import scala.collection.mutable
import _root_.org.scalameta.invariants._

trait Helpers {
  self: _root_.org.scalameta.reflection.GlobalToolkit =>

  import global.{require => _, _}
  import definitions._
  import treeInfo._
  import build._

  implicit class RichFoundationHelperTree[T <: Tree](tree: T) {
    def copyAttrs(other: Tree): T = tree.copyAttrs(other)
  }

  // NOTE: partially copy/pasted from TreeInfo.scala and ReificationSupport.scala in scalac
  object SyntacticTemplate {
    def unapply(in: Template): Some[(List[Tree], ValDef, List[Tree], List[Tree])] = {
      object UnCtor {
        def unapply(tree: Tree): Option[(Modifiers, List[List[ValDef]], List[Tree], Symbol, List[List[Tree]])] = tree match {
          case DefDef(mods, nme.MIXIN_CONSTRUCTOR, _, _, _, build.SyntacticBlock(lvdefs :+ _)) =>
            Some((mods | Flag.TRAIT, Nil, lvdefs, NoSymbol, Nil))
          case DefDef(mods, nme.CONSTRUCTOR, Nil, vparamss, _, build.SyntacticBlock(lvdefs :+ Applied(core, _, argss) :+ _)) =>
            Some((mods, vparamss, lvdefs, core.symbol, argss))
          case _ => None
        }
      }
      def indexOfCtor(trees: List[Tree]) = {
        trees.indexWhere { case UnCtor(_, _, _, _, _) => true ; case _ => false }
      }
      val body1 = in.body // NOTE: filterBody and recoverBody moved to Ensugar
      val (rawEdefs, rest) = body1.span(treeInfo.isEarlyDef)
      val (gvdefs, etdefs) = rawEdefs.partition(treeInfo.isEarlyValDef)
      var (fieldDefs, lvdefs, superSym, superArgss, body2) = rest.splitAt(indexOfCtor(rest)) match {
        case (fieldDefs, UnCtor(_, _, lvdefs, superSym, superArgss) :: body2) => (fieldDefs, lvdefs, superSym, superArgss, body2)
        case (Nil, body2) => (Nil, Nil, NoSymbol, Nil, body2)
      }
      // TODO: really discern `... extends C()` and `... extends C`
      if (superArgss == List(Nil) && superSym.info.paramss.flatten.isEmpty) superArgss = Nil
      val evdefs = gvdefs.zip(lvdefs).map {
        case (gvdef @ ValDef(_, _, tpt, _), ValDef(_, _, _, rhs)) =>
          copyValDef(gvdef)(tpt = tpt, rhs = rhs)
      }
      val edefs = evdefs ::: etdefs
      val parents = in.parents match {
        case firstParent +: otherParents =>
          val firstParent1 = superArgss.foldLeft(firstParent)((curr, args) => Apply(curr, args).setType(firstParent.tpe).appendScratchpad(superSym))
          firstParent1 +: otherParents
        case Nil =>
          Nil
      }
      Some(parents, in.self, edefs, body2)
    }
  }

  object ApplyImplicitView {
    def unapply(tree: Tree): Option[(Tree, Tree)] = tree match {
      case tree: ApplyImplicitView => Some((tree.fun, tree.args.head))
      case _ => None
    }
  }

  object ApplyToImplicitArgs {
    def unapply(tree: Tree): Option[(Tree, List[Tree])] = tree match {
      case tree: ApplyToImplicitArgs => Some((tree.fun, tree.args))
      case _ => None
    }
  }

  object DesugaredSetter {
    def unapply(tree: Tree): Option[(Tree, Tree)] = tree.metadata.get("originalAssign").map(_.asInstanceOf[(Tree, Name, List[Tree])]) match {
      case Some((Desugared(lhs), name: TermName, Desugared(List(rhs)))) if nme.isSetterName(tree.symbol.name) && name == nme.EQL =>
        require(lhs.symbol.owner == tree.symbol.owner && lhs.symbol.name.setterName == tree.symbol.name)
        Some((lhs, rhs))
      case _ =>
        None
    }
  }

  object DesugaredUpdate {
    def unapply(tree: Tree): Option[(Tree, Tree)] = tree match {
      case Apply(core @ Select(lhs, _), args :+ rhs)
      if core.symbol.name == nme.update && !tree.hasMetadata("originalAssign") && !lhs.isInstanceOf[Super] && (args :+ rhs).forall(!_.isInstanceOf[AssignOrNamedArg]) =>
        Some((Apply(lhs, args).setType(NoType), rhs))
      case _ =>
        None
    }
  }

  object DesugaredOpAssign {
    def unapply(tree: Tree): Option[(Tree, List[Tree])] = tree.metadata.get("originalAssign").map(_.asInstanceOf[(Tree, Name, List[Tree])]) match {
      // TODO: can't use `args` here, because they might be untyped, so we have to workaround
      // case Some((Desugared(qual), name: TermName, Desugared(args))) if name != nme.EQL =><commit_msg>fixes an overeager assertion in DesugaredSetter<commit_after>package scala
package org.scalameta.reflection

import scala.tools.nsc.Global
import scala.reflect.internal.Flags
import scala.collection.mutable
import _root_.org.scalameta.invariants._

trait Helpers {
  self: _root_.org.scalameta.reflection.GlobalToolkit =>

  import global.{require => _, _}
  import definitions._
  import treeInfo._
  import build._

  implicit class RichFoundationHelperTree[T <: Tree](tree: T) {
    def copyAttrs(other: Tree): T = tree.copyAttrs(other)
  }

  // NOTE: partially copy/pasted from TreeInfo.scala and ReificationSupport.scala in scalac
  object SyntacticTemplate {
    def unapply(in: Template): Some[(List[Tree], ValDef, List[Tree], List[Tree])] = {
      object UnCtor {
        def unapply(tree: Tree): Option[(Modifiers, List[List[ValDef]], List[Tree], Symbol, List[List[Tree]])] = tree match {
          case DefDef(mods, nme.MIXIN_CONSTRUCTOR, _, _, _, build.SyntacticBlock(lvdefs :+ _)) =>
            Some((mods | Flag.TRAIT, Nil, lvdefs, NoSymbol, Nil))
          case DefDef(mods, nme.CONSTRUCTOR, Nil, vparamss, _, build.SyntacticBlock(lvdefs :+ Applied(core, _, argss) :+ _)) =>
            Some((mods, vparamss, lvdefs, core.symbol, argss))
          case _ => None
        }
      }
      def indexOfCtor(trees: List[Tree]) = {
        trees.indexWhere { case UnCtor(_, _, _, _, _) => true ; case _ => false }
      }
      val body1 = in.body // NOTE: filterBody and recoverBody moved to Ensugar
      val (rawEdefs, rest) = body1.span(treeInfo.isEarlyDef)
      val (gvdefs, etdefs) = rawEdefs.partition(treeInfo.isEarlyValDef)
      var (fieldDefs, lvdefs, superSym, superArgss, body2) = rest.splitAt(indexOfCtor(rest)) match {
        case (fieldDefs, UnCtor(_, _, lvdefs, superSym, superArgss) :: body2) => (fieldDefs, lvdefs, superSym, superArgss, body2)
        case (Nil, body2) => (Nil, Nil, NoSymbol, Nil, body2)
      }
      // TODO: really discern `... extends C()` and `... extends C`
      if (superArgss == List(Nil) && superSym.info.paramss.flatten.isEmpty) superArgss = Nil
      val evdefs = gvdefs.zip(lvdefs).map {
        case (gvdef @ ValDef(_, _, tpt, _), ValDef(_, _, _, rhs)) =>
          copyValDef(gvdef)(tpt = tpt, rhs = rhs)
      }
      val edefs = evdefs ::: etdefs
      val parents = in.parents match {
        case firstParent +: otherParents =>
          val firstParent1 = superArgss.foldLeft(firstParent)((curr, args) => Apply(curr, args).setType(firstParent.tpe).appendScratchpad(superSym))
          firstParent1 +: otherParents
        case Nil =>
          Nil
      }
      Some(parents, in.self, edefs, body2)
    }
  }

  object ApplyImplicitView {
    def unapply(tree: Tree): Option[(Tree, Tree)] = tree match {
      case tree: ApplyImplicitView => Some((tree.fun, tree.args.head))
      case _ => None
    }
  }

  object ApplyToImplicitArgs {
    def unapply(tree: Tree): Option[(Tree, List[Tree])] = tree match {
      case tree: ApplyToImplicitArgs => Some((tree.fun, tree.args))
      case _ => None
    }
  }

  object DesugaredSetter {
    def unapply(tree: Tree): Option[(Tree, Tree)] = tree.metadata.get("originalAssign").map(_.asInstanceOf[(Tree, Name, List[Tree])]) match {
      case Some((Desugared(lhs), name: TermName, Desugared(List(rhs)))) if nme.isSetterName(tree.symbol.name) && name == nme.EQL =>
        require(lhs.symbol.name.setterName == tree.symbol.name)
        Some((lhs, rhs))
      case _ =>
        None
    }
  }

  object DesugaredUpdate {
    def unapply(tree: Tree): Option[(Tree, Tree)] = tree match {
      case Apply(core @ Select(lhs, _), args :+ rhs)
      if core.symbol.name == nme.update && !tree.hasMetadata("originalAssign") && !lhs.isInstanceOf[Super] && (args :+ rhs).forall(!_.isInstanceOf[AssignOrNamedArg]) =>
        Some((Apply(lhs, args).setType(NoType), rhs))
      case _ =>
        None
    }
  }

  object DesugaredOpAssign {
    def unapply(tree: Tree): Option[(Tree, List[Tree])] = tree.metadata.get("originalAssign").map(_.asInstanceOf[(Tree, Name, List[Tree])]) match {
      // TODO: can't use `args` here, because they might be untyped, so we have to workaround
      // case Some((Desugared(qual), name: TermName, Desugared(args))) if name != nme.EQL =>
{'fraction_non_alphanumeric': 0.1426070720037344, 'fraction_numerical': 0.0023339946318123466, 'mean_word_length': 3.2092337917485265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '5507314', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2854, 'n_words': 637}