123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::LineLengthHelp

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Alignment
Defined in: lib/rubocop/cop/mixin/line_length_help.rb

Overview

Help methods for determining if a line is too long.

Constant Summary

Alignment - Included

SPACE

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#allow_qualified_name?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 28

def allow_qualified_name?
  config.for_cop('Layout/LineLength')['AllowQualifiedName']
end

#allow_uri?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 24

def allow_uri?
  config.for_cop('Layout/LineLength')['AllowURI']
end

#ignore_cop_directives?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 11

def ignore_cop_directives?
  config.for_cop('Layout/LineLength')['IgnoreCopDirectives']
end

Instance Method Details

#allowed_position?(line, range) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 32

def allowed_position?(line, range)
  range.begin < max_line_length && range.end == line_length(line)
end

#directive_on_source_line?(line_index) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 15

def directive_on_source_line?(line_index)
  source_line_number = line_index + processed_source.buffer.first_line
  comment = processed_source.comment_at_line(source_line_number)

  return false unless comment

  !!DirectiveComment.new(comment).match_captures
end

#extend_end_position(line, end_position) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 85

def extend_end_position(line, end_position)
  # Extend the end position YARD comments with linked URLs of the form {<uri> <title>}
  if line&.match(/{(\s|\S)*}$/)
    match = line[end_position..line_length(line)]&.match(/(\s|\S)*}/)
    end_position += match.offset(0).last
  end

  # Extend the end position until the start of the next word, if any.
  # This allows for URIs that are wrapped in quotes or parens to be handled properly
  # while not allowing additional words to be added after the URL.
  if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
    end_position += match.offset(0).last
  end
  end_position
end

#find_excessive_range(line, type) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 40

def find_excessive_range(line, type)
  last_match = (type == :uri ? match_uris(line) : match_qualified_names(line)).last
  return nil unless last_match

  begin_position, end_position = last_match.offset(0)
  end_position = extend_end_position(line, end_position)

  line_indentation_difference = indentation_difference(line)
  begin_position += line_indentation_difference
  end_position += line_indentation_difference

  return nil if begin_position < max_line_length && end_position < max_line_length

  begin_position...end_position
end

#indentation_difference(line) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 72

def indentation_difference(line)
  return 0 unless tab_indentation_width

  index =
    if line.match?(/^[^\t]/)
      0
    else
      line.index(/[^\t]/) || 0
    end

  index * (tab_indentation_width - 1)
end

#line_length(line) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 36

def line_length(line)
  line.length + indentation_difference(line)
end

#line_length_without_directive(line) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 127

def line_length_without_directive(line)
  DirectiveComment.before_comment(line).rstrip.length
end

#match_qualified_names(string) (private)

[ GitHub ]

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

def match_qualified_names(string)
  matches = []
  string.scan(qualified_name_regexp) do
    matches << $LAST_MATCH_INFO
  end
  matches
end

#match_uris(string) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 56

def match_uris(string)
  matches = []
  string.scan(uri_regexp) do
    matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
  end
  matches
end

#qualified_name_regexp (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 116

def qualified_name_regexp
  /\b(?:[A-Z][A-Za-z0-9_]*::)+[A-Za-z_][A-Za-z0-9_]*\b/
end

#tab_indentation_width (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 101

def tab_indentation_width
  config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
    configured_indentation_width
end

#uri_regexp (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 106

def uri_regexp
  @uri_regexp ||= begin
    # Ruby 3.4 changes the default parser to RFC3986 which warns on make_regexp.
    # Additionally, the RFC2396_PARSER alias is only available on 3.4 for now.
    # Extra info at https://github.com/ruby/uri/issues/118
    parser = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
    parser.make_regexp(config.for_cop('Layout/LineLength')['URISchemes'])
  end
end

#valid_uri?(uri_ish_string) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/line_length_help.rb', line 120

def valid_uri?(uri_ish_string)
  URI.parse(uri_ish_string)
  true
rescue URI::InvalidURIError, NoMethodError
  false
end