lib/chusaku/parser.rb in chusaku-0.5.0 vs lib/chusaku/parser.rb in chusaku-0.6.0

- old
+ new

@@ -11,26 +11,30 @@ # content: <Original file content>, # groups: [ # { # type: :code, # body: 'class Foo\n', - # action: nil + # action: nil, + # line_number: 1 # }, # { # type: :comment, # body: ' # Bar\n # Baz\n', - # action: nil + # action: nil, + # line_number: 2 # }, # { # type: :action, # body: ' def action_name; end\n', - # action: 'action_name' + # action: 'action_name', + # line_number: 4 # } # { # type: :code, # body: 'end # vanilla is the best flavor\n', - # action: nil + # action: nil, + # line_number: 5 # } # ] # } # # @param path [String] File path to parse @@ -39,20 +43,20 @@ def self.call(path:, actions:) groups = [] group = {} content = IO.read(path) - content.each_line do |line| + content.each_line.with_index do |line, index| parsed_line = parse_line(line: line, actions: actions) - if group[:type] != parsed_line[:type] + if group[:type] == parsed_line[:type] + # Same group. Push the current line into the current group. + group[:body] += line + else # Now looking at a new group. Push the current group onto the array # and start a new one. groups.push(group) unless group.empty? - group = parsed_line - else - # Same group. Push the current line into the current group. - group[:body] += line + group = parsed_line.merge(line_number: index + 1) end end # Push the last group onto the array and return. groups.push(group)