Class: RBS::InlineParser::CommentAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/inline_parser/comment_association.rb

Defined Under Namespace

Classes: Reference

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_blocksObject (readonly)

Returns the value of attribute associated_blocks.



6
7
8
# File 'lib/rbs/inline_parser/comment_association.rb', line 6

def associated_blocks
  @associated_blocks
end

#blocksObject (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_mapObject (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_mapObject (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) ⇒ Object



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(node) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File '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

#each_unassociated_blockObject



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) ⇒ Object



47
48
49
50
51
52
53
# File '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

#leading_block!(node) ⇒ Object



55
56
57
58
59
60
61
# File '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

#trailing_block(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File '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

#trailing_block!(node) ⇒ Object



76
77
78
79
80
81
82
# File '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