Repository: elixir-gettext/gettext (elixir)

Issue:
Apparent mishandling of reference comments on merge
What I’m reporting here is not unique to Elixir’s gettext implementation, as I have seen it with some version of `msguniq` from the official GNU tooling, and it may not be a bug, but it feels like one and it results in an unnecessarily large delta.

We ran `mix gettext.merge priv/gettext` and got a diff that looks like this:

```
diff --git a/priv/gettext/en_CA/LC_MESSAGES/translations.po b/priv/gettext/en_CA/LC_MESSAGES/translations.po
index 7b87ad0d..e61885ff 100644
--- a/priv/gettext/en_CA/LC_MESSAGES/translations.po
+++ b/priv/gettext/en_CA/LC_MESSAGES/translations.po
@@ -95,8 +95,7 @@ msgid "Account will expire on %{when}"
 msgstr "Account will expire on %{when}"
 
 #: assets/src/components/common/elements/modal/LinkChargeBuyerModal.vue:26
-#: assets/src/components/common/elements/tables/PurchaseHistoryDetails.vue:24
-#: assets/src/components/common/elements/tables/PurchaseTable.vue:41
+#: assets/src/components/common/elements/tables/PurchaseHistoryDetails.vue:24 assets/src/components/common/elements/tables/PurchaseTable.vue:41
 msgid "Account:"
 msgstr "Account:"
 
```

There are _dozens_ of cases like this. The problem is that it has taken two lines:

```gettext
#: assets/src/components/common/elements/tables/PurchaseHistoryDetails.vue:24
#: assets/src/components/common/elements/tables/PurchaseTable.vue:41
```

And turned them into one long line (much longer than 80 characters):

```gettext
#: assets/src/components/common/elements/tables/PurchaseHistoryDetails.vue:24 assets/src/components/common/elements/tables/PurchaseTable.vue:41
```

This is common enough with what I’ve been dealing with lately (both from the Elixir tools and `msguniq`) that I have a ex-command in my vim history: `:%s/\([^:]\) assets/\1^M#: assets/g` to fix it.

Pull request description:
Put each reference on its own line
Fixes #255 

Update tests to match this new behavior

Patch:
diff --git a/lib/gettext/po.ex b/lib/gettext/po.ex
index e5a06c2..c4c300a 100644
--- a/lib/gettext/po.ex
+++ b/lib/gettext/po.ex
@@ -27,8 +27,6 @@ defmodule Gettext.PO do
           file: nil | Path.t()
         }
 
-  @wrapping_column 80
-  @reference_comment_length String.length("#:")
   @bom <<0xEF, 0xBB, 0xBF>>
 
   @doc """
@@ -267,40 +265,10 @@ defmodule Gettext.PO do
   end
 
   defp dump_references(references) do
-    # This function outputs a bunch of #: comments with as many references on
-    # each line as there can be under @reference_wrapping_column columns.
-    wrapping_column = @wrapping_column - @reference_comment_length
+    # This function outputs a line of #: comments for each reference
 
     references
-    |> chunk_references_by_line(wrapping_column, _line_length = 0, _chunk = [], _acc = [])
-    |> Enum.map(fn line -> ["#:", line, ?\n] end)
-  end
-
-  defp chunk_references_by_line([], _wrapping_col, _line_length, _chunk_acc = [], acc) do
-    Enum.reverse(acc)
-  end
-
-  defp chunk_references_by_line([], _wrapping_col, _line_length, _chunk_acc = chunk_acc, acc) do
-    acc = [Enum.reverse(chunk_acc) | acc]
-    Enum.reverse(acc)
-  end
-
-  defp chunk_references_by_line([{file, line} | rest], wrapping_col, line_length, chunk_acc, acc) do
-    ref = " #{file}:#{line}"
-    ref_length = String.length(ref)
-
-    cond do
-      ref_length + line_length > wrapping_col and chunk_acc == [] ->
-        chunk_references_by_line(rest, wrapping_col, 0, [ref], acc)
-
-      ref_length + line_length > wrapping_col ->
-        acc = [Enum.reverse(chunk_acc) | acc]
-        chunk_references_by_line(rest, wrapping_col, 0, [ref], acc)
-
-      true ->
-        new_line_length = line_length + ref_length
-        chunk_references_by_line(rest, wrapping_col, new_line_length, [ref | chunk_acc], acc)
-    end
+    |> Enum.map(fn {file, line} -> ["#: ", file, ?:, Integer.to_string(line), ?\n] end)
   end
 
   defp dump_flags(flags) do


Test patch:
diff --git a/test/gettext/extractor_test.exs b/test/gettext/extractor_test.exs
index b35a47a..c09fabd 100644
--- a/test/gettext/extractor_test.exs
+++ b/test/gettext/extractor_test.exs
@@ -284,7 +284,8 @@ defmodule Gettext.ExtractorTest do
        #. some other comment
        #. one more comment
        #, elixir-format
-       #: foo.ex:16 foo.ex:19
+       #: foo.ex:16
+       #: foo.ex:19
        msgid "foo"
        msgstr ""
 
diff --git a/test/gettext/po_test.exs b/test/gettext/po_test.exs
index 01db74b..c77ed0c 100644
--- a/test/gettext/po_test.exs
+++ b/test/gettext/po_test.exs
@@ -338,11 +338,13 @@ defmodule Gettext.POTest do
       }
 
       assert IO.iodata_to_binary(PO.dump(po)) == ~S"""
-             #: foo.ex:1 lib/bar.ex:2
+             #: foo.ex:1
+             #: lib/bar.ex:2
              msgid "foo"
              msgstr "bar"
 
-             #: lib/with spaces.ex:1 lib/with other spaces.ex:2
+             #: lib/with spaces.ex:1
+             #: lib/with other spaces.ex:2
              msgid "foo"
              msgid_plural "foos"
              msgstr[0] ""
@@ -385,7 +387,8 @@ defmodule Gettext.POTest do
       }
 
       assert IO.iodata_to_binary(PO.dump(po)) == ~S"""
-             #: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ex:1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.ex:1
+             #: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ex:1
+             #: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.ex:1
              #: cccccccccccccccccccccccccccccc.ex:1
              msgid "foo"
              msgstr "bar"