Merge pull request #9420 from Microsoft/formatting_loc

Do not scan nodes preceding formatted region, just skip over them
This commit is contained in:
Mohamed Hegazy
2016-07-05 14:40:48 -07:00
committed by GitHub
8 changed files with 55 additions and 22 deletions
+6 -3
View File
@@ -78,7 +78,7 @@ namespace ts.formatting {
// 1. the end of the previous line
// 2. the last non-whitespace character in the current line
let endOfFormatSpan = getEndLinePosition(line, sourceFile);
while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
while (isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(endOfFormatSpan))) {
endOfFormatSpan--;
}
// if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to
@@ -599,6 +599,9 @@ namespace ts.formatting {
// child node is outside the target range - do not dive inside
if (!rangeOverlapsWithStartEnd(originalRange, child.pos, child.end)) {
if (child.end < originalRange.pos) {
formattingScanner.skipToEndOf(child);
}
return inheritedIndentation;
}
@@ -963,7 +966,7 @@ namespace ts.formatting {
const whitespaceStart = getTrailingWhitespaceStartPosition(lineStartPosition, lineEndPosition);
if (whitespaceStart !== -1) {
Debug.assert(whitespaceStart === lineStartPosition || !isWhiteSpace(sourceFile.text.charCodeAt(whitespaceStart - 1)));
Debug.assert(whitespaceStart === lineStartPosition || !isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(whitespaceStart - 1)));
recordDelete(whitespaceStart, lineEndPosition + 1 - whitespaceStart);
}
}
@@ -975,7 +978,7 @@ namespace ts.formatting {
*/
function getTrailingWhitespaceStartPosition(start: number, end: number) {
let pos = end;
while (pos >= start && isWhiteSpace(sourceFile.text.charCodeAt(pos))) {
while (pos >= start && isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(pos))) {
pos--;
}
if (pos !== end) {
+16 -4
View File
@@ -17,6 +17,7 @@ namespace ts.formatting {
readTokenInfo(n: Node): TokenInfo;
getCurrentLeadingTrivia(): TextRangeWithKind[];
lastTrailingTriviaWasNewLine(): boolean;
skipToEndOf(node: Node): void;
close(): void;
}
@@ -36,12 +37,12 @@ namespace ts.formatting {
scanner.setTextPos(startPos);
let wasNewLine = true;
let leadingTrivia: TextRangeWithKind[];
let trailingTrivia: TextRangeWithKind[];
let leadingTrivia: TextRangeWithKind[] | undefined;
let trailingTrivia: TextRangeWithKind[] | undefined;
let savedPos: number;
let lastScanAction: ScanAction;
let lastTokenInfo: TokenInfo;
let lastScanAction: ScanAction | undefined;
let lastTokenInfo: TokenInfo | undefined;
return {
advance,
@@ -49,6 +50,7 @@ namespace ts.formatting {
isOnToken,
getCurrentLeadingTrivia: () => leadingTrivia,
lastTrailingTriviaWasNewLine: () => wasNewLine,
skipToEndOf,
close: () => {
Debug.assert(scanner !== undefined);
@@ -278,5 +280,15 @@ namespace ts.formatting {
}
return tokenInfo;
}
function skipToEndOf(node: Node): void {
scanner.setTextPos(node.end);
savedPos = scanner.getStartPos();
lastScanAction = undefined;
lastTokenInfo = undefined;
wasNewLine = false;
leadingTrivia = undefined;
trailingTrivia = undefined;
}
}
}
+2 -2
View File
@@ -42,7 +42,7 @@ namespace ts.formatting {
let current = position;
while (current > 0) {
const char = sourceFile.text.charCodeAt(current);
if (!isWhiteSpace(char) && !isLineBreak(char)) {
if (!isWhiteSpace(char)) {
break;
}
current--;
@@ -406,7 +406,7 @@ namespace ts.formatting {
let column = 0;
for (let pos = startPos; pos < endPos; pos++) {
const ch = sourceFile.text.charCodeAt(pos);
if (!isWhiteSpace(ch)) {
if (!isWhiteSpaceSingleLine(ch)) {
break;
}
+4 -6
View File
@@ -474,8 +474,7 @@ namespace ts {
for (; pos < end; pos++) {
const ch = sourceFile.text.charCodeAt(pos);
if (!isWhiteSpace(ch) || isLineBreak(ch)) {
// Either found lineBreak or non whiteSpace
if (!isWhiteSpaceSingleLine(ch)) {
return pos;
}
}
@@ -494,8 +493,7 @@ namespace ts {
function isName(pos: number, end: number, sourceFile: SourceFile, name: string) {
return pos + name.length < end &&
sourceFile.text.substr(pos, name.length) === name &&
(isWhiteSpace(sourceFile.text.charCodeAt(pos + name.length)) ||
isLineBreak(sourceFile.text.charCodeAt(pos + name.length)));
isWhiteSpace(sourceFile.text.charCodeAt(pos + name.length));
}
function isParamTag(pos: number, end: number, sourceFile: SourceFile) {
@@ -690,7 +688,7 @@ namespace ts {
return paramDocComments;
function consumeWhiteSpaces(pos: number) {
while (pos < end && isWhiteSpace(sourceFile.text.charCodeAt(pos))) {
while (pos < end && isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(pos))) {
pos++;
}
@@ -5727,7 +5725,7 @@ namespace ts {
// Avoid recalculating getStart() by iterating backwards.
for (let j = ifKeyword.getStart() - 1; j >= elseKeyword.end; j--) {
if (!isWhiteSpace(sourceFile.text.charCodeAt(j))) {
if (!isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(j))) {
shouldCombindElseAndIf = false;
break;
}