Skip to content

Commit

Permalink
rubocop: fix offences of the Metrics/LineLength cop
Browse files Browse the repository at this point in the history
I realise that some code might be less readable now, but now that we set a good
default limit, we protect the codebase from further mess. It's important to do
this to prevent adding more mess to already messy code that we have. :doctor:
  • Loading branch information
kyrylo committed Mar 3, 2019
1 parent b2af65a commit 4956376
Show file tree
Hide file tree
Showing 108 changed files with 1,398 additions and 583 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ Style/StringLiterals:
# TODO: delete this rule when we drop Ruby 1.9.3 support.
Style/SymbolArray:
EnforcedStyle: brackets

Metrics/LineLength:
Max: 90
8 changes: 1 addition & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 760
Max: 794

# Offense count: 1
# Configuration parameters: CountBlocks.
Expand Down Expand Up @@ -313,9 +313,3 @@ Style/PerlBackrefs:
- 'lib/pry/last_exception.rb'
- 'lib/pry/method.rb'
- 'lib/pry/rubygem.rb'

# Offense count: 869
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 205
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ group :development do
gem 'yard'

# Rubocop supports only >=2.2.0
gem 'rubocop', '= 0.65.0', require: false if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.2.0')
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.2.0')
gem 'rubocop', '= 0.65.0', require: false
end
end

group :test do
Expand Down
8 changes: 6 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ $LOAD_PATH.unshift 'lib'
require 'pry/version'

CLOBBER.include('**/*~', '**/*#*', '**/*.log')
CLEAN.include('**/*#*', '**/*#*.*', '**/*_flymake*.*', '**/*_flymake', '**/*.rbc', '**/.#*.*')
CLEAN.include(
'**/*#*', '**/*#*.*', '**/*_flymake*.*', '**/*_flymake', '**/*.rbc', '**/.#*.*'
)

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
Expand Down Expand Up @@ -112,6 +114,8 @@ namespace :docker do

desc "test pry on multiple ruby versions"
task test: :build do
system "docker run -i -t -v /tmp/prytmp:/tmp/prytmp pry/pry ./multi_test_inside_docker.sh"
system(
"docker run -i -t -v /tmp/prytmp:/tmp/prytmp pry/pry ./multi_test_inside_docker.sh"
)
end
end
8 changes: 6 additions & 2 deletions lib/pry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

class Pry
# The default hooks - display messages when beginning and ending Pry sessions.
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session, :default) do |_out, _target, _pry_|
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(
:before_session, :default
) do |_out, _target, _pry_|
next if _pry_.quiet?

_pry_.run_command("whereami --quiet")
Expand Down Expand Up @@ -75,7 +77,9 @@ class Pry
end

DEFAULT_SYSTEM = proc do |output, cmd, _|
output.puts "Error: there was a problem executing system command: #{cmd}" unless system(cmd)
unless system(cmd)
output.puts "Error: there was a problem executing system command: #{cmd}"
end
end

# This is to keep from breaking under Rails 3.2 for people who are doing that
Expand Down
10 changes: 8 additions & 2 deletions lib/pry/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def reset
end

def parse_options(args = ARGV)
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options." unless options
unless options
raise NoOptionsError,
"No command line options defined! Use Pry::CLI.add_options to " \
"add command line options."
end

@pass_argv = args.index { |cli_arg| %w[- --].include?(cli_arg) }
if @pass_argv
Expand Down Expand Up @@ -133,7 +137,9 @@ def start(opts)
"See http://pryrepl.org/ for more information.\n"
)

on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
on(
:e, :exec=, "A line of code to execute in context before the session starts"
) do |input|
Pry.config.exec_string += "\n" unless Pry.config.exec_string.empty?
Pry.config.exec_string += input
end
Expand Down
11 changes: 8 additions & 3 deletions lib/pry/code/loc.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Pry
class Code
# Represents a line of code (which may, in fact, contain multiple lines if the
# entirety was eval'd as a single unit following the `edit` command).
# Represents a line of code (which may, in fact, contain multiple lines if
# the entirety was eval'd as a single unit following the `edit` command).
#
# A line of code is a tuple, which consists of a line and a line number. A
# `LOC` object's state (namely, the line parameter) can be changed via
Expand Down Expand Up @@ -61,7 +61,12 @@ def colorize(code_type)
# @return [void]
def add_line_number(max_width = 0, color = false)
padded = lineno.to_s.rjust(max_width)
colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
colorized_lineno =
if color
Pry::Helpers::BaseHelpers.colorize_code(padded)
else
padded
end
properly_padded_line = handle_multiline_entries_from_edit_command(line, max_width)
tuple[0] = "#{colorized_lineno}: #{properly_padded_line}"
end
Expand Down
18 changes: 11 additions & 7 deletions lib/pry/code_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def empty_lookup

