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()");