@@ -88,7 +88,7 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
88
88
DeclarationPhaseIntrospector declarationsPhaseIntrospector) async {
89
89
// TODO(davidmorgan): this is a hack to access analyzer internals; remove.
90
90
introspector = declarationsPhaseIntrospector;
91
- return AnalyzerMacroExecutionResult (
91
+ return await AnalyzerMacroExecutionResult . expandTemplates (
92
92
target,
93
93
await _impl._host.augment (
94
94
name, AugmentRequest (phase: 2 , target: target.qualifiedName)));
@@ -100,7 +100,7 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
100
100
DefinitionPhaseIntrospector definitionPhaseIntrospector) async {
101
101
// TODO(davidmorgan): this is a hack to access analyzer internals; remove.
102
102
introspector = definitionPhaseIntrospector;
103
- return AnalyzerMacroExecutionResult (
103
+ return await AnalyzerMacroExecutionResult . expandTemplates (
104
104
target,
105
105
await _impl._host.augment (
106
106
name, AugmentRequest (phase: 3 , target: target.qualifiedName)));
@@ -111,7 +111,7 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
111
111
MacroTarget target, TypePhaseIntrospector typePhaseIntrospector) async {
112
112
// TODO(davidmorgan): this is a hack to access analyzer internals; remove.
113
113
introspector = typePhaseIntrospector;
114
- return AnalyzerMacroExecutionResult (
114
+ return await AnalyzerMacroExecutionResult . expandTemplates (
115
115
target,
116
116
await _impl._host.augment (
117
117
name, AugmentRequest (phase: 1 , target: target.qualifiedName)));
@@ -124,9 +124,27 @@ class AnalyzerRunningMacro implements injected.RunningMacro {
124
124
/// functionality of `MacroExecutionResult` .
125
125
class AnalyzerMacroExecutionResult implements injected.MacroExecutionResult {
126
126
final MacroTarget target;
127
- final AugmentResponse augmentResponse;
127
+ @override
128
+ final Map <Identifier , Iterable <DeclarationCode >> typeAugmentations;
129
+
130
+ AnalyzerMacroExecutionResult (
131
+ this .target, Iterable <DeclarationCode > declarations)
132
+ // TODO(davidmorgan): this assumes augmentations are for the macro
133
+ // application target. Instead, it should be explicit in
134
+ // `AugmentResponse`.
135
+ : typeAugmentations = {(target as Declaration ).identifier: declarations};
136
+
137
+ static Future <AnalyzerMacroExecutionResult > expandTemplates (
138
+ MacroTarget target, AugmentResponse augmentResponse) async {
139
+ final declarations = < DeclarationCode > [];
140
+ for (final augmentation in augmentResponse.augmentations) {
141
+ declarations.add (
142
+ DeclarationCode .fromParts (await _expandTemplates (augmentation.code)));
143
+ }
144
+ return AnalyzerMacroExecutionResult (target, declarations);
145
+ }
128
146
129
- AnalyzerMacroExecutionResult ( this .target, this .augmentResponse);
147
+ Future < void > resolveTypes () async {}
130
148
131
149
@override
132
150
List <Diagnostic > get diagnostics => [];
@@ -155,16 +173,6 @@ class AnalyzerMacroExecutionResult implements injected.MacroExecutionResult {
155
173
156
174
@override
157
175
void serialize (Object serializer) => throw UnimplementedError ();
158
-
159
- @override
160
- Map <Identifier , Iterable <DeclarationCode >> get typeAugmentations => {
161
- // TODO(davidmorgan): this assumes augmentations are for the macro
162
- // application target. Instead, it should be explicit in
163
- // `AugmentResponse`.
164
- (target as Declaration ).identifier: augmentResponse.augmentations
165
- .map ((a) => DeclarationCode .fromParts ([a.code]))
166
- .toList (),
167
- };
168
176
}
169
177
170
178
extension MacroTargetExtension on MacroTarget {
@@ -176,3 +184,35 @@ extension MacroTargetExtension on MacroTarget {
176
184
name: element.displayName);
177
185
}
178
186
}
187
+
188
+ /// Converts [code] to a mix of `Identifier` and `String` .
189
+ ///
190
+ /// Looks up references of the form `{{uri#name}}` using `resolveIdentifier` .
191
+ Future <List <Object >> _expandTemplates (String code) async {
192
+ final result = < Object > [];
193
+ var index = 0 ;
194
+ while (index < code.length) {
195
+ final start = code.indexOf ('{{' , index);
196
+ if (start == - 1 ) {
197
+ result.add (code.substring (index));
198
+ break ;
199
+ }
200
+ result.add (code.substring (index, start));
201
+ final end = code.indexOf ('}}' , start);
202
+ if (end == - 1 ) {
203
+ throw ArgumentError ('Unmatched opening brace: $code ' );
204
+ }
205
+ final name = code.substring (start + 2 , end);
206
+ final parts = name.split ('#' );
207
+ if (parts.length != 2 ) {
208
+ throw ArgumentError ('Expected "uri#name" in: $name ' );
209
+ }
210
+ final uri = Uri .parse (parts[0 ]);
211
+ final identifier = await (introspector as TypePhaseIntrospector )
212
+ // ignore: deprecated_member_use
213
+ .resolveIdentifier (uri, parts[1 ]);
214
+ result.add (identifier);
215
+ index = end + 2 ;
216
+ }
217
+ return result;
218
+ }
0 commit comments