Class: RBS::InlineParser::CommentAssociation
- Inherits:
-
Object
- Object
- RBS::InlineParser::CommentAssociation
- Defined in:
- sig/inline_parser/comment_association.rbs,
lib/rbs/inline_parser/comment_association.rb
Overview
CommentAssociation manages the association between Prism::Node and CommentBlock
Defined Under Namespace
Classes: Reference
Instance Attribute Summary collapse
-
#associated_blocks ⇒ Set[CommentBlock]
readonly
CommentBlocks that are already associated to a node, which cannot be associated to another node again.
-
#blocks ⇒ Array[CommentBlock]
readonly
Returns the value of attribute blocks.
-
#end_line_map ⇒ Hash[Integer, CommentBlock]
readonly
Returns the value of attribute end_line_map.
-
#start_line_map ⇒ Hash[Integer, CommentBlock]
readonly
Returns the value of attribute start_line_map.
Class Method Summary collapse
Instance Method Summary collapse
-
#each_enclosed_block(node) {|arg0| ... } ⇒ Object
Yields leading CommentBlocks that is enclosed in the given node.
- #each_unassociated_block {|arg0| ... } ⇒ Object
-
#initialize(blocks) ⇒ CommentAssociation
constructor
A new instance of CommentAssociation.
-
#leading_block(node) ⇒ Reference?
Returns a Reference that is associated to given node.
-
#leading_block!(node) ⇒ CommentBlock?
Returns an unassociated CommentBlock that can be associated to given node.
-
#trailing_block(node) ⇒ Reference?
Returns a Reference that is associated to given node, or by its location.
-
#trailing_block!(node) ⇒ CommentBlock?
Returns a CommentBlock that is associated to given node, or by its location.
Constructor Details
#initialize(blocks) ⇒ CommentAssociation
Returns a new instance of CommentAssociation.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File '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 |
Instance Attribute Details
#associated_blocks ⇒ Set[CommentBlock] (readonly)
CommentBlocks that are already associated to a node, which cannot be associated to another node again
16 17 18 |
# File 'sig/inline_parser/comment_association.rbs', line 16 def associated_blocks @associated_blocks end |
#blocks ⇒ Array[CommentBlock] (readonly)
Returns the value of attribute blocks.
6 7 8 |
# File 'lib/rbs/inline_parser/comment_association.rb', line 6 def blocks @blocks end |
#end_line_map ⇒ Hash[Integer, CommentBlock] (readonly)
Returns the value of attribute end_line_map.
6 7 8 |
# File 'lib/rbs/inline_parser/comment_association.rb', line 6 def end_line_map @end_line_map end |
#start_line_map ⇒ Hash[Integer, CommentBlock] (readonly)
Returns the value of attribute start_line_map.
6 7 8 |
# File 'lib/rbs/inline_parser/comment_association.rb', line 6 def start_line_map @start_line_map end |
Class Method Details
.build(buffer, result) ⇒ instance
24 25 26 27 |
# File '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 |
Instance Method Details
#each_enclosed_block(arg0) ⇒ void #each_enclosed_block(arg0) ⇒ Enumerator[CommentBlock]
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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'sig/inline_parser/comment_association.rbs', line 64 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 |
#each_unassociated_block ⇒ void #each_unassociated_block ⇒ Enumerator[CommentBlock]
104 105 106 107 108 109 110 111 112 113 114 |
# File '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 |
#leading_block(node) ⇒ Reference?
Returns a Reference that is associated to given node
Updates association explicitly through the reference.
44 45 46 47 48 49 50 |
# File 'sig/inline_parser/comment_association.rbs', line 44 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 |
#leading_block!(node) ⇒ CommentBlock?
Returns an unassociated CommentBlock that can be associated to given node
Automatically updates association status.
38 39 40 41 42 43 44 |
# File 'sig/inline_parser/comment_association.rbs', line 38 def leading_block!(node) if ref = leading_block(node) unless ref.associated? ref.associate!.block end end end |
#trailing_block(node) ⇒ Reference?
Returns a Reference that is associated to given node, or by its location
Updates association explicitly through the reference.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'sig/inline_parser/comment_association.rbs', line 56 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 |
#trailing_block!(node) ⇒ CommentBlock?
Returns a CommentBlock that is associated to given node, or by its location
Update association status.
50 51 52 53 54 55 56 |
# File 'sig/inline_parser/comment_association.rbs', line 50 def trailing_block!(node) if ref = trailing_block(node) unless ref.associated? ref.associate!.block end end end |