Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formatter): fix small issue where there was only one newline added #13867

Closed

Conversation

pdgonzalez872
Copy link
Contributor

to a def that is followed by a module attribute.

Closes #13866


I'm not sure this implementation is the best, but it seems to do the job. I wouldn't be surprised if there is a better way to solve it though. This was my naive approach.

The test cases show the current behavior (def + def works, but def + mod attr doesn't).

Feel free to adjust/modify/close/do whatever you'd like to it if you see it fit.

Thank you for all of your work as usual! ❤️

to a def that is followed by a module attribute.
@@ -656,6 +656,14 @@ defmodule Code.Formatter do
defp block_next_line(:@), do: @empty
defp block_next_line(_), do: break("")

defp maybe_add_newlines({:def, [{:@, _, _} = _next | _]}, _meta) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not look at def, because we can have def, defp, defmacro, defmacrop... and projects can define defn, defnp, etc. We do have some code today that skips adding new lines if they are module attributes but perhaps the code should only avoid adding lines below, not lines up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes a lot of sense.

I'll try to play around with this in the next couple of days, but I won't be upset if you solve this in 2 minutes :)

No hard feelings :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim, seems closer now, but likely a bit far from the optimal solution.

I kinda wanted to offer a devil's advocate here, to the issue we put together 😂. After reading and working on the code a little bit, I came to appreciate the user's choice. I didn't think of this when seeing the issue for the first time mostly because I had not seen/worked with/knew how the formatter works.

I like how the formatter respect's the user's choice first. The crux to this devil's advocate argument is that the original code "wanted" to save space by doing the shorthard def.

If space was not an issue, the code would have used a multiline def, which the formatter already handles:

# pre-format
defmodule Example do
  @impl FooA
  def a() do
    :something
  end
  @impl FooB
  def b() do
    :something_else
  end
end

# post-format
defmodule Example do
  @impl FooA
  def a() do
    :something
  end

  @impl FooB
  def b() do
    :something_else
  end
end

Even after writing the above I'm more convinced that this is:

  • not applicable
  • likely a doc adjustment to document the user's choice rather than a behavior change

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, looking at all the formatting changes in this repo highlighted by the CI, this seems to have a much bigger impact than just the original example, and the user's choice perspective is a good point.

@josevalim
Copy link
Member

Thank you @pdgonzalez872!

I agree with the overall conclusions and the diff from your PR was helpful in seeing that. I think this will impose too much and leaving control to the user is the best way to go here.

@josevalim josevalim closed this Oct 3, 2024
@pdgonzalez872 pdgonzalez872 deleted the pg-formatter-newline-01 branch October 3, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Potential formatter bug - newlines don't seem to be added as expected
3 participants