Raxol.UI.Components.Harness.LineDiff (Raxol v2.6.1)

View Source

Line-based diff between two blocks of text.

Computes the longest common subsequence (LCS) of lines via dynamic programming, then backtracks it into an ordered list of :equal, :delete, and :insert operations -- the shape a unified diff walks over. This is a plain LCS diff (not Myers): O(lines_old * lines_new) time and space, which is the right tradeoff for the file-edit-sized diffs a pre-apply confirmation UI renders (correctness over cleverness), not for diffing huge files.

Used by Raxol.UI.Components.Harness.DiffViewer.

Summary

Functions

Diffs two texts line-by-line, returning ops in old-then-new document order.

Types

op()

@type op() :: {:equal, String.t()} | {:delete, String.t()} | {:insert, String.t()}

Functions

diff(old_text, new_text)

@spec diff(String.t(), String.t()) :: [op()]

Diffs two texts line-by-line, returning ops in old-then-new document order.

Splits on "\n"; a trailing newline on both sides produces a matching trailing empty-line pair (shown as context), not a spurious change. An empty string is treated as zero lines (not one blank line), so diffing against "" reads as a whole-file add/remove.

Examples

iex> Raxol.UI.Components.Harness.LineDiff.diff("a\nb\nc", "a\nx\nc")
[equal: "a", delete: "b", insert: "x", equal: "c"]

iex> Raxol.UI.Components.Harness.LineDiff.diff("", "a\nb")
[insert: "a", insert: "b"]