Class: SyntaxTree::Index::EntryComments

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/syntax_tree/index.rb

Overview

This class handles parsing comments from Ruby source code in the case that we use the instruction sequence backend. Because the instruction sequence backend doesn’t provide comments (since they are dropped) we provide this interface to lazily parse them out.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_comments, location) ⇒ EntryComments

Returns a new instance of EntryComments.



130
131
132
133
# File 'lib/syntax_tree/index.rb', line 130

def initialize(file_comments, location)
  @file_comments = file_comments
  @location = location
end

Instance Attribute Details

#file_commentsObject (readonly)

Returns the value of attribute file_comments.



128
129
130
# File 'lib/syntax_tree/index.rb', line 128

def file_comments
  @file_comments
end

#locationObject (readonly)

Returns the value of attribute location.



128
129
130
# File 'lib/syntax_tree/index.rb', line 128

def location
  @location
end

Instance Method Details

#each(&block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/syntax_tree/index.rb', line 135

def each(&block)
  line = location.line - 1
  result = []

  while line >= 0 && (comment = file_comments.comments[line])
    result.unshift(comment)
    line -= 1
  end

  result.each(&block)
end