Skip to content

Commit

Permalink
Test focusLocation with ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed May 21, 2024
1 parent 126479b commit 054ce2d
Show file tree
Hide file tree
Showing 26 changed files with 93 additions and 28 deletions.
34 changes: 30 additions & 4 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{},
);
Expand Down
4 changes: 2 additions & 2 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/mapCodeReplaceUsingRange.mapCode.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeClassInvalidClassMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeFocusLocationReplacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeMethodInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeMethodReplacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeMixedClassDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedClassIfInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedClassIfReplacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedForInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedForOfInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedForOfReplacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedForReplacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedIfInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedIfReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedLabeledInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedLabeledReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedWhileInsertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeNestedWhileReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeRangeReplacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
17 changes: 17 additions & 0 deletions tests/cases/fourslash/mapCodeReplaceUsingRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
///<reference path="fourslash.ts"/>

// @Filename: /incomingChanges.ts
//// function foo() {
//// return 3;
//// }
////
// @Filename: /index.ts
//// [|function foo() {
//// return 1;
//// }[||]
//// function bar() {
//// return [|2;|]
//// }|]
////

verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeToplevelInsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
//// return 2;
//// }

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeToplevelInsertNoClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
//// return 2;
//// }

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");
2 changes: 1 addition & 1 deletion tests/cases/fourslash/mapCodeToplevelReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
//// }
////

verify.baselineMapCode(test.ranges()[0], "/incomingChanges.ts");
verify.baselineMapCode(test.ranges(), "/incomingChanges.ts");

0 comments on commit 054ce2d

Please sign in to comment.