2014-09-08 09:25:50 -04:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class Line
|
2015-12-30 18:42:11 -05:00
|
|
|
attr_reader :type, :index, :old_pos, :new_pos
|
2016-07-29 08:29:14 -04:00
|
|
|
attr_writer :rich_text
|
2015-12-30 18:42:11 -05:00
|
|
|
attr_accessor :text
|
2014-09-08 09:25:50 -04:00
|
|
|
|
2016-08-02 04:20:22 -04:00
|
|
|
def initialize(text, type, index, old_pos, new_pos, parent_file: 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
|
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)
|
|
|
|
new(hash[:text], hash[:type], hash[:index], hash[:old_pos], hash[:new_pos])
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_keys
|
|
|
|
@serialize_keys ||= %i(text type index old_pos new_pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
|
|
|
hash = {}
|
|
|
|
serialize_keys.each { |key| hash[key] = send(key) }
|
|
|
|
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-03-13 16:09:43 -04:00
|
|
|
type == 'new' || type == 'new-nonewline'
|
2015-10-01 07:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def removed?
|
2017-03-13 16:09:43 -04:00
|
|
|
type == 'old' || type == 'old-nonewline'
|
2015-10-01 07:52:08 -04:00
|
|
|
end
|
2016-06-20 13:15:44 -04:00
|
|
|
|
2016-07-29 08:29:14 -04:00
|
|
|
def rich_text
|
2016-08-02 04:20:22 -04:00
|
|
|
@parent_file.highlight_lines! if @parent_file && !@rich_text
|
2016-07-29 08:29:14 -04:00
|
|
|
|
|
|
|
@rich_text
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
def meta?
|
2017-03-13 16:09:43 -04:00
|
|
|
type == 'match'
|
2016-06-20 13:15:44 -04:00
|
|
|
end
|
2016-07-25 14:47:09 -04:00
|
|
|
|
2017-05-30 16:38:06 -04:00
|
|
|
def discussable?
|
|
|
|
!['match', 'new-nonewline', 'old-nonewline'].include?(type)
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:47:09 -04:00
|
|
|
def as_json(opts = nil)
|
|
|
|
{
|
|
|
|
type: type,
|
|
|
|
old_line: old_line,
|
|
|
|
new_line: new_line,
|
|
|
|
text: text,
|
|
|
|
rich_text: rich_text || text
|
|
|
|
}
|
|
|
|
end
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|