MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / applyEditToFile

Function applyEditToFile

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

Source from the content-addressed store, hash-verified

206 * @returns Array of edits with replace_all guaranteed to be boolean
207 */
208export function applyEditToFile(
209 originalContent: string,
210 oldString: string,
211 newString: string,
212 replaceAll: boolean = false,
213): string {
214 const f = replaceAll
215 ? (content: string, search: string, replace: string) =>
216 content.replaceAll(search, () => replace)
217 : (content: string, search: string, replace: string) =>
218 content.replace(search, () => replace)
219
220 if (newString !== '') {
221 return f(originalContent, oldString, newString)
222 }
223
224 const stripTrailingNewline =
225 !oldString.endsWith('\n') && originalContent.includes(oldString + '\n')
226
227 return stripTrailingNewline
228 ? f(originalContent, oldString + '\n', newString)
229 : f(originalContent, oldString, newString)
230}
231
232/**
233 * 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.85

Tested by

no test coverage detected