2015-12-30 05:52:50 +00:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Highlight
|
2016-01-08 03:37:01 +00:00
|
|
|
attr_reader :diff_file
|
|
|
|
|
2016-01-14 21:38:37 +00:00
|
|
|
delegate :old_path, :new_path, :old_ref, :new_ref, to: :diff_file, prefix: :diff
|
2016-01-08 03:37:01 +00:00
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
def initialize(diff_file)
|
|
|
|
@diff_file = diff_file
|
|
|
|
@diff_lines = diff_file.diff_lines
|
|
|
|
@raw_lines = @diff_lines.map(&:text)
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
def highlight
|
|
|
|
return [] if @diff_lines.empty?
|
2016-01-08 03:37:01 +00:00
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
find_inline_diffs
|
2016-01-08 03:37:01 +00:00
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
process_lines
|
|
|
|
|
|
|
|
@diff_lines
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
private
|
2015-12-31 06:05:52 +00:00
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
def find_inline_diffs
|
2016-01-15 13:25:29 +00:00
|
|
|
@inline_diffs = InlineDiff.new(@raw_lines).inline_diffs
|
2016-01-14 21:28:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_lines
|
|
|
|
@diff_lines.each_with_index do |diff_line, i|
|
2015-12-31 02:44:12 +00:00
|
|
|
# ignore highlighting for "match" lines
|
2016-01-14 21:28:07 +00:00
|
|
|
next if diff_line.type == 'match'
|
|
|
|
|
|
|
|
rich_line = highlight_line(diff_line, i)
|
|
|
|
rich_line = mark_inline_diffs(rich_line, diff_line, i)
|
|
|
|
diff_line.text = rich_line.html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_line(diff_line, index)
|
2016-01-15 13:25:29 +00:00
|
|
|
line_prefix = diff_line.text.match(/\A([+-])/) ? $1 : ' '
|
2016-01-14 21:28:07 +00:00
|
|
|
|
|
|
|
case diff_line.type
|
|
|
|
when 'new', nil
|
|
|
|
rich_line = new_lines[diff_line.new_pos - 1]
|
|
|
|
when 'old'
|
|
|
|
rich_line = old_lines[diff_line.old_pos - 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only update text if line is found. This will prevent
|
|
|
|
# issues with submodules given the line only exists in diff content.
|
|
|
|
rich_line ? line_prefix + rich_line : diff_line.text
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_inline_diffs(rich_line, diff_line, index)
|
2016-01-15 13:25:29 +00:00
|
|
|
line_inline_diffs = @inline_diffs[index]
|
|
|
|
return rich_line unless line_inline_diffs
|
2016-01-14 21:28:07 +00:00
|
|
|
|
|
|
|
raw_line = diff_line.text
|
|
|
|
position_mapping = map_character_positions(raw_line, rich_line)
|
|
|
|
|
|
|
|
offset = 0
|
2016-01-15 13:25:29 +00:00
|
|
|
line_inline_diffs.each do |inline_diff_range|
|
|
|
|
inline_diff_positions = position_mapping[inline_diff_range]
|
|
|
|
marker_ranges = collapse_ranges(inline_diff_positions)
|
2016-01-14 21:28:07 +00:00
|
|
|
|
2016-01-15 13:25:29 +00:00
|
|
|
marker_ranges.each do |range|
|
|
|
|
offset = insert_around_range(rich_line, range, "<span class='idiff'>", "</span>", offset)
|
2016-01-08 03:37:01 +00:00
|
|
|
end
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
2016-01-15 13:25:29 +00:00
|
|
|
|
|
|
|
rich_line
|
2016-01-14 21:28:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def map_character_positions(raw_line, rich_line)
|
|
|
|
mapping = []
|
|
|
|
raw_pos = 0
|
|
|
|
rich_pos = 0
|
|
|
|
(0..raw_line.length).each do |raw_pos|
|
|
|
|
raw_char = raw_line[raw_pos]
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
|
|
|
|
while rich_char == '<'
|
|
|
|
until rich_char == '>'
|
|
|
|
rich_pos += 1
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
end
|
|
|
|
|
|
|
|
rich_pos += 1
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
end
|
2015-12-30 05:52:50 +00:00
|
|
|
|
2016-01-14 21:28:07 +00:00
|
|
|
mapping[raw_pos] = rich_pos
|
|
|
|
|
|
|
|
rich_pos += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
mapping
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
|
2016-01-08 03:37:01 +00:00
|
|
|
def old_lines
|
2016-01-15 13:11:36 +00:00
|
|
|
@old_lines ||= Gitlab::Highlight.highlight_lines(*processing_args(:old))
|
2016-01-08 03:37:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_lines
|
2016-01-15 13:11:36 +00:00
|
|
|
@new_lines ||= Gitlab::Highlight.highlight_lines(*processing_args(:new))
|
2016-01-14 21:28:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def collapse_ranges(positions)
|
|
|
|
return [] if positions.empty?
|
|
|
|
ranges = []
|
|
|
|
|
|
|
|
start = prev = positions[0]
|
|
|
|
range = start..prev
|
|
|
|
positions[1..-1].each do |pos|
|
|
|
|
if pos == prev + 1
|
|
|
|
range = start..pos
|
|
|
|
prev = pos
|
|
|
|
else
|
|
|
|
ranges << range
|
|
|
|
start = prev = pos
|
|
|
|
range = start..prev
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ranges << range
|
|
|
|
|
|
|
|
ranges
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert_around_range(text, range, before, after, offset = 0)
|
|
|
|
from = range.begin
|
|
|
|
to = range.end
|
|
|
|
|
|
|
|
text.insert(offset + from, before)
|
|
|
|
offset += before.length
|
|
|
|
|
|
|
|
text.insert(offset + to + 1, after)
|
|
|
|
offset += after.length
|
|
|
|
|
|
|
|
offset
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
2016-01-14 21:38:37 +00:00
|
|
|
|
|
|
|
def processing_args(version)
|
|
|
|
ref = send("diff_#{version}_ref")
|
|
|
|
path = send("diff_#{version}_path")
|
|
|
|
|
|
|
|
[ref.project.repository, ref.id, path]
|
|
|
|
end
|
2015-12-30 05:52:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|