Skip to content

Commit 0cc414e

Browse files
committed
1 parent 92db4e9 commit 0cc414e

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Diff for: src/services/formatting/formatting.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ namespace ts.formatting {
401401
let previousRange: TextRangeWithKind;
402402
let previousParent: Node;
403403
let previousRangeStartLine: number;
404+
let previousRangeEndLine: number;
404405

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

796797
const tokenStart = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.pos);
798+
const tokenEnd = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.end);
797799
if (isTokenInRange) {
798800
const rangeHasError = rangeContainsError(currentTokenInfo.token);
799801
// save previousRange since processRange will overwrite this value with current one
800802
const savePreviousRange = previousRange;
801-
lineAdded = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation);
803+
lineAdded = processRange(currentTokenInfo.token, tokenStart, tokenEnd, parent, childContextNode, dynamicIndentation);
802804
if (rangeHasError) {
803805
// do not indent comments\token if token range overlaps with some error
804806
indentToken = false;
@@ -869,13 +871,15 @@ namespace ts.formatting {
869871
for (const triviaItem of trivia) {
870872
if (isComment(triviaItem.kind) && rangeContainsRange(originalRange, triviaItem)) {
871873
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);
873876
}
874877
}
875878
}
876879

877880
function processRange(range: TextRangeWithKind,
878881
rangeStart: LineAndCharacter,
882+
rangeEnd: LineAndCharacter,
879883
parent: Node,
880884
contextNode: Node,
881885
dynamicIndentation: DynamicIndentation): boolean {
@@ -890,13 +894,14 @@ namespace ts.formatting {
890894
}
891895
else {
892896
lineAdded =
893-
processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousParent, contextNode, dynamicIndentation);
897+
processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousRangeEndLine, previousParent, contextNode, dynamicIndentation);
894898
}
895899
}
896900

897901
previousRange = range;
898902
previousParent = parent;
899903
previousRangeStartLine = rangeStart.line;
904+
previousRangeEndLine = rangeEnd.line;
900905

901906
return lineAdded;
902907
}
@@ -906,6 +911,7 @@ namespace ts.formatting {
906911
currentParent: Node,
907912
previousItem: TextRangeWithKind,
908913
previousStartLine: number,
914+
previousEndLine: number,
909915
previousParent: Node,
910916
contextNode: Node,
911917
dynamicIndentation: DynamicIndentation): boolean {
@@ -917,7 +923,7 @@ namespace ts.formatting {
917923
let trimTrailingWhitespaces: boolean;
918924
let lineAdded: boolean;
919925
if (rule) {
920-
applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine);
926+
applyRuleEdits(rule, previousItem, previousStartLine, previousEndLine, currentItem, currentStartLine);
921927

922928
if (rule.Operation.Action & (RuleAction.Space | RuleAction.Delete) && currentStartLine !== previousStartLine) {
923929
lineAdded = false;
@@ -1108,6 +1114,7 @@ namespace ts.formatting {
11081114
function applyRuleEdits(rule: Rule,
11091115
previousRange: TextRangeWithKind,
11101116
previousStartLine: number,
1117+
previousEndLine: number,
11111118
currentRange: TextRangeWithKind,
11121119
currentStartLine: number): void {
11131120

@@ -1137,7 +1144,7 @@ namespace ts.formatting {
11371144
break;
11381145
case RuleAction.Space:
11391146
// 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) {
11411148
return;
11421149
}
11431150

0 commit comments

Comments
 (0)