diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index 4fb6bc885b961..5c7d576a29a4b 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -4512,16 +4512,42 @@ export class TestState { } public baselineMapCode( - { fileName, pos }: Range, + ranges: Range[], changesFilename: string, ): void { const changes = this.getFileContent(changesFilename); - const beforeContents = this.getFileContent(fileName); - const before = beforeContents.slice(0, pos) + "[||]" + beforeContents.slice(pos); + let fileName: string | undefined; + const focusLocations = ranges.map(({ fileName: fn, pos, end }) => { + if (!fileName) { + fileName = fn; + } + return [{ start: pos, length: end - pos }]; + }); + if (!fileName) { + throw new Error("No ranges passed in, or something went wrong."); + } + let before = this.getFileContent(fileName); + focusLocations.sort((a, b) => a[0].start - b[0].start); + for (const subLoc of focusLocations) { + for (const { start, length } of subLoc) { + let offset = 0; + for (const sl2 of focusLocations) { + for (const { start: s2, length: l2 } of sl2) { + if (s2 < start) { + offset += 4; + if ((s2 + l2) > start) { + offset -= 2; + } + } + } + } + before = before.slice(0, start + offset) + "[|" + before.slice(start + offset, start + offset + length) + "|]" + before.slice(start + offset + length); + } + } const edits = this.languageService.mapCode( fileName, [changes], - [[{ start: pos, length: 1 }]], + focusLocations, this.formatCodeSettings, {}, ); diff --git a/src/harness/fourslashInterfaceImpl.ts b/src/harness/fourslashInterfaceImpl.ts index c1262a25ada0f..cc101cc148e58 100644 --- a/src/harness/fourslashInterfaceImpl.ts +++ b/src/harness/fourslashInterfaceImpl.ts @@ -240,8 +240,8 @@ export class VerifyNegatable { this.state.uncommentSelection(newFileContent); } - public baselineMapCode(range: FourSlash.Range, changesFilename: string): void { - this.state.baselineMapCode(range, changesFilename); + public baselineMapCode(ranges: FourSlash.Range[], changesFilename: string): void { + this.state.baselineMapCode(ranges, changesFilename); } } diff --git a/tests/baselines/reference/mapCodeReplaceUsingRange.mapCode.ts b/tests/baselines/reference/mapCodeReplaceUsingRange.mapCode.ts new file mode 100644 index 0000000000000..7afc919ae8aca --- /dev/null +++ b/tests/baselines/reference/mapCodeReplaceUsingRange.mapCode.ts @@ -0,0 +1,22 @@ +// === mapCode === + +// === ORIGINAL === +[|function foo() { + return 1; +}[||] +function bar() { + return [|2;|] +}|] + +// === INCOMING CHANGES === +function foo() { + return 3; +} + +// === MAPPED === +function foo() { + return 3; +} +function bar() { + return 2; +} diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index e76d4e4ebb63a..499df9cced4a1 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -466,7 +466,7 @@ declare namespace FourSlashInterface { copiedFrom?: { file: string, range: { pos: number, end: number }[] }; } }): void; - baselineMapCode(range: Range, changesFilename?: string): void; + baselineMapCode(ranges: Range[], changesFilename?: string): void; } class edit { caretPosition(): Marker; diff --git a/tests/cases/fourslash/mapCodeClassInvalidClassMember.ts b/tests/cases/fourslash/mapCodeClassInvalidClassMember.ts index 6d932edf04b47..0ff05e37b3393 100644 --- a/tests/cases/fourslash/mapCodeClassInvalidClassMember.ts +++ b/tests/cases/fourslash/mapCodeClassInvalidClassMember.ts @@ -10,4 +10,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeFocusLocationReplacement.ts b/tests/cases/fourslash/mapCodeFocusLocationReplacement.ts index 6f7a59c0ff0d7..1c01f6065ab25 100644 --- a/tests/cases/fourslash/mapCodeFocusLocationReplacement.ts +++ b/tests/cases/fourslash/mapCodeFocusLocationReplacement.ts @@ -35,4 +35,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeMethodInsertion.ts b/tests/cases/fourslash/mapCodeMethodInsertion.ts index ce91c8401b85b..3f966f7ef7e6d 100644 --- a/tests/cases/fourslash/mapCodeMethodInsertion.ts +++ b/tests/cases/fourslash/mapCodeMethodInsertion.ts @@ -20,4 +20,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeMethodReplacement.ts b/tests/cases/fourslash/mapCodeMethodReplacement.ts index 1dd1238cb7b11..2787d50af95ff 100644 --- a/tests/cases/fourslash/mapCodeMethodReplacement.ts +++ b/tests/cases/fourslash/mapCodeMethodReplacement.ts @@ -20,4 +20,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); diff --git a/tests/cases/fourslash/mapCodeMixedClassDeclaration.ts b/tests/cases/fourslash/mapCodeMixedClassDeclaration.ts index 1c55f21476be9..1b3d306637c52 100644 --- a/tests/cases/fourslash/mapCodeMixedClassDeclaration.ts +++ b/tests/cases/fourslash/mapCodeMixedClassDeclaration.ts @@ -22,4 +22,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedClassIfInsertion.ts b/tests/cases/fourslash/mapCodeNestedClassIfInsertion.ts index ffb231208984d..8f609e16267d0 100644 --- a/tests/cases/fourslash/mapCodeNestedClassIfInsertion.ts +++ b/tests/cases/fourslash/mapCodeNestedClassIfInsertion.ts @@ -22,4 +22,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedClassIfReplacement.ts b/tests/cases/fourslash/mapCodeNestedClassIfReplacement.ts index 88cdb4c634d76..d944d05fa3f16 100644 --- a/tests/cases/fourslash/mapCodeNestedClassIfReplacement.ts +++ b/tests/cases/fourslash/mapCodeNestedClassIfReplacement.ts @@ -22,4 +22,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedForInsertion.ts b/tests/cases/fourslash/mapCodeNestedForInsertion.ts index efe36833b3bbe..09ead4ee37389 100644 --- a/tests/cases/fourslash/mapCodeNestedForInsertion.ts +++ b/tests/cases/fourslash/mapCodeNestedForInsertion.ts @@ -18,4 +18,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedForOfInsertion.ts b/tests/cases/fourslash/mapCodeNestedForOfInsertion.ts index dce3c6e812d17..4a37e87ec5eab 100644 --- a/tests/cases/fourslash/mapCodeNestedForOfInsertion.ts +++ b/tests/cases/fourslash/mapCodeNestedForOfInsertion.ts @@ -16,4 +16,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedForOfReplacement.ts b/tests/cases/fourslash/mapCodeNestedForOfReplacement.ts index 4b6b352def78d..32c6414c2f01e 100644 --- a/tests/cases/fourslash/mapCodeNestedForOfReplacement.ts +++ b/tests/cases/fourslash/mapCodeNestedForOfReplacement.ts @@ -16,4 +16,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedForReplacement.ts b/tests/cases/fourslash/mapCodeNestedForReplacement.ts index 0bfe1a615bcf2..ced6e5264f425 100644 --- a/tests/cases/fourslash/mapCodeNestedForReplacement.ts +++ b/tests/cases/fourslash/mapCodeNestedForReplacement.ts @@ -16,4 +16,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedIfInsertion.ts b/tests/cases/fourslash/mapCodeNestedIfInsertion.ts index 8c5cdff24e830..1732172c2302e 100644 --- a/tests/cases/fourslash/mapCodeNestedIfInsertion.ts +++ b/tests/cases/fourslash/mapCodeNestedIfInsertion.ts @@ -18,4 +18,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedIfReplace.ts b/tests/cases/fourslash/mapCodeNestedIfReplace.ts index 9f6d1424e5418..c3ca1c5fb8366 100644 --- a/tests/cases/fourslash/mapCodeNestedIfReplace.ts +++ b/tests/cases/fourslash/mapCodeNestedIfReplace.ts @@ -18,4 +18,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedLabeledInsertion.ts b/tests/cases/fourslash/mapCodeNestedLabeledInsertion.ts index ab9266d8b8b7d..fc4a87471da41 100644 --- a/tests/cases/fourslash/mapCodeNestedLabeledInsertion.ts +++ b/tests/cases/fourslash/mapCodeNestedLabeledInsertion.ts @@ -20,4 +20,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedLabeledReplace.ts b/tests/cases/fourslash/mapCodeNestedLabeledReplace.ts index c17ff7e951176..0e60d329af91a 100644 --- a/tests/cases/fourslash/mapCodeNestedLabeledReplace.ts +++ b/tests/cases/fourslash/mapCodeNestedLabeledReplace.ts @@ -20,4 +20,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedWhileInsertion.ts b/tests/cases/fourslash/mapCodeNestedWhileInsertion.ts index 4e8132b4bfb0d..ac98cd69c0f50 100644 --- a/tests/cases/fourslash/mapCodeNestedWhileInsertion.ts +++ b/tests/cases/fourslash/mapCodeNestedWhileInsertion.ts @@ -18,4 +18,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeNestedWhileReplace.ts b/tests/cases/fourslash/mapCodeNestedWhileReplace.ts index eeea214584229..7bd21a9698e18 100644 --- a/tests/cases/fourslash/mapCodeNestedWhileReplace.ts +++ b/tests/cases/fourslash/mapCodeNestedWhileReplace.ts @@ -18,4 +18,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeRangeReplacement.ts b/tests/cases/fourslash/mapCodeRangeReplacement.ts index f94a5ebce6b4a..8fddddb6e918e 100644 --- a/tests/cases/fourslash/mapCodeRangeReplacement.ts +++ b/tests/cases/fourslash/mapCodeRangeReplacement.ts @@ -29,4 +29,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeReplaceUsingRange.ts b/tests/cases/fourslash/mapCodeReplaceUsingRange.ts new file mode 100644 index 0000000000000..fb723acc7c835 --- /dev/null +++ b/tests/cases/fourslash/mapCodeReplaceUsingRange.ts @@ -0,0 +1,17 @@ +/// + +// @Filename: /incomingChanges.ts +//// function foo() { +//// return 3; +//// } +//// +// @Filename: /index.ts +//// [|function foo() { +//// return 1; +//// }[||] +//// function bar() { +//// return [|2;|] +//// }|] +//// + +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); diff --git a/tests/cases/fourslash/mapCodeToplevelInsert.ts b/tests/cases/fourslash/mapCodeToplevelInsert.ts index 6d2c0663b9aca..8b0f7b2c431a8 100644 --- a/tests/cases/fourslash/mapCodeToplevelInsert.ts +++ b/tests/cases/fourslash/mapCodeToplevelInsert.ts @@ -13,4 +13,4 @@ //// return 2; //// } -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file diff --git a/tests/cases/fourslash/mapCodeToplevelInsertNoClass.ts b/tests/cases/fourslash/mapCodeToplevelInsertNoClass.ts index 67ec6850c839d..9feb70fb13f04 100644 --- a/tests/cases/fourslash/mapCodeToplevelInsertNoClass.ts +++ b/tests/cases/fourslash/mapCodeToplevelInsertNoClass.ts @@ -13,4 +13,4 @@ //// return 2; //// } -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); diff --git a/tests/cases/fourslash/mapCodeToplevelReplace.ts b/tests/cases/fourslash/mapCodeToplevelReplace.ts index e605c13ddfc41..dc11d2d702e38 100644 --- a/tests/cases/fourslash/mapCodeToplevelReplace.ts +++ b/tests/cases/fourslash/mapCodeToplevelReplace.ts @@ -14,4 +14,4 @@ //// } //// -verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts"); \ No newline at end of file +verify.baselineMapCode(test.ranges(), "/incomingChanges.ts"); \ No newline at end of file