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
# 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, @type, @index = text, type, index
  @old_pos, @new_pos = old_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
end

Instance Attribute Details

#indexObject

Returns the value of attribute index


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

def index
  @index
end

#line_codeObject (readonly)

Returns the value of attribute line_code


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

def line_code
  @line_code
end

#new_posObject (readonly)

Returns the value of attribute new_pos


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

def new_pos
  @new_pos
end

#old_posObject (readonly)

Returns the value of attribute old_pos


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

def old_pos
  @old_pos
end

#rich_textObject


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

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

  @rich_text
end

#textObject

Returns the value of attribute text


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

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type


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

def type
  @type
end

Class Method Details

.init_from_hash(hash) ⇒ Object


26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/diff/line.rb', line 26

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


37
38
39
40
41
42
43
# File 'lib/gitlab/diff/line.rb', line 37

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)

67
68
69
# File 'lib/gitlab/diff/line.rb', line 67

def added?
  %w[new new-nonewline].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


108
109
110
# File 'lib/gitlab/diff/line.rb', line 108

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

#discussable?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'lib/gitlab/diff/line.rb', line 83

def discussable?
  !meta?
end

#lineObject


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

def line
  new_line || old_line
end

#match?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/gitlab/diff/line.rb', line 79

def match?
  type == :match
end

#meta?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/gitlab/diff/line.rb', line 75

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

#meta_positionsObject


97
98
99
100
101
102
103
104
# File 'lib/gitlab/diff/line.rb', line 97

def meta_positions
  return unless meta?

  {
    old_pos: old_pos,
    new_pos: new_pos
  }
end

#new_lineObject


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

def new_line
  new_pos unless removed? || meta?
end

#old_lineObject


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

def old_line
  old_pos unless added? || meta?
end

#removed?Boolean

Returns:

  • (Boolean)

71
72
73
# File 'lib/gitlab/diff/line.rb', line 71

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

#suggestible?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/gitlab/diff/line.rb', line 87

def suggestible?
  !removed?
end

#to_hashObject


45
46
47
48
49
# File 'lib/gitlab/diff/line.rb', line 45

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

#unchanged?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/gitlab/diff/line.rb', line 63

def unchanged?
  type.nil?
end