File tree Expand file tree Collapse file tree 3 files changed +52
-10
lines changed Expand file tree Collapse file tree 3 files changed +52
-10
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ Metrics/PerceivedComplexity: { Exclude: [lib/bashly/config_validator.rb] }
1919Metrics/MethodLength : { Exclude: [lib/bashly/config_validator.rb] }
2020Style/GuardClause : { Exclude: [lib/bashly/config_validator.rb] }
2121
22+ # False positive report of invalid use of a method that starts with `set_`
23+ Naming/AccessorMethodName :
24+ Exclude :
25+ - ' lib/bashly/indenter.rb'
26+
2227# FIXME: The `Command` class is too long
2328Metrics/ClassLength : { Exclude: [lib/bashly/script/command.rb] }
2429
Original file line number Diff line number Diff line change 1+ require 'bashly/indenter'
2+
13class Array
24 def indent ( offset )
35 return self unless offset . positive?
46
57 indentation = ' ' * offset
6- heredoc_marker = nil
8+ indenter = Indenter . new indentation
79
8- map do |line |
9- if heredoc_marker
10- heredoc_marker = nil if /^#{ heredoc_marker } \n ?$/ . match? ( line )
11- line
12- else
13- heredoc_marker = $1 if line =~ /<<-?\s *(\w +)/
14- "#{ indentation } #{ line } "
15- end
16- end
10+ map { |line | indenter . indent line }
1711 end
1812
1913 def nonuniq
Original file line number Diff line number Diff line change 1+ # A helper class, used by the `Array#indent` extension.
2+ # It will return the array of strings with all strings prefixed by `indentation`
3+ # unless the line is within a heredoc block.
4+ class Indenter
5+ attr_reader :marker , :indentation
6+
7+ def initialize ( indentation )
8+ @indentation = indentation
9+ @marker = nil
10+ end
11+
12+ def indent ( line )
13+ if inside_heredoc?
14+ reset_marker if heredoc_closed? ( line )
15+ line
16+ else
17+ set_heredoc_state ( line )
18+ "#{ indentation } #{ line } "
19+ end
20+ end
21+
22+ private
23+
24+ def reset_marker
25+ @marker = nil
26+ end
27+
28+ def inside_heredoc?
29+ !!marker
30+ end
31+
32+ def set_heredoc_state ( line )
33+ @marker = extract_heredoc_marker ( line )
34+ end
35+
36+ def extract_heredoc_marker ( line )
37+ line =~ /<<-?\s *(\w +)/ ? $1 : nil
38+ end
39+
40+ def heredoc_closed? ( line )
41+ inside_heredoc? && /^#{ marker } \n ?$/ . match? ( line )
42+ end
43+ end
You can’t perform that action at this time.
0 commit comments