Class: SeeingIsBelieving::Binary::CommentLines

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/binary/comment_lines.rb

Overview

takes a body and a block passes the block the line the block returns the comment to add at the end of it

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_code, &commenter) ⇒ CommentLines

Returns a new instance of CommentLines.



13
14
15
# File 'lib/seeing_is_believing/binary/comment_lines.rb', line 13

def initialize(raw_code, &commenter)
  self.raw_code, self.commenter = raw_code, commenter
end

Class Method Details

.call(raw_code, &commenter) ⇒ Object



9
10
11
# File 'lib/seeing_is_believing/binary/comment_lines.rb', line 9

def self.call(raw_code, &commenter)
  new(raw_code, &commenter).call
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/seeing_is_believing/binary/comment_lines.rb', line 17

def call
  @call ||= begin
    commentable_lines = CommentableLines.new raw_code
    commentable_lines.call.each do |line_number, (index_of_newline, col)|
      first_index  = last_index = index_of_newline
      first_index -= 1 while first_index > 0 && raw_code[first_index-1] != "\n"
      comment_text = commenter.call raw_code[first_index...last_index], line_number
      range        = Parser::Source::Range.new(commentable_lines.buffer, first_index, last_index)
      commentable_lines.rewriter.insert_after range, comment_text
    end
    commentable_lines.rewriter.process
  end
end