hrzndhrn/recode

Feature request: Support collapsing of newlines

Closed this issue · 4 comments

Thanks for the great work on this project!

I was recently looking for a way to collapse extra newlines that had been added to a file which mix format would otherwise have not added. This happens commonly when working on a project and the formatter splits a statement into multiple lines, but then the author shortens it such that it would otherwise fit on one line. The result is a source file with unnecessary line breaks.

Example:

  # Functions w/ long params get split into multiple lines by mix format (expected, desired):
  def function(
        very_long_parameter_that_causes_line_break1,
        very_long_parameter_that_causes_line_break2
      ) do
    # ...
  end

  # Shortening the long variable names does not result in the function definition being shortened: (i would like it to)
  def function(
        shortened_param1,
        shortened_param1
      ) do
    # ...
  end

  # Manually shortening the params works, but it is laborious:
  def function(shortened_param1, shortened_param1) do
    # ...
  end

  # This applies to structs/maps, too. I'd like these unnecessary newlines to be removed...
  def function() do
    #
    %{
      a: 1
    }
  end

  # ... so that after running recode, I have:
  def function() do
    %{a: 1}
  end

Is it possible to add support for collapsing these previous expanded but no longer necessary lines?

Hello @tensiondriven

That should be possible. I will take a look the next days.

I think mix format works here with both code styles to let developers format some code in sake of readability and not for the sake compactness. Like in this example:

    color1 = %{r: 0x00FF11, g: 0xFF0055, g: 0x001122}

    color2 = %{
      r: 0x00FF11,
      g: 0xFF0055,
      g: 0x001122
    }

But that doesn't speak against providing a task for it.

Thanks for the background - im not advocating for mix format to be different, only finding it frustrating that there is no easy way to remove such formatting in any of the tools.

The result is that once added, these line breaks tend to persist in some codebases, and require extra effort to remove.

Hello @tensiondriven

Not quite ready yet, but you can try it out if you like.

You can add the following line to your deps:

{:recode, github: "hrzndhrn/recode", branch: "oneliner"}

Add a config:

mix recode.gen.config

Note that SameLine is not used by default (config: run: false).
You can run the task by:

mix recode --dry --task SameLine

With the flag --dry you can see the changes in the output without updating your original files. You can also run recode for one file:

mix recode --dry --task SameLine lib/my_app/my_module.ex

Thank you!!!