({
filePath,
fileContents,
edits,
ignoreWhitespace = false,
}: {
filePath: string
fileContents: string
edits: FileEdit[]
ignoreWhitespace?: boolean
})
| 126 | */ |
| 127 | |
| 128 | export function getPatchForDisplay({ |
| 129 | filePath, |
| 130 | fileContents, |
| 131 | edits, |
| 132 | ignoreWhitespace = false, |
| 133 | }: { |
| 134 | filePath: string |
| 135 | fileContents: string |
| 136 | edits: FileEdit[] |
| 137 | ignoreWhitespace?: boolean |
| 138 | }): StructuredPatchHunk[] { |
| 139 | const preparedFileContents = escapeForDiff( |
| 140 | convertLeadingTabsToSpaces(fileContents), |
| 141 | ) |
| 142 | const result = structuredPatch( |
| 143 | filePath, |
| 144 | filePath, |
| 145 | preparedFileContents, |
| 146 | edits.reduce((p, edit) => { |
| 147 | const { old_string, new_string } = edit |
| 148 | const replace_all = 'replace_all' in edit ? edit.replace_all : false |
| 149 | const escapedOldString = escapeForDiff( |
| 150 | convertLeadingTabsToSpaces(old_string), |
| 151 | ) |
| 152 | const escapedNewString = escapeForDiff( |
| 153 | convertLeadingTabsToSpaces(new_string), |
| 154 | ) |
| 155 | |
| 156 | if (replace_all) { |
| 157 | return p.replaceAll(escapedOldString, () => escapedNewString) |
| 158 | } else { |
| 159 | return p.replace(escapedOldString, () => escapedNewString) |
| 160 | } |
| 161 | }, preparedFileContents), |
| 162 | undefined, |
| 163 | undefined, |
| 164 | { |
| 165 | context: CONTEXT_LINES, |
| 166 | ignoreWhitespace, |
| 167 | timeout: DIFF_TIMEOUT_MS, |
| 168 | }, |
| 169 | ) |
| 170 | if (!result) { |
| 171 | return [] |
| 172 | } |
| 173 | return result.hunks.map(_ => ({ |
| 174 | ..._, |
| 175 | lines: _.lines.map(unescapeFromDiff), |
| 176 | })) |
| 177 | } |
no test coverage detected