class RBS::InlineParser::CommentAssociation
CommentAssociation manages the association between Prism::Node and CommentBlock
Attributes
CommentBlocks that are already associated to a node, which cannot be associated to another node again
Public Class Methods
(Buffer, Prism::Result) → instance
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 24 def self.build(buffer, result) blocks = AST::Ruby::CommentBlock.build(buffer, result.comments) new(blocks) end
(Array[CommentBlock]) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 8 def initialize(blocks) @blocks = blocks.sort_by {|block| block.start_line } @associated_blocks = Set[].compare_by_identity @start_line_map = {} @end_line_map = {} blocks.each do |block| if block.leading? end_line_map[block.end_line] = block else start_line_map[block.start_line] = block end end end
Public Instance Methods
(Prism::Node) { (CommentBlock) → void } → void
(Prism::Node) → Enumerator[CommentBlock]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 84 def each_enclosed_block(node) if block_given? start_line = node.location.start_line end_line = node.location.end_line if start_line+1 < end_line ((start_line + 1)...end_line).each do |line| if block = end_line_map.fetch(line, nil) unless associated_blocks.include?(block) associated_blocks << block yield block end end end end else enum_for :each_enclosed_block, node end end
Yields leading CommentBlocks that is enclosed in the given node
Note that enclosed_blocks works only after all of the leading blocks inside the node is associated.
Update association status.
() { (CommentBlock) → void } → void
() → Enumerator[CommentBlock]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 104 def each_unassociated_block if block_given? blocks.each do |block| unless associated_blocks.include?(block) yield block end end else enum_for :each_unassociated_block end end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 47 def leading_block(node) start_line = node.location.start_line if block = end_line_map.fetch(start_line - 1, nil) Reference.new(block, associated_blocks) end end
Returns a Reference that is associated to given node
Updates association explicitly through the reference.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 55 def leading_block!(node) if ref = leading_block(node) unless ref.associated? ref.associate!.block end end end
Returns an unassociated CommentBlock that can be associated to given node
Automatically updates association status.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 63 def trailing_block(node) location = if node.is_a?(Prism::Node) node.location else node end #: Prism::Location end_line = location.end_line if block = start_line_map.fetch(end_line, nil) Reference.new(block, associated_blocks) end end
Returns a Reference that is associated to given node, or by its location
Updates association explicitly through the reference.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/inline_parser/comment_association.rb, line 76 def trailing_block!(node) if ref = trailing_block(node) unless ref.associated? ref.associate!.block end end end
Returns a CommentBlock that is associated to given node, or by its location
Update association status.