From d90c262f573839d85ef82e6b7e4e2db30ebb25d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 29 Aug 2021 09:49:25 +0200 Subject: [PATCH] Do not touch hashbangs when formatting, closes #11215 --- lib/elixir/lib/code/formatter.ex | 20 ++++++------------- .../elixir/code_formatter/comments_test.exs | 3 ++- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/elixir/lib/code/formatter.ex b/lib/elixir/lib/code/formatter.ex index 6e49c7aff3..ee7bd84164 100644 --- a/lib/elixir/lib/code/formatter.ex +++ b/lib/elixir/lib/code/formatter.ex @@ -238,22 +238,14 @@ defmodule Code.Formatter do end defp format_comment(%{text: text} = comment) do - %{comment | text: format_comment_text(text, "")} + %{comment | text: format_comment_text(text)} end - defp format_comment_text("##" <> rest, acc), do: format_comment_text("#" <> rest, "#" <> acc) - - defp format_comment_text("#!", acc), do: reverse_with_prefix(acc, "#!") - defp format_comment_text("#! " <> _ = rest, acc), do: reverse_with_prefix(acc, rest) - defp format_comment_text("#!" <> rest, acc), do: reverse_with_prefix(acc, "#! " <> rest) - - defp format_comment_text("#", acc), do: reverse_with_prefix(acc, "#") - defp format_comment_text("# " <> _ = rest, acc), do: reverse_with_prefix(acc, rest) - defp format_comment_text("#" <> rest, acc), do: reverse_with_prefix(acc, "# " <> rest) - - defp reverse_with_prefix(acc, prefix) do - String.reverse(acc) <> prefix - end + defp format_comment_text("#"), do: "#" + defp format_comment_text("#!" <> rest), do: "#!" <> rest + defp format_comment_text("##" <> rest), do: "#" <> format_comment_text("#" <> rest) + defp format_comment_text("# " <> rest), do: "# " <> rest + defp format_comment_text("#" <> rest), do: "# " <> rest # If there is a no new line before, we can't gather all followup comments. defp gather_comments([%{previous_eol_count: 0} = comment | comments]) do diff --git a/lib/elixir/test/elixir/code_formatter/comments_test.exs b/lib/elixir/test/elixir/code_formatter/comments_test.exs index 288cc22460..d2900924ec 100644 --- a/lib/elixir/test/elixir/code_formatter/comments_test.exs +++ b/lib/elixir/test/elixir/code_formatter/comments_test.exs @@ -19,7 +19,8 @@ defmodule Code.Formatter.CommentsTest do end test "recognizes hashbangs" do - assert_format "#!/usr/bin/env elixir", "#! /usr/bin/env elixir" + assert_format "#! /usr/bin/env elixir", "#! /usr/bin/env elixir" + assert_format "#!/usr/bin/env elixir", "#!/usr/bin/env elixir" assert_same "#!" end