# lookup variables and constants and `self` that are not modules
def default_lookup
# we skip instance methods as we want those to fall through to method_or_class_lookup()
# we skip instance methods as we want those to fall through to
# method_or_class_lookup()
if safe_to_evaluate?(str) && !looks_like_an_instance_method?(str)
obj = target.eval(str)

Expand All @@ -130,12 +131,15 @@ def default_lookup
end

def method_or_class_lookup
obj = case str
when /\S+\(\)\z/
Pry::Method.from_str(str.sub(/\(\)\z/, ''), target) || Pry::WrappedModule.from_str(str, target)
else
Pry::WrappedModule.from_str(str, target) || Pry::Method.from_str(str, target)
end
obj =
case str
when /\S+\(\)\z/
Pry::Method.from_str(str.sub(/\(\)\z/, ''), target) ||
Pry::WrappedModule.from_str(str, target)
else
Pry::WrappedModule.from_str(str, target) ||
Pry::Method.from_str(str, target)
end

lookup_super(obj, super_level)
end
Expand Down
29 changes: 22 additions & 7 deletions lib/pry/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

class Pry
# The super-class of all commands, new commands should be created by calling
# {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
# which creates a ClassCommand. Please don't use this class directly.
# {Pry::CommandSet#command} which creates a BlockCommand or
# {Pry::CommandSet#create_command} which creates a ClassCommand. Please don't
# use this class directly.
class Command
extend Helpers::DocumentationHelpers
extend CodeObject::Helpers
Expand Down Expand Up @@ -177,7 +178,11 @@ def matches?(val)
# @return [Fixnum]
def match_score(val)
if command_regex =~ val
Regexp.last_match.size > 1 ? Regexp.last_match.begin(1) : Regexp.last_match.end(0)
if Regexp.last_match.size > 1
Regexp.last_match.begin(1)
else
Regexp.last_match.end(0)
end
else
-1
end
Expand Down Expand Up @@ -352,7 +357,9 @@ def tokenize(val)
self.class.command_regex =~ val

# please call Command.matches? before Command#call_safely
raise CommandError, "fatal: called a command which didn't match?!" unless Regexp.last_match
unless Regexp.last_match
raise CommandError, "fatal: called a command which didn't match?!"
end

captures = Regexp.last_match.captures
pos = Regexp.last_match.end(0)
Expand All @@ -367,7 +374,11 @@ def tokenize(val)

args =
if arg_string
command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
if command_options[:shellwords]
Shellwords.shellwords(arg_string)
else
arg_string.split(" ")
end
else
[]
end
Expand All @@ -381,7 +392,9 @@ def tokenize(val)
def process_line(line)
command_match, arg_string, captures, args = tokenize(line)

check_for_command_collision(command_match, arg_string) if Pry.config.collision_warning
if Pry.config.collision_warning
check_for_command_collision(command_match, arg_string)
end

self.arg_string = arg_string
self.captures = captures
Expand Down Expand Up @@ -445,7 +458,9 @@ def call_safely(*args)
return void
end

raise CommandError, "The command '#{command_name}' requires an argument." if command_options[:argument_required] && args.empty?
if command_options[:argument_required] && args.empty?
raise CommandError, "The command '#{command_name}' requires an argument."
end

ret = use_unpatched_symbol do
call_with_hooks(*args)
Expand Down
27 changes: 19 additions & 8 deletions lib/pry/command_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def initialize(*imported_sets, &block)
# # Greet somebody
# @example Regexp command
# MyCommands = Pry::CommandSet.new do
# command /number-(\d+)/, "number-N regex command", :listing => "number" do |num, name|
# command(
# /number-(\d+)/, "number-N regex command", :listing => "number"
# ) do |num, name|
# puts "hello #{name}, nice number: #{num}"
# end
# end
Expand All @@ -82,7 +84,9 @@ def block_command(match, description = "No description.", options = {}, &block)
end
options = Pry::Command.default_options(match).merge!(options)

@commands[match] = Pry::BlockCommand.subclass(match, description, options, helper_module, &block)
@commands[match] = Pry::BlockCommand.subclass(
match, description, options, helper_module, &block
)
end
alias command block_command

Expand All @@ -102,7 +106,9 @@ def block_command(match, description = "No description.", options = {}, &block)
# end
#
# def process
# raise Pry::CommandError, "-u and -d makes no sense" if opts.present?(:u) && opts.present?(:d)
# if opts.present?(:u) && opts.present?(:d)
# raise Pry::CommandError, "-u and -d makes no sense"
# end
# result = args.join(" ")
# result.downcase! if opts.present?(:downcase)
# result.upcase! if opts.present?(:upcase)
Expand All @@ -117,7 +123,9 @@ def create_command(match, description = "No description.", options = {}, &block)
end
options = Pry::Command.default_options(match).merge!(options)

@commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block)
@commands[match] = Pry::ClassCommand.subclass(
match, description, options, helper_module, &block
)
@commands[match].class_eval(&block)
@commands[match]
end
Expand All @@ -127,7 +135,8 @@ def each(&block)
end

