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.



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

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.



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

def file_comments
  @file_comments
end

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

Instance Method Details

#each(&block) ⇒ Object



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

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