Module: SeeingIsBelieving::Binary::Engine::ToggleMark

Defined in:
lib/seeing_is_believing/binary/engine.rb

Class Method Summary collapse

Class Method Details

.call(options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/seeing_is_believing/binary/engine.rb', line 31

def self.call(options)
  options = options.dup
  body    = options.delete :body
  line    = options.delete :line
  markers = options.delete :markers
  alignment_strategy = options.delete :alignment_strategy

  marker_regexes = markers.values.map(&:regex)
  RewriteComments.call body, include_lines: [line] do |comment|
    if line == comment.line_number && marker_regexes.any? { |r| r =~ comment.text }
      new_comment = ''
    elsif line == comment.line_number && comment.text.empty?
      new_comment = FormatComment.call(
        comment.whitespace_col,
        markers.value.prefix,
        '',
        options.merge(
          pad_to: alignment_strategy.line_length_for(comment.line_number)
        ),
      )
    elsif line == comment.line_number
      new_comment = comment.whitespace + comment.text
    elsif match = markers.value.regex.match(comment.text)
      new_comment = FormatComment.call(
        comment.whitespace_col,
        markers.value.prefix,
        match.post_match,
        options.merge(
          pad_to: alignment_strategy.line_length_for(comment.line_number)
        )
      )
    else
      new_comment = comment.whitespace + comment.text
    end
    [new_comment[/^\s*/], new_comment.lstrip]
  end
end