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

109 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-23 12:14:25 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
module Diff
class Line
2018-11-08 19:23:39 +05:30
SERIALIZE_KEYS = %i(line_code rich_text text type index old_pos new_pos).freeze
2018-12-13 13:39:08 +05:30
attr_reader :line_code, :type, :old_pos, :new_pos
2016-09-13 17:45:13 +05:30
attr_writer :rich_text
2018-12-13 13:39:08 +05:30
attr_accessor :text, :index
2015-04-26 12:48:37 +05:30
2018-11-08 19:23:39 +05:30
def initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: 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
2018-11-08 19:23:39 +05:30
@rich_text = rich_text
2016-09-13 17:45:13 +05:30
2018-11-08 19:23:39 +05:30
# 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
2016-09-13 17:45:13 +05:30
end
2018-11-08 19:23:39 +05:30
def self.init_from_hash(hash)
2018-12-13 13:39:08 +05:30
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-09-13 17:45:13 +05:30
end
def to_hash
hash = {}
2018-11-08 19:23:39 +05:30
SERIALIZE_KEYS.each { |key| hash[key] = send(key) } # rubocop:disable GitlabSecurity/PublicSend
2016-09-13 17:45:13 +05:30
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-09-10 17:25:29 +05:30
%w[new new-nonewline].include?(type)
2015-10-24 18:46:33 +05:30
end
def removed?
2017-09-10 17:25:29 +05:30
%w[old old-nonewline].include?(type)
end
def meta?
%w[match new-nonewline old-nonewline].include?(type)
end
2018-11-08 19:23:39 +05:30
def match?
type == :match
end
2017-09-10 17:25:29 +05:30
def discussable?
!meta?
2015-10-24 18:46:33 +05:30
end
2016-08-24 12:49:21 +05:30
2018-12-23 12:14:25 +05:30
def suggestible?
!removed?
end
2016-09-13 17:45:13 +05:30
def rich_text
2018-11-08 19:23:39 +05:30
@parent_file.try(:highlight_lines!) if @parent_file && !@rich_text
2016-09-13 17:45:13 +05:30
@rich_text
end
2018-11-08 19:23:39 +05:30
def meta_positions
return unless meta?
{
old_pos: old_pos,
new_pos: new_pos
}
end
2018-11-20 20:47:30 +05:30
# 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-09-13 17:45:13 +05:30
def as_json(opts = nil)
2018-11-20 20:47:30 +05:30
DiffLineSerializer.new.represent(self)
2016-09-13 17:45:13 +05:30
end
2018-11-08 19:23:39 +05:30
private
def calculate_line_code
@parent_file&.line_code(self)
end
2015-04-26 12:48:37 +05:30
end
end
end