Skip to content

Extension type constructors #3550

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
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions lib/src/generator/templates.aot_renderers_for_html.dart
Original file line number Diff line number Diff line change
@@ -852,7 +852,7 @@ String renderExtensionType<T extends ExtensionType>(
<dt>on</dt>
<dd>
<ul class="comma-separated clazz-relationships">''');
var context3 = context2.extendedType;
var context3 = context2.representationType;
buffer.writeln();
buffer.write('''
<li>''');
@@ -868,7 +868,10 @@ String renderExtensionType<T extends ExtensionType>(
buffer.writeln();
buffer.write('''
</section>
''');

''');
buffer.write(_renderExtensionType_partial_constructors_6(context2));
buffer.writeln();
if (context2.hasPublicInstanceFields == true) {
buffer.writeln();
buffer.write('''
@@ -879,31 +882,31 @@ String renderExtensionType<T extends ExtensionType>(
var context4 = context2.publicInstanceFieldsSorted;
for (var context5 in context4) {
buffer.write('\n ');
buffer.write(_renderExtensionType_partial_property_6(context5));
buffer.write(_renderExtensionType_partial_property_7(context5));
}
buffer.writeln();
buffer.write('''
</dl>
</section>''');
}
buffer.write('\n\n ');
buffer.write(_renderExtensionType_partial_instance_methods_7(context2));
buffer.write(_renderExtensionType_partial_instance_methods_8(context2));
buffer.write('\n ');
buffer.write(_renderExtensionType_partial_instance_operators_8(context2));
buffer.write(_renderExtensionType_partial_instance_operators_9(context2));
buffer.write('\n ');
buffer.write(_renderExtensionType_partial_static_properties_9(context2));
buffer.write(_renderExtensionType_partial_static_properties_10(context2));
buffer.write('\n ');
buffer.write(_renderExtensionType_partial_static_methods_10(context2));
buffer.write(_renderExtensionType_partial_static_methods_11(context2));
buffer.write('\n ');
buffer.write(_renderExtensionType_partial_static_constants_11(context2));
buffer.write(_renderExtensionType_partial_static_constants_12(context2));
buffer.writeln();
buffer.write('''

</div><!-- /.main-content -->

<div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
''');
buffer.write(_renderExtensionType_partial_search_sidebar_12(context0));
buffer.write(_renderExtensionType_partial_search_sidebar_13(context0));
buffer.writeln();
buffer.write('''
<h5>''');
@@ -917,7 +920,7 @@ String renderExtensionType<T extends ExtensionType>(
</div><!--/.sidebar-offcanvas-->

''');
buffer.write(_renderExtensionType_partial_footer_13(context0));
buffer.write(_renderExtensionType_partial_footer_14(context0));
buffer.writeln();
buffer.writeln();

@@ -2864,33 +2867,36 @@ String _renderExtensionType_partial_container_annotations_5(
ExtensionType context1) =>
_deduplicated_lib_templates_html__container_annotations_html(context1);

String _renderExtensionType_partial_property_6(Field context2) =>
String _renderExtensionType_partial_constructors_6(ExtensionType context1) =>
_deduplicated_lib_templates_html__constructors_html(context1);

String _renderExtensionType_partial_property_7(Field context2) =>
_deduplicated_lib_templates_html__property_html(context2);

String _renderExtensionType_partial_instance_methods_7(
String _renderExtensionType_partial_instance_methods_8(
ExtensionType context1) =>
_deduplicated_lib_templates_html__instance_methods_html(context1);

String _renderExtensionType_partial_instance_operators_8(
String _renderExtensionType_partial_instance_operators_9(
ExtensionType context1) =>
_deduplicated_lib_templates_html__instance_operators_html(context1);

String _renderExtensionType_partial_static_properties_9(
String _renderExtensionType_partial_static_properties_10(
ExtensionType context1) =>
_deduplicated_lib_templates_html__static_properties_html(context1);

String _renderExtensionType_partial_static_methods_10(ExtensionType context1) =>
String _renderExtensionType_partial_static_methods_11(ExtensionType context1) =>
_deduplicated_lib_templates_html__static_methods_html(context1);

String _renderExtensionType_partial_static_constants_11(
String _renderExtensionType_partial_static_constants_12(
ExtensionType context1) =>
_deduplicated_lib_templates_html__static_constants_html(context1);

String _renderExtensionType_partial_search_sidebar_12<T extends ExtensionType>(
String _renderExtensionType_partial_search_sidebar_13<T extends ExtensionType>(
ExtensionTypeTemplateData<T> context0) =>
_deduplicated_lib_templates_html__search_sidebar_html(context0);

String _renderExtensionType_partial_footer_13<T extends ExtensionType>(
String _renderExtensionType_partial_footer_14<T extends ExtensionType>(
ExtensionTypeTemplateData<T> context0) =>
_deduplicated_lib_templates_html__footer_html(context0);

35 changes: 20 additions & 15 deletions lib/src/generator/templates.aot_renderers_for_md.dart
Original file line number Diff line number Diff line change
@@ -491,7 +491,7 @@ String renderExtensionType<T extends ExtensionType>(
buffer.writeln();
buffer.write('''
on ''');
var context2 = context1.extendedType;
var context2 = context1.representationType;
buffer.write(context2.linkedName);
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_source_link_1(context1));
@@ -505,6 +505,8 @@ on ''');
buffer.write(_renderExtensionType_partial_documentation_4(context3));
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_annotations_5(context3));
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_constructors_6(context3));
buffer.writeln();
if (context3.hasPublicInstanceFields == true) {
buffer.writeln();
@@ -514,25 +516,25 @@ on ''');
var context4 = context3.publicInstanceFieldsSorted;
for (var context5 in context4) {
buffer.writeln();
buffer.write(_renderExtensionType_partial_property_6(context5));
buffer.write(_renderExtensionType_partial_property_7(context5));
buffer.writeln();
}
}
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_instance_methods_7(context3));
buffer.write(_renderExtensionType_partial_instance_methods_8(context3));
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_instance_operators_8(context3));
buffer.write(_renderExtensionType_partial_instance_operators_9(context3));
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_static_properties_9(context3));
buffer.write(_renderExtensionType_partial_static_properties_10(context3));
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_static_methods_10(context3));
buffer.write(_renderExtensionType_partial_static_methods_11(context3));
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_static_constants_11(context3));
buffer.write(_renderExtensionType_partial_static_constants_12(context3));
buffer.writeln();
buffer.write('''
{{ /extension }}''');
buffer.write('\n\n');
buffer.write(_renderExtensionType_partial_footer_12(context0));
buffer.write(_renderExtensionType_partial_footer_13(context0));
buffer.writeln();
buffer.writeln();

