textChanges: Simplify getChanges (#21971)

* textChanges: Simplify getChanges

* Return ReadonlyArray
This commit is contained in:
Andy
2018-02-15 13:02:45 -08:00
committed by GitHub
parent 81df5313d7
commit 347bff14a9
2 changed files with 13 additions and 25 deletions

View File

@@ -1438,6 +1438,14 @@ namespace ts {
}
}
export function group<T>(values: ReadonlyArray<T>, getGroupId: (value: T) => string): ReadonlyArray<ReadonlyArray<T>> {
const groupIdToGroup = createMultiMap<T>();
for (const value of values) {
groupIdToGroup.add(getGroupId(value), value);
}
return arrayFrom(groupIdToGroup.values());
}
/**
* Tests whether a value is an array.
*/

View File

@@ -593,32 +593,12 @@ namespace ts.textChanges {
public getChanges(): FileTextChanges[] {
this.finishInsertNodeAtClassStart();
const changesPerFile = createMap<Change[]>();
// group changes per file
for (const c of this.changes) {
let changesInFile = changesPerFile.get(c.sourceFile.path);
if (!changesInFile) {
changesPerFile.set(c.sourceFile.path, changesInFile = []);
}
changesInFile.push(c);
}
// convert changes
const fileChangesList: FileTextChanges[] = [];
changesPerFile.forEach(changesInFile => {
return group(this.changes, c => c.sourceFile.path).map(changesInFile => {
const sourceFile = changesInFile[0].sourceFile;
const fileTextChanges: FileTextChanges = { fileName: sourceFile.fileName, textChanges: [] };
for (const c of ChangeTracker.normalize(changesInFile)) {
fileTextChanges.textChanges.push(createTextChange(this.computeSpan(c, sourceFile), this.computeNewText(c, sourceFile)));
}
fileChangesList.push(fileTextChanges);
const textChanges = ChangeTracker.normalize(changesInFile).map(c =>
createTextChange(createTextSpanFromRange(c.range), this.computeNewText(c, sourceFile)));
return { fileName: sourceFile.fileName, textChanges };
});
return fileChangesList;
}
private computeSpan(change: Change, _sourceFile: SourceFile): TextSpan {
return createTextSpanFromRange(change.range);
}
private computeNewText(change: Change, sourceFile: SourceFile): string {
@@ -675,7 +655,7 @@ namespace ts.textChanges {
return applyFormatting(nonformattedText, sourceFile, initialIndentation, delta, this.formatContext);
}
private static normalize(changes: Change[]): Change[] {
private static normalize(changes: ReadonlyArray<Change>): ReadonlyArray<Change> {
// order changes by start position
const normalized = stableSort(changes, (a, b) => a.range.pos - b.range.pos);
// verify that change intervals do not overlap, except possibly at end points.