Skip to content

Commit bc7feac

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Fix missing trailing comma when inserting arguments before solo child/children
The logic here was bad, assuming we didn't need a trailing comma when inserting an arg before a solo argument (which can happen for child/children). Fixes flutter/devtools#8921 Change-Id: I38f9003cdc8b499d6417cac7989c65a4760416a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411140 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Elliott Brooks <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 292e006 commit bc7feac

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

pkg/analysis_server/lib/src/lsp/handlers/custom/editable_arguments/handler_edit_argument.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,15 +431,16 @@ class EditArgumentHandler extends SharedMessageHandler<EditArgumentParams, Null>
431431
(false, false) => '',
432432
};
433433

434-
// The suffix depends on whether there is an argument after us and whether
435-
// we are multiline. If there is an argument after us, there is already
436-
// the correct whitespace and comma after our insertion point.
434+
// The suffix depends on whether there is an argument after us, whether
435+
// we are multiline and whether there is already a comma after us.
437436
var codeSuffix = switch ((
438437
isMultiline,
439438
hasArgumentsAfter,
440439
commaFollowsInsertion,
441440
)) {
442-
(_, true, _) => '',
441+
(_, true, true) => '',
442+
(true, true, false) => ',',
443+
(false, true, false) => ', ',
443444
(true, false, false) => ',',
444445
(true, false, true) => '',
445446
(false, false, _) => '',

pkg/analysis_server/test/shared/shared_edit_argument_tests.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ mixin SharedEditArgumentTests
158158
);
159159
}
160160

161+
test_named_addAfterNamed_beforeChild_noOthers() async {
162+
await _expectSimpleArgumentEdit(
163+
params: '({ int? y, Widget? child })',
164+
originalArgs: '(child: null)',
165+
edit: ArgumentEdit(name: 'y', newValue: 2),
166+
expectedArgs: '(y: 2, child: null)',
167+
);
168+
}
169+
170+
test_named_addAfterNamed_beforeChild_noOthers_multiline() async {
171+
await _expectSimpleArgumentEdit(
172+
params: '({ int? y, Widget? child })',
173+
originalArgs: '''\n
174+
(
175+
child: null
176+
)''',
177+
edit: ArgumentEdit(name: 'y', newValue: 2),
178+
expectedArgs: '''\n
179+
(
180+
y: 2,
181+
child: null
182+
)''',
183+
);
184+
}
185+
161186
test_named_addAfterNamed_beforeChildAtEnd() async {
162187
await _expectSimpleArgumentEdit(
163188
params: '({ int? x, int? y, Widget? child })',
@@ -167,6 +192,31 @@ mixin SharedEditArgumentTests
167192
);
168193
}
169194

195+
test_named_addAfterNamed_beforeChildren_noOthers() async {
196+
await _expectSimpleArgumentEdit(
197+
params: '({ int? y, List<Widget>? children })',
198+
originalArgs: '(children: [])',
199+
edit: ArgumentEdit(name: 'y', newValue: 2),
200+
expectedArgs: '(y: 2, children: [])',
201+
);
202+
}
203+
204+
test_named_addAfterNamed_beforeChildren_noOthers_multiline() async {
205+
await _expectSimpleArgumentEdit(
206+
params: '({ int? y, List<Widget>? children })',
207+
originalArgs: '''\n
208+
(
209+
children: []
210+
)''',
211+
edit: ArgumentEdit(name: 'y', newValue: 2),
212+
expectedArgs: '''\n
213+
(
214+
y: 2,
215+
children: []
216+
)''',
217+
);
218+
}
219+
170220
test_named_addAfterNamed_beforeChildrenAtEnd() async {
171221
await _expectSimpleArgumentEdit(
172222
params: '({ int? x, int? y, List<Widget>? children })',

0 commit comments

Comments
 (0)