Class: Gitlab::Diff::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/diff/line.rb

Constant Summary collapse

SERIALIZE_KEYS =

When SERIALIZE_KEYS is updated, to reset the redis cache entries you’ll

need to bump the VERSION constant on Gitlab::Diff::HighlightCache
%i[line_code rich_text text type index old_pos new_pos].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: nil) ⇒ Line

Returns a new instance of Line.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gitlab/diff/line.rb', line 15

def initialize(text, type, index, old_pos, new_pos, parent_file: nil, line_code: nil, rich_text: nil)
  @text = text
  @type = type
  @index = index
  @old_pos = old_pos
  @new_pos = new_pos
  @parent_file = parent_file
  @rich_text = rich_text

  # 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
  @marker_ranges = []
end

Instance Attribute Details

#embedded_imageObject

Returns the value of attribute embedded_image.



13
14
15
# File 'lib/gitlab/diff/line.rb', line 13

def embedded_image
  @embedded_image
end

#indexObject

Returns the value of attribute index.



13
14
15
# File 'lib/gitlab/diff/line.rb', line 13

def index
  @index
end

#line_codeObject

Returns the value of attribute line_code.



13
14
15
# File 'lib/gitlab/diff/line.rb', line 13

def line_code
  @line_code
end

#marker_rangesObject (readonly)

Returns the value of attribute marker_ranges.



11
12
13
# File 'lib/gitlab/diff/line.rb', line 11

def marker_ranges
  @marker_ranges
end

#new_posObject

Returns the value of attribute new_pos.



13
14
15
# File 'lib/gitlab/diff/line.rb', line 13

def new_pos
  @new_pos
end

#old_posObject

Returns the value of attribute old_pos.



13
14
15
# File 'lib/gitlab/diff/line.rb', line 13

def old_pos
  @old_pos
end

#rich_textObject



110
111
112
113
114
# File 'lib/gitlab/diff/line.rb', line 110

def rich_text
  @parent_file.try(:highlight_lines!) if @parent_file && !@rich_text

  @rich_text
end

#text(prefix: true) ⇒ Object



59
60
61
62
63
# File 'lib/gitlab/diff/line.rb', line 59

def text(prefix: true)
  return @text if prefix

  @text&.slice(1..).to_s
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/gitlab/diff/line.rb', line 13

def type
  @type
end

Class Method Details

.init_from_hash(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/diff/line.rb', line 30

def self.init_from_hash(hash)
  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])
end

.safe_init_from_hash(hash) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/gitlab/diff/line.rb', line 41

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

Instance Method Details

#added?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/gitlab/diff/line.rb', line 81

def added?
  %w[new new-nonewline new-nomappinginraw].include?(type)
end

#as_json(opts = nil) ⇒ Object

We have to keep this here since it is still used for conflict resolution Conflict::File#as_json renders json diff lines in sections



127
128
129
# File 'lib/gitlab/diff/line.rb', line 127

def as_json(opts = nil)
  DiffLineSerializer.new.represent(self)
end

#discussable?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/gitlab/diff/line.rb', line 102

def discussable?
  has_mapping_in_raw? && !meta?
end

#has_mapping_in_raw?Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/gitlab/diff/line.rb', line 93

def has_mapping_in_raw?
  # Used for rendered diff, when the displayed line doesn't have a matching line in the raw diff
  !type&.ends_with?('nomappinginraw')
end

#lineObject



73
74
75
# File 'lib/gitlab/diff/line.rb', line 73

def line
  new_line || old_line
end

#match?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/gitlab/diff/line.rb', line 98

def match?
  type == :match
end

#meta?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/gitlab/diff/line.rb', line 89

def meta?
  %w[match new-nonewline old-nonewline].include?(type)
end

#meta_positionsObject



116
117
118
119
120
121
122
123
# File 'lib/gitlab/diff/line.rb', line 116

def meta_positions
  return unless meta?

  {
    old_pos: old_pos,
    new_pos: new_pos
  }
end

#new_lineObject



69
70
71
# File 'lib/gitlab/diff/line.rb', line 69

def new_line
  new_pos unless removed? || meta?
end

#old_lineObject



65
66
67
# File 'lib/gitlab/diff/line.rb', line 65

def old_line
  old_pos unless added? || meta?
end

#removed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/gitlab/diff/line.rb', line 85

def removed?
  %w[old old-nonewline old-nomappinginraw].include?(type)
end

#set_marker_ranges(marker_ranges) ⇒ Object



55
56
57
# File 'lib/gitlab/diff/line.rb', line 55

def set_marker_ranges(marker_ranges)
  @marker_ranges = marker_ranges
end

#suggestible?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/gitlab/diff/line.rb', line 106

def suggestible?
  !removed?
end

#to_hashObject



49
50
51
52
53
# File 'lib/gitlab/diff/line.rb', line 49

def to_hash
  hash = {}
  SERIALIZE_KEYS.each { |key| hash[key] = send(key) } # rubocop:disable GitlabSecurity/PublicSend
  hash
end

#unchanged?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/gitlab/diff/line.rb', line 77

def unchanged?
  type.nil?
end