Class: DamageControl::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/damagecontrol/diff_parser.rb

Overview

Represents a unified diff section in a diff file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minus, nminus, plus, nplus) ⇒ Diff

Create a new Diff. minus and plus represent the number of removed and added lines.



32
33
34
35
# File 'lib/damagecontrol/diff_parser.rb', line 32

def initialize(minus, nminus, plus, nplus)
  @minus, @nminus, @plus, @nplus = minus, nminus, plus, nplus
  @lines = []
end

Instance Attribute Details

#minusObject (readonly)

Returns the value of attribute minus.



28
29
30
# File 'lib/damagecontrol/diff_parser.rb', line 28

def minus
  @minus
end

#nminusObject (readonly)

Returns the value of attribute nminus.



28
29
30
# File 'lib/damagecontrol/diff_parser.rb', line 28

def nminus
  @nminus
end

#nplusObject (readonly)

Returns the value of attribute nplus.



28
29
30
# File 'lib/damagecontrol/diff_parser.rb', line 28

def nplus
  @nplus
end

#plusObject (readonly)

Returns the value of attribute plus.



28
29
30
# File 'lib/damagecontrol/diff_parser.rb', line 28

def plus
  @plus
end

#removed_rangeObject (readonly)

Returns the value of attribute removed_range.



28
29
30
# File 'lib/damagecontrol/diff_parser.rb', line 28

def removed_range
  @removed_range
end

Instance Method Details

#<<(line) ⇒ Object



37
38
39
# File 'lib/damagecontrol/diff_parser.rb', line 37

def <<(line)
  @lines << line
end

#[](n) ⇒ Object



45
46
47
# File 'lib/damagecontrol/diff_parser.rb', line 45

def[](n)
  @lines[n]
end

#accept(visitor) ⇒ Object



79
80
81
82
83
# File 'lib/damagecontrol/diff_parser.rb', line 79

def accept(visitor)
  @lines.each do |line|
    visitor.visitLine(line)
  end
end

#line_countObject



41
42
43
# File 'lib/damagecontrol/diff_parser.rb', line 41

def line_count
  @lines.length
end

#parseObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/damagecontrol/diff_parser.rb', line 49

def parse
  prev = nil
  @lines.each do |line|
    prefix_length = 1
    suffix_len = 0
    if(prev && prev.removed? && line.added?)
      a = line[1..-1]
      b = prev[1..-1]
      prefix_length = a.common_prefix_length(b)+1
      suffix_len = a.reverse.common_prefix_length(b.reverse)
      # prevent prefix/suffix having overlap,
      suffix_len = min(suffix_len, min(line.length,prev.length)-prefix_length)
      remove_infix_length = prev.length - (prefix_length+suffix_len)
      add_infix_length = line.length - (prefix_length+suffix_len)
      oversize_change = remove_infix_length*100/prev.length>33 || add_infix_length*100/line.length>33

      if prefix_length==1 && suffix_len==0 || remove_infix_length<=0 || oversize_change
      else
        prev.removed_range = (prefix_length..prefix_length+remove_infix_length-1)
      end

      if prefix_length==1 && suffix_len==0 || add_infix_length<=0 || oversize_change
      else
        line.added_range = (prefix_length..prefix_length+add_infix_length-1)
      end
    end
    prev = line
  end
end