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