@@ -141,6 +141,13 @@ namespace ts.textChanges {
141
141
return node . parent && ( separator . kind === SyntaxKind . CommaToken || ( separator . kind === SyntaxKind . SemicolonToken && node . parent . kind === SyntaxKind . ObjectLiteralExpression ) ) ;
142
142
}
143
143
144
+ function getSeparator ( sourceFile : SourceFile , element : Node ) {
145
+ const previousToken = getTokenAtPosition ( sourceFile , element . end ) ;
146
+ return previousToken && isSeparator ( element , previousToken )
147
+ ? previousToken
148
+ : createToken ( SyntaxKind . CommaToken ) ;
149
+ }
150
+
144
151
function spaces ( count : number ) {
145
152
let s = "" ;
146
153
for ( let i = 0 ; i < count ; i ++ ) {
@@ -309,7 +316,6 @@ namespace ts.textChanges {
309
316
else {
310
317
// different lines
311
318
startPos = getStartPositionOfLine ( lineAndCharOfNextElement . line , sourceFile ) ;
312
- //prefix = formatting.getIndentationString(lineAndCharOfNextElement.character, this.rulesProvider.getFormatOptions());
313
319
}
314
320
315
321
this . changes . push ( {
@@ -326,22 +332,39 @@ namespace ts.textChanges {
326
332
}
327
333
else {
328
334
// insert element after the last element in the list that has more than one item
329
- // use previos sibling
330
- const prevPreviousElement = containingList [ index - 1 ] ;
331
- let separator = getTokenAtPosition ( sourceFile , prevPreviousElement . end ) ;
332
- separator = createToken ( separator && isSeparator ( prevPreviousElement , separator ) ? separator . kind : SyntaxKind . CommaToken ) ;
335
+ // pick the element preceding the after element to:
336
+ // - pick the separator
337
+ // - determine if list is a multiline
338
+ const afterMinusOne = containingList [ index - 1 ] ;
339
+ const separatorBefore = < Token < SyntaxKind . CommaToken | SyntaxKind . SemicolonToken > > getSeparator ( sourceFile , afterMinusOne ) ;
333
340
const endPosition = getAdjustedEndPosition ( sourceFile , after , options ) ;
334
- const multilineList =
335
- getLineOfLocalPosition ( sourceFile , prevPreviousElement . getStart ( sourceFile ) ) !== getLineOfLocalPosition ( sourceFile , after . getStart ( sourceFile ) ) ;
336
- this . changes . push ( {
337
- sourceFile,
338
- range : { pos : endPosition , end : endPosition } ,
339
- node : newNode ,
340
- useIndentationFromFile : true ,
341
- options : multilineList ? { insertLeadingNewLine : true } : { } ,
342
- prefix : multilineList ? undefined : " " ,
343
- separatorBefore : < Token < SyntaxKind . CommaToken | SyntaxKind . SemicolonToken > > separator
344
- } )
341
+ const range = { pos : endPosition , end : endPosition } ;
342
+ const afterMinusOneStartLinePosition = getLineStartPositionForPosition ( afterMinusOne . getStart ( sourceFile ) , sourceFile ) ;
343
+ const afterStart = after . getStart ( sourceFile ) ;
344
+ const afterStartLinePosition = getLineStartPositionForPosition ( afterStart , sourceFile ) ;
345
+ if ( afterMinusOneStartLinePosition !== afterStartLinePosition ) {
346
+ // multiline list
347
+ // use the same indentation as 'after' item
348
+ const indentation = formatting . SmartIndenter . findFirstNonWhitespaceColumn ( afterStartLinePosition , afterStart , sourceFile , this . rulesProvider . getFormatOptions ( ) ) ;
349
+ this . changes . push ( {
350
+ sourceFile,
351
+ range,
352
+ node : newNode ,
353
+ options : { insertLeadingNewLine : true , indentation } ,
354
+ separatorBefore
355
+ } ) ;
356
+ }
357
+ else {
358
+ // single line list
359
+ this . changes . push ( {
360
+ sourceFile,
361
+ range,
362
+ node : newNode ,
363
+ options : { } ,
364
+ prefix : " " , // ensure that new item is separate from previous item by one whitespace
365
+ separatorBefore
366
+ } )
367
+ }
345
368
}
346
369
}
347
370
@@ -409,7 +432,8 @@ namespace ts.textChanges {
409
432
410
433
let text = applyFormatting ( nonFormattedText , sourceFile , initialIndentation , delta , this . rulesProvider ) ;
411
434
// strip initial indentation (spaces or tabs) if text will be inserted in the middle of the line
412
- text = posStartsLine ? text : text . replace ( / ^ \s + / , "" ) ;
435
+ // however keep indentation if it is was forced
436
+ text = posStartsLine || change . options . indentation !== undefined ? text : text . replace ( / ^ \s + / , "" ) ;
413
437
414
438
if ( options . insertLeadingNewLine ) {
415
439
text = this . newLineCharacter + text ;
0 commit comments