-
Notifications
You must be signed in to change notification settings - Fork 205
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
add InheritedWidget macro example and fix up FunctionalWidget #3256
Merged
Merged
Changes from 1 commit
Commits
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 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,97 @@ | ||
// 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. | ||
|
||
// There is no public API exposed yet, the in-progress API lives here. | ||
import 'package:_fe_analyzer_shared/src/macros/api.dart'; | ||
|
||
/// A macro that annotates a class and turns it into an inherited widget. | ||
/// | ||
/// This will fill in any "holes" that do not have custom implementations, | ||
/// specifically the following items will be added if they don't exist: | ||
/// | ||
/// - Make the class extend `InheritedWidget`. | ||
/// - Add a constructor that will initialize any fields that are defined, and | ||
/// take `key` and `child` parameters which it forwards to the super | ||
/// constructor. | ||
/// - Add static `of` and `maybeOf` methods which take a build context and | ||
/// return an instance of this class using `dependOnIheritedWidgetOfExactType`. | ||
/// - Add an `updateShouldNotify` method which does checks for equality of all | ||
/// fields. | ||
macro class InheritedWidget implements ClassTypesMacro, ClassDeclarationsMacro { | ||
const InheritedWidget(); | ||
|
||
@override | ||
void buildTypesForClass( | ||
ClassDeclaration clazz, ClassTypeBuilder builder) { | ||
if (clazz.superclass != null) return; | ||
// TODO: Add `extends InheritedWidget` once we have an API for that. | ||
} | ||
|
||
@override | ||
Future<void> buildDeclarationsForClass(IntrospectableClassDeclaration clazz, MemberDeclarationBuilder builder) async { | ||
final fields = await builder.fieldsOf(clazz); | ||
final methods = await builder.methodsOf(clazz); | ||
if (!methods.any((method) => method is ConstructorDeclaration)) { | ||
builder.declareInType(DeclarationCode.fromParts([ | ||
'const ${clazz.identifier.name}(', | ||
'{', | ||
for (var field in fields)...[ | ||
field.type.isNullable ? '' : 'required ', | ||
field.identifier, | ||
',', | ||
], | ||
'super.key,', | ||
'required super.child,', | ||
'});', | ||
])); | ||
} | ||
|
||
final buildContext = | ||
// ignore: deprecated_member_use | ||
await builder.resolveIdentifier(Uri.parse('package:flutter/widgets.dart'), 'BuildContext'); | ||
if (!methods.any((method) => method.identifier.name == "maybeOf")) { | ||
builder.declareInType(DeclarationCode.fromParts([ | ||
'static ', | ||
clazz.identifier, | ||
'? maybeOf(', | ||
buildContext, | ||
' context) => context.dependOnInheritedWidgetOfExactType<', | ||
clazz.identifier, | ||
'>();', | ||
])); | ||
} | ||
|
||
if (!methods.any((method) => method.identifier.name == "of")) { | ||
builder.declareInType(DeclarationCode.fromParts([ | ||
'static ', | ||
clazz.identifier, | ||
' of(', | ||
buildContext, | ||
''' context) { | ||
final result = this.maybeOf(context); | ||
assert(result != null, 'No ${clazz.identifier.name} found in context'); | ||
return result!; | ||
}''', | ||
])); | ||
} | ||
|
||
if (!methods.any((method) => method.identifier.name == 'updateShouldNotify')) { | ||
// ignore: deprecated_member_use | ||
final override = await builder.resolveIdentifier( | ||
Uri.parse('package:meta/meta.dart'), 'override'); | ||
builder.declareInType(DeclarationCode.fromParts([ | ||
'@', | ||
override, | ||
' bool updateShouldNotify(', | ||
clazz.identifier, | ||
' oldWidget) =>', | ||
...[ | ||
for (var field in fields) | ||
'oldWidget.${field.identifier.name} != this.${field.identifier.name}', | ||
].joinAsCode(' || '), | ||
';', | ||
])); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Looks like this hasn't been run through dart format?
(I noticed the lack of space before
...
. This is what working on the formatter has done to me. :) )...Oh, I just realized you can't because of the
macro
keywords. :(There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah because dart format doesn't allow the
macro
keyword so its a pain to use it lol.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fwiw, we should have a larger discussion, but the current way the formatter works with experiments is causing other problems with deprecating old experiments. We should re-evaluate the whole thing and come up with a way to explicitly enable any experiments imo.