<commit_before>require 'pp'
require 'prettyprint'

module RethinkDB
  module RPP
    def self.sanitize_context context
      if __FILE__ =~ /^(.*\/)[^\/]+.rb$/
        prefix = $1;
        context.reject{|x| x =~ /^#{prefix}/}
      else
        context
      end
    end

    def self.pp_int_optargs(q, optargs, pre_dot = false)
      q.text("r(") if pre_dot
      q.group(1, "{", "}") {
        optargs.each_with_index {|optarg, i|
          if i != 0
            q.text(",")
            q.breakable
          end
          q.text(":#{optarg.key} =>")
          q.nest(2) {
            q.breakable
            pp_int(q, optarg.val)
          }
        }
      }
      q.text(")") if pre_dot
    end

    def self.pp_int_args(q, args, pre_dot = false)
      q.text("r(") if pre_dot
      q.group(1, "[", "]") {
        args.each_with_index {|arg, i|
          if i != 0
            q.text(",")
            q.breakable
          end
          pp_int(q, arg)
        }
      }
      q.text(")") if pre_dot
    end

    def self.pp_int_datum(q, dat, pre_dot)
      q.text("r(") if pre_dot
      q.text(Shim.datum_to_native(dat).inspect)
      q.text(")") if pre_dot
    end

    def self.pp_int_func(q, func)
      func_args = func.args[0].args.map{|x| x.datum.r_num}
      func_body = func.args[1]
      q.text(" ")
      q.group(0, "{", "}") {
        if func_args != []
          q.text(func_args.map{|x| :"var_#{x}"}.inspect.gsub(/\[|\]/,"|").gsub(":",""))
        end
        q.nest(2) {
          q.breakable
          pp_int(q, func_body)
        }
        q.breakable('')
      }
    end

    def self.can_prefix (name, args)
      return false if name == "db" || name == "funcall"
      return false if args.size == 1 && args[0].type == Term::TermType::DATUM
      return true
    end
    def self.pp_int(q, term, pre_dot=false)
      q.text("\x7", 0) if term.is_error
      @@context = term.context if term.is_error

      if term.type == Term::TermType::DATUM
        res = pp_int_datum(q, term.datum, pre_dot)
        q.text("\x7", 0) if term.is_error
        return res
      end

      if term.type == Term::TermType::VAR
        q.text("var_")
        res = pp_int_datum(q, term.args[0].datum, false)
        q.text("\x7", 0) if term.is_error
        return res
      elsif term.type == Term::TermType::FUNC
        q.text("r(") if pre_dot
        q.text("lambda")
        res = pp_int_func(q, term)
        q.text(")") if pre_dot
        q.text("\x7", 0) if term.is_error
        return res
      elsif term.type == Term::TermType::MAKE_OBJ
        res = pp_int_optargs(q, term.optargs, pre_dot)
        q.text("\x7", 0) if term.is_error
        return res
      elsif term.type == Term::TermType::MAKE_ARRAY
        res = pp_int_args(q, term.args, pre_dot)
        q.text("\x7", 0) if term.is_error
        return res
      end

      name = term.type.to_s.downcase
      args = term.args.dup
      optargs = term.optargs.dup

      if can_prefix(name, args) && first_arg = args.shift
        pp_int(q, first_arg, true)
      else
        q.text("r")<commit_msg>fixed pretty printer<commit_after>require 'pp'
require 'prettyprint'

module RethinkDB
  module RPP
    @@termtype_to_str = Hash[
      Term::TermType.constants.map{|x| [Term::TermType.const_get(x), x]}
    ]

    def self.sanitize_context context
      if __FILE__ =~ /^(.*\/)[^\/]+.rb$/
        prefix = $1;
        context.reject{|x| x =~ /^#{prefix}/}
      else
        context
      end
    end

    def self.pp_int_optargs(q, optargs, pre_dot = false)
      q.text("r(") if pre_dot
      q.group(1, "{", "}") {
        optargs.each_with_index {|optarg, i|
          if i != 0
            q.text(",")
            q.breakable
          end
          q.text(":#{optarg.key} =>")
          q.nest(2) {
            q.breakable
            pp_int(q, optarg.val)
          }
        }
      }
      q.text(")") if pre_dot
    end

    def self.pp_int_args(q, args, pre_dot = false)
      q.text("r(") if pre_dot
      q.group(1, "[", "]") {
        args.each_with_index {|arg, i|
          if i != 0
            q.text(",")
            q.breakable
          end
          pp_int(q, arg)
        }
      }
      q.text(")") if pre_dot
    end

    def self.pp_int_datum(q, dat, pre_dot)
      q.text("r(") if pre_dot
      q.text(Shim.datum_to_native(dat).inspect)
      q.text(")") if pre_dot
    end

    def self.pp_int_func(q, func)
      func_args = func.args[0].args.map{|x| x.datum.r_num}
      func_body = func.args[1]
      q.text(" ")
      q.group(0, "{", "}") {
        if func_args != []
          q.text(func_args.map{|x| :"var_#{x}"}.inspect.gsub(/\[|\]/,"|").gsub(":",""))
        end
        q.nest(2) {
          q.breakable
          pp_int(q, func_body)
        }
        q.breakable('')
      }
    end

    def self.can_prefix (name, args)
      return false if name == "db" || name == "funcall"
      return false if args.size == 1 && args[0].type == Term::TermType::DATUM
      return true
    end
    def self.pp_int(q, term, pre_dot=false)
      q.text("\x7", 0) if term.is_error
      @@context = term.context if term.is_error

      if term.type == Term::TermType::DATUM
        res = pp_int_datum(q, term.datum, pre_dot)
        q.text("\x7", 0) if term.is_error
        return res
      end

      if term.type == Term::TermType::VAR
        q.text("var_")
        res = pp_int_datum(q, term.args[0].datum, false)
        q.text("\x7", 0) if term.is_error
        return res
      elsif term.type == Term::TermType::FUNC
        q.text("r(") if pre_dot
        q.text("lambda")
        res = pp_int_func(q, term)
        q.text(")") if pre_dot
        q.text("\x7", 0) if term.is_error
        return res
      elsif term.type == Term::TermType::MAKE_OBJ
        res = pp_int_optargs(q, term.optargs, pre_dot)
        q.text("\x7", 0) if term.is_error
        return res
      elsif term.type == Term::TermType::MAKE_ARRAY
        res = pp_int_args(q, term.args, pre_dot)
        q.text("\x7", 0) if term.is_error
        return res
      end

      name = @@termtype_to_str[term.type].downcase
      args = term.args.dup
      optargs = term.optargs.dup

      if can_prefix(name, args) && first_arg = args.shift
        pp_int(q, first_arg, true)
      else
        q.text("r")
{'fraction_non_alphanumeric': 0.15624502784407318, 'fraction_numerical': 0.007955449482895784, 'mean_word_length': 1.79005770084332, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '1743630', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2309, 'n_words': 466}