|
1 | 1 | import * as fs from "fs"; |
2 | | -import {diff_match_patch as DiffMatchPatch} from "diff-match-patch"; |
3 | 2 | import ignore from "ignore"; |
4 | 3 | import {extname, join} from "path"; |
5 | 4 | import {Options as PrettierOptions, getSupportInfo, format, resolveConfig, check} from "prettier"; |
6 | 5 |
|
7 | 6 | import {PreciseFormatter} from "../precise-formatter"; |
8 | 7 | import {CharacterRange} from "../utils"; |
9 | 8 |
|
10 | | -const dmp = new DiffMatchPatch(); |
11 | | - |
12 | 9 | let PRETTIER_SUPPORTED_FILE_EXTENSIONS: string[] = []; |
13 | 10 | getSupportInfo().languages.forEach(language => { |
14 | 11 | PRETTIER_SUPPORTED_FILE_EXTENSIONS = [ |
@@ -67,20 +64,14 @@ export const preciseFormatterPrettier: PreciseFormatter<PrettierOptions> = { |
67 | 64 | config: PrettierOptions | null, |
68 | 65 | characterRanges: CharacterRange[] |
69 | 66 | ): string { |
70 | | - let patches: any = []; |
71 | | - characterRanges.forEach(characterRange => { |
72 | | - const diffs = dmp.diff_main( |
73 | | - fileContents, |
74 | | - format(fileContents, { |
75 | | - ...config, |
76 | | - rangeStart: characterRange.rangeStart, |
77 | | - rangeEnd: characterRange.rangeEnd |
78 | | - }) |
79 | | - ); |
80 | | - patches = [...patches, ...dmp.patch_make(fileContents, diffs)]; |
81 | | - }); |
82 | | - const [formattedContents] = dmp.patch_apply(patches, fileContents); |
83 | | - return formattedContents; |
| 67 | + // Start from the last character range and work backwards so that |
| 68 | + // we don't have to update character ranges to account for the changes |
| 69 | + // we've already made. |
| 70 | + return characterRanges.reduceRight( |
| 71 | + (fileContents, {rangeStart, rangeEnd}) => |
| 72 | + format(fileContents, {...config, rangeStart, rangeEnd}), |
| 73 | + fileContents |
| 74 | + ); |
84 | 75 | }, |
85 | 76 | /** |
86 | 77 | * Generate a predicate function which will return true if the filename |
|
0 commit comments