mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-24 11:43:18 -05:00
textChanges: Simplify getChanges (#21971)
* textChanges: Simplify getChanges * Return ReadonlyArray
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user