Skip to content

Commit

Permalink
analyzer: Document DisplayStringBuilder and clean up fields.
Browse files Browse the repository at this point in the history
* `skipAllDynamicArguments` was always false; remove.
* `withNullability` and `multiline` are both unused outside the class;
  make them private.

Change-Id: Ic869abc1f76d0cc59e9feaffe969a24ea1deb363
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335360
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Kallen Tu <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Nov 9, 2023
1 parent e0751fb commit 706663a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
30 changes: 12 additions & 18 deletions pkg/analyzer/lib/src/dart/element/display_string_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/dart/resolver/variance.dart';
import 'package:analyzer/src/generated/element_type_provider.dart';

/// A class that builds a "display string" for [Element]s and [DartType]s.
class ElementDisplayStringBuilder {
final StringBuffer _buffer = StringBuffer();

final bool skipAllDynamicArguments;
final bool withNullability;
final bool multiline;
/// Whether to include the nullability ('?' characters) in a display string.
final bool _withNullability;

/// Whether to allow a display string to be written in multiple lines.
final bool _multiline;

ElementDisplayStringBuilder({
required this.skipAllDynamicArguments,
required this.withNullability,
this.multiline = false,
});
required bool withNullability,
bool multiline = false,
}) : _withNullability = withNullability,
_multiline = multiline;

@override
String toString() {
Expand Down Expand Up @@ -336,7 +339,7 @@ class ElementDisplayStringBuilder {
// Assume the display string looks better wrapped when there are at least
// three parameters. This avoids having to pre-compute the single-line
// version and know the length of the function name/return type.
var multiline = allowMultiline && this.multiline && parameters.length >= 3;
var multiline = allowMultiline && _multiline && parameters.length >= 3;

// The prefix for open groups is included in separator for single-line but
// not for multline so must be added explicitly.
Expand Down Expand Up @@ -388,16 +391,13 @@ class ElementDisplayStringBuilder {
}

void _writeNullability(NullabilitySuffix nullabilitySuffix) {
if (withNullability) {
if (_withNullability) {
switch (nullabilitySuffix) {
case NullabilitySuffix.question:
_write('?');
break;
case NullabilitySuffix.star:
_write('*');
break;
case NullabilitySuffix.none:
break;
}
}
}
Expand All @@ -411,12 +411,6 @@ class ElementDisplayStringBuilder {
return;
}

if (skipAllDynamicArguments) {
if (typeArguments.every((t) => t is DynamicType)) {
return;
}
}

_write('<');
for (var i = 0; i < typeArguments.length; i++) {
if (i != 0) {
Expand Down
1 change: 0 additions & 1 deletion pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,6 @@ abstract class ElementImpl implements Element {
bool multiline = false,
}) {
var builder = ElementDisplayStringBuilder(
skipAllDynamicArguments: false,
withNullability: withNullability,
multiline: multiline,
);
Expand Down
1 change: 0 additions & 1 deletion pkg/analyzer/lib/src/dart/element/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@ abstract class Member implements Element {
bool multiline = false,
}) {
var builder = ElementDisplayStringBuilder(
skipAllDynamicArguments: false,
withNullability: withNullability,
multiline: multiline,
);
Expand Down
2 changes: 0 additions & 2 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1347,11 +1347,9 @@ abstract class TypeImpl implements DartType {

@override
String getDisplayString({
bool skipAllDynamicArguments = false,
required bool withNullability,
}) {
var builder = ElementDisplayStringBuilder(
skipAllDynamicArguments: skipAllDynamicArguments,
withNullability: withNullability,
);
appendTo(builder);
Expand Down

0 comments on commit 706663a

Please sign in to comment.