From e961fa6a3bc5c471c86d59213038bd21dccef55a Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 23 Oct 2017 21:12:09 -0400 Subject: [PATCH] Tweak #1 --- src/services/formatting/formatting.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index ad9b2f179aac6..a0cc61402ecb5 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -402,6 +402,7 @@ namespace ts.formatting { let previousRange: TextRangeWithKind; let previousParent: Node; let previousRangeStartLine: number; + let previousRangeEndLine: number; let lastIndentedLine: number; let indentationOnLastIndentedLine: number; @@ -795,11 +796,12 @@ namespace ts.formatting { const isTokenInRange = rangeContainsRange(originalRange, currentTokenInfo.token); const tokenStart = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.pos); + const tokenEnd = sourceFile.getLineAndCharacterOfPosition(currentTokenInfo.token.end); if (isTokenInRange) { const rangeHasError = rangeContainsError(currentTokenInfo.token); // save previousRange since processRange will overwrite this value with current one const savePreviousRange = previousRange; - lineAdded = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation); + lineAdded = processRange(currentTokenInfo.token, tokenStart, tokenEnd, parent, childContextNode, dynamicIndentation); if (rangeHasError) { // do not indent comments\token if token range overlaps with some error indentToken = false; @@ -870,13 +872,15 @@ namespace ts.formatting { for (const triviaItem of trivia) { if (isComment(triviaItem.kind) && rangeContainsRange(originalRange, triviaItem)) { const triviaItemStart = sourceFile.getLineAndCharacterOfPosition(triviaItem.pos); - processRange(triviaItem, triviaItemStart, parent, contextNode, dynamicIndentation); + const triviaItemEnd = sourceFile.getLineAndCharacterOfPosition(triviaItem.end); + processRange(triviaItem, triviaItemStart, triviaItemEnd, parent, contextNode, dynamicIndentation); } } } function processRange(range: TextRangeWithKind, rangeStart: LineAndCharacter, + rangeEnd: LineAndCharacter, parent: Node, contextNode: Node, dynamicIndentation: DynamicIndentation): boolean { @@ -891,13 +895,14 @@ namespace ts.formatting { } else { lineAdded = - processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousParent, contextNode, dynamicIndentation); + processPair(range, rangeStart.line, parent, previousRange, previousRangeStartLine, previousRangeEndLine, previousParent, contextNode, dynamicIndentation); } } previousRange = range; previousParent = parent; previousRangeStartLine = rangeStart.line; + previousRangeEndLine = rangeEnd.line; previousRangeHasError = rangeHasError; return lineAdded; @@ -908,6 +913,7 @@ namespace ts.formatting { currentParent: Node, previousItem: TextRangeWithKind, previousStartLine: number, + previousEndLine: number, previousParent: Node, contextNode: Node, dynamicIndentation: DynamicIndentation): boolean { @@ -919,7 +925,7 @@ namespace ts.formatting { let trimTrailingWhitespaces: boolean; let lineAdded: boolean; if (rule) { - applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine); + applyRuleEdits(rule, previousItem, previousStartLine, previousEndLine, currentItem, currentStartLine); if (rule.Operation.Action & (RuleAction.Space | RuleAction.Delete) && currentStartLine !== previousStartLine) { lineAdded = false; @@ -1110,6 +1116,7 @@ namespace ts.formatting { function applyRuleEdits(rule: Rule, previousRange: TextRangeWithKind, previousStartLine: number, + previousEndLine: number, currentRange: TextRangeWithKind, currentStartLine: number): void { @@ -1139,7 +1146,7 @@ namespace ts.formatting { break; case RuleAction.Space: // exit early if we on different lines and rule cannot change number of newlines - if (rule.Flag !== RuleFlags.CanDeleteNewLines && previousStartLine !== currentStartLine) { + if (rule.Flag !== RuleFlags.CanDeleteNewLines && previousEndLine !== currentStartLine) { return; }