@@ -1434,29 +1436,32 @@ String _renderExtensionType_partial_documentation_4(ExtensionType context1) =>
String _renderExtensionType_partial_annotations_5(ExtensionType context1) =>
_deduplicated_lib_templates_md__annotations_md(context1);

String _renderExtensionType_partial_property_6(Field context2) =>
String _renderExtensionType_partial_constructors_6(ExtensionType context1) =>
_deduplicated_lib_templates_md__constructors_md(context1);

String _renderExtensionType_partial_property_7(Field context2) =>
_deduplicated_lib_templates_md__property_md(context2);

String _renderExtensionType_partial_instance_methods_7(
String _renderExtensionType_partial_instance_methods_8(
ExtensionType context1) =>
_deduplicated_lib_templates_md__instance_methods_md(context1);

String _renderExtensionType_partial_instance_operators_8(
String _renderExtensionType_partial_instance_operators_9(
ExtensionType context1) =>
_deduplicated_lib_templates_md__instance_operators_md(context1);

String _renderExtensionType_partial_static_properties_9(
String _renderExtensionType_partial_static_properties_10(
ExtensionType context1) =>
_deduplicated_lib_templates_md__static_properties_md(context1);

String _renderExtensionType_partial_static_methods_10(ExtensionType context1) =>
String _renderExtensionType_partial_static_methods_11(ExtensionType context1) =>
_deduplicated_lib_templates_md__static_methods_md(context1);

String _renderExtensionType_partial_static_constants_11(
String _renderExtensionType_partial_static_constants_12(
ExtensionType context1) =>
_deduplicated_lib_templates_md__static_constants_md(context1);

String _renderExtensionType_partial_footer_12<T extends ExtensionType>(
String _renderExtensionType_partial_footer_13<T extends ExtensionType>(
ExtensionTypeTemplateData<T> context0) =>
_deduplicated_lib_templates_md__footer_md(context0);

45 changes: 23 additions & 22 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
@@ -5498,28 +5498,6 @@ class _Renderer_ExtensionType extends RendererBase<ExtensionType> {
parent: r);
},
),
'extendedType': Property(
getValue: (CT_ c) => c.extendedType,
renderVariable:
(CT_ c, Property<CT_> self, List<String> remainingNames) {
if (remainingNames.isEmpty) {
return self.getValue(c).toString();
}
var name = remainingNames.first;
var nextProperty =
_Renderer_ElementType.propertyMap().getValue(name);
return nextProperty.renderVariable(
self.getValue(c) as ElementType,
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_ElementType(c.extendedType, ast, r.template, sink,
parent: r);
},
),
'filePath': Property(
getValue: (CT_ c) => c.filePath,
renderVariable:
@@ -5661,6 +5639,29 @@ class _Renderer_ExtensionType extends RendererBase<ExtensionType> {
parent: r);
},
),
'representationType': Property(
getValue: (CT_ c) => c.representationType,
renderVariable:
(CT_ c, Property<CT_> self, List<String> remainingNames) {
if (remainingNames.isEmpty) {
return self.getValue(c).toString();
}
var name = remainingNames.first;
var nextProperty =
_Renderer_ElementType.propertyMap().getValue(name);
return nextProperty.renderVariable(
self.getValue(c) as ElementType,
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_ElementType(
c.representationType, ast, r.template, sink,
parent: r);
},
),
'sidebarPath': Property(
getValue: (CT_ c) => c.sidebarPath,
renderVariable:
8 changes: 4 additions & 4 deletions lib/src/model/extension_type.dart
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ class ExtensionType extends InheritingContainer with Constructable {
@override
final ExtensionTypeElement element;

late final ElementType extendedType =
late final ElementType representationType =
modelBuilder.typeFrom(element.typeErasure, library);

ExtensionType(this.element, super.library, super.packageGraph);
@@ -56,7 +56,7 @@ class ExtensionType extends InheritingContainer with Constructable {
@override
late final List<ModelElement> allModelElements = [
...super.allModelElements,
...typeParameters,
...constructors,
];

@override
@@ -86,8 +86,8 @@ class ExtensionType extends InheritingContainer with Constructable {
@override
Map<String, CommentReferable> get referenceChildren {
return _referenceChildren ??= {
...extendedType.referenceChildren,
// Override `extendedType` entries with local items.
...representationType.referenceChildren,
// Override `representationType` entries with local items.
...super.referenceChildren,
};
}
16 changes: 9 additions & 7 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
@@ -459,13 +459,15 @@ abstract class ModelElement extends Canonicalization
// The canonical ModelElement for this ModelElement,
// or null if there isn't one.
late final ModelElement? canonicalModelElement = () {
Container? preferredClass;
// TODO(srawlins): Add mixin.
if (enclosingElement is Class ||
enclosingElement is Enum ||
enclosingElement is Extension) {
preferredClass = enclosingElement as Container?;
}
final enclosingElement = this.enclosingElement;
var preferredClass = switch (enclosingElement) {
// TODO(srawlins): Add mixin.
Class() => enclosingElement,
Enum() => enclosingElement,
Extension() => enclosingElement,
ExtensionType() => enclosingElement,
_ => null,
};
return packageGraph.findCanonicalModelElementFor(element,
preferredClass: preferredClass);
}();
6 changes: 4 additions & 2 deletions lib/templates/html/extension_type.html
Original file line number Diff line number Diff line change
@@ -16,15 +16,17 @@
<dt>on</dt>
<dd>
<ul class="comma-separated clazz-relationships">
{{ #extendedType }}
{{ #representationType }}
<li>{{{ linkedName }}}</li>
{{ /extendedType }}
{{ /representationType }}
</ul>
</dd>
</dl>
{{ >container_annotations }}
</section>

{{ >constructors }}

{{ #hasPublicInstanceFields }}
<section class="summary offset-anchor" id="instance-properties">
<h2>Properties</h2>
4 changes: 3 additions & 1 deletion lib/templates/md/extension_type.md
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

{{ #self }}
# {{{ nameWithGenerics }}} {{ kind }}
on {{ #extendedType }}{{{ linkedName }}}{{ /extendedType }}
on {{ #representationType }}{{{ linkedName }}}{{ /representationType }}

{{ >source_link }}

@@ -15,6 +15,8 @@ on {{ #extendedType }}{{{ linkedName }}}{{ /extendedType }}

{{ >annotations }}

{{ >constructors }}

{{ #hasPublicInstanceFields }}
## Properties

18 changes: 18 additions & 0 deletions test/extension_types_test.dart
Original file line number Diff line number Diff line change
@@ -85,6 +85,24 @@ class C {}
);
}

@FailingTest(reason: 'Not implemented yet')
void test_referenceToExtensionTypeConstructor() async {
var library = await bootPackageWithLibrary('''
extension type ET(int it) {
ET.named(int it);
}

/// Doc referring to [ET.new] and [Et.named].
class C {}
''');

expect(
library.classes.named('C').documentationAsHtml,
'<p>Doc referring to '
'<a href="${placeholder}extension_types/ET/named.html">ET.named</a>.</p>',
);
}

void test_referenceToExtensionTypeMember() async {
var library = await bootPackageWithLibrary('''
extension type ET(int it) {
Loading