Search documentation of Ragex

# Ragex.Editor.Diff (Ragex v0.4.0)

View Source

Diff generation and formatting for refactoring operations.

Provides multiple diff formats for previewing code changes:

- Unified diff (Git-style)
- Side-by-side diff
- JSON structured diff
- HTML diff (for web UIs)

Uses Elixir's built-in Myers algorithm via `List.myers_difference/2`.

## Summary

### Types

diff_chunk()
diff_format()
diff_line()
diff_result()

### Functions

apply_diff(original_content, diff_result)

Applies a diff to content (reverse operation of generate_diff).

compare_files(file1, file2, opts \\ [])

Compares two files and returns a diff.

format_diff(diff_result, format \\ :unified, opts \\ [])

Formats a diff result in the specified format.

generate(old_content, new_content, file_path, opts \\ [])

Combined generate and format function for test compatibility.

generate_diff(old_content, new_content, opts \\ [])

Generates a diff between original and modified content.

## Types

# diff_chunk()

    @type diff_chunk() :: %{
      old_start: pos_integer(),
      old_count: non_neg_integer(),
      new_start: pos_integer(),
      new_count: non_neg_integer(),
      lines: [diff_line()]
    }

# diff_format()

    @type diff_format() :: :unified | :side_by_side | :json | :html

# diff_line()

    @type diff_line() :: {:eq, String.t()} | {:del, String.t()} | {:ins, String.t()}

# diff_result()

    @type diff_result() :: %{
      old_file: String.t(),
      new_file: String.t(),
      chunks: [diff_chunk()],
      stats: %{
        additions: non_neg_integer(),
        deletions: non_neg_integer(),
        changes: non_neg_integer()
      }
    }

## Functions

# apply_diff(original_content, diff_result)

    @spec apply_diff(String.t(), diff_result()) :: {:ok, String.t()} | {:error, term()}

Applies a diff to content (reverse operation of generate_diff).

## Parameters

- `original_content`: Original content
- `diff_result`: Diff to apply

## Returns

- `{:ok, new_content}` on success
- `{:error, reason}` on failure

# compare_files(file1, file2, opts \\ [])

Compares two files and returns a diff.

# format_diff(diff_result, format \\ :unified, opts \\ [])

    @spec format_diff(diff_result(), diff_format(), keyword()) ::
      {:ok, String.t()} | {:error, term()}

Formats a diff result in the specified format.

## Parameters

- `diff_result`: Result from `generate_diff/3`
- `format`: One of `:unified`, `:side_by_side`, `:json`, `:html`
- `opts`: Format-specific options

## Returns

- `{:ok, formatted_string}` on success
- `{:error, reason}` on failure

# generate(old_content, new_content, file_path, opts \\ [])

Combined generate and format function for test compatibility.

Generates a diff and returns it in a format suitable for testing.

# generate_diff(old_content, new_content, opts \\ [])

    @spec generate_diff(String.t(), String.t(), keyword()) ::
      {:ok, diff_result()} | {:error, term()}

Generates a diff between original and modified content.

## Parameters

- `old_content`: Original content as string
- `new_content`: Modified content as string
- `opts`: Options
  - `:context_lines` - Number of context lines (default: 3)
  - `:old_file` - Label for old file (default: "original")
  - `:new_file` - Label for new file (default: "modified")

## Returns

- `{:ok, diff_result}` on success
- `{:error, reason}` on failure

## Examples

    iex> Diff.generate_diff("line1\nline2\n", "line1\nmodified\n")
    {:ok, %{chunks: [%{old_start: 1, ...}], stats: %{...}}}