fabricatio_diff.models.diff

Diff module providing a dataclass for managing text diffs.

Classes

Diff

A dataclass representing a text diff operation.

Module Contents

class fabricatio_diff.models.diff.Diff(/, **data: Any)

Bases: fabricatio_core.models.generic.Display

A dataclass representing a text diff operation.

search: str

The text pattern to search for.

replace: str

The text to replace the matched pattern with.

start_anchor: str | None = None

HASH format).

Type:

Hashline anchor for the range start (LINE

end_anchor: str | None = None

HASH format).

Type:

Hashline anchor for the range end (LINE

start_line: int | None = None

1-indexed line number for range start.

end_line: int | None = None

1-indexed line number for range end.

apply(text: str, match_precision: float = 1.0) str | None

Applies the diff operation to the given text.

Supports three modes: 1. Anchor-based: Uses start_anchor and end_anchor to define a line range 2. Line-number-based: Uses start_line and end_line to define a line range 3. Pattern matching: Uses search/replace for fuzzy line matching

Parameters:
  • text (str) – The original text to apply the diff on.

  • match_precision (float) – The precision threshold for matching lines (default is 1.0).

Returns:

The modified text if a match is found and replaced; otherwise None.

Return type:

str | None

property diff: str

Returns the diff between the search and replace patterns.

reverse() Diff

Reverses the diff operation.

Returns:

A new Diff object with the reversed search and replace patterns.

Return type:

Diff

classmethod from_anchors(start_anchor: str, end_anchor: str, replace: str) Diff

Create a line-range Diff from hashline anchors (LINE:HASH format).

Parameters:
  • start_anchor (str) – The hashline anchor for range start (e.g., “42:ab123def”).

  • end_anchor (str) – The hashline anchor for range end (e.g., “45:cd456789”).

  • replace (str) – The replacement text for the lines range.

Returns:

A new Diff object configured for anchor-based line replacement.

Return type:

Diff

classmethod from_line_range(start: int, end: int, replace: str) Diff

Create a line-range Diff from line numbers (1-indexed inclusive).

Parameters:
  • start (int) – The 1-indexed line number for range start.

  • end (int) – The 1-indexed line number for range end.

  • replace (str) – The replacement text for the lines range.

Returns:

A new Diff object configured for line-number-based line replacement.

Return type:

Diff

format_with_hashes(content: str) str

Formats content with LINE:HASH anchors for LLM context.

Parameters:

content (str) – The text content to format.

Returns:

A string where each line is prefixed with its line number and hash.

Return type:

str

display() str

Returns a string representation of the Diff object.

Returns:

A string representation of the Diff object.

Return type:

str