gitlab-org--gitlab-foss/lib/gitlab/diff/highlight.rb

66 lines
1.8 KiB
Ruby
Raw Normal View History

module Gitlab
module Diff
class Highlight
attr_reader :diff_file
delegate :repository, :old_path, :new_path, :old_ref, :new_ref,
to: :diff_file, prefix: :diff
# Apply syntax highlight to provided source code
#
# diff_file - an instance of Gitlab::Diff::File
#
# Returns an Array with the processed items.
def self.process_diff_lines(diff_file)
processor = new(diff_file)
processor.highlight
end
def self.process_file(repository, ref, file_name)
blob = repository.blob_at(ref, file_name)
return [] unless blob
Gitlab::Highlight.highlight(file_name, blob.data).lines.map!(&:html_safe)
end
def initialize(diff_file)
@diff_file = diff_file
@file_name = diff_file.new_path
@lines = diff_file.diff_lines
end
def highlight
return [] if @lines.empty?
2016-01-12 22:36:08 +00:00
@lines.each_with_index do |line, i|
2016-01-14 15:13:35 +00:00
line_prefix = line.text.match(/\A([+-])/) ? $1 : ' '
2015-12-31 02:44:12 +00:00
# ignore highlighting for "match" lines
2016-01-12 22:36:08 +00:00
next if line.type == 'match'
2015-12-31 02:44:12 +00:00
2016-01-12 22:36:08 +00:00
case line.type
when 'new', nil
2016-01-12 22:36:08 +00:00
highlighted_line = new_lines[line.new_pos - 1]
when 'old'
2016-01-12 22:36:08 +00:00
highlighted_line = old_lines[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.
2016-01-14 15:13:35 +00:00
line.text = highlighted_line.insert(0, line_prefix).html_safe if highlighted_line
end
@lines
end
def old_lines
2016-01-14 15:13:35 +00:00
@old_lines ||= self.class.process_file(diff_repository, diff_old_ref, diff_old_path)
end
def new_lines
2016-01-14 15:13:35 +00:00
@new_lines ||= self.class.process_file(diff_repository, diff_new_ref, diff_new_path)
end
end
end
end