From 853415dd1bc605cf3ab4a4fbde71e3a7d003a67c Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 22 Jun 2021 10:56:08 -0700 Subject: [PATCH] send back Widget class name information in the widget rebuild/repaint count events (#84804) send back Widget class name information in the widget rebuild/repaint count event --- .../lib/src/widgets/widget_inspector.dart | 50 +++++++++++-------- .../test/widgets/widget_inspector_test.dart | 33 +++++++++++- 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart index d130c40407..26e161e48d 100644 --- a/packages/flutter/lib/src/widgets/widget_inspector.dart +++ b/packages/flutter/lib/src/widgets/widget_inspector.dart @@ -1527,10 +1527,10 @@ mixin WidgetInspectorService { } bool _isLocalCreationLocation(_Location? location) { - if (location == null || location.file == null) { + if (location == null) { return false; } - final String file = Uri.parse(location.file!).path; + final String file = Uri.parse(location.file).path; // By default check whether the creation location was within package:flutter. if (_pubRootDirectories == null) { @@ -2127,16 +2127,32 @@ class _ElementLocationStatsTracker { final Map> locationsJson = >{}; for (final _LocationCount entry in newLocations) { final _Location location = entry.location; - if (location.file != null) { - final List jsonForFile = locationsJson.putIfAbsent( - location.file!, - () => [], - ); - jsonForFile..add(entry.id)..add(location.line)..add(location.column); - } + final List jsonForFile = locationsJson.putIfAbsent( + location.file, + () => [], + ); + jsonForFile..add(entry.id)..add(location.line)..add(location.column); } json['newLocations'] = locationsJson; } + + // Add in a data structure for the location names. + if (newLocations.isNotEmpty) { + final Map> namesJson = >{}; + for (final _LocationCount entry in newLocations) { + final _Location location = entry.location; + final Map jsonForFile = namesJson.putIfAbsent( + location.file, + () => {}, + ); + final String? name = location.name; + if (name != null) { + jsonForFile[entry.id] = name; + } + } + json['newLocationsNames'] = namesJson; + } + resetCounts(); newLocations.clear(); return json; @@ -2852,23 +2868,20 @@ class _Location { required this.line, required this.column, this.name, - this.parameterLocations, }); /// File path of the location. - final String? file; + final String file; /// 1-based line number. final int line; + /// 1-based column number. final int column; /// Optional name of the parameter or function at this location. final String? name; - /// Optional locations of the parameters of the member at this location. - final List<_Location>? parameterLocations; - Map toJsonMap() { final Map json = { 'file': file, @@ -2878,11 +2891,6 @@ class _Location { if (name != null) { json['name'] = name; } - if (parameterLocations != null) { - json['parameterLocations'] = parameterLocations!.map>( - (_Location location) => location.toJsonMap(), - ).toList(); - } return json; } @@ -2892,9 +2900,7 @@ class _Location { if (name != null) { parts.add(name!); } - if (file != null) { - parts.add(file!); - } + parts.add(file); parts..add('$line')..add('$column'); return parts.join(':'); } diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index 46e293d678..6c80546c70 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -134,17 +134,19 @@ class CyclicDiagnostic extends DiagnosticableTree { } class _CreationLocation { - const _CreationLocation({ + _CreationLocation({ required this.file, required this.line, required this.column, required this.id, + this.name, }); final String file; final int line; final int column; final int id; + String? name; } typedef InspectorServiceExtensionCallback = FutureOr> Function(Map parameters); @@ -943,6 +945,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { final String fileA = creationLocationA['file']! as String; final int lineA = creationLocationA['line']! as int; final int columnA = creationLocationA['column']! as int; + final String nameA = creationLocationA['name']! as String; + expect(nameA, equals('Text')); service.setSelection(elementB, 'my-group'); final Map jsonB = json.decode(service.getSelectedWidget(null, 'my-group')) as Map; @@ -951,6 +955,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { final String fileB = creationLocationB['file']! as String; final int lineB = creationLocationB['line']! as int; final int columnB = creationLocationB['column']! as int; + final String? nameB = creationLocationB['name'] as String?; + expect(nameB, equals('Text')); expect(fileA, endsWith('widget_inspector_test.dart')); expect(fileA, equals(fileB)); // We don't hardcode the actual lines the widgets are created on as that @@ -1919,6 +1925,10 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { expect(newLocations, isNotNull); expect(newLocations.length, equals(1)); expect(newLocations.keys.first, equals(file)); + final Map> newLocationsNames = event['newLocationsNames']! as Map>; + expect(newLocationsNames, isNotNull); + expect(newLocationsNames.length, equals(1)); + expect(newLocationsNames.keys.first, equals(file)); final List locationsForFile = newLocations[file]!; expect(locationsForFile.length, equals(21)); final int numLocationEntries = locationsForFile.length ~/ 3; @@ -1928,6 +1938,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { _addToKnownLocationsMap( knownLocations: knownLocations, newLocations: newLocations, + newLocationsNames: newLocationsNames, ); int totalCount = 0; int maxCount = 0; @@ -1956,6 +1967,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { data = event['events']! as List; // No new locations were rebuilt. expect(event, isNot(contains('newLocations'))); + expect(event, isNot(contains('newLocationsNames'))); // There were two rebuilds: one for the ClockText element itself and one // for its child. @@ -1967,6 +1979,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { // ClockText widget. expect(location.line, equals(53)); expect(location.column, equals(9)); + expect(location.name, equals('ClockText')); expect(count, equals(1)); id = data[2]; @@ -1976,6 +1989,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { // Text widget in _ClockTextState build method. expect(location.line, equals(91)); expect(location.column, equals(12)); + expect(location.name, equals('Text')); expect(count, equals(1)); // Update 3 of the clocks; @@ -1992,6 +2006,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { data = event['events']! as List; // No new locations were rebuilt. expect(event, isNot(contains('newLocations'))); + expect(event, isNot(contains('newLocationsNames'))); expect(data.length, equals(4)); id = data[0]; @@ -2001,6 +2016,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { // ClockText widget. expect(location.line, equals(53)); expect(location.column, equals(9)); + expect(location.name, equals('ClockText')); expect(count, equals(3)); // 3 clock widget instances rebuilt. id = data[2]; @@ -2010,6 +2026,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { // Text widget in _ClockTextState build method. expect(location.line, equals(91)); expect(location.column, equals(12)); + expect(location.name, equals('Text')); expect(count, equals(3)); // 3 clock widget instances rebuilt. // Update one clock 3 times. @@ -2026,6 +2043,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { data = event['events']! as List; // No new locations were rebuilt. expect(event, isNot(contains('newLocations'))); + expect(event, isNot(contains('newLocationsNames'))); expect(data.length, equals(4)); id = data[0]; @@ -2053,6 +2071,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { _addToKnownLocationsMap( knownLocations: knownLocations, newLocations: newLocations, + newLocationsNames: newLocationsNames, ); // Verify the rebuild location was included in the newLocations data. expect(knownLocations, contains(id)); @@ -2117,6 +2136,10 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { expect(newLocations, isNotNull); expect(newLocations.length, equals(1)); expect(newLocations.keys.first, equals(file)); + final Map> newLocationsNames = event['newLocationsNames']! as Map>; + expect(newLocationsNames, isNotNull); + expect(newLocationsNames.length, equals(1)); + expect(newLocationsNames.keys.first, equals(file)); final List locationsForFile = newLocations[file]!; expect(locationsForFile.length, equals(27)); final int numLocationEntries = locationsForFile.length ~/ 3; @@ -2127,6 +2150,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { _addToKnownLocationsMap( knownLocations: knownLocations, newLocations: newLocations, + newLocationsNames: newLocationsNames, ); int totalCount = 0; int maxCount = 0; @@ -2155,6 +2179,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { data = event['events']! as List; // No new locations were rebuilt. expect(event, isNot(contains('newLocations'))); + expect(event, isNot(contains('newLocationsNames'))); // Triggering a rebuild of one widget in this app causes the whole app // to repaint. @@ -3008,6 +3033,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { void _addToKnownLocationsMap({ required Map knownLocations, required Map> newLocations, + required Map> newLocationsNames, }) { newLocations.forEach((String file, List entries) { assert(entries.length % 3 == 0); @@ -3020,4 +3046,9 @@ void _addToKnownLocationsMap({ _CreationLocation(file: file, line: line, column: column, id: id); } }); + newLocationsNames.forEach((String file, Map namesInfo) { + namesInfo.forEach((int id, String name) { + knownLocations[id]!.name = name; + }); + }); }