Skip to content

Commit

Permalink
fix: #685 - Fix applyTextChanges in node 11 and 12.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Sep 12, 2019
1 parent fe8d3a4 commit e72b124
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/manipulation/formatting/getTextFromFormattingEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { SourceFile, TextChange } from "../../compiler";

export function getTextFromFormattingEdits(sourceFile: SourceFile, formattingEdits: ReadonlyArray<TextChange>) {
// reverse the order
formattingEdits = [...formattingEdits].sort((a, b) => {
const aStart = a.getSpan().getStart();
const bStart = b.getSpan().getStart();
const reversedFormattingEdits = formattingEdits.map((edit, index) => ({ edit, index })).sort((a, b) => {
const aStart = a.edit.getSpan().getStart();
const bStart = b.edit.getSpan().getStart();
const difference = bStart - aStart;
return difference === 0 ? 1 : difference;
if (difference === 0)
return a.index < b.index ? 1 : -1;
return difference > 0 ? 1 : -1;
});

let text = sourceFile.getFullText();
for (const textChange of formattingEdits) {
const span = textChange.getSpan();
text = text.slice(0, span.getStart()) + textChange.getNewText() + text.slice(span.getEnd());
for (const { edit } of reversedFormattingEdits) {
const span = edit.getSpan();
text = text.slice(0, span.getStart()) + edit.getNewText() + text.slice(span.getEnd());
}
return text;
}

0 comments on commit e72b124

Please sign in to comment.