|
| 1 | +require "diff/lcs" |
| 2 | + |
| 3 | +module Coplan |
| 4 | + module Plans |
| 5 | + # Character-offset replacements between two document versions. Skip common |
| 6 | + # edges, use LCS for small changes, and use a bounded Myers shortest-edit |
| 7 | + # search for larger changes. Never infer character positions from lines: |
| 8 | + # matching the wrong repeated line can misplace comments or concurrent edits. |
| 9 | + # If the search exhausts its budget, replace the entire changed middle. |
| 10 | + module TextDiff |
| 11 | + MAX_CHARACTER_CELLS = 1_000_000 |
| 12 | + |
| 13 | + # One budget for the whole search, counting frontier states and character |
| 14 | + # comparisons. Stored frontier entries also consume this budget, bounding |
| 15 | + # the backtracking trace as well as CPU work. |
| 16 | + MAX_SEARCH_STEPS = 1_000_000 |
| 17 | + |
| 18 | + module_function |
| 19 | + |
| 20 | + # [{ from:, to:, text: }]: replace before[from...to] with text. |
| 21 | + def hunks(before, after) |
| 22 | + return [] if before == after |
| 23 | + |
| 24 | + prefix = common_prefix_length(before, after) |
| 25 | + suffix = common_suffix_length(before, after, prefix) |
| 26 | + old_middle = before[prefix...(before.length - suffix)] |
| 27 | + new_middle = after[prefix...(after.length - suffix)] |
| 28 | + replacement = [ { from: prefix, to: prefix + old_middle.length, text: new_middle } ] |
| 29 | + return replacement if old_middle.empty? || new_middle.empty? |
| 30 | + return character_hunks(old_middle, new_middle, prefix) if small?(old_middle, new_middle) |
| 31 | + |
| 32 | + bounded_character_hunks(old_middle, new_middle, prefix) || replacement |
| 33 | + end |
| 34 | + |
| 35 | + # Myers' frontier records the furthest old-text position on each diagonal |
| 36 | + # after d insertions/deletions. An array index i represents diagonal 2*i-d. |
| 37 | + # Equal characters advance along a diagonal without another edit. A found |
| 38 | + # path is shortest; an exhausted search returns no partial alignment. |
| 39 | + def bounded_character_hunks(before, after, offset) |
| 40 | + return if [ before.length, after.length ].max > MAX_SEARCH_STEPS |
| 41 | + |
| 42 | + old = before.codepoints |
| 43 | + fresh = after.codepoints |
| 44 | + frontier = [ 0 ] |
| 45 | + trace = [] |
| 46 | + steps = 0 |
| 47 | + (0..old.length + fresh.length).each do |distance| |
| 48 | + next_frontier = [] |
| 49 | + (0..distance).each do |index| |
| 50 | + steps += 1 |
| 51 | + return if steps > MAX_SEARCH_STEPS |
| 52 | + |
| 53 | + insertion = index.zero? || (index < distance && frontier[index - 1] < frontier[index]) |
| 54 | + x = insertion ? frontier[index] : frontier[index - 1] + 1 |
| 55 | + y = x - (2 * index - distance) |
| 56 | + while x < old.length && y < fresh.length |
| 57 | + steps += 1 |
| 58 | + return if steps > MAX_SEARCH_STEPS |
| 59 | + break unless old[x] == fresh[y] |
| 60 | + x += 1 |
| 61 | + y += 1 |
| 62 | + end |
| 63 | + next_frontier << x |
| 64 | + if x >= old.length && y >= fresh.length |
| 65 | + return backtrack(trace, old.length, fresh.length, fresh, after.encoding, offset) |
| 66 | + end |
| 67 | + end |
| 68 | + trace << next_frontier |
| 69 | + frontier = next_frontier |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def backtrack(trace, x, y, fresh, encoding, offset) |
| 74 | + edits = [] |
| 75 | + trace.length.downto(1) do |distance| |
| 76 | + frontier = trace[distance - 1] |
| 77 | + diagonal = x - y |
| 78 | + index = (diagonal + distance) / 2 |
| 79 | + insertion = index.zero? || (index < distance && frontier[index - 1] < frontier[index]) |
| 80 | + previous_x = insertion ? frontier[index] : frontier[index - 1] |
| 81 | + previous_y = previous_x - (insertion ? diagonal + 1 : diagonal - 1) |
| 82 | + edits << { from: offset + previous_x, to: offset + previous_x + (insertion ? 0 : 1), |
| 83 | + text: insertion ? fresh[previous_y].chr(encoding) : +"" } |
| 84 | + x, y = previous_x, previous_y |
| 85 | + end |
| 86 | + |
| 87 | + edits.reverse_each.with_object([]) do |edit, hunks| |
| 88 | + if hunks.last && hunks.last[:to] == edit[:from] |
| 89 | + hunks.last[:to] = edit[:to] |
| 90 | + hunks.last[:text] << edit[:text] |
| 91 | + else |
| 92 | + hunks << edit |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + # [[old_from, old_to, new_from, new_to]]: runs of changed whole lines, |
| 98 | + # as character offsets into each side. A pure insertion or deletion is |
| 99 | + # an empty range at the line where it happens. |
| 100 | + def line_hunks(before, after) |
| 101 | + old_lines = before.lines |
| 102 | + new_lines = after.lines |
| 103 | + old_offsets = line_offsets(old_lines) |
| 104 | + new_offsets = line_offsets(new_lines) |
| 105 | + groups = [] |
| 106 | + current = nil |
| 107 | + Diff::LCS.sdiff(old_lines, new_lines).each do |change| |
| 108 | + if change.action == "=" |
| 109 | + groups << current if current |
| 110 | + current = nil |
| 111 | + next |
| 112 | + end |
| 113 | + current ||= { old: [], new: [], old_anchor: change.old_position, new_anchor: change.new_position } |
| 114 | + current[:old] << change.old_position if %w[- !].include?(change.action) |
| 115 | + current[:new] << change.new_position if %w[+ !].include?(change.action) |
| 116 | + end |
| 117 | + groups << current if current |
| 118 | + groups.map do |group| |
| 119 | + [ *offset_range(group[:old], group[:old_anchor], old_offsets), |
| 120 | + *offset_range(group[:new], group[:new_anchor], new_offsets) ] |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + def character_hunks(before, after, offset) |
| 125 | + result = [] |
| 126 | + position = offset |
| 127 | + pending = nil |
| 128 | + Diff::LCS.sdiff(before.each_char.to_a, after.each_char.to_a).each do |change| |
| 129 | + if change.action == "=" |
| 130 | + result << pending if pending |
| 131 | + pending = nil |
| 132 | + position += 1 |
| 133 | + else |
| 134 | + pending ||= { from: position, to: position, text: +"" } |
| 135 | + unless change.action == "+" |
| 136 | + position += 1 |
| 137 | + pending[:to] = position |
| 138 | + end |
| 139 | + pending[:text] << change.new_element unless change.action == "-" |
| 140 | + end |
| 141 | + end |
| 142 | + result << pending if pending |
| 143 | + result |
| 144 | + end |
| 145 | + |
| 146 | + def small?(before, after) |
| 147 | + before.length * after.length <= MAX_CHARACTER_CELLS |
| 148 | + end |
| 149 | + |
| 150 | + def common_prefix_length(before, after, limit = [ before.length, after.length ].min) |
| 151 | + # String#[] at successive character indexes rescans UTF-8 from the |
| 152 | + # start. Walk characters once so non-ASCII documents stay linear too. |
| 153 | + characters = after.each_char |
| 154 | + length = 0 |
| 155 | + before.each_char do |character| |
| 156 | + break if length == limit || character != characters.next |
| 157 | + length += 1 |
| 158 | + end |
| 159 | + length |
| 160 | + end |
| 161 | + |
| 162 | + def common_suffix_length(before, after, prefix) |
| 163 | + limit = [ before.length, after.length ].min - prefix |
| 164 | + common_prefix_length(before.reverse, after.reverse, limit) |
| 165 | + end |
| 166 | + |
| 167 | + def line_offsets(lines) |
| 168 | + lines.each_with_object([ 0 ]) { |line, offsets| offsets << offsets.last + line.length } |
| 169 | + end |
| 170 | + |
| 171 | + def offset_range(indexes, anchor, offsets) |
| 172 | + return [ offsets[anchor], offsets[anchor] ] if indexes.empty? |
| 173 | + |
| 174 | + [ offsets[indexes.min], offsets[indexes.max + 1] ] |
| 175 | + end |
| 176 | + end |
| 177 | + end |
| 178 | +end |
0 commit comments