-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format adjacent strings. Most of this was fairly straightforward, but the two tricky bits are: ### 1. Deciding whether or not to indent There are some rules around whether subsequent strings in the adjacent strings get indented or not. The answer is yes in some cases to avoid confusion: ``` var list = function( 'string 1', 'adjacent' 'string 2', 'string 3', ]; ``` But not in others since it looks nicer to line them up when possible: ``` var description = 'some text ' 'more text'; ``` ### 2. Handling `test()` and `group()` It's really important that test functions don't end up fully split because doing so would lead to the inner function expression getting indented +2: ``` test('this looks good', () { body; }); test( 'this looks bad', () { body; }, ); ``` Test descriptions often span multiple lines using adjacent strings: ``` test('this is a very long test description ' 'spanning multiple lines, () { body; }); ``` Normally, the newline inside the adjacent strings would cause the entire argument list to split. The old style handles that (I think) by allowing multiple block-formatted arguments and then treating both the adjacent strings and the function expressions as block arguments. The new style currently only allows a single block argument (because in almost all of the Flutter code I found using block formatting, one argument was sufficient). So I chose a more narrowly targeted rule here where we allow adjacent strings to not prevent block formatting only if the adjacent strings are the first argument and the block argument is a function expression as the next argument. I left a TODO to see if we want to iterate on that rule, but I think it works pretty well. ### Other stuff Unlike the old style, I chose to always split between adjacent strings. The old style will preserve newlines there but if a user chooses to deliberately put multiple adjacent strings on the same line and they fit, it will honor it. That didn't seem useful to me, so now they just always split. I don't think adjacent strings ever look good on the same line. I ended up moving the state to track which elements in a ListPiece out of ListPiece and into the ListElements themselves. I think it's clearer this way and will be easier to evolve if we end up supporting multiple block formatted elements in a single list.
- Loading branch information
1 parent
990df02
commit e697b6e
Showing
8 changed files
with
605 additions
and
24 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
import '../back_end/code_writer.dart'; | ||
import '../constants.dart'; | ||
import 'piece.dart'; | ||
|
||
/// Piece for a series of adjacent strings, like: | ||
/// | ||
/// var message = | ||
/// 'This is a long message ' | ||
/// 'split into multiple strings'; | ||
class AdjacentStringsPiece extends Piece { | ||
final List<Piece> _strings; | ||
|
||
/// Whether strings after the first should be indented. | ||
final bool _indent; | ||
|
||
AdjacentStringsPiece(this._strings, {bool indent = true}) : _indent = indent; | ||
|
||
@override | ||
void format(CodeWriter writer, State state) { | ||
if (_indent) writer.setIndent(Indent.expression); | ||
|
||
for (var i = 0; i < _strings.length; i++) { | ||
if (i > 0) writer.newline(); | ||
writer.format(_strings[i]); | ||
} | ||
} | ||
|
||
@override | ||
void forEachChild(void Function(Piece piece) callback) { | ||
_strings.forEach(callback); | ||
} | ||
} |
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.