From fcdf1fa548f3f50379fe3a088082fff3001b3093 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Mon, 2 May 2022 12:19:44 -0700 Subject: [PATCH] Fix formatter's processChildNodes processChildNodes needs to skip processing when the node array is outside the target range, just like processChildNode already does for a single node. Fixes #48006 --- src/services/formatting/formatting.ts | 7 +++++++ src/services/formatting/formattingScanner.ts | 4 ++-- tests/cases/fourslash/formatAfterPasteInString.ts | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/formatAfterPasteInString.ts diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 5a9948cd35f72..cc5e2b6c41c46 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -778,6 +778,13 @@ namespace ts.formatting { let listDynamicIndentation = parentDynamicIndentation; let startLine = parentStartLine; + // node range is outside the target range - do not dive inside + if (!rangeOverlapsWithStartEnd(originalRange, nodes.pos, nodes.end)) { + if (nodes.end < originalRange.pos) { + formattingScanner.skipToEndOf(nodes); + } + return; + } if (listStartToken !== SyntaxKind.Unknown) { // introduce a new indentation scope for lists (including list start and end tokens) diff --git a/src/services/formatting/formattingScanner.ts b/src/services/formatting/formattingScanner.ts index 05bbc34041600..56ad87770ba7b 100644 --- a/src/services/formatting/formattingScanner.ts +++ b/src/services/formatting/formattingScanner.ts @@ -12,7 +12,7 @@ namespace ts.formatting { readEOFTokenRange(): TextRangeWithKind; getCurrentLeadingTrivia(): TextRangeWithKind[] | undefined; lastTrailingTriviaWasNewLine(): boolean; - skipToEndOf(node: Node): void; + skipToEndOf(node: Node | NodeArray): void; skipToStartOf(node: Node): void; } @@ -286,7 +286,7 @@ namespace ts.formatting { return tokenInfo; } - function skipToEndOf(node: Node): void { + function skipToEndOf(node: Node | NodeArray): void { scanner.setTextPos(node.end); savedPos = scanner.getStartPos(); lastScanAction = undefined; diff --git a/tests/cases/fourslash/formatAfterPasteInString.ts b/tests/cases/fourslash/formatAfterPasteInString.ts new file mode 100644 index 0000000000000..716af9183bbf9 --- /dev/null +++ b/tests/cases/fourslash/formatAfterPasteInString.ts @@ -0,0 +1,9 @@ +/// + +//// /*2*/const x = f('aa/*1*/a').x() + +goTo.marker('1'); +edit.paste("bb"); +format.document(); +goTo.marker('2'); +verify.currentLineContentIs("const x = f('aabba').x()");