-
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.
Format list literals (and some other things). (#1281)
Format list literals (and some other things). I'm trying to keep these PRs mostly well-contained and not just adding a random pile of formatting. But I'm also trying to make sure I don't drop stuff on the floor. For this one, I filled in a few more things that lists touch on: - Handle type argument lists in lists and function calls. - Support block-like formatting for right-hand sides of `=`. - Format parenthesized expressions. - Format named type annotations with type arguments and nullable types. - Add support for states with different costs so we can prioritize splitting choices. Co-authored-by: Nate Bosch <nbosch@google.com>
- Loading branch information
1 parent
c179702
commit d8baf12
Showing
17 changed files
with
573 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
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,83 @@ | ||
// Copyright (c) 2023, 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'; | ||
|
||
/// A piece for any construct where `=` is followed by an expression: variable | ||
/// initializer, assignment, constructor initializer, etc. Assignments can be | ||
/// formatted three ways: | ||
/// | ||
/// [State.initial] No split at all: | ||
/// | ||
/// ``` | ||
/// var x = 123; | ||
/// ``` | ||
/// | ||
/// [_insideValue] If the value is a delimited "block-like" expression, | ||
/// then we can split inside the block but not at the `=` with no additional | ||
/// indentation: | ||
/// | ||
/// ``` | ||
/// var list = [ | ||
/// element, | ||
/// ]; | ||
/// ``` | ||
/// | ||
/// [State.split] Split after the `=`: | ||
/// | ||
/// ``` | ||
/// var name = | ||
/// longValueExpression; | ||
/// ``` | ||
class AssignPiece extends Piece { | ||
/// Split inside the value but not at the `=`. | ||
/// | ||
/// This is only allowed when the value is a delimited expression. | ||
static const State _insideValue = State(1); | ||
|
||
/// Split after the `=` and allow splitting inside the value. | ||
/// | ||
/// This is more costly because, when it's possible to split inside a | ||
/// delimited value, we want to prefer that. | ||
static const State _atEquals = State(2, cost: 2); | ||
|
||
/// The left-hand side of the `=` and the `=` itself. | ||
final Piece target; | ||
|
||
/// The right-hand side of the `=`. | ||
final Piece value; | ||
|
||
final bool _isValueDelimited; | ||
|
||
AssignPiece(this.target, this.value, {required bool isValueDelimited}) | ||
: _isValueDelimited = isValueDelimited; | ||
|
||
@override | ||
List<State> get states => [if (_isValueDelimited) _insideValue, _atEquals]; | ||
|
||
@override | ||
void format(CodeWriter writer, State state) { | ||
writer.format(target); | ||
|
||
// A split inside the value forces splitting at the "=" unless it's a | ||
// delimited expression. | ||
if (state == State.initial) writer.setAllowNewlines(false); | ||
|
||
// Don't indent a split delimited expression. | ||
if (state != _insideValue) writer.setIndent(Indent.expression); | ||
|
||
writer.splitIf(state == _atEquals); | ||
writer.format(value); | ||
} | ||
|
||
@override | ||
void forEachChild(void Function(Piece piece) callback) { | ||
callback(target); | ||
callback(value); | ||
} | ||
|
||
@override | ||
String toString() => 'Assign'; | ||
} |
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
Oops, something went wrong.