-
Notifications
You must be signed in to change notification settings - Fork 128
Better formatting for long type parameter bounds. #1576
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
+164
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
// 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'; | ||
|
||
/// A piece for a type parameter and its bound. | ||
/// | ||
/// Handles not splitting before `extends` if we can split inside the bound's | ||
/// own type arguments, or splitting before `extends` if that isn't enough to | ||
/// get it to fit. | ||
final class TypeParameterBoundPiece extends Piece { | ||
/// Split inside the type arguments of the bound, but not at `extends`, as in: | ||
/// | ||
/// class C< | ||
/// T extends Map< | ||
/// LongKeyType, | ||
/// LongValueType | ||
/// > | ||
/// >{} | ||
static const State _insideBound = State(1); | ||
|
||
/// Split at `extends`, like: | ||
/// | ||
/// class C< | ||
/// LongTypeParameters | ||
/// extends LongBoundType | ||
/// >{} | ||
static const State _beforeExtends = State(2); | ||
|
||
/// The type parameter name. | ||
final Piece _typeParameter; | ||
|
||
/// The bound with the preceding `extends` keyword. | ||
final Piece _bound; | ||
|
||
TypeParameterBoundPiece(this._typeParameter, this._bound); | ||
|
||
@override | ||
List<State> get additionalStates => const [_insideBound, _beforeExtends]; | ||
|
||
@override | ||
bool allowNewlineInChild(State state, Piece child) => switch (state) { | ||
State.unsplit => false, | ||
_insideBound => child == _bound, | ||
_beforeExtends => true, | ||
_ => throw ArgumentError('Unexpected state.'), | ||
}; | ||
|
||
@override | ||
void format(CodeWriter writer, State state) { | ||
if (state == _beforeExtends) writer.pushIndent(Indent.expression); | ||
writer.format(_typeParameter); | ||
writer.splitIf(state == _beforeExtends); | ||
writer.format(_bound); | ||
if (state == _beforeExtends) writer.popIndent(); | ||
} | ||
|
||
@override | ||
void forEachChild(void Function(Piece piece) callback) { | ||
callback(_typeParameter); | ||
callback(_bound); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,67 @@ | ||
>>> Conveniently fits in the new style. | ||
abstract interface class TypeAnalyzerOperations< | ||
TypeStructure extends SharedTypeStructure<TypeStructure>, | ||
Variable extends Object, | ||
TypeParameterStructure extends SharedTypeParameterStructure<TypeStructure>, | ||
TypeDeclarationType extends Object, | ||
TypeDeclaration extends Object> | ||
implements FlowAnalysisOperations<Variable, SharedTypeView<TypeStructure>> { | ||
// ... | ||
} | ||
<<< | ||
abstract interface class TypeAnalyzerOperations< | ||
TypeStructure extends SharedTypeStructure<TypeStructure>, | ||
Variable extends Object, | ||
TypeParameterStructure extends SharedTypeParameterStructure<TypeStructure>, | ||
TypeDeclarationType extends Object, | ||
TypeDeclaration extends Object | ||
> | ||
implements FlowAnalysisOperations<Variable, SharedTypeView<TypeStructure>> { | ||
// ... | ||
} | ||
>>> Make some type arguments longer to force it to split. | ||
abstract interface class TypeAnalyzerOperations< | ||
TypeStructure extends SharedTypeStructure<TypeStructure>, | ||
Variable extends Object, | ||
TypeParameterStructure extends SharedTypeParameterStructure<LongerTypeStructure>, | ||
TypeDeclarationType extends Object, | ||
TypeDeclaration extends Object> | ||
implements FlowAnalysisOperations<Variable, SharedTypeView<TypeStructure>> { | ||
// ... | ||
} | ||
<<< | ||
abstract interface class TypeAnalyzerOperations< | ||
TypeStructure extends SharedTypeStructure<TypeStructure>, | ||
Variable extends Object, | ||
TypeParameterStructure | ||
extends SharedTypeParameterStructure<LongerTypeStructure>, | ||
TypeDeclarationType extends Object, | ||
TypeDeclaration extends Object | ||
> | ||
implements FlowAnalysisOperations<Variable, SharedTypeView<TypeStructure>> { | ||
// ... | ||
} | ||
>>> Even longer for more splitting. | ||
abstract interface class TypeAnalyzerOperations< | ||
TypeStructure extends SharedTypeStructure<TypeStructure>, | ||
Variable extends Object, | ||
TypeParameterStructure extends SharedTypeParameterStructure<LongerTypeStructure, AnotherTypeArgument>, | ||
TypeDeclarationType extends Object, | ||
TypeDeclaration extends Object> | ||
implements FlowAnalysisOperations<Variable, SharedTypeView<TypeStructure>> { | ||
// ... | ||
} | ||
<<< | ||
abstract interface class TypeAnalyzerOperations< | ||
TypeStructure extends SharedTypeStructure<TypeStructure>, | ||
Variable extends Object, | ||
TypeParameterStructure extends SharedTypeParameterStructure< | ||
LongerTypeStructure, | ||
AnotherTypeArgument | ||
>, | ||
TypeDeclarationType extends Object, | ||
TypeDeclaration extends Object | ||
> | ||
implements FlowAnalysisOperations<Variable, SharedTypeView<TypeStructure>> { | ||
// ... | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.