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.



156
157
158
159
# File 'lib/syntax_tree/index.rb', line 156

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.



154
155
156
# File 'lib/syntax_tree/index.rb', line 154

def file_comments
  @file_comments
end

#locationObject (readonly)

Returns the value of attribute location.



154
155
156
# File 'lib/syntax_tree/index.rb', line 154

def location
  @location
end

Instance Method Details

#each(&block) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/syntax_tree/index.rb', line 161

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