@@ -401,6 +401,7 @@ namespace ts.formatting {
401
401
let previousRange : TextRangeWithKind ;
402
402
let previousParent : Node ;
403
403
let previousRangeStartLine : number ;
404
+ let previousRangeEndLine : number ;
404
405
405
406
let lastIndentedLine : number ;
406
407
let indentationOnLastIndentedLine : number ;
@@ -794,11 +795,12 @@ namespace ts.formatting {
794
795
const isTokenInRange = rangeContainsRange ( originalRange , currentTokenInfo . token ) ;
795
796
796
797
const tokenStart = sourceFile . getLineAndCharacterOfPosition ( currentTokenInfo . token . pos ) ;
798
+ const tokenEnd = sourceFile . getLineAndCharacterOfPosition ( currentTokenInfo . token . end ) ;
797
799
if ( isTokenInRange ) {
798
800
const rangeHasError = rangeContainsError ( currentTokenInfo . token ) ;
799
801
// save previousRange since processRange will overwrite this value with current one
800
802
const savePreviousRange = previousRange ;
801
- lineAdded = processRange ( currentTokenInfo . token , tokenStart , parent , childContextNode , dynamicIndentation ) ;
803
+ lineAdded = processRange ( currentTokenInfo . token , tokenStart , tokenEnd , parent , childContextNode , dynamicIndentation ) ;
802
804
if ( rangeHasError ) {
803
805
// do not indent comments\token if token range overlaps with some error
804
806
indentToken = false ;
@@ -869,13 +871,15 @@ namespace ts.formatting {
869
871
for ( const triviaItem of trivia ) {
870
872
if ( isComment ( triviaItem . kind ) && rangeContainsRange ( originalRange , triviaItem ) ) {
871
873
const triviaItemStart = sourceFile . getLineAndCharacterOfPosition ( triviaItem . pos ) ;
872
- processRange ( triviaItem , triviaItemStart , parent , contextNode , dynamicIndentation ) ;
874
+ const triviaItemEnd = sourceFile . getLineAndCharacterOfPosition ( triviaItem . end ) ;
875
+ processRange ( triviaItem , triviaItemStart , triviaItemEnd , parent , contextNode , dynamicIndentation ) ;
873
876
}
874
877
}
875
878
}
876
879
877
880
function processRange ( range : TextRangeWithKind ,
878
881
rangeStart : LineAndCharacter ,
882
+ rangeEnd : LineAndCharacter ,
879
883
parent : Node ,
880
884
contextNode : Node ,
881
885
dynamicIndentation : DynamicIndentation ) : boolean {
@@ -890,13 +894,14 @@ namespace ts.formatting {
890
894
}
891
895
else {
892
896
lineAdded =
893
- processPair ( range , rangeStart . line , parent , previousRange , previousRangeStartLine , previousParent , contextNode , dynamicIndentation ) ;
897
+ processPair ( range , rangeStart . line , parent , previousRange , previousRangeStartLine , previousRangeEndLine , previousParent , contextNode , dynamicIndentation ) ;
894
898
}
895
899
}
896
900
897
901
previousRange = range ;
898
902
previousParent = parent ;
899
903
previousRangeStartLine = rangeStart . line ;
904
+ previousRangeEndLine = rangeEnd . line ;
900
905
901
906
return lineAdded ;
902
907
}
@@ -906,6 +911,7 @@ namespace ts.formatting {
906
911
currentParent : Node ,
907
912
previousItem : TextRangeWithKind ,
908
913
previousStartLine : number ,
914
+ previousEndLine : number ,
909
915
previousParent : Node ,
910
916
contextNode : Node ,
911
917
dynamicIndentation : DynamicIndentation ) : boolean {
@@ -917,7 +923,7 @@ namespace ts.formatting {
917
923
let trimTrailingWhitespaces : boolean ;
918
924
let lineAdded : boolean ;
919
925
if ( rule ) {
920
- applyRuleEdits ( rule , previousItem , previousStartLine , currentItem , currentStartLine ) ;
926
+ applyRuleEdits ( rule , previousItem , previousStartLine , previousEndLine , currentItem , currentStartLine ) ;
921
927
922
928
if ( rule . Operation . Action & ( RuleAction . Space | RuleAction . Delete ) && currentStartLine !== previousStartLine ) {
923
929
lineAdded = false ;
@@ -1108,6 +1114,7 @@ namespace ts.formatting {
1108
1114
function applyRuleEdits ( rule : Rule ,
1109
1115
previousRange : TextRangeWithKind ,
1110
1116
previousStartLine : number ,
1117
+ previousEndLine : number ,
1111
1118
currentRange : TextRangeWithKind ,
1112
1119
currentStartLine : number ) : void {
1113
1120
@@ -1137,7 +1144,7 @@ namespace ts.formatting {
1137
1144
break ;
1138
1145
case RuleAction . Space :
1139
1146
// exit early if we on different lines and rule cannot change number of newlines
1140
- if ( rule . Flag !== RuleFlags . CanDeleteNewLines && previousStartLine !== currentStartLine ) {
1147
+ if ( rule . Flag !== RuleFlags . CanDeleteNewLines && previousEndLine !== currentStartLine ) {
1141
1148
return ;
1142
1149
}
1143
1150
0 commit comments