From e115ae89adde17fc1f96968db2547f5492161e37 Mon Sep 17 00:00:00 2001 From: Jonathan Koren Date: Thu, 21 Nov 2019 10:26:20 -0800 Subject: [PATCH] Create renderers for ElementType and subclasses --- lib/src/element_type.dart | 74 ++++----------- lib/src/render/element_type_renderer.dart | 111 ++++++++++++++++++++++ 2 files changed, 129 insertions(+), 56 deletions(-) create mode 100644 lib/src/render/element_type_renderer.dart diff --git a/lib/src/element_type.dart b/lib/src/element_type.dart index a3a8b853df..a8fe86895e 100644 --- a/lib/src/element_type.dart +++ b/lib/src/element_type.dart @@ -11,7 +11,7 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/nullability_suffix.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:dartdoc/src/model/model.dart'; -import 'package:dartdoc/src/render/parameter_renderer.dart'; +import 'package:dartdoc/src/render/element_type_renderer.dart'; /// Base class representing a type in Dartdoc. It wraps a [DartType], and /// may link to a [ModelElement]. @@ -143,13 +143,8 @@ class FunctionTypeElementType extends UndefinedElementType { @override String get linkedName { if (_linkedName == null) { - StringBuffer buf = StringBuffer(); - buf.write('${returnType.linkedName} '); - buf.write('${nameWithGenerics}'); - buf.write(''); - buf.write('(${ParameterRendererHtml().renderLinkedParams(parameters)})'); - buf.write(''); - _linkedName = buf.toString(); + _linkedName = + FunctionTypeElementTypeRendererHtml().renderLinkedName(this); } return _linkedName; } @@ -162,17 +157,8 @@ class FunctionTypeElementType extends UndefinedElementType { @override String get nameWithGenerics { if (_nameWithGenerics == null) { - StringBuffer buf = StringBuffer(); - buf.write(name); - if ((type as FunctionType).typeFormals.isNotEmpty) { - if (!typeFormals.every((t) => t.name == 'dynamic')) { - buf.write('<'); - buf.writeAll(typeFormals.map((t) => t.name), - ', '); - buf.write('>'); - } - } - _nameWithGenerics = buf.toString(); + _nameWithGenerics = + FunctionTypeElementTypeRendererHtml().renderNameWithGenerics(this); } return _nameWithGenerics; } @@ -198,21 +184,8 @@ class ParameterizedElementType extends DefinedElementType { @override String get linkedName { if (_linkedName == null) { - StringBuffer buf = StringBuffer(); - - buf.write(element.linkedName); - - if (!typeArguments.every((t) => t.name == 'dynamic') && - typeArguments.isNotEmpty) { - buf.write(''); - buf.write('<'); - buf.writeAll(typeArguments.map((t) => t.linkedName), - ', '); - buf.write('>'); - buf.write(''); - } - - _linkedName = buf.toString(); + _linkedName = + ParameterizedElementTypeRendererHtml().renderLinkedName(this); } return _linkedName; } @@ -221,18 +194,8 @@ class ParameterizedElementType extends DefinedElementType { @override String get nameWithGenerics { if (_nameWithGenerics == null) { - StringBuffer buf = StringBuffer(); - - buf.write(element.name); - - if (!typeArguments.every((t) => t.name == 'dynamic') && - typeArguments.isNotEmpty) { - buf.write('<'); - buf.writeAll(typeArguments.map((t) => t.nameWithGenerics), - ', '); - buf.write('>'); - } - _nameWithGenerics = buf.toString(); + _nameWithGenerics = + ParameterizedElementTypeRendererHtml().renderNameWithGenerics(this); } return _nameWithGenerics; } @@ -246,17 +209,12 @@ class TypeParameterElementType extends DefinedElementType { @override String get linkedName => name; - String _nameWithGenerics; @override - String get nameWithGenerics { - if (_nameWithGenerics == null) { - _nameWithGenerics = name; - } - return _nameWithGenerics; - } + String get nameWithGenerics => name; @override ClassElement get _boundClassElement => interfaceType.element; + @override InterfaceType get interfaceType => (type as TypeParameterType).bound; } @@ -417,9 +375,13 @@ class CallableElementType extends ParameterizedElementType @override String get linkedName { - if (name != null && name.isNotEmpty) return super.linkedName; - return '${nameWithGenerics}(${ParameterRendererHtml(showNames: false).renderLinkedParams(element.parameters).trim()}) → ${returnType.linkedName}'; + if (_linkedName == null) { + _linkedName = CallableElementTypeRendererHtml().renderLinkedName(this); + } + return _linkedName; } + + String get superLinkedName => super.linkedName; } /// This is an anonymous function using the generic function syntax (declared @@ -435,7 +397,7 @@ class CallableAnonymousElementType extends CallableElementType { String get linkedName { if (_linkedName == null) { _linkedName = - '${returnType.linkedName} ${super.linkedName}(${ParameterRendererHtml().renderLinkedParams(element.parameters)})'; + CallableAnonymousElementTypeRendererHtml().renderLinkedName(this); } return _linkedName; } diff --git a/lib/src/render/element_type_renderer.dart b/lib/src/render/element_type_renderer.dart new file mode 100644 index 0000000000..458aea63cb --- /dev/null +++ b/lib/src/render/element_type_renderer.dart @@ -0,0 +1,111 @@ +// Copyright (c) 2019, 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 'package:dartdoc/dartdoc.dart'; +import 'package:dartdoc/src/render/parameter_renderer.dart'; + +abstract class ElementTypeRenderer { + String renderLinkedName(T elementType); + + String renderNameWithGenerics(T elementType) => ''; +} + +class FunctionTypeElementTypeRendererHtml + extends ElementTypeRenderer { + @override + String renderLinkedName(FunctionTypeElementType elementType) { + StringBuffer buf = StringBuffer(); + buf.write('${elementType.returnType.linkedName} '); + buf.write('${elementType.nameWithGenerics}'); + buf.write('('); + buf.write( + ParameterRendererHtml().renderLinkedParams(elementType.parameters)); + buf.write(')'); + return buf.toString(); + } + + @override + String renderNameWithGenerics(FunctionTypeElementType elementType) { + StringBuffer buf = StringBuffer(); + buf.write(elementType.name); + if (elementType.typeFormals.isNotEmpty) { + if (!elementType.typeFormals.every((t) => t.name == 'dynamic')) { + buf.write('<'); + buf.writeAll(elementType.typeFormals.map((t) => t.name), + ', '); + buf.write('>'); + } + } + return buf.toString(); + } +} + +class ParameterizedElementTypeRendererHtml + extends ElementTypeRenderer { + @override + String renderLinkedName(ParameterizedElementType elementType) { + StringBuffer buf = StringBuffer(); + buf.write(elementType.element.linkedName); + if (elementType.typeArguments.isNotEmpty && + !elementType.typeArguments.every((t) => t.name == 'dynamic')) { + buf.write(''); + buf.write('<'); + buf.writeAll(elementType.typeArguments.map((t) => t.linkedName), + ', '); + buf.write('>'); + buf.write(''); + } + return buf.toString(); + } + + @override + String renderNameWithGenerics(ParameterizedElementType elementType) { + StringBuffer buf = StringBuffer(); + buf.write(elementType.element.name); + if (elementType.typeArguments.isNotEmpty && + !elementType.typeArguments.every((t) => t.name == 'dynamic')) { + buf.write('<'); + buf.writeAll(elementType.typeArguments.map((t) => t.nameWithGenerics), + ', '); + buf.write('>'); + } + return buf.toString(); + } +} + +class CallableElementTypeRendererHtml + extends ElementTypeRenderer { + @override + String renderLinkedName(CallableElementType elementType) { + if (elementType.name != null && elementType.name.isNotEmpty) { + return elementType.superLinkedName; + } + + StringBuffer buf = StringBuffer(); + buf.write(elementType.nameWithGenerics); + buf.write('('); + buf.write(ParameterRendererHtml(showNames: false) + .renderLinkedParams(elementType.element.parameters) + .trim()); + buf.write(') → '); + buf.write(elementType.returnType.linkedName); + return buf.toString(); + } +} + +class CallableAnonymousElementTypeRendererHtml + extends ElementTypeRenderer { + @override + String renderLinkedName(CallableAnonymousElementType elementType) { + StringBuffer buf = StringBuffer(); + buf.write(elementType.returnType.linkedName); + buf.write(' '); + buf.write(elementType.superLinkedName); + buf.write('('); + buf.write(ParameterRendererHtml() + .renderLinkedParams(elementType.element.parameters)); + buf.write(')'); + return buf.toString(); + } +}