module RuboCop::Cop::LineLengthHelp

Help methods for determining if a line is too long.

Private Instance Methods

allow_uri?() click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 22
def allow_uri?
  config.for_cop('Layout/LineLength')['AllowURI']
end
allowed_uri_position?(line, uri_range) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 26
def allowed_uri_position?(line, uri_range)
  uri_range.begin < max_line_length && uri_range.end == line_length(line)
end
directive_on_source_line?(line_index) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 13
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_uri_end_position(line, end_position) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 63
def extend_uri_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_uri_range(line) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 34
def find_excessive_uri_range(line)
  last_uri_match = match_uris(line).last
  return nil unless last_uri_match

  begin_position, end_position = last_uri_match.offset(0).map do |pos|
    pos + indentation_difference(line)
  end

  end_position = extend_uri_end_position(line, end_position)

  return nil if begin_position < max_line_length && end_position < max_line_length

  begin_position...end_position
end
ignore_cop_directives?() click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 9
def ignore_cop_directives?
  config.for_cop('Layout/LineLength')['IgnoreCopDirectives']
end
indentation_difference(line) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 57
def indentation_difference(line)
  return 0 unless tab_indentation_width

  (line.index(/[^\t]/) || 0) * (tab_indentation_width - 1)
end
line_length(line) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 30
def line_length(line)
  line.length + indentation_difference(line)
end
line_length_without_directive(line) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 96
def line_length_without_directive(line)
  DirectiveComment.before_comment(line).rstrip.length
end
match_uris(string) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 49
def match_uris(string)
  matches = []
  string.scan(uri_regexp) do
    matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
  end
  matches
end
tab_indentation_width() click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 79
def tab_indentation_width
  config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
    config.for_cop('Layout/IndentationWidth')['Width']
end
uri_regexp() click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 84
def uri_regexp
  @uri_regexp ||=
    URI::DEFAULT_PARSER.make_regexp(config.for_cop('Layout/LineLength')['URISchemes'])
end
valid_uri?(uri_ish_string) click to toggle source
# File lib/rubocop/cop/mixin/line_length_help.rb, line 89
def valid_uri?(uri_ish_string)
  URI.parse(uri_ish_string)
  true
rescue URI::InvalidURIError, NoMethodError
  false
end