Module: Textus::Protocol::Diff

Defined in:
lib/textus/protocol/diff.rb

Class Method Summary collapse

Class Method Details

.backtrack(trace, x_pos, y_pos) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/textus/protocol/diff.rb', line 67

def backtrack(trace, x_pos, y_pos)
  script = []
  d = trace.size - 1
  return script if d.negative?

  k = x_pos - y_pos
  while d.positive?
    prev_v = trace[d - 1]
    prev_k = if k == -d || (k != d && (prev_v[k - 1] || -1) < (prev_v[k + 1] || -1))
               k + 1
             else
               k - 1
             end
    prev_x = prev_v[prev_k] || 0
    prev_y = prev_x - prev_k

    while x_pos > prev_x && y_pos > prev_y
      script.unshift([:eq, x_pos - 1, y_pos - 1])
      x_pos -= 1
      y_pos -= 1
    end

    if x_pos > prev_x
      script.unshift([:del, x_pos - 1, nil])
      x_pos -= 1
    elsif y_pos > prev_y
      script.unshift([:ins, nil, y_pos - 1])
      y_pos -= 1
    end

    d -= 1
    k = prev_k
    x_pos = prev_x
    y_pos = prev_y
  end

  script
end

.body(a_text, b_text) ⇒ Object



6
7
8
9
10
11
# File 'lib/textus/protocol/diff.rb', line 6

def body(a_text, b_text)
  a_lines = (a_text || "").lines.map(&:chomp)
  b_lines = (b_text || "").lines.map(&:chomp)
  hunks = build_hunks(myers_diff(a_lines, b_lines), a_lines, b_lines)
  { "type" => "body", "hunks" => hunks, "stats" => stats(hunks) }
end

.build_hunks(script, a_lines, b_lines) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/textus/protocol/diff.rb', line 106

def build_hunks(script, a_lines, b_lines)
  return [] if script.empty?

  hunks = []
  i = 0
  while i < script.size
    next_context = find_next_change(script, i)
    break unless next_context

    start = [next_context - 3, 0].max
    hunk_end_script = find_hunk_end(script, next_context)
    hunk_end = [hunk_end_script + 3, script.size - 1].min

    a_start = nil
    b_start = nil
    a_lines_hunk = []
    b_lines_hunk = []

    (start..hunk_end).each do |j|
      op, ai, bi = script[j]
      a_start ||= ai if ai
      b_start ||= bi if bi

      case op
      when :eq
        a_lines_hunk << { "type" => "context", "line" => a_lines[ai] }
        b_lines_hunk << { "type" => "context", "line" => b_lines[bi] }
      when :del
        a_lines_hunk << { "type" => "deletion", "line" => a_lines[ai] }
      when :ins
        b_lines_hunk << { "type" => "addition", "line" => b_lines[bi] }
      end
    end

    hunks << {
      "a_start" => a_start, "b_start" => b_start,
      "a_lines" => a_lines_hunk, "b_lines" => b_lines_hunk
    }

    i = hunk_end + 1
  end

  hunks
end

.find_hunk_end(script, from) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/textus/protocol/diff.rb', line 155

def find_hunk_end(script, from)
  last_change = from
  (from...[script.size, from + 6].min).each do |i|
    last_change = i if script[i][0] != :eq
  end
  last_change
end

.find_next_change(script, from) ⇒ Object



151
152
153
# File 'lib/textus/protocol/diff.rb', line 151

def find_next_change(script, from)
  (from...script.size).find { |i| script[i][0] != :eq }
end

.meta(a_hash, b_hash) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/textus/protocol/diff.rb', line 13

def meta(a_hash, b_hash)
  a = a_hash || {}
  b = b_hash || {}
  all_keys = (a.keys + b.keys).uniq.sort
  changes = all_keys.filter_map do |k|
    av = a[k]
    bv = b[k]
    next if av == bv

    { "key" => k.to_s, "old" => av, "new" => bv }
  end
  { "type" => "meta", "changes" => changes, "stats" => { "changed" => changes.size } }
end

.myers_diff(a_lines, b_lines) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/textus/protocol/diff.rb', line 39

def myers_diff(a_lines, b_lines)
  a_count = a_lines.size
  b_count = b_lines.size
  max_d = a_count + b_count
  v = Hash.new(0)
  v[1] = 0
  trace = []

  (0..max_d).each do |d|
    (-d..d).step(2).each do |k|
      x = if k == -d || (k != d && v[k - 1] < v[k + 1])
            v[k + 1]
          else
            v[k - 1] + 1
          end
      y = x - k
      while x < a_count && y < b_count && a_lines[x] == b_lines[y]
        x += 1
        y += 1
      end
      v[k] = x
      return backtrack(trace, x, y) if x >= a_count && y >= b_count
    end
    trace << v.dup
  end
  []
end

.schema(a_schema, b_schema) ⇒ Object



27
28
29
# File 'lib/textus/protocol/diff.rb', line 27

def schema(a_schema, b_schema)
  meta(a_schema, b_schema).merge("type" => "schema")
end

.stats(hunks) ⇒ Object



163
164
165
166
167
# File 'lib/textus/protocol/diff.rb', line 163

def stats(hunks)
  additions = hunks.sum { |h| h["b_lines"].count { |l| l["type"] == "addition" } }
  deletions = hunks.sum { |h| h["a_lines"].count { |l| l["type"] == "deletion" } }
  { "additions" => additions, "deletions" => deletions, "hunks" => hunks.size }
end

.summary(diff_result) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/textus/protocol/diff.rb', line 31

def summary(diff_result)
  parts = []
  parts << "#{diff_result.dig("body", "stats", "additions") || 0}+" if diff_result["body"]
  parts << "#{diff_result.dig("body", "stats", "deletions") || 0}-" if diff_result["body"]
  parts << "#{diff_result.dig("meta", "stats", "changed") || 0}~" if diff_result["meta"]
  parts.join(" ")
end