Skip to content

Commit 1102fa6

Browse files
authored
Merge pull request microsoft#9420 from Microsoft/formatting_loc
Do not scan nodes preceding formatted region, just skip over them
2 parents 29ac7e4 + 2c40fea commit 1102fa6

8 files changed

Lines changed: 55 additions & 22 deletions

File tree

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7686,7 +7686,7 @@ const _super = (function (geti, seti) {
76867686
}
76877687
firstNonWhitespace = -1;
76887688
}
7689-
else if (!isWhiteSpace(c)) {
7689+
else if (!isWhiteSpaceSingleLine(c)) {
76907690
lastNonWhitespace = i;
76917691
if (firstNonWhitespace === -1) {
76927692
firstNonWhitespace = i;

src/compiler/scanner.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace ts {
3131
scanJsxToken(): SyntaxKind;
3232
scanJSDocToken(): SyntaxKind;
3333
scan(): SyntaxKind;
34+
getText(): string;
3435
// Sets the text for the scanner to scan. An optional subrange starting point and length
3536
// can be provided to have the scanner only scan a portion of the text.
3637
setText(text: string, start?: number, length?: number): void;
@@ -365,6 +366,11 @@ namespace ts {
365366
const hasOwnProperty = Object.prototype.hasOwnProperty;
366367

367368
export function isWhiteSpace(ch: number): boolean {
369+
return isWhiteSpaceSingleLine(ch) || isLineBreak(ch);
370+
}
371+
372+
/** Does not include line breaks. For that, see isWhiteSpaceLike. */
373+
export function isWhiteSpaceSingleLine(ch: number): boolean {
368374
// Note: nextLine is in the Zs space, and should be considered to be a whitespace.
369375
// It is explicitly not a line-break as it isn't in the exact set specified by EcmaScript.
370376
return ch === CharacterCodes.space ||
@@ -505,7 +511,7 @@ namespace ts {
505511
break;
506512

507513
default:
508-
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch) || isLineBreak(ch))) {
514+
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch))) {
509515
pos++;
510516
continue;
511517
}
@@ -658,7 +664,7 @@ namespace ts {
658664
}
659665
break;
660666
default:
661-
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch) || isLineBreak(ch))) {
667+
if (ch > CharacterCodes.maxAsciiCharacter && (isWhiteSpace(ch))) {
662668
if (result && result.length && isLineBreak(ch)) {
663669
lastOrUndefined(result).hasTrailingNewLine = true;
664670
}
@@ -763,6 +769,7 @@ namespace ts {
763769
scanJsxToken,
764770
scanJSDocToken,
765771
scan,
772+
getText,
766773
setText,
767774
setScriptTarget,
768775
setLanguageVariant,
@@ -1202,7 +1209,7 @@ namespace ts {
12021209
continue;
12031210
}
12041211
else {
1205-
while (pos < end && isWhiteSpace(text.charCodeAt(pos))) {
1212+
while (pos < end && isWhiteSpaceSingleLine(text.charCodeAt(pos))) {
12061213
pos++;
12071214
}
12081215
return token = SyntaxKind.WhitespaceTrivia;
@@ -1520,7 +1527,7 @@ namespace ts {
15201527
}
15211528
return token = getIdentifierToken();
15221529
}
1523-
else if (isWhiteSpace(ch)) {
1530+
else if (isWhiteSpaceSingleLine(ch)) {
15241531
pos++;
15251532
continue;
15261533
}
@@ -1689,7 +1696,7 @@ namespace ts {
16891696
let ch = text.charCodeAt(pos);
16901697
while (pos < end) {
16911698
ch = text.charCodeAt(pos);
1692-
if (isWhiteSpace(ch)) {
1699+
if (isWhiteSpaceSingleLine(ch)) {
16931700
pos++;
16941701
}
16951702
else {
@@ -1789,6 +1796,10 @@ namespace ts {
17891796
return speculationHelper(callback, /*isLookahead*/ false);
17901797
}
17911798

1799+
function getText(): string {
1800+
return text;
1801+
}
1802+
17921803
function setText(newText: string, start: number, length: number) {
17931804
text = newText || "";
17941805
end = length === undefined ? text.length : start + length;

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ namespace ts {
26052605

26062606
function calculateIndent(text: string, pos: number, end: number) {
26072607
let currentLineIndent = 0;
2608-
for (; pos < end && isWhiteSpace(text.charCodeAt(pos)); pos++) {
2608+
for (; pos < end && isWhiteSpaceSingleLine(text.charCodeAt(pos)); pos++) {
26092609
if (text.charCodeAt(pos) === CharacterCodes.tab) {
26102610
// Tabs = TabSize = indent size and go to next tabStop
26112611
currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize());

src/services/formatting/formatting.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace ts.formatting {
7878
// 1. the end of the previous line
7979
// 2. the last non-whitespace character in the current line
8080
let endOfFormatSpan = getEndLinePosition(line, sourceFile);
81-
while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
81+
while (isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(endOfFormatSpan))) {
8282
endOfFormatSpan--;
8383
}
8484
// 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 {
599599

600600
// child node is outside the target range - do not dive inside
601601
if (!rangeOverlapsWithStartEnd(originalRange, child.pos, child.end)) {
602+
if (child.end < originalRange.pos) {
603+
formattingScanner.skipToEndOf(child);
604+
}
602605
return inheritedIndentation;
603606
}
604607

@@ -963,7 +966,7 @@ namespace ts.formatting {
963966

964967
const whitespaceStart = getTrailingWhitespaceStartPosition(lineStartPosition, lineEndPosition);
965968
if (whitespaceStart !== -1) {
966-
Debug.assert(whitespaceStart === lineStartPosition || !isWhiteSpace(sourceFile.text.charCodeAt(whitespaceStart - 1)));
969+
Debug.assert(whitespaceStart === lineStartPosition || !isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(whitespaceStart - 1)));
967970
recordDelete(whitespaceStart, lineEndPosition + 1 - whitespaceStart);
968971
}
969972
}
@@ -975,7 +978,7 @@ namespace ts.formatting {
975978
*/
976979
function getTrailingWhitespaceStartPosition(start: number, end: number) {
977980
let pos = end;
978-
while (pos >= start && isWhiteSpace(sourceFile.text.charCodeAt(pos))) {
981+
while (pos >= start && isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(pos))) {
979982
pos--;
980983
}
981984
if (pos !== end) {

src/services/formatting/formattingScanner.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace ts.formatting {
1717
readTokenInfo(n: Node): TokenInfo;
1818
getCurrentLeadingTrivia(): TextRangeWithKind[];
1919
lastTrailingTriviaWasNewLine(): boolean;
20+
skipToEndOf(node: Node): void;
2021
close(): void;
2122
}
2223

@@ -36,19 +37,20 @@ namespace ts.formatting {
3637
scanner.setTextPos(startPos);
3738

3839
let wasNewLine = true;
39-
let leadingTrivia: TextRangeWithKind[];
40-
let trailingTrivia: TextRangeWithKind[];
40+
let leadingTrivia: TextRangeWithKind[] | undefined;
41+
let trailingTrivia: TextRangeWithKind[] | undefined;
4142

4243
let savedPos: number;
43-
let lastScanAction: ScanAction;
44-
let lastTokenInfo: TokenInfo;
44+
let lastScanAction: ScanAction | undefined;
45+
let lastTokenInfo: TokenInfo | undefined;
4546

4647
return {
4748
advance,
4849
readTokenInfo,
4950
isOnToken,
5051
getCurrentLeadingTrivia: () => leadingTrivia,
5152
lastTrailingTriviaWasNewLine: () => wasNewLine,
53+
skipToEndOf,
5254
close: () => {
5355
Debug.assert(scanner !== undefined);
5456

@@ -278,5 +280,15 @@ namespace ts.formatting {
278280
}
279281
return tokenInfo;
280282
}
283+
284+
function skipToEndOf(node: Node): void {
285+
scanner.setTextPos(node.end);
286+
savedPos = scanner.getStartPos();
287+
lastScanAction = undefined;
288+
lastTokenInfo = undefined;
289+
wasNewLine = false;
290+
leadingTrivia = undefined;
291+
trailingTrivia = undefined;
292+
}
281293
}
282294
}

src/services/formatting/smartIndenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace ts.formatting {
4242
let current = position;
4343
while (current > 0) {
4444
const char = sourceFile.text.charCodeAt(current);
45-
if (!isWhiteSpace(char) && !isLineBreak(char)) {
45+
if (!isWhiteSpace(char)) {
4646
break;
4747
}
4848
current--;
@@ -406,7 +406,7 @@ namespace ts.formatting {
406406
let column = 0;
407407
for (let pos = startPos; pos < endPos; pos++) {
408408
const ch = sourceFile.text.charCodeAt(pos);
409-
if (!isWhiteSpace(ch)) {
409+
if (!isWhiteSpaceSingleLine(ch)) {
410410
break;
411411
}
412412

src/services/services.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ namespace ts {
474474

475475
for (; pos < end; pos++) {
476476
const ch = sourceFile.text.charCodeAt(pos);
477-
if (!isWhiteSpace(ch) || isLineBreak(ch)) {
478-
// Either found lineBreak or non whiteSpace
477+
if (!isWhiteSpaceSingleLine(ch)) {
479478
return pos;
480479
}
481480
}
@@ -494,8 +493,7 @@ namespace ts {
494493
function isName(pos: number, end: number, sourceFile: SourceFile, name: string) {
495494
return pos + name.length < end &&
496495
sourceFile.text.substr(pos, name.length) === name &&
497-
(isWhiteSpace(sourceFile.text.charCodeAt(pos + name.length)) ||
498-
isLineBreak(sourceFile.text.charCodeAt(pos + name.length)));
496+
isWhiteSpace(sourceFile.text.charCodeAt(pos + name.length));
499497
}
500498

501499
function isParamTag(pos: number, end: number, sourceFile: SourceFile) {
@@ -690,7 +688,7 @@ namespace ts {
690688
return paramDocComments;
691689

692690
function consumeWhiteSpaces(pos: number) {
693-
while (pos < end && isWhiteSpace(sourceFile.text.charCodeAt(pos))) {
691+
while (pos < end && isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(pos))) {
694692
pos++;
695693
}
696694

@@ -5727,7 +5725,7 @@ namespace ts {
57275725

57285726
// Avoid recalculating getStart() by iterating backwards.
57295727
for (let j = ifKeyword.getStart() - 1; j >= elseKeyword.end; j--) {
5730-
if (!isWhiteSpace(sourceFile.text.charCodeAt(j))) {
5728+
if (!isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(j))) {
57315729
shouldCombindElseAndIf = false;
57325730
break;
57335731
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////`${1}`;
4+
////`
5+
////`;/**/1
6+
7+
goTo.marker();
8+
edit.insert('\n');
9+
verify.currentLineContentIs("1");

0 commit comments

Comments
 (0)