Skip to content

Commit 347bff1

Browse files
author
Andy
authored
textChanges: Simplify getChanges (#21971)
* textChanges: Simplify getChanges * Return ReadonlyArray
1 parent 81df531 commit 347bff1

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/compiler/core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,14 @@ namespace ts {
14381438
}
14391439
}
14401440

1441+
export function group<T>(values: ReadonlyArray<T>, getGroupId: (value: T) => string): ReadonlyArray<ReadonlyArray<T>> {
1442+
const groupIdToGroup = createMultiMap<T>();
1443+
for (const value of values) {
1444+
groupIdToGroup.add(getGroupId(value), value);
1445+
}
1446+
return arrayFrom(groupIdToGroup.values());
1447+
}
1448+
14411449
/**
14421450
* Tests whether a value is an array.
14431451
*/

src/services/textChanges.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -593,32 +593,12 @@ namespace ts.textChanges {
593593

594594
public getChanges(): FileTextChanges[] {
595595
this.finishInsertNodeAtClassStart();
596-
597-
const changesPerFile = createMap<Change[]>();
598-
// group changes per file
599-
for (const c of this.changes) {
600-
let changesInFile = changesPerFile.get(c.sourceFile.path);
601-
if (!changesInFile) {
602-
changesPerFile.set(c.sourceFile.path, changesInFile = []);
603-
}
604-
changesInFile.push(c);
605-
}
606-
// convert changes
607-
const fileChangesList: FileTextChanges[] = [];
608-
changesPerFile.forEach(changesInFile => {
596+
return group(this.changes, c => c.sourceFile.path).map(changesInFile => {
609597
const sourceFile = changesInFile[0].sourceFile;
610-
const fileTextChanges: FileTextChanges = { fileName: sourceFile.fileName, textChanges: [] };
611-
for (const c of ChangeTracker.normalize(changesInFile)) {
612-
fileTextChanges.textChanges.push(createTextChange(this.computeSpan(c, sourceFile), this.computeNewText(c, sourceFile)));
613-
}
614-
fileChangesList.push(fileTextChanges);
598+
const textChanges = ChangeTracker.normalize(changesInFile).map(c =>
599+
createTextChange(createTextSpanFromRange(c.range), this.computeNewText(c, sourceFile)));
600+
return { fileName: sourceFile.fileName, textChanges };
615601
});
616-
617-
return fileChangesList;
618-
}
619-
620-
private computeSpan(change: Change, _sourceFile: SourceFile): TextSpan {
621-
return createTextSpanFromRange(change.range);
622602
}
623603

624604
private computeNewText(change: Change, sourceFile: SourceFile): string {
@@ -675,7 +655,7 @@ namespace ts.textChanges {
675655
return applyFormatting(nonformattedText, sourceFile, initialIndentation, delta, this.formatContext);
676656
}
677657

678-
private static normalize(changes: Change[]): Change[] {
658+
private static normalize(changes: ReadonlyArray<Change>): ReadonlyArray<Change> {
679659
// order changes by start position
680660
const normalized = stableSort(changes, (a, b) => a.range.pos - b.range.pos);
681661
// verify that change intervals do not overlap, except possibly at end points.

0 commit comments

Comments
 (0)