MCPcopy
hub / github.com/codeaashu/claude-code / applyEditToFile

Function applyEditToFile

src/tools/FileEditTool/utils.ts:206–228  ·  view source on GitHub ↗
(
  originalContent: string,
  oldString: string,
  newString: string,
  replaceAll: boolean = false,
)

Source from the content-addressed store, hash-verified

204 * @returns Array of edits with replace_all guaranteed to be boolean
205 */
206export function applyEditToFile(
207 originalContent: string,
208 oldString: string,
209 newString: string,
210 replaceAll: boolean = false,
211): string {
212 const f = replaceAll
213 ? (content: string, search: string, replace: string) =>
214 content.replaceAll(search, () => replace)
215 : (content: string, search: string, replace: string) =>
216 content.replace(search, () => replace)
217
218 if (newString !== '') {
219 return f(originalContent, oldString, newString)
220 }
221
222 const stripTrailingNewline =
223 !oldString.endsWith('\n') && originalContent.includes(oldString + '\n')
224
225 return stripTrailingNewline
226 ? f(originalContent, oldString + '\n', newString)
227 : f(originalContent, oldString, newString)
228}
229
230/**
231 * Applies an edit to a file and returns the patch and updated file.

Callers 2

getPatchForEditsFunction · 0.85
getSnippetFunction · 0.85

Calls 1

fFunction · 0.50

Tested by

no test coverage detected