@@ -908,24 +908,25 @@ namespace ts.formatting {
908
908
let trimTrailingWhitespaces : boolean ;
909
909
let lineAction = LineAction . None ;
910
910
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 ) ;
929
930
}
930
931
931
932
// 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 {
1102
1103
previousRange : TextRangeWithKind ,
1103
1104
previousStartLine : number ,
1104
1105
currentRange : TextRangeWithKind ,
1105
- currentStartLine : number ) : void {
1106
-
1106
+ currentStartLine : number ,
1107
+ ) : LineAction {
1108
+ const onLaterLine = currentStartLine !== previousStartLine ;
1107
1109
switch ( rule . action ) {
1108
1110
case RuleAction . Ignore :
1109
1111
// no action required
1110
- return ;
1112
+ return LineAction . None ;
1111
1113
case RuleAction . Delete :
1112
1114
if ( previousRange . end !== currentRange . pos ) {
1113
1115
// delete characters starting from t1.end up to t2.pos exclusive
1114
1116
recordDelete ( previousRange . end , currentRange . pos - previousRange . end ) ;
1117
+ return onLaterLine ? LineAction . LineRemoved : LineAction . None ;
1115
1118
}
1116
1119
break ;
1117
1120
case RuleAction . NewLine :
1118
1121
// exit early if we on different lines and rule cannot change number of newlines
1119
1122
// if line1 and line2 are on subsequent lines then no edits are required - ok to exit
1120
1123
// if line1 and line2 are separated with more than one newline - ok to exit since we cannot delete extra new lines
1121
1124
if ( rule . flags !== RuleFlags . CanDeleteNewLines && previousStartLine !== currentStartLine ) {
1122
- return ;
1125
+ return LineAction . None ;
1123
1126
}
1124
1127
1125
1128
// edit should not be applied if we have one line feed between elements
1126
1129
const lineDelta = currentStartLine - previousStartLine ;
1127
1130
if ( lineDelta !== 1 ) {
1128
1131
recordReplace ( previousRange . end , currentRange . pos - previousRange . end , options . newLineCharacter ) ;
1132
+ return onLaterLine ? LineAction . None : LineAction . LineAdded ;
1129
1133
}
1130
1134
break ;
1131
1135
case RuleAction . Space :
1132
1136
// exit early if we on different lines and rule cannot change number of newlines
1133
1137
if ( rule . flags !== RuleFlags . CanDeleteNewLines && previousStartLine !== currentStartLine ) {
1134
- return ;
1138
+ return LineAction . None ;
1135
1139
}
1136
1140
1137
1141
const posDelta = currentRange . pos - previousRange . end ;
1138
1142
if ( posDelta !== 1 || sourceFile . text . charCodeAt ( previousRange . end ) !== CharacterCodes . space ) {
1139
1143
recordReplace ( previousRange . end , currentRange . pos - previousRange . end , " " ) ;
1144
+ return onLaterLine ? LineAction . LineRemoved : LineAction . None ;
1140
1145
}
1141
- break ;
1142
1146
}
1147
+ return LineAction . None ;
1143
1148
}
1144
1149
}
1145
1150
0 commit comments