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 formatting when comment is before first statement in anonymous function #3128

Merged
merged 4 commits into from May 23, 2024

Conversation

Acepie
Copy link
Contributor

@Acepie Acepie commented May 11, 2024

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.

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you. I've left one note inline

compiler-core/src/ast/untyped.rs Outdated Show resolved Hide resolved
@Acepie Acepie force-pushed the fixFunctionCommentAtStartOfBody branch from a0f8800 to c830340 Compare May 20, 2024 14:12
@Acepie Acepie force-pushed the fixFunctionCommentAtStartOfBody branch from c830340 to 92effe0 Compare May 23, 2024 14:37
@lpil lpil merged commit 17604c9 into gleam-lang:main May 23, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comment put in wrong place by formatter
2 participants