Skip to content

Commit

Permalink
Tweak microsoft#1
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Oct 25, 2017
1 parent 181e04b commit e961fa6
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -908,6 +913,7 @@ namespace ts.formatting {
currentParent: Node,
previousItem: TextRangeWithKind,
previousStartLine: number,
previousEndLine: number,
previousParent: Node,
contextNode: Node,
dynamicIndentation: DynamicIndentation): boolean {
Expand All @@ -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;
Expand Down Expand Up @@ -1110,6 +1116,7 @@ namespace ts.formatting {
function applyRuleEdits(rule: Rule,
previousRange: TextRangeWithKind,
previousStartLine: number,
previousEndLine: number,
currentRange: TextRangeWithKind,
currentStartLine: number): void {

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e961fa6

Please sign in to comment.