Class: RBS::Inline::AnnotationParser::ParsingResult

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/inline/annotation_parser.rb,
sig/generated/rbs/inline/annotation_parser.rbs

Overview

ParsingResut groups consecutive comments, which may contain several annotations

Consecutive comments are comments are defined in below. They are basically comments that follows from the previous line, but there are some more requirements.

# Line 1
# Line 2           #=> Line 1 and Line 2 are consecutive

   # Line 3
 # Line4           #=> Line 3 and Line 4 are not consecutive, because the starting column are different

        # Line 5
foo()   # Line 6   #=> Line 5 and Line 6 are not consecutive, because Line 6 has leading code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first_comment) ⇒ ParsingResult

Returns a new instance of ParsingResult.

Parameters:

  • first_comment (Prism::Comment)


42
43
44
45
46
47
48
# File 'lib/rbs/inline/annotation_parser.rb', line 42

def initialize(first_comment) #: void
  @comments = [first_comment]
  @annotations = []
  content = first_comment.location.slice
  index = content.index(/[^#\s]/) || content.size
  @first_comment_offset = index
end

Instance Attribute Details

#annotationsArray[AST::Annotations::t | AST::CommentLines] (readonly)

: Array[AST::Annotations::t | AST::CommentLines]

Returns:



24
25
26
# File 'lib/rbs/inline/annotation_parser.rb', line 24

def annotations
  @annotations
end

#commentsArray[Prism::Comment] (readonly)

: Array

Returns:

  • (Array[Prism::Comment])


23
24
25
# File 'lib/rbs/inline/annotation_parser.rb', line 23

def comments
  @comments
end

#first_comment_offsetInteger (readonly)

: Integer

Returns:

  • (Integer)


25
26
27
# File 'lib/rbs/inline/annotation_parser.rb', line 25

def first_comment_offset
  @first_comment_offset
end

Instance Method Details

#add_comment(comment) ⇒ self?

Parameters:

  • comment (Prism::Comment)

Returns:

  • (self, nil)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbs/inline/annotation_parser.rb', line 65

def add_comment(comment)
  if last_comment.location.end_line + 1 == comment.location.start_line
    if last_comment.location.start_column == comment.location.start_column
      if prefix = comment.location.start_line_slice[..comment.location.start_column]
        prefix.strip!
        if prefix.empty?
          comments << comment
          self
        end
      end
    end
  end
end

#content(trim: false) ⇒ String

Parameters:

  • trim: (Boolean) (defaults to: false)

Returns:

  • (String)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbs/inline/annotation_parser.rb', line 80

def content(trim: false) #: String
  if trim
    leading_spaces = lines[0][/\A\s*/]
    offset = leading_spaces ? leading_spaces.length : 0

    lines.map do |line|
      prefix = line[0..offset] || ""
      if prefix.strip.empty?
        line[offset..]
      else
        line.lstrip
      end
    end.join("\n")
  else
    lines.join("\n")
  end
end

#each_annotationvoid #each_annotationEnumerator[AST::Annotations::t, void]

: () { (AST::Annotations::t) -> void } -> void : () -> Enumerator[AST::Annotations::t, void]

Overloads:

  • #each_annotationvoid

    This method returns an undefined value.

  • #each_annotationEnumerator[AST::Annotations::t, void]

    Returns:

    • (Enumerator[AST::Annotations::t, void])

Yields:

Yield Parameters:

  • arg0 (AST::Annotations::t)

Yield Returns:

  • (void)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbs/inline/annotation_parser.rb', line 29

def each_annotation(&block)
  if block
    annotations.each do |annot|
      if annot.is_a?(AST::Annotations::Base)
        yield annot
      end
    end
  else
    enum_for :each_annotation
  end
end

#last_commentPrism::Comment

Returns:

  • (Prism::Comment)


59
60
61
# File 'lib/rbs/inline/annotation_parser.rb', line 59

def last_comment
  comments.last or raise
end

#line_rangeRange[Integer]

Returns:

  • (Range[Integer])


51
52
53
54
55
56
# File 'lib/rbs/inline/annotation_parser.rb', line 51

def line_range
  first = comments.first or raise
  last = comments.last or raise

  first.location.start_line .. last.location.end_line
end

#linesArray[String]

: Array

Returns:

  • (Array[String])


98
99
100
# File 'lib/rbs/inline/annotation_parser.rb', line 98

def lines #: Array[String]
  comments.map { _1.location.slice[1...] || "" }
end