<commit_before>defmodule Protobuf.Protoc.Generator do
  alias Protobuf.Protoc.Generator.Message, as: MessageGenerator
  alias Protobuf.Protoc.Generator.Enum, as: EnumGenerator
  alias Protobuf.Protoc.Generator.Service, as: ServiceGenerator

  def generate(ctx, desc) do
    name = new_file_name(desc.name)

    Google.Protobuf.Compiler.CodeGeneratorResponse.File.new(
      name: name,
      content: generate_content(ctx, desc)
    )
  end

  defp new_file_name(name) do
    String.replace_suffix(name, ".proto", ".pb.ex")
  end

  def generate_content(ctx, desc) do
    ctx = %{ctx | package: desc.package, syntax: syntax(desc.syntax)}
    ctx = %{ctx | dep_pkgs: get_dep_pkgs(ctx, desc.dependency)}

    list =
      MessageGenerator.generate_list(ctx, desc.message_type) ++
        EnumGenerator.generate_list(ctx, desc.enum_type) ++
        ServiceGenerator.generate_list(ctx, desc.service)

    list
    |> List.flatten()
    |> Enum.join("\n")
    |> format_code()
  end

  @doc false
  def get_dep_pkgs(%{pkg_mapping: mapping, package: pkg}, deps) do
    pkgs = deps |> Enum.map(fn dep -> mapping[dep] end)
    pkgs = if pkg && String.length(pkg) > 0, do: [pkg | pkgs], else: pkgs
    Enum.sort(pkgs, &(byte_size(&2) <= byte_size(&1)))
  end

  defp syntax("proto3"), do: :proto3
  defp syntax(_), do: :proto2

  def format_code(code) do
    formated =
      if Code.ensure_loaded?(Code) && function_exported?(Code, :format_string!, 2) do
        code
        |> Code.format_string!(locals_without_parens: [field: 2, field: 3, oneof: 2])
        |> IO.iodata_to_binary()
      else<commit_msg>add rpc/3 to locals_without_parens in generator<commit_after>defmodule Protobuf.Protoc.Generator do
  alias Protobuf.Protoc.Generator.Message, as: MessageGenerator
  alias Protobuf.Protoc.Generator.Enum, as: EnumGenerator
  alias Protobuf.Protoc.Generator.Service, as: ServiceGenerator

  def generate(ctx, desc) do
    name = new_file_name(desc.name)

    Google.Protobuf.Compiler.CodeGeneratorResponse.File.new(
      name: name,
      content: generate_content(ctx, desc)
    )
  end

  defp new_file_name(name) do
    String.replace_suffix(name, ".proto", ".pb.ex")
  end

  def generate_content(ctx, desc) do
    ctx = %{ctx | package: desc.package, syntax: syntax(desc.syntax)}
    ctx = %{ctx | dep_pkgs: get_dep_pkgs(ctx, desc.dependency)}

    list =
      MessageGenerator.generate_list(ctx, desc.message_type) ++
        EnumGenerator.generate_list(ctx, desc.enum_type) ++
        ServiceGenerator.generate_list(ctx, desc.service)

    list
    |> List.flatten()
    |> Enum.join("\n")
    |> format_code()
  end

  @doc false
  def get_dep_pkgs(%{pkg_mapping: mapping, package: pkg}, deps) do
    pkgs = deps |> Enum.map(fn dep -> mapping[dep] end)
    pkgs = if pkg && String.length(pkg) > 0, do: [pkg | pkgs], else: pkgs
    Enum.sort(pkgs, &(byte_size(&2) <= byte_size(&1)))
  end

  defp syntax("proto3"), do: :proto3
  defp syntax(_), do: :proto2

  def format_code(code) do
    formated =
      if Code.ensure_loaded?(Code) && function_exported?(Code, :format_string!, 2) do
        code
        |> Code.format_string!(locals_without_parens: [field: 2, field: 3, oneof: 2, rpc: 3])
        |> IO.iodata_to_binary()
      else
{'fraction_non_alphanumeric': 0.1316276202219482, 'fraction_numerical': 0.006781750924784217, 'mean_word_length': 3.5639943741209565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '906036', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1094, 'n_words': 244}