Skip to content

Commit 84899c3

Browse files
committed
0 failing tests
1 parent 995aace commit 84899c3

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

src/services/textChanges.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ namespace ts.textChanges {
141141
return node.parent && (separator.kind === SyntaxKind.CommaToken || (separator.kind === SyntaxKind.SemicolonToken && node.parent.kind === SyntaxKind.ObjectLiteralExpression));
142142
}
143143

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+
144151
function spaces(count: number) {
145152
let s = "";
146153
for (let i = 0; i < count; i++) {
@@ -309,7 +316,6 @@ namespace ts.textChanges {
309316
else {
310317
// different lines
311318
startPos = getStartPositionOfLine(lineAndCharOfNextElement.line, sourceFile);
312-
//prefix = formatting.getIndentationString(lineAndCharOfNextElement.character, this.rulesProvider.getFormatOptions());
313319
}
314320

315321
this.changes.push({
@@ -326,22 +332,39 @@ namespace ts.textChanges {
326332
}
327333
else {
328334
// 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);
333340
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+
}
345368
}
346369
}
347370

@@ -409,7 +432,8 @@ namespace ts.textChanges {
409432

410433
let text = applyFormatting(nonFormattedText, sourceFile, initialIndentation, delta, this.rulesProvider);
411434
// 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+/, "");
413437

414438
if (options.insertLeadingNewLine) {
415439
text = this.newLineCharacter + text;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
===ORIGINAL===
2+
3+
const x = 1,
4+
y = 2;
5+
===MODIFIED===
6+
7+
const x = 1,
8+
y = 2,
9+
z = 1;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
===ORIGINAL===
2+
3+
const /*x*/ x = 1,
4+
/*y*/ y = 2;
5+
===MODIFIED===
6+
7+
const /*x*/ x = 1,
8+
/*y*/ y = 2,
9+
z = 1;

0 commit comments

Comments
 (0)