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