Skip to content

Keep formatter output stable with comments - #16009

Merged
josevalim merged 1 commit into
elixir-lang:mainfrom
mandar1045:fix-formatter-comment-stability
Oct 20, 2026
Merged

josevalim merged 1 commit into
elixir-lang:mainfrom
mandar1045:fix-formatter-comment-stability

Conversation

@mandar1045

Copy link
Copy Markdown
Contributor

Closes #16007.

Since #15893, a comment can end up attached to the leftmost operand of an operator (like a in a ++ b), so the formatter printed it in the middle of the expression and moved it again on the next run. This PR:

  • pops the leftmost operand's comments together with the operator, so they go before the whole expression
  • starts fn and (a, b -> c) clauses on their own line when their arguments have comments
  • stops adding the newlines after an end of line comment inside lists, calls and operators (1.20 never kept them there)

To check it, I added comments to Elixir's own lib files and formatted them twice: about 20 out of 20k changed on the second run before, none after. Formatting the repo itself doesn't change any file.

Comments attached to the leftmost operand of an operator are now
written before the whole expression, function clauses with comments
in their arguments start on their own line, and newlines after end
of line comments are no longer added inside lists, calls and
operators.

Closes elixir-lang#16007
@josevalim
josevalim merged commit 689ce8a into elixir-lang:main Oct 20, 2026
15 checks passed
@mandar1045
mandar1045 deleted the fix-formatter-comment-stability branch October 21, 2026 02:07
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.

Formatter output changes on a second run when there are comments

2 participants