debian-mirror-gitlab/lib/gitlab/diff/line.rb

74 lines
1.5 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
module Gitlab
module Diff
class Line
2016-01-29 22:53:50 +05:30
attr_reader :type, :index, :old_pos, :new_pos
2016-09-13 17:45:13 +05:30
attr_writer :rich_text
2016-01-29 22:53:50 +05:30
attr_accessor :text
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
def initialize(text, type, index, old_pos, new_pos, parent_file: nil)
2015-04-26 12:48:37 +05:30
@text, @type, @index = text, type, index
@old_pos, @new_pos = old_pos, new_pos
2016-09-13 17:45:13 +05:30
@parent_file = parent_file
end
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
2015-04-26 12:48:37 +05:30
end
2015-10-24 18:46:33 +05:30
2016-08-24 12:49:21 +05:30
def old_line
old_pos unless added? || meta?
end
def new_line
new_pos unless removed? || meta?
end
2017-08-17 22:00:37 +05:30
def line
new_line || old_line
end
2016-08-24 12:49:21 +05:30
def unchanged?
type.nil?
end
2015-10-24 18:46:33 +05:30
def added?
2017-08-17 22:00:37 +05:30
type == 'new' || type == 'new-nonewline'
2015-10-24 18:46:33 +05:30
end
def removed?
2017-08-17 22:00:37 +05:30
type == 'old' || type == 'old-nonewline'
2015-10-24 18:46:33 +05:30
end
2016-08-24 12:49:21 +05:30
2016-09-13 17:45:13 +05:30
def rich_text
@parent_file.highlight_lines! if @parent_file && !@rich_text
@rich_text
end
2016-08-24 12:49:21 +05:30
def meta?
2017-08-17 22:00:37 +05:30
type == 'match'
2016-08-24 12:49:21 +05:30
end
2016-09-13 17:45:13 +05:30
def as_json(opts = nil)
{
type: type,
old_line: old_line,
new_line: new_line,
text: text,
rich_text: rich_text || text
}
end
2015-04-26 12:48:37 +05:30
end
end
end