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 =
"#"- SCHEMA_HEADER_PREFIXES =
[ COMPAT_PREFIX, COMPAT_PREFIX_MD, "Table name:", "Database name:", "Schema version:" ].freeze
- SCHEMA_HEADER_EXACT =
[ IndexAnnotation::Annotation::HEADER_TEXT, ForeignKeyAnnotation::Annotation::HEADER_TEXT, CheckConstraintAnnotation::Annotation::HEADER_TEXT, UniqueConstraintAnnotation::Annotation::HEADER_TEXT, ExclusionConstraintAnnotation::Annotation::HEADER_TEXT, EnumAnnotation::Annotation::HEADER_TEXT ].freeze
- MARKDOWN_HEADER_PREFIX =
format_markdown: truerenders each of the section headers above as "###" instead of " ", so SCHEMA_HEADER_EXACT can't match it directly. There's no HEADER_TEXT constant for the columns table itself, so "Columns" is listed explicitly here. "### "- MARKDOWN_SECTION_HEADERS =
(SCHEMA_HEADER_EXACT + ["Columns"]).freeze
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.
46 47 48 49 50 51 52 53 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 46 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.
42 43 44 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 42 def annotation_end @annotation_end end |
#annotation_start ⇒ Object (readonly)
Returns the line index (not the line number) that the annotation starts.
40 41 42 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 40 def annotation_start @annotation_start end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
44 45 46 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 44 def parser @parser end |
Instance Method Details
#annotated? ⇒ Boolean
Returns true if annotations are detected in the file content
116 117 118 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 116 def annotated? @annotation_start && @annotation_end end |
#annotation ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 107 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
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 56 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 ending = walk_forward_through_schema(comments, start, ending) 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 |