Class: Gitlab::Diff::SuggestionsParser

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

Constant Summary collapse

SUGGESTION_CONTEXT =

Matches for instance “-1”, “+1” or “-1+2”.

/^(\-(?<above>\d+))?(\+(?<below>\d+))?$/
CSS =
'pre.language-suggestion'
XPATH =
Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze

Class Method Summary collapse

Class Method Details

.parse(text, position:, project:, supports_suggestion: true) ⇒ Object

Returns an array of Gitlab::Diff::Suggestion which represents each suggestion in the given text.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gitlab/diff/suggestions_parser.rb', line 16

def parse(text, position:, project:, supports_suggestion: true)
  return [] unless position.complete?

  html = Banzai.render(text, project: nil,
                             no_original_data: true,
                             suggestions_filter_enabled: supports_suggestion)
  doc = Nokogiri::HTML(html)
  suggestion_nodes = doc.xpath(XPATH)

  return [] if suggestion_nodes.empty?

  diff_file = position.diff_file(project.repository)

  suggestion_nodes.map do |node|
    lang_param = node['data-lang-params']

    lines_above, lines_below = nil

    if lang_param && suggestion_params = fetch_suggestion_params(lang_param)
      lines_above = suggestion_params[:above]
      lines_below = suggestion_params[:below]
    end

    Gitlab::Diff::Suggestion.new(node.text,
                                 line: position.new_line,
                                 above: lines_above.to_i,
                                 below: lines_below.to_i,
                                 diff_file: diff_file)
  end
end