Repository: microsoft/TypeScript
File: src/harness/fourslashImpl.ts (TypeScript)

Code before:
            let linkedEditInfoBaseline = "";
            linkedEditsByRange.forEach(([linkedEdit, positions]) => {
                let rangeStart = 0;
                for (let j = 0; j < positions.length - 1; j++) {
                    // for each distinct range in the list of positions, add an entry to the list of places that need to be annotated in the baseline
                    if (positions[j] + 1 !== positions[j + 1]) {
                        inlineLinkedEditBaseline.push({ start: positions[rangeStart], end: positions[j], index: offset });
                        rangeStart = j + 1;
                    }
                }
                inlineLinkedEditBaseline.push({ start: positions[rangeStart], end: positions[positions.length - 1], index: offset });

                // add the LinkedEditInfo with its index to the baseline
                linkedEditInfoBaseline += `\n\n=== ${offset} ===\n` + linkedEdit;
                offset++;
            });

            inlineLinkedEditBaseline = inlineLinkedEditBaseline.sort((a, b) => a.start - b.start);
            const fileText = activeFile.content;
            baselineContent += fileText.slice(0, inlineLinkedEditBaseline[0].start);
            for (let i = 0 ; i < inlineLinkedEditBaseline.length - 1; i++) {
                const e = inlineLinkedEditBaseline[i];
                baselineContent += `[|/*${e.index}*/` + fileText.slice(e.start, e.end) + `|]` + fileText.slice(e.end, inlineLinkedEditBaseline[i + 1].start);
            }
            const n = inlineLinkedEditBaseline[inlineLinkedEditBaseline.length - 1];
            baselineContent += `[|/*${n.index}*/` + fileText.slice(n.start, n.end) + `|]` + fileText.slice(inlineLinkedEditBaseline[inlineLinkedEditBaseline.length - 1].end) + linkedEditInfoBaseline;

            return { baselineContent, offset };
        }
    }

    public verifyMatchingBracePosition(bracePosition: number, expectedMatchPosition: number) {
        const actual = this.languageService.getBraceMatchingAtPosition(this.activeFile.fileName, bracePosition);

        if (actual.length !== 2) {
            this.raiseError(`verifyMatchingBracePosition failed - expected result to contain 2 spans, but it had ${actual.length}`);
        }

        let actualMatchPosition = -1;
        if (bracePosition === actual[0].start) {
            actualMatchPosition = actual[1].start;
        }
        else if (bracePosition === actual[1].start) {
            actualMatchPosition = actual[0].start;
        }
        else {
            this.raiseError(`verifyMatchingBracePosition failed - could not find the brace position: ${bracePosition} in the returned list: (${actual[0].start},${ts.textSpanEnd(actual[0])}) and (${actual[1].start},${ts.textSpanEnd(actual[1])})`);
        }

        if (actualMatchPosition !== expectedMatchPosition) {
            this.raiseError(`verifyMatchingBracePosition failed - expected: ${actualMatchPosition},  actual: ${expectedMatchPosition}`);

Review comment (suggestion):
```suggestion
            const lastRange = inlineLinkedEditBaseline[inlineLinkedEditBaseline.length - 1];
            baselineContent += `[|/*${lastRange.index}*/` + fileText.slice(lastRange.start, lastRange.end) + `|]` + fileText.slice(lastRange.end) + linkedEditInfoBaseline;
```

Code after:
                const existingPositions = linkedEditsInFile.get(linkedEditString) ?? [];
                linkedEditsInFile.set(linkedEditString, [...existingPositions, pos]);
            }

            const linkedEditsByRange = [...linkedEditsInFile.entries()].sort((a, b) => a[1][0] - b[1][0]);
            if (linkedEditsByRange.length === 0) {
                return { baselineContent: baselineContent + activeFile.content + `\n\n--No linked edits found--`, offset };
            }

            let inlineLinkedEditBaselines: { start: number, end: number, index: number }[] = [];
            let linkedEditInfoBaseline = "";
            for (const edit of linkedEditsByRange) {
                const [linkedEdit, positions] = edit;
                let rangeStart = 0;
                for (let j = 0; j < positions.length - 1; j++) {
                    // for each distinct range in the list of positions, add an entry to the list of places that need to be annotated in the baseline
                    if (positions[j] + 1 !== positions[j + 1]) {
                        inlineLinkedEditBaselines.push({ start: positions[rangeStart], end: positions[j], index: offset });
                        rangeStart = j + 1;
                    }
                }
                inlineLinkedEditBaselines.push({ start: positions[rangeStart], end: positions[positions.length - 1], index: offset });

                // add the LinkedEditInfo with its index to the baseline
                linkedEditInfoBaseline += `\n\n=== ${offset} ===\n` + linkedEdit;
                offset++;
            }

            inlineLinkedEditBaselines = inlineLinkedEditBaselines.sort((a, b) => a.start - b.start);
            const fileText = activeFile.content;
            baselineContent += fileText.slice(0, inlineLinkedEditBaselines[0].start);
            for (let i = 0; i < inlineLinkedEditBaselines.length; i++) {
                const e = inlineLinkedEditBaselines[i];
                const sliceEnd = inlineLinkedEditBaselines[i + 1]?.start;
                baselineContent += `[|/*${e.index}*/` + fileText.slice(e.start, e.end) + `|]` + fileText.slice(e.end, sliceEnd);
            }

            baselineContent += linkedEditInfoBaseline;
            return { baselineContent, offset };
        }
    }

    public verifyMatchingBracePosition(bracePosition: number, expectedMatchPosition: number) {
        const actual = this.languageService.getBraceMatchingAtPosition(this.activeFile.fileName, bracePosition);

        if (actual.length !== 2) {
            this.raiseError(`verifyMatchingBracePosition failed - expected result to contain 2 spans, but it had ${actual.length}`);
        }

        let actualMatchPosition = -1;
        if (bracePosition === actual[0].start) {