Commit cecdb0b384d2f6943fab704497e67a744eb553f7

Authored by Valeriy Sizov
1 parent a0955892

InlineDiff: refactoring

app/helpers/commits_helper.rb
... ... @@ -36,8 +36,7 @@ module CommitsHelper
36 36 line_new = 1
37 37 type = nil
38 38  
39   - lines_arr = inline_diff diff_arr
40   - #lines_arr = diff_arr
  39 + lines_arr = ::Gitlab::InlineDiff.processing diff_arr
41 40 lines_arr.each do |line|
42 41 next if line.match(/^\-\-\- \/dev\/null/)
43 42 next if line.match(/^\+\+\+ \/dev\/null/)
... ... @@ -45,9 +44,7 @@ module CommitsHelper
45 44 next if line.match(/^\+\+\+ b/)
46 45  
47 46 full_line = html_escape(line.gsub(/\n/, ''))
48   -
49   - full_line.gsub!("", "<span class='idiff'>")
50   - full_line.gsub!("", "</span>")
  47 + full_line = ::Gitlab::InlineDiff.replace_markers full_line
51 48  
52 49 if line.match(/^@@ -/)
53 50 type = "match"
... ... @@ -86,50 +83,4 @@ module CommitsHelper
86 83 end
87 84 end
88 85  
89   - def inline_diff diff_arr
90   - chain_of_first_symbols = ""
91   - diff_arr.each_with_index do |line, i|
92   - chain_of_first_symbols += line[0]
93   - end
94   - chain_of_first_symbols.gsub!(/[^\-\+]/, "#")
95   -
96   - offset = 0
97   - indexes = []
98   - while index = chain_of_first_symbols.index("#-+#", offset)
99   - indexes << index
100   - offset = index + 1
101   - end
102   -
103   - indexes.each do |index|
104   - first_line = diff_arr[index+1]
105   - second_line = diff_arr[index+2]
106   - max_length = [first_line.size, second_line.size].max
107   -
108   - first_the_same_symbols = 0
109   - (0..max_length + 1).each do |i|
110   - first_the_same_symbols = i - 1
111   - if first_line[i] != second_line[i] && i > 0
112   - break
113   - end
114   - end
115   - first_token = first_line[0..first_the_same_symbols][1..-1]
116   -
117   - diff_arr[index+1].sub!(first_token, first_token + "")
118   - diff_arr[index+2].sub!(first_token, first_token + "")
119   -
120   - last_the_same_symbols = 0
121   - (1..max_length + 1).each do |i|
122   - last_the_same_symbols = -i
123   - if first_line[-i] != second_line[-i]
124   - break
125   - end
126   - end
127   - last_the_same_symbols += 1
128   - last_token = first_line[last_the_same_symbols..-1]
129   -
130   - diff_arr[index+1].sub!(/#{last_token}$/, "" + last_token)
131   - diff_arr[index+2].sub!(/#{last_token}$/, "" + last_token)
132   - end
133   - diff_arr
134   - end
135 86 end
... ...
lib/gitlab/inline_diff.rb 0 → 100644
... ... @@ -0,0 +1,69 @@
  1 +module Gitlab
  2 + class InlineDiff
  3 + class << self
  4 +
  5 + START = ""
  6 + FINISH = ""
  7 +
  8 + def processing diff_arr
  9 + indexes = _indexes_of_changed_lines diff_arr
  10 +
  11 + indexes.each do |index|
  12 + first_line = diff_arr[index+1]
  13 + second_line = diff_arr[index+2]
  14 + max_length = [first_line.size, second_line.size].max
  15 +
  16 + first_the_same_symbols = 0
  17 + (0..max_length + 1).each do |i|
  18 + first_the_same_symbols = i - 1
  19 + if first_line[i] != second_line[i] && i > 0
  20 + break
  21 + end
  22 + end
  23 + first_token = first_line[0..first_the_same_symbols][1..-1]
  24 +
  25 + diff_arr[index+1].sub!(first_token, first_token + START)
  26 + diff_arr[index+2].sub!(first_token, first_token + START)
  27 +
  28 + last_the_same_symbols = 0
  29 + (1..max_length + 1).each do |i|
  30 + last_the_same_symbols = -i
  31 + if first_line[-i] != second_line[-i]
  32 + break
  33 + end
  34 + end
  35 + last_the_same_symbols += 1
  36 + last_token = first_line[last_the_same_symbols..-1]
  37 +
  38 + diff_arr[index+1].sub!(/#{last_token}$/, FINISH + last_token)
  39 + diff_arr[index+2].sub!(/#{last_token}$/, FINISH + last_token)
  40 + end
  41 + diff_arr
  42 + end
  43 +
  44 + def _indexes_of_changed_lines diff_arr
  45 + chain_of_first_symbols = ""
  46 + diff_arr.each_with_index do |line, i|
  47 + chain_of_first_symbols += line[0]
  48 + end
  49 + chain_of_first_symbols.gsub!(/[^\-\+]/, "#")
  50 +
  51 + offset = 0
  52 + indexes = []
  53 + while index = chain_of_first_symbols.index("#-+#", offset)
  54 + indexes << index
  55 + offset = index + 1
  56 + end
  57 + indexes
  58 + end
  59 +
  60 + def replace_markers line
  61 + line.gsub!(START, "<span class='idiff'>")
  62 + line.gsub!(FINISH, "</span>")
  63 + line
  64 + end
  65 +
  66 + end
  67 +
  68 + end
  69 +end
... ...