2016-01-29 22:53:50 +05:30
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Highlight
|
2016-08-24 12:49:21 +05:30
|
|
|
attr_reader :diff_file, :diff_lines, :raw_lines, :repository
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
delegate :old_path, :new_path, :old_sha, :new_sha, to: :diff_file, prefix: :diff
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def initialize(diff_lines, repository: nil)
|
|
|
|
@repository = repository
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
if diff_lines.is_a?(Gitlab::Diff::File)
|
|
|
|
@diff_file = diff_lines
|
|
|
|
@diff_lines = @diff_file.diff_lines
|
|
|
|
else
|
|
|
|
@diff_lines = diff_lines
|
|
|
|
end
|
|
|
|
@raw_lines = @diff_lines.map(&:text)
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlight
|
|
|
|
@diff_lines.map.with_index do |diff_line, i|
|
|
|
|
diff_line = diff_line.dup
|
|
|
|
# ignore highlighting for "match" lines
|
2016-08-24 12:49:21 +05:30
|
|
|
next diff_line if diff_line.meta?
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
rich_line = highlight_line(diff_line) || diff_line.text
|
2016-01-29 22:53:50 +05:30
|
|
|
|
|
|
|
if line_inline_diffs = inline_diffs[i]
|
|
|
|
rich_line = InlineDiffMarker.new(diff_line.text, rich_line).mark(line_inline_diffs)
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
diff_line.text = rich_line
|
2016-01-29 22:53:50 +05:30
|
|
|
|
|
|
|
diff_line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def highlight_line(diff_line)
|
|
|
|
return unless diff_file && diff_file.diff_refs
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
rich_line =
|
|
|
|
if diff_line.unchanged? || diff_line.added?
|
2017-09-10 17:25:29 +05:30
|
|
|
new_lines[diff_line.new_pos - 1]&.html_safe
|
2016-08-24 12:49:21 +05:30
|
|
|
elsif diff_line.removed?
|
2017-09-10 17:25:29 +05:30
|
|
|
old_lines[diff_line.old_pos - 1]&.html_safe
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-01-29 22:53:50 +05:30
|
|
|
|
|
|
|
# Only update text if line is found. This will prevent
|
|
|
|
# issues with submodules given the line only exists in diff content.
|
2016-09-13 17:45:13 +05:30
|
|
|
if rich_line
|
2017-08-17 22:00:37 +05:30
|
|
|
line_prefix = diff_line.text =~ /\A(.)/ ? $1 : ' '
|
2016-09-13 17:45:13 +05:30
|
|
|
"#{line_prefix}#{rich_line}".html_safe
|
|
|
|
end
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def inline_diffs
|
2016-04-02 18:10:28 +05:30
|
|
|
@inline_diffs ||= InlineDiff.for_lines(@raw_lines)
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def old_lines
|
2017-09-10 17:25:29 +05:30
|
|
|
@old_lines ||= highlighted_blob_lines(diff_file.old_blob)
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def new_lines
|
2017-09-10 17:25:29 +05:30
|
|
|
@new_lines ||= highlighted_blob_lines(diff_file.new_blob)
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_blob_lines(blob)
|
|
|
|
return [] unless blob
|
|
|
|
|
|
|
|
blob.load_all_data!
|
|
|
|
Gitlab::Highlight.highlight(blob.path, blob.data, repository: repository).lines
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|