Skip to content

Commit e961fa6

Browse files
committed
1 parent 181e04b commit e961fa6

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/services/formatting/formatting.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ namespace ts.formatting {
402402
let previousRange: TextRangeWithKind;
403403
let previousParent: Node;
404404
let previousRangeStartLine: number;
405+
let previousRangeEndLine: number;
405406

406407
let lastIndentedLine: number;
407408
let indentationOnLastIndentedLine: number;
@@ -795,11 +796,12 @@ namespace ts.formatting {
795796
const isTokenInRange = rangeContainsRange(originalRange, currentTokenInfo.token);
796797

797798
const tokenStart = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.pos);
799+
const tokenEnd = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.end);
798800
if (isTokenInRange) {
799801
const rangeHasError = rangeContainsError(currentTokenInfo.token);
800802
// save previousRange since processRange will overwrite this value with current one
801803
const savePreviousRange = previousRange;
802-
lineAdded = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation);
804+
lineAdded = processRange(currentTokenInfo.token, tokenStart, tokenEnd, parent, childContextNode, dynamicIndentation);
803805
if (rangeHasError) {
804806
// do not indent comments\token if token range overlaps with some error
805807
indentToken = false;
@@ -870,13 +872,15 @@ namespace ts.formatting {
870872
for (const triviaItem of trivia) {
871873
if (isComment(triviaItem.kind) && rangeContainsRange(originalRange, triviaItem)) {
872874
const triviaItemStart = sourceFile.getLineAndCharacterOfPosition(triviaItem.pos);
873-
processRange(triviaItem, triviaItemStart, parent, contextNode, dynamicIndentation);
875+
const triviaItemEnd = sourceFile.getLineAndCharacterOfPosition(triviaItem.end);
876+
processRange(triviaItem, triviaItemStart, triviaItemEnd, parent, contextNode, dynamicIndentation);
874877
}
875878
}
876879
}
877880

878881
function processRange(range: TextRangeWithKind,
879882
rangeStart: LineAndCharacter,
883+
rangeEnd: LineAndCharacter,
880884
parent: Node,
881885
contextNode: Node,
882886
dynamicIndentation: DynamicIndentation): boolean {
@@ -891,13 +895,14 @@ namespace ts.formatting {
891895
}
892896
else {
893897
lineAdded =
894-
processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousParent, contextNode, dynamicIndentation);
898+
processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousRangeEndLine, previousParent, contextNode, dynamicIndentation);
895899
}
896900
}
897901

898902
previousRange = range;
899903
previousParent = parent;
900904
previousRangeStartLine = rangeStart.line;
905+
previousRangeEndLine = rangeEnd.line;
901906
previousRangeHasError = rangeHasError;
902907

903908
return lineAdded;
@@ -908,6 +913,7 @@ namespace ts.formatting {
908913
currentParent: Node,
909914
previousItem: TextRangeWithKind,
910915
previousStartLine: number,
916+
previousEndLine: number,
911917
previousParent: Node,
912918
contextNode: Node,
913919
dynamicIndentation: DynamicIndentation): boolean {
@@ -919,7 +925,7 @@ namespace ts.formatting {
919925
let trimTrailingWhitespaces: boolean;
920926
let lineAdded: boolean;
921927
if (rule) {
922-
applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine);
928+
applyRuleEdits(rule, previousItem, previousStartLine, previousEndLine, currentItem, currentStartLine);
923929

924930
if (rule.Operation.Action & (RuleAction.Space | RuleAction.Delete) && currentStartLine !== previousStartLine) {
925931
lineAdded = false;
@@ -1110,6 +1116,7 @@ namespace ts.formatting {
11101116
function applyRuleEdits(rule: Rule,
11111117
previousRange: TextRangeWithKind,
11121118
previousStartLine: number,
1119+
previousEndLine: number,
11131120
currentRange: TextRangeWithKind,
11141121
currentStartLine: number): void {
11151122

@@ -1139,7 +1146,7 @@ namespace ts.formatting {
11391146
break;
11401147
case RuleAction.Space:
11411148
// exit early if we on different lines and rule cannot change number of newlines
1142-
if (rule.Flag !== RuleFlags.CanDeleteNewLines && previousStartLine !== currentStartLine) {
1149+
if (rule.Flag !== RuleFlags.CanDeleteNewLines && previousEndLine !== currentStartLine) {
11431150
return;
11441151
}
11451152

0 commit comments

Comments
 (0)