-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't allow a block argument to be named. (#1561)
There are three ways an argument list can be formatted: ```dart // 1. All inline: function(argument, another); // 2. Block formatted: function(argument, [ element, element, ]); // 3. Tall style: function( argument, another, ); ``` One of the trickiest parts of the formatter is deciding the heuristics to choose between 2 and 3. Prior to this PR, there is a fairly subtle rule: Arguments before the block argument can be named or not, the block argument can be named or not, and the argument after the block argument can be named or not, but only one of those three sections can have a named argument. The intent of that rule is to allow named block arguments and named non-block arguments, while avoiding multiple argument names on the same line when block formatted. Also, it allows an argument list containing only a named argument to be block formatted: ```dart function(name: [ element, ]); ``` From looking at how contributors to Flutter have hand-formatted their code, it doesn't look like this level of complexity is well motivated. And allowing a single named argument to be block formatted but not if there are other named arguments means that adding a second named argument to a function can cause the entire argument list to be reformatted and indented differently. That can be a lot of churn if the block argument is itself a large nested widget tree. This PR tweaks that rule to something simpler: 1. The block argument must be positional. 2. Other non-block arguments have no restrictions on whether they can be named or not. This reduces diff churn and means that widget trees (which almost always use named arguments) have a more consistent (but taller) style across the entire tree. From talking to Michael Goderbauer, this seems like a reasonable trade-off to me.
- Loading branch information
1 parent
ab9cbef
commit 30dce1c
Showing
10 changed files
with
199 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,52 @@ | ||
40 columns | | ||
### Test how named arguments interact with block formatting. | ||
>>> Block format all positional. | ||
function(be, fore, [element, element], after); | ||
<<< | ||
function(be, fore, [ | ||
element, | ||
element, | ||
], after); | ||
>>> Block format named only leading. | ||
function(a: be, b: fore, [element, element], after); | ||
<<< | ||
function(a: be, b: fore, [ | ||
element, | ||
element, | ||
], after); | ||
>>> Block format named only block argument. | ||
function(be, fore, a: [element, element], after); | ||
<<< | ||
function(be, fore, a: [ | ||
element, | ||
element, | ||
], after); | ||
>>> Block format named only trailing. | ||
function(be, fore, [element, element], a: after); | ||
<<< | ||
function(be, fore, [ | ||
element, | ||
element, | ||
], a: after); | ||
>>> Don't block format named leading and block argument. | ||
function(a: be, fore, b: [element, element], after); | ||
>>> A function block argument can't be named. | ||
function(name: () {;}); | ||
<<< | ||
function( | ||
a: be, | ||
fore, | ||
b: [element, element], | ||
after, | ||
name: () { | ||
; | ||
}, | ||
); | ||
>>> Don't block format named leading and trailing. | ||
function(a: be, fore, [element, element], b: after); | ||
>>> A collection block argument can't be named. | ||
function(name: [element, element, element, element]); | ||
<<< | ||
function( | ||
a: be, | ||
fore, | ||
[element, element], | ||
b: after, | ||
name: [ | ||
element, | ||
element, | ||
element, | ||
element, | ||
], | ||
); | ||
>>> Don't block format named block argument and trailing. | ||
function(be, fore, a: [element, element], b: after); | ||
>>> If there are multiple functions, don't block format, even if only one is positional. | ||
function(a: () {;}, () {;}); | ||
<<< | ||
function( | ||
be, | ||
fore, | ||
a: [element, element], | ||
b: after, | ||
a: () { | ||
; | ||
}, | ||
() { | ||
; | ||
}, | ||
); | ||
>>> Don't block format all named. | ||
function(a: be, b: fore, c: [element, element], d: after); | ||
>>> If there are multiple collections, don't block format, even if only one is positional. | ||
function(a: [element], [element, element, element, element, element]); | ||
<<< | ||
function( | ||
a: be, | ||
b: fore, | ||
c: [element, element], | ||
d: after, | ||
); | ||
a: [element], | ||
[ | ||
element, | ||
element, | ||
element, | ||
element, | ||
element, | ||
], | ||
); | ||
>>> Other non-block arguments can be named or not. | ||
function(1, a: 2, 3, b: 4, [element, element], c: 5); | ||
<<< | ||
function(1, a: 2, 3, b: 4, [ | ||
element, | ||
element, | ||
], c: 5); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.