Class: RBS::Inline::AnnotationParser::ParsingResult
- Inherits:
-
Object
- Object
- RBS::Inline::AnnotationParser::ParsingResult
- 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
-
#annotations ⇒ Array[AST::Annotations::t | AST::CommentLines]
readonly
: Array[AST::Annotations::t | AST::CommentLines].
-
#comments ⇒ Array[Prism::Comment]
readonly
: Array.
-
#first_comment_offset ⇒ Integer
readonly
: Integer.
Instance Method Summary collapse
- #add_comment(comment) ⇒ self?
- #content(trim: false) ⇒ String
-
#each_annotation {|arg0| ... } ⇒ Object
: () { (AST::Annotations::t) -> void } -> void : () -> Enumerator[AST::Annotations::t, void].
-
#initialize(first_comment) ⇒ ParsingResult
constructor
A new instance of ParsingResult.
- #last_comment ⇒ Prism::Comment
- #line_range ⇒ Range[Integer]
-
#lines ⇒ Array[String]
: Array.
Constructor Details
#initialize(first_comment) ⇒ ParsingResult
Returns a new instance of ParsingResult.
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
#annotations ⇒ Array[AST::Annotations::t | AST::CommentLines] (readonly)
: Array[AST::Annotations::t | AST::CommentLines]
24 25 26 |
# File 'lib/rbs/inline/annotation_parser.rb', line 24 def annotations @annotations end |
#comments ⇒ Array[Prism::Comment] (readonly)
: Array
23 24 25 |
# File 'lib/rbs/inline/annotation_parser.rb', line 23 def comments @comments end |
#first_comment_offset ⇒ Integer (readonly)
: 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?
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
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_annotation ⇒ void #each_annotation ⇒ Enumerator[AST::Annotations::t, void]
: () { (AST::Annotations::t) -> void } -> void : () -> Enumerator[AST::Annotations::t, 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_comment ⇒ Prism::Comment
59 60 61 |
# File 'lib/rbs/inline/annotation_parser.rb', line 59 def last_comment comments.last or raise end |
#line_range ⇒ 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 |
#lines ⇒ Array[String]
: Array
98 99 100 |
# File 'lib/rbs/inline/annotation_parser.rb', line 98 def lines #: Array[String] comments.map { _1.location.slice[1...] || "" } end |