fuzzypatch

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2025 License: MIT Imports: 6 Imported by: 0

README

FuzzyPatch

FuzzyPatch is a Go library that enables fuzzy matching and patching of text files. It's particularly useful for applying patches when the exact line positions may have changed. It's meant for use with AI coding agents which often make small mistakes when generating diffs.

Installation

go get github.com/icholy/fuzzypatch

Usage

FuzzyPatch accepts diff in the following format:

<<<<<<< SEARCH line:<n>
[search text...]
=======
[replace text...]
>>>>>>> REPLACE

Where:

  • <n> is the line number hint where the search should start
  • [search text...] is the text to find (can span multiple lines)
  • [replace text...] is the text to replace it with (can span multiple lines)
Example
package main

import (
    "fmt"
    "github.com/icholy/fuzzypatch"
)

var difftext = `
<<<<<<< SEARCH line:2
    console.log("Hello, world!");
=======
    console.log("Hello, Universe!");
>>>>>>> REPLACE
`

var source = `
function sayHello() {
    console.log("Hello, world!");
    // Some comment
    doSomethingElse();
}
`

func main() {
    // Parse a diff block
    diffs, _ := fuzzypatch.Parse(difftext)

    // Map diffs to edits
    var edits []fuzzypatch.Edit
    for _, diff := range diffs {
        // Search with 0.9 similarity threshold (90% similar)
        if edit, ok := fuzzypatch.Search(source, diff, 0.9); ok {
            edits = append(edits, edit)
        }
    }

    // Apply all edits in one pass
    result, _ := fuzzypatch.Apply(source, edits)
    fmt.Println(result)
}

Documentation

Index

Constants

View Source
const (
	EOF tokenType
)

Variables

This section is empty.

Functions

func Apply

func Apply(source string, edits []Edit) (string, error)

Apply performs all edits in one pass. Edits are applied back‑to‑front so earlier byte offsets remain valid.

Types

type Diff

type Diff struct {
	Line    int    // 1-based line number where the search should start
	Search  string // Text to find in the document
	Replace string // Text to replace the found section with
}

Diff represents a text replacement operation with search and replace strings that should be applied at a specific line position in a document.

func Parse

func Parse(input string) ([]Diff, error)

type Edit

type Edit struct {
	Start int    // Byte offset where the edit starts (inclusive)
	End   int    // Byte offset where the edit ends (exclusive)
	Text  string // New text to replace the section between Start and End
}

Edit represents a specific text edit operation with byte offsets that can be applied to a document.

func Search(source string, diff Diff, threshold float64) (Edit, bool)

Search tries to locate `diff.Search` inside `source`. It begins at the requested line and expands alternately upward/downward until a slice whose similarity ≥ threshold is found.

Similarity = 1 - (levenshtein distance / maxLen). On success it returns the byte‑offset edit [Start, End) to replace and true. If nothing satisfies the threshold it returns (zero Edit, false).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL