@@ -17,6 +17,7 @@ def initialize(file_content, new_annotations, annotation_position, options)
17
17
@parsed_file = FileParser ::ParsedFile . new ( @file_content , @new_annotations , options ) . parse
18
18
end
19
19
20
+ # @return [String] Returns the annotated file content to be written back to a file
20
21
def generate
21
22
# Need to keep `.to_s` for now since the it can be either a String or Symbol
22
23
annotation_write_position = @options [ @annotation_position ] . to_s
@@ -33,33 +34,43 @@ def generate
33
34
# We need to get class start and class end depending on the position
34
35
parsed = FileParser ::CustomParser . new ( content_without_annotations , "" , 0 ) . tap ( &:parse )
35
36
36
- same_write_position = @parsed_file . has_annotations? && @parsed_file . annotation_position . to_s == annotation_write_position
37
+ _content = if %w[ after bottom ] . include? ( annotation_write_position )
38
+ content_annotated_after ( parsed , content_without_annotations )
39
+ else
40
+ content_annotated_before ( parsed , content_without_annotations , annotation_write_position )
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def content_annotated_before ( parsed , content_without_annotations , write_position )
47
+ same_write_position = @parsed_file . has_annotations? && @parsed_file . annotation_position . to_s == write_position
37
48
38
49
# Could error if there's no class or module declaration
39
50
_constant_name , line_number_before = parsed . starts . first
51
+
40
52
content_with_annotations_written_before = [ ]
41
53
content_with_annotations_written_before << content_without_annotations . lines [ 0 ...line_number_before ]
42
54
content_with_annotations_written_before << $/ if @parsed_file . has_leading_whitespace? && same_write_position
43
55
content_with_annotations_written_before << @new_wrapped_annotations . lines
44
56
content_with_annotations_written_before << $/ if @parsed_file . has_trailing_whitespace? && same_write_position
45
57
content_with_annotations_written_before << content_without_annotations . lines [ line_number_before ..]
46
58
59
+ content_with_annotations_written_before . join
60
+ end
61
+
62
+ def content_annotated_after ( parsed , content_without_annotations )
47
63
_constant_name , line_number_after = parsed . ends . last
64
+
48
65
content_with_annotations_written_after = [ ]
49
66
content_with_annotations_written_after << content_without_annotations . lines [ 0 ..line_number_after ]
50
67
content_with_annotations_written_after << $/
51
68
content_with_annotations_written_after << @new_wrapped_annotations . lines
52
69
content_with_annotations_written_after << content_without_annotations . lines [ ( line_number_after + 1 ) ..]
53
70
54
- _content = if %w[ after bottom ] . include? ( annotation_write_position )
55
- content_with_annotations_written_after . join
56
- else
57
- content_with_annotations_written_before . join
58
- end
71
+ content_with_annotations_written_after . join
59
72
end
60
73
61
- private
62
-
63
74
def wrapped_content ( content )
64
75
wrapper_open = if @options [ :wrapper_open ]
65
76
"# #{ @options [ :wrapper_open ] } \n "
0 commit comments