Module: RuboCop::Cop::AutocorrectAlignment
- Included in:
- Performance::CaseWhenSplat, Style::AccessModifierIndentation, Style::AlignArray, Style::AlignParameters, Style::CaseIndentation, Style::ClosingParenthesisIndentation, Style::CommentIndentation, Style::ElseAlignment, Style::FirstParameterIndentation, Style::IndentArray, Style::IndentAssignment, Style::IndentHash, Style::IndentationConsistency, Style::IndentationWidth, Style::MultilineMethodCallIndentation, Style::MultilineOperationIndentation, Style::ParallelAssignment::GenericCorrector, Style::RescueModifier, Style::SingleLineMethods
- Defined in:
- lib/rubocop/cop/mixin/autocorrect_alignment.rb
Overview
This module does auto-correction of nodes that should just be moved to the left or to the right, amount being determined by the instance variable @column_delta.
Constant Summary collapse
- SPACE =
' '.freeze
Instance Method Summary collapse
- #autocorrect(arg) ⇒ Object
- #check_alignment(items, base_column = nil) ⇒ Object
- #configured_indentation_width ⇒ Object
- #display_column(range) ⇒ Object
- #indentation(node) ⇒ Object
- #offset(node) ⇒ Object
Instance Method Details
#autocorrect(arg) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 50 def autocorrect(arg) return unless arg heredoc_ranges = heredoc_ranges(arg) expr = arg.respond_to?(:loc) ? arg.loc.expression : arg # We can't use the instance variable inside the lambda. That would just # give each lambda the same reference and they would all get the last # value of @column_delta. A local variable fixes the problem. column_delta = @column_delta return if block_comment_within?(expr) lambda do |corrector| each_line(expr) do |line_begin_pos| autocorrect_line(corrector, line_begin_pos, expr, column_delta, heredoc_ranges) end end end |
#check_alignment(items, base_column = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 30 def check_alignment(items, base_column = nil) unless items.empty? base_column ||= display_column(items.first.source_range) end each_bad_alignment(items, base_column) do |current| expr = current.source_range if offenses.any? { |o| within?(expr, o.location) } # If this offense is within a line range that is already being # realigned by autocorrect, we report the offense without # autocorrecting it. Two rewrites in the same area by the same # cop can not be handled. The next iteration will find the # offense again and correct it. add_offense(nil, expr) else add_offense(current, :expression) end end end |
#configured_indentation_width ⇒ Object
12 13 14 15 |
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 12 def configured_indentation_width cop_config['IndentationWidth'] || config.for_cop('IndentationWidth')['Width'] end |
#display_column(range) ⇒ Object
25 26 27 28 |
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 25 def display_column(range) line = processed_source.lines[range.line - 1] Unicode::DisplayWidth.of(line[0, range.column]) end |
#indentation(node) ⇒ Object
17 18 19 |
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 17 def indentation(node) offset(node) + (SPACE * configured_indentation_width) end |
#offset(node) ⇒ Object
21 22 23 |
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 21 def offset(node) SPACE * node.loc.column end |