2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-08 09:25:50 -04:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Line
|
2019-12-04 07:07:12 -05:00
|
|
|
# When SERIALIZE_KEYS is updated, to reset the redis cache entries you'll
|
|
|
|
# need to bump the VERSION constant on Gitlab::Diff::HighlightCache
|
|
|
|
#
|
2018-08-28 09:39:17 -04:00
|
|
|
SERIALIZE_KEYS = %i(line_code rich_text text type index old_pos new_pos).freeze
|
2018-07-04 05:34:35 -04:00
|
|
|
|
2018-10-16 12:21:16 -04:00
|
|
|
attr_reader :line_code, :type, :old_pos, :new_pos
|
2016-07-29 08:29:14 -04:00
|
|
|
attr_writer :rich_text
|
2018-10-16 12:21:16 -04:00
|
|
|
attr_accessor :text, :index
|
2014-09-08 09:25:50 -04:00
|
|
|
|
2018-08-28 09:39:17 -04:00
|
|
|
def initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: nil)
|
2014-09-08 14:54:52 -04:00
|
|
|
@text, @type, @index = text, type, index
|
2014-09-08 09:25:50 -04:00
|
|
|
@old_pos, @new_pos = old_pos, new_pos
|
2016-08-02 04:20:22 -04:00
|
|
|
@parent_file = parent_file
|
2018-08-28 09:39:17 -04:00
|
|
|
@rich_text = rich_text
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
# When line code is not provided from cache store we build it
|
|
|
|
# using the parent_file(Diff::File or Conflict::File).
|
|
|
|
@line_code = line_code || calculate_line_code
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
2015-10-01 07:52:08 -04:00
|
|
|
|
2016-07-20 12:25:36 -04:00
|
|
|
def self.init_from_hash(hash)
|
2018-10-16 12:21:16 -04:00
|
|
|
new(hash[:text],
|
|
|
|
hash[:type],
|
|
|
|
hash[:index],
|
|
|
|
hash[:old_pos],
|
|
|
|
hash[:new_pos],
|
|
|
|
parent_file: hash[:parent_file],
|
|
|
|
line_code: hash[:line_code],
|
|
|
|
rich_text: hash[:rich_text])
|
2016-07-20 12:25:36 -04:00
|
|
|
end
|
|
|
|
|
2019-12-11 10:07:38 -05:00
|
|
|
def self.safe_init_from_hash(hash)
|
|
|
|
line = hash.with_indifferent_access
|
|
|
|
rich_text = line[:rich_text]
|
|
|
|
line[:rich_text] = rich_text&.html_safe
|
|
|
|
|
|
|
|
init_from_hash(line)
|
|
|
|
end
|
|
|
|
|
2016-07-20 12:25:36 -04:00
|
|
|
def to_hash
|
|
|
|
hash = {}
|
2018-07-04 05:34:35 -04:00
|
|
|
SERIALIZE_KEYS.each { |key| hash[key] = send(key) } # rubocop:disable GitlabSecurity/PublicSend
|
2016-07-20 12:25:36 -04:00
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
def old_line
|
|
|
|
old_pos unless added? || meta?
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_line
|
|
|
|
new_pos unless removed? || meta?
|
|
|
|
end
|
|
|
|
|
2017-03-15 20:14:58 -04:00
|
|
|
def line
|
|
|
|
new_line || old_line
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
def unchanged?
|
|
|
|
type.nil?
|
|
|
|
end
|
|
|
|
|
2015-10-01 07:52:08 -04:00
|
|
|
def added?
|
2017-06-19 12:25:21 -04:00
|
|
|
%w[new new-nonewline].include?(type)
|
2015-10-01 07:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def removed?
|
2017-06-19 12:25:21 -04:00
|
|
|
%w[old old-nonewline].include?(type)
|
2016-07-29 08:29:14 -04:00
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
def meta?
|
2017-06-19 12:25:21 -04:00
|
|
|
%w[match new-nonewline old-nonewline].include?(type)
|
2016-06-20 13:15:44 -04:00
|
|
|
end
|
2016-07-25 14:47:09 -04:00
|
|
|
|
2018-06-04 18:20:58 -04:00
|
|
|
def match?
|
|
|
|
type == :match
|
|
|
|
end
|
|
|
|
|
2017-05-30 16:38:06 -04:00
|
|
|
def discussable?
|
2017-06-19 12:25:21 -04:00
|
|
|
!meta?
|
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
def suggestible?
|
|
|
|
!removed?
|
|
|
|
end
|
|
|
|
|
2017-06-19 12:25:21 -04:00
|
|
|
def rich_text
|
2018-06-21 08:22:40 -04:00
|
|
|
@parent_file.try(:highlight_lines!) if @parent_file && !@rich_text
|
2017-06-19 12:25:21 -04:00
|
|
|
|
|
|
|
@rich_text
|
2017-05-30 16:38:06 -04:00
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
def meta_positions
|
|
|
|
return unless meta?
|
|
|
|
|
|
|
|
{
|
|
|
|
old_pos: old_pos,
|
|
|
|
new_pos: new_pos
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-16 14:53:33 -04:00
|
|
|
# We have to keep this here since it is still used for conflict resolution
|
|
|
|
# Conflict::File#as_json renders json diff lines in sections
|
2016-07-25 14:47:09 -04:00
|
|
|
def as_json(opts = nil)
|
2018-08-16 14:53:33 -04:00
|
|
|
DiffLineSerializer.new.represent(self)
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_line_code
|
|
|
|
@parent_file&.line_code(self)
|
|
|
|
end
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|