Skip to content

Commit 314fe3b

Browse files
committed
Use range character difference to decide whether to insert initial trailing comma
1 parent 8ec2efb commit 314fe3b

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

server/src/codeActions.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,17 @@ let handleUndefinedRecordFieldsAction = ({
380380
newText += `${paddingContentEndBrace}`;
381381
} else {
382382
// A single line record definition body is a bit easier - we'll just add the new fields on the same line.
383-
newText += ", ";
383+
384+
// For an empty record (`range.end.character - range.start.character == 2`),
385+
// we don't want to add an initial trailing comma as that would be invalid syntax.
386+
//
387+
// We assume that records that already contain some characters between
388+
// their braces have at least one field and therefore we need to insert
389+
// an initial trailing comma.
390+
if (range.end.character - range.start.character > 2) {
391+
newText += ", ";
392+
}
393+
384394
newText += recordFieldNames
385395
.map((fieldName) => `${fieldName}: failwith("TODO")`)
386396
.join(", ");

0 commit comments

Comments
 (0)