# Removes some commands from the set
# @param [Array<String>] searches the matches or listings of the commands to remove
# @param [Array<String>] searches the matches or listings of the commands
# to remove
def delete(*searches)
searches.each do |search|
cmd = find_command_by_match_or_listing(search)
Expand Down Expand Up @@ -231,8 +240,8 @@ def rename_command(new_match, search, options = {})
@commands.delete(cmd.match)
end

def disabled_command(name_of_disabled_command, message, matcher = name_of_disabled_command)
create_command name_of_disabled_command do
def disabled_command(disabled_command_name, message, matcher = disabled_command_name)
create_command(disabled_command_name) do
match matcher
description ""

Expand Down Expand Up @@ -317,7 +326,9 @@ def [](pattern)
#
def []=(pattern, command)
return @commands.delete(pattern) if command.equal?(nil)
raise TypeError, "command is not a subclass of Pry::Command" unless Class === command && command < Pry::Command
unless Class === command && command < Pry::Command
raise TypeError, "command is not a subclass of Pry::Command"
end

bind_command_to_pattern = pattern != command.match
if bind_command_to_pattern
Expand Down
3 changes: 2 additions & 1 deletion lib/pry/commands/amend_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def replace_in_array(array, range)
array[range] = arg_string + "\n"
end

# @return [Fixnum] The number of lines currently in `eval_string` (the input buffer).
# @return [Fixnum] The number of lines currently in `eval_string` (the
# input buffer)
def line_count
eval_string.lines.count
end
Expand Down
16 changes: 11 additions & 5 deletions lib/pry/commands/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ class Cat < Pry::ClassCommand
BANNER

def options(opt)
opt.on :ex, "Show the context of the last exception", optional_argument: true, as: Integer
opt.on :i, :in, "Show one or more entries from Pry's expression history", optional_argument: true, as: Range, default: -5..-1
opt.on :s, :start, "Starting line (defaults to the first line)", optional_argument: true, as: Integer
opt.on :e, :end, "Ending line (defaults to the last line)", optional_argument: true, as: Integer
opt.on :ex, "Show the context of the last exception",
optional_argument: true, as: Integer
opt.on :i, :in, "Show one or more entries from Pry's expression history",
optional_argument: true, as: Range, default: -5..-1
opt.on :s, :start, "Starting line (defaults to the first line)",
optional_argument: true, as: Integer
opt.on :e, :end, "Ending line (defaults to the last line)",
optional_argument: true, as: Integer
opt.on :l, :'line-numbers', "Show line numbers"
opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python')", argument: true, as: Symbol
opt.on :t, :type, "The file type for syntax highlighting " \
"(e.g., 'ruby' or 'python')",
argument: true, as: Symbol
end

def process
Expand Down
18 changes: 11 additions & 7 deletions lib/pry/commands/cat/exception_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def backtrace_line

def check_for_errors
raise CommandError, "No exception found." unless ex
raise CommandError, "The given backtrace level is out of bounds." unless backtrace_file

unless backtrace_file
raise CommandError, "The given backtrace level is out of bounds."
end
end

def start_and_end_line_for_code_window
Expand All @@ -70,12 +73,13 @@ def start_and_end_line_for_code_window
end

def header
unindent %{
#{bold 'Exception:'} #{ex.class}: #{ex.message}
--
#{bold('From:')} #{backtrace_file}:#{backtrace_line} @ #{bold("level: #{backtrace_level}")} of backtrace (of #{ex.backtrace.size - 1}).
}
unindent(
"#{bold 'Exception:'} #{ex.class}: #{ex.message}\n" \
"--\n" \
"#{bold('From:')} #{backtrace_file}:#{backtrace_line} @ " \
"#{bold("level: #{backtrace_level}")} of backtrace " \
"(of #{ex.backtrace.size - 1}).\n\n"
)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/pry/commands/cat/file_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class FileFormatter < AbstractFormatter
attr_reader :_pry_

def initialize(file_with_embedded_line, _pry_, opts)
raise CommandError, "Must provide a filename, --in, or --ex." unless file_with_embedded_line
unless file_with_embedded_line
raise CommandError, "Must provide a filename, --in, or --ex."
end

@file_with_embedded_line = file_with_embedded_line
@opts = opts
Expand Down
3 changes: 2 additions & 1 deletion lib/pry/commands/cat/input_expression_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def format
if numbered_input_items.length > 1
content = ""
numbered_input_items.each do |i, s|
content << "#{Helpers::Text.bold(i.to_s)}:\n" << decorate(Pry::Code(s).with_indentation(2)).to_s
content << "#{Helpers::Text.bold(i.to_s)}:\n"
content << decorate(Pry::Code(s).with_indentation(2)).to_s
end

content
Expand Down
Loading

0 comments on commit 4956376

Please sign in to comment.