Skip to content

Commit 8857bd4

Browse files
authored
[PropertyEditor] Do not send requests to getEditableArguments for non-Dart files (#9051)
1 parent 4689675 commit 8857bd4

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

packages/devtools_app/lib/src/standalone_ui/ide_shared/property_editor/property_editor_controller.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import '../../../shared/utils/utils.dart';
1616
import 'property_editor_types.dart';
1717

1818
typedef EditableWidgetData =
19-
({List<EditableProperty> properties, String? name, String? documentation});
19+
({
20+
List<EditableProperty> properties,
21+
String? name,
22+
String? documentation,
23+
String? fileUri,
24+
});
2025

2126
typedef EditArgumentFunction =
2227
Future<EditArgumentResponse?> Function<T>({
@@ -84,6 +89,15 @@ class PropertyEditorController extends DisposableController
8489
cursorPosition == _currentCursorPosition) {
8590
return;
8691
}
92+
if (!textDocument.uriAsString.endsWith('.dart')) {
93+
_editableWidgetData.value = (
94+
properties: [],
95+
name: null,
96+
documentation: null,
97+
fileUri: textDocument.uriAsString,
98+
);
99+
return;
100+
}
87101
_editableArgsDebouncer.run(
88102
() => _updateWithEditableArgs(
89103
textDocument: textDocument,
@@ -148,6 +162,7 @@ class PropertyEditorController extends DisposableController
148162
properties: properties,
149163
name: name,
150164
documentation: result?.documentation,
165+
fileUri: _currentDocument?.uriAsString,
151166
);
152167
filterData(activeFilter.value);
153168
// Register impression.
@@ -180,6 +195,7 @@ class PropertyEditorController extends DisposableController
180195
editableArgsResult.args.map(argToProperty).nonNulls.toList(),
181196
name: editableArgsResult.name,
182197
documentation: editableArgsResult.documentation,
198+
fileUri: document?.uriAsString,
183199
);
184200
}
185201
if (document != null) {

packages/devtools_app/lib/src/standalone_ui/ide_shared/property_editor/property_editor_view.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ class PropertyEditorView extends StatelessWidget {
5050
);
5151
}
5252

53+
final (:properties, :name, :documentation, :fileUri) =
54+
editableWidgetData;
55+
if (fileUri != null && !fileUri.endsWith('.dart')) {
56+
return const CenteredMessage(
57+
message: 'No Dart code found at the current cursor location.',
58+
);
59+
}
60+
5361
final filteredProperties = values.fourth as List<EditableProperty>;
54-
final (:properties, :name, :documentation) = editableWidgetData;
5562
return Column(
5663
crossAxisAlignment: CrossAxisAlignment.start,
5764
children: [

packages/devtools_app/test/standalone_ui/ide_shared/property_editor/property_editor_test.dart

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void main() {
5757

5858
group('on cursor location change', () {
5959
void Function()? listener;
60+
Completer? getEditableArgsCalled;
6061

6162
Future<List<EditableArgument>> waitForEditableArgs() {
6263
final argsCompleter = Completer<List<EditableArgument>>();
@@ -87,6 +88,7 @@ void main() {
8788
}
8889

8990
setUp(() {
91+
getEditableArgsCalled = Completer<void>();
9092
for (final MapEntry(key: location, value: result)
9193
in locationToArgsResult.entries) {
9294
when(
@@ -95,7 +97,11 @@ void main() {
9597
textDocument: location.document,
9698
position: location.position,
9799
),
98-
).thenAnswer((realInvocation) => Future.value(result));
100+
).thenAnswer((realInvocation) {
101+
getEditableArgsCalled?.complete();
102+
getEditableArgsCalled = Completer<void>();
103+
return Future.value(result);
104+
});
99105
}
100106
});
101107

@@ -175,6 +181,38 @@ void main() {
175181
expect(titleValue, equals('Hello world!'));
176182
});
177183
});
184+
185+
testWidgets('verify does not fetch editable arguments for non-Dart files', (
186+
tester,
187+
) async {
188+
return await tester.runAsync(() async {
189+
// Load the property editor.
190+
await tester.pumpWidget(wrap(propertyEditor));
191+
final getEditableArgsCalledFuture = getEditableArgsCalled!.future;
192+
193+
// Send an active location changed event.
194+
eventController.add(activeLocationChangedEventNotDart);
195+
196+
// Verify it doesn't trigger a request to getEditableArgs.
197+
try {
198+
await getEditableArgsCalledFuture.timeout(
199+
const Duration(milliseconds: 100),
200+
);
201+
fail('getEditableArgs was unexpectedly called.');
202+
} on TimeoutException catch (e) {
203+
expect(e, isA<TimeoutException>());
204+
}
205+
206+
// Verify "No Dart code" message is shown.
207+
await tester.pumpAndSettle();
208+
expect(
209+
find.textContaining(
210+
'No Dart code found at the current cursor location.',
211+
),
212+
findsOneWidget,
213+
);
214+
});
215+
});
178216
});
179217

180218
group('inputs for editable arguments', () {
@@ -1020,6 +1058,15 @@ final activeLocationChangedEvent4 = ActiveLocationChangedEvent(
10201058
textDocument: textDocument1,
10211059
);
10221060

1061+
final notADartDocument = TextDocument(
1062+
uriAsString: '/my/fake/other.js',
1063+
version: 1,
1064+
);
1065+
final activeLocationChangedEventNotDart = ActiveLocationChangedEvent(
1066+
selections: [editorSelection1],
1067+
textDocument: notADartDocument,
1068+
);
1069+
10231070
// Widget name and documentation
10241071
const widgetName = 'MyFlutterWidget';
10251072

0 commit comments

Comments
 (0)