123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::Alignment

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Defined in: lib/rubocop/cop/mixin/alignment.rb

Overview

This module checks for nodes that should be aligned to the left or right. This amount is determined by the instance variable @column_delta.

Constant Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#column_delta (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 12

attr_reader :column_delta

Instance Method Details

#check_alignment(items, base_column = nil) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 26

def check_alignment(items, base_column = nil)
  base_column ||= display_column(items.first.source_range) unless items.empty?

  each_bad_alignment(items, base_column) do |current|
    expr = current.source_range
    if @current_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 cannot be handled. The next iteration will find the
      # offense again and correct it.
      register_offense(expr, nil)
    else
      register_offense(current, current)
    end
  end
end

#configured_indentation_width (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 14

def configured_indentation_width
  cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width'] || 2
end

#display_column(range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 58

def display_column(range)
  line = processed_source.lines[range.line - 1]
  Unicode::DisplayWidth.of(line[0, range.column])
end

#each_bad_alignment(items, base_column) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 45

def each_bad_alignment(items, base_column)
  prev_line = -1
  items.each do |current|
    if current.loc.line > prev_line && begins_its_line?(current.source_range)
      @column_delta = base_column - display_column(current.source_range)

      yield current if @column_delta.nonzero?
    end
    prev_line = current.loc.line
  end
end

#end_of_line_comment(line) (private)

Deprecated.

Use processed_source.line_with_comment?(line)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 69

def end_of_line_comment(line)
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `end_of_line_comment` is deprecated. Use `processed_source.line_with_comment?` instead.
  WARNING

  processed_source.line_with_comment?(line)
end

#indentation(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 18

def indentation(node)
  offset(node) + (SPACE * configured_indentation_width)
end

#offset(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 22

def offset(node)
  SPACE * node.loc.column
end

#register_offense(offense_node, message_node) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 78

def register_offense(offense_node, message_node)
  add_offense(offense_node, message: message(message_node)) do |corrector|
    autocorrect(corrector, message_node)
  end
end

#within?(inner, outer) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/alignment.rb', line 64

def within?(inner, outer)
  inner.begin_pos >= outer.begin_pos && inner.end_pos <= outer.end_pos
end