Ragex.Editor.Diff
(Ragex v0.11.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
Functions
Applies a diff to content (reverse operation of generate_diff).
Compares two files and returns a diff.
Formats a diff result in the specified format.
Combined generate and format function for test compatibility.
Generates a diff between original and modified content.
Types
@type diff_chunk() :: %{ old_start: pos_integer(), old_count: non_neg_integer(), new_start: pos_integer(), new_count: non_neg_integer(), lines: [diff_line()] }
@type diff_format() :: :unified | :side_by_side | :json | :html
@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
@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 contentdiff_result: Diff to apply
Returns
{:ok, new_content}on success{:error, reason}on failure
Compares two files and returns a diff.
@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 fromgenerate_diff/3format: One of:unified,:side_by_side,:json,:htmlopts: Format-specific options
Returns
{:ok, formatted_string}on success{:error, reason}on failure
Combined generate and format function for test compatibility.
Generates a diff and returns it in a format suitable for testing.
@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 stringnew_content: Modified content as stringopts: 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: %{...}}}