Class: Gitlab::Diff::Suggestion

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize, Suggestible
Defined in:
lib/gitlab/diff/suggestion.rb

Constant Summary

Constants included from Suggestible

Suggestible::MAX_LINES_CONTEXT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from Suggestible

#diff_lines, #fetch_from_content, #from_line, #to_line

Constructor Details

#initialize(text, line:, above:, below:, diff_file:) ⇒ Suggestion

Returns a new instance of Suggestion.


12
13
14
15
16
17
18
# File 'lib/gitlab/diff/suggestion.rb', line 12

def initialize(text, line:, above:, below:, diff_file:)
  @text = text
  @target_line = line
  @lines_above = above.to_i
  @lines_below = below.to_i
  @diff_file = diff_file
end

Instance Attribute Details

#diff_fileObject (readonly)

Returns the value of attribute diff_file


9
10
11
# File 'lib/gitlab/diff/suggestion.rb', line 9

def diff_file
  @diff_file
end

#lines_aboveObject (readonly)

Returns the value of attribute lines_above


9
10
11
# File 'lib/gitlab/diff/suggestion.rb', line 9

def lines_above
  @lines_above
end

#lines_belowObject (readonly)

Returns the value of attribute lines_below


9
10
11
# File 'lib/gitlab/diff/suggestion.rb', line 9

def lines_below
  @lines_below
end

#target_lineObject (readonly)

Returns the value of attribute target_line


9
10
11
# File 'lib/gitlab/diff/suggestion.rb', line 9

def target_line
  @target_line
end

Instance Method Details

#from_contentObject


29
30
31
32
33
# File 'lib/gitlab/diff/suggestion.rb', line 29

def from_content
  strong_memoize(:from_content) do
    fetch_from_content
  end
end

#to_contentObject


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

def to_content
  return "" if @text.blank?

  # The parsed suggestion doesn't have information about the correct
  # ending characters (we may have a line break, or not), so we take
  # this information from the last line being changed (last
  # characters).
  endline_chars = line_break_chars(from_content.lines.last)
  "#{@text}#{endline_chars}"
end

#to_hashObject


20
21
22
23
24
25
26
27
# File 'lib/gitlab/diff/suggestion.rb', line 20

def to_hash
  {
    from_content: from_content,
    to_content: to_content,
    lines_above: @lines_above,
    lines_below: @lines_below
  }
end