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

Allow disabling formatting for a region of code. #1522

Merged
merged 6 commits into from
Aug 14, 2024
Merged

Conversation

munificent
Copy link
Member

Allow disabling formatting for a region of code.

There are two special line comments:

// dart format off
// dart format on

Any code between those keeps its original formatting completely, including indentation. It just splats the original unformatted code into the output.

Users can have as many of those pairs of comments as they want and formatting is disabled in them. The comments do not nest.

I didn't update the docs or CHANGELOG because this feature only works in the new tall style which is still behind an experiment flag. When it ships, I'll update the docs.

Fix #361.

There are only a few selection test files, and there were a couple of
tiles in top_level/ that didn't really have to do with top-level code
and were just in there for lack of a better home.

I'm about to add tests for disabling formatting, so I figured it makes
sense to have an "other" directory for all of these tests. This commit
just creates that directory and moves the existing tests there.
There are two special line comments:

```
// dart format off
// dart format on
```

Any code between those keeps its original formatting completely,
including indentation. It just splats the original unformatted code into
the output.
@srawlins
Copy link
Member

Drive-by suggestion: the analyzer offers a few "warnings" for other machine-meaningful comments (mostly // @dart=x.y), for example maybe when it looks like the user is trying to write a Dart Language Version comment, but didn't get it quite right. We could offer similar "warnings" or lint rules for "dart-format-toggling" comments. E.g. misspelled "dart format on/off", "dart format yes/no", "dart format true/false", /* dart format on/off */ "off" without "on", "on" without "off", etc. In order to support this feature, we'd take any static analysis suggestions in the dart-lang/linter or dart-lang/sdk repo issue trackers. Or if it is expected to be used exceedingly rarely, these ideas may be just low priority.

@munificent munificent mentioned this pull request Aug 13, 2024
22 tasks
lib/src/back_end/code.dart Outdated Show resolved Hide resolved
lib/src/back_end/code.dart Outdated Show resolved Hide resolved
munificent and others added 3 commits August 13, 2024 18:52
# Conflicts:
#	CHANGELOG.md
Co-authored-by: Nate Bosch <nbosch@google.com>
@munificent munificent merged commit dd1f7d9 into main Aug 14, 2024
7 checks passed
@munificent munificent deleted the disable-region branch August 14, 2024 21:18
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.

dartfmt should recognize off/on delimiter comment pairs to temporarily disable reformatting
4 participants