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

Make the formatter aware of the language version of what it's formatting #1402

Open
5 tasks
munificent opened this issue Feb 21, 2024 · 0 comments
Open
5 tasks
Labels
enhancement tall Issues related to the new "tall" style (#1253)

Comments

@munificent
Copy link
Member

Dart format is moving to a new "tall" style designed to be more readable for the kind of deeply nested declarative code that is common in Flutter (#1253). When users install a new Dart SDK, they don't generally expect the formatter to radically change the style of their already-formatted code, and we don't want that to happen when the new style ships either.

We've discussed a few ways to let users control when they adopt the new tall style, and the best approach we've been able to come up with is to tie it to language version. Let's say that dart format support for the tall style ships in Dart SDK 3.x.0. The idea is:

  • Any Dart file whose language version is 3.x or greater will automatically be formatted using the new tall style.

  • Any Dart file whose language version is older than 3.x will continue to use the current "short" style.

This means that by default, when you update to a new Flutter or Dart SDK, your formatting won't change. It's only when you change the Dart SDK constraint in your pubspec to opt into the new language version that you'll also get a formatting change. (We also intend to support some explicit way to opt in to the new style for older language versioned files.)

To implement that, dart_style needs to know the language version of everything it's formatting. Currently, it doesn't. This issue is to track adding support for that. It may need some elaboration, but the basic idea is:

  • The FormatCommand class which drives the dart format command-line tool and accesses the file system directly should determine the language version of each file it processes. This means looking for either a pubspec or package configuration file in surrounding directories to determine the default language version. I'm not sure what the exact logic should be here, but it should probably follow how dart analyze behaves. Ideally, there is some code we can reuse.

  • Extend the DartFormatter library API to support passing in a language version for each file. If omitted, default to something reasonable (without hitting the file system). Probably just the most recent language version.

  • Talk to the analyzer and other IDE integration folks about updating their integration to use that new API and pass in language versions when requesting formatting.

  • If a compilation unit being formatted (from the command line or through the DartFormatter library API) has a @dart= language version comment, honor that to determine the style used for that file.

  • There is currently a very nasty hack in dart_style to workaround the fact that Dart 3.0 had a couple of breaking syntax changes. Once the formatter actually knows the language version of what it's parsing, we can just parse at that version and remove that hack.

@munificent munificent added enhancement tall Issues related to the new "tall" style (#1253) labels Feb 21, 2024
@kallentu kallentu removed their assignment May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement tall Issues related to the new "tall" style (#1253)
Projects
None yet
Development

No branches or pull requests

2 participants