Skip to content

Only add dart format width comment on format #733

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

Merged
merged 2 commits into from
Dec 12, 2024
Merged

Conversation

natebosch
Copy link
Member

Closes #724

A use case with a custom formatCode callback can reasonably use a
different line width than the default, so only add the width comment
when this package is responsible for performing a format.

Uses with a custom header will no longer slot the width comment in
following the header.

Some outputs will no longer have the comment. This is not breaking since
there has been no published since the comment was introduced.

Closes #724

A use case with a custom `formatCode` callback can reasonably use a
different line width than the default, so only add the width comment
when this package is responsible for performing a format.

Uses with a custom header will no longer slot the width comment in
following the header.

Some outputs will no longer have the comment. This is not breaking since
there has been no published since the comment was introduced.
@natebosch natebosch requested a review from jakemac53 December 10, 2024 00:07
Files using the shared part builder will need to resolve format width
conflicts using some other mechanism. A shared part file can have
outputs contributed from different code generators that are unrelated,
and may have different behavior around formatting, so it is not possible
to write a single width comment that applies to the combined file.
@kevmoo
Copy link
Member

kevmoo commented Dec 12, 2024

So the idea here is that we don't have a good way to extract the style info from analysis_options, so we're just going to hard-wire?

@natebosch
Copy link
Member Author

So the idea here is that we don't have a good way to extract the style info from analysis_options, so we're just going to hard-wire?

Yes. It is not guaranteed that the configuration is in a file that we make readable during builds.

@natebosch natebosch merged commit 9f95ac3 into master Dec 12, 2024
12 checks passed
@natebosch natebosch deleted the width-on-format branch December 12, 2024 21:50
@marvin-kolja
Copy link

marvin-kolja commented Jun 17, 2025

@natebosch, may I ask why the comment was added as a first line? This conflicts with GitHub Linguist to recognize the file as generated (https://github.com/github-linguist/linguist/blob/main/lib/linguist/generated.rb#L737). I was about to fix a related issue in Freezed (rrousselGit/freezed#1261) when I came across this.

EDIT:
Sorry, I missed your comment here: #724 (comment).

Anyway, it could make sense to change the recognition in GitHub Linguist, as that's the tool outside of the Dart ecosystem. However, this change might have affected other tools, not just GitHub Linguist. What are your thoughts?

@natebosch
Copy link
Member Author

I'm not sure what it would take to change the ecosystem to allow the comment on the second line. If we need to reorder the lines we either need a breaking change to the signature of the callback (we'd need to pass the header and body separately to put the line width comment between them) or we can do something hacky and hardcode support for the generate content line specifically. I'd lean towards doing the latter so I'll open a PR with that approach, but I haven't fully thought it through and I can't roll out the change. @kevmoo @jakemac53 if you think this is urgent you may want to run with #760

@marvin-kolja
Copy link

Thanks for the quick response.

For now, I've also opened a request to improve how GitHub Linguist recognizes generated files by looking at the first 3 lines github-linguist/linguist#7448.

Like that second idea, I will have a look.

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.

Add "// dart format width=80" comment in generated code
4 participants