fix formatting when comment is before first statement in anonymous function #3128
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #3109
The root cause of the issue here is that 2d56b6a#diff-6e737fd16c3877b93866d0f63709896f2f7b2f1afa64a6543a8344f99f7ebe96R2383 with this change the arg wrapping is dependent on a passed in location to determine when to stop looking for comments but in the AST for functions we use locations inconsistently between top level function definitions and anonymous function definitions. In top level functions, the location refers to only the function head and so https://github.com/gleam-lang/gleam/blob/main/compiler-core/src/format.rs#L753 points to the exact opening bracket of the body. On the other hand for anonymous functions, the location actually include the whole function meaning that the ast does not know where the real location of the body opening bracket is. Instead the current implementation looks at the start of the first statement https://github.com/gleam-lang/gleam/blob/main/compiler-core/src/format.rs#L794 hence the issue.
To solve the issue I added a new field to the AST that would consistently be the location of the head. For top level functions it would be exactly the same as the location whereas for anonymous functions the behavior would match top level.