Class: AnnotateRb::ModelAnnotator::FileParser::AnnotationFinder
- Inherits:
-
Object
- Object
- AnnotateRb::ModelAnnotator::FileParser::AnnotationFinder
- Defined in:
- lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb
Defined Under Namespace
Classes: MalformedAnnotation, NoAnnotationFound
Constant Summary collapse
- COMPAT_PREFIX =
"== Schema Info"
- COMPAT_PREFIX_MD =
"## Schema Info"
- DEFAULT_ANNOTATION_ENDING =
"#"
Instance Attribute Summary collapse
-
#annotation_end ⇒ Object
readonly
Returns the line index (not the line number) that the annotation ends, inclusive.
-
#annotation_start ⇒ Object
readonly
Returns the line index (not the line number) that the annotation starts.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
-
#annotated? ⇒ Boolean
Returns true if annotations are detected in the file content.
- #annotation ⇒ Object
-
#initialize(content, wrapper_open, wrapper_close, parser) ⇒ AnnotationFinder
constructor
A new instance of AnnotationFinder.
-
#run ⇒ Object
Find the annotation’s line start and line end.
Constructor Details
#initialize(content, wrapper_open, wrapper_close, parser) ⇒ AnnotationFinder
Returns a new instance of AnnotationFinder.
22 23 24 25 26 27 28 29 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 22 def initialize(content, wrapper_open, wrapper_close, parser) @content = content @wrapper_open = wrapper_open @wrapper_close = wrapper_close @annotation_start = nil @annotation_end = nil @parser = parser end |
Instance Attribute Details
#annotation_end ⇒ Object (readonly)
Returns the line index (not the line number) that the annotation ends, inclusive.
18 19 20 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 18 def annotation_end @annotation_end end |
#annotation_start ⇒ Object (readonly)
Returns the line index (not the line number) that the annotation starts.
16 17 18 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 16 def annotation_start @annotation_start end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
20 21 22 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 20 def parser @parser end |
Instance Method Details
#annotated? ⇒ Boolean
Returns true if annotations are detected in the file content
95 96 97 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 95 def annotated? @annotation_start && @annotation_end end |
#annotation ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 86 def annotation @annotation ||= begin lines = @content.lines lines[@annotation_start..@annotation_end].join end end |
#run ⇒ Object
Find the annotation’s line start and line end
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 32 def run comments = @parser.comments start = comments.find_index { |comment, _| comment.include?(COMPAT_PREFIX) || comment.include?(COMPAT_PREFIX_MD) } raise NoAnnotationFound if start.nil? # Stop execution because we did not find if @wrapper_open prev_comment, _prev_line_number = comments[start - 1] # Change start to the line before if wrapper_open is defined and we find the wrapper open comment if prev_comment&.include?(@wrapper_open) start -= 1 end end # Find a contiguous block of comments from the starting point ending = start while ending < comments.size - 1 _comment, line_number = comments[ending] _next_comment, next_line_number = comments[ending + 1] if next_line_number - line_number == 1 ending += 1 else break end end raise MalformedAnnotation if start == ending if @wrapper_close if comments[ending].first.include?(@wrapper_close) # We can end here because it's the end of the annotation block else # Walk back until we find the end of the annotation comment block or the wrapper close to be flexible # We check if @wrapper_close is a substring because `comments` contains strings with the comment character while ending > start && comments[ending].first != DEFAULT_ANNOTATION_ENDING && !comments[ending].first.include?(@wrapper_close) ending -= 1 end end else # Walk back until we find the end of the annotation comment block while ending > start && comments[ending].first != DEFAULT_ANNOTATION_ENDING ending -= 1 end end # We want .last because we want the line indexes @annotation_start = comments[start].last @annotation_end = comments[ending].last [@annotation_start, @annotation_end] end |