Class: Gitlab::Diff::LinesUnfolder

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

Constant Summary collapse

UNFOLD_CONTEXT_SIZE =
3

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(diff_file, position) ⇒ LinesUnfolder

Returns a new instance of LinesUnfolder.


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/diff/lines_unfolder.rb', line 12

def initialize(diff_file, position)
  @diff_file = diff_file
  @blob = diff_file.old_blob
  @position = position
  @generate_top_match_line = true
  @generate_bottom_match_line = true

  # These methods update `@generate_top_match_line` and
  # `@generate_bottom_match_line`.
  @from_blob_line = calculate_from_blob_line!
  @to_blob_line = calculate_to_blob_line!
end

Instance Method Details

#blob_linesObject

Returns the extracted lines from the old blob which should be merged with the current diff lines.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/diff/lines_unfolder.rb', line 37

def blob_lines
  strong_memoize(:blob_lines) do
    # Blob lines, unlike diffs, doesn't start with an empty space for
    # unchanged line, so the parsing and highlighting step can get fuzzy
    # without the following change.
    line_prefix = ' '
    blob_as_diff_lines = @blob.data.each_line.map { |line| "#{line_prefix}#{line}" }

    lines = Gitlab::Diff::Parser.new.parse(blob_as_diff_lines, diff_file: @diff_file).to_a

    from = from_blob_line - 1
    to = to_blob_line - 1

    lines[from..to]
  end
end

#unfold_required?Boolean

Returns:

  • (Boolean)

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitlab/diff/lines_unfolder.rb', line 54

def unfold_required?
  strong_memoize(:unfold_required) do
    next false unless @diff_file.text?
    next false unless @position.unfoldable?
    next false if @diff_file.new_file? || @diff_file.deleted_file?
    next false unless @position.old_line
    # Invalid position (MR import scenario)
    next false if @position.old_line > @blob.lines.size
    next false if @diff_file.diff_lines.empty?
    next false if @diff_file.line_for_position(@position)
    next false unless unfold_line

    true
  end
end

#unfolded_diff_linesObject

Returns merged diff lines with required blob lines with correct positions.


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

def unfolded_diff_lines
  strong_memoize(:unfolded_diff_lines) do
    next unless unfold_required?

    merged_diff_with_blob_lines
  end
end