Module: SeeingIsBelieving::Binary::RewriteComments

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

Class Method Summary collapse

Class Method Details

.call(code, &mapping) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/seeing_is_believing/binary/rewrite_comments.rb', line 6

def self.call(code, &mapping)
  buffer, parser, rewriter = ParserHelpers.initialize_parser code, 'rewrite_comments'
  ast, comments  = parser.parse_with_comments buffer

  comments.each do |comment|
    next unless comment.type == :inline
    # find whitespace
    last_char                   = comment.location.expression.begin_pos
    first_char                  = last_char
    first_char -= 1 while first_char > 0 && buffer.source[first_char-1] =~ /[ \t]/
    preceeding_whitespace       = buffer.source[first_char...last_char]
    preceeding_whitespace_range = Parser::Source::Range.new buffer, first_char, last_char

    # find line
    last_char = first_char
    first_char -= 1 while first_char > 0 && buffer.source[first_char-1] !~ /[\r\n]/
    line = buffer.source[first_char...last_char]

    # get results
    new_whitespace, new_comment = mapping.call(comment.location.line,
                                               line,
                                               preceeding_whitespace,
                                               comment.text)

    # update code
    rewriter.replace preceeding_whitespace_range, new_whitespace
    rewriter.replace comment.location.expression, new_comment
  end

  rewriter.process
end