MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / ReplaceInFilePartial

Function ReplaceInFilePartial

pkg/util/fileutil/fileutil.go:371–397  ·  view source on GitHub ↗

ReplaceInFilePartial applies edits incrementally up to the first failure. Returns the results for each edit and writes the partially modified content.

(filePath string, edits []EditSpec)

Source from the content-addressed store, hash-verified

369// ReplaceInFilePartial applies edits incrementally up to the first failure.
370// Returns the results for each edit and writes the partially modified content.
371func ReplaceInFilePartial(filePath string, edits []EditSpec) ([]EditResult, error) {
372 fileInfo, err := os.Stat(filePath)
373 if err != nil {
374 return nil, fmt.Errorf("failed to stat file: %w", err)
375 }
376
377 if !fileInfo.Mode().IsRegular() {
378 return nil, fmt.Errorf("not a regular file: %s", filePath)
379 }
380
381 if fileInfo.Size() > MaxEditFileSize {
382 return nil, fmt.Errorf("file too large for editing: %d bytes (max: %d)", fileInfo.Size(), MaxEditFileSize)
383 }
384
385 contents, err := os.ReadFile(filePath)
386 if err != nil {
387 return nil, fmt.Errorf("failed to read file: %w", err)
388 }
389
390 modifiedContents, results := ApplyEditsPartial(contents, edits)
391
392 if err := os.WriteFile(filePath, modifiedContents, fileInfo.Mode()); err != nil {
393 return nil, fmt.Errorf("failed to write file: %w", err)
394 }
395
396 return results, nil
397}

Callers 1

ReplaceInAppFilePartialFunction · 0.92

Calls 6

ApplyEditsPartialFunction · 0.85
StatMethod · 0.80
ModeMethod · 0.80
ReadFileMethod · 0.80
WriteFileMethod · 0.80
SizeMethod · 0.45

Tested by

no test coverage detected