2012-07-02 16:34:25 -04:00
|
|
|
module Gitlab
|
|
|
|
class InlineDiff
|
|
|
|
class << self
|
|
|
|
|
|
|
|
START = "#!idiff-start!#"
|
|
|
|
FINISH = "#!idiff-finish!#"
|
2013-03-20 17:46:30 -04:00
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def processing(diff_arr)
|
2012-07-02 16:34:25 -04:00
|
|
|
indexes = _indexes_of_changed_lines diff_arr
|
|
|
|
|
|
|
|
indexes.each do |index|
|
|
|
|
first_line = diff_arr[index+1]
|
|
|
|
second_line = diff_arr[index+2]
|
|
|
|
max_length = [first_line.size, second_line.size].max
|
|
|
|
|
2013-09-04 11:15:42 -04:00
|
|
|
# Skip inline diff if empty line was replaced with content
|
|
|
|
next if first_line == "-\n"
|
|
|
|
|
2012-07-02 16:34:25 -04:00
|
|
|
first_the_same_symbols = 0
|
|
|
|
(0..max_length + 1).each do |i|
|
|
|
|
first_the_same_symbols = i - 1
|
|
|
|
if first_line[i] != second_line[i] && i > 0
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2013-09-04 11:15:42 -04:00
|
|
|
|
2012-07-02 16:34:25 -04:00
|
|
|
first_token = first_line[0..first_the_same_symbols][1..-1]
|
2013-06-12 09:43:58 -04:00
|
|
|
start = first_token + START
|
2013-09-04 11:15:42 -04:00
|
|
|
|
|
|
|
if first_token.empty?
|
|
|
|
# In case if we remove string of spaces in commit
|
|
|
|
diff_arr[index+1].sub!("-", "-" => "-#{START}")
|
|
|
|
diff_arr[index+2].sub!("+", "+" => "+#{START}")
|
|
|
|
else
|
|
|
|
diff_arr[index+1].sub!(first_token, first_token => start)
|
|
|
|
diff_arr[index+2].sub!(first_token, first_token => start)
|
|
|
|
end
|
|
|
|
|
2012-07-02 16:34:25 -04:00
|
|
|
last_the_same_symbols = 0
|
|
|
|
(1..max_length + 1).each do |i|
|
|
|
|
last_the_same_symbols = -i
|
2012-10-04 06:28:58 -04:00
|
|
|
shortest_line = second_line.size > first_line.size ? first_line : second_line
|
|
|
|
if ( first_line[-i] != second_line[-i] ) || "#{first_token}#{START}".size == shortest_line[1..-i].size
|
2012-07-02 16:34:25 -04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
last_the_same_symbols += 1
|
|
|
|
last_token = first_line[last_the_same_symbols..-1]
|
2012-07-04 17:57:12 -04:00
|
|
|
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
|
|
|
|
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
|
2012-07-02 16:34:25 -04:00
|
|
|
end
|
|
|
|
diff_arr
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def _indexes_of_changed_lines(diff_arr)
|
2012-07-02 16:34:25 -04:00
|
|
|
chain_of_first_symbols = ""
|
|
|
|
diff_arr.each_with_index do |line, i|
|
|
|
|
chain_of_first_symbols += line[0]
|
|
|
|
end
|
|
|
|
chain_of_first_symbols.gsub!(/[^\-\+]/, "#")
|
|
|
|
|
|
|
|
offset = 0
|
|
|
|
indexes = []
|
|
|
|
while index = chain_of_first_symbols.index("#-+#", offset)
|
|
|
|
indexes << index
|
|
|
|
offset = index + 1
|
|
|
|
end
|
|
|
|
indexes
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def replace_markers(line)
|
2012-07-02 16:34:25 -04:00
|
|
|
line.gsub!(START, "<span class='idiff'>")
|
|
|
|
line.gsub!(FINISH, "</span>")
|
|
|
|
line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|