Class: RBS::InlineParser::CommentAssociation

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blocks) ⇒ CommentAssociation

Returns a new instance of CommentAssociation.

Parameters:

  • (Array[CommentBlock])


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_blocksSet[CommentBlock] (readonly)

CommentBlocks that are already associated to a node, which cannot be associated to another node again

Returns:

  • (Set[CommentBlock])


16
17
18
# File 'sig/inline_parser/comment_association.rbs', line 16

def associated_blocks
  @associated_blocks
end

#blocksArray[CommentBlock] (readonly)

Returns the value of attribute blocks.

Returns:

  • (Array[CommentBlock])


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

def blocks
  @blocks
end

#end_line_mapHash[Integer, CommentBlock] (readonly)

Returns the value of attribute end_line_map.

Returns:

  • (Hash[Integer, CommentBlock])


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

def end_line_map
  @end_line_map
end

#start_line_mapHash[Integer, CommentBlock] (readonly)

Returns the value of attribute start_line_map.

Returns:

  • (Hash[Integer, CommentBlock])


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

Parameters:

Returns:

  • (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.

Overloads:

  • #each_enclosed_block(arg0) ⇒ void

    This method returns an undefined value.

    Parameters:

    • arg0 (Prism::Node)
  • #each_enclosed_block(arg0) ⇒ Enumerator[CommentBlock]

    Parameters:

    • arg0 (Prism::Node)

    Returns:

    • (Enumerator[CommentBlock])

Yields:

Yield Parameters:

  • arg0 (CommentBlock)

Yield Returns:

  • (void)


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_blockvoid #each_unassociated_blockEnumerator[CommentBlock]

Overloads:

  • #each_unassociated_blockvoid

    This method returns an undefined value.

  • #each_unassociated_blockEnumerator[CommentBlock]

    Returns:

    • (Enumerator[CommentBlock])

Yields:

Yield Parameters:

  • arg0 (CommentBlock)

Yield Returns:

  • (void)


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.

Parameters:

  • (Prism::Node)

Returns:



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.

Parameters:

  • (Prism::Node)

Returns:

  • (CommentBlock, nil)


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.

Parameters:

  • (Prism::Node, Prism::Location)

Returns:



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.

Parameters:

  • (Prism::Node, Prism::Location)

Returns:

  • (CommentBlock, nil)


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