Skip to content

Formatting support for named arguments anywhere. #1094

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
Feb 23, 2022

Conversation

munificent
Copy link
Member

@munificent munificent commented Feb 17, 2022

I opted to add support for this while being minimally invasive to the
existing style and formatting. All existing code which does not have any
positional arguments after named ones retains its previous formatting.

For new argument lists that use positional arguments after named ones,
I try to mostly follow the existing style rules even though they can be
fairly complex. In particular, it will be pretty aggressive about
applying block-style formatting to function literals inside argument
lists even with named args anywhere. I think that's important to support
the main use case I know of for the feature which is trailing positional
closures like:

function(named: 1, another: 2, () {
  block...
});

In argument lists using named args anywhere that don't have block
functions, it treats all arguments like named ones. That provides nice
simple formatting like:

function(
    argument1,
    named: argument2,
    argument3,
    another: argument4);

I think that does a good job of highlighting which arguments are named,
which is what we want.

Fix #1072.

I opted to add support for this while being minimally invasive to the
existing style and formatting. All existing code which does not have any
positional arguments after named ones retains its previous formatting.

For new argument lists that use positional arguments after named ones,
I try to mostly follow the existing style rules even though they can be
fairly complex. In particular, it will be pretty aggressive about
applying block-style formatting to function literals inside argument
lists even with named args anywhere. I think that's important to support
the main use case I know of for the feature which is trailing positional
closures like:

function(named: 1, another: 2, () {
  block...
});

In argument lists using named args anywhere that don't have block
functions, it treats all arguments like named ones. That provides nice
simple formatting like:

function(
    argument1,
    named: argument2,
    argument3,
    another: argument4);

I think that does a good job of highlighting which arguments are named,
which is what we want.

Fix #1072.
@munificent munificent requested a review from natebosch February 17, 2022 02:00
///
/// Returns a list of (start, end] indexes if found, otherwise returns `null`.
static List<int>? _contiguousFunctions(List<Expression> arguments) {
int? functionsStart;
Copy link
Member Author

Choose a reason for hiding this comment

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

This code is essentially unchanged from line 66 on. The main difference is that the old code used to use arguments[0] is NamedExpression as a clever check to see if all arguments are named. That doesn't work with named args anywhere so now it iterates the list and checks them all.

@@ -1,3 +1,7 @@
# 2.2.2
Copy link
Member

Choose a reason for hiding this comment

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

How do you normally choose when to bump the feature version? Support for a new language feature could justify it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm usually pretty conservative. This isn't a breaking change and doesn't really add any new "features" (in terms of the CLI arguments or library API). Also, it doesn't change the formatting of any existing code. That sounds like a point release to me, even though it's significant in that it adds support for new syntax. But since no code using that syntax exists in the wild, from the user's perspective, there's no visible change in dart_style at all.

@munificent munificent merged commit 68eab57 into master Feb 23, 2022
@munificent munificent deleted the named-args-anywhere branch February 23, 2022 22:19
final List<Expression> _positional;

/// The named arguments, in order, and any positional arguments that follow
/// at least one named argument.
/// The named arguments, in order. If there are any named arguments that occur
Copy link
Member

Choose a reason for hiding this comment

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

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.

dart_style implementation for named arguments anywhere
2 participants