123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::HashAlignmentStyles::TableAlignment

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/rubocop/cop/mixin/hash_alignment_styles.rb

Overview

Handles calculation of deltas when the enforced style is 'table'.

Instance Method Summary

ValueAlignment - Included

Instance Method Details

#deltas_for_first_pair(first_pair)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 84

def deltas_for_first_pair(first_pair)
  separator_delta = separator_delta(first_pair, first_pair, 0)
  {
    separator: separator_delta,
    value: value_delta(first_pair, first_pair) - separator_delta
  }
end

#hash_rocket_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 98

def hash_rocket_delta(first_pair, current_pair)
  target_operator_column(first_pair) - current_pair.loc.operator.column
end

#key_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 94

def key_delta(first_pair, current_pair)
  first_pair.key_delta(current_pair)
end

#max_delimiter_width(hash_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 132

def max_delimiter_width(hash_node)
  hash_node.pairs.map { |pair| pair.delimiter(true).length }.max
end

#max_key_width(hash_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 128

def max_key_width(hash_node)
  hash_node.keys.select(&:single_line?).map { |key| key.source.length }.max || 0
end

#multiline_key_end_columns(hash_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 124

def multiline_key_end_columns(hash_node)
  hash_node.keys.reject(&:single_line?).map { |key| key.source_range.end.column + 1 }
end

#target_operator_column(first_pair) (private)

The column the separator should land on: the shared key margin plus the widest single-line key, or, if larger, a multiline key’s own last-line end column. The latter can’t be measured from a multiline key’s (newline-including) source length, so it’s a separate candidate rather than folded into #max_key_width.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 114

def target_operator_column(first_pair)
  hash_node = first_pair.parent
  candidates = multiline_key_end_columns(hash_node)

  key_width = max_key_width(hash_node)
  candidates << (first_pair.loc.column + key_width + 1) if key_width.positive?

  candidates.max
end

#value_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 102

def value_delta(first_pair, current_pair)
  correct_value_column = target_operator_column(first_pair) +
                         max_delimiter_width(first_pair.parent) - 1

  current_pair.value_omission? ? 0 : correct_value_column - current_pair.value.loc.column
end