Skip to content

Commit 7392230

Browse files
author
Andy
authored
In formatter, get lineAction directly from applyRuleEdits (#21245)
1 parent 095aa77 commit 7392230

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/services/formatting/formatting.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -908,24 +908,25 @@ namespace ts.formatting {
908908
let trimTrailingWhitespaces: boolean;
909909
let lineAction = LineAction.None;
910910
if (rule) {
911-
applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine);
912-
913-
if (rule.action & (RuleAction.Space | RuleAction.Delete) && currentStartLine !== previousStartLine) {
914-
lineAction = LineAction.LineRemoved;
915-
// Handle the case where the next line is moved to be the end of this line.
916-
// In this case we don't indent the next line in the next pass.
917-
if (currentParent.getStart(sourceFile) === currentItem.pos) {
918-
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ false);
919-
}
920-
}
921-
else if (rule.action & RuleAction.NewLine && currentStartLine === previousStartLine) {
922-
lineAction = LineAction.LineAdded;
923-
// Handle the case where token2 is moved to the new line.
924-
// In this case we indent token2 in the next pass but we set
925-
// sameLineIndent flag to notify the indenter that the indentation is within the line.
926-
if (currentParent.getStart(sourceFile) === currentItem.pos) {
927-
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ true);
928-
}
911+
lineAction = applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine);
912+
switch (lineAction) {
913+
case LineAction.LineRemoved:
914+
// Handle the case where the next line is moved to be the end of this line.
915+
// In this case we don't indent the next line in the next pass.
916+
if (currentParent.getStart(sourceFile) === currentItem.pos) {
917+
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ false);
918+
}
919+
break;
920+
case LineAction.LineAdded:
921+
// Handle the case where token2 is moved to the new line.
922+
// In this case we indent token2 in the next pass but we set
923+
// sameLineIndent flag to notify the indenter that the indentation is within the line.
924+
if (currentParent.getStart(sourceFile) === currentItem.pos) {
925+
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ true);
926+
}
927+
break;
928+
default:
929+
Debug.assert(lineAction === LineAction.None);
929930
}
930931

931932
// We need to trim trailing whitespace between the tokens if they were on different lines, and no rule was applied to put them on the same line
@@ -1102,44 +1103,48 @@ namespace ts.formatting {
11021103
previousRange: TextRangeWithKind,
11031104
previousStartLine: number,
11041105
currentRange: TextRangeWithKind,
1105-
currentStartLine: number): void {
1106-
1106+
currentStartLine: number,
1107+
): LineAction {
1108+
const onLaterLine = currentStartLine !== previousStartLine;
11071109
switch (rule.action) {
11081110
case RuleAction.Ignore:
11091111
// no action required
1110-
return;
1112+
return LineAction.None;
11111113
case RuleAction.Delete:
11121114
if (previousRange.end !== currentRange.pos) {
11131115
// delete characters starting from t1.end up to t2.pos exclusive
11141116
recordDelete(previousRange.end, currentRange.pos - previousRange.end);
1117+
return onLaterLine ? LineAction.LineRemoved : LineAction.None;
11151118
}
11161119
break;
11171120
case RuleAction.NewLine:
11181121
// exit early if we on different lines and rule cannot change number of newlines
11191122
// if line1 and line2 are on subsequent lines then no edits are required - ok to exit
11201123
// if line1 and line2 are separated with more than one newline - ok to exit since we cannot delete extra new lines
11211124
if (rule.flags !== RuleFlags.CanDeleteNewLines && previousStartLine !== currentStartLine) {
1122-
return;
1125+
return LineAction.None;
11231126
}
11241127

11251128
// edit should not be applied if we have one line feed between elements
11261129
const lineDelta = currentStartLine - previousStartLine;
11271130
if (lineDelta !== 1) {
11281131
recordReplace(previousRange.end, currentRange.pos - previousRange.end, options.newLineCharacter);
1132+
return onLaterLine ? LineAction.None : LineAction.LineAdded;
11291133
}
11301134
break;
11311135
case RuleAction.Space:
11321136
// exit early if we on different lines and rule cannot change number of newlines
11331137
if (rule.flags !== RuleFlags.CanDeleteNewLines && previousStartLine !== currentStartLine) {
1134-
return;
1138+
return LineAction.None;
11351139
}
11361140

11371141
const posDelta = currentRange.pos - previousRange.end;
11381142
if (posDelta !== 1 || sourceFile.text.charCodeAt(previousRange.end) !== CharacterCodes.space) {
11391143
recordReplace(previousRange.end, currentRange.pos - previousRange.end, " ");
1144+
return onLaterLine ? LineAction.LineRemoved : LineAction.None;
11401145
}
1141-
break;
11421146
}
1147+
return LineAction.None;
11431148
}
11441149
}
11451150

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference path="fourslash.ts"/>
2+
3+
////const {
4+
////x,
5+
////y,
6+
////} = 0;
7+
8+
format.document();
9+
verify.currentFileContentIs(
10+
`const {
11+
x,
12+
y,
13+
} = 0;`
14+
);

0 commit comments

Comments
 (0)