From b031df2f2f5850ee6e9018f33d35f3485a9b0423 Mon Sep 17 00:00:00 2001 From: Christopher Sexton Date: Fri, 22 Nov 2013 19:36:18 -0500 Subject: [PATCH 001/186] Add Watch Expression Command Usage: watch [EXPRESSION] watch watch --delete [INDEX] Evaluate an expression after every command and display it when its value changes. -d, --delete Delete the watch expression with the given index. If no index is given; clear all watch expressions. -l, --list Show all current watch expressions and their values. Calling watch with no expressions or options will also show the watch expressions. The watch command will use Pry's command `state` to keep track of watched expressions. --- lib/pry/commands/watch_expression.rb | 97 +++++++++++++++++++ .../commands/watch_expression/expression.rb | 41 ++++++++ spec/commands/watch_expression_spec.rb | 61 ++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 lib/pry/commands/watch_expression.rb create mode 100644 lib/pry/commands/watch_expression/expression.rb create mode 100644 spec/commands/watch_expression_spec.rb diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb new file mode 100644 index 00000000..4fe1746e --- /dev/null +++ b/lib/pry/commands/watch_expression.rb @@ -0,0 +1,97 @@ +class Pry + class Command::WatchExpression < Pry::ClassCommand + require 'pry/commands/watch_expression/expression.rb' + extend Pry::Helpers::BaseHelpers + + match 'watch' + group 'Context' + description 'Evaluate an expression after every command and display it when its value changes.' + command_options :use_prefix => false + + banner <<-'BANNER' + Usage: watch [EXPRESSION] + watch + watch --delete [INDEX] + + Evaluate an expression after every command and display it when its value changes. + BANNER + + def options(opt) + opt.on :d, :delete, + "Delete the watch expression with the given index. If no index is given; clear all watch expressions.", + :optional_argument => true, :as => Integer + opt.on :l, :list, + "Show all current watch expressions and their values. Calling watch with no expressions or options will also show the watch expressions." + end + + def process + ret = case + when opts.present?(:delete) + delete opts[:delete] + when opts.present?(:list) || args.empty? + list + else + add_hook + add_expression(args) + end + end + + private + + def expressions + state.expressions ||= [] + state.expressions + end + + def delete(index) + if index + output.puts "Deleting watch expression ##{index}: #{expressions[index-1]}" + expressions.delete_at(index-1) + else + output.puts "Deleting all watched expressions" + expressions.clear + end + end + + def list + if expressions.empty? + output.puts "No watched expressions" + else + Pry::Pager.with_pager(output) do |pager| + pager.puts "Listing all watched expressions:" + pager.puts "" + expressions.each_with_index do |expr, index| + pager.print text.with_line_numbers(expr.to_s, index+1) + end + pager.puts "" + end + end + end + + def eval_and_print_changed + expressions.each do |expr| + expr.eval! + if expr.changed? + output.puts "#{text.blue "watch"}: #{expr.to_s}" + end + end + end + + def add_expression(arguments) + e = expressions + e << Expression.new(target, arg_string) + output.puts "Watching #{Code.new(arg_string)}" + end + + def add_hook + hook = [:after_eval, :watch_expression] + unless Pry.config.hooks.hook_exists? *hook + _pry_.hooks.add_hook(*hook) do + eval_and_print_changed + end + end + end + end + + Pry::Commands.add_command(Pry::Command::WatchExpression) +end diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb new file mode 100644 index 00000000..1a3c1de9 --- /dev/null +++ b/lib/pry/commands/watch_expression/expression.rb @@ -0,0 +1,41 @@ +class Pry + class Command::WatchExpression + class Expression + attr_reader :target, :source, :value, :previous_value + + def initialize(target, source) + @target = target + @source = source + end + + def eval! + @previous_value = value + @value = target_eval(target, source) + end + + def to_s + "#{print_source} => #{print_value}" + end + + def changed? + (value != previous_value) + end + + def print_value + Pry::ColorPrinter.pp(value, "") + end + + def print_source + Code.new(source).strip + end + + private + + def target_eval(target, source) + target.eval(source) + rescue => e + e + end + end + end +end diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb new file mode 100644 index 00000000..71984e58 --- /dev/null +++ b/spec/commands/watch_expression_spec.rb @@ -0,0 +1,61 @@ +require 'helper' + +describe "watch expression" do + + # Custom eval that will: + # 1) Create an instance of pry that can use for multiple calls + # 2) Exercise the after_eval hook + # 3) Return the output + def eval(expr) + output = @tester.eval expr + @tester.pry.hooks.exec_hook :after_eval + output + end + + before do + @tester = pry_tester + @tester.pry.hooks.clear :after_eval + eval "watch --delete" + end + + it "registers the before_session hook" do + eval 'watch 1+1' + @tester.pry.hooks.hook_exists?(:after_eval, :watch_expression).should == true + end + + it "prints no watched expressions" do + eval('watch').should =~ /No watched expressions/ + end + + it "watches an expression" do + eval "watch 1+1" + eval('watch').should =~ /=> 2/ + end + + it "watches a local variable" do + eval 'foo = :bar' + eval 'watch foo' + eval('watch').should =~ /=> :bar/ + end + + #it "prints only when an expression changes" do + # # TODO: This is one of the main features, but I am not sure how to test the + # # output from a hook. + #end + + describe "deleting expressions" do + before do + eval 'watch :keeper' + eval 'watch :delete' + eval 'watch -d 2' + end + + it "keeps keeper" do + eval('watch').should =~ /keeper/ + end + + it "deletes delete" do + eval('watch').should.not =~ /delete/ + end + end +end From 2801c2f3c57bdd3c11aa11f395fe231e8779e310 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Thu, 19 Dec 2013 21:29:07 +0900 Subject: [PATCH 002/186] delete require 'rb-readline' from lib/pry.rb . When use gem rb-readline, require 'readline' not 'rb-readline'. --- lib/pry.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index b5cba74d..659b39a1 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -193,11 +193,7 @@ require 'tempfile' require 'pathname' begin - begin - require 'readline' - rescue LoadError - require 'rb-readline' - end + require 'readline' rescue LoadError warn "You're running a version of ruby with no Readline support" warn "Please `gem install rb-readline` or recompile ruby --with-readline." From de920882298ee55e675a36b75eb3ab5ec7e846ff Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 21 Dec 2013 00:01:04 +0900 Subject: [PATCH 003/186] Add 'public' to description of :M option in ls command. --- lib/pry/commands/ls.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index bfcdb30d..e8115970 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -29,7 +29,7 @@ class Pry def options(opt) opt.on :m, :methods, "Show public methods defined on the Object" - opt.on :M, "instance-methods", "Show methods defined in a Module or Class" + opt.on :M, "instance-methods", "Show public methods defined in a Module or Class" opt.on :p, :ppp, "Show public, protected (in yellow) and private (in green) methods" opt.on :q, :quiet, "Show only methods defined on object.singleton_class and object.class" opt.on :v, :verbose, "Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)" From f0de5fa9eb964f39df657803beed9544e11121b6 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 20 Dec 2013 20:28:53 +0100 Subject: [PATCH 004/186] pass an empty prompt to Readline.readline() from Pry::SimplePager. fixes #1048. --- lib/pry/pager.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb index 725439f3..3c01b44b 100644 --- a/lib/pry/pager.rb +++ b/lib/pry/pager.rb @@ -97,7 +97,7 @@ module Pry::Pager @out.print "\e[0m" if Pry.color @out.print " --- Press enter to continue " \ "( q to break ) --- \n" - raise StopPaging if Readline.readline.chomp == "q" + raise StopPaging if Readline.readline("").chomp == "q" @tracker.reset end end From 50460337fd2e93a846727970733ee1436aaae6be Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 21 Dec 2013 20:19:25 +0900 Subject: [PATCH 005/186] Add 'group should not be changed' to Pry::Command.group . --- lib/pry/command.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 4b3cb9de..158fa3f4 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -194,6 +194,7 @@ class Pry # The group in which the command should be displayed in "help" output. # This is usually auto-generated from directory naming, but it can be # manually overridden if necessary. + # Group should not be changed once it is initialized. def group(name=nil) @group ||= if name name From 9f9c72c82bec99407b77507ee5a6ec97509b8407 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 21 Dec 2013 23:20:42 +0900 Subject: [PATCH 006/186] Remove helpers for spec. Helpers for spec and specs for helper are mixed in spec/helpers, so remove helpers to spec/spec_helpers. --- spec/helper.rb | 6 +++--- spec/{helpers => spec_helpers}/bacon.rb | 0 spec/{helpers => spec_helpers}/mock_pry.rb | 0 spec/{helpers => spec_helpers}/repl_tester.rb | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename spec/{helpers => spec_helpers}/bacon.rb (100%) rename spec/{helpers => spec_helpers}/mock_pry.rb (100%) rename spec/{helpers => spec_helpers}/repl_tester.rb (100%) diff --git a/spec/helper.rb b/spec/helper.rb index d262205f..f48fc108 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -11,9 +11,9 @@ end require 'mocha/api' require 'pry/test/helper' -require 'helpers/bacon' -require 'helpers/mock_pry' -require 'helpers/repl_tester' +require 'spec_helpers/bacon' +require 'spec_helpers/mock_pry' +require 'spec_helpers/repl_tester' class Module public :remove_const diff --git a/spec/helpers/bacon.rb b/spec/spec_helpers/bacon.rb similarity index 100% rename from spec/helpers/bacon.rb rename to spec/spec_helpers/bacon.rb diff --git a/spec/helpers/mock_pry.rb b/spec/spec_helpers/mock_pry.rb similarity index 100% rename from spec/helpers/mock_pry.rb rename to spec/spec_helpers/mock_pry.rb diff --git a/spec/helpers/repl_tester.rb b/spec/spec_helpers/repl_tester.rb similarity index 100% rename from spec/helpers/repl_tester.rb rename to spec/spec_helpers/repl_tester.rb From 45c38d6b9c135ec3c7072c9f5a8def01301629c6 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 23 Dec 2013 01:23:22 +0900 Subject: [PATCH 007/186] Refactor lib/pry/method.rb(Pry::Method.all_from_obj). --- lib/pry/method.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/method.rb b/lib/pry/method.rb index da46f81b..06284420 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -154,7 +154,7 @@ class Pry # @param [Boolean] include_super Whether to include methods from ancestors. # @return [Array[Pry::Method]] def all_from_obj(obj, include_super=true) - all_from_class(class << obj; self; end, include_super) + all_from_class(singleton_class_of(obj), include_super) end # Get every `Class` and `Module`, in order, that will be checked when looking From 2bb5d645815adcbbd6cbd8eaa62bc7b78c64d3c7 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 23 Dec 2013 19:53:52 +0900 Subject: [PATCH 008/186] Fix to Pry::Command::Ls.error_list. Now non_mod_interrogatee is always false, so fix it. --- lib/pry/commands/ls.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index e8115970..02f40dac 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -73,7 +73,7 @@ class Pry def error_list any_args = args.any? - non_mod_interrogatee = !Module === @interrogatee + non_mod_interrogatee = !(Module === @interrogatee) [ ['-l does not make sense with a specified Object', :locals, any_args], ['-g does not make sense with a specified Object', :globals, any_args], From b12aa12ed14384f85c714b68ac526482cd9f7717 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 23 Dec 2013 22:53:17 +0900 Subject: [PATCH 009/186] Add spec to ls command. When 'ls -M instance' is called, exception should be raised. --- spec/commands/ls_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/commands/ls_spec.rb b/spec/commands/ls_spec.rb index 3a0b28a6..7c2b672a 100644 --- a/spec/commands/ls_spec.rb +++ b/spec/commands/ls_spec.rb @@ -92,6 +92,11 @@ describe "ls" do test.should.not.raise end + it "should show error message when instance is given with -M option" do + error = lambda{ pry_eval("ls -M String.new") }.should.raise(Pry::CommandError) + error.message.should.match(/-M only makes sense with a Module or a Class/) + end + # see: https://travis-ci.org/pry/pry/jobs/5071918 unless Pry::Helpers::BaseHelpers.rbx? From 107c7bcb72103b752e03764d476bf499a58fbebe Mon Sep 17 00:00:00 2001 From: yui-knk Date: Tue, 24 Dec 2013 23:12:43 +0900 Subject: [PATCH 010/186] change private_method_color and protected_method_color in config.ls to match 'ls --help'. --- lib/pry/pry_class.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 303a5a5e..d5099722 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -284,8 +284,8 @@ Readline version #{ver} detected - will not auto_resize! correctly. :heading_color => :bright_blue, :public_method_color => :default, - :private_method_color => :blue, - :protected_method_color => :blue, + :private_method_color => :green, + :protected_method_color => :yellow, :method_missing_color => :bright_red, :local_var_color => :yellow, From f3394b42ba73738ebef3a4591a4f200c4980040c Mon Sep 17 00:00:00 2001 From: yui-knk Date: Tue, 24 Dec 2013 23:35:37 +0900 Subject: [PATCH 011/186] Fix 'ls -g' shows colored results. If Ruby is post 1.9.2 global_variables returns Array of symbol. So change them to string to compare BUILTIN_GLOBALS or PSEUDO_GLOBALS. --- lib/pry/commands/ls/globals.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pry/commands/ls/globals.rb b/lib/pry/commands/ls/globals.rb index 00d9c748..f2b143e1 100644 --- a/lib/pry/commands/ls/globals.rb +++ b/lib/pry/commands/ls/globals.rb @@ -33,6 +33,7 @@ class Pry def format(globals) globals.sort_by(&:downcase).map do |name| + name = name.to_s if PSEUDO_GLOBALS.include?(name) color(:pseudo_global, name) elsif BUILTIN_GLOBALS.include?(name) From 5f1ccfa4279232d5bda1c8349ad8a9924d5dfa1a Mon Sep 17 00:00:00 2001 From: yui-knk Date: Thu, 26 Dec 2013 23:34:10 +0900 Subject: [PATCH 012/186] Typo in lib/pry/commands/cd.rb --- lib/pry/commands/cd.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/cd.rb b/lib/pry/commands/cd.rb index 95b9550d..b628176e 100644 --- a/lib/pry/commands/cd.rb +++ b/lib/pry/commands/cd.rb @@ -9,7 +9,7 @@ class Pry Move into new context (object or scope). As in UNIX shells use `cd ..` to go back, `cd /` to return to Pry top-level and `cd -` to toggle between last two - scopes. Complex syntax (e.g `cd ../@x/y`) also supported. + scopes. Complex syntax (e.g `cd ../@x/@y`) also supported. cd @x cd .. From b05b6edd4c1e9aa005b6f40a2e7d6d55a0618ab7 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 28 Dec 2013 10:03:36 +0900 Subject: [PATCH 013/186] Refactor lib/pry/commands/ls/globals.rb format method. --- lib/pry/commands/ls/globals.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pry/commands/ls/globals.rb b/lib/pry/commands/ls/globals.rb index f2b143e1..4a44f5f9 100644 --- a/lib/pry/commands/ls/globals.rb +++ b/lib/pry/commands/ls/globals.rb @@ -32,8 +32,7 @@ class Pry private def format(globals) - globals.sort_by(&:downcase).map do |name| - name = name.to_s + globals.map(&:to_s).sort_by(&:downcase).map do |name| if PSEUDO_GLOBALS.include?(name) color(:pseudo_global, name) elsif BUILTIN_GLOBALS.include?(name) From e0e1451836243d406c3abcf05151fb9891df2dda Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 29 Dec 2013 09:52:53 +0900 Subject: [PATCH 014/186] Add MRI 2.1.0 to .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 614e2f56..8f653b0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ rvm: - 1.9.2 - 1.9.3 - 2.0.0 + - 2.1.0 - ruby-head - ree - rbx From 38a1f71303bd3d437025b59592713ea893dba3ea Mon Sep 17 00:00:00 2001 From: yui-knk Date: Thu, 2 Jan 2014 11:15:20 +0900 Subject: [PATCH 015/186] change bond gem version in pry.gemspec, from 0.4.2 to 0.5.0 --- pry.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pry.gemspec b/pry.gemspec index b30acbf4..544729ab 100644 --- a/pry.gemspec +++ b/pry.gemspec @@ -27,5 +27,5 @@ Gem::Specification.new do |s| s.add_development_dependency 'mocha', '~> 0.13.1' s.add_development_dependency 'simplecov' # TODO: make this a plain dependency: - s.add_development_dependency 'bond', '~> 0.4.2' + s.add_development_dependency 'bond', '~> 0.5.0' end From c0da62be91780ca938eb56a0534ef00492c7bb33 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Fri, 3 Jan 2014 11:50:19 +0900 Subject: [PATCH 016/186] Refactor Pry::Method.from_str , because of comment on method_or_class_lookup in lib/pry/code_object.rb . --- lib/pry/code_object.rb | 6 ------ lib/pry/method.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/pry/code_object.rb b/lib/pry/code_object.rb index 8e50b6a7..42b57b94 100644 --- a/lib/pry/code_object.rb +++ b/lib/pry/code_object.rb @@ -112,12 +112,6 @@ class Pry end def method_or_class_lookup - # we need this here because stupid Pry::Method.from_str() does a - # Pry::Method.from_binding when str is nil. - # Once we refactor Pry::Method.from_str() so it doesnt lookup - # from bindings, we can get rid of this check - return nil if str.to_s.empty? - obj = case str when /\S+\(\)\z/ Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target) diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 06284420..7486001e 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -43,7 +43,7 @@ class Pry # method, or `nil` if no method could be located matching the parameters. def from_str(name, target=TOPLEVEL_BINDING, options={}) if name.nil? - from_binding(target) + nil elsif name.to_s =~ /(.+)\#(\S+)\Z/ context, meth_name = $1, $2 from_module(target.eval(context), meth_name, target) From 96b4686517a7768df30fe931973fa564e957cbc2 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Fri, 3 Jan 2014 12:31:21 +0900 Subject: [PATCH 017/186] Refactor WrappedModule#all_methods_for, delete private method all_from_common to Pry::Method.all_from_obj and Pry::Method.all_from_class. --- lib/pry/wrapped_module.rb | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/lib/pry/wrapped_module.rb b/lib/pry/wrapped_module.rb index fc265a39..abe9e0b8 100644 --- a/lib/pry/wrapped_module.rb +++ b/lib/pry/wrapped_module.rb @@ -367,22 +367,7 @@ class Pry # given module. # @return [Array] def all_methods_for(mod) - all_from_common(mod, :instance_method) + all_from_common(mod, :method) - end - - # FIXME: a variant of this method is also found in Pry::Method - def all_from_common(mod, method_type) - %w(public protected private).map do |visibility| - safe_send(mod, :"#{visibility}_#{method_type}s", false).select do |method_name| - if method_type == :method - safe_send(mod, method_type, method_name).owner == class << mod; self; end - else - safe_send(mod, method_type, method_name).owner == mod - end - end.map do |method_name| - Pry::Method.new(safe_send(mod, method_type, method_name), :visibility => visibility.to_sym) - end - end.flatten + Pry::Method.all_from_obj(mod, false) + Pry::Method.all_from_class(mod, false) end def nested_module?(parent, name) From 3217205f0a8f6a904766ec7a51141c286c9becb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Wed, 8 Jan 2014 09:50:59 +0100 Subject: [PATCH 018/186] Fix spacing in README --- README.markdown | 1 - 1 file changed, 1 deletion(-) diff --git a/README.markdown b/README.markdown index d6e5fdee..eaaa6ba1 100644 --- a/README.markdown +++ b/README.markdown @@ -37,7 +37,6 @@ including: * Exotic object support (BasicObject instances, IClasses, ...) * A Powerful and flexible command system * Ability to view and replay history - * Many convenience commands inspired by IPython, Smalltalk and other advanced REPLs * A wide-range number of [plugins](https://github.com/pry/pry/wiki/Available-plugins) that provide remote sessions, full debugging functionality, and more. From 4d5f6166008c5e1c3b512645f19e62f350419761 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Thu, 9 Jan 2014 12:44:04 +0900 Subject: [PATCH 019/186] Fix ansicon link. Link to adoxa/ansicon is broken, so change link to github repo. --- lib/pry.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry.rb b/lib/pry.rb index 659b39a1..c558b3df 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -215,7 +215,7 @@ if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi # only fail on jruby (where win32console doesn't work). # Instead we'll recommend ansicon, which does. rescue LoadError - warn "For a better pry experience, please use ansicon: http://adoxa.3eeweb.com/ansicon/" + warn "For a better pry experience, please use ansicon: https://github.com/adoxa/ansicon" end end From 090e58ccdc97c5b0ee19e9ae6f4be8e34d841a2f Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 11 Jan 2014 22:42:25 +0900 Subject: [PATCH 020/186] Rename Pry::CLI.process_options to add_option_processor . This method dose not process options but adds option processor, so rename it. --- lib/pry/cli.rb | 4 ++-- spec/cli_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pry/cli.rb b/lib/pry/cli.rb index 6f737703..11eaf0c1 100644 --- a/lib/pry/cli.rb +++ b/lib/pry/cli.rb @@ -44,7 +44,7 @@ class Pry end # Add a block responsible for processing parsed options. - def process_options(&block) + def add_option_processor(&block) self.option_processors ||= [] option_processors << block @@ -198,7 +198,7 @@ Copyright (c) 2013 John Mair (banisterfiend) "Start the session in the specified context. Equivalent to `context.pry` in a session.", :default => "Pry.toplevel_binding" ) -end.process_options do |opts| +end.add_option_processor do |opts| exit if opts.help? diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 7cb1f8bf..be5b0912 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -60,7 +60,7 @@ describe Pry::Hooks do Pry::CLI.add_options do on :optiontest, "A test option" - end.process_options do |opts| + end.add_option_processor do |opts| run = true if opts.present?(:optiontest) end.parse_options(["--optiontest"]) @@ -74,9 +74,9 @@ describe Pry::Hooks do Pry::CLI.add_options do on :optiontest, "A test option" on :optiontest2, "Another test option" - end.process_options do |opts| + end.add_option_processor do |opts| run = true if opts.present?(:optiontest) - end.process_options do |opts| + end.add_option_processor do |opts| run2 = true if opts.present?(:optiontest2) end.parse_options(["--optiontest", "--optiontest2"]) From 4a9b958e8b81752009e96e3a4a9e41324cb14811 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 13 Jan 2014 00:18:41 +0900 Subject: [PATCH 021/186] Fix doc for Pry::Method.from_str to match refactoring. --- lib/pry/method.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 7486001e..34aa30f5 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -31,8 +31,7 @@ class Pry # search in, find and return the requested method wrapped in a `Pry::Method` # instance. # - # @param [String, nil] name The name of the method to retrieve, or `nil` to - # delegate to `from_binding` instead. + # @param [String] name The name of the method to retrieve. # @param [Binding] target The context in which to search for the method. # @param [Hash] options # @option options [Boolean] :instance Look for an instance method if `name` doesn't @@ -40,7 +39,7 @@ class Pry # @option options [Boolean] :methods Look for a bound/singleton method if `name` doesn't # contain any context. # @return [Pry::Method, nil] A `Pry::Method` instance containing the requested - # method, or `nil` if no method could be located matching the parameters. + # method, or `nil` if name is `nil` or no method could be located matching the parameters. def from_str(name, target=TOPLEVEL_BINDING, options={}) if name.nil? nil From b83f945b5b9d0506f9f8e0acd0e2be063947e394 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 12 Jan 2014 22:59:16 +0100 Subject: [PATCH 022/186] drop 1.8 support from 'master'. continues on '0.9.12-stable'. --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8f653b0e..743f5872 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,10 @@ rvm: - - 1.8.7 - 1.9.2 - 1.9.3 - 2.0.0 - 2.1.0 - ruby-head - - ree - rbx - - jruby-18mode - jruby-19mode - jruby-head From 34b25ef03d485d66986c2339c1b771c01514d712 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 13 Jan 2014 00:38:56 +0100 Subject: [PATCH 023/186] update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfa2e9c5..2ec1ea1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ### 1.0.0 (2013/??/??) #### Dependency changes + +* 1.8 support discontinued from 1.0+ onwards. + * 0.9.12+ may continue to provide 1.8+ bug fixes. + * Require Coderay `>= 1.1.0` #### Features From 4464286aad205ccb077f3f927f146ef9de32782a Mon Sep 17 00:00:00 2001 From: yui-knk Date: Wed, 15 Jan 2014 23:09:08 +0900 Subject: [PATCH 024/186] Fix typo in README.markdown --- README.markdown | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.markdown b/README.markdown index eaaa6ba1..2f5e8b33 100644 --- a/README.markdown +++ b/README.markdown @@ -102,9 +102,9 @@ an instance variable inside that class: pry(Hello):1> ls -i instance variables: @x pry(Hello):1> cd @x - pry(20:2)> self + 10 + pry(20):2> self + 10 => 30 - pry(20:2)> cd .. + pry(20):2> cd .. pry(Hello):1> cd .. pry(main)> cd .. @@ -112,7 +112,7 @@ The number after the `:` in the pry prompt indicates the nesting level. To display more information about nesting, use the `nesting` command. E.g - pry("friend":3)> nesting + pry("friend"):3> nesting Nesting status: 0. main (Pry top level) 1. Hello @@ -123,7 +123,7 @@ command. E.g We can then jump back to any of the previous nesting levels by using the `jump-to` command: - pry("friend":3)> jump-to 1 + pry("friend"):3> jump-to 1 => 100 pry(Hello):1> @@ -224,7 +224,7 @@ In the following example we will enter the `Pry` class, list the instance methods beginning with 're' and display the source code for the `rep` method: pry(main)> cd Pry - pry(Pry)> ls -M --grep re + pry(Pry):1> ls -M --grep re Pry#methods: re readline refresh rep repl repl_epilogue repl_prologue retrieve_line pry(Pry):1> show-method rep -l @@ -256,9 +256,9 @@ Note that we can also view C methods (from Ruby Core) using the RETURN_ENUMERATOR(ary, 0, 0); result = rb_ary_new2(RARRAY_LEN(ary)); for (i = 0; i < RARRAY_LEN(ary); i++) { - if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) { - rb_ary_push(result, rb_ary_elt(ary, i)); - } + if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) { + rb_ary_push(result, rb_ary_elt(ary, i)); + } } return result; } From a56ac3f73dd375be208320b1f98c62a8cdfd5df7 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 17 Jan 2014 08:45:41 +0100 Subject: [PATCH 025/186] remove special-case for 'main' from Pry.view_clip(). --- lib/pry.rb | 2 +- lib/pry/pry_class.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index c558b3df..61afea6e 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -65,7 +65,7 @@ class Pry } ] - DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false] + DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false, TOPLEVEL_BINDING.eval("self")] # A simple prompt - doesn't display target or nesting level SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }] diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index d5099722..6edd734d 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -130,15 +130,11 @@ class Pry def self.view_clip(obj, max_length = 60) if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length obj.name.to_s - elsif TOPLEVEL_BINDING.eval('self') == obj - # special case for 'main' object :) - obj.to_s elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max_length obj.inspect else "#<#{obj.class}>"#:%x>"# % (obj.object_id << 1) end - rescue RescuableException "unknown" end From ea6cd413a457e75873e1ce66d49d934708fe795f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 17 Jan 2014 08:58:20 +0100 Subject: [PATCH 026/186] Revert "remove special-case for 'main' from Pry.view_clip()." This reverts commit a56ac3f73dd375be208320b1f98c62a8cdfd5df7. because, jruby. cc @headius --- lib/pry.rb | 2 +- lib/pry/pry_class.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/pry.rb b/lib/pry.rb index 61afea6e..c558b3df 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -65,7 +65,7 @@ class Pry } ] - DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false, TOPLEVEL_BINDING.eval("self")] + DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false] # A simple prompt - doesn't display target or nesting level SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }] diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 6edd734d..d5099722 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -130,11 +130,15 @@ class Pry def self.view_clip(obj, max_length = 60) if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length obj.name.to_s + elsif TOPLEVEL_BINDING.eval('self') == obj + # special case for 'main' object :) + obj.to_s elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max_length obj.inspect else "#<#{obj.class}>"#:%x>"# % (obj.object_id << 1) end + rescue RescuableException "unknown" end From 8b0866b5527883d38c9fbb9e6249040850a059e7 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 17 Jan 2014 10:27:32 +0100 Subject: [PATCH 027/186] add comment about special-case for jruby. --- lib/pry/pry_class.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index d5099722..76f2f3ed 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -131,7 +131,9 @@ class Pry if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length obj.name.to_s elsif TOPLEVEL_BINDING.eval('self') == obj - # special case for 'main' object :) + # special-case to support jruby. + # fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039 + # we can drop in the future. obj.to_s elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max_length obj.inspect From 74c07a82fc4cffbf12d7efc0357ae5ce4317519d Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 17 Jan 2014 10:32:29 +0100 Subject: [PATCH 028/186] add Pry.main. cache its value. --- lib/pry/pry_class.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 76f2f3ed..7cc1d55b 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -7,6 +7,10 @@ class Pry HOME_RC_FILE = ENV["PRYRC"] || "~/.pryrc" LOCAL_RC_FILE = "./.pryrc" + def self.main + @main ||= TOPLEVEL_BINDING.eval "self" + end + # @return [Hash] Pry's `Thread.current` hash def self.current Thread.current[:__pry__] ||= {} @@ -130,7 +134,7 @@ class Pry def self.view_clip(obj, max_length = 60) if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length obj.name.to_s - elsif TOPLEVEL_BINDING.eval('self') == obj + elsif Pry.main == obj # special-case to support jruby. # fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039 # we can drop in the future. From 0a8591aee80e6d1a5843e22069af21a883b47fe4 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 17 Jan 2014 10:39:13 +0100 Subject: [PATCH 029/186] update Pry.binding_for() to use Pry.main --- lib/pry/pry_class.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 7cc1d55b..6817db5a 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -353,7 +353,7 @@ Readline version #{ver} detected - will not auto_resize! correctly. if Binding === target target else - if TOPLEVEL_BINDING.eval('self') == target + if Pry.main == target TOPLEVEL_BINDING else target.__binding__ From 54989203e5cb840734f2a950532ac2934f5385c5 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 17 Jan 2014 10:44:11 +0100 Subject: [PATCH 030/186] update whereami command to use Pry.main (last one). --- lib/pry/commands/whereami.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index 2ec82252..1cf4bab7 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -109,7 +109,7 @@ class Pry end def top_level? - target_self == TOPLEVEL_BINDING.eval("self") + target_self == Pry.main end def handle_internal_binding From 5db725eca6f899fd2b34311fba99c00578b08ab7 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 17 Dec 2013 18:27:42 +0100 Subject: [PATCH 031/186] implement Pry global and pry-local configuration. maybe best explained with an example: [1] pry(main)> Pry.config.hi = "hi" => "hi" [2] pry(main)> _pry_.config.hi => "hi" [3] pry(main)> _pry_.config.x = 1 => 1 [4] pry(main)> Pry.config.x => nil [5] pry(main)> 'Pry.config' is an instance of Pry::Config::Default. it defines the initial default configuration values an instance of 'Pry' is going to have. an instance of 'Pry' uses an instance of Pry::Config who relies on Pry.config as a default or fallback for keys it cannot find locally. for example 'Pry.config.color = true' is seen by _all_ pry REPLs who are active but _pry_.config.color = false is seen only by the REPL session being interacted with. a number of "config shortcircuts" are still available, for example it is possible to say `_pry_.input = StringIO.new` or `Pry.input = StringIO.new`. the shortcuts are maintained for the Pry class and instances of the Pry class through Pry::Config.shortcuts. Pry::Config::Convenience adds a method called 'config_shortcuts' which can be used to setup shortcut access to 'some_obj.config.blah' as 'some_obj.blah' a lot of tests still fail, so work in progress. this should help solve #1055. --- Gemfile | 1 + lib/pry.rb | 60 ------- lib/pry/config.rb | 329 +++++++++------------------------- lib/pry/config/convenience.rb | 27 +++ lib/pry/config/default.rb | 61 +++++++ lib/pry/history.rb | 1 + lib/pry/pry_class.rb | 124 +++---------- lib/pry/pry_instance.rb | 114 ++++-------- 8 files changed, 226 insertions(+), 491 deletions(-) create mode 100644 lib/pry/config/convenience.rb create mode 100644 lib/pry/config/default.rb diff --git a/Gemfile b/Gemfile index 11553a13..09471218 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ group :development do gem 'jist' gem 'rb-inotify', :require => 'false' gem 'rb-fsevent', :require => 'false' + gem 'binding.repl' end if RbConfig::CONFIG['ruby_install_name'] == 'rbx' diff --git a/lib/pry.rb b/lib/pry.rb index b5cba74d..a3b3bbbc 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -12,7 +12,6 @@ require 'securerandom' require 'forwardable' 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_| next if _pry_.quiet? @@ -122,65 +121,6 @@ class Pry # This is to keep from breaking under Rails 3.2 for people who are doing that # IRB = Pry thing. module ExtendCommandBundle; end - - # class accessors - # define class attributes before pry library is required - # fix initialize step of Pry (#1037) - class << self - extend Forwardable - - # convenience method - def self.delegate_accessors(delagatee, *names) - def_delegators delagatee, *names - def_delegators delagatee, *names.map { |v| "#{v}=" } - end - - # Get/Set the Proc that defines extra Readline completions (on top - # of the ones defined for IRB). - # @return [Proc] The Proc that defines extra Readline completions (on top - # @example Add file names to completion list - # Pry.custom_completions = proc { Dir.entries('.') } - attr_accessor :custom_completions - - # @return [Fixnum] The current input line. - attr_accessor :current_line - - # @return [Array] The Array of evaluated expressions. - attr_accessor :line_buffer - - # @return [String] The __FILE__ for the `eval()`. Should be "(pry)" - # by default. - attr_accessor :eval_path - - # @return [OpenStruct] Return Pry's config object. - attr_accessor :config - - # @return [History] Return Pry's line history object. - attr_accessor :history - - # @return [Boolean] Whether Pry was activated from the command line. - attr_accessor :cli - - # @return [Boolean] Whether Pry sessions are quiet by default. - attr_accessor :quiet - - # @return [Exception, nil] The last pry internal error. - # (a CommandError in most cases) - attr_accessor :last_internal_error - - # plugin forwardables - def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins - - delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler, - :hooks, :color, :pager, :editor, :memory_size, :extra_sticky_locals - end -end - -if Pry::Helpers::BaseHelpers.mri_18? - begin - require 'ruby18_source_location' - rescue LoadError - end end require 'method_source' diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 74e270fb..6ac8f6f6 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,260 +1,99 @@ -require 'ostruct' +class Pry::Config + require 'ostruct' + require 'pry/config/default' + require 'pry/config/convenience' -class Pry - class Config < OpenStruct + def self.shortcuts + Convenience::SHORTCUTS + end - # Get/Set the object to use for input by default by all Pry instances. - # Pry.config.input is an option determining the input object - the object - # from which Pry retrieves its lines of input. Pry accepts any object that - # implements the readline method. This includes IO objects, StringIO, - # Readline, File and custom objects. - # @return [#readline] The object to use for input by default by all - # Pry instances. - # @example - # Pry.config.input = StringIO.new("@x = 10\nexit") - attr_accessor :input + def initialize(default = Pry.config) + @default = default + @lookup = {} + configure_gist + configure_ls + configure_history + end - # Get/Set the object to use for output by default by all Pry instances. - # Pry.config.output is an option determining the output object - the object - # to which Pry writes its output. Pry accepts any object that implements the - # puts method. This includes IO objects, StringIO, File and custom objects. - # @return [#puts] The object to use for output by default by all - # Pry instances. - # @example - # Pry.config.output = StringIO.new - attr_accessor :output + def [](key) + @lookup[key] + end - # Get/Set the object to use for commands by default by all Pry instances. - # @return [Pry::CommandBase] The object to use for commands by default by - # all Pry instances. - # @example - # Pry.config.commands = Pry::CommandSet.new do - # import_from Pry::Commands, "ls" - # command "greet" do |name| - # output.puts "hello #{name}" - # end - # end - attr_accessor :commands + def []=(key, value) + @lookup[key] = value + end - # Get/Set the Proc to use for printing by default by all Pry - # instances. - # Two parameters are passed to the print Proc: these are (1) the - # output object for the current session and (2) the expression value to - # print. It is important that you write to the output object instead of just - # stdout so that all Pry output can be redirected if necessary. This is the - # 'print' component of the REPL. - # @return [Proc] The Proc to use for printing by default by all - # Pry instances. - # @example - # Pry.config.print = proc { |output, value| output.puts "=> #{value.inspect}" } - attr_accessor :print - - # Pry.config.exception_handler is an option determining the exception - # handler object - the Proc responsible for dealing with exceptions raised - # by user input to the REPL. Three parameters are passed to the exception - # handler Proc: these are (1) the output object for the current session, (2) - # the exception object that was raised inside the Pry session, and (3) a - # reference to the associated Pry instance. - # @return [Proc] The Proc to use for printing exceptions by default by all - # Pry instances. - # @example - # Pry.config.exception_handler = proc do |output, exception, _| - # output.puts "#{exception.class}: #{exception.message}" - # output.puts "from #{exception.backtrace.first}" - # end - attr_accessor :exception_handler - - # @return [Array] The classes of exception that will not be caught by Pry. - # @example - # Pry.config.exception_whitelist = [SystemExit, SignalException] - attr_accessor :exception_whitelist - - # Send Pry into interactive mode after finishing execution - # @return [Boolean] - attr_accessor :exit_interactive - - # @return [Fixnum] The number of lines of context to show before and after - # exceptions, etc. - # @example - # Pry.config.default_window_size = 10 - attr_accessor :default_window_size - - # Get/Set the `Pry::Hooks` instance that defines Pry hooks used by default - # by all Pry instances. - # @return [Pry::Hooks] The hooks used by default by all Pry instances. - # @example - # Pry.config.hooks = Pry::Hooks.new.add_hook(:before_session, - # :default) { |output, target, _pry_| output.puts "Good morning!" } - attr_reader :hooks - - # FIXME: - # This is a hack to alert people of the new API. - # @param [Pry::Hooks] v Only accept `Pry::Hooks` now! - def hooks=(v) - if v.is_a?(Hash) - warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks" - @hooks = Pry::Hooks.from_hash(v) - else - @hooks = v - end + def method_missing(name, *args, &block) + key = name.to_s + if @lookup.has_key?(key) + @lookup[key] + elsif @default.respond_to?(name) + @default.public_send(name, *args, &block) + elsif key[-1] == "=" + short_key = key.to_s[0..-2] + @lookup[short_key] = args[0] + else + nil end + end - # Get the array of Procs (or single Proc) to be used for the prompts by - # default by all Pry instances. Three parameters are passed into the prompt - # procs, (1) the object that is the target of the session, (2) the current - # nesting level, and (3) a reference to the associated Pry instance. These - # objects can be used in the prompt, if desired. - # @return [Array, Proc] The array of Procs to be used for the - # prompts by default by all Pry instances. - # @example - # Pry.config.prompt = proc { |obj, nest_level, _pry_| "#{obj}:#{nest_level}> " } - attr_accessor :prompt + def merge!(other) + @lookup.merge!(other.to_h) + end - # The display name that is part of the prompt. Default is 'pry'. You can - # set your own name so you can identify which project your current pry - # session is using. This is useful if you have a local pryrc file in a - # Rails project for example. - # @return [String] - # @example - # Pry.config.prompt_name = 'my_rails_project' - attr_accessor :prompt_name + def respond_to?(name, boolean=false) + @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) + end - # The list of safe objects, the `#inspect` method of which can be used for - # the prompt. The default safe objects are defined in - # `DEFAULT_PROMPT_SAFE_OBJECTS` (see Pry::DEFAULT_PROMPT_SAFE_OBJECTS). - # @return [Array] - # @example - # class Barbie - # def inspect - # 'You can brush my hair, undress me everywhere!' - # end - # end - # - # Pry.config.prompt_safe_objects << Barbie - attr_accessor :prompt_safe_objects + def to_h + @lookup + end - # The default editor to use. Defaults to $VISUAL, $EDITOR, or a sensible - # fallback for the platform. If `editor` is a String then that string is - # used as the shell command to invoke the editor. If `editor` is callable - # (e.g a Proc) then `file`, `line`, and `reloading` are passed in as - # parameters and the return value of that callable invocation is used as the - # exact shell command to invoke the editor. `reloading` indicates whether - # Pry will be reloading code after the shell command returns. Any or all of - # these parameters can be omitted from the callable's signature. - # @example String - # Pry.config.editor = "emacsclient" - # @example Callable - # Pry.config.editor = proc { |file, line| "emacsclient #{file} +#{line}" } - # @example Callable waiting only if reloading - # Pry.config.editor = proc { |file, line, reloading| - # "subl #{'--wait' if reloading} #{file}:#{line}" - # } - # @return [String, #call] - attr_accessor :editor + def quiet? + quiet + end - # A string that must precede all Pry commands (e.g., if command_prefix is - # set to "%", the "cd" command must be invoked as "%cd"). - # @return [String] - attr_accessor :command_prefix +private + # TODO: + # all of this configure_* stuff is a relic of old code. + # we should try move this code to being command-local. + def configure_ls + @lookup["ls"] = OpenStruct.new({ + :heading_color => :bright_blue, + :public_method_color => :default, + :private_method_color => :blue, + :protected_method_color => :blue, + :method_missing_color => :bright_red, + :local_var_color => :yellow, + :pry_var_color => :default, # e.g. _, _pry_, _file_ + :instance_var_color => :blue, # e.g. @foo + :class_var_color => :bright_blue, # e.g. @@foo + :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library + :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID + :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO + :constant_color => :default, # e.g. VERSION, ARGF + :class_constant_color => :blue, # e.g. Object, Kernel + :exception_constant_color => :magenta, # e.g. Exception, RuntimeError + :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state + :separator => " ", + :ceiling => [Object, Module, Class] + }) + end - # @return [Boolean] Toggle Pry color on and off. - attr_accessor :color + def configure_gist + @lookup["gist"] = OpenStruct.new + gist.inspecter = proc(&:pretty_inspect) + end - # @return [Boolean] Toggle paging on and off. - attr_accessor :pager - - # Determines whether the rc file (~/.pryrc) should be loaded. - # @return [Boolean] - attr_accessor :should_load_rc - - # Determines whether the local rc file (./.pryrc) should be loaded. - # @return [Boolean] - attr_accessor :should_load_local_rc - - # Determines whether plugins should be loaded. - # @return [Boolean] - attr_accessor :should_load_plugins - - # Determines whether to load files specified with the -r flag. - # @return [Boolean] - attr_accessor :should_load_requires - - # Determines whether to disable edit-method's auto-reloading behavior. - # @return [Boolean] - attr_accessor :disable_auto_reload - - # Determines whether Pry should trap SIGINT and cause it to raise an - # Interrupt exception. This is only useful on jruby, MRI does this - # for us. - # @return [Boolean] - attr_accessor :should_trap_interrupts - - # Config option for history. - # sub-options include history.file, history.load, and history.save - # history.file is the file to save/load history to, e.g - # Pry.config.history.file = "~/.pry_history". - # history.should_load is a boolean that determines whether history will be - # loaded from history.file at session start. - # history.should_save is a boolean that determines whether history will be - # saved to history.file at session end. - # @return [OpenStruct] - attr_accessor :history - - # Config option for plugins: - # sub-options include: - # `plugins.strict_loading` (Boolean) which toggles whether referring to a - # non-existent plugin should raise an exception (defaults to `false`). - # @return [OpenStruct] - attr_accessor :plugins - - # @return [Array] Ruby files to be required after loading - # any plugins. - attr_accessor :requires - - # @return [Integer] Amount of results that will be stored into out - attr_accessor :memory_size - - # @return [Proc] The proc that manages ^D presses in the REPL. - # The proc is passed the current eval_string and the current pry instance. - attr_accessor :control_d_handler - - # @return [Proc] The proc that runs system commands - # The proc is passed the pry output object, the command string - # to eval, and a reference to the pry instance - attr_accessor :system - - # @return [Boolean] Whether or not code should be indented - # using Pry::Indent. - attr_accessor :auto_indent - - # @return [Boolean] Whether or not indentation should be corrected - # after hitting enter. This feature is not supported by all terminals. - attr_accessor :correct_indent - - # @return [Boolean] Whether or not a warning will be displayed when - # a command name collides with a method/local in the current context. - attr_accessor :collision_warning - - # Config option for gist. - # sub-options include `gist.inspecter`, - # `gist.inspecter` is a callable that defines how the expression output - # will be displayed when using the `gist -i` command. - # @example Pretty inspect output - # Pry.config.gist.inspecter = proc { |v| v.pretty_inspect } - # @example Regular inspect - # Pry.config.gist.inspecter = proc &:inspect - # @return [OpenStruct] - attr_accessor :gist - - # @return [Hash] Additional sticky locals (to the standard ones) to use in - # Pry sessions. - # @example Inject `random_number` sticky local into Pry session - # Pry.config.extra_sticky_locals = { :random_number => proc { - # rand(10) } } - attr_accessor :extra_sticky_locals - - # @return [#build_completion_proc] A completer to use. - attr_accessor :completer + def configure_history + @lookup["history"] = OpenStruct.new + history.should_save = true + history.should_load = true + history.file = File.expand_path("~/.pry_history") rescue nil + if history.file.nil? + self.should_load_rc = false + history.should_save = false + history.should_load = false + end end end - diff --git a/lib/pry/config/convenience.rb b/lib/pry/config/convenience.rb new file mode 100644 index 00000000..b814c773 --- /dev/null +++ b/lib/pry/config/convenience.rb @@ -0,0 +1,27 @@ +module Pry::Config::Convenience + SHORTCUTS = [ + :input, + :output, + :commands, + :prompt, + :print, + :exception_handler, + :quiet?, + :hooks, + :color, + :pager, + :editor, + :memory_size, + :extra_sticky_locals + ] + + + def config_shortcut(*names) + names.each do |name| + reader = name + setter = "#{name}=" + define_method(reader) { config.public_send(name) } + define_method(setter) { |value| config.public_send(setter, value) } + end + end +end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb new file mode 100644 index 00000000..87e16e06 --- /dev/null +++ b/lib/pry/config/default.rb @@ -0,0 +1,61 @@ +class Pry::Config::Default < Pry::Config + STATE = { + :input => proc { Readline }, + :output => proc { $stdout }, + :commands => proc { Pry::Commands }, + :prompt_name => proc { Pry::DEFAULT_PROMPT_NAME }, + :prompt => proc { Pry::DEFAULT_PROMPT }, + :prompt_safe_objects => proc { Pry::DEFAULT_PROMPT_SAFE_OBJECTS }, + :print => proc { Pry::DEFAULT_PRINT }, + :quiet => proc { false }, + :exception_handler => proc { Pry::DEFAULT_EXCEPTION_HANDLER }, + :exception_whitelist => proc { Pry::DEFAULT_EXCEPTION_WHITELIST }, + :hooks => proc { Pry::DEFAULT_HOOKS }, + :pager => proc { true }, + :system => proc { Pry::DEFAULT_SYSTEM }, + :color => proc { Pry::Helpers::BaseHelpers.use_ansi_codes? }, + :default_window_size => proc { 5 }, + :editor => proc { Pry.default_editor_for_platform }, # TODO: Pry::Platform.editor + :should_load_rc => proc { true }, + :should_load_local_rc => proc { true }, + :should_trap_interrupts => proc { Pry::Helpers::BaseHelpers.jruby? }, # TODO: Pry::Platform.jruby? + :disable_auto_reload => proc { false }, + :command_prefix => proc { "" }, + :auto_indent => proc { Pry::Helpers::BaseHelpers.use_ansi_codes? }, + :correct_indent => proc { true }, + :collision_warning => proc { true }, + :output_prefix => proc { "=> "}, + :requires => proc { [] }, + :should_load_requires => proc { true }, + :should_load_plugins => proc { true }, + :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, + :memory_size => proc { 100 }, + :extra_sticky_locals => proc { {} }, + :sticky_locals => proc { |pry| + { _in_: pry.input_array, + _out_: pry.output_array, + _pry_: pry, + _ex_: pry.last_exception, + _file_: pry.last_file, + _dir_: pry.last_dir, + _: pry.last_result, + __: pry.output_array[-2] + } + }, + :completer => proc { + if defined?(Bond) && Readline::VERSION !~ /editline/i + Pry::BondCompleter.start + else + Pry::InputCompleter.start + end + } + }.freeze + + def initialize(*) + super(nil) + end + + STATE.each do |key, value| + define_method(key, &value) + end +end diff --git a/lib/pry/history.rb b/lib/pry/history.rb index 4df275da..571a9318 100644 --- a/lib/pry/history.rb +++ b/lib/pry/history.rb @@ -2,6 +2,7 @@ class Pry # The History class is responsible for maintaining the user's input history, # both internally and within Readline. class History + attr_accessor :loader, :saver, :pusher, :clearer # @return [Fixnum] Number of lines in history when Pry first loaded. diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 303a5a5e..79ded310 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -1,12 +1,30 @@ -require 'ostruct' require 'pry/config' - class Pry - # The RC Files to load. HOME_RC_FILE = ENV["PRYRC"] || "~/.pryrc" LOCAL_RC_FILE = "./.pryrc" + class << self + extend Forwardable + attr_accessor :custom_completions + attr_accessor :current_line + attr_accessor :line_buffer + attr_accessor :eval_path + attr_accessor :history + attr_accessor :cli + attr_accessor :quiet + attr_accessor :last_internal_error + + def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins + + extend Pry::Config::Convenience + config_shortcut *Pry::Config.shortcuts + + def config + @config ||= Pry::Config::Default.new + end + end + # @return [Hash] Pry's `Thread.current` hash def self.current Thread.current[:__pry__] ||= {} @@ -62,7 +80,6 @@ class Pry # Including: loading .pryrc, loading plugins, loading requires, and # loading history. def self.initial_session_setup - return unless initial_session? @initial_session = false @@ -189,7 +206,7 @@ class Pry def self.default_editor_for_platform return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty? return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty? - +o if Helpers::BaseHelpers.windows? 'notepad' else @@ -223,104 +240,9 @@ Readline version #{ver} detected - will not auto_resize! correctly. end end - def self.set_config_defaults - config.input = Readline - config.output = $stdout - config.commands = Pry::Commands - config.prompt_name = DEFAULT_PROMPT_NAME - config.prompt = DEFAULT_PROMPT - config.prompt_safe_objects = DEFAULT_PROMPT_SAFE_OBJECTS - config.print = DEFAULT_PRINT - config.exception_handler = DEFAULT_EXCEPTION_HANDLER - config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST - config.default_window_size = 5 - config.hooks = DEFAULT_HOOKS - config.color = Helpers::BaseHelpers.use_ansi_codes? - config.pager = true - config.system = DEFAULT_SYSTEM - config.editor = default_editor_for_platform - config.should_load_rc = true - config.should_load_local_rc = true - config.should_trap_interrupts = Helpers::BaseHelpers.jruby? - config.disable_auto_reload = false - config.command_prefix = "" - config.auto_indent = Helpers::BaseHelpers.use_ansi_codes? - config.correct_indent = true - config.collision_warning = false - config.output_prefix = "=> " - - if defined?(Bond) && Readline::VERSION !~ /editline/i - config.completer = Pry::BondCompleter.start - else - config.completer = Pry::InputCompleter.start - end - - config.gist ||= OpenStruct.new - config.gist.inspecter = proc(&:pretty_inspect) - - config.should_load_plugins = true - - config.requires ||= [] - config.should_load_requires = true - - config.history ||= OpenStruct.new - config.history.should_save = true - config.history.should_load = true - config.history.file = File.expand_path("~/.pry_history") rescue nil - - if config.history.file.nil? - config.should_load_rc = false - config.history.should_save = false - config.history.should_load = false - end - - config.control_d_handler = DEFAULT_CONTROL_D_HANDLER - - config.memory_size = 100 - - config.extra_sticky_locals = {} - - config.ls ||= OpenStruct.new({ - :heading_color => :bright_blue, - - :public_method_color => :default, - :private_method_color => :blue, - :protected_method_color => :blue, - :method_missing_color => :bright_red, - - :local_var_color => :yellow, - :pry_var_color => :default, # e.g. _, _pry_, _file_ - - :instance_var_color => :blue, # e.g. @foo - :class_var_color => :bright_blue, # e.g. @@foo - - :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library - :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID - :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO - - :constant_color => :default, # e.g. VERSION, ARGF - :class_constant_color => :blue, # e.g. Object, Kernel - :exception_constant_color => :magenta, # e.g. Exception, RuntimeError - :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state - - # What should separate items listed by ls? - :separator => " ", - - # Any methods defined on these classes, or modules included into these classes, will not - # be shown by ls unless the -v flag is used. - # A user of Rails may wih to add ActiveRecord::Base to the list. - # add the following to your .pryrc: - # Pry.config.ls.ceiling << ActiveRecord::Base if defined? ActiveRecordBase - :ceiling => [Object, Module, Class] - }) - end - # Set all the configurable options back to their default values def self.reset_defaults - set_config_defaults - @initial_session = true - self.custom_completions = DEFAULT_CUSTOM_COMPLETIONS self.cli = false self.current_line = 1 @@ -331,9 +253,7 @@ Readline version #{ver} detected - will not auto_resize! correctly. # Basic initialization. def self.init @plugin_manager ||= PluginManager.new - self.config ||= Config.new self.history ||= History.new - reset_defaults locate_plugins end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index f540b06f..5b79f63a 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- require "pry/indent" ## @@ -21,54 +22,26 @@ require "pry/indent" # * https://github.com/pry/pry # * the IRC channel, which is #pry on the Freenode network # + class Pry - attr_accessor :input - attr_accessor :output - attr_accessor :commands - attr_accessor :print - attr_accessor :exception_handler - attr_accessor :quiet - alias :quiet? :quiet - - attr_accessor :custom_completions - attr_accessor :binding_stack attr_accessor :eval_string - + attr_accessor :backtrace + attr_accessor :suppress_output attr_accessor :last_result attr_accessor :last_file attr_accessor :last_dir attr_reader :last_exception - + attr_reader :command_state + attr_reader :exit_value attr_reader :input_array attr_reader :output_array + attr_reader :hooks + attr_reader :config - attr_accessor :backtrace - - attr_accessor :extra_sticky_locals - - attr_accessor :suppress_output - - # This is exposed via Pry::Command#state. - attr_reader :command_state - - attr_reader :exit_value - - attr_reader :hooks # Special treatment as we want to alert people of the - # changed API. - - # FIXME: This is a hack to alert people of the new API. - # @param [Pry::Hooks] hooks - def hooks=(hooks) - if hooks.is_a?(Hash) - warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object " \ - "instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks" - @hooks = Pry::Hooks.from_hash(hooks) - else - @hooks = hooks - end - end + extend Pry::Config::Convenience + config_shortcut *Pry::Config.shortcuts # Create a new {Pry} instance. # @param [Hash] options @@ -96,39 +69,28 @@ class Pry @command_state = {} @eval_string = "" @backtrace = options[:backtrace] || caller - - refresh_config(options) + @config = Pry::Config.new + @config.merge!(options) + @input_array = Pry::HistoryArray.new config.memory_size + @output_array = Pry::HistoryArray.new config.memory_size push_initial_binding(options[:target]) - set_last_result nil @input_array << nil # add empty input so _in_ and _out_ match - # yield the binding_stack to the hook for modification exec_hook(:when_started, options[:target], options, self) end - # Refresh the Pry instance settings from the Pry class. - # Allows options to be specified to override settings from Pry class. - # @param [Hash] options The options to override Pry class settings - # for this instance. - def refresh_config(options={}) - defaults = {} - attributes = [ - :input, :output, :commands, :print, :quiet, - :exception_handler, :hooks, :custom_completions, - :prompt, :memory_size, :extra_sticky_locals - ] - - attributes.each do |attribute| - defaults[attribute] = Pry.send attribute + # FIXME: This is a hack to alert people of the new API. + # @param [Pry::Hooks] hooks + def hooks=(hooks) + if hooks.is_a?(Hash) + warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object " \ + "instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks" + @hooks = Pry::Hooks.from_hash(hooks) + else + @hooks = hooks end - - defaults.merge!(options).each do |key, value| - send("#{key}=", value) if respond_to?("#{key}=") - end - - true end # Initialize this instance by pushing its initial context into the binding @@ -137,6 +99,10 @@ class Pry push_binding(target || Pry.toplevel_binding) end + def hooks + config.hooks + end + # The currently active `Binding`. # @return [Binding] The currently active `Binding` for the session. def current_binding @@ -151,18 +117,6 @@ class Pry binding_stack << Pry.binding_for(object) end - # The current prompt. - # This is the prompt at the top of the prompt stack. - # - # @example - # self.prompt = Pry::SIMPLE_PROMPT - # self.prompt # => Pry::SIMPLE_PROMPT - # - # @return [Array] Current prompt. - def prompt - prompt_stack.last - end - def prompt=(new_prompt) if prompt_stack.empty? push_prompt new_prompt @@ -221,18 +175,8 @@ class Pry sticky_locals[name] = block end - # @return [Hash] The currently defined sticky locals. def sticky_locals - @sticky_locals ||= { - :_in_ => proc { @input_array }, - :_out_ => proc { @output_array }, - :_pry_ => self, - :_ex_ => proc { last_exception }, - :_file_ => proc { last_file }, - :_dir_ => proc { last_dir }, - :_ => proc { last_result }, - :__ => proc { @output_array[-2] } - }.merge(extra_sticky_locals) + config.sticky_locals(self) end # Reset the current eval string. If the user has entered part of a multiline @@ -676,4 +620,6 @@ class Pry end def raise_up(*args); raise_up_common(false, *args); end def raise_up!(*args); raise_up_common(true, *args); end + +private end From 2abeaba6011f6f8eba632c988c422c2985ddd8df Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 03:37:57 +0100 Subject: [PATCH 032/186] add Pry::Config#refresh. --- lib/pry/config.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 6ac8f6f6..6c24ee42 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -45,6 +45,10 @@ class Pry::Config @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) end + def refresh + @lookup = {} + end + def to_h @lookup end From 358395435b926d14925336a43645b0624371bfbc Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 03:43:37 +0100 Subject: [PATCH 033/186] update Gemfile --- Gemfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Gemfile b/Gemfile index 09471218..11553a13 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,6 @@ group :development do gem 'jist' gem 'rb-inotify', :require => 'false' gem 'rb-fsevent', :require => 'false' - gem 'binding.repl' end if RbConfig::CONFIG['ruby_install_name'] == 'rbx' From f4462a48cc4b2c23d896f84b70ba233f93629397 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 04:12:39 +0100 Subject: [PATCH 034/186] look for a setter before anything else in Pry::Config#method_missing(). --- lib/pry/config.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 6c24ee42..32b559cb 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -25,13 +25,13 @@ class Pry::Config def method_missing(name, *args, &block) key = name.to_s - if @lookup.has_key?(key) + if key[-1] == "=" + short_key = key.to_s[0..-2] + @lookup[short_key] = args[0] + elsif @lookup.has_key?(key) @lookup[key] elsif @default.respond_to?(name) @default.public_send(name, *args, &block) - elsif key[-1] == "=" - short_key = key.to_s[0..-2] - @lookup[short_key] = args[0] else nil end From c1af234bbd972d9147c3a6c2a2a23c9f6c4bbe9e Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 04:21:22 +0100 Subject: [PATCH 035/186] add Pry::Config#to_hash for 1.9 compatibility. --- lib/pry/config.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 32b559cb..4e2a2f7e 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -38,7 +38,7 @@ class Pry::Config end def merge!(other) - @lookup.merge!(other.to_h) + @lookup.merge!(other.to_hash) end def respond_to?(name, boolean=false) @@ -49,10 +49,14 @@ class Pry::Config @lookup = {} end - def to_h + def to_hash @lookup end + def to_h + to_hash + end + def quiet? quiet end From fc74f8f1dc26f3961bef3762b497b4ae8ba5d530 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 08:40:04 +0100 Subject: [PATCH 036/186] empty Config::Default::STATE when default methods have been defined. --- lib/pry/config/default.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 87e16e06..e1c01534 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -49,13 +49,12 @@ class Pry::Config::Default < Pry::Config Pry::InputCompleter.start end } - }.freeze + } def initialize(*) super(nil) end - STATE.each do |key, value| - define_method(key, &value) - end + STATE.each { |key, value| define_method(key, &value) } + STATE.clear end From 64843e27ad6889a4294e8219042dbf11350d0680 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 17:42:35 +0100 Subject: [PATCH 037/186] rename 'STATE' as 'state' to communicate its temporary nature. --- lib/pry/config/default.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index e1c01534..d6ac9257 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,5 +1,5 @@ class Pry::Config::Default < Pry::Config - STATE = { + state = { :input => proc { Readline }, :output => proc { $stdout }, :commands => proc { Pry::Commands }, @@ -55,6 +55,5 @@ class Pry::Config::Default < Pry::Config super(nil) end - STATE.each { |key, value| define_method(key, &value) } - STATE.clear + state.each { |key, value| define_method(key, &value) } end From f9f96ce1a87d30f05c6da33e694af3edaedb6317 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 18:26:54 +0100 Subject: [PATCH 038/186] define Pry.config as instance of Pry::Config. --- lib/pry/pry_class.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index cfd0d3cd..28721b72 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -22,7 +22,7 @@ class Pry end def self.config - @config ||= Pry::Config::Default.new + @config ||= Pry::Config.new(Pry::Config::Default.new) end def self.main From 4f155043509f05b14d7756e6a8e0096632876484 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 19 Jan 2014 19:28:09 +0100 Subject: [PATCH 039/186] remove Pry#hooks (use Pry.config.hooks or _pry_.config.hooks) --- lib/pry/pry_instance.rb | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 5b79f63a..8c22068f 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -37,7 +37,6 @@ class Pry attr_reader :exit_value attr_reader :input_array attr_reader :output_array - attr_reader :hooks attr_reader :config extend Pry::Config::Convenience @@ -81,28 +80,12 @@ class Pry exec_hook(:when_started, options[:target], options, self) end - # FIXME: This is a hack to alert people of the new API. - # @param [Pry::Hooks] hooks - def hooks=(hooks) - if hooks.is_a?(Hash) - warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object " \ - "instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks" - @hooks = Pry::Hooks.from_hash(hooks) - else - @hooks = hooks - end - end - # Initialize this instance by pushing its initial context into the binding # stack. If no target is given, start at the top level. def push_initial_binding(target=nil) push_binding(target || Pry.toplevel_binding) end - def hooks - config.hooks - end - # The currently active `Binding`. # @return [Binding] The currently active `Binding` for the session. def current_binding From 3bbe0f0c32cb46e4effbd764599b4196f9d53bec Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Wed, 23 Oct 2013 22:58:26 -0700 Subject: [PATCH 040/186] Remove support for 1.8-compatible versions of Ruby --- lib/pry.rb | 7 - lib/pry/commands/find_method.rb | 6 - lib/pry/commands/show_info.rb | 5 - lib/pry/core_extensions.rb | 60 +- lib/pry/helpers/base_helpers.rb | 4 - lib/pry/hooks.rb | 20 +- lib/pry/method.rb | 2 - lib/pry/terminal.rb | 3 +- lib/pry/test/helper.rb | 4 - lib/pry/wrapped_module.rb | 36 +- spec/command_spec.rb | 6 +- spec/commands/find_method_spec.rb | 105 ++- spec/commands/gem_list_spec.rb | 1 - spec/commands/ls_spec.rb | 20 +- spec/commands/show_doc_spec.rb | 968 ++++++++++---------- spec/commands/show_source_spec.rb | 1360 ++++++++++++++--------------- spec/commands/whereami_spec.rb | 16 +- spec/method_spec.rb | 30 +- spec/pry_spec.rb | 26 +- spec/run_command_spec.rb | 10 +- 20 files changed, 1291 insertions(+), 1398 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index c558b3df..626f46fa 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -176,13 +176,6 @@ class Pry end end -if Pry::Helpers::BaseHelpers.mri_18? - begin - require 'ruby18_source_location' - rescue LoadError - end -end - require 'method_source' require 'shellwords' require 'stringio' diff --git a/lib/pry/commands/find_method.rb b/lib/pry/commands/find_method.rb index a6d024e7..da9c85d3 100644 --- a/lib/pry/commands/find_method.rb +++ b/lib/pry/commands/find_method.rb @@ -6,8 +6,6 @@ class Pry group 'Context' description 'Recursively search for a method within a Class/Module or the current namespace.' command_options :shellwords => false - command_options :requires_gem => 'ruby18_source_location' if mri_18? - banner <<-'BANNER' Usage: find-method [-n|-c] METHOD [NAMESPACE] @@ -26,10 +24,6 @@ class Pry find-method -c 'output.puts' Pry BANNER - def setup - require 'ruby18_source_location' if mri_18? - end - def options(opti) opti.on :n, :name, "Search for a method by name" opti.on :c, :content, "Search for a method based on content in Regex form" diff --git a/lib/pry/commands/show_info.rb b/lib/pry/commands/show_info.rb index 722e656d..6dbc0cd6 100644 --- a/lib/pry/commands/show_info.rb +++ b/lib/pry/commands/show_info.rb @@ -3,11 +3,6 @@ class Pry extend Pry::Helpers::BaseHelpers command_options :shellwords => false, :interpolate => false - command_options :requires_gem => "ruby18_source_location" if mri_18? - - def setup - require 'ruby18_source_location' if mri_18? - end def options(opt) opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors", :as => :count diff --git a/lib/pry/core_extensions.rb b/lib/pry/core_extensions.rb index 7b4b33b2..a418daf3 100644 --- a/lib/pry/core_extensions.rb +++ b/lib/pry/core_extensions.rb @@ -99,37 +99,33 @@ class Object end end -if defined?(BasicObject) - class BasicObject - # Return a binding object for the receiver. - # - # The `self` of the binding is set to the current object, and it contains no - # local variables. - # - # The default definee (http://yugui.jp/articles/846) is set such that new - # methods defined will be added to the singleton class of the BasicObject. - # - # @return [Binding] - def __binding__ - # BasicObjects don't have respond_to?, so we just define the method - # every time. As they also don't have `.freeze`, this call won't - # fail as it can for normal Objects. - (class << self; self; end).class_eval <<-EOF, __FILE__, __LINE__ + 1 - # Get a binding with 'self' set to self, and no locals. - # - # The default definee is determined by the context in which the - # definition is eval'd. - # - # Please don't call this method directly, see {__binding__}. - # - # @return [Binding] - def __pry__ - # In ruby-1.8.7 ::Kernel.binding sets self to Kernel in the returned binding. - # Luckily ruby-1.8.7 doesn't have BasicObject, so this is safe. - ::Kernel.binding - end - EOF - self.__pry__ - end +class BasicObject + # Return a binding object for the receiver. + # + # The `self` of the binding is set to the current object, and it contains no + # local variables. + # + # The default definee (http://yugui.jp/articles/846) is set such that new + # methods defined will be added to the singleton class of the BasicObject. + # + # @return [Binding] + def __binding__ + # BasicObjects don't have respond_to?, so we just define the method + # every time. As they also don't have `.freeze`, this call won't + # fail as it can for normal Objects. + (class << self; self; end).class_eval <<-EOF, __FILE__, __LINE__ + 1 + # Get a binding with 'self' set to self, and no locals. + # + # The default definee is determined by the context in which the + # definition is eval'd. + # + # Please don't call this method directly, see {__binding__}. + # + # @return [Binding] + def __pry__ + ::Kernel.binding + end + EOF + self.__pry__ end end diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index df831a0d..cc447701 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -90,10 +90,6 @@ class Pry RbConfig::CONFIG['ruby_install_name'] == 'ruby' end - def mri_18? - mri? && RUBY_VERSION =~ /1.8/ - end - def mri_19? mri? && RUBY_VERSION =~ /1.9/ end diff --git a/lib/pry/hooks.rb b/lib/pry/hooks.rb index ca48ad67..f234ab5a 100644 --- a/lib/pry/hooks.rb +++ b/lib/pry/hooks.rb @@ -146,18 +146,14 @@ class Pry def exec_hook(event_name, *args, &block) @hooks[event_name] ||= [] - # silence warnings to get rid of 1.8's "warning: multiple values - # for a block parameter" warnings - Pry::Helpers::BaseHelpers.silence_warnings do - @hooks[event_name].map do |hook_name, callable| - begin - callable.call(*args, &block) - rescue RescuableException => e - errors << e - e - end - end.last - end + @hooks[event_name].map do |hook_name, callable| + begin + callable.call(*args, &block) + rescue RescuableException => e + errors << e + e + end + end.last end # Return the number of hook functions registered for the `event_name` event. diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 06284420..2b247c57 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -405,8 +405,6 @@ class Pry end # @return [Array] All known aliases for the method. - # @note On Ruby 1.8 this method always returns an empty Array for methods - # implemented in C. def aliases owner = @method.owner # Avoid using `to_sym` on {Method#name}, which returns a `String`, because diff --git a/lib/pry/terminal.rb b/lib/pry/terminal.rb index 10cbcf85..d88716b0 100644 --- a/lib/pry/terminal.rb +++ b/lib/pry/terminal.rb @@ -44,7 +44,8 @@ class Pry::Terminal require 'io/console' $stdout.winsize if $stdout.tty? and $stdout.respond_to?(:winsize) rescue LoadError - # They're probably on 1.8 without the io-console gem. We'll keep trying. + # They probably don't have the io/console stdlib or the io-console gem. + # We'll keep trying. end def screen_size_according_to_env diff --git a/lib/pry/test/helper.rb b/lib/pry/test/helper.rb index 3cf0371d..74d40512 100644 --- a/lib/pry/test/helper.rb +++ b/lib/pry/test/helper.rb @@ -47,10 +47,6 @@ module PryTestHelpers end end - def mri18_and_no_real_source_location? - Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method) - end - # Open a temp file and yield it to the block, closing it after # @return [String] The path of the temp file def temp_file(ext='.rb') diff --git a/lib/pry/wrapped_module.rb b/lib/pry/wrapped_module.rb index abe9e0b8..6022c5df 100644 --- a/lib/pry/wrapped_module.rb +++ b/lib/pry/wrapped_module.rb @@ -66,28 +66,12 @@ class Pry end # Returns an array of the names of the constants accessible in the wrapped - # module. This provides a consistent interface between 1.8 and 1.9 and also - # avoids the problem of accidentally calling the singleton method - # `Module.constants`. + # module. This avoids the problem of accidentally calling the singleton + # method `Module.constants`. # @param [Boolean] inherit Include the names of constants from included # modules? def constants(inherit = true) - method = Module.instance_method(:constants).bind(@wrapped) - - # If we're on 1.8, we have to manually remove ancestors' constants. If - # we're on 1.9, though, it's better to use the built-in `inherit` param, - # since it doesn't do things like incorrectly remove Pry::Config. - if method.arity == 0 - consts = method.call - if !inherit - ancestors_ = Pry::Method.safe_send(@wrapped, :ancestors) - consts -= (ancestors_ - [@wrapped]).map(&:constants).flatten - end - else - consts = method.call(inherit) - end - - consts + Module.instance_method(:constants).bind(@wrapped).call(inherit) end # The prefix that would appear before methods defined on this class. @@ -259,7 +243,7 @@ class Pry # @return [Enumerator, Array] on JRuby 1.9 and higher returns Array, on # other rubies returns Enumerator def candidates - enum = generator.new do |y| + enum = Enumerator.new do |y| (0...number_of_candidates).each do |num| y.yield candidate(num) end @@ -292,18 +276,6 @@ class Pry private - # Ruby 1.8 doesn't support `Enumerator` (it's called Generator instead) - # - # @return [Object] Return the appropriate generator class. - def generator - @generator ||= if defined?(Enumerator) - Enumerator - else - require 'generator' - Generator - end - end - # @return [Pry::WrappedModule::Candidate] The candidate with the # highest rank, that is the 'monkey patch' of this module with the # highest number of methods, which contains a source code line that diff --git a/spec/command_spec.rb b/spec/command_spec.rb index 358d82d9..f89c2a2a 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -664,10 +664,8 @@ describe "Pry::Command" do pry_eval('my---test').should =~ /my-testmy-test/ end - if !mri18_and_no_real_source_location? - it "shows the source of the process method" do - pry_eval('show-source my-test').should =~ /output.puts command_name/ - end + it "shows the source of the process method" do + pry_eval('show-source my-test').should =~ /output.puts command_name/ end describe "command options hash" do diff --git a/spec/commands/find_method_spec.rb b/spec/commands/find_method_spec.rb index 78ee5a78..7991b678 100644 --- a/spec/commands/find_method_spec.rb +++ b/spec/commands/find_method_spec.rb @@ -1,70 +1,63 @@ require 'helper' -# we turn off the test for MRI 1.8 because our source_location hack -# for C methods actually runs the methods - and since it runs ALL -# methods (in an attempt to find a match) it runs 'exit' and aborts -# the test, causing a failure. We should fix this in the future by -# blacklisting certain methods for 1.8 MRI (such as exit, fork, and so on) -unless Pry::Helpers::BaseHelpers.mri_18? - MyKlass = Class.new do - def hello - "timothy" +MyKlass = Class.new do + def hello + "timothy" + end + def goodbye + "jenny" + end + def tea_tim? + "timothy" + end + def tea_time? + "polly" + end +end + +describe "find-method" do + describe "find matching methods by name regex (-n option)" do + it "should find a method by regex" do + pry_eval("find-method hell MyKlass").should =~ + /MyKlass.*?hello/m end - def goodbye - "jenny" - end - def tea_tim? - "timothy" - end - def tea_time? - "polly" + + it "should NOT match a method that does not match the regex" do + pry_eval("find-method hell MyKlass").should.not =~ + /MyKlass.*?goodbye/m end end - describe "find-method" do - describe "find matching methods by name regex (-n option)" do - it "should find a method by regex" do - pry_eval("find-method hell MyKlass").should =~ - /MyKlass.*?hello/m - end - - it "should NOT match a method that does not match the regex" do - pry_eval("find-method hell MyKlass").should.not =~ - /MyKlass.*?goodbye/m - end - end - - describe "find matching methods by content regex (-c option)" do - it "should find a method by regex" do - pry_eval("find-method -c timothy MyKlass").should =~ - /MyKlass.*?hello/m - end - - it "should NOT match a method that does not match the regex" do - pry_eval("find-method timothy MyKlass").should.not =~ - /MyKlass.*?goodbye/m - end - end - - it "should work with badly behaved constants" do - MyKlass::X = Object.new - def (MyKlass::X).hash - raise "mooo" - end - + describe "find matching methods by content regex (-c option)" do + it "should find a method by regex" do pry_eval("find-method -c timothy MyKlass").should =~ /MyKlass.*?hello/m end - it "should escape regexes correctly" do - good = /tea_time\?/ - bad = /tea_tim\?/ - pry_eval('find-method tea_time? MyKlass').should =~ good - pry_eval('find-method tea_time? MyKlass').should =~ good - pry_eval('find-method tea_time\? MyKlass').should.not =~ bad - pry_eval('find-method tea_time\? MyKlass').should =~ good + it "should NOT match a method that does not match the regex" do + pry_eval("find-method timothy MyKlass").should.not =~ + /MyKlass.*?goodbye/m end end - Object.remove_const(:MyKlass) + it "should work with badly behaved constants" do + MyKlass::X = Object.new + def (MyKlass::X).hash + raise "mooo" + end + + pry_eval("find-method -c timothy MyKlass").should =~ + /MyKlass.*?hello/m + end + + it "should escape regexes correctly" do + good = /tea_time\?/ + bad = /tea_tim\?/ + pry_eval('find-method tea_time? MyKlass').should =~ good + pry_eval('find-method tea_time? MyKlass').should =~ good + pry_eval('find-method tea_time\? MyKlass').should.not =~ bad + pry_eval('find-method tea_time\? MyKlass').should =~ good + end end + +Object.remove_const(:MyKlass) diff --git a/spec/commands/gem_list_spec.rb b/spec/commands/gem_list_spec.rb index 7f7c2510..0e630574 100644 --- a/spec/commands/gem_list_spec.rb +++ b/spec/commands/gem_list_spec.rb @@ -1,7 +1,6 @@ require 'helper' describe "gem-list" do - # fixing bug for 1.8 compat it 'should not raise when invoked' do proc { pry_eval(self, 'gem-list') diff --git a/spec/commands/ls_spec.rb b/spec/commands/ls_spec.rb index 7c2b672a..a35d8198 100644 --- a/spec/commands/ls_spec.rb +++ b/spec/commands/ls_spec.rb @@ -34,18 +34,16 @@ describe "ls" do end end - if defined?(BasicObject) - describe "BasicObject" do - it "should work on BasicObject" do - pry_eval("ls BasicObject.new").should =~ /BasicObject#methods:.*__send__/m - end + describe "BasicObject" do + it "should work on BasicObject" do + pry_eval("ls BasicObject.new").should =~ /BasicObject#methods:.*__send__/m + end - it "should work on subclasses of BasicObject" do - pry_eval( - "class LessBasic < BasicObject; def jaroussky; 5; end; end", - "ls LessBasic.new" - ).should =~ /LessBasic#methods:.*jaroussky/m - end + it "should work on subclasses of BasicObject" do + pry_eval( + "class LessBasic < BasicObject; def jaroussky; 5; end; end", + "ls LessBasic.new" + ).should =~ /LessBasic#methods:.*jaroussky/m end end diff --git a/spec/commands/show_doc_spec.rb b/spec/commands/show_doc_spec.rb index f69910e7..16d1757b 100644 --- a/spec/commands/show_doc_spec.rb +++ b/spec/commands/show_doc_spec.rb @@ -1,575 +1,571 @@ require 'helper' require "fixtures/show_source_doc_examples" -if !PryTestHelpers.mri18_and_no_real_source_location? - describe "show-doc" do +describe "show-doc" do + before do + @o = Object.new + + # sample doc + def @o.sample_method + :sample + end + + def @o.no_docs;end + + end + + it 'should output a method\'s documentation' do + pry_eval(binding, "show-doc @o.sample_method").should =~ /sample doc/ + end + + it 'should raise exception when cannot find docs' do + lambda { pry_eval(binding, "show-doc @o.no_docs") }.should.raise(Pry::CommandError) + end + + it 'should output a method\'s documentation with line numbers' do + pry_eval(binding, "show-doc @o.sample_method -l").should =~ /\d: sample doc/ + end + + it 'should output a method\'s documentation with line numbers (base one)' do + pry_eval(binding, "show-doc @o.sample_method -b").should =~ /1: sample doc/ + end + + it 'should output a method\'s documentation if inside method without needing to use method name' do + # sample comment + def @o.sample + pry_eval(binding, 'show-doc').should =~ /sample comment/ + end + @o.sample + end + + describe "finding find super method docs with help of `--super` switch" do before do - @o = Object.new - - # sample doc - def @o.sample_method - :sample + class Daddy + # daddy initialize! + def initialize(*args); end end - def @o.no_docs;end - - end - - it 'should output a method\'s documentation' do - pry_eval(binding, "show-doc @o.sample_method").should =~ /sample doc/ - end - - it 'should raise exception when cannot find docs' do - lambda { pry_eval(binding, "show-doc @o.no_docs") }.should.raise(Pry::CommandError) - end - - it 'should output a method\'s documentation with line numbers' do - pry_eval(binding, "show-doc @o.sample_method -l").should =~ /\d: sample doc/ - end - - it 'should output a method\'s documentation with line numbers (base one)' do - pry_eval(binding, "show-doc @o.sample_method -b").should =~ /1: sample doc/ - end - - it 'should output a method\'s documentation if inside method without needing to use method name' do - # sample comment - def @o.sample - pry_eval(binding, 'show-doc').should =~ /sample comment/ + class Classy < Daddy + # classy initialize! + def initialize(*args); end end - @o.sample + + class Grungy < Classy + # grungy initialize?? + def initialize(*args); end + end + + @o = Grungy.new + + # instancey initialize! + def @o.initialize; end end - describe "finding find super method docs with help of `--super` switch" do - before do - class Daddy - # daddy initialize! - def initialize(*args); end + after do + Object.remove_const(:Grungy) + Object.remove_const(:Classy) + Object.remove_const(:Daddy) + end + + it "finds super method docs" do + output = pry_eval(binding, 'show-doc --super @o.initialize') + output.should =~ /grungy initialize/ + end + + it "traverses ancestor chain and finds super method docs" do + output = pry_eval(binding, 'show-doc -ss @o.initialize') + output.should =~ /classy initialize/ + end + + it "traverses ancestor chain even higher and finds super method doc" do + output = pry_eval(binding, 'show-doc @o.initialize -sss') + output.should =~ /daddy initialize/ + end + + it "finds super method docs without explicit method argument" do + fatty = Grungy.new + + # fatty initialize! + def fatty.initialize + pry_eval(binding, 'show-doc --super') + end + + output = fatty.initialize + output.should =~ /grungy initialize/ + end + + it "finds super method docs without `--super` but with the `super` keyword" do + fatty = Grungy.new + + fatty.extend Module.new { + def initialize + :nibble end + } - class Classy < Daddy - # classy initialize! - def initialize(*args); end + # fatty initialize! + def fatty.initialize + pry_eval(binding, 'show-doc --super --super') + end + + output = fatty.initialize + output.should =~ /grungy initialize/ + end + end + + describe "rdoc highlighting" do + it "should syntax highlight code in rdoc" do + c = Class.new{ + # This can initialize your class: + # + # a = c.new :foo + # + # @param foo + def initialize(foo); end + } + + begin + t = pry_tester(binding) + t.eval("show-doc c#initialize").should =~ /c.new :foo/ + Pry.config.color = true + # I don't want the test to rely on which colour codes are there, just to + # assert that "something" is being colourized. + t.eval("show-doc c#initialize").should.not =~ /c.new :foo/ + ensure + Pry.config.color = false + end + end + + it "should syntax highlight `code` in rdoc" do + c = Class.new{ + # After initializing your class with `c.new(:foo)`, go have fun! + # + # @param foo + def initialize(foo); end + } + + begin + t = pry_tester(binding) + t.eval("show-doc c#initialize").should =~ /c.new\(:foo\)/ + Pry.config.color = true + # I don't want the test to rely on which colour codes are there, just to + # assert that "something" is being colourized. + t.eval("show-doc c#initialize").should.not =~ /c.new\(:foo\)/ + ensure + Pry.config.color = false + end + + end + + it "should not syntax highlight `` inside code" do + c = Class.new{ + # Convert aligned output (from many shell commands) into nested arrays: + # + # a = decolumnize `ls -l $HOME` + # + # @param output + def decolumnize(output); end + } + + begin + t = pry_tester(binding) + Pry.config.color = true + t.eval("show-doc c#decolumnize").should =~ /ls -l \$HOME/ + t.eval("show-doc c#decolumnize").should.not =~ /`ls -l \$HOME`/ + ensure + Pry.config.color = false + end + end + end + + describe "on sourcable objects" do + it "should show documentation for object" do + # this is a documentation + hello = proc { puts 'hello world!' } + mock_pry(binding, "show-doc hello").should =~ /this is a documentation/ + end + end + + describe "on modules" do + before do + # god this is boring1 + class ShowSourceTestClass + def alpha end + end - class Grungy < Classy - # grungy initialize?? - def initialize(*args); end + # god this is boring2 + module ShowSourceTestModule + def alpha end - - @o = Grungy.new - - # instancey initialize! - def @o.initialize; end end - after do - Object.remove_const(:Grungy) - Object.remove_const(:Classy) - Object.remove_const(:Daddy) - end - - it "finds super method docs" do - output = pry_eval(binding, 'show-doc --super @o.initialize') - output.should =~ /grungy initialize/ - end - - it "traverses ancestor chain and finds super method docs" do - output = pry_eval(binding, 'show-doc -ss @o.initialize') - output.should =~ /classy initialize/ - end - - it "traverses ancestor chain even higher and finds super method doc" do - output = pry_eval(binding, 'show-doc @o.initialize -sss') - output.should =~ /daddy initialize/ - end - - it "finds super method docs without explicit method argument" do - fatty = Grungy.new - - # fatty initialize! - def fatty.initialize - pry_eval(binding, 'show-doc --super') + # god this is boring3 + ShowSourceTestClassWeirdSyntax = Class.new do + def beta end - - output = fatty.initialize - output.should =~ /grungy initialize/ end - it "finds super method docs without `--super` but with the `super` keyword" do - fatty = Grungy.new + # god this is boring4 + ShowSourceTestModuleWeirdSyntax = Module.new do + def beta + end + end + end - fatty.extend Module.new { - def initialize - :nibble + after do + Object.remove_const :ShowSourceTestClass + Object.remove_const :ShowSourceTestClassWeirdSyntax + Object.remove_const :ShowSourceTestModule + Object.remove_const :ShowSourceTestModuleWeirdSyntax + end + + describe "basic functionality, should show docs for top-level module definitions" do + it 'should show docs for a class' do + pry_eval("show-doc ShowSourceTestClass").should =~ + /god this is boring1/ + end + + it 'should show docs for a module' do + pry_eval("show-doc ShowSourceTestModule").should =~ + /god this is boring2/ + end + + it 'should show docs for a class when Const = Class.new syntax is used' do + pry_eval("show-doc ShowSourceTestClassWeirdSyntax").should =~ + /god this is boring3/ + end + + it 'should show docs for a module when Const = Module.new syntax is used' do + pry_eval("show-doc ShowSourceTestModuleWeirdSyntax").should =~ + /god this is boring4/ + end + end + + describe "in REPL" do + it 'should find class defined in repl' do + t = pry_tester + t.eval <<-RUBY + # hello tobina + class TobinaMyDog + def woof + end end - } - - # fatty initialize! - def fatty.initialize - pry_eval(binding, 'show-doc --super --super') - end - - output = fatty.initialize - output.should =~ /grungy initialize/ + RUBY + t.eval('show-doc TobinaMyDog').should =~ /hello tobina/ + Object.remove_const :TobinaMyDog end end - describe "rdoc highlighting" do - it "should syntax highlight code in rdoc" do - c = Class.new{ - # This can initialize your class: - # - # a = c.new :foo - # - # @param foo - def initialize(foo); end - } - - begin - t = pry_tester(binding) - t.eval("show-doc c#initialize").should =~ /c.new :foo/ - Pry.config.color = true - # I don't want the test to rely on which colour codes are there, just to - # assert that "something" is being colourized. - t.eval("show-doc c#initialize").should.not =~ /c.new :foo/ - ensure - Pry.config.color = false - end - end - - it "should syntax highlight `code` in rdoc" do - c = Class.new{ - # After initializing your class with `c.new(:foo)`, go have fun! - # - # @param foo - def initialize(foo); end - } - - begin - t = pry_tester(binding) - t.eval("show-doc c#initialize").should =~ /c.new\(:foo\)/ - Pry.config.color = true - # I don't want the test to rely on which colour codes are there, just to - # assert that "something" is being colourized. - t.eval("show-doc c#initialize").should.not =~ /c.new\(:foo\)/ - ensure - Pry.config.color = false - end - - end - - it "should not syntax highlight `` inside code" do - c = Class.new{ - # Convert aligned output (from many shell commands) into nested arrays: - # - # a = decolumnize `ls -l $HOME` - # - # @param output - def decolumnize(output); end - } - - begin - t = pry_tester(binding) - Pry.config.color = true - t.eval("show-doc c#decolumnize").should =~ /ls -l \$HOME/ - t.eval("show-doc c#decolumnize").should.not =~ /`ls -l \$HOME`/ - ensure - Pry.config.color = false - end - end - end - - describe "on sourcable objects" do - it "should show documentation for object" do - # this is a documentation - hello = proc { puts 'hello world!' } - mock_pry(binding, "show-doc hello").should =~ /this is a documentation/ - end - end - - describe "on modules" do - before do - # god this is boring1 - class ShowSourceTestClass + it 'should lookup module name with respect to current context' do + constant_scope(:AlphaClass, :BetaClass) do + # top-level beta + class BetaClass def alpha end end - # god this is boring2 - module ShowSourceTestModule - def alpha - end - end - - # god this is boring3 - ShowSourceTestClassWeirdSyntax = Class.new do - def beta - end - end - - # god this is boring4 - ShowSourceTestModuleWeirdSyntax = Module.new do - def beta - end - end - end - - after do - Object.remove_const :ShowSourceTestClass - Object.remove_const :ShowSourceTestClassWeirdSyntax - Object.remove_const :ShowSourceTestModule - Object.remove_const :ShowSourceTestModuleWeirdSyntax - end - - describe "basic functionality, should show docs for top-level module definitions" do - it 'should show docs for a class' do - pry_eval("show-doc ShowSourceTestClass").should =~ - /god this is boring1/ - end - - it 'should show docs for a module' do - pry_eval("show-doc ShowSourceTestModule").should =~ - /god this is boring2/ - end - - it 'should show docs for a class when Const = Class.new syntax is used' do - pry_eval("show-doc ShowSourceTestClassWeirdSyntax").should =~ - /god this is boring3/ - end - - it 'should show docs for a module when Const = Module.new syntax is used' do - pry_eval("show-doc ShowSourceTestModuleWeirdSyntax").should =~ - /god this is boring4/ - end - end - - if !Pry::Helpers::BaseHelpers.mri_18? - describe "in REPL" do - it 'should find class defined in repl' do - t = pry_tester - t.eval <<-RUBY - # hello tobina - class TobinaMyDog - def woof - end - end - RUBY - t.eval('show-doc TobinaMyDog').should =~ /hello tobina/ - Object.remove_const :TobinaMyDog - end - end - end - - it 'should lookup module name with respect to current context' do - constant_scope(:AlphaClass, :BetaClass) do - # top-level beta + class AlphaClass + # nested beta class BetaClass - def alpha + def beta end end - - class AlphaClass - # nested beta - class BetaClass - def beta - end - end - end - - pry_eval(AlphaClass, "show-doc BetaClass").should =~ /nested beta/ end + + pry_eval(AlphaClass, "show-doc BetaClass").should =~ /nested beta/ + end + end + + it 'should look up nested modules' do + constant_scope(:AlphaClass) do + class AlphaClass + # nested beta + class BetaClass + def beta + end + end + end + + pry_eval("show-doc AlphaClass::BetaClass").should =~ + /nested beta/ + end + end + + describe "show-doc -a" do + it 'should show the docs for all monkeypatches defined in different files' do + # local monkeypatch + class TestClassForShowSource + def beta + end + end + + result = pry_eval("show-doc TestClassForShowSource -a") + result.should =~ /used by/ + result.should =~ /local monkeypatch/ end - it 'should look up nested modules' do - constant_scope(:AlphaClass) do - class AlphaClass - # nested beta - class BetaClass - def beta - end + describe "messages relating to -a" do + it "displays the original definition by default (not a doc of a monkeypatch)" do + class TestClassForCandidatesOrder + def beta end end - pry_eval("show-doc AlphaClass::BetaClass").should =~ - /nested beta/ + result = pry_eval("show-doc TestClassForCandidatesOrder") + result.should =~ /Number of monkeypatches: 2/ + result.should =~ /The first definition/ end - end - describe "show-doc -a" do - it 'should show the docs for all monkeypatches defined in different files' do - # local monkeypatch + it 'indicates all available monkeypatches can be shown with -a ' \ + '(when -a not used and more than one candidate exists for class)' do + # Still reading boring tests, eh? class TestClassForShowSource def beta end end - result = pry_eval("show-doc TestClassForShowSource -a") - result.should =~ /used by/ - result.should =~ /local monkeypatch/ + result = pry_eval('show-doc TestClassForShowSource') + result.should =~ /available monkeypatches/ end - describe "messages relating to -a" do - it "displays the original definition by default (not a doc of a monkeypatch)" do - class TestClassForCandidatesOrder - def beta - end - end - - result = pry_eval("show-doc TestClassForCandidatesOrder") - result.should =~ /Number of monkeypatches: 2/ - result.should =~ /The first definition/ + it 'shouldnt say anything about monkeypatches when only one candidate exists for selected class' do + # Do not remove me. + class Aarrrrrghh + def o;end end - it 'indicates all available monkeypatches can be shown with -a ' \ - '(when -a not used and more than one candidate exists for class)' do - # Still reading boring tests, eh? - class TestClassForShowSource - def beta - end - end - - result = pry_eval('show-doc TestClassForShowSource') - result.should =~ /available monkeypatches/ - end - - it 'shouldnt say anything about monkeypatches when only one candidate exists for selected class' do - # Do not remove me. - class Aarrrrrghh - def o;end - end - - result = pry_eval('show-doc Aarrrrrghh') - result.should.not =~ /available monkeypatches/ - Object.remove_const(:Aarrrrrghh) - end - end - end - - describe "when no class/module arg is given" do - before do - module TestHost - - # hello there froggy - module M - def d; end - def e; end - end - end - end - - after do - Object.remove_const(:TestHost) - end - - it 'should return doc for current module' do - pry_eval(TestHost::M, "show-doc").should =~ /hello there froggy/ - end - end - - # FIXME: THis is nto a good spec anyway, because i dont think it - # SHOULD skip! - describe "should skip over broken modules" do - before do - module TestHost - # hello - module M - binding.eval("def a; end", "dummy.rb", 1) - binding.eval("def b; end", "dummy.rb", 2) - binding.eval("def c; end", "dummy.rb", 3) - end - - # goodbye - module M - def d; end - def e; end - end - end - end - - after do - Object.remove_const(:TestHost) - end - - it 'should return doc for first valid module' do - result = pry_eval("show-doc TestHost::M") - result.should =~ /goodbye/ - result.should.not =~ /hello/ + result = pry_eval('show-doc Aarrrrrghh') + result.should.not =~ /available monkeypatches/ + Object.remove_const(:Aarrrrrghh) end end end - describe "on commands" do - # mostly copied & modified from test_help.rb + describe "when no class/module arg is given" do before do - @oldset = Pry.config.commands - @set = Pry.config.commands = Pry::CommandSet.new do - import Pry::Commands + module TestHost + + # hello there froggy + module M + def d; end + def e; end + end end end after do - Pry.config.commands = @oldset + Object.remove_const(:TestHost) end - it 'should display help for a specific command' do - pry_eval('show-doc ls').should =~ /Usage: ls/ + it 'should return doc for current module' do + pry_eval(TestHost::M, "show-doc").should =~ /hello there froggy/ end + end - it 'should display help for a regex command with a "listing"' do - @set.command /bar(.*)/, "Test listing", :listing => "foo" do; end - pry_eval('show-doc foo').should =~ /Test listing/ - end - - it 'should display help for a command with a spaces in its name' do - @set.command "command with spaces", "description of a command with spaces" do; end - pry_eval('show-doc command with spaces').should =~ /description of a command with spaces/ - end - - describe "class commands" do - before do - # pretty pink pincers - class LobsterLady < Pry::ClassCommand - match "lobster-lady" - description "nada." - def process - "lobster" - end + # FIXME: THis is nto a good spec anyway, because i dont think it + # SHOULD skip! + describe "should skip over broken modules" do + before do + module TestHost + # hello + module M + binding.eval("def a; end", "dummy.rb", 1) + binding.eval("def b; end", "dummy.rb", 2) + binding.eval("def c; end", "dummy.rb", 3) end - Pry.commands.add_command(LobsterLady) + # goodbye + module M + def d; end + def e; end + end + end + end + + after do + Object.remove_const(:TestHost) + end + + it 'should return doc for first valid module' do + result = pry_eval("show-doc TestHost::M") + result.should =~ /goodbye/ + result.should.not =~ /hello/ + end + end + end + + describe "on commands" do + # mostly copied & modified from test_help.rb + before do + @oldset = Pry.config.commands + @set = Pry.config.commands = Pry::CommandSet.new do + import Pry::Commands + end + end + + after do + Pry.config.commands = @oldset + end + + it 'should display help for a specific command' do + pry_eval('show-doc ls').should =~ /Usage: ls/ + end + + it 'should display help for a regex command with a "listing"' do + @set.command /bar(.*)/, "Test listing", :listing => "foo" do; end + pry_eval('show-doc foo').should =~ /Test listing/ + end + + it 'should display help for a command with a spaces in its name' do + @set.command "command with spaces", "description of a command with spaces" do; end + pry_eval('show-doc command with spaces').should =~ /description of a command with spaces/ + end + + describe "class commands" do + before do + # pretty pink pincers + class LobsterLady < Pry::ClassCommand + match "lobster-lady" + description "nada." + def process + "lobster" + end + end + + Pry.commands.add_command(LobsterLady) + end + + after do + Object.remove_const(:LobsterLady) + end + + it 'should display "help" when looking up by command name' do + pry_eval('show-doc lobster-lady').should =~ /nada/ + Pry.commands.delete("lobster-lady") + end + + it 'should display actual preceding comment for a class command, when class is used (rather than command name) when looking up' do + pry_eval('show-doc LobsterLady').should =~ /pretty pink pincers/ + Pry.commands.delete("lobster-lady") + end + end + end + + describe "should set _file_ and _dir_" do + it 'should set _file_ and _dir_ to file containing method source' do + t = pry_tester + t.process_command "show-doc TestClassForShowSource#alpha" + t.pry.last_file.should =~ /show_source_doc_examples/ + t.pry.last_dir.should =~ /fixtures/ + end + end + + unless Pry::Helpers::BaseHelpers.rbx? + describe "can't find class docs" do + describe "for classes" do + before do + module Jesus + class Brian; end + + # doink-doc + class Jingle + def a; :doink; end + end + + class Jangle < Jingle; end + class Bangle < Jangle; end + end end after do - Object.remove_const(:LobsterLady) + Object.remove_const(:Jesus) end - it 'should display "help" when looking up by command name' do - pry_eval('show-doc lobster-lady').should =~ /nada/ - Pry.commands.delete("lobster-lady") + it 'shows superclass doc' do + t = pry_tester + t.process_command "show-doc Jesus::Jangle" + t.last_output.should =~ /doink-doc/ end - it 'should display actual preceding comment for a class command, when class is used (rather than command name) when looking up' do - pry_eval('show-doc LobsterLady').should =~ /pretty pink pincers/ - Pry.commands.delete("lobster-lady") + it 'errors when class has no superclass to show' do + t = pry_tester + lambda { t.process_command "show-doc Jesus::Brian" }.should.raise(Pry::CommandError).message. + should =~ /Couldn't locate/ + end + + it 'shows warning when reverting to superclass docs' do + t = pry_tester + t.process_command "show-doc Jesus::Jangle" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Jangle.*Showing.*Jesus::Jingle instead/ + end + + it 'shows nth level superclass docs (when no intermediary superclasses have code either)' do + t = pry_tester + t.process_command "show-doc Jesus::Bangle" + t.last_output.should =~ /doink-doc/ + end + + it 'shows correct warning when reverting to nth level superclass' do + t = pry_tester + t.process_command "show-doc Jesus::Bangle" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Bangle.*Showing.*Jesus::Jingle instead/ end end - end - describe "should set _file_ and _dir_" do - it 'should set _file_ and _dir_ to file containing method source' do - t = pry_tester - t.process_command "show-doc TestClassForShowSource#alpha" - t.pry.last_file.should =~ /show_source_doc_examples/ - t.pry.last_dir.should =~ /fixtures/ - end - end + describe "for modules" do + before do + module Jesus - unless Pry::Helpers::BaseHelpers.rbx? - describe "can't find class docs" do - describe "for classes" do - before do - module Jesus - class Brian; end - - # doink-doc - class Jingle - def a; :doink; end - end - - class Jangle < Jingle; end - class Bangle < Jangle; end + # alpha-doc + module Alpha + def alpha; :alpha; end end - end - after do - Object.remove_const(:Jesus) - end + module Zeta; end - it 'shows superclass doc' do - t = pry_tester - t.process_command "show-doc Jesus::Jangle" - t.last_output.should =~ /doink-doc/ - end + module Beta + include Alpha + end - it 'errors when class has no superclass to show' do - t = pry_tester - lambda { t.process_command "show-doc Jesus::Brian" }.should.raise(Pry::CommandError).message. - should =~ /Couldn't locate/ - end - - it 'shows warning when reverting to superclass docs' do - t = pry_tester - t.process_command "show-doc Jesus::Jangle" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Jangle.*Showing.*Jesus::Jingle instead/ - end - - it 'shows nth level superclass docs (when no intermediary superclasses have code either)' do - t = pry_tester - t.process_command "show-doc Jesus::Bangle" - t.last_output.should =~ /doink-doc/ - end - - it 'shows correct warning when reverting to nth level superclass' do - t = pry_tester - t.process_command "show-doc Jesus::Bangle" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Bangle.*Showing.*Jesus::Jingle instead/ + module Gamma + include Beta + end end end - describe "for modules" do - before do - module Jesus + after do + Object.remove_const(:Jesus) + end - # alpha-doc - module Alpha - def alpha; :alpha; end - end + it 'shows included module doc' do + t = pry_tester + t.process_command "show-doc Jesus::Beta" + t.last_output.should =~ /alpha-doc/ + end - module Zeta; end + it 'shows warning when reverting to included module doc' do + t = pry_tester + t.process_command "show-doc Jesus::Beta" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Beta.*Showing.*Jesus::Alpha instead/ + end - module Beta - include Alpha - end + it 'errors when module has no included module to show' do + t = pry_tester + lambda { t.process_command "show-source Jesus::Zeta" }.should.raise(Pry::CommandError).message. + should =~ /Couldn't locate/ + end - module Gamma - include Beta - end - end - end + it 'shows nth level included module doc (when no intermediary modules have code either)' do + t = pry_tester + t.process_command "show-doc Jesus::Gamma" + t.last_output.should =~ /alpha-doc/ + end - after do - Object.remove_const(:Jesus) - end - - it 'shows included module doc' do - t = pry_tester - t.process_command "show-doc Jesus::Beta" - t.last_output.should =~ /alpha-doc/ - end - - it 'shows warning when reverting to included module doc' do - t = pry_tester - t.process_command "show-doc Jesus::Beta" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Beta.*Showing.*Jesus::Alpha instead/ - end - - it 'errors when module has no included module to show' do - t = pry_tester - lambda { t.process_command "show-source Jesus::Zeta" }.should.raise(Pry::CommandError).message. - should =~ /Couldn't locate/ - end - - it 'shows nth level included module doc (when no intermediary modules have code either)' do - t = pry_tester - t.process_command "show-doc Jesus::Gamma" - t.last_output.should =~ /alpha-doc/ - end - - it 'shows correct warning when reverting to nth level included module' do - t = pry_tester - t.process_command "show-source Jesus::Gamma" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Gamma.*Showing.*Jesus::Alpha instead/ - end + it 'shows correct warning when reverting to nth level included module' do + t = pry_tester + t.process_command "show-source Jesus::Gamma" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Gamma.*Showing.*Jesus::Alpha instead/ end end end diff --git a/spec/commands/show_source_spec.rb b/spec/commands/show_source_spec.rb index d98f604c..4c1a5648 100644 --- a/spec/commands/show_source_spec.rb +++ b/spec/commands/show_source_spec.rb @@ -1,284 +1,524 @@ require 'helper' require "fixtures/show_source_doc_examples" -if !PryTestHelpers.mri18_and_no_real_source_location? - describe "show-source" do - before do - @o = Object.new - def @o.sample_method - :sample +describe "show-source" do + before do + @o = Object.new + def @o.sample_method + :sample + end + + Object.const_set(:Test, Module.new) + end + + after do + Pad.clear + end + + it "should output a method's source" do + pry_eval(binding, 'show-source @o.sample_method').should =~ /def @o.sample/ + end + + it "should output help" do + pry_eval('show-source -h').should =~ /Usage:\s+show-source/ + end + + it "should output a method's source with line numbers" do + pry_eval(binding, 'show-source -l @o.sample_method').should =~ /\d+: def @o.sample/ + end + + it "should output a method's source with line numbers starting at 1" do + pry_eval(binding, 'show-source -b @o.sample_method').should =~ /1: def @o.sample/ + end + + it "should output a method's source if inside method and no name given" do + def @o.sample + pry_eval(binding, 'show-source').should =~ /def @o.sample/ + end + @o.sample + end + + it "should output a method's source inside method using the -l switch" do + def @o.sample + pry_eval(binding, 'show-source -l').should =~ /def @o.sample/ + end + @o.sample + end + + it "should find methods even if there are spaces in the arguments" do + def @o.foo(*bars) + "Mr flibble" + self + end + + out = pry_eval(binding, "show-source @o.foo('bar', 'baz bam').foo") + out.should =~ /Mr flibble/ + end + + it "should find methods even if the object overrides method method" do + c = Class.new{ + def method; + 98 + end + } + + pry_eval(binding, "show-source c.new.method").should =~ /98/ + end + + it "should not show the source when a non-extant method is requested" do + c = Class.new{ def method; 98; end } + mock_pry(binding, "show-source c#wrongmethod").should =~ /Couldn't locate/ + end + + it "should find instance_methods if the class overrides instance_method" do + c = Class.new{ + def method; + 98 end - Object.const_set(:Test, Module.new) + def self.instance_method; 789; end + } + + pry_eval(binding, "show-source c#method").should =~ /98/ + end + + it "should find instance methods with self#moo" do + c = Class.new{ def moo; "ve over!"; end } + + pry_eval(binding, "cd c", "show-source self#moo").should =~ /ve over/ + end + + it "should not find instance methods with self.moo" do + c = Class.new{ def moo; "ve over!"; end } + + proc { + pry_eval(binding, 'cd c', 'show-source self.moo') + }.should.raise(Pry::CommandError).message.should =~ /Couldn't locate/ + end + + it "should find normal methods with self.moo" do + c = Class.new{ def self.moo; "ve over!"; end } + + pry_eval(binding, 'cd c', 'show-source self.moo').should =~ /ve over/ + end + + it "should not find normal methods with self#moo" do + c = Class.new{ def self.moo; "ve over!"; end } + + proc { + pry_eval(binding, 'cd c', 'show-source self#moo') + }.should.raise(Pry::CommandError).message.should =~ /Couldn't locate/ + end + + it "should find normal methods (i.e non-instance methods) by default" do + c = Class.new{ def self.moo; "ve over!"; end } + + pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/ + end + + it "should find instance methods if no normal methods available" do + c = Class.new{ def moo; "ve over!"; end } + + pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/ + end + + it "should raise a CommandError when super method doesn't exist" do + def @o.foo(*bars); end + + proc { + pry_eval(binding, "show-source --super @o.foo") + }.should.raise(Pry::CommandError).message.should =~ /No superclass found/ + end + + it "should output the source of a method defined inside Pry" do + out = pry_eval("def dyn_method\n:test\nend", 'show-source dyn_method') + out.should =~ /def dyn_method/ + Object.remove_method :dyn_method + end + + it 'should output source for an instance method defined inside pry' do + pry_tester.tap do |t| + t.eval "class Test::A\n def yo\n end\nend" + t.eval('show-source Test::A#yo').should =~ /def yo/ + end + end + + it 'should output source for a repl method defined using define_method' do + pry_tester.tap do |t| + t.eval "class Test::A\n define_method(:yup) {}\nend" + t.eval('show-source Test::A#yup').should =~ /define_method\(:yup\)/ + end + end + + it "should output the source of a command defined inside Pry" do + command_definition = %{ + Pry.commands.command "hubba-hubba" do + puts "that's what she said!" + end + } + out = pry_eval(command_definition, 'show-source hubba-hubba') + out.should =~ /what she said/ + Pry.commands.delete "hubba-hubba" + end + + describe "finding super methods with help of `--super` switch" do + before do + class Foo + def foo(*bars) + :super_wibble + end + end end after do - Pad.clear + Object.remove_const(:Foo) end - it "should output a method's source" do - pry_eval(binding, 'show-source @o.sample_method').should =~ /def @o.sample/ - end + it "finds super methods with explicit method argument" do - it "should output help" do - pry_eval('show-source -h').should =~ /Usage:\s+show-source/ - end - - it "should output a method's source with line numbers" do - pry_eval(binding, 'show-source -l @o.sample_method').should =~ /\d+: def @o.sample/ - end - - it "should output a method's source with line numbers starting at 1" do - pry_eval(binding, 'show-source -b @o.sample_method').should =~ /1: def @o.sample/ - end - - it "should output a method's source if inside method and no name given" do - def @o.sample - pry_eval(binding, 'show-source').should =~ /def @o.sample/ - end - @o.sample - end - - it "should output a method's source inside method using the -l switch" do - def @o.sample - pry_eval(binding, 'show-source -l').should =~ /def @o.sample/ - end - @o.sample - end - - it "should find methods even if there are spaces in the arguments" do - def @o.foo(*bars) - "Mr flibble" - self + o = Foo.new + def o.foo(*bars) + :wibble end - out = pry_eval(binding, "show-source @o.foo('bar', 'baz bam').foo") - out.should =~ /Mr flibble/ + pry_eval(binding, "show-source --super o.foo").should =~ /:super_wibble/ end - it "should find methods even if the object overrides method method" do - c = Class.new{ - def method; - 98 + it "finds super methods without explicit method argument" do + o = Foo.new + def o.foo(*bars) + :wibble + pry_eval(binding, 'show-source --super') + end + + o.foo.should =~ /:super_wibble/ + end + + it "finds super methods with multiple --super " do + o = Foo.new + + o.extend Module.new { + def foo + :nibble end } - pry_eval(binding, "show-source c.new.method").should =~ /98/ - end - - it "should not show the source when a non-extant method is requested" do - c = Class.new{ def method; 98; end } - mock_pry(binding, "show-source c#wrongmethod").should =~ /Couldn't locate/ - end - - it "should find instance_methods if the class overrides instance_method" do - c = Class.new{ - def method; - 98 - end - - def self.instance_method; 789; end - } - - pry_eval(binding, "show-source c#method").should =~ /98/ - end - - it "should find instance methods with self#moo" do - c = Class.new{ def moo; "ve over!"; end } - - pry_eval(binding, "cd c", "show-source self#moo").should =~ /ve over/ - end - - it "should not find instance methods with self.moo" do - c = Class.new{ def moo; "ve over!"; end } - - proc { - pry_eval(binding, 'cd c', 'show-source self.moo') - }.should.raise(Pry::CommandError).message.should =~ /Couldn't locate/ - end - - it "should find normal methods with self.moo" do - c = Class.new{ def self.moo; "ve over!"; end } - - pry_eval(binding, 'cd c', 'show-source self.moo').should =~ /ve over/ - end - - it "should not find normal methods with self#moo" do - c = Class.new{ def self.moo; "ve over!"; end } - - proc { - pry_eval(binding, 'cd c', 'show-source self#moo') - }.should.raise(Pry::CommandError).message.should =~ /Couldn't locate/ - end - - it "should find normal methods (i.e non-instance methods) by default" do - c = Class.new{ def self.moo; "ve over!"; end } - - pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/ - end - - it "should find instance methods if no normal methods available" do - c = Class.new{ def moo; "ve over!"; end } - - pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/ - end - - it "should raise a CommandError when super method doesn't exist" do - def @o.foo(*bars); end - - proc { - pry_eval(binding, "show-source --super @o.foo") - }.should.raise(Pry::CommandError).message.should =~ /No superclass found/ - end - - # dynamically defined method source retrieval is only supported in - # 1.9 - where Method#source_location is native - if RUBY_VERSION =~ /1.9/ - it "should output the source of a method defined inside Pry" do - out = pry_eval("def dyn_method\n:test\nend", 'show-source dyn_method') - out.should =~ /def dyn_method/ - Object.remove_method :dyn_method + def o.foo(*bars) + :wibble + pry_eval(binding, 'show-source --super --super') end - it 'should output source for an instance method defined inside pry' do - pry_tester.tap do |t| - t.eval "class Test::A\n def yo\n end\nend" - t.eval('show-source Test::A#yo').should =~ /def yo/ - end - end + o.foo.should =~ /:super_wibble/ + end + end - it 'should output source for a repl method defined using define_method' do - pry_tester.tap do |t| - t.eval "class Test::A\n define_method(:yup) {}\nend" - t.eval('show-source Test::A#yup').should =~ /define_method\(:yup\)/ - end - end - - it "should output the source of a command defined inside Pry" do - command_definition = %{ - Pry.commands.command "hubba-hubba" do - puts "that's what she said!" - end - } - out = pry_eval(command_definition, 'show-source hubba-hubba') - out.should =~ /what she said/ - Pry.commands.delete "hubba-hubba" + describe "on sourcable objects" do + it "should output source defined inside pry" do + pry_tester.tap do |t| + t.eval "hello = proc { puts 'hello world!' }" + t.eval("show-source hello").should =~ /proc \{ puts/ end end - describe "finding super methods with help of `--super` switch" do + it "should output source for procs/lambdas stored in variables" do + hello = proc { puts 'hello world!' } + pry_eval(binding, 'show-source hello').should =~ /proc \{ puts/ + end + + it "should output source for procs/lambdas stored in constants" do + HELLO = proc { puts 'hello world!' } + pry_eval(binding, "show-source HELLO").should =~ /proc \{ puts/ + Object.remove_const(:HELLO) + end + + it "should output source for method objects" do + def @o.hi; puts 'hi world'; end + meth = @o.method(:hi) + pry_eval(binding, "show-source meth").should =~ /puts 'hi world'/ + end + + describe "on variables that shadow methods" do before do - class Foo - def foo(*bars) - :super_wibble + @t = pry_tester.eval unindent(<<-EOS) + class ::TestHost + def hello + hello = proc { ' smile ' } + pry_tester(binding) + end + end + ::TestHost.new.hello + EOS + end + + after do + Object.remove_const(:TestHost) + end + + it "source of variable should take precedence over method that is being shadowed" do + source = @t.eval('show-source hello') + source.should.not =~ /def hello/ + source.should =~ /proc \{ ' smile ' \}/ + end + + it "source of method being shadowed should take precedence over variable + if given self.meth_name syntax" do + @t.eval('show-source self.hello').should =~ /def hello/ + end + end + end + + describe "on variable or constant" do + before do + class TestHost + def hello + "hi there" end end end after do - Object.remove_const(:Foo) + Object.remove_const(:TestHost) end - it "finds super methods with explicit method argument" do - - o = Foo.new - def o.foo(*bars) - :wibble - end - - pry_eval(binding, "show-source --super o.foo").should =~ /:super_wibble/ + it "should output source of its class if variable doesn't respond to source_location" do + test_host = TestHost.new + pry_eval(binding, 'show-source test_host'). + should =~ /class TestHost\n.*def hello/ end - it "finds super methods without explicit method argument" do - o = Foo.new - def o.foo(*bars) - :wibble - pry_eval(binding, 'show-source --super') - end - - o.foo.should =~ /:super_wibble/ - end - - it "finds super methods with multiple --super " do - o = Foo.new - - o.extend Module.new { - def foo - :nibble - end - } - - def o.foo(*bars) - :wibble - pry_eval(binding, 'show-source --super --super') - end - - o.foo.should =~ /:super_wibble/ + it "should output source of its class if constant doesn't respond to source_location" do + TEST_HOST = TestHost.new + pry_eval(binding, 'show-source TEST_HOST'). + should =~ /class TestHost\n.*def hello/ + Object.remove_const(:TEST_HOST) end end - describe "on sourcable objects" do - if RUBY_VERSION =~ /1.9/ - it "should output source defined inside pry" do - pry_tester.tap do |t| - t.eval "hello = proc { puts 'hello world!' }" - t.eval("show-source hello").should =~ /proc \{ puts/ + describe "on modules" do + before do + class ShowSourceTestSuperClass + def alpha + end + end + + class ShowSourceTestClass "bar" do; end - - pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/ - end - end - - describe "create_command commands" do - it 'should show source for a command' do - @set.create_command "foo", "babble" do - def process() :body_of_foo end - end - pry_eval('show-source foo').should =~ /:body_of_foo/ - end - - it 'should show source for a command defined inside pry' do - pry_eval %{ - _pry_.commands.create_command "foo", "babble" do - def process() :body_of_foo end - end - } pry_eval('show-source foo').should =~ /:body_of_foo/ end + + it "should output source of commands using special characters" do + @set.command "!%$", "I gots the yellow fever" do; end + + pry_eval('show-source !%$').should =~ /yellow fever/ + end + + it 'should show source for a command with spaces in its name' do + @set.command "foo bar", :body_of_foo_bar do; end + + pry_eval('show-source foo bar').should =~ /:body_of_foo_bar/ + end + + it 'should show source for a command by listing name' do + @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end + + pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/ + end end - describe "real class-based commands" do - before do - class ::TemporaryCommand < Pry::ClassCommand - match 'temp-command' - def process() :body_of_temp end - end - - Pry.commands.add_command(::TemporaryCommand) - end - - after do - Object.remove_const(:TemporaryCommand) - end - + describe "create_command commands" do it 'should show source for a command' do - pry_eval('show-source temp-command').should =~ /:body_of_temp/ + @set.create_command "foo", "babble" do + def process() :body_of_foo end + end + pry_eval('show-source foo').should =~ /:body_of_foo/ end it 'should show source for a command defined inside pry' do pry_eval %{ - class ::TemporaryCommandInPry < Pry::ClassCommand - match 'temp-command-in-pry' - def process() :body_of_temp end - end - } - Pry.commands.add_command(::TemporaryCommandInPry) - pry_eval('show-source temp-command-in-pry').should =~ /:body_of_temp/ - Object.remove_const(:TemporaryCommandInPry) - end + _pry_.commands.create_command "foo", "babble" do + def process() :body_of_foo end + end + } + pry_eval('show-source foo').should =~ /:body_of_foo/ end end - describe "should set _file_ and _dir_" do - it 'should set _file_ and _dir_ to file containing method source' do - t = pry_tester - t.process_command "show-source TestClassForShowSource#alpha" - t.pry.last_file.should =~ /show_source_doc_examples/ - t.pry.last_dir.should =~ /fixtures/ + describe "real class-based commands" do + before do + class ::TemporaryCommand < Pry::ClassCommand + match 'temp-command' + def process() :body_of_temp end + end + + Pry.commands.add_command(::TemporaryCommand) + end + + after do + Object.remove_const(:TemporaryCommand) + end + + it 'should show source for a command' do + pry_eval('show-source temp-command').should =~ /:body_of_temp/ + end + + it 'should show source for a command defined inside pry' do + pry_eval %{ + class ::TemporaryCommandInPry < Pry::ClassCommand + match 'temp-command-in-pry' + def process() :body_of_temp end + end + } + Pry.commands.add_command(::TemporaryCommandInPry) + pry_eval('show-source temp-command-in-pry').should =~ /:body_of_temp/ + Object.remove_const(:TemporaryCommandInPry) end end + end - unless Pry::Helpers::BaseHelpers.rbx? - describe "can't find class/module code" do - describe "for classes" do - before do - module Jesus - module Pig - def lillybing; :lillybing; end - end + describe "should set _file_ and _dir_" do + it 'should set _file_ and _dir_ to file containing method source' do + t = pry_tester + t.process_command "show-source TestClassForShowSource#alpha" + t.pry.last_file.should =~ /show_source_doc_examples/ + t.pry.last_dir.should =~ /fixtures/ + end + end - class Brian; end - class Jingle - def a; :doink; end - end - - class Jangle < Jingle; include Pig; end - class Bangle < Jangle; end + unless Pry::Helpers::BaseHelpers.rbx? + describe "can't find class/module code" do + describe "for classes" do + before do + module Jesus + module Pig + def lillybing; :lillybing; end end - end - after do - Object.remove_const(:Jesus) - end + class Brian; end + class Jingle + def a; :doink; end + end - it 'shows superclass code' do - t = pry_tester - t.process_command "show-source Jesus::Jangle" - t.last_output.should =~ /doink/ - end - - it 'ignores included modules' do - t = pry_tester - t.process_command "show-source Jesus::Jangle" - t.last_output.should.not =~ /lillybing/ - end - - it 'errors when class has no superclass to show' do - t = pry_tester - lambda { t.process_command "show-source Jesus::Brian" }.should.raise(Pry::CommandError).message. - should =~ /Couldn't locate/ - end - - it 'shows warning when reverting to superclass code' do - t = pry_tester - t.process_command "show-source Jesus::Jangle" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Jangle.*Showing.*Jesus::Jingle instead/ - end - - it 'shows nth level superclass code (when no intermediary superclasses have code either)' do - t = pry_tester - t.process_command "show-source Jesus::Bangle" - t.last_output.should =~ /doink/ - end - - it 'shows correct warning when reverting to nth level superclass' do - t = pry_tester - t.process_command "show-source Jesus::Bangle" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Bangle.*Showing.*Jesus::Jingle instead/ + class Jangle < Jingle; include Pig; end + class Bangle < Jangle; end end end - describe "for modules" do - before do - module Jesus - module Alpha - def alpha; :alpha; end - end + after do + Object.remove_const(:Jesus) + end - module Zeta; end + it 'shows superclass code' do + t = pry_tester + t.process_command "show-source Jesus::Jangle" + t.last_output.should =~ /doink/ + end - module Beta - include Alpha - end + it 'ignores included modules' do + t = pry_tester + t.process_command "show-source Jesus::Jangle" + t.last_output.should.not =~ /lillybing/ + end - module Gamma - include Beta - end + it 'errors when class has no superclass to show' do + t = pry_tester + lambda { t.process_command "show-source Jesus::Brian" }.should.raise(Pry::CommandError).message. + should =~ /Couldn't locate/ + end + + it 'shows warning when reverting to superclass code' do + t = pry_tester + t.process_command "show-source Jesus::Jangle" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Jangle.*Showing.*Jesus::Jingle instead/ + end + + it 'shows nth level superclass code (when no intermediary superclasses have code either)' do + t = pry_tester + t.process_command "show-source Jesus::Bangle" + t.last_output.should =~ /doink/ + end + + it 'shows correct warning when reverting to nth level superclass' do + t = pry_tester + t.process_command "show-source Jesus::Bangle" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Bangle.*Showing.*Jesus::Jingle instead/ + end + end + + describe "for modules" do + before do + module Jesus + module Alpha + def alpha; :alpha; end + end + + module Zeta; end + + module Beta + include Alpha + end + + module Gamma + include Beta end end + end - after do - Object.remove_const(:Jesus) - end + after do + Object.remove_const(:Jesus) + end - it 'shows included module code' do - t = pry_tester - t.process_command "show-source Jesus::Beta" - t.last_output.should =~ /alpha/ - end + it 'shows included module code' do + t = pry_tester + t.process_command "show-source Jesus::Beta" + t.last_output.should =~ /alpha/ + end - it 'shows warning when reverting to included module code' do - t = pry_tester - t.process_command "show-source Jesus::Beta" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Beta.*Showing.*Jesus::Alpha instead/ - end + it 'shows warning when reverting to included module code' do + t = pry_tester + t.process_command "show-source Jesus::Beta" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Beta.*Showing.*Jesus::Alpha instead/ + end - it 'errors when module has no included module to show' do - t = pry_tester - lambda { t.process_command "show-source Jesus::Zeta" }.should.raise(Pry::CommandError).message. - should =~ /Couldn't locate/ - end + it 'errors when module has no included module to show' do + t = pry_tester + lambda { t.process_command "show-source Jesus::Zeta" }.should.raise(Pry::CommandError).message. + should =~ /Couldn't locate/ + end - it 'shows nth level included module code (when no intermediary modules have code either)' do - t = pry_tester - t.process_command "show-source Jesus::Gamma" - t.last_output.should =~ /alpha/ - end + it 'shows nth level included module code (when no intermediary modules have code either)' do + t = pry_tester + t.process_command "show-source Jesus::Gamma" + t.last_output.should =~ /alpha/ + end - it 'shows correct warning when reverting to nth level included module' do - t = pry_tester - t.process_command "show-source Jesus::Gamma" - t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Gamma.*Showing.*Jesus::Alpha instead/ - end + it 'shows correct warning when reverting to nth level included module' do + t = pry_tester + t.process_command "show-source Jesus::Gamma" + t.last_output.should =~ /Warning.*?Cannot find.*?Jesus::Gamma.*Showing.*Jesus::Alpha instead/ end end end diff --git a/spec/commands/whereami_spec.rb b/spec/commands/whereami_spec.rb index 65bd8321..f75291a8 100644 --- a/spec/commands/whereami_spec.rb +++ b/spec/commands/whereami_spec.rb @@ -41,16 +41,14 @@ describe "whereami" do Object.remove_const(:Cor) end - if defined?(BasicObject) - it 'should work in BasicObjects' do - cor = Class.new(BasicObject) do - def blimey! - ::Kernel.binding # omnom - end - end.new.blimey! + it 'should work in BasicObjects' do + cor = Class.new(BasicObject) do + def blimey! + ::Kernel.binding # omnom + end + end.new.blimey! - pry_eval(cor, 'whereami').should =~ /::Kernel.binding [#] omnom/ - end + pry_eval(cor, 'whereami').should =~ /::Kernel.binding [#] omnom/ end it 'should show description and correct code when __LINE__ and __FILE__ are outside @method.source_location' do diff --git a/spec/method_spec.rb b/spec/method_spec.rb index 304c6feb..de902a03 100644 --- a/spec/method_spec.rb +++ b/spec/method_spec.rb @@ -144,15 +144,13 @@ describe Pry::Method do m.name.should == "gag" end - if defined?(BasicObject) && !Pry::Helpers::BaseHelpers.rbx? # rubinius issue 1921 - it "should find the right method from a BasicObject" do - a = Class.new(BasicObject) { def gag; ::Kernel.binding; end; def self.line; __LINE__; end } + it "should find the right method from a BasicObject" do + a = Class.new(BasicObject) { def gag; ::Kernel.binding; end; def self.line; __LINE__; end } - m = Pry::Method.from_binding(a.new.gag) - m.owner.should == a - m.source_file.should == __FILE__ - m.source_line.should == a.line - end + m = Pry::Method.from_binding(a.new.gag) + m.owner.should == a + m.source_file.should == __FILE__ + m.source_line.should == a.line end it 'should find the right method even if it was renamed and replaced' do @@ -433,8 +431,8 @@ describe Pry::Method do it "should include the Pry::Method.instance_resolution_order of Class after the singleton classes" do Pry::Method.resolution_order(LS::Top).should == - [singleton_class(LS::Top), singleton_class(Object), (defined? BasicObject) && singleton_class(BasicObject)].compact + - Pry::Method.instance_resolution_order(Class) + [singleton_class(LS::Top), singleton_class(Object), singleton_class(BasicObject), + *Pry::Method.instance_resolution_order(Class)] end end end @@ -496,15 +494,11 @@ describe Pry::Method do end end - unless Pry::Helpers::BaseHelpers.mri_18? - # Ruby 1.8 doesn't support this feature. - it 'should be able to find aliases for methods implemented in C' do - meth = Pry::Method(Hash.new.method(:key?)) - aliases = Set.new(meth.aliases) + it 'should be able to find aliases for methods implemented in C' do + meth = Pry::Method(Hash.new.method(:key?)) + aliases = Set.new(meth.aliases) - aliases.should == Set.new(["include?", "member?", "has_key?"]) - end + aliases.should == Set.new(["include?", "member?", "has_key?"]) end - end end diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index 8331619d..79eecc52 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -5,15 +5,11 @@ describe Pry do @str_output = StringIO.new end - if RUBY_VERSION =~ /1.9/ - describe "Exotic object support" do - # regression test for exotic object support - it "Should not error when return value is a BasicObject instance" do - - ReplTester.start do - input('BasicObject.new').should =~ /^=> # # @context, :output => out = StringIO.new - out.string.should =~ /roken is dodelijk/ - end + # This is a regression test as 0.9.11 broke this behaviour + it 'can perform a show-source' do + Pry.run_command "show-source drum", :context => @context, :output => out = StringIO.new + out.string.should =~ /roken is dodelijk/ end end From add29398f0e96e63c3dc6c9a66997821809a1cf0 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Wed, 23 Oct 2013 23:22:42 -0700 Subject: [PATCH 041/186] Fix failing jruby spec --- spec/pry_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index 79eecc52..b12a0423 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -319,7 +319,8 @@ describe Pry do end it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do - [:test, 0, true, false, nil, 0.0].each do |val| + [:test, 0, true, false, nil, + (0.0 unless Pry::Helpers::BaseHelpers.jruby?)].each do |val| pry_eval(val, "def hello; end"); val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true end From 4aed2ab1c0ab8f347360f963da2807d869f8f5c6 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 19 Jan 2014 18:00:25 -0800 Subject: [PATCH 042/186] Fix default inspect for Ruby 2.1 In Ruby 2.1, the ancestor list of a singleton class includes the singleton as the first element, so we need to filter it out. --- lib/pry/color_printer.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pry/color_printer.rb b/lib/pry/color_printer.rb index 5f804fd1..39d9a9ae 100644 --- a/lib/pry/color_printer.rb +++ b/lib/pry/color_printer.rb @@ -37,8 +37,9 @@ class Pry # Read the class name off of the singleton class to provide a default # inspect. - eig = class << obj; self; end - klass = Pry::Method.safe_send(eig, :ancestors).first + singleton = class << obj; self; end + ancestors = Pry::Method.safe_send(singleton, :ancestors) + klass = ancestors.reject { |k| k == singleton }.first obj_id = obj.__id__.to_s(16) rescue 0 str = "#<#{klass}:0x#{obj_id}>" From b39ba9725b9d720b1f2fd042c0a9912881d69735 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 19 Jan 2014 20:48:31 -0800 Subject: [PATCH 043/186] Disable spec to work around rubinius/rubinius#2871 --- spec/method_spec.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/spec/method_spec.rb b/spec/method_spec.rb index de902a03..0c49d104 100644 --- a/spec/method_spec.rb +++ b/spec/method_spec.rb @@ -144,13 +144,16 @@ describe Pry::Method do m.name.should == "gag" end - it "should find the right method from a BasicObject" do - a = Class.new(BasicObject) { def gag; ::Kernel.binding; end; def self.line; __LINE__; end } + # Temporarily disabled to work around rubinius/rubinius#2871. + unless Pry::Helpers::BaseHelpers.rbx? + it "should find the right method from a BasicObject" do + a = Class.new(BasicObject) { def gag; ::Kernel.binding; end; def self.line; __LINE__; end } - m = Pry::Method.from_binding(a.new.gag) - m.owner.should == a - m.source_file.should == __FILE__ - m.source_line.should == a.line + m = Pry::Method.from_binding(a.new.gag) + m.owner.should == a + m.source_file.should == __FILE__ + m.source_line.should == a.line + end end it 'should find the right method even if it was renamed and replaced' do From 358cb978ec27177e9528bad7c2e2b75372fe1670 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 08:39:35 +0100 Subject: [PATCH 044/186] remove 'sticky_locals' as a configuration option. --- lib/pry/config/default.rb | 11 ----------- lib/pry/pry_instance.rb | 12 ++++++++++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index d6ac9257..d944a39d 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -31,17 +31,6 @@ class Pry::Config::Default < Pry::Config :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, :memory_size => proc { 100 }, :extra_sticky_locals => proc { {} }, - :sticky_locals => proc { |pry| - { _in_: pry.input_array, - _out_: pry.output_array, - _pry_: pry, - _ex_: pry.last_exception, - _file_: pry.last_file, - _dir_: pry.last_dir, - _: pry.last_result, - __: pry.output_array[-2] - } - }, :completer => proc { if defined?(Bond) && Readline::VERSION !~ /editline/i Pry::BondCompleter.start diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 8c22068f..c6cb290a 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -68,7 +68,7 @@ class Pry @command_state = {} @eval_string = "" @backtrace = options[:backtrace] || caller - @config = Pry::Config.new + @config = Pry::Config.new @config.merge!(options) @input_array = Pry::HistoryArray.new config.memory_size @output_array = Pry::HistoryArray.new config.memory_size @@ -159,7 +159,15 @@ class Pry end def sticky_locals - config.sticky_locals(self) + { _in_: input_array, + _out_: output_array, + _pry_: self, + _ex_: last_exception, + _file_: last_file, + _dir_: last_dir, + _: last_result, + __: output_array[-2] + } end # Reset the current eval string. If the user has entered part of a multiline From b8ace53104cd41273ebcd24895b82bb852dd5715 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 08:40:05 +0100 Subject: [PATCH 045/186] Pry::Config#merge! turns Symbol keys to String's. --- lib/pry/config.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 4e2a2f7e..54600a6f 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -38,7 +38,9 @@ class Pry::Config end def merge!(other) - @lookup.merge!(other.to_hash) + other = other.to_hash + keys, values = other.keys.map(&:to_s), other.values + @lookup.merge! Hash[keys.zip(values)] end def respond_to?(name, boolean=false) From 52f7260090ae8875103107832f418890c11359d4 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 09:08:38 +0100 Subject: [PATCH 046/186] add Pry.config.file_completer, Pry.config.command_completer --- lib/pry.rb | 1 - lib/pry/commands/shell_mode.rb | 4 ++-- lib/pry/config/default.rb | 2 ++ lib/pry/custom_completions.rb | 6 ------ lib/pry/pry_class.rb | 1 - lib/pry/pry_instance.rb | 6 +++--- 6 files changed, 7 insertions(+), 13 deletions(-) delete mode 100644 lib/pry/custom_completions.rb diff --git a/lib/pry.rb b/lib/pry.rb index 8ba3f954..2edec674 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -177,7 +177,6 @@ require 'pry/history' require 'pry/command' require 'pry/command_set' require 'pry/commands' -require 'pry/custom_completions' require 'pry/completion' require 'pry/plugins' require 'pry/core_extensions' diff --git a/lib/pry/commands/shell_mode.rb b/lib/pry/commands/shell_mode.rb index db5e69fa..64db7668 100644 --- a/lib/pry/commands/shell_mode.rb +++ b/lib/pry/commands/shell_mode.rb @@ -12,10 +12,10 @@ class Pry case _pry_.prompt when Pry::SHELL_PROMPT _pry_.pop_prompt - _pry_.custom_completions = Pry::DEFAULT_CUSTOM_COMPLETIONS + _pry_.custom_completions = _pry_.config.file_completer else _pry_.push_prompt Pry::SHELL_PROMPT - _pry_.custom_completions = Pry::FILE_COMPLETIONS + _pry_.custom_completions = _pry_.config.command_completer end end end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index d944a39d..40b92ac4 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -31,6 +31,8 @@ class Pry::Config::Default < Pry::Config :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, :memory_size => proc { 100 }, :extra_sticky_locals => proc { {} }, + :command_completer => proc { Pry.commands.commands.keys }, + :file_completer => proc { Dir["."] }, :completer => proc { if defined?(Bond) && Readline::VERSION !~ /editline/i Pry::BondCompleter.start diff --git a/lib/pry/custom_completions.rb b/lib/pry/custom_completions.rb deleted file mode 100644 index 7a2bb7a8..00000000 --- a/lib/pry/custom_completions.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Pry - - # This proc will be instance_eval's against the active Pry instance - DEFAULT_CUSTOM_COMPLETIONS = proc { commands.commands.keys } - FILE_COMPLETIONS = proc { commands.commands.keys + Dir.entries('.') } -end diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 28721b72..60399cac 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -248,7 +248,6 @@ Readline version #{ver} detected - will not auto_resize! correctly. # Set all the configurable options back to their default values def self.reset_defaults @initial_session = true - self.custom_completions = DEFAULT_CUSTOM_COMPLETIONS self.cli = false self.current_line = 1 self.line_buffer = [""] diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index c6cb290a..cba7bc67 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -25,6 +25,7 @@ require "pry/indent" class Pry attr_accessor :binding_stack + attr_accessor :custom_completions attr_accessor :eval_string attr_accessor :backtrace attr_accessor :suppress_output @@ -72,11 +73,10 @@ class Pry @config.merge!(options) @input_array = Pry::HistoryArray.new config.memory_size @output_array = Pry::HistoryArray.new config.memory_size - + @custom_completions = config.command_completer push_initial_binding(options[:target]) set_last_result nil - @input_array << nil # add empty input so _in_ and _out_ match - # yield the binding_stack to the hook for modification + @input_array << nil exec_hook(:when_started, options[:target], options, self) end From 92ec9480293f4bedae9a1e24bf80ca9a97fe95e8 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 09:16:02 +0100 Subject: [PATCH 047/186] remove instance_eval of Pry#custom_completions Proc. --- lib/pry/pry_instance.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index cba7bc67..50d73dc2 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -113,9 +113,9 @@ class Pry # @return [Array] Possible completions def complete(input) Pry.critical_section do - Pry.config.completer.call(input, :target => current_binding, - :pry => self, - :custom_completions => instance_eval(&custom_completions)) + config.completer.call input, :target => current_binding, + :pry => self, + :custom_completions => custom_completions end end From dc2298d0d6490b82584287c0c1e7f4c647f5d648 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 09:20:28 +0100 Subject: [PATCH 048/186] move require of 'pry/indent' to pry.rb --- lib/pry.rb | 1 + lib/pry/pry_instance.rb | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 2edec674..9d698db0 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -188,3 +188,4 @@ require 'pry/pager' require 'pry/terminal' require 'pry/editor' require 'pry/rubygem' +require "pry/indent" diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 50d73dc2..41912fa2 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -require "pry/indent" - ## # Pry is a powerful alternative to the standard IRB shell for Ruby. It # features syntax highlighting, a flexible plugin architecture, runtime From 05583abd1d8fe12edb08b5c76ee6d5bd2a05d8b2 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 09:29:37 +0100 Subject: [PATCH 049/186] define Ls/Gist/History defaults on instance of Pry::Config::Default. --- lib/pry/config.rb | 42 ---------------------------------- lib/pry/config/default.rb | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 54600a6f..6c1c4f0d 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -64,46 +64,4 @@ class Pry::Config end private - # TODO: - # all of this configure_* stuff is a relic of old code. - # we should try move this code to being command-local. - def configure_ls - @lookup["ls"] = OpenStruct.new({ - :heading_color => :bright_blue, - :public_method_color => :default, - :private_method_color => :blue, - :protected_method_color => :blue, - :method_missing_color => :bright_red, - :local_var_color => :yellow, - :pry_var_color => :default, # e.g. _, _pry_, _file_ - :instance_var_color => :blue, # e.g. @foo - :class_var_color => :bright_blue, # e.g. @@foo - :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library - :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID - :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO - :constant_color => :default, # e.g. VERSION, ARGF - :class_constant_color => :blue, # e.g. Object, Kernel - :exception_constant_color => :magenta, # e.g. Exception, RuntimeError - :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state - :separator => " ", - :ceiling => [Object, Module, Class] - }) - end - - def configure_gist - @lookup["gist"] = OpenStruct.new - gist.inspecter = proc(&:pretty_inspect) - end - - def configure_history - @lookup["history"] = OpenStruct.new - history.should_save = true - history.should_load = true - history.file = File.expand_path("~/.pry_history") rescue nil - if history.file.nil? - self.should_load_rc = false - history.should_save = false - history.should_load = false - end - end end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 40b92ac4..4f4bd2fe 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -44,7 +44,54 @@ class Pry::Config::Default < Pry::Config def initialize(*) super(nil) + configure_ls + configure_gist + configure_history end state.each { |key, value| define_method(key, &value) } + +private + # TODO: + # all of this configure_* stuff is a relic of old code. + # we should try move this code to being command-local. + def configure_ls + @lookup["ls"] = OpenStruct.new({ + :heading_color => :bright_blue, + :public_method_color => :default, + :private_method_color => :blue, + :protected_method_color => :blue, + :method_missing_color => :bright_red, + :local_var_color => :yellow, + :pry_var_color => :default, # e.g. _, _pry_, _file_ + :instance_var_color => :blue, # e.g. @foo + :class_var_color => :bright_blue, # e.g. @@foo + :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library + :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID + :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO + :constant_color => :default, # e.g. VERSION, ARGF + :class_constant_color => :blue, # e.g. Object, Kernel + :exception_constant_color => :magenta, # e.g. Exception, RuntimeError + :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state + :separator => " ", + :ceiling => [Object, Module, Class] + }) + end + + def configure_gist + @lookup["gist"] = OpenStruct.new + gist.inspecter = proc(&:pretty_inspect) + end + + def configure_history + @lookup["history"] = OpenStruct.new + history.should_save = true + history.should_load = true + history.file = File.expand_path("~/.pry_history") rescue nil + if history.file.nil? + self.should_load_rc = false + history.should_save = false + history.should_load = false + end + end end From 34b103a87266080aa031619f552a75269b485614 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 09:46:26 +0100 Subject: [PATCH 050/186] initialize Pry.config in Pry.reset_defaults(revisit later). we don't want to depend on Pry.reset_defaults if we can avoid it. `Pry.config` being a lazy-loaded value has benefits. I'll try to revisit this later when the rest of the new work on Pry::Config and Pry::Config::Default is finished. --- lib/pry/pry_class.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 60399cac..6d5ebd49 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -14,6 +14,7 @@ class Pry attr_accessor :cli attr_accessor :quiet attr_accessor :last_internal_error + attr_accessor :config def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins @@ -21,10 +22,6 @@ class Pry config_shortcut *Pry::Config.shortcuts end - def self.config - @config ||= Pry::Config.new(Pry::Config::Default.new) - end - def self.main @main ||= TOPLEVEL_BINDING.eval "self" end @@ -248,6 +245,7 @@ Readline version #{ver} detected - will not auto_resize! correctly. # Set all the configurable options back to their default values def self.reset_defaults @initial_session = true + self.config = Pry::Config.new Pry::Config::Default.new self.cli = false self.current_line = 1 self.line_buffer = [""] From 3c5c9928488dfb20a9007a7ff1300cd4b3f4c2e3 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 12:34:34 +0100 Subject: [PATCH 051/186] stringify keys passed to Pry::Config#[] and Pry::Config#[]=. --- lib/pry/config.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 6c1c4f0d..76540ac8 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -16,11 +16,11 @@ class Pry::Config end def [](key) - @lookup[key] + @lookup[key.to_s] end def []=(key, value) - @lookup[key] = value + @lookup[key.to_s] = value end def method_missing(name, *args, &block) From 57c1921e1eb7e912a8cd793f3ea90ec513c225fa Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 12:39:56 +0100 Subject: [PATCH 052/186] read and write via Pry::Config#{[],[]=} in Pry::Config#method_missing. --- lib/pry/config.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 76540ac8..6a81e224 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -27,9 +27,9 @@ class Pry::Config key = name.to_s if key[-1] == "=" short_key = key.to_s[0..-2] - @lookup[short_key] = args[0] + self[short_key] = args[0] elsif @lookup.has_key?(key) - @lookup[key] + self[key] elsif @default.respond_to?(name) @default.public_send(name, *args, &block) else @@ -62,6 +62,4 @@ class Pry::Config def quiet? quiet end - -private end From 57f2a66612c30dd6022c4c03f8ae74897d0edcac Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 12:43:32 +0100 Subject: [PATCH 053/186] remove dead code from Pry::Config#initialize. --- lib/pry/config.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 6a81e224..317ce861 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -10,9 +10,6 @@ class Pry::Config def initialize(default = Pry.config) @default = default @lookup = {} - configure_gist - configure_ls - configure_history end def [](key) From da323ef62a5cccabe447afa9d62e436e540324ef Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 13:00:28 +0100 Subject: [PATCH 054/186] raise a TypeError from Config#merge! when argument cannot be coerced. --- lib/pry/config.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 317ce861..1c307648 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -35,6 +35,7 @@ class Pry::Config end def merge!(other) + raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) other = other.to_hash keys, values = other.keys.map(&:to_s), other.values @lookup.merge! Hash[keys.zip(values)] From 591c3432ab2870db4694f2596426e624ed70d13f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 13:27:16 +0100 Subject: [PATCH 055/186] add Pry::Config::ASSIGNMENT. --- lib/pry/config.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 1c307648..54780c36 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -2,6 +2,7 @@ class Pry::Config require 'ostruct' require 'pry/config/default' require 'pry/config/convenience' + ASSIGNMENT = "=".freeze def self.shortcuts Convenience::SHORTCUTS @@ -22,7 +23,7 @@ class Pry::Config def method_missing(name, *args, &block) key = name.to_s - if key[-1] == "=" + if key[-1] == ASSIGNMENT short_key = key.to_s[0..-2] self[short_key] = args[0] elsif @lookup.has_key?(key) From c992f36dff1cfe95b84173fa4be63dcd28dc25c6 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 13:28:06 +0100 Subject: [PATCH 056/186] remove call of Module#private from pry_instance.rb --- lib/pry/pry_instance.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 41912fa2..b30cfff9 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -609,6 +609,4 @@ class Pry end def raise_up(*args); raise_up_common(false, *args); end def raise_up!(*args); raise_up_common(true, *args); end - -private end From 072da52a7c05510fe8e8cad37196aaf77500da83 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 13:40:55 +0100 Subject: [PATCH 057/186] Pry::Config::Default#initialize accepts 0 arguments. Pry::Config#initialize accepts a single (optional) argument. the optional argument is a default (an instance of Pry::Config or Pry::Config::Default). an instance of Pry::Config::Default doesn't have a default to fall back on. --- lib/pry/config/default.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 4f4bd2fe..bc671566 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -42,14 +42,16 @@ class Pry::Config::Default < Pry::Config } } - def initialize(*) + def initialize super(nil) configure_ls configure_gist configure_history end - state.each { |key, value| define_method(key, &value) } + state.each do |key, value| + define_method(key, &value) + end private # TODO: From ef53b2f2c5ef790ff33a23653e97956fadf9eb2b Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 13:51:52 +0100 Subject: [PATCH 058/186] tidy up code in Pry::Config. --- lib/pry/config.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 54780c36..a985711c 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -24,7 +24,7 @@ class Pry::Config def method_missing(name, *args, &block) key = name.to_s if key[-1] == ASSIGNMENT - short_key = key.to_s[0..-2] + short_key = key[0..-2] self[short_key] = args[0] elsif @lookup.has_key?(key) self[key] @@ -55,7 +55,7 @@ class Pry::Config end def to_h - to_hash + @lookup end def quiet? From 71251c5e7f0c11fb88dbd91e14dda2b8fff08dc1 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 16:00:22 +0100 Subject: [PATCH 059/186] fix (most of) sticky_local_spec.rb. --- lib/pry/pry_instance.rb | 4 ++-- spec/sticky_locals_spec.rb | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index b30cfff9..a03441f6 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -153,7 +153,7 @@ class Pry # @yield The block that defines the content of the local. The local # will be refreshed at each tick of the repl loop. def add_sticky_local(name, &block) - sticky_locals[name] = block + config.extra_sticky_locals[name] = block end def sticky_locals @@ -165,7 +165,7 @@ class Pry _dir_: last_dir, _: last_result, __: output_array[-2] - } + }.merge(config.extra_sticky_locals) end # Reset the current eval string. If the user has entered part of a multiline diff --git a/spec/sticky_locals_spec.rb b/spec/sticky_locals_spec.rb index bda34ffa..0bf933ca 100644 --- a/spec/sticky_locals_spec.rb +++ b/spec/sticky_locals_spec.rb @@ -44,13 +44,11 @@ describe "Sticky locals (_file_ and friends)" do describe "setting as Pry.config option" do it 'should define a new sticky local for the session (normal value)' do Pry.config.extra_sticky_locals[:test_local] = :john - o = Object.new redirect_pry_io(InputTester.new("@value = test_local", "exit-all")) do Pry.start(o) end - o.instance_variable_get(:@value).should == :john Pry.config.extra_sticky_locals = {} end From 185004ec68edd74b7dd08e723c6c850d06d40f4f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 16:14:00 +0100 Subject: [PATCH 060/186] cache value on access (fixes sticky_locals_spec.rb). thanks @yui-knk! --- lib/pry/config/default.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index bc671566..e1b7dddd 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -50,7 +50,14 @@ class Pry::Config::Default < Pry::Config end state.each do |key, value| - define_method(key, &value) + define_method(key) do + ivar = "@#{key}" + if instance_variable_defined?(ivar) + return instance_variable_get(ivar) + else + instance_variable_set(ivar, value.call) + end + end end private From c0dd290d4f6a478d8e3c8640aab8d9137c99cbcd Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 22:44:05 +0100 Subject: [PATCH 061/186] avoid set of too many instance variables via Proc/local. --- lib/pry/config/default.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index e1b7dddd..f3aa3f37 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -51,12 +51,10 @@ class Pry::Config::Default < Pry::Config state.each do |key, value| define_method(key) do - ivar = "@#{key}" - if instance_variable_defined?(ivar) - return instance_variable_get(ivar) - else - instance_variable_set(ivar, value.call) + if state[key] == value + state[key] = value.call end + state[key] end end From 0f8afdd010655426f06dd4b41a1bf849bc34238e Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 22:52:19 +0100 Subject: [PATCH 062/186] rename 'state' local as 'default'. --- lib/pry/config/default.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index f3aa3f37..7c5576a8 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,5 +1,5 @@ class Pry::Config::Default < Pry::Config - state = { + default = { :input => proc { Readline }, :output => proc { $stdout }, :commands => proc { Pry::Commands }, @@ -49,12 +49,12 @@ class Pry::Config::Default < Pry::Config configure_history end - state.each do |key, value| + default.each do |key, value| define_method(key) do - if state[key] == value - state[key] = value.call + if default[key] == value + default[key] = value.call end - state[key] + default[key] end end From bc9c75e54693d9241e96b3a24d2acd92f69f1a6f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 23:02:23 +0100 Subject: [PATCH 063/186] support old Pry#hooks= API (argument as a Hash) on Pry::Config. --- lib/pry/config.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index a985711c..8c5fd456 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -58,6 +58,20 @@ class Pry::Config @lookup end + # + # FIXME + # @param [Pry::Hooks] hooks + # + def hooks=(hooks) + if hooks.is_a?(Hash) + warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object " \ + "instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks" + self["hooks"] = Pry::Hooks.from_hash(hooks) + else + self["hooks"] = hooks + end + end + def quiet? quiet end From f4dc366b9c619521f9471535365434b6a698991a Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 23:13:31 +0100 Subject: [PATCH 064/186] Pry::Config::Default#*_completer should return Proc's. --- lib/pry/config/default.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 7c5576a8..077cb1ee 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -31,14 +31,16 @@ class Pry::Config::Default < Pry::Config :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, :memory_size => proc { 100 }, :extra_sticky_locals => proc { {} }, - :command_completer => proc { Pry.commands.commands.keys }, - :file_completer => proc { Dir["."] }, + :command_completer => proc { proc { Pry.commands.commands.keys } }, + :file_completer => proc { proc { Dir["."] } }, :completer => proc { - if defined?(Bond) && Readline::VERSION !~ /editline/i - Pry::BondCompleter.start - else - Pry::InputCompleter.start - end + proc { + if defined?(Bond) && Readline::VERSION !~ /editline/i + Pry::BondCompleter.start + else + Pry::InputCompleter.start + end + } } } From 5204a89af483297c00356ac9bcc36bccd1e87bfc Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 20 Jan 2014 23:19:51 +0100 Subject: [PATCH 065/186] Pry::Config::Default#collision_warning defaults to false (as on master). --- lib/pry/config/default.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 077cb1ee..2cf94ffd 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -23,7 +23,7 @@ class Pry::Config::Default < Pry::Config :command_prefix => proc { "" }, :auto_indent => proc { Pry::Helpers::BaseHelpers.use_ansi_codes? }, :correct_indent => proc { true }, - :collision_warning => proc { true }, + :collision_warning => proc { false }, :output_prefix => proc { "=> "}, :requires => proc { [] }, :should_load_requires => proc { true }, From ad3fa34e48a15fcba9210e02e1ab7e6c055c1fa6 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 00:08:13 +0100 Subject: [PATCH 066/186] =?UTF-8?q?add=20Pry::Config.from=5Fhash(=E2=80=A6?= =?UTF-8?q?).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit suitable as a OpenStruct replacement (for Pry & Pry::Config). --- lib/pry/config.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 8c5fd456..a988a9d7 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -8,6 +8,12 @@ class Pry::Config Convenience::SHORTCUTS end + def self.from_hash(hash) + new(nil).tap do |config| + config.merge!(hash) + end + end + def initialize(default = Pry.config) @default = default @lookup = {} From 7c63654206bdd330b0e35528ef62f5325b9b9bc5 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 00:10:14 +0100 Subject: [PATCH 067/186] accept a default as second argument to Pry::Config.from_hash(). the default argument will fallback to 'nil'(no default) unless an explicit default is given. --- lib/pry/config.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index a988a9d7..7aded635 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -8,8 +8,8 @@ class Pry::Config Convenience::SHORTCUTS end - def self.from_hash(hash) - new(nil).tap do |config| + def self.from_hash(hash, default = nil) + new(default).tap do |config| config.merge!(hash) end end From 19c301696b4cfeee5d36f2305e956c8547de4333 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 00:21:29 +0100 Subject: [PATCH 068/186] add Pry::Config::Behavior. a Module Pry::Config and Pry::Config::Default can use to share common behavior. before now ::Default was a subclass of Pry::Config but are class methods(such as Pry::Config.from_hash()) that do not make sense on Pry::Config::Default. --- lib/pry/config.rb | 57 ++------------------------------------ lib/pry/config/behavior.rb | 57 ++++++++++++++++++++++++++++++++++++++ lib/pry/config/default.rb | 4 ++- 3 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 lib/pry/config/behavior.rb diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 7aded635..f6308ed4 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,8 +1,9 @@ class Pry::Config require 'ostruct' + require 'pry/config/behavior' require 'pry/config/default' require 'pry/config/convenience' - ASSIGNMENT = "=".freeze + include Pry::Config::Behavior def self.shortcuts Convenience::SHORTCUTS @@ -14,56 +15,6 @@ class Pry::Config end end - def initialize(default = Pry.config) - @default = default - @lookup = {} - end - - def [](key) - @lookup[key.to_s] - end - - def []=(key, value) - @lookup[key.to_s] = value - end - - def method_missing(name, *args, &block) - key = name.to_s - if key[-1] == ASSIGNMENT - short_key = key[0..-2] - self[short_key] = args[0] - elsif @lookup.has_key?(key) - self[key] - elsif @default.respond_to?(name) - @default.public_send(name, *args, &block) - else - nil - end - end - - def merge!(other) - raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) - other = other.to_hash - keys, values = other.keys.map(&:to_s), other.values - @lookup.merge! Hash[keys.zip(values)] - end - - def respond_to?(name, boolean=false) - @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) - end - - def refresh - @lookup = {} - end - - def to_hash - @lookup - end - - def to_h - @lookup - end - # # FIXME # @param [Pry::Hooks] hooks @@ -77,8 +28,4 @@ class Pry::Config self["hooks"] = hooks end end - - def quiet? - quiet - end end diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb new file mode 100644 index 00000000..36d45313 --- /dev/null +++ b/lib/pry/config/behavior.rb @@ -0,0 +1,57 @@ +module Pry::Config::Behavior + ASSIGNMENT = "=".freeze + + def initialize(default = Pry.config) + @default = default + @lookup = {} + end + + def [](key) + @lookup[key.to_s] + end + + def []=(key, value) + @lookup[key.to_s] = value + end + + def method_missing(name, *args, &block) + key = name.to_s + if key[-1] == ASSIGNMENT + short_key = key[0..-2] + self[short_key] = args[0] + elsif @lookup.has_key?(key) + self[key] + elsif @default.respond_to?(name) + @default.public_send(name, *args, &block) + else + nil + end + end + + def merge!(other) + raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) + other = other.to_hash + keys, values = other.keys.map(&:to_s), other.values + @lookup.merge! Hash[keys.zip(values)] + end + + def respond_to?(name, boolean=false) + @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) + end + + def refresh + @lookup = {} + end + + def to_hash + @lookup + end + + def to_h + @lookup + end + + def quiet? + quiet + end +end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 2cf94ffd..80691093 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,4 +1,6 @@ -class Pry::Config::Default < Pry::Config +class Pry::Config::Default + include Pry::Config::Behavior + default = { :input => proc { Readline }, :output => proc { $stdout }, From 3b3a97d682da34a36407e040568d491849c4c768 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 08:48:24 +0100 Subject: [PATCH 069/186] compare with #equal? (object identity) in Pry::Config::Default. --- lib/pry/config/default.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 80691093..3faf0d0e 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -55,7 +55,7 @@ class Pry::Config::Default default.each do |key, value| define_method(key) do - if default[key] == value + if default[key].equal?(value) default[key] = value.call end default[key] From eab4241ff64265f323d6bbf70b779be1eba2bc31 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 08:49:47 +0100 Subject: [PATCH 070/186] Default#completer should return InputCompleter or Bond (not a Proc). --- lib/pry/config/default.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 3faf0d0e..fb0b2c33 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -36,13 +36,11 @@ class Pry::Config::Default :command_completer => proc { proc { Pry.commands.commands.keys } }, :file_completer => proc { proc { Dir["."] } }, :completer => proc { - proc { - if defined?(Bond) && Readline::VERSION !~ /editline/i - Pry::BondCompleter.start - else - Pry::InputCompleter.start - end - } + if defined?(Bond) && Readline::VERSION !~ /editline/i + Pry::BondCompleter.start + else + Pry::InputCompleter.start + end } } From 325016eb3a41dec75e2c2ec35de80fb620b51d20 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 09:13:26 +0100 Subject: [PATCH 071/186] fix the failing Pry#prompt specs. this is a strange and odd case. Pry.prompt is a delegate to Pry.config, as it has always been. the same delegate was setup on an instance of Pry, but never used because we define #prompt and #prompt= with our implementation. the thing that would make the most sense (to me) is to not support Pry.prompt anymore and recommend the use of Pry.config.prompt instead. a lot of code relies on Pry.prompt though, so we have to support the delegate to config and implement custom behavior for the pry instance. --- lib/pry/config/convenience.rb | 1 - lib/pry/pry_class.rb | 8 ++++++++ lib/pry/pry_instance.rb | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/convenience.rb b/lib/pry/config/convenience.rb index b814c773..9d9273df 100644 --- a/lib/pry/config/convenience.rb +++ b/lib/pry/config/convenience.rb @@ -3,7 +3,6 @@ module Pry::Config::Convenience :input, :output, :commands, - :prompt, :print, :exception_handler, :quiet?, diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 6d5ebd49..c3cb2ada 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -20,6 +20,14 @@ class Pry extend Pry::Config::Convenience config_shortcut *Pry::Config.shortcuts + + def prompt=(value) + config.prompt = value + end + + def prompt + config.prompt + end end def self.main diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index a03441f6..138cd403 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -69,6 +69,7 @@ class Pry @backtrace = options[:backtrace] || caller @config = Pry::Config.new @config.merge!(options) + push_prompt(config.prompt) @input_array = Pry::HistoryArray.new config.memory_size @output_array = Pry::HistoryArray.new config.memory_size @custom_completions = config.command_completer @@ -78,6 +79,26 @@ class Pry exec_hook(:when_started, options[:target], options, self) end + # The current prompt. + # This is the prompt at the top of the prompt stack. + # + # @example + # self.prompt = Pry::SIMPLE_PROMPT + # self.prompt # => Pry::SIMPLE_PROMPT + # + # @return [Array] Current prompt. + def prompt + prompt_stack.last + end + + def prompt=(new_prompt) + if prompt_stack.empty? + push_prompt new_prompt + else + prompt_stack[-1] = new_prompt + end + end + # Initialize this instance by pushing its initial context into the binding # stack. If no target is given, start at the top level. def push_initial_binding(target=nil) From fea2e22c0da39f7f578e2bd13b5492c79b523e2e Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 09:26:50 +0100 Subject: [PATCH 072/186] coerce :hooks (as a Hash) to Pry::Hooks instance in Pry.start(). --- lib/pry/hooks.rb | 1 + lib/pry/pry_class.rb | 2 +- lib/pry/pry_instance.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pry/hooks.rb b/lib/pry/hooks.rb index f234ab5a..f902d58e 100644 --- a/lib/pry/hooks.rb +++ b/lib/pry/hooks.rb @@ -18,6 +18,7 @@ class Pry # @param [Hash] hash The hash to convert to `Pry::Hooks`. # @return [Pry::Hooks] The resulting `Pry::Hooks` instance. def self.from_hash(hash) + return hash if hash.instance_of?(self) instance = new hash.each do |k, v| instance.add_hook(k, nil, v) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index c3cb2ada..296ba23d 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -119,7 +119,7 @@ class Pry end options[:target] = Pry.binding_for(target || toplevel_binding) - + options[:hooks] = Pry::Hooks.from_hash options.delete(:hooks) if options.key?(:hooks) initial_session_setup # Unless we were given a backtrace, save the current one diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 138cd403..e2b287e6 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -68,7 +68,7 @@ class Pry @eval_string = "" @backtrace = options[:backtrace] || caller @config = Pry::Config.new - @config.merge!(options) + config.merge!(options) push_prompt(config.prompt) @input_array = Pry::HistoryArray.new config.memory_size @output_array = Pry::HistoryArray.new config.memory_size From f3cde86a79f8f546ec42fd91bf84419ca32d5f02 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 10:53:45 +0100 Subject: [PATCH 073/186] last spec, green! (pry load order also is a PITA). --- lib/pry/command.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 158fa3f4..77d9f9b8 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -175,7 +175,7 @@ class Pry end def command_regex - pr = defined?(Pry.config.command_prefix) ? Pry.config.command_prefix : "" + pr = Pry.respond_to?(:config) ? Pry.config.command_prefix : "" prefix = convert_to_regex(pr) prefix = "(?:#{prefix})?" unless options[:use_prefix] From d544db36905c46c44ab75ab0b71fe3076ca253fd Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 11:06:58 +0100 Subject: [PATCH 074/186] remove leftovers of OpenStruct dependency from Pry::Config. --- lib/pry.rb | 1 + lib/pry/config.rb | 1 - lib/pry/config/default.rb | 10 ++++------ 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 9d698db0..845b81e2 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -164,6 +164,7 @@ begin rescue LoadError end +require "ostruct" require 'pry/version' require 'pry/repl' require 'pry/rbx_path' diff --git a/lib/pry/config.rb b/lib/pry/config.rb index f6308ed4..2f8f3aef 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,5 +1,4 @@ class Pry::Config - require 'ostruct' require 'pry/config/behavior' require 'pry/config/default' require 'pry/config/convenience' diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index fb0b2c33..3bc92eb4 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -65,7 +65,7 @@ private # all of this configure_* stuff is a relic of old code. # we should try move this code to being command-local. def configure_ls - @lookup["ls"] = OpenStruct.new({ + @lookup["ls"] = Pry::Config.from_hash({ :heading_color => :bright_blue, :public_method_color => :default, :private_method_color => :blue, @@ -88,14 +88,12 @@ private end def configure_gist - @lookup["gist"] = OpenStruct.new - gist.inspecter = proc(&:pretty_inspect) + @lookup["gist"] = Pry::Config.from_hash(inspecter: proc(&:pretty_inspect)) end def configure_history - @lookup["history"] = OpenStruct.new - history.should_save = true - history.should_load = true + @lookup["history"] = Pry::Config.from_hash "should_save" => true, + "should_load" => true history.file = File.expand_path("~/.pry_history") rescue nil if history.file.nil? self.should_load_rc = false From 91d412c044f174a2c50d1583a3f34c1f0f795e7d Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 11:11:42 +0100 Subject: [PATCH 075/186] remove openstruct dependency from all of pry. --- lib/pry.rb | 2 +- lib/pry/command.rb | 2 +- lib/pry/plugins.rb | 2 +- lib/pry/pry_instance.rb | 4 ++-- spec/helper.rb | 2 +- spec/prompt_spec.rb | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 845b81e2..5fd42609 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -102,7 +102,7 @@ class Pry else # Otherwise, saves current binding stack as old stack and pops last # binding out of binding stack (the old stack still has that binding). - _pry_.command_state["cd"] ||= OpenStruct.new # FIXME + _pry_.command_state["cd"] ||= Pry::Config.from_hash({}) # FIXME _pry_.command_state['cd'].old_stack = _pry_.binding_stack.dup _pry_.binding_stack.pop end diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 77d9f9b8..0527ade7 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -284,7 +284,7 @@ class Pry # state.my_state = "my state" # this will not conflict with any # # `state.my_state` used in another command. def state - _pry_.command_state[match] ||= OpenStruct.new + _pry_.command_state[match] ||= Pry::Config.from_hash({}) end # Revaluate the string (str) and perform interpolation. diff --git a/lib/pry/plugins.rb b/lib/pry/plugins.rb index e9bce09a..0813be35 100644 --- a/lib/pry/plugins.rb +++ b/lib/pry/plugins.rb @@ -43,7 +43,7 @@ class Pry # Does not reload plugin if it's already active. def activate! # Create the configuration object for the plugin. - Pry.config.send("#{gem_name.gsub('-', '_')}=", OpenStruct.new) + Pry.config.send("#{gem_name.gsub('-', '_')}=", Pry::Config.from_hash({})) begin require gem_name if !active? diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index e2b287e6..647bc960 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -522,7 +522,7 @@ class Pry open_token = @indent.open_delimiters.any? ? @indent.open_delimiters.last : @indent.stack.last - c = OpenStruct.new( + c = Pry::Config.from_hash({ :object => object, :nesting_level => binding_stack.size - 1, :open_token => open_token, @@ -533,7 +533,7 @@ class Pry :binding_stack => binding_stack, :input_array => input_array, :eval_string => @eval_string, - :cont => !@eval_string.empty?) + :cont => !@eval_string.empty?}) Pry.critical_section do # If input buffer is empty then use normal prompt diff --git a/spec/helper.rb b/spec/helper.rb index f48fc108..60193949 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -9,7 +9,7 @@ unless Object.const_defined? 'Pry' end require 'mocha/api' - +require "ostruct" require 'pry/test/helper' require 'spec_helpers/bacon' require 'spec_helpers/mock_pry' diff --git a/spec/prompt_spec.rb b/spec/prompt_spec.rb index 5de2a442..b7aa79a5 100644 --- a/spec/prompt_spec.rb +++ b/spec/prompt_spec.rb @@ -7,7 +7,7 @@ describe "Prompts" do redirect_pry_io(InputTester.new("exit-all")) do Pry.start(self, :prompt => proc { |v| config = v }) end - config.is_a?(OpenStruct).should == true + config.is_a?(Pry::Config).should == true end it 'should get full config object, when using a proc array' do @@ -15,7 +15,7 @@ describe "Prompts" do redirect_pry_io(InputTester.new("exit-all")) do Pry.start(self, :prompt => [proc { |v| config1 = v }, proc { |v| config2 = v }]) end - config1.is_a?(OpenStruct).should == true + config1.is_a?(Pry::Config).should == true end it 'should receive correct data in the config object' do From 7723550d8c1245d25d0f84bcb9218cb578891eab Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 11:12:19 +0100 Subject: [PATCH 076/186] delete require of 'ostruct' --- lib/pry.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pry.rb b/lib/pry.rb index 5fd42609..856b8aa8 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -164,7 +164,6 @@ begin rescue LoadError end -require "ostruct" require 'pry/version' require 'pry/repl' require 'pry/rbx_path' From 9da6c98fd6cf0a7d6ec00c7d09d9939da68a6d92 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Wed, 22 Jan 2014 00:32:03 +0900 Subject: [PATCH 077/186] Add spec for Pry::Config --- spec/config_spec.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 spec/config_spec.rb diff --git a/spec/config_spec.rb b/spec/config_spec.rb new file mode 100644 index 00000000..7829ca81 --- /dev/null +++ b/spec/config_spec.rb @@ -0,0 +1,64 @@ +require 'helper' + +describe Pry::Config do + describe "local config" do + it "should be set" do + t = pry_tester + t.eval "_pry_.config.foobar = 'hello'" + t.eval("_pry_.config.foobar").should == 'hello' + end + + it "should be set (array)" do + t = pry_tester + t.eval "_pry_.config.foobar = []" + t.eval "_pry_.config.foobar << 1 << 2" + t.eval("_pry_.config.foobar").should == [1, 2] + end + + it "should be global config value when local config is not set" do + Pry.config.foobar = 'hello' + t = pry_tester + t.eval("_pry_.config.foobar").should == 'hello' + Pry.config.foobar = nil + end + + it "should be local config value when local config is set" do + Pry.config.foobar = 'hello' + t = pry_tester + t.eval "_pry_.config.foobar = 'goodbye'" + t.eval("_pry_.config.foobar").should == 'goodbye' + Pry.config.foobar = nil + end + end + + describe "global config" do + it "should be set" do + Pry.config.foobar = 'hello' + Pry.config.foobar.should == 'hello' + Pry.config.foobar = nil + end + + it "should be set (array)" do + Pry.config.foobar = [] + Pry.config.foobar << 1 << 2 + Pry.config.foobar.should == [1, 2] + Pry.config.foobar = nil + end + + it "should keep value when local config is set" do + Pry.config.foobar = 'hello' + t = pry_tester + t.eval "_pry_.config.foobar = 'goodbye'" + Pry.config.foobar.should == 'hello' + Pry.config.foobar = nil + end + + it "should keep value when local config is set (array)" do + Pry.config.foobar = [1, 2] + t = pry_tester + t.eval "_pry_.config.foobar << 3 << 4" + Pry.config.foobar.should == [1, 2] + Pry.config.foobar = nil + end + end +end From bb1418c623fdb9cdaea3a0dbff3d01247439a4e3 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 23 Jan 2014 22:13:53 -0800 Subject: [PATCH 078/186] Minor space changes per @rf- --- lib/pry/commands/watch_expression.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 4fe1746e..0ced7ece 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -1,7 +1,6 @@ class Pry class Command::WatchExpression < Pry::ClassCommand require 'pry/commands/watch_expression/expression.rb' - extend Pry::Helpers::BaseHelpers match 'watch' group 'Context' @@ -25,22 +24,21 @@ class Pry end def process - ret = case - when opts.present?(:delete) - delete opts[:delete] - when opts.present?(:list) || args.empty? - list - else - add_hook - add_expression(args) - end + case + when opts.present?(:delete) + delete opts[:delete] + when opts.present?(:list) || args.empty? + list + else + add_hook + add_expression(args) + end end private def expressions - state.expressions ||= [] - state.expressions + state.expressions ||= [] end def delete(index) @@ -85,7 +83,7 @@ class Pry def add_hook hook = [:after_eval, :watch_expression] - unless Pry.config.hooks.hook_exists? *hook + unless Pry.config.hooks.hook_exists?(*hook) _pry_.hooks.add_hook(*hook) do eval_and_print_changed end From 483728fcc78deb478c3fe8db4629b3b63fca18a2 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 23 Jan 2014 23:00:12 -0800 Subject: [PATCH 079/186] Fix hook name in test --- spec/commands/watch_expression_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index 71984e58..ea10ae87 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -18,7 +18,7 @@ describe "watch expression" do eval "watch --delete" end - it "registers the before_session hook" do + it "registers the after_eval hook" do eval 'watch 1+1' @tester.pry.hooks.hook_exists?(:after_eval, :watch_expression).should == true end From 7ff98c60a43cfb50b57e769965fdb3455e49a9f7 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 23 Jan 2014 23:00:20 -0800 Subject: [PATCH 080/186] Implement expression change test (per @rf-) --- spec/commands/watch_expression_spec.rb | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index ea10ae87..4801bdb3 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -38,10 +38,31 @@ describe "watch expression" do eval('watch').should =~ /=> :bar/ end - #it "prints only when an expression changes" do - # # TODO: This is one of the main features, but I am not sure how to test the - # # output from a hook. - #end + it "prints when an expression changes" do + ReplTester.start do + input 'a = 1' + output '=> 1' + + input 'watch a' + output "Watching a\nwatch: a => 1" + + input "a = 2" + output "watch: a => 2\n=> 2" + end + end + + it "doesn't print when an expresison remains the same" do + ReplTester.start do + input 'a = 1' + output '=> 1' + + input 'watch a' + output "Watching a\nwatch: a => 1" + + input "a = 1" + output "=> 1" + end + end describe "deleting expressions" do before do From b4ad243570d51802bf9a886264375b0e379f32aa Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 23 Jan 2014 23:01:53 -0800 Subject: [PATCH 081/186] remove unnecessary temporary --- lib/pry/commands/watch_expression.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 0ced7ece..ee28a1b1 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -76,8 +76,7 @@ class Pry end def add_expression(arguments) - e = expressions - e << Expression.new(target, arg_string) + expressions << Expression.new(target, arg_string) output.puts "Watching #{Code.new(arg_string)}" end From 0320ae8471cbd02c04fd7c1f329cd2844bd6d545 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 23 Jan 2014 23:36:46 -0800 Subject: [PATCH 082/186] Make watch expressions fully global At the moment Pry::Hooks are not local to each instance, so the hook was only being added once. This caused problems when you opened two binding.pry's in one program, as the second one's watch expressions appeared to be ignored. --- lib/pry/commands/watch_expression.rb | 10 +++++----- spec/commands/watch_expression_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index ee28a1b1..05d8e36e 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -38,7 +38,7 @@ class Pry private def expressions - state.expressions ||= [] + Pry.config.watch_expressions ||= [] end def delete(index) @@ -66,7 +66,7 @@ class Pry end end - def eval_and_print_changed + def eval_and_print_changed(output) expressions.each do |expr| expr.eval! if expr.changed? @@ -82,9 +82,9 @@ class Pry def add_hook hook = [:after_eval, :watch_expression] - unless Pry.config.hooks.hook_exists?(*hook) - _pry_.hooks.add_hook(*hook) do - eval_and_print_changed + unless Pry.hooks.hook_exists?(*hook) + Pry.hooks.add_hook(*hook) do |_, _pry_| + eval_and_print_changed _pry_.output end end end diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index 4801bdb3..e87d8be4 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -64,6 +64,30 @@ describe "watch expression" do end end + it "continues to work if you start a second pry instance" do + ReplTester.start do + input 'a = 1' + output '=> 1' + + input 'watch a' + output "Watching a\nwatch: a => 1" + + input "a = 2" + output "watch: a => 2\n=> 2" + end + + ReplTester.start do + input 'b = 1' + output '=> 1' + + input 'watch b' + output "Watching b\nwatch: b => 1" + + input "b = 2" + output "watch: b => 2\n=> 2" + end + end + describe "deleting expressions" do before do eval 'watch :keeper' From 3e0d76458feadd4e565d13269eb9703df008f1f0 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 24 Jan 2014 00:16:16 -0800 Subject: [PATCH 083/186] Fix watch tests --- spec/commands/watch_expression_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index e87d8be4..ea62cedd 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -8,7 +8,7 @@ describe "watch expression" do # 3) Return the output def eval(expr) output = @tester.eval expr - @tester.pry.hooks.exec_hook :after_eval + @tester.pry.hooks.exec_hook :after_eval, nil, @tester.pry output end From c7386aebc2bd2977a1234cffbb001e9a22a0b357 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 24 Jan 2014 00:24:42 -0800 Subject: [PATCH 084/186] Remove level of indirection --- lib/pry/commands/ls/ls_entity.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/pry/commands/ls/ls_entity.rb b/lib/pry/commands/ls/ls_entity.rb index 7d0b4883..a8958d19 100644 --- a/lib/pry/commands/ls/ls_entity.rb +++ b/lib/pry/commands/ls/ls_entity.rb @@ -29,34 +29,32 @@ class Pry private - def greppable - proc do |entity| - entity.tap { |o| o.grep = @grep } - end + def grep(entity) + entity.tap { |o| o.grep = @grep } end def globals - greppable.call(Globals.new(@target, @opts)) + grep(Globals.new(@target, @opts)) end def constants - greppable.call(Constants.new(@interrogatee, @target, @no_user_opts, @opts)) + grep(Constants.new(@interrogatee, @target, @no_user_opts, @opts)) end def methods - greppable.call(Methods.new(@interrogatee, @no_user_opts, @opts)) + grep(Methods.new(@interrogatee, @no_user_opts, @opts)) end def self_methods - greppable.call(SelfMethods.new(@interrogatee, @no_user_opts, @opts)) + grep(SelfMethods.new(@interrogatee, @no_user_opts, @opts)) end def instance_vars - greppable.call(InstanceVars.new(@interrogatee, @no_user_opts, @opts)) + grep(InstanceVars.new(@interrogatee, @no_user_opts, @opts)) end def local_names - greppable.call(LocalNames.new(@target, @no_user_opts, @sticky_locals, @args)) + grep(LocalNames.new(@target, @no_user_opts, @sticky_locals, @args)) end def local_vars From f83ee346fc1f93b307a8607184bafefcfebafb58 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 26 Jan 2014 01:07:48 +0900 Subject: [PATCH 085/186] Go to interactive_mode end-of-input is raised When pry is run with file which causes syntax error (expecting end-of-input), gose to interactive_mode. This is same when other errors are raised. This commit will fix #1098. --- lib/pry/repl_file_loader.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/pry/repl_file_loader.rb b/lib/pry/repl_file_loader.rb index b587cb2e..f1ffe035 100644 --- a/lib/pry/repl_file_loader.rb +++ b/lib/pry/repl_file_loader.rb @@ -44,6 +44,11 @@ class Pry content.lines.each do |line| break unless _pry_.eval line, :generated => true end + + unless _pry_.eval_string.empty? + _pry_.output.puts "#{_pry_.eval_string}...exception encountered, going interactive!" + interactive_mode(_pry_) + end end # Define a few extra commands useful for flipping back & forth From a987a8e7c250568264e49b4b90a0a1760b15c67e Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Sat, 25 Jan 2014 22:55:47 +0100 Subject: [PATCH 086/186] Fix bug in edit regarding file names with path but no suffix There was a bug in Command::Edit#probably_a_file? due to missing parentheses in a method call followed by an operator. --- CHANGELOG.md | 1 + lib/pry/commands/edit.rb | 2 +- spec/commands/edit_spec.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ec1ea1c..b28c9195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ being inferred from context (#877) * Rename `--installed-plugins` flag to `--plugins` * Strip ANSI codes from prompt before measuring length for indentation (#493) +* Fix bug in `edit` regarding recognition of file names without suffix. #### Dev-facing changes * `rake pry` now accepts switches prefixed with `_` (e.g., `rake pry _v`) diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index a3b55999..dd00c5ec 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -186,7 +186,7 @@ class Pry end def probably_a_file?(str) - [".rb", ".c", ".py", ".yml", ".gemspec"].include? File.extname(str) || + [".rb", ".c", ".py", ".yml", ".gemspec"].include?(File.extname(str)) || str =~ /\/|\\/ end diff --git a/spec/commands/edit_spec.rb b/spec/commands/edit_spec.rb index 5150aec4..fb7fee2f 100644 --- a/spec/commands/edit_spec.rb +++ b/spec/commands/edit_spec.rb @@ -35,6 +35,16 @@ describe "edit" do FileUtils.rm(@tf_path) if File.exists?(@tf_path) end + it "should not allow patching any known kind of file" do + ["file.rb", "file.c", "file.py", "file.yml", "file.gemspec", + "/tmp/file", "\\\\Temp\\\\file"].each do |file| + proc { + pry_eval "edit -p #{file}" + }.should.raise(NotImplementedError). + message.should =~ /Cannot yet patch false objects!/ + end + end + it "should invoke Pry.config.editor with absolutified filenames" do pry_eval 'edit lib/pry.rb' @file.should == File.expand_path('lib/pry.rb') From d3ed9ba893973652ef2ee468f418d42d83b91e71 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 25 Jan 2014 23:55:15 +0100 Subject: [PATCH 087/186] dup (most) values from default. track dirty keys. (WIP). --- lib/pry/config/behavior.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 36d45313..ceca8fe9 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,9 +1,12 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze + NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric] + DIRTY_MAP = {nil => []} def initialize(default = Pry.config) @default = default @lookup = {} + DIRTY_MAP[self] = [] end def [](key) @@ -18,11 +21,17 @@ module Pry::Config::Behavior key = name.to_s if key[-1] == ASSIGNMENT short_key = key[0..-2] + DIRTY_MAP[self] << short_key self[short_key] = args[0] + elsif DIRTY_MAP[@default].include?(key) + DIRTY_MAP[@default].delete(key) + value = @default.public_send(name, *args, &block) + self[key] = _dup(value) elsif @lookup.has_key?(key) self[key] elsif @default.respond_to?(name) - @default.public_send(name, *args, &block) + value = @default.public_send(name, *args, &block) + self[key] = _dup(value) else nil end @@ -54,4 +63,13 @@ module Pry::Config::Behavior def quiet? quiet end + +private + def _dup(value) + if NODUP.any? { |klass| klass === value } + value + else + value.dup + end + end end From c200c9404677069e1b351ecf7a63b79be14694fd Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 26 Jan 2014 09:16:51 +0100 Subject: [PATCH 088/186] freeze Behavior::NODUP. --- lib/pry/config/behavior.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index ceca8fe9..00973c4f 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,6 +1,6 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze - NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric] + NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze DIRTY_MAP = {nil => []} def initialize(default = Pry.config) @@ -21,7 +21,7 @@ module Pry::Config::Behavior key = name.to_s if key[-1] == ASSIGNMENT short_key = key[0..-2] - DIRTY_MAP[self] << short_key + DIRTY_MAP[self].push(short_key) self[short_key] = args[0] elsif DIRTY_MAP[@default].include?(key) DIRTY_MAP[@default].delete(key) From c524e88f4cf02384ba2c801c5ab7327c693cc0ce Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 26 Jan 2014 19:13:51 +0100 Subject: [PATCH 089/186] no more DIRTY_MAP (replace with @read_lookup). --- lib/pry/config/behavior.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 00973c4f..9b29414d 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,12 +1,11 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze - DIRTY_MAP = {nil => []} def initialize(default = Pry.config) @default = default @lookup = {} - DIRTY_MAP[self] = [] + @read_lookup = {} end def [](key) @@ -21,17 +20,14 @@ module Pry::Config::Behavior key = name.to_s if key[-1] == ASSIGNMENT short_key = key[0..-2] - DIRTY_MAP[self].push(short_key) self[short_key] = args[0] - elsif DIRTY_MAP[@default].include?(key) - DIRTY_MAP[@default].delete(key) - value = @default.public_send(name, *args, &block) - self[key] = _dup(value) elsif @lookup.has_key?(key) self[key] + elsif @read_lookup.has_key?(key) + @read_lookup[key] elsif @default.respond_to?(name) value = @default.public_send(name, *args, &block) - self[key] = _dup(value) + @read_lookup[key] = _dup(value) else nil end From bb79885787d7b04b29de44e3876de2c07fc89b0e Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 26 Jan 2014 19:56:09 +0100 Subject: [PATCH 090/186] one step closer to the behavior we want. $ rake pry [1] pry(main)> _pry_.config.requires => [] [2] pry(main)> _pry_.config.requires << 1 => [1] [3] pry(main)> Pry.config.requires = [1,2,3] => [1, 2, 3] [4] pry(main)> _pry_.config.requires => [1, 2, 3] [5] pry(main)> _pry_.config.requires = [42] => [42] [6] pry(main)> Pry.config.requires = [5667] => [5667] [7] pry(main)> _pry_.config.requires => [42] [8] pry(main)> exit --- Gemfile | 2 ++ lib/pry/config/behavior.rb | 4 +++- lib/pry/config/default.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 11553a13..fa029348 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,8 @@ group :development do gem 'rb-fsevent', :require => 'false' end +gem 'binding.repl' + if RbConfig::CONFIG['ruby_install_name'] == 'rbx' gem 'rubysl-singleton' gem 'rubysl-prettyprint' diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 9b29414d..8613d058 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,9 +1,10 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze - NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze + NODUP = [TrueClass, FalseClass, NilClass, String, Module, Proc, Numeric].freeze def initialize(default = Pry.config) @default = default + @default._register(self) if @default @lookup = {} @read_lookup = {} end @@ -20,6 +21,7 @@ module Pry::Config::Behavior key = name.to_s if key[-1] == ASSIGNMENT short_key = key[0..-2] + @default._forget(short_key) if @default self[short_key] = args[0] elsif @lookup.has_key?(key) self[key] diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 3bc92eb4..b37dc944 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -51,6 +51,14 @@ class Pry::Config::Default configure_history end + def _register(other) + @child = other + end + + def _forget(key) + @child.instance_variable_get(:@read_lookup).delete(key.to_s) + end + default.each do |key, value| define_method(key) do if default[key].equal?(value) From 7d0a8e351341f3438b7c8018584f43e31aa27df9 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 26 Jan 2014 21:31:58 +0100 Subject: [PATCH 091/186] use 'platform' on rubinius (over RbConfig) in Gemfile. --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 11553a13..bc55a05d 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ group :development do gem 'rb-fsevent', :require => 'false' end -if RbConfig::CONFIG['ruby_install_name'] == 'rbx' +platform :rbx do gem 'rubysl-singleton' gem 'rubysl-prettyprint' gem 'rb-readline' From 6ec764981390c64f5fe3d8410869974fdff052fe Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 06:00:29 +0100 Subject: [PATCH 092/186] add Behavior#inherited_by and Behavior#wipe!. many TODO's to clean up. --- lib/pry/config/behavior.rb | 29 +++++++++++++++++++++++------ lib/pry/config/default.rb | 8 -------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 8613d058..a86883ed 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,10 +1,10 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze - NODUP = [TrueClass, FalseClass, NilClass, String, Module, Proc, Numeric].freeze + NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze def initialize(default = Pry.config) @default = default - @default._register(self) if @default + @default.inherited_by(self) if @default @lookup = {} @read_lookup = {} end @@ -20,8 +20,8 @@ module Pry::Config::Behavior def method_missing(name, *args, &block) key = name.to_s if key[-1] == ASSIGNMENT + @inherited_by.wipe!(short_key) if @inherited_by short_key = key[0..-2] - @default._forget(short_key) if @default self[short_key] = args[0] elsif @lookup.has_key?(key) self[key] @@ -43,18 +43,35 @@ module Pry::Config::Behavior end def respond_to?(name, boolean=false) - @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) + @lookup.has_key?(name.to_s) or @read_lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) end - def refresh - @lookup = {} + def wipe!(key = nil) + if key == nil + @lookup = {} + @read_lookup = {} + else + @lookup.delete(key) + @read_lookup.delete(key) + end + end + + def inherited_by(other) + if @inherited_by + # TODO + raise + else + @inherited_by = other + end end def to_hash + # TODO: should merge read_lookup? @lookup end def to_h + # TODO: should merge read_lookup? @lookup end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index b37dc944..3bc92eb4 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -51,14 +51,6 @@ class Pry::Config::Default configure_history end - def _register(other) - @child = other - end - - def _forget(key) - @child.instance_variable_get(:@read_lookup).delete(key.to_s) - end - default.each do |key, value| define_method(key) do if default[key].equal?(value) From 3f089d43dfe5f94aaa93550febdef0b44f4b4fb5 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 06:11:15 +0100 Subject: [PATCH 093/186] add option to wipe from read/write lookup. don't delete writes. --- lib/pry/config/behavior.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index a86883ed..59644c51 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -20,7 +20,7 @@ module Pry::Config::Behavior def method_missing(name, *args, &block) key = name.to_s if key[-1] == ASSIGNMENT - @inherited_by.wipe!(short_key) if @inherited_by + @inherited_by.forget(:read, short_key) if @inherited_by short_key = key[0..-2] self[short_key] = args[0] elsif @lookup.has_key?(key) @@ -46,13 +46,20 @@ module Pry::Config::Behavior @lookup.has_key?(name.to_s) or @read_lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) end - def wipe!(key = nil) - if key == nil - @lookup = {} - @read_lookup = {} - else + def refresh + @lookup.clear + @read_lookup.clear + true + end + + def forget(lookup_type, key) + case lookup_type + when :write @lookup.delete(key) + when :read @read_lookup.delete(key) + else + raise ArgumentError, "specify a lookup type (:read or :write)" end end From 4bd07762c2cfe05b6d1cc9b9a172e6f9b521d096 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 06:19:45 +0100 Subject: [PATCH 094/186] assign short_key before we use it. --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 59644c51..3b9ffad8 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -20,8 +20,8 @@ module Pry::Config::Behavior def method_missing(name, *args, &block) key = name.to_s if key[-1] == ASSIGNMENT - @inherited_by.forget(:read, short_key) if @inherited_by short_key = key[0..-2] + @inherited_by.forget(:read, short_key) if @inherited_by self[short_key] = args[0] elsif @lookup.has_key?(key) self[key] From bc9ed393768ac4684580c66ff7648f58506b99d4 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 06:30:01 +0100 Subject: [PATCH 095/186] Behavior#[] lookup @read_lookup & @lookup. --- lib/pry/config/behavior.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 3b9ffad8..da6967bb 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -10,7 +10,8 @@ module Pry::Config::Behavior end def [](key) - @lookup[key.to_s] + lookup = @read_lookup.merge(@lookup) + lookup[key.to_s] end def []=(key, value) From 55783572d7b96ff10376c1bb8d20289e414ae666 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 06:37:49 +0100 Subject: [PATCH 096/186] ignore attempts to re-assign inherited_by. --- lib/pry/config/behavior.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index da6967bb..3ffea197 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -65,12 +65,7 @@ module Pry::Config::Behavior end def inherited_by(other) - if @inherited_by - # TODO - raise - else - @inherited_by = other - end + @inherited_by ||= other end def to_hash From 2975030fdec8fb5576c6f77f110246303b3b77b1 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 06:44:59 +0100 Subject: [PATCH 097/186] dup the default in Behavior#initialize & raise on reassign of @inherited_by. --- lib/pry/config/behavior.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 3ffea197..c7918927 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -2,9 +2,9 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze - def initialize(default = Pry.config) - @default = default - @default.inherited_by(self) if @default + def initialize(default = Pry.config.dup) + @default = default.dup if default + @default.inherited_by(self) if default @lookup = {} @read_lookup = {} end @@ -65,7 +65,11 @@ module Pry::Config::Behavior end def inherited_by(other) - @inherited_by ||= other + if @inherited_by + raise RuntimeError + else + @inherited_by = other + end end def to_hash From 3a55922fa937704d1590ec12c3c441c53fbe2fc4 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 07:02:55 +0100 Subject: [PATCH 098/186] add Behavior#key?(). --- lib/pry/config/behavior.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index c7918927..8c93b58d 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -44,7 +44,12 @@ module Pry::Config::Behavior end def respond_to?(name, boolean=false) - @lookup.has_key?(name.to_s) or @read_lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) + key?(name) or @default.respond_to?(name) or super(name, boolean) + end + + def key?(key) + key = key.to_s + @lookup.key?(key) or @read_lookup.key?(key) end def refresh From 805b5ab5f4c670cf5c44c6a19b17f09ffa181a72 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 07:16:10 +0100 Subject: [PATCH 099/186] remove duplicate use of 'dup' (irony). --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 8c93b58d..8292e81e 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -2,7 +2,7 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze - def initialize(default = Pry.config.dup) + def initialize(default = Pry.config) @default = default.dup if default @default.inherited_by(self) if default @lookup = {} From 4ba451fef5751a46f962660ed4e949a325865afc Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 10:06:06 +0100 Subject: [PATCH 100/186] guard against the assignment of reserved keys (add Behavior::RESERVED_KEYS). --- lib/pry/config/behavior.rb | 12 +++++++++++- spec/config_spec.rb | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 8292e81e..602008f1 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,6 +1,12 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze + RESERVED_KEYS = [ + "[]", "[]=", "merge!", + "respond_to?", "key?", "refresh", + "forget", "inherited_by", "to_h", + "to_hash" + ].freeze def initialize(default = Pry.config) @default = default.dup if default @@ -15,7 +21,11 @@ module Pry::Config::Behavior end def []=(key, value) - @lookup[key.to_s] = value + key = key.to_s + if RESERVED_KEYS.include?(key) + raise ArgumentError, "sorry, '#{key}' is a reserved configuration option." + end + @lookup[key] = value end def method_missing(name, *args, &block) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 7829ca81..82df56c1 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -1,6 +1,16 @@ require 'helper' describe Pry::Config do + describe "reserved keys" do + before do + @config = Pry::Config.from_hash({}, nil) + end + + it "raises an ArgumentError on assignment of a reserved key" do + should.raise(ArgumentError) { @config.inherited_by = "abc" } + end + end + describe "local config" do it "should be set" do t = pry_tester From 841cb5d309634e2ce6878112e170c74fa7aca2f4 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 10:13:10 +0100 Subject: [PATCH 101/186] '@lookup' => '@writes', '@read_lookup' => '@reads'. --- lib/pry/config/behavior.rb | 32 ++++++++++++++++---------------- lib/pry/config/default.rb | 6 +++--- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 602008f1..40f09b9b 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -11,12 +11,12 @@ module Pry::Config::Behavior def initialize(default = Pry.config) @default = default.dup if default @default.inherited_by(self) if default - @lookup = {} - @read_lookup = {} + @writes = {} + @reads = {} end def [](key) - lookup = @read_lookup.merge(@lookup) + lookup = @reads.merge(@writes) lookup[key.to_s] end @@ -25,7 +25,7 @@ module Pry::Config::Behavior if RESERVED_KEYS.include?(key) raise ArgumentError, "sorry, '#{key}' is a reserved configuration option." end - @lookup[key] = value + @writes[key] = value end def method_missing(name, *args, &block) @@ -34,13 +34,13 @@ module Pry::Config::Behavior short_key = key[0..-2] @inherited_by.forget(:read, short_key) if @inherited_by self[short_key] = args[0] - elsif @lookup.has_key?(key) + elsif @writes.has_key?(key) self[key] - elsif @read_lookup.has_key?(key) - @read_lookup[key] + elsif @reads.has_key?(key) + @reads[key] elsif @default.respond_to?(name) value = @default.public_send(name, *args, &block) - @read_lookup[key] = _dup(value) + @reads[key] = _dup(value) else nil end @@ -50,7 +50,7 @@ module Pry::Config::Behavior raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) other = other.to_hash keys, values = other.keys.map(&:to_s), other.values - @lookup.merge! Hash[keys.zip(values)] + @writes.merge! Hash[keys.zip(values)] end def respond_to?(name, boolean=false) @@ -59,21 +59,21 @@ module Pry::Config::Behavior def key?(key) key = key.to_s - @lookup.key?(key) or @read_lookup.key?(key) + @writes.key?(key) or @reads.key?(key) end def refresh - @lookup.clear - @read_lookup.clear + @writes.clear + @reads.clear true end def forget(lookup_type, key) case lookup_type when :write - @lookup.delete(key) + @writes.delete(key) when :read - @read_lookup.delete(key) + @reads.delete(key) else raise ArgumentError, "specify a lookup type (:read or :write)" end @@ -89,12 +89,12 @@ module Pry::Config::Behavior def to_hash # TODO: should merge read_lookup? - @lookup + @writes end def to_h # TODO: should merge read_lookup? - @lookup + @writes end def quiet? diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 3bc92eb4..990048a1 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -65,7 +65,7 @@ private # all of this configure_* stuff is a relic of old code. # we should try move this code to being command-local. def configure_ls - @lookup["ls"] = Pry::Config.from_hash({ + self["ls"] = Pry::Config.from_hash({ :heading_color => :bright_blue, :public_method_color => :default, :private_method_color => :blue, @@ -88,11 +88,11 @@ private end def configure_gist - @lookup["gist"] = Pry::Config.from_hash(inspecter: proc(&:pretty_inspect)) + self["gist"] = Pry::Config.from_hash(inspecter: proc(&:pretty_inspect)) end def configure_history - @lookup["history"] = Pry::Config.from_hash "should_save" => true, + self["history"] = Pry::Config.from_hash "should_save" => true, "should_load" => true history.file = File.expand_path("~/.pry_history") rescue nil if history.file.nil? From e0935c68515045b2b860f0910ced0659c621ac49 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 27 Jan 2014 12:33:55 +0100 Subject: [PATCH 102/186] read through Behavior#[] for read or write lookup. --- lib/pry/config/behavior.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 40f09b9b..8b08cc44 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -34,10 +34,8 @@ module Pry::Config::Behavior short_key = key[0..-2] @inherited_by.forget(:read, short_key) if @inherited_by self[short_key] = args[0] - elsif @writes.has_key?(key) + elsif @writes.key?(key) or @reads.key?(key) self[key] - elsif @reads.has_key?(key) - @reads[key] elsif @default.respond_to?(name) value = @default.public_send(name, *args, &block) @reads[key] = _dup(value) From 3b074fdfb57ca2783b7675132d4ea65b15aaaca2 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 27 Jan 2014 21:20:18 +0900 Subject: [PATCH 103/186] Add message to RuntimeError Add message to RuntimeError in Pry::Config::Behavior.inherited_by --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 8b08cc44..eea7ead0 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -79,7 +79,7 @@ module Pry::Config::Behavior def inherited_by(other) if @inherited_by - raise RuntimeError + raise RuntimeError, "instance of Pry::Config should be inherited by only one instance" else @inherited_by = other end From 70391c2dbe89308abf9770e6cd6ecc6bb3ec8c52 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 28 Jan 2014 05:39:05 +0100 Subject: [PATCH 104/186] use Behavhior#key?() to do the lookup bizness in Behavior#method_missing(). --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index eea7ead0..9884939e 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -34,7 +34,7 @@ module Pry::Config::Behavior short_key = key[0..-2] @inherited_by.forget(:read, short_key) if @inherited_by self[short_key] = args[0] - elsif @writes.key?(key) or @reads.key?(key) + elsif key?(key) self[key] elsif @default.respond_to?(name) value = @default.public_send(name, *args, &block) From 02b8f18aa61b0047f8a4125135fa9b0d7eca1a81 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 28 Jan 2014 05:44:45 +0100 Subject: [PATCH 105/186] update exception message in Behavior#[]=. --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 9884939e..1d3fed8a 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -23,7 +23,7 @@ module Pry::Config::Behavior def []=(key, value) key = key.to_s if RESERVED_KEYS.include?(key) - raise ArgumentError, "sorry, '#{key}' is a reserved configuration option." + raise ArgumentError, "sorry, '#{key}' is a reserved configuration key." end @writes[key] = value end From ed034a4a19eaeee4a25a28644b4e1cd426cd1980 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Tue, 28 Jan 2014 22:42:41 +0900 Subject: [PATCH 106/186] Fix repl_tester to access correct config object Now by refactoring config, Pry.config and @pry.config have different output. So to access correct pry config object, fix ReplTester.input ReplTester.output and ReplTester.reset_output --- spec/spec_helpers/repl_tester.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/spec_helpers/repl_tester.rb b/spec/spec_helpers/repl_tester.rb index c6fc80ce..51457e13 100644 --- a/spec/spec_helpers/repl_tester.rb +++ b/spec/spec_helpers/repl_tester.rb @@ -67,7 +67,7 @@ class ReplTester reset_output repl_mailbox.push input wait - Pry.output.string + @pry.output.string end # Assert that the current prompt matches the given string or regex. @@ -78,7 +78,7 @@ class ReplTester # Assert that the most recent output (since the last time input was called) # matches the given string or regex. def output(match) - match.should === Pry.output.string.chomp + match.should === @pry.output.string.chomp end # Assert that the Pry session ended naturally after the last input. @@ -99,7 +99,7 @@ class ReplTester private def reset_output - Pry.output.clear + @pry.output.clear end def repl_mailbox From 1bd41dec24529b0271078cda1f85e4957672f4d8 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 28 Jan 2014 15:17:29 +0100 Subject: [PATCH 107/186] merge @writes into @reads from Behavior#to_hash. --- lib/pry/config/behavior.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 1d3fed8a..9fc8d36a 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -86,13 +86,11 @@ module Pry::Config::Behavior end def to_hash - # TODO: should merge read_lookup? - @writes + @reads.merge(@writes) end def to_h - # TODO: should merge read_lookup? - @writes + to_hash end def quiet? From 764652b1130634c655c2e097a3b9c585c0e1809d Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 28 Jan 2014 15:20:26 +0100 Subject: [PATCH 108/186] call to_hash on 'options' in Pry.start(). --- lib/pry/hooks.rb | 1 - lib/pry/pry_class.rb | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/hooks.rb b/lib/pry/hooks.rb index f902d58e..496e344d 100644 --- a/lib/pry/hooks.rb +++ b/lib/pry/hooks.rb @@ -23,7 +23,6 @@ class Pry hash.each do |k, v| instance.add_hook(k, nil, v) end - instance end diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 296ba23d..77c73f3a 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -111,6 +111,7 @@ class Pry # Pry.start(Object.new, :input => MyInput.new) def self.start(target=nil, options={}) return if ENV['DISABLE_PRY'] + options = options.to_hash if in_critical_section? output.puts "ERROR: Pry started inside Pry." From 95144d802da03935146be4ba9c11e26eab3e3d5f Mon Sep 17 00:00:00 2001 From: yui-knk Date: Tue, 28 Jan 2014 23:37:07 +0900 Subject: [PATCH 109/186] Refactor Pry::Config::Behavior.to_h to be alias --- lib/pry/config/behavior.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 9fc8d36a..d4a89b0a 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -88,10 +88,7 @@ module Pry::Config::Behavior def to_hash @reads.merge(@writes) end - - def to_h - to_hash - end + alias_method :to_h, :to_hash def quiet? quiet From ed26866c899f1f34eaf5d3185c4b09766d749457 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Wed, 29 Jan 2014 00:05:48 +0900 Subject: [PATCH 110/186] Fix watch_expression to access correct hooks Now by refactoring config, Pry.config and @pry.config have different hooks. So to access correct pry config hooks, fix Pry::Command::WatchExpression.add_hook. --- lib/pry/commands/watch_expression.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 05d8e36e..24724123 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -82,8 +82,8 @@ class Pry def add_hook hook = [:after_eval, :watch_expression] - unless Pry.hooks.hook_exists?(*hook) - Pry.hooks.add_hook(*hook) do |_, _pry_| + unless _pry_.hooks.hook_exists?(*hook) + _pry_.hooks.add_hook(*hook) do |_, _pry_| eval_and_print_changed _pry_.output end end From f200e2704ff17b6113fc9b9089e3c3f7828d08a2 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 28 Jan 2014 17:20:58 +0100 Subject: [PATCH 111/186] copy from default on read. reassignment on the default(e.g: Pry.config.color = false) does not propagate back to _pry_.config.color after '_pry_.config.color' has been read for the first time. you can say, '_pry_.config.forget(:color)' for propagation to happen again. --- lib/pry/config/behavior.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index d4a89b0a..54a510b1 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -12,12 +12,10 @@ module Pry::Config::Behavior @default = default.dup if default @default.inherited_by(self) if default @writes = {} - @reads = {} end def [](key) - lookup = @reads.merge(@writes) - lookup[key.to_s] + @writes[key.to_s] end def []=(key, value) @@ -32,13 +30,12 @@ module Pry::Config::Behavior key = name.to_s if key[-1] == ASSIGNMENT short_key = key[0..-2] - @inherited_by.forget(:read, short_key) if @inherited_by self[short_key] = args[0] elsif key?(key) self[key] elsif @default.respond_to?(name) value = @default.public_send(name, *args, &block) - @reads[key] = _dup(value) + self[key] = _dup(value) else nil end @@ -57,24 +54,16 @@ module Pry::Config::Behavior def key?(key) key = key.to_s - @writes.key?(key) or @reads.key?(key) + @writes.key?(key) end def refresh @writes.clear - @reads.clear true end - def forget(lookup_type, key) - case lookup_type - when :write - @writes.delete(key) - when :read - @reads.delete(key) - else - raise ArgumentError, "specify a lookup type (:read or :write)" - end + def forget(key) + @writes.delete(key) end def inherited_by(other) @@ -86,7 +75,7 @@ module Pry::Config::Behavior end def to_hash - @reads.merge(@writes) + @writes end alias_method :to_h, :to_hash From 9828d7f30ea924929ec5758f7b44698e3be66e96 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 29 Jan 2014 14:46:57 +0100 Subject: [PATCH 112/186] add ruby-version with default on 2.1 --- .ruby-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..42f7d233 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.1 \ No newline at end of file From c3d79eb59f0faafff4b3623ae33cefda5b196832 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 29 Jan 2014 14:56:54 +0100 Subject: [PATCH 113/186] fix last fail. inherit StringIO 'output' from wrapped "Pry" in ReplTester. --- spec/pry_repl_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/pry_repl_spec.rb b/spec/pry_repl_spec.rb index a6611310..adeb3287 100644 --- a/spec/pry_repl_spec.rb +++ b/spec/pry_repl_spec.rb @@ -47,7 +47,7 @@ describe "The whole thing" do it "shouldn't break if we start a nested instance" do ReplTester.start do - input 'Pry.start(10)' + input 'Pry.start(10, _pry_.config)' output '' prompt /10.*> $/ From 2717a17d9e2caf9359e33898ca3fa9e6d6a2ec7c Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 29 Jan 2014 15:59:08 +0100 Subject: [PATCH 114/186] add "_dup" to RESERVED_KEYS. --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 54a510b1..7f54c56f 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -5,7 +5,7 @@ module Pry::Config::Behavior "[]", "[]=", "merge!", "respond_to?", "key?", "refresh", "forget", "inherited_by", "to_h", - "to_hash" + "to_hash", "_dup" ].freeze def initialize(default = Pry.config) From 0765e13cdd0fd9c5e7e3b888502358b1b2b9ea79 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 29 Jan 2014 19:21:15 +0100 Subject: [PATCH 115/186] add yui-knk to CONTRIBUTORS. --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 896a7230..d78f368d 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -2,6 +2,7 @@ 372 Conrad Irwin 215 Ryan Fitzgerald 108 Kyrylo Silin + yui-knk 92 Rob Gleeson 54 Mon ouïe 51 Lee Jarvis From e9bfc43578f68c1326e45a751b8dfa9304daf607 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 29 Jan 2014 20:40:06 +0100 Subject: [PATCH 116/186] coerce key to string since all lookup keys are strings. --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 7f54c56f..0fd50798 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -63,7 +63,7 @@ module Pry::Config::Behavior end def forget(key) - @writes.delete(key) + @writes.delete(key.to_s) end def inherited_by(other) From 77b8e6d89bbdd1020ef5778d77486f2ae91e3410 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Thu, 30 Jan 2014 01:37:58 +0100 Subject: [PATCH 117/186] implement Behavior#merge! on top of Behavior#[]=. --- lib/pry/config/behavior.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 0fd50798..7ebeeb32 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -44,8 +44,9 @@ module Pry::Config::Behavior def merge!(other) raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) other = other.to_hash - keys, values = other.keys.map(&:to_s), other.values - @writes.merge! Hash[keys.zip(values)] + other.each do |key, value| + self[key] = value + end end def respond_to?(name, boolean=false) From e3f2475671c1bf2e484719bde93210dc13a8e8f0 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Thu, 30 Jan 2014 01:41:56 +0100 Subject: [PATCH 118/186] extend RESERVED_KEYS tests. --- spec/config_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 82df56c1..e1a2d428 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -7,7 +7,9 @@ describe Pry::Config do end it "raises an ArgumentError on assignment of a reserved key" do - should.raise(ArgumentError) { @config.inherited_by = "abc" } + Pry::Config::RESERVED_KEYS.each do |key| + should.raise(ArgumentError) { @config[key] = 1 } + end end end From c17bc60d9e737d39e950b1b697f33cbab7032978 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Thu, 30 Jan 2014 18:54:18 +0100 Subject: [PATCH 119/186] include of Pry::Config::Behavior extends class/receiver with "from_hash". --- lib/pry/config.rb | 6 ------ lib/pry/config/behavior.rb | 10 ++++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 2f8f3aef..d504ba9a 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -8,12 +8,6 @@ class Pry::Config Convenience::SHORTCUTS end - def self.from_hash(hash, default = nil) - new(default).tap do |config| - config.merge!(hash) - end - end - # # FIXME # @param [Pry::Hooks] hooks diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 7ebeeb32..2ffca653 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -8,6 +8,16 @@ module Pry::Config::Behavior "to_hash", "_dup" ].freeze + def self.included(klass) + klass.extend Module.new { + def from_hash(hash, default = nil) + new(default).tap do |config| + config.merge!(hash) + end + end + } + end + def initialize(default = Pry.config) @default = default.dup if default @default.inherited_by(self) if default From 929c3f5aaa55cbe9e73cbaaaef06d59a3cf07893 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 31 Jan 2014 02:39:23 +0100 Subject: [PATCH 120/186] add spec's for Pry::Config. --- spec/config_spec.rb | 73 +++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 49 deletions(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index e1a2d428..21c94918 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -13,64 +13,39 @@ describe Pry::Config do end end - describe "local config" do - it "should be set" do - t = pry_tester - t.eval "_pry_.config.foobar = 'hello'" - t.eval("_pry_.config.foobar").should == 'hello' + describe "traversal to parent" do + it "traverses back to the parent when a local key is not found" do + config = Pry::Config.new Pry::Config.from_hash(foo: 1) + config.foo.should == 1 end - it "should be set (array)" do - t = pry_tester - t.eval "_pry_.config.foobar = []" - t.eval "_pry_.config.foobar << 1 << 2" - t.eval("_pry_.config.foobar").should == [1, 2] + it "stores a local key and prevents traversal to the parent" do + config = Pry::Config.new Pry::Config.from_hash(foo: 1) + config.foo = 2 + config.foo.should == 2 end - it "should be global config value when local config is not set" do - Pry.config.foobar = 'hello' - t = pry_tester - t.eval("_pry_.config.foobar").should == 'hello' - Pry.config.foobar = nil + it "duplicates a copy on read from the parent" do + ukraine = "i love" + config = Pry::Config.new Pry::Config.from_hash(home: ukraine) + config.home.equal?(ukraine).should == false end - it "should be local config value when local config is set" do - Pry.config.foobar = 'hello' - t = pry_tester - t.eval "_pry_.config.foobar = 'goodbye'" - t.eval("_pry_.config.foobar").should == 'goodbye' - Pry.config.foobar = nil + it "forgets a local copy in favor of the parent's new value" do + default = Pry::Config.from_hash(shoes: "and socks") + local = Pry::Config.new(default).tap(&:shoes) + default.shoes = 1 + local.shoes.should == "and socks" + local.forget(:shoes) + local.shoes.should == 1 end end - describe "global config" do - it "should be set" do - Pry.config.foobar = 'hello' - Pry.config.foobar.should == 'hello' - Pry.config.foobar = nil - end - - it "should be set (array)" do - Pry.config.foobar = [] - Pry.config.foobar << 1 << 2 - Pry.config.foobar.should == [1, 2] - Pry.config.foobar = nil - end - - it "should keep value when local config is set" do - Pry.config.foobar = 'hello' - t = pry_tester - t.eval "_pry_.config.foobar = 'goodbye'" - Pry.config.foobar.should == 'hello' - Pry.config.foobar = nil - end - - it "should keep value when local config is set (array)" do - Pry.config.foobar = [1, 2] - t = pry_tester - t.eval "_pry_.config.foobar << 3 << 4" - Pry.config.foobar.should == [1, 2] - Pry.config.foobar = nil + describe "#[]=" do + it "stores keys as strings" do + local = Pry::Config.from_hash({}) + local[:zoo] = "hello" + local.to_hash.should == { "zoo" => "hello" } end end end From 0a25818c6b8a29890133ce7b6e1c4039d965cc0f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 31 Jan 2014 02:43:13 +0100 Subject: [PATCH 121/186] cleanup in config_spec.rb --- spec/config_spec.rb | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 21c94918..7a1f71d6 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -2,33 +2,30 @@ require 'helper' describe Pry::Config do describe "reserved keys" do - before do - @config = Pry::Config.from_hash({}, nil) - end - it "raises an ArgumentError on assignment of a reserved key" do + local = Pry::Config.from_hash({}) Pry::Config::RESERVED_KEYS.each do |key| - should.raise(ArgumentError) { @config[key] = 1 } + should.raise(ArgumentError) { local[key] = 1 } end end end describe "traversal to parent" do it "traverses back to the parent when a local key is not found" do - config = Pry::Config.new Pry::Config.from_hash(foo: 1) - config.foo.should == 1 + local = Pry::Config.new Pry::Config.from_hash(foo: 1) + local.foo.should == 1 end it "stores a local key and prevents traversal to the parent" do - config = Pry::Config.new Pry::Config.from_hash(foo: 1) - config.foo = 2 - config.foo.should == 2 + local = Pry::Config.new Pry::Config.from_hash(foo: 1) + local.foo = 2 + local.foo.should == 2 end it "duplicates a copy on read from the parent" do ukraine = "i love" - config = Pry::Config.new Pry::Config.from_hash(home: ukraine) - config.home.equal?(ukraine).should == false + local = Pry::Config.new Pry::Config.from_hash(home: ukraine) + local.home.equal?(ukraine).should == false end it "forgets a local copy in favor of the parent's new value" do From a6783e2628816655ddc251e73ca2a3dd49a863f0 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 31 Jan 2014 14:12:04 +0100 Subject: [PATCH 122/186] add config test for traversal through chain of parents. --- spec/config_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 7a1f71d6..6a7b6e38 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -36,6 +36,14 @@ describe Pry::Config do local.forget(:shoes) local.shoes.should == 1 end + + it "traverses through a chain of parents" do + root = Pry::Config.from_hash({foo: 21}) + local1 = Pry::Config.new(root) + local2 = Pry::Config.new(local1) + local3 = Pry::Config.new(local2) + local3.foo.should == 21 + end end describe "#[]=" do From 04444b7bb73b8cd5fed6ca23f7ab0607cec6a2e0 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 31 Jan 2014 14:18:32 +0100 Subject: [PATCH 123/186] add tests for config. --- spec/config_spec.rb | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 6a7b6e38..27c7dcda 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -28,15 +28,6 @@ describe Pry::Config do local.home.equal?(ukraine).should == false end - it "forgets a local copy in favor of the parent's new value" do - default = Pry::Config.from_hash(shoes: "and socks") - local = Pry::Config.new(default).tap(&:shoes) - default.shoes = 1 - local.shoes.should == "and socks" - local.forget(:shoes) - local.shoes.should == 1 - end - it "traverses through a chain of parents" do root = Pry::Config.from_hash({foo: 21}) local1 = Pry::Config.new(root) @@ -46,6 +37,38 @@ describe Pry::Config do end end + describe "#forget" do + it "forgets a local key" do + local = Pry::Config.new Pry::Config.from_hash(foo: 1) + local.foo = 2 + local.foo.should == 2 + local.forget(:foo) + local.foo.should == 1 + end + end + + describe "#to_hash" do + it "provides a copy of local key & value pairs as a Hash" do + local = Pry::Config.new Pry::Config.from_hash(bar: true) + local.foo = "21" + local.to_hash.should == { "foo" => "21" } + end + end + + describe "#merge!" do + it "can merge a Hash-like object" do + local = Pry::Config.new + local.merge! Pry::Config.from_hash(foo: 21) + local.foo.should == 21 + end + + it "can merge a Hash" do + local = Pry::Config.new + local.merge!(foo: 21) + local.foo.should == 21 + end + end + describe "#[]=" do it "stores keys as strings" do local = Pry::Config.from_hash({}) From c0b61b407bd5db390ba0ad8c879e9af6717f46cf Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 31 Jan 2014 23:38:13 +0100 Subject: [PATCH 124/186] lazy load readline through Proc defined on Pry::Config::Default. closes #1107 closes #1081 closes #1095 --- lib/pry.rb | 7 ------- lib/pry/config/default.rb | 12 +++++++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 856b8aa8..2bde869a 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -132,13 +132,6 @@ require 'rbconfig' require 'tempfile' require 'pathname' -begin - require 'readline' -rescue LoadError - warn "You're running a version of ruby with no Readline support" - warn "Please `gem install rb-readline` or recompile ruby --with-readline." - exit! -end if Pry::Helpers::BaseHelpers.jruby? begin diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 990048a1..7ab077d4 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,8 +1,18 @@ class Pry::Config::Default include Pry::Config::Behavior + def self.lazy_readline + require 'readline' + Readline + rescue LoadError + warn "Pry says!" + warn "You're running a version of ruby with no Readline support" + warn "Please `gem install rb-readline` or recompile ruby --with-readline." + exit! + end + default = { - :input => proc { Readline }, + :input => method(:lazy_readline).to_proc, :output => proc { $stdout }, :commands => proc { Pry::Commands }, :prompt_name => proc { Pry::DEFAULT_PROMPT_NAME }, From 78d44436904e735f0d6de92f953542c82382b97f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 31 Jan 2014 23:49:58 +0100 Subject: [PATCH 125/186] improve error message on failure to require 'readline'. references #1095 --- lib/pry/config/default.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 7ab077d4..2c1d930d 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -6,8 +6,9 @@ class Pry::Config::Default Readline rescue LoadError warn "Pry says!" - warn "You're running a version of ruby with no Readline support" - warn "Please `gem install rb-readline` or recompile ruby --with-readline." + warn "require of 'readline' has failed." + warn "you can rebuild ruby with readline support from C through '--with-readline'." + warn "alternatively, you can use the pure-ruby version of readline through 'gem install rb-readline'" exit! end From 57aecd6a23b80740d36f59a3b481755cc54d83fc Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 00:59:00 +0100 Subject: [PATCH 126/186] re-raise LoadError (part of #1081) --- lib/pry/config/default.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 2c1d930d..7950a2d6 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -4,12 +4,12 @@ class Pry::Config::Default def self.lazy_readline require 'readline' Readline - rescue LoadError + rescue LoadError => e warn "Pry says!" warn "require of 'readline' has failed." warn "you can rebuild ruby with readline support from C through '--with-readline'." warn "alternatively, you can use the pure-ruby version of readline through 'gem install rb-readline'" - exit! + raise(e) end default = { From 005ea4898decd60fdde7b14529a25344b3cc9de4 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 01:14:32 +0100 Subject: [PATCH 127/186] remove reference to 'Readline' from if condition in tests. --- spec/completion_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/completion_spec.rb b/spec/completion_spec.rb index 2867bcea..876a3828 100644 --- a/spec/completion_spec.rb +++ b/spec/completion_spec.rb @@ -6,7 +6,7 @@ def completer_test(bind, pry=nil, assert_flag=true) return proc {|*symbols| symbols.each(&test) } end -if defined?(Bond) && Readline::VERSION !~ /editline/i +if defined?(Bond) describe 'bond-based completion' do it 'should pull in Bond by default' do Pry.config.completer.should == Pry::BondCompleter From df866e40a914d08f571b2f6aa10e1015ce940874 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 01:21:38 +0100 Subject: [PATCH 128/186] update Pry::REPL to not assume Readline is loaded. --- lib/pry/repl.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb index fa1caf04..0fa8d753 100644 --- a/lib/pry/repl.rb +++ b/lib/pry/repl.rb @@ -179,9 +179,9 @@ class Pry end end - if input == Readline + if defined?(Readline) and input == Readline if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows? - Readline.output = File.open('/dev/tty', 'w') + _pry_.input.output = File.open('/dev/tty', 'w') end input_readline(current_prompt, false) # false since we'll add it manually elsif defined? Coolline and input.is_a? Coolline From 1816493ed58506ed511f2c04b8963299cb487818 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 01:24:40 +0100 Subject: [PATCH 129/186] visit instance of 'Pry' through delegate. --- lib/pry/repl.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb index 0fa8d753..ac057497 100644 --- a/lib/pry/repl.rb +++ b/lib/pry/repl.rb @@ -181,7 +181,7 @@ class Pry if defined?(Readline) and input == Readline if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows? - _pry_.input.output = File.open('/dev/tty', 'w') + input.output = File.open('/dev/tty', 'w') end input_readline(current_prompt, false) # false since we'll add it manually elsif defined? Coolline and input.is_a? Coolline From 54a374dc01fa35408dd8639c68b8eef15e87ecf8 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Fri, 31 Jan 2014 16:51:38 -0800 Subject: [PATCH 130/186] Ensures that loading pry inside config doesn't explode --- lib/pry/pry_class.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 77c73f3a..682b2e2e 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -51,7 +51,9 @@ class Pry # This method can also be used to reload the files if they have changed. def self.load_rc_files rc_files_to_load.each do |file| - load_file_at_toplevel(file) + critical_section do + load_file_at_toplevel(file) + end end end From 4d39a53e6120afb814f3a19793fd75afe3a9771b Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 03:15:18 +0100 Subject: [PATCH 131/186] specify 'nil' default in config_spec.rb (don't fallback on Pry.config) --- spec/config_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 27c7dcda..db25f2a9 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -57,13 +57,13 @@ describe Pry::Config do describe "#merge!" do it "can merge a Hash-like object" do - local = Pry::Config.new + local = Pry::Config.new(nil) local.merge! Pry::Config.from_hash(foo: 21) local.foo.should == 21 end it "can merge a Hash" do - local = Pry::Config.new + local = Pry::Config.new(nil) local.merge!(foo: 21) local.foo.should == 21 end From fe54ff7ca03b1ffbd1c32e15323c5d97272c607f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 14:03:11 +0100 Subject: [PATCH 132/186] return an instance of Pry::Config from Pry.current. --- lib/pry/pry_class.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 682b2e2e..7065effa 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -34,9 +34,12 @@ class Pry @main ||= TOPLEVEL_BINDING.eval "self" end - # @return [Hash] Pry's `Thread.current` hash + # + # @return [Pry::Config] + # Returns a value store for an instance of Pry running on the current thread. + # def self.current - Thread.current[:__pry__] ||= {} + Thread.current[:__pry__] ||= Pry::Config.from_hash({}, nil) end # Load the given file in the context of `Pry.toplevel_binding` From 0b64b55ed89dc6879347147a803a63d079321a51 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 1 Feb 2014 14:52:40 +0100 Subject: [PATCH 133/186] require 'config/*' dependencies through require_relative. --- lib/pry/config.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index d504ba9a..73b7742e 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,7 +1,7 @@ class Pry::Config - require 'pry/config/behavior' - require 'pry/config/default' - require 'pry/config/convenience' + require_relative 'config/behavior' + require_relative 'config/default' + require_relative 'config/convenience' include Pry::Config::Behavior def self.shortcuts From aef19935c66c3a4f0290435caa93577090bc3311 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 14:47:48 +0100 Subject: [PATCH 134/186] add Behavior#{==, eql?}. compare equality through lookup table. --- lib/pry/config/behavior.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 2ffca653..717aa554 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -85,6 +85,11 @@ module Pry::Config::Behavior end end + def ==(other) + to_hash == other.to_hash + end + alias_method :eql?, :== + def to_hash @writes end From 6591ddd418bd76973bcda99ebf419f00e49e7202 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 14:52:55 +0100 Subject: [PATCH 135/186] add tests. --- spec/config_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index db25f2a9..52fce1e8 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -37,6 +37,30 @@ describe Pry::Config do end end + describe ".from_hash" do + it "returns an object without a default when given 1 argument" do + local = Pry::Config.from_hash({}) + local.instance_variable_get(:@default).should == nil + end + + it "returns an object with a default when given 2 arguments" do + default = Pry::Config.new(nil) + local = Pry::Config.from_hash({}, default) + local.instance_variable_get(:@default).should == default + end + end + + + describe "#==" do + it "compares equality through the underlying lookup table" do + local1 = Pry::Config.new(nil) + local2 = Pry::Config.new(nil) + local1.foo = "hi" + local2.foo = "hi" + local1.should == local2 + end + end + describe "#forget" do it "forgets a local key" do local = Pry::Config.new Pry::Config.from_hash(foo: 1) From 557cd0fa7be280740b112d5a22e202590bef5fe3 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 15:01:32 +0100 Subject: [PATCH 136/186] return false when argument does not implement #to_hash. --- lib/pry/config/behavior.rb | 1 + spec/config_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 717aa554..c53db899 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -86,6 +86,7 @@ module Pry::Config::Behavior end def ==(other) + return false unless other.respond_to?(:to_hash) to_hash == other.to_hash end alias_method :eql?, :== diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 52fce1e8..958f92b9 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -59,6 +59,11 @@ describe Pry::Config do local2.foo = "hi" local1.should == local2 end + + it "compares equality against an object who does not implement #to_hash" do + local1 = Pry::Config.new(nil) + local1.should.not == Object.new + end end describe "#forget" do From 8e723512dd1d655f046ca6d4a15444976a974a4c Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 15:34:35 +0100 Subject: [PATCH 137/186] duplicate Hash returned by Behavior#to_hash (prevents outside-mutation) --- lib/pry/config/behavior.rb | 2 +- spec/config_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index c53db899..bb5dd45d 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -92,7 +92,7 @@ module Pry::Config::Behavior alias_method :eql?, :== def to_hash - @writes + @writes.dup end alias_method :to_h, :to_hash diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 958f92b9..f3f67158 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -82,6 +82,12 @@ describe Pry::Config do local.foo = "21" local.to_hash.should == { "foo" => "21" } end + + it "returns a duplicate of the lookup table" do + local = Pry::Config.new + local.to_hash.merge!("foo" => 42) + local.foo.should.not == 42 + end end describe "#merge!" do From 41a90bd93be52637e5ea1c2d0891edd1ecbdc85c Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 15:58:10 +0100 Subject: [PATCH 138/186] forgot to set an explicit default of `nil`. --- spec/config_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index f3f67158..bfbaaa89 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -84,7 +84,7 @@ describe Pry::Config do end it "returns a duplicate of the lookup table" do - local = Pry::Config.new + local = Pry::Config.new(nil) local.to_hash.merge!("foo" => 42) local.foo.should.not == 42 end From 421a546194fe85280243bd96de302ab6f8581f28 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 16:31:32 +0100 Subject: [PATCH 139/186] update CHANGELOG.md to include #1118. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b28c9195..7502cbd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * `whereami` is now aliased to `@` #### Bug fixes, etc. +* `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) * Add support for BasicObjects to `ls` (#984) * Allow `ls -c ` (#891) * Fix indentation not working if the `mathn` stdlib was loaded (#872) From e73bce7521f0e175648a5f8b82bf454b8a9c770b Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 16:54:32 +0100 Subject: [PATCH 140/186] use a dynamic class name in Behavior#inherited_by(). covers: class Foo include Pry::Config::Behavior end foo = Foo.new(Pry::Config.new) foo.inherited_by(Object.new) --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index bb5dd45d..534cf90c 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -79,7 +79,7 @@ module Pry::Config::Behavior def inherited_by(other) if @inherited_by - raise RuntimeError, "instance of Pry::Config should be inherited by only one instance" + raise RuntimeError, "instance of '#{self.class}' cannot reassign its parent." else @inherited_by = other end From 307424d1928f29a2a7df1eb5a254f30a71706af1 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 17:04:29 +0100 Subject: [PATCH 141/186] fix backwards logic in exception message. --- lib/pry/config/behavior.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 534cf90c..29eea187 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -79,7 +79,7 @@ module Pry::Config::Behavior def inherited_by(other) if @inherited_by - raise RuntimeError, "instance of '#{self.class}' cannot reassign its parent." + raise RuntimeError, "instance of '#{self.class}' cannot reassign its child." else @inherited_by = other end From ee94d2029bfc0f3690c10ca83eaa5563aada1f9a Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 18:52:32 +0100 Subject: [PATCH 142/186] update CHANGELOG.md to include #1096 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7502cbd1..6333bde5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * User can whitelist objects whose inspect output should appear in prompt (#885) * See `Pry.config.prompt_safe_objects` * `whereami` is now aliased to `@` +* improve configuration(Pry.config) to lazy-load default configuration values. (#1096) #### Bug fixes, etc. * `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) @@ -44,6 +45,10 @@ * Fix bug in `edit` regarding recognition of file names without suffix. #### Dev-facing changes +* through changes to configuration, all commands should reference configuration values + via `_pry_.config` and not `Pry.config`. (#1096) +* improve configuration(Pry::Config) for easier support of concurrent environments + through a 'pry-local' config who, at times, acts as a 'pry-local store'. (#1096) * `rake pry` now accepts switches prefixed with `_` (e.g., `rake pry _v`) * Pagers now act like `IO`s and accept streaming output * See `Pager.page` and `Pager.with_pager` From b495af23cfa4a549ad11ecd6c7fd10b49ce81bd0 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 23:12:56 +0100 Subject: [PATCH 143/186] delete the backtrace to avoid storing it on `_pry_.config`. --- lib/pry/pry_instance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 647bc960..69da3386 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -66,7 +66,7 @@ class Pry @indent = Pry::Indent.new @command_state = {} @eval_string = "" - @backtrace = options[:backtrace] || caller + @backtrace = options.delete(:backtrace) || caller @config = Pry::Config.new config.merge!(options) push_prompt(config.prompt) From 76d4f03934de39d85133510cf85dfa5a2eeff0e5 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 23:23:23 +0100 Subject: [PATCH 144/186] add Behavior#keys. --- lib/pry/config/behavior.rb | 4 ++++ spec/config_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 29eea187..d72d7afa 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -91,6 +91,10 @@ module Pry::Config::Behavior end alias_method :eql?, :== + def keys + @writes.keys + end + def to_hash @writes.dup end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index bfbaaa89..aa38a1bc 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -51,6 +51,14 @@ describe Pry::Config do end + describe "#keys" do + it "returns an array of local keys" do + root = Pry::Config.from_hash({zoo: "boo"}, nil) + local = Pry::Config.from_hash({foo: "bar"}, root) + local.keys.should == ["foo"] + end + end + describe "#==" do it "compares equality through the underlying lookup table" do local1 = Pry::Config.new(nil) From d0a54c5260558975bd358e6678e4e4eb0b72860f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 2 Feb 2014 23:34:56 +0100 Subject: [PATCH 145/186] delete .ruby-version --- .ruby-version | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index 42f7d233..00000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.1 \ No newline at end of file From f72ce4ec7ee2c35198090c82160399182737f643 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 2 Feb 2014 15:34:44 -0800 Subject: [PATCH 146/186] Fix completion spec for Ruby with editline --- spec/completion_spec.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/completion_spec.rb b/spec/completion_spec.rb index 876a3828..1d710bfb 100644 --- a/spec/completion_spec.rb +++ b/spec/completion_spec.rb @@ -6,16 +6,17 @@ def completer_test(bind, pry=nil, assert_flag=true) return proc {|*symbols| symbols.each(&test) } end -if defined?(Bond) - describe 'bond-based completion' do - it 'should pull in Bond by default' do +describe 'Bond-based completion' do + it "should use Bond if it's available" do + if defined?(Bond) && Readline::VERSION !~ /editline/i Pry.config.completer.should == Pry::BondCompleter + else + Pry.config.completer.should == Pry::InputCompleter end end end describe Pry::InputCompleter do - before do # The AMQP gem has some classes like this: # pry(main)> AMQP::Protocol::Test::ContentOk.name From 16c6037cde860c8b406e8140c5263fb7f4b26506 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 2 Feb 2014 15:43:40 -0800 Subject: [PATCH 147/186] Remove Foreman hack from 5f3b5857fc94f0237ecd0440069cf34cc6aed86a --- lib/pry/repl.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb index fa1caf04..83047cbf 100644 --- a/lib/pry/repl.rb +++ b/lib/pry/repl.rb @@ -180,9 +180,6 @@ class Pry end if input == Readline - if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows? - Readline.output = File.open('/dev/tty', 'w') - end input_readline(current_prompt, false) # false since we'll add it manually elsif defined? Coolline and input.is_a? Coolline input_readline(current_prompt) From 8ed7a869c7adf233d00f0b59b579272df01c9fcf Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 2 Feb 2014 17:01:32 -0800 Subject: [PATCH 148/186] Remove order dependency between :input and :completer procs This required switching Config::Default to use instance_eval instead of call so that config procs can reference each other. I also brought over a couple of copy changes from 0-9-12-stable. --- lib/pry.rb | 6 +++-- lib/pry/config/default.rb | 47 ++++++++++++++++++++++----------------- spec/helper.rb | 3 +++ 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 2bde869a..10b1a675 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -137,7 +137,8 @@ if Pry::Helpers::BaseHelpers.jruby? begin require 'ffi' rescue LoadError - warn "Need to `gem install ffi`" + # TODO: Why do we need this? + warn "For a better Pry experience on JRuby, please `gem install ffi`." end end @@ -148,7 +149,8 @@ if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi # only fail on jruby (where win32console doesn't work). # Instead we'll recommend ansicon, which does. rescue LoadError - warn "For a better pry experience, please use ansicon: https://github.com/adoxa/ansicon" + warn "For a better Pry experience on Windows, please use ansicon:" + warn " http://adoxa.3eeweb.com/ansicon/" end end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 7950a2d6..49d588ab 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,19 +1,8 @@ class Pry::Config::Default include Pry::Config::Behavior - def self.lazy_readline - require 'readline' - Readline - rescue LoadError => e - warn "Pry says!" - warn "require of 'readline' has failed." - warn "you can rebuild ruby with readline support from C through '--with-readline'." - warn "alternatively, you can use the pure-ruby version of readline through 'gem install rb-readline'" - raise(e) - end - default = { - :input => method(:lazy_readline).to_proc, + :input => proc { lazy_readline }, :output => proc { $stdout }, :commands => proc { Pry::Commands }, :prompt_name => proc { Pry::DEFAULT_PROMPT_NAME }, @@ -46,13 +35,7 @@ class Pry::Config::Default :extra_sticky_locals => proc { {} }, :command_completer => proc { proc { Pry.commands.commands.keys } }, :file_completer => proc { proc { Dir["."] } }, - :completer => proc { - if defined?(Bond) && Readline::VERSION !~ /editline/i - Pry::BondCompleter.start - else - Pry::InputCompleter.start - end - } + :completer => proc { lazy_completer } } def initialize @@ -65,13 +48,14 @@ class Pry::Config::Default default.each do |key, value| define_method(key) do if default[key].equal?(value) - default[key] = value.call + default[key] = instance_eval(&value) end default[key] end end private + # TODO: # all of this configure_* stuff is a relic of old code. # we should try move this code to being command-local. @@ -112,4 +96,27 @@ private history.should_load = false end end + + def lazy_readline + require 'readline' + Readline + rescue LoadError + warn "Sorry, you can't use Pry without Readline or a compatible library." + warn "Possible solutions:" + warn " * Rebuild Ruby with Readline support using `--with-readline`" + warn " * Use the rb-readline gem, which is a pure-Ruby port of Readline" + raise + end + + def lazy_completer + if defined?(Bond) && !is_editline?(input) + Pry::BondCompleter.start + else + Pry::InputCompleter.start + end + end + + def is_editline?(input) + defined?(input::VERSION) && input::VERSION =~ /editline/i + end end diff --git a/spec/helper.rb b/spec/helper.rb index 60193949..0290d0d6 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -15,6 +15,9 @@ require 'spec_helpers/bacon' require 'spec_helpers/mock_pry' require 'spec_helpers/repl_tester' +# FIXME: temporary until history is fixed to not need Readline +require 'readline' + class Module public :remove_const public :remove_method From bfe70aa1651b6e07ee2c6c49ba4b87b98a412709 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 2 Feb 2014 17:08:44 -0800 Subject: [PATCH 149/186] Check if Readline is defined before getting screen size --- lib/pry/terminal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/terminal.rb b/lib/pry/terminal.rb index d88716b0..67f2979f 100644 --- a/lib/pry/terminal.rb +++ b/lib/pry/terminal.rb @@ -54,7 +54,7 @@ class Pry::Terminal end def screen_size_according_to_readline - if Readline.respond_to?(:get_screen_size) + if defined?(Readline) && Readline.respond_to?(:get_screen_size) size = Readline.get_screen_size size if nonzero_column?(size) end From 99b367f9132cb774a3a41731b363ea49cc5be70a Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 02:26:15 +0100 Subject: [PATCH 150/186] go through pry-local config when to call control_d_handler. --- lib/pry/pry_instance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 69da3386..d3522c7d 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -240,7 +240,7 @@ class Pry def handle_line(line, options) if line.nil? - Pry.config.control_d_handler.call(@eval_string, self) + config.control_d_handler.call(@eval_string, self) return end From 95d0ca534b5dd948005a29f7e3aab46ef63a87cd Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 2 Feb 2014 17:36:06 -0800 Subject: [PATCH 151/186] Don't assume Readline is loaded in auto_resize --- lib/pry/pry_class.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 7065effa..cf284b89 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -233,15 +233,22 @@ class Pry end def self.auto_resize! - ver = Readline::VERSION - if ver[/edit/i] + Pry.config.input # by default, load Readline + + if !defined?(Readline) || Pry.config.input != Readline + warn "Sorry, you must be using Readline for Pry.auto_resize! to work." + return + end + + if Readline::VERSION =~ /edit/i warn <<-EOT -Readline version #{ver} detected - will not auto_resize! correctly. +Readline version #{Readline::VERSION} detected - will not auto_resize! correctly. For the fix, use GNU Readline instead: https://github.com/guard/guard/wiki/Add-proper-Readline-support-to-Ruby-on-Mac-OS-X EOT return end + trap :WINCH do begin Readline.set_screen_size(*Terminal.size!) From 87f8ac439e53c762441ef5743d9eeb4e1e6b48aa Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sun, 2 Feb 2014 19:51:39 -0800 Subject: [PATCH 152/186] Initialize History lazily, make it work without Readline --- lib/pry/history.rb | 17 ++++++++++++----- lib/pry/pry_class.rb | 7 +++++-- spec/helper.rb | 3 --- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/pry/history.rb b/lib/pry/history.rb index 571a9318..1bef4a23 100644 --- a/lib/pry/history.rb +++ b/lib/pry/history.rb @@ -2,7 +2,6 @@ class Pry # The History class is responsible for maintaining the user's input history, # both internally and within Readline. class History - attr_accessor :loader, :saver, :pusher, :clearer # @return [Fixnum] Number of lines in history when Pry first loaded. @@ -17,10 +16,18 @@ class Pry # Assign the default methods for loading, saving, pushing, and clearing. def restore_default_behavior - @loader = method(:read_from_file) - @saver = method(:save_to_file) - @pusher = method(:push_to_readline) - @clearer = method(:clear_readline) + Pry.config.input # force Readline to load if applicable + + @loader = method(:read_from_file) + @saver = method(:save_to_file) + + if defined?(Readline) + @pusher = method(:push_to_readline) + @clearer = method(:clear_readline) + else + @pusher = proc { } + @clearer = proc { } + end end # Load the input history using `History.loader`. diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index cf284b89..e9ba2be5 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -10,11 +10,11 @@ class Pry attr_accessor :current_line attr_accessor :line_buffer attr_accessor :eval_path - attr_accessor :history attr_accessor :cli attr_accessor :quiet attr_accessor :last_internal_error attr_accessor :config + attr_writer :history def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins @@ -28,6 +28,10 @@ class Pry def prompt config.prompt end + + def history + @history ||= History.new + end end def self.main @@ -276,7 +280,6 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly # Basic initialization. def self.init @plugin_manager ||= PluginManager.new - self.history ||= History.new reset_defaults locate_plugins end diff --git a/spec/helper.rb b/spec/helper.rb index 0290d0d6..60193949 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -15,9 +15,6 @@ require 'spec_helpers/bacon' require 'spec_helpers/mock_pry' require 'spec_helpers/repl_tester' -# FIXME: temporary until history is fixed to not need Readline -require 'readline' - class Module public :remove_const public :remove_method From fdc8715e09d3f909574d8f387c43a9ccae7e66f1 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 12:35:23 +0100 Subject: [PATCH 153/186] add spec/integration/readline_spec.rb --- .travis.yml | 4 ++++ Rakefile | 2 +- spec/integration/readline_spec.rb | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 spec/integration/readline_spec.rb diff --git a/.travis.yml b/.travis.yml index 743f5872..e5c3f5db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,10 @@ rvm: - jruby-19mode - jruby-head +script: + - rake spec + - bundle exec bacon spec/integration/readline_spec.rb + matrix: allow_failures: - rvm: ruby-head diff --git a/Rakefile b/Rakefile index 70e84c45..f4439ad0 100644 --- a/Rakefile +++ b/Rakefile @@ -42,7 +42,7 @@ task :test do if explicit_list = ENV['run'] explicit_list.split(',') else - Dir['spec/**/*_spec.rb'].shuffle! + (Dir['spec/**/*_spec.rb'] - Dir["spec/integration/**/*_spec.rb"]).shuffle! end run_specs paths end diff --git a/spec/integration/readline_spec.rb b/spec/integration/readline_spec.rb new file mode 100644 index 00000000..3ff77237 --- /dev/null +++ b/spec/integration/readline_spec.rb @@ -0,0 +1,11 @@ +require "bundler/setup" +require "bacon" + +describe "Readline" do + describe "on require of 'pry'" do + it "is not made available" do + require('pry').should.be.true + defined?(Readline).should.be.nil + end + end +end From a9209f2d2cf2cce209dff234176d234a6bbeedd0 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 12:46:33 +0100 Subject: [PATCH 154/186] integration/readline_spec.rb => isolation/readline_spec.rb --- Rakefile | 2 +- spec/{integration => isolation}/readline_spec.rb | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename spec/{integration => isolation}/readline_spec.rb (99%) diff --git a/Rakefile b/Rakefile index f4439ad0..c427a5e7 100644 --- a/Rakefile +++ b/Rakefile @@ -42,7 +42,7 @@ task :test do if explicit_list = ENV['run'] explicit_list.split(',') else - (Dir['spec/**/*_spec.rb'] - Dir["spec/integration/**/*_spec.rb"]).shuffle! + (Dir['spec/**/*_spec.rb'] - Dir["spec/isolation/*_spec.rb"]).shuffle! end run_specs paths end diff --git a/spec/integration/readline_spec.rb b/spec/isolation/readline_spec.rb similarity index 99% rename from spec/integration/readline_spec.rb rename to spec/isolation/readline_spec.rb index 3ff77237..0b1478a9 100644 --- a/spec/integration/readline_spec.rb +++ b/spec/isolation/readline_spec.rb @@ -1,6 +1,5 @@ require "bundler/setup" require "bacon" - describe "Readline" do describe "on require of 'pry'" do it "is not made available" do From bd123d9674cd8781aab1521734e1b0a595595670 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 12:50:27 +0100 Subject: [PATCH 155/186] update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e5c3f5db..95574215 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ rvm: script: - rake spec - - bundle exec bacon spec/integration/readline_spec.rb + - bundle exec bacon spec/isolation/readline_spec.rb matrix: allow_failures: From ce25a5e476276ed2ca2576146901bb3c11c1d585 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 13:09:11 +0100 Subject: [PATCH 156/186] fix build on MRI1.9, 2.0.0 --- spec/completion_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/completion_spec.rb b/spec/completion_spec.rb index 1d710bfb..9379d6a1 100644 --- a/spec/completion_spec.rb +++ b/spec/completion_spec.rb @@ -8,7 +8,7 @@ end describe 'Bond-based completion' do it "should use Bond if it's available" do - if defined?(Bond) && Readline::VERSION !~ /editline/i + if defined?(Bond) && defined?(Readline) && Readline::VERSION !~ /editline/i Pry.config.completer.should == Pry::BondCompleter else Pry.config.completer.should == Pry::InputCompleter From be5050a4e17373a9606af4b9770f74e377980210 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 13:21:34 +0100 Subject: [PATCH 157/186] eager-load 'completer' via Default in completion_spec.rb --- spec/completion_spec.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/completion_spec.rb b/spec/completion_spec.rb index 9379d6a1..6ab6647f 100644 --- a/spec/completion_spec.rb +++ b/spec/completion_spec.rb @@ -7,11 +7,16 @@ def completer_test(bind, pry=nil, assert_flag=true) end describe 'Bond-based completion' do + before do + @local = Pry::Config.new Pry::Config::Default.new + @local.completer + end + it "should use Bond if it's available" do if defined?(Bond) && defined?(Readline) && Readline::VERSION !~ /editline/i - Pry.config.completer.should == Pry::BondCompleter + @local.completer.should == Pry::BondCompleter else - Pry.config.completer.should == Pry::InputCompleter + @local.completer.should == Pry::InputCompleter end end end From b4d805f63b5c4837e9e68d9bcf793a971dda0612 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 13:46:51 +0100 Subject: [PATCH 158/186] confirm 'Readline' is made available on Pry.start(). --- spec/isolation/readline_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/isolation/readline_spec.rb b/spec/isolation/readline_spec.rb index 0b1478a9..7f5fb79f 100644 --- a/spec/isolation/readline_spec.rb +++ b/spec/isolation/readline_spec.rb @@ -7,4 +7,11 @@ describe "Readline" do defined?(Readline).should.be.nil end end + + describe "on invoke of 'pry'" do + it "is made available" do + Pry.start self, input: StringIO.new("exit-all\n"), output: StringIO.new + defined?(Readline).should == "constant" + end + end end From f4445df7d7db6b92599d3462875a49690a19bd1a Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 13:54:39 +0100 Subject: [PATCH 159/186] mention pry-coolline as an alternative when 'readline' is unavailable. --- lib/pry/config/default.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 49d588ab..c5d9d48d 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -105,6 +105,7 @@ private warn "Possible solutions:" warn " * Rebuild Ruby with Readline support using `--with-readline`" warn " * Use the rb-readline gem, which is a pure-Ruby port of Readline" + warn " * Use the pry-coolline gem, a pure-ruby alternative to Readline" raise end From 0dd077a838001adea59bb5bfa15425fcff2376c5 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 13:58:46 +0100 Subject: [PATCH 160/186] update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6333bde5..933f8d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,8 @@ * User can whitelist objects whose inspect output should appear in prompt (#885) * See `Pry.config.prompt_safe_objects` * `whereami` is now aliased to `@` -* improve configuration(Pry.config) to lazy-load default configuration values. (#1096) +* default configuration(Pry.config) lazy loads its values. (#1096) +* require of 'readline' is delayed until Pry.start() has been called for the first time. (#1117) #### Bug fixes, etc. * `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) From 260d94025f6823d0a45b019010c847a928e501e0 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 3 Feb 2014 22:51:27 +0900 Subject: [PATCH 161/186] Fix sticky_locals(_ and __) when lambda is given When lambda is given, Pry#inject_sticky_locals! causes arguments error. By warpping (_ and __) with proc, solve this bug. This will fix #1119. --- lib/pry/pry_instance.rb | 4 ++-- spec/sticky_locals_spec.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index d3522c7d..4d8232e4 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -184,8 +184,8 @@ class Pry _ex_: last_exception, _file_: last_file, _dir_: last_dir, - _: last_result, - __: output_array[-2] + _: proc { last_result }, + __: proc { output_array[-2] } }.merge(config.extra_sticky_locals) end diff --git a/spec/sticky_locals_spec.rb b/spec/sticky_locals_spec.rb index 0bf933ca..be266b5f 100644 --- a/spec/sticky_locals_spec.rb +++ b/spec/sticky_locals_spec.rb @@ -40,6 +40,21 @@ describe "Sticky locals (_file_ and friends)" do Pry.commands.delete "file-and-dir-test" end + it 'locals should return last result (_)' do + pry_tester.tap do |t| + lam = t.eval 'lambda { |foo| }' + t.eval('_').should == lam + end + end + + it 'locals should return second last result (__)' do + pry_tester.tap do |t| + lam = t.eval 'lambda { |foo| }' + t.eval 'num = 1' + t.eval('__').should == lam + end + end + describe "User defined sticky locals" do describe "setting as Pry.config option" do it 'should define a new sticky local for the session (normal value)' do From ea6f12eebc7b86fd085cd058d24fd0613a110c79 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 14:55:33 +0100 Subject: [PATCH 162/186] remove 1.8-compatibility hack from Rakefile. --- Rakefile | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Rakefile b/Rakefile index c427a5e7..6bc2c58d 100644 --- a/Rakefile +++ b/Rakefile @@ -21,15 +21,6 @@ end desc "Set up and run tests" task :default => [:test] -unless [].respond_to? :shuffle! - class Array - def shuffle! - # TODO: fill this in if anyone cares - self - end - end -end - def run_specs paths quiet = ENV['VERBOSE'] ? '' : '-q' exec "bacon -Ispec -rubygems #{quiet} #{paths.join ' '}" From 044ddd4b0d0b41249cda74a307b2065859b3a4b9 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 15:11:35 +0100 Subject: [PATCH 163/186] rubygems-test is dead (see http://gem-testers.org/, https://github.com/rubygems/rubygems-test) --- README.markdown | 7 ------- 1 file changed, 7 deletions(-) diff --git a/README.markdown b/README.markdown index 2f5e8b33..278e0595 100644 --- a/README.markdown +++ b/README.markdown @@ -65,13 +65,6 @@ methods. The additional docs are accessed through the `show-doc` and * Read the [YARD API documentation](http://rdoc.info/github/pry/pry/master/file/README.markdown) * See the [source code](http://github.com/pry/pry) -Pry also has `rubygems-test` support; to participate, first install -Pry, then: - -1. Install rubygems-test: `gem install rubygems-test` -2. Run the test: `gem test pry` -3. Finally choose 'Yes' to upload the results. - ### Commands Nearly every piece of functionality in a Pry session is implemented as From 247e4af83c6190cf172e4d06c50c8a5a3b106bc2 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 19:28:47 +0100 Subject: [PATCH 164/186] add option to disable to the input completer (ref #1123). --- CHANGELOG.md | 1 + lib/pry/pry_instance.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 933f8d5c..25212c29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * `whereami` is now aliased to `@` * default configuration(Pry.config) lazy loads its values. (#1096) * require of 'readline' is delayed until Pry.start() has been called for the first time. (#1117) +* add option to disable input completer through `_pry_.config.completer = nil` #### Bug fixes, etc. * `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 4d8232e4..b7458769 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -40,6 +40,7 @@ class Pry extend Pry::Config::Convenience config_shortcut *Pry::Config.shortcuts + EMPTY_COMPLETIONS = [].freeze # Create a new {Pry} instance. # @param [Hash] options @@ -131,6 +132,7 @@ class Pry # @param [String] input What the user has typed so far # @return [Array] Possible completions def complete(input) + return EMPTY_COMPLETIONS unless config.completer Pry.critical_section do config.completer.call input, :target => current_binding, :pry => self, From c7eed553ff8b1f616162b9011cf75dbe7c9e1a70 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 19:46:07 +0100 Subject: [PATCH 165/186] rename "*_completer" to "*_completions" Config#file_completer -> Config#file_completions. Config#command_completer -> Config#command_completions. --- lib/pry/commands/shell_mode.rb | 4 ++-- lib/pry/config/default.rb | 4 ++-- lib/pry/pry_instance.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pry/commands/shell_mode.rb b/lib/pry/commands/shell_mode.rb index 64db7668..ee521d3b 100644 --- a/lib/pry/commands/shell_mode.rb +++ b/lib/pry/commands/shell_mode.rb @@ -12,10 +12,10 @@ class Pry case _pry_.prompt when Pry::SHELL_PROMPT _pry_.pop_prompt - _pry_.custom_completions = _pry_.config.file_completer + _pry_.custom_completions = _pry_.config.file_completions else _pry_.push_prompt Pry::SHELL_PROMPT - _pry_.custom_completions = _pry_.config.command_completer + _pry_.custom_completions = _pry_.config.command_completions end end end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index c5d9d48d..557e0cf8 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -33,8 +33,8 @@ class Pry::Config::Default :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, :memory_size => proc { 100 }, :extra_sticky_locals => proc { {} }, - :command_completer => proc { proc { Pry.commands.commands.keys } }, - :file_completer => proc { proc { Dir["."] } }, + :command_completions => proc { proc { Pry.commands.commands.keys } }, + :file_completions => proc { proc { Dir["."] } }, :completer => proc { lazy_completer } } diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index b7458769..5db6a3fa 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -73,7 +73,7 @@ class Pry push_prompt(config.prompt) @input_array = Pry::HistoryArray.new config.memory_size @output_array = Pry::HistoryArray.new config.memory_size - @custom_completions = config.command_completer + @custom_completions = config.command_completions push_initial_binding(options[:target]) set_last_result nil @input_array << nil From c283e644b580959be3eed4f97bc5b5bc7ed728cf Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Mon, 3 Feb 2014 22:35:56 +0100 Subject: [PATCH 166/186] go through local config for command set keys. read from whatever 'Default#commands' is. --- lib/pry/config/default.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 557e0cf8..f939c93d 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -33,7 +33,7 @@ class Pry::Config::Default :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, :memory_size => proc { 100 }, :extra_sticky_locals => proc { {} }, - :command_completions => proc { proc { Pry.commands.commands.keys } }, + :command_completions => proc { proc { commands.commands.keys } }, :file_completions => proc { proc { Dir["."] } }, :completer => proc { lazy_completer } } From 77a9c000679c99265d4b9604c9e88da27c3cd751 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 4 Feb 2014 05:12:28 +0100 Subject: [PATCH 167/186] rename Pry::CommandSet#commands as Pry::CommandSet#to_hash. 'Pry.commands.commands' gone in favor of 'Pry.commands.to_hash' or as using a CommandSet as an enumerable (Pry.commands.each etc) instead. --- CHANGELOG.md | 9 ++++-- lib/pry/command.rb | 2 +- lib/pry/command_set.rb | 55 +++++++++++++++++--------------- lib/pry/config/default.rb | 2 +- spec/command_integration_spec.rb | 34 ++++++++++---------- spec/command_set_spec.rb | 38 +++++++++++----------- spec/command_spec.rb | 14 ++++---- 7 files changed, 81 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25212c29..86f5e2f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,7 @@ ### 1.0.0 (2013/??/??) #### Dependency changes -* 1.8 support discontinued from 1.0+ onwards. - * 0.9.12+ may continue to provide 1.8+ bug fixes. - +* 1.8 support discontinued from 0.10/1.0 up. 0.9 branch continues 1.8 support. * Require Coderay `>= 1.1.0` #### Features @@ -25,6 +23,11 @@ * require of 'readline' is delayed until Pry.start() has been called for the first time. (#1117) * add option to disable input completer through `_pry_.config.completer = nil` +#### API change. +* CommandSet#commands, sometimes referenced through Pry.commands.commands, renamed as 'CommandSet#to_hash'. + it returns a duplicate of the internal hash a CommandSet uses. +* CommandSet#keys is now an alias of CommandSet#list_commands. + #### Bug fixes, etc. * `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) * Add support for BasicObjects to `ls` (#984) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 0527ade7..4dc2e6e6 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -248,7 +248,7 @@ class Pry end def commands - command_set.commands + command_set.to_hash end def text diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index b1d1e199..d60f6e07 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -10,19 +10,15 @@ class Pry class CommandSet include Enumerable include Pry::Helpers::BaseHelpers - - attr_reader :commands attr_reader :helper_module - # @param [Array] imported_sets Sets which will be imported - # automatically + # @param [Array] imported_sets + # Sets which will be imported automatically # @yield Optional block run to define commands def initialize(*imported_sets, &block) @commands = {} @helper_module = Module.new - import(*imported_sets) - instance_eval(&block) if block end @@ -83,7 +79,7 @@ class Pry description, options = ["No description.", description] if description.is_a?(Hash) 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_method :command, :block_command @@ -115,9 +111,9 @@ class Pry description, options = ["No description.", description] if description.is_a?(Hash) options = Pry::Command.default_options(match).merge!(options) - commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block) - commands[match].class_eval(&block) - commands[match] + @commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block) + @commands[match].class_eval(&block) + @commands[match] end # Execute a block of code before a command is invoked. The block also @@ -157,7 +153,7 @@ class Pry def delete(*searches) searches.each do |search| cmd = find_command_by_match_or_listing(search) - commands.delete cmd.match + @commands.delete cmd.match end end @@ -167,7 +163,7 @@ class Pry # @return [Pry::CommandSet] Returns the reciever (a command set). def import(*sets) sets.each do |set| - commands.merge! set.commands + @commands.merge! set.to_hash helper_module.send :include, set.helper_module end self @@ -181,7 +177,7 @@ class Pry helper_module.send :include, set.helper_module matches.each do |match| cmd = set.find_command_by_match_or_listing(match) - commands[cmd.match] = cmd + @commands[cmd.match] = cmd end self end @@ -190,8 +186,8 @@ class Pry # of the command to retrieve. # @return [Command] The command object matched. def find_command_by_match_or_listing(match_or_listing) - cmd = (commands[match_or_listing] || - Pry::Helpers::BaseHelpers.find_command(match_or_listing, commands)) + cmd = (@commands[match_or_listing] || + Pry::Helpers::BaseHelpers.find_command(match_or_listing, @commands)) cmd or raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!" end @@ -250,11 +246,11 @@ class Pry :description => cmd.description }.merge!(options) - commands[new_match] = cmd.dup - commands[new_match].match = new_match - commands[new_match].description = options.delete(:description) - commands[new_match].options.merge!(options) - commands.delete(cmd.match) + @commands[new_match] = cmd.dup + @commands[new_match].match = new_match + @commands[new_match].description = options.delete(:description) + @commands[new_match].options.merge!(options) + @commands.delete(cmd.match) end def disabled_command(name_of_disabled_command, message, matcher=name_of_disabled_command) @@ -303,16 +299,23 @@ class Pry end - # @return [Array] The list of commands provided by the command set. + # @return [Array] + # The list of commands provided by the command set. def list_commands - commands.keys + @commands.keys end + alias_method :keys, :list_commands + + def to_hash + @commands.dup + end + alias_method :to_h, :to_hash # Find a command that matches the given line # @param [String] val The line that might be a command invocation # @return [Pry::Command, nil] def [](pattern) - commands.values.select do |command| + @commands.values.select do |command| command.matches?(pattern) end.sort_by do |command| command.match_score(pattern) @@ -337,7 +340,7 @@ class Pry # def []=(pattern, command) if command.equal?(nil) - return commands.delete(pattern) + return @commands.delete(pattern) end unless Class === command && command < Pry::Command raise TypeError, "command is not a subclass of Pry::Command" @@ -396,7 +399,7 @@ class Pry # @private (used for testing) def run_command(context, match, *args) - command = commands[match] or raise NoCommandError.new(match, self) + command = @commands[match] or raise NoCommandError.new(match, self) command.new(context).call_safely(*args) end @@ -408,7 +411,7 @@ class Pry if command = find_command(search) command.new(context).complete(search) else - commands.keys.select do |key| + @commands.keys.select do |key| String === key && key.start_with?(search) end.map{ |key| key + " " } + Bond::DefaultMission.completions end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index f939c93d..e93088e3 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -33,7 +33,7 @@ class Pry::Config::Default :control_d_handler => proc { Pry::DEFAULT_CONTROL_D_HANDLER }, :memory_size => proc { 100 }, :extra_sticky_locals => proc { {} }, - :command_completions => proc { proc { commands.commands.keys } }, + :command_completions => proc { proc { commands.keys } }, :file_completions => proc { proc { Dir["."] } }, :completer => proc { lazy_completer } } diff --git a/spec/command_integration_spec.rb b/spec/command_integration_spec.rb index 9a124dd8..9d2c6104 100644 --- a/spec/command_integration_spec.rb +++ b/spec/command_integration_spec.rb @@ -473,22 +473,23 @@ describe "commands" do end end - klass.commands.include?("nesting").should == true - klass.commands.include?("jump-to").should == true - klass.commands.include?("cd").should == true - klass.commands.include?("v").should == true + klass.to_hash.include?("nesting").should == true + klass.to_hash.include?("jump-to").should == true + klass.to_hash.include?("cd").should == true + klass.to_hash.include?("v").should == true end it 'should change description of a command using desc' do klass = Pry::CommandSet.new do import Pry::Commands end - orig = klass.commands["help"].description + orig = klass["help"].description klass.instance_eval do desc "help", "blah" end - klass.commands["help"].description.should.not == orig - klass.commands["help"].description.should == "blah" + commands = klass.to_hash + commands["help"].description.should.not == orig + commands["help"].description.should == "blah" end it 'should enable an inherited method to access opts and output and target, due to instance_exec' do @@ -512,8 +513,8 @@ describe "commands" do import_from Pry::Commands, "ls", "jump-to" end - klass.commands.include?("ls").should == true - klass.commands.include?("jump-to").should == true + klass.to_hash.include?("ls").should == true + klass.to_hash.include?("jump-to").should == true end it 'should delete some inherited commands when using delete method' do @@ -525,13 +526,14 @@ describe "commands" do delete "ls" end - klass.commands.include?("nesting").should == true - klass.commands.include?("jump-to").should == true - klass.commands.include?("cd").should == true - klass.commands.include?("v").should == true - klass.commands.include?("show-doc").should == false - klass.commands.include?("show-method").should == false - klass.commands.include?("ls").should == false + commands = klass.to_hash + commands.include?("nesting").should == true + commands.include?("jump-to").should == true + commands.include?("cd").should == true + commands.include?("v").should == true + commands.include?("show-doc").should == false + commands.include?("show-method").should == false + commands.include?("ls").should == false end it 'should override some inherited commands' do diff --git a/spec/command_set_spec.rb b/spec/command_set_spec.rb index 5e45724b..eb4b9e1b 100644 --- a/spec/command_set_spec.rb +++ b/spec/command_set_spec.rb @@ -167,7 +167,7 @@ describe Pry::CommandSet do it 'should set the descriptions of commands' do @set.command('foo', 'some stuff') {} - @set.commands['foo'].description.should == 'some stuff' + @set['foo'].description.should == 'some stuff' end describe "aliases" do @@ -176,8 +176,8 @@ describe Pry::CommandSet do @set.command('foo', 'stuff') { run = true } @set.alias_command 'bar', 'foo' - @set.commands['bar'].match.should == 'bar' - @set.commands['bar'].description.should == 'Alias for `foo`' + @set['bar'].match.should == 'bar' + @set['bar'].description.should == 'Alias for `foo`' @set.run_command @ctx, 'bar' run.should == true @@ -188,12 +188,12 @@ describe Pry::CommandSet do @set.command('foo', 'stuff', :shellwords => true, :interpolate => false) { run = true } @set.alias_command 'bar', 'foo' - @set.commands['bar'].options[:shellwords].should == @set.commands['foo'].options[:shellwords] - @set.commands['bar'].options[:interpolate].should == @set.commands['foo'].options[:interpolate] + @set['bar'].options[:shellwords].should == @set['foo'].options[:shellwords] + @set['bar'].options[:interpolate].should == @set['foo'].options[:interpolate] # however some options should not be inherited - @set.commands['bar'].options[:listing].should.not == @set.commands['foo'].options[:listing] - @set.commands['bar'].options[:listing].should == "bar" + @set['bar'].options[:listing].should.not == @set['foo'].options[:listing] + @set['bar'].options[:listing].should == "bar" end it 'should be able to specify alias\'s description when aliasing' do @@ -201,8 +201,8 @@ describe Pry::CommandSet do @set.command('foo', 'stuff') { run = true } @set.alias_command 'bar', 'foo', :desc => "tobina" - @set.commands['bar'].match.should == 'bar' - @set.commands['bar'].description.should == "tobina" + @set['bar'].match.should == 'bar' + @set['bar'].description.should == "tobina" @set.run_command @ctx, 'bar' run.should == true @@ -213,8 +213,8 @@ describe Pry::CommandSet do @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true } @set.alias_command 'bar', 'foo1' - @set.commands['bar'].match.should == 'bar' - @set.commands['bar'].description.should == 'Alias for `foo1`' + @set['bar'].match.should == 'bar' + @set['bar'].description.should == 'Alias for `foo1`' @set.run_command @ctx, 'bar' run.should == true @@ -225,7 +225,7 @@ describe Pry::CommandSet do @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true } @set.alias_command /^b.r/, 'foo1', :listing => "bar" - @set.commands[/^b.r/].options[:listing].should == "bar" + @set.to_hash[/^b.r/].options[:listing].should == "bar" end it "should set description to default if description parameter is nil" do @@ -233,7 +233,7 @@ describe Pry::CommandSet do @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true } @set.alias_command "bar", 'foo1' - @set.commands["bar"].description.should == "Alias for `foo1`" + @set["bar"].description.should == "Alias for `foo1`" end end @@ -241,7 +241,7 @@ describe Pry::CommandSet do @set.command('foo', 'bar') {} @set.desc 'foo', 'baz' - @set.commands['foo'].description.should == 'baz' + @set['foo'].description.should == 'baz' end it 'should get the descriptions of commands' do @@ -327,12 +327,12 @@ describe Pry::CommandSet do it 'should provide a :listing for a command that defaults to its name' do @set.command 'foo', "" do;end - @set.commands['foo'].options[:listing].should == 'foo' + @set['foo'].options[:listing].should == 'foo' end it 'should provide a :listing for a command that differs from its name' do @set.command 'foo', "", :listing => 'bar' do;end - @set.commands['foo'].options[:listing].should == 'bar' + @set['foo'].options[:listing].should == 'bar' end it "should provide a 'help' command" do @@ -377,9 +377,9 @@ describe Pry::CommandSet do listing = "bing" @set.command('foo') { } @set.rename_command('bar', 'foo', :description => desc, :listing => listing, :keep_retval => true) - @set.commands['bar'].description.should == desc - @set.commands['bar'].options[:listing].should == listing - @set.commands['bar'].options[:keep_retval].should == true + @set['bar'].description.should == desc + @set['bar'].options[:listing].should == listing + @set['bar'].options[:keep_retval].should == true end end diff --git a/spec/command_spec.rb b/spec/command_spec.rb index f89c2a2a..586a42e7 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -94,7 +94,7 @@ describe "Pry::Command" do # end - mock_command(@set.commands['help'], %w(oolon-colluphid), :command_set => @set).output.should =~ /Raving Atheist/ + mock_command(@set['help'], %w(oolon-colluphid), :command_set => @set).output.should =~ /Raving Atheist/ end it 'should use slop to generate the help for classy commands' do @@ -104,7 +104,7 @@ describe "Pry::Command" do end end - mock_command(@set.commands['help'], %w(eddie), :command_set => @set).output.should =~ /Over-cheerful/ + mock_command(@set['help'], %w(eddie), :command_set => @set).output.should =~ /Over-cheerful/ end it 'should provide --help for classy commands' do @@ -803,17 +803,17 @@ describe "Pry::Command" do end it 'should be correct for default commands' do - @set.commands["help"].group.should == "Help" + @set["help"].group.should == "Help" end it 'should not change once it is initialized' do - @set.commands["magic"].group("-==CD COMMAND==-") - @set.commands["magic"].group.should == "Not for a public use" + @set["magic"].group("-==CD COMMAND==-") + @set["magic"].group.should == "Not for a public use" end it 'should not disappear after the call without parameters' do - @set.commands["magic"].group - @set.commands["magic"].group.should == "Not for a public use" + @set["magic"].group + @set["magic"].group.should == "Not for a public use" end end end From 8334cd33ce479d8389bd00acdd0cf67f06138177 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 4 Feb 2014 05:19:38 +0100 Subject: [PATCH 168/186] use 'dev-facing changes' in changelog for command set change. --- CHANGELOG.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86f5e2f1..7ad069d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,11 +23,6 @@ * require of 'readline' is delayed until Pry.start() has been called for the first time. (#1117) * add option to disable input completer through `_pry_.config.completer = nil` -#### API change. -* CommandSet#commands, sometimes referenced through Pry.commands.commands, renamed as 'CommandSet#to_hash'. - it returns a duplicate of the internal hash a CommandSet uses. -* CommandSet#keys is now an alias of CommandSet#list_commands. - #### Bug fixes, etc. * `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) * Add support for BasicObjects to `ls` (#984) @@ -50,6 +45,9 @@ * Fix bug in `edit` regarding recognition of file names without suffix. #### Dev-facing changes +* CommandSet#commands, sometimes referenced through Pry.commands.commands, renamed as 'CommandSet#to_hash'. + it returns a duplicate of the internal hash a CommandSet uses. +* CommandSet#keys is now an alias of CommandSet#list_commands. * through changes to configuration, all commands should reference configuration values via `_pry_.config` and not `Pry.config`. (#1096) * improve configuration(Pry::Config) for easier support of concurrent environments From 71bb7b03c3f7f72857fde0d87472cade1215967e Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 4 Feb 2014 10:18:10 -0800 Subject: [PATCH 169/186] Fix watch command docs --- lib/pry/commands/watch_expression.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 24724123..7f66f3eb 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -4,7 +4,7 @@ class Pry match 'watch' group 'Context' - description 'Evaluate an expression after every command and display it when its value changes.' + description 'Watch the value of an expression and print a notification whenever it changes.' command_options :use_prefix => false banner <<-'BANNER' @@ -12,7 +12,18 @@ class Pry watch watch --delete [INDEX] - Evaluate an expression after every command and display it when its value changes. + watch [EXPRESSION] adds an expression to the list of those being watched. + It will be re-evaluated every time you hit enter in pry. If its value has + changed, the new value will be printed to the console. + + This is useful if you are step-through debugging and want to see how + something changes over time. It's also useful if you're trying to write + a method inside pry and want to check that it gives the right answers + every time you redefine it. + + watch on its own displays all the currently watched expressions and their + values, and watch --delete [INDEX] allows you to delete expressions from + the list being watched. BANNER def options(opt) From 263ab5bc063b8593ef1c2851da251e71d2ea7b1b Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 4 Feb 2014 10:19:56 -0800 Subject: [PATCH 170/186] Add watch to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ad069d7..a9a0ed22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Require Coderay `>= 1.1.0` #### Features +* Added a `watch` command that lets you see how values change over time. * Added an experimental `Pry.auto_resize!` method * Makes Pry notice that your window has resized and tell Readline about it * Fixes various bugs with command history after a window resize From a8156cbdc8eae76445e7255f6fbe31db6eaaf909 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 4 Feb 2014 10:55:06 -0800 Subject: [PATCH 171/186] .dup watched expressions --- lib/pry/commands/watch_expression/expression.rb | 4 ++++ spec/commands/watch_expression_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb index 1a3c1de9..e2a8dfba 100644 --- a/lib/pry/commands/watch_expression/expression.rb +++ b/lib/pry/commands/watch_expression/expression.rb @@ -11,6 +11,10 @@ class Pry def eval! @previous_value = value @value = target_eval(target, source) + begin + @value = @value.dup + rescue Pry::RescuableException + end end def to_s diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index ea62cedd..2642fbd1 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -51,6 +51,19 @@ describe "watch expression" do end end + it "prints when an expression is mutated" do + ReplTester.start do + input 'a = "one"' + output '=> "one"' + + input 'watch a' + output %(Watching a\nwatch: a => "one") + + input "a.sub! 'o', 'p'" + output %(watch: a => "pne"\n=> "pne") + end + end + it "doesn't print when an expresison remains the same" do ReplTester.start do input 'a = 1' From 95d9d9a0545de534224322ada5d080ceef36299e Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 4 Feb 2014 21:40:34 +0100 Subject: [PATCH 172/186] avoid dup on the UNDUP-able without rescue. --- lib/pry/commands/watch_expression/expression.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb index e2a8dfba..f9d9fa99 100644 --- a/lib/pry/commands/watch_expression/expression.rb +++ b/lib/pry/commands/watch_expression/expression.rb @@ -1,6 +1,7 @@ class Pry class Command::WatchExpression class Expression + NODUP = [TrueClass, FalseClass, NilClass, Numeric].freeze attr_reader :target, :source, :value, :previous_value def initialize(target, source) @@ -11,10 +12,7 @@ class Pry def eval! @previous_value = value @value = target_eval(target, source) - begin - @value = @value.dup - rescue Pry::RescuableException - end + @value = @value.dup unless NODUP.any? { |klass| klass === @value } end def to_s From 0fcc10cc1f2322ba0db0b91b5df9ed4c9ff19fa7 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 4 Feb 2014 21:55:27 +0100 Subject: [PATCH 173/186] add 'Symbol' to the un'dupables. --- lib/pry/commands/watch_expression/expression.rb | 2 +- lib/pry/config/behavior.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb index f9d9fa99..d01379a1 100644 --- a/lib/pry/commands/watch_expression/expression.rb +++ b/lib/pry/commands/watch_expression/expression.rb @@ -1,7 +1,7 @@ class Pry class Command::WatchExpression class Expression - NODUP = [TrueClass, FalseClass, NilClass, Numeric].freeze + NODUP = [TrueClass, FalseClass, NilClass, Numeric, Symbol].freeze attr_reader :target, :source, :value, :previous_value def initialize(target, source) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index d72d7afa..e4c950f0 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -1,6 +1,6 @@ module Pry::Config::Behavior ASSIGNMENT = "=".freeze - NODUP = [TrueClass, FalseClass, NilClass, Module, Proc, Numeric].freeze + NODUP = [TrueClass, FalseClass, NilClass, Symbol, Numeric, Module, Proc].freeze RESERVED_KEYS = [ "[]", "[]=", "merge!", "respond_to?", "key?", "refresh", From 21aa8959c8a5fd5739b4513d33eb1aba70531881 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 4 Feb 2014 23:17:27 +0100 Subject: [PATCH 174/186] read "watch_expressions" via _pry_.config --- lib/pry/commands/watch_expression.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 7f66f3eb..85ee2945 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -49,7 +49,7 @@ class Pry private def expressions - Pry.config.watch_expressions ||= [] + _pry_.config.watch_expressions ||= [] end def delete(index) From 4e9df5b901cb8cab044345c2e561b06cb8e7d69f Mon Sep 17 00:00:00 2001 From: yui-knk Date: Wed, 5 Feb 2014 22:29:25 +0900 Subject: [PATCH 175/186] Fix pry.config.output_prefix to work. Fix #1128 --- lib/pry.rb | 4 ++-- lib/pry/pry_instance.rb | 2 +- spec/pry_output_spec.rb | 19 ++++++++++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 10b1a675..f5cccff6 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -19,9 +19,9 @@ class Pry end # The default print - DEFAULT_PRINT = proc do |output, value| + DEFAULT_PRINT = proc do |output, value, _pry_| Pry::Pager.with_pager(output) do |pager| - pager.print "=> " + pager.print _pry_.config.output_prefix Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1) end end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 5db6a3fa..bd6abacc 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -347,7 +347,7 @@ class Pry if last_result_is_exception? exception_handler.call(output, result, self) elsif should_print? - print.call(output, result) + print.call(output, result, self) else # nothin' end diff --git a/spec/pry_output_spec.rb b/spec/pry_output_spec.rb index 5fe2d22f..c3707e45 100644 --- a/spec/pry_output_spec.rb +++ b/spec/pry_output_spec.rb @@ -35,8 +35,9 @@ describe Pry do end it "should include the =>" do + pry = Pry.new accumulator = StringIO.new - Pry.config.print.call(accumulator, [1]) + pry.config.print.call(accumulator, [1], pry) accumulator.string.should == "=> \[1\]\n" end @@ -49,6 +50,16 @@ describe Pry do end end + describe "output_prefix" do + it "should be able to change output_prefix" do + pry = Pry.new + accumulator = StringIO.new + pry.config.output_prefix = "-> " + pry.config.print.call(accumulator, [1], pry) + accumulator.string.should == "-> \[1\]\n" + end + end + describe "color" do before do Pry.color = true @@ -59,19 +70,21 @@ describe Pry do end it "should colorize strings as though they were ruby" do + pry = Pry.new accumulator = StringIO.new colorized = CodeRay.scan("[1]", :ruby).term - Pry.config.print.call(accumulator, [1]) + pry.config.print.call(accumulator, [1], pry) accumulator.string.should == "=> #{colorized}\n" end it "should not colorize strings that already include color" do + pry = Pry.new f = Object.new def f.inspect "\e[1;31mFoo\e[0m" end accumulator = StringIO.new - Pry.config.print.call(accumulator, f) + pry.config.print.call(accumulator, f, pry) # We add an extra \e[0m to prevent color leak accumulator.string.should == "=> \e[1;31mFoo\e[0m\e[0m\n" end From a01265881d498f16bc565d596fc4d3e937b9a0d2 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 5 Feb 2014 15:32:45 -0800 Subject: [PATCH 176/186] Fix ls on Fixnum [Fixes #1120] --- lib/pry/commands/ls/interrogateable.rb | 5 ++--- lib/pry/method.rb | 10 +++++++--- spec/commands/ls_spec.rb | 6 ++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/pry/commands/ls/interrogateable.rb b/lib/pry/commands/ls/interrogateable.rb index 9945fa23..89b94d45 100644 --- a/lib/pry/commands/ls/interrogateable.rb +++ b/lib/pry/commands/ls/interrogateable.rb @@ -10,9 +10,8 @@ module Pry::Command::Ls::Interrogateable if interrogating_a_module? @interrogatee else - class << @interrogatee - ancestors.grep(::Class).reject { |c| c == self }.first - end + singleton = Pry::Method.singleton_class_of(@interrogatee) + singleton.ancestors.grep(::Class).reject { |c| c == singleton }.first end end diff --git a/lib/pry/method.rb b/lib/pry/method.rb index c072fe89..64530b16 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -192,8 +192,6 @@ class Pry /^define_method\(?\s*[:\"\']#{Regexp.escape(name)}|^def\s*#{Regexp.escape(name)}/ =~ definition_line.strip end - private - # Get the singleton classes of superclasses that could define methods on # the given class object, and any modules they include. # If a module is included at multiple points in the ancestry, only @@ -207,7 +205,13 @@ class Pry resolution_order.reverse.uniq.reverse - Class.included_modules end - def singleton_class_of(obj); class << obj; self; end end + def singleton_class_of(obj) + begin + class << obj; self; end + rescue TypeError # can't define singleton. Fixnum, Symbol, Float, ... + obj.class + end + end end # A new instance of `Pry::Method` wrapping the given `::Method`, `UnboundMethod`, or `Proc`. diff --git a/spec/commands/ls_spec.rb b/spec/commands/ls_spec.rb index a35d8198..285fd589 100644 --- a/spec/commands/ls_spec.rb +++ b/spec/commands/ls_spec.rb @@ -47,6 +47,12 @@ describe "ls" do end end + describe "immediates" do + it "should work on Fixnum" do + pry_eval("ls 5").should =~ /Fixnum#methods:.*modulo/m + end + end + describe "methods" do it "should show public methods by default" do output = pry_eval("ls Class.new{ def goo; end; public :goo }.new") From 15d4a773533f16fa8c02e478a6e9609739b137f2 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 5 Feb 2014 15:33:56 -0800 Subject: [PATCH 177/186] sp. --- lib/pry/commands/ls/constants.rb | 4 ++-- lib/pry/commands/ls/instance_vars.rb | 4 ++-- lib/pry/commands/ls/{interrogateable.rb => interrogatable.rb} | 2 +- lib/pry/commands/ls/methods.rb | 4 ++-- lib/pry/commands/ls/self_methods.rb | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) rename lib/pry/commands/ls/{interrogateable.rb => interrogatable.rb} (88%) diff --git a/lib/pry/commands/ls/constants.rb b/lib/pry/commands/ls/constants.rb index 58561547..48670088 100644 --- a/lib/pry/commands/ls/constants.rb +++ b/lib/pry/commands/ls/constants.rb @@ -1,10 +1,10 @@ -require 'pry/commands/ls/interrogateable' +require 'pry/commands/ls/interrogatable' class Pry class Command::Ls < Pry::ClassCommand class Constants < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogateable + include Pry::Command::Ls::Interrogatable def initialize(interrogatee, target, no_user_opts, opts) super(target) diff --git a/lib/pry/commands/ls/instance_vars.rb b/lib/pry/commands/ls/instance_vars.rb index c25789fe..23155686 100644 --- a/lib/pry/commands/ls/instance_vars.rb +++ b/lib/pry/commands/ls/instance_vars.rb @@ -1,10 +1,10 @@ -require 'pry/commands/ls/interrogateable' +require 'pry/commands/ls/interrogatable' class Pry class Command::Ls < Pry::ClassCommand class InstanceVars < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogateable + include Pry::Command::Ls::Interrogatable def initialize(interrogatee, no_user_opts, opts) @interrogatee = interrogatee diff --git a/lib/pry/commands/ls/interrogateable.rb b/lib/pry/commands/ls/interrogatable.rb similarity index 88% rename from lib/pry/commands/ls/interrogateable.rb rename to lib/pry/commands/ls/interrogatable.rb index 89b94d45..5904d1b4 100644 --- a/lib/pry/commands/ls/interrogateable.rb +++ b/lib/pry/commands/ls/interrogatable.rb @@ -1,4 +1,4 @@ -module Pry::Command::Ls::Interrogateable +module Pry::Command::Ls::Interrogatable private diff --git a/lib/pry/commands/ls/methods.rb b/lib/pry/commands/ls/methods.rb index a301b33e..9bc108ee 100644 --- a/lib/pry/commands/ls/methods.rb +++ b/lib/pry/commands/ls/methods.rb @@ -1,11 +1,11 @@ require 'pry/commands/ls/methods_helper' -require 'pry/commands/ls/interrogateable' +require 'pry/commands/ls/interrogatable' class Pry class Command::Ls < Pry::ClassCommand class Methods < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogateable + include Pry::Command::Ls::Interrogatable include Pry::Command::Ls::MethodsHelper def initialize(interrogatee, no_user_opts, opts) diff --git a/lib/pry/commands/ls/self_methods.rb b/lib/pry/commands/ls/self_methods.rb index 0756cf78..cd68b227 100644 --- a/lib/pry/commands/ls/self_methods.rb +++ b/lib/pry/commands/ls/self_methods.rb @@ -1,11 +1,11 @@ -require 'pry/commands/ls/interrogateable' +require 'pry/commands/ls/interrogatable' require 'pry/commands/ls/methods_helper' class Pry class Command::Ls < Pry::ClassCommand class SelfMethods < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogateable + include Pry::Command::Ls::Interrogatable include Pry::Command::Ls::MethodsHelper def initialize(interrogatee, no_user_opts, opts) From ebfa28a84abcc73c72cead3622e4d62b114868d9 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 7 Feb 2014 01:52:12 +0100 Subject: [PATCH 178/186] update shell_command.rb to use _pry_.config.system --- lib/pry/commands/shell_command.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pry/commands/shell_command.rb b/lib/pry/commands/shell_command.rb index 69559405..6cb32ec9 100644 --- a/lib/pry/commands/shell_command.rb +++ b/lib/pry/commands/shell_command.rb @@ -20,11 +20,10 @@ class Pry process_cd parse_destination($1) else pass_block(cmd) - if command_block command_block.call `#{cmd}` else - Pry.config.system.call(output, cmd, _pry_) + _pry_.config.system.call(output, cmd, _pry_) end end end From e24788f39949f7298a734639012f4cb01bbf99ce Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 7 Feb 2014 01:53:36 +0100 Subject: [PATCH 179/186] update whereami command to use _pry_.config.default_window_size. --- lib/pry/commands/whereami.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index 1cf4bab7..9be10e7f 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -178,7 +178,7 @@ class Pry def window_size if args.empty? - Pry.config.default_window_size + _pry_.config.default_window_size else args.first.to_i end From fd9a23eb30aec723d0775d72a31e93280409d3fb Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 7 Feb 2014 01:55:48 +0100 Subject: [PATCH 180/186] update 'cat' command to use _pry_.config.default_window_size --- lib/pry/commands/cat/exception_formatter.rb | 8 ++++---- lib/pry/commands/cat/file_formatter.rb | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/pry/commands/cat/exception_formatter.rb b/lib/pry/commands/cat/exception_formatter.rb index ca6818da..f794c68d 100644 --- a/lib/pry/commands/cat/exception_formatter.rb +++ b/lib/pry/commands/cat/exception_formatter.rb @@ -1,9 +1,9 @@ class Pry class Command::Cat class ExceptionFormatter < AbstractFormatter - attr_accessor :ex - attr_accessor :opts - attr_accessor :_pry_ + attr_reader :ex + attr_reader :opts + attr_reader :_pry_ def initialize(exception, _pry_, opts) @ex = exception @@ -23,7 +23,7 @@ class Pry private def code_window_size - Pry.config.default_window_size || 5 + _pry_.config.default_window_size || 5 end def backtrace_level diff --git a/lib/pry/commands/cat/file_formatter.rb b/lib/pry/commands/cat/file_formatter.rb index a03655fb..92c62688 100644 --- a/lib/pry/commands/cat/file_formatter.rb +++ b/lib/pry/commands/cat/file_formatter.rb @@ -1,9 +1,9 @@ class Pry class Command::Cat class FileFormatter < AbstractFormatter - attr_accessor :file_with_embedded_line - attr_accessor :opts - attr_accessor :_pry_ + attr_reader :file_with_embedded_line + attr_reader :opts + attr_reader :_pry_ def initialize(file_with_embedded_line, _pry_, opts) @file_with_embedded_line = file_with_embedded_line @@ -36,7 +36,7 @@ class Pry end def code_window_size - Pry.config.default_window_size || 7 + _pry_.config.default_window_size || 7 end def decorate(content) @@ -49,7 +49,7 @@ class Pry def detect_code_type_from_file(file_name) code_type = @code_from_file.code_type - + if code_type == :unknown name, ext = File.basename(file_name).split('.', 2) case name From 7f0e6f999f86f9bd88c35d68f3ba1db0abd13b34 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 7 Feb 2014 01:59:03 +0100 Subject: [PATCH 181/186] update lib/pry/commands/code_collector.rb to use _pry_.config.gist.inspecter.call --- lib/pry/commands/code_collector.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pry/commands/code_collector.rb b/lib/pry/commands/code_collector.rb index ffeb5d1e..72e81815 100644 --- a/lib/pry/commands/code_collector.rb +++ b/lib/pry/commands/code_collector.rb @@ -2,9 +2,9 @@ class Pry class Command::CodeCollector include Helpers::CommandHelpers - attr_accessor :args - attr_accessor :opts - attr_accessor :_pry_ + attr_reader :args + attr_reader :opts + attr_reader :_pry_ # The name of the explicitly given file (if any). attr_accessor :file @@ -91,7 +91,7 @@ class Pry # @return [String] def pry_output_content pry_array_content_as_string(_pry_.output_array, self.class.output_result_ranges) do |v| - Pry.config.gist.inspecter.call(v) + _pry_.config.gist.inspecter.call(v) end end From 14771b2bb43c89c797d67d3ced5afab59e8337a2 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 7 Feb 2014 02:00:50 +0100 Subject: [PATCH 182/186] update 'edit' command to use _pry_.config.disable_auto_reload --- lib/pry/commands/edit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index dd00c5ec..93873575 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -165,7 +165,7 @@ class Pry end def never_reload? - opts.present?(:'no-reload') || Pry.config.disable_auto_reload + opts.present?(:'no-reload') || _pry_.config.disable_auto_reload end def reload?(file_name="") From e3c80b20f2b53af47bd1317b4180fc3c1f712ca1 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Fri, 7 Feb 2014 02:01:39 +0100 Subject: [PATCH 183/186] update 'gist' command to use _pry_.config.gist.inspecter --- lib/pry/commands/edit.rb | 2 +- lib/pry/commands/gist.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index 93873575..310fe93e 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -12,7 +12,7 @@ class Pry Open a text editor. When no FILE is given, edits the pry input buffer. When a method/module/command is given, the code is opened in an editor. - Ensure `Pry.config.editor` is set to your editor of choice. + Ensure `Pry.config.editor` or `_pry_.config.editor` is set to your editor of choice. edit sample.rb edit -p MyClass#my_method edit sample.rb --line 105 edit MyClass diff --git a/lib/pry/commands/gist.rb b/lib/pry/commands/gist.rb index 9465cf3d..cacfb05a 100644 --- a/lib/pry/commands/gist.rb +++ b/lib/pry/commands/gist.rb @@ -58,7 +58,7 @@ class Pry if code && code != "" content << code if code !~ /;\Z/ - content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}" + content << "#{comment_expression_result_for_gist(_pry_.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}" end end end From ea307f87218dcbf9f91fd32898a25e800d16a1cf Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 7 Feb 2014 18:02:00 -0800 Subject: [PATCH 184/186] Use string difference in watch --- .../commands/watch_expression/expression.rb | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb index d01379a1..c0e15f36 100644 --- a/lib/pry/commands/watch_expression/expression.rb +++ b/lib/pry/commands/watch_expression/expression.rb @@ -1,36 +1,30 @@ class Pry class Command::WatchExpression class Expression - NODUP = [TrueClass, FalseClass, NilClass, Numeric, Symbol].freeze attr_reader :target, :source, :value, :previous_value def initialize(target, source) @target = target - @source = source + @source = Code.new(source).strip end def eval! - @previous_value = value - @value = target_eval(target, source) - @value = @value.dup unless NODUP.any? { |klass| klass === @value } + @previous_value = @value + @value = Pry::ColorPrinter.pp(target_eval(target, source), "") end def to_s - "#{print_source} => #{print_value}" + "#{source} => #{value}" end + # Has the value of the expression changed? + # + # We use the pretty-printed string represenation to detect differences + # as this avoids problems with dup (causes too many differences) and == (causes too few) def changed? (value != previous_value) end - def print_value - Pry::ColorPrinter.pp(value, "") - end - - def print_source - Code.new(source).strip - end - private def target_eval(target, source) From cf088c32e0917f7aa52b43a6143dfd871aa26c96 Mon Sep 17 00:00:00 2001 From: Ivo Wever Date: Sat, 8 Feb 2014 12:33:46 +0100 Subject: [PATCH 185/186] Updated changelog as promised in #1114 Fixes #1114 --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9a0ed22..ff0720d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,15 @@ methods of determining the terminal's dimensions * Add `ReplTester` class for high-level simulation of Pry sessions in tests +### 0.9.12.6 (2014/01/28) +* Don't fail if Bond is not installed (#1106) + +### 0.9.12.5 (2014/01/27) +* Fix early readline errors by deferring require of readline (#1081, #1095) + +### 0.9.12.4 (2013/11/23) +* Fix issue with Coderay colors being black, even when on a black background (#1016) + ### 0.9.12.3 (2013/09/11) * Bump Coderay dependency (#987) * Fix consecutive newlines in heredocs being collapsed (#962) From e5ecf91291f5be0fe3c78caf01a9640468b391ec Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 8 Feb 2014 05:32:42 +0100 Subject: [PATCH 186/186] update 'ls' command to go through `_pry_.config`. closes #1126. --- .travis.yml | 2 +- lib/pry/commands/ls.rb | 5 ++--- lib/pry/commands/ls/constants.rb | 6 +++--- lib/pry/commands/ls/formatter.rb | 9 +++++---- lib/pry/commands/ls/globals.rb | 4 ++-- lib/pry/commands/ls/instance_vars.rb | 4 ++-- lib/pry/commands/ls/local_names.rb | 6 +++--- lib/pry/commands/ls/local_vars.rb | 6 +++--- lib/pry/commands/ls/ls_entity.rb | 19 +++++++++---------- lib/pry/commands/ls/methods.rb | 7 ++++--- lib/pry/commands/ls/self_methods.rb | 4 ++-- 11 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.travis.yml b/.travis.yml index 95574215..321fb5eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ rvm: - 2.1.0 - ruby-head - rbx - - jruby-19mode + - jruby - jruby-head script: diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 02f40dac..1730b16e 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -59,11 +59,10 @@ class Pry ls_entity = LsEntity.new({ :interrogatee => @interrogatee, - :target => target, :no_user_opts => no_user_opts?, :opts => opts, - :sticky_locals => _pry_.sticky_locals, - :args => args + :args => args, + :_pry_ => _pry_ }) stagger_output(ls_entity.entities_table) diff --git a/lib/pry/commands/ls/constants.rb b/lib/pry/commands/ls/constants.rb index 48670088..3db81624 100644 --- a/lib/pry/commands/ls/constants.rb +++ b/lib/pry/commands/ls/constants.rb @@ -3,11 +3,11 @@ require 'pry/commands/ls/interrogatable' class Pry class Command::Ls < Pry::ClassCommand class Constants < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogatable - def initialize(interrogatee, target, no_user_opts, opts) - super(target) + + def initialize(interrogatee, no_user_opts, opts, _pry_) + super(_pry_) @interrogatee = interrogatee @no_user_opts = no_user_opts @default_switch = opts[:constants] diff --git a/lib/pry/commands/ls/formatter.rb b/lib/pry/commands/ls/formatter.rb index e657681a..844a220f 100644 --- a/lib/pry/commands/ls/formatter.rb +++ b/lib/pry/commands/ls/formatter.rb @@ -1,11 +1,12 @@ class Pry class Command::Ls < Pry::ClassCommand class Formatter - attr_accessor :grep + attr_reader :_pry_ - def initialize(target) - @target = target + def initialize(_pry_) + @_pry_ = _pry_ + @target = _pry_.current_context end def write_out @@ -16,7 +17,7 @@ class Pry private def color(type, str) - Pry::Helpers::Text.send(Pry.config.ls.send(:"#{type}_color"), str) + Pry::Helpers::Text.send _pry_.config.ls["#{type}_color"], str end # Add a new section to the output. diff --git a/lib/pry/commands/ls/globals.rb b/lib/pry/commands/ls/globals.rb index 4a44f5f9..2b3d0fde 100644 --- a/lib/pry/commands/ls/globals.rb +++ b/lib/pry/commands/ls/globals.rb @@ -19,8 +19,8 @@ class Pry $CHILD_STATUS $SAFE $ERROR_INFO $ERROR_POSITION $LAST_MATCH_INFO $LAST_PAREN_MATCH $LAST_READ_LINE $MATCH $POSTMATCH $PREMATCH) - def initialize(target, opts) - super(target) + def initialize(opts, _pry_) + super(_pry_) @default_switch = opts[:globals] end diff --git a/lib/pry/commands/ls/instance_vars.rb b/lib/pry/commands/ls/instance_vars.rb index 23155686..2f33677f 100644 --- a/lib/pry/commands/ls/instance_vars.rb +++ b/lib/pry/commands/ls/instance_vars.rb @@ -3,10 +3,10 @@ require 'pry/commands/ls/interrogatable' class Pry class Command::Ls < Pry::ClassCommand class InstanceVars < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogatable - def initialize(interrogatee, no_user_opts, opts) + def initialize(interrogatee, no_user_opts, opts, _pry_) + super(_pry_) @interrogatee = interrogatee @no_user_opts = no_user_opts @default_switch = opts[:ivars] diff --git a/lib/pry/commands/ls/local_names.rb b/lib/pry/commands/ls/local_names.rb index c4240149..9d6350b5 100644 --- a/lib/pry/commands/ls/local_names.rb +++ b/lib/pry/commands/ls/local_names.rb @@ -2,11 +2,11 @@ class Pry class Command::Ls < Pry::ClassCommand class LocalNames < Pry::Command::Ls::Formatter - def initialize(target, no_user_opts, sticky_locals, args) - super(target) + def initialize(no_user_opts, args, _pry_) + super(_pry_) @no_user_opts = no_user_opts - @sticky_locals = sticky_locals @args = args + @sticky_locals = _pry_.sticky_locals end def correct_opts? diff --git a/lib/pry/commands/ls/local_vars.rb b/lib/pry/commands/ls/local_vars.rb index 9f0952ad..d48ddae7 100644 --- a/lib/pry/commands/ls/local_vars.rb +++ b/lib/pry/commands/ls/local_vars.rb @@ -2,10 +2,10 @@ class Pry class Command::Ls < Pry::ClassCommand class LocalVars < Pry::Command::Ls::Formatter - def initialize(target, sticky_locals, opts) - super(target) - @sticky_locals = sticky_locals + def initialize(opts, _pry_) + super(_pry_) @default_switch = opts[:locals] + @sticky_locals = _pry_.sticky_locals end def output_self diff --git a/lib/pry/commands/ls/ls_entity.rb b/lib/pry/commands/ls/ls_entity.rb index a8958d19..3d69bcdd 100644 --- a/lib/pry/commands/ls/ls_entity.rb +++ b/lib/pry/commands/ls/ls_entity.rb @@ -12,15 +12,15 @@ class Pry class Command::Ls < Pry::ClassCommand class LsEntity + attr_reader :_pry_ def initialize(opts) @interrogatee = opts[:interrogatee] - @target = opts[:target] @no_user_opts = opts[:no_user_opts] @opts = opts[:opts] - @sticky_locals = opts[:sticky_locals] @args = opts[:args] @grep = Grep.new(Regexp.new(opts[:opts][:G] || '.')) + @_pry_ = opts.delete(:_pry_) end def entities_table @@ -34,38 +34,37 @@ class Pry end def globals - grep(Globals.new(@target, @opts)) + grep Globals.new(@opts, _pry_) end def constants - grep(Constants.new(@interrogatee, @target, @no_user_opts, @opts)) + grep Constants.new(@interrogatee, @no_user_opts, @opts, _pry_) end def methods - grep(Methods.new(@interrogatee, @no_user_opts, @opts)) + grep(Methods.new(@interrogatee, @no_user_opts, @opts, _pry_)) end def self_methods - grep(SelfMethods.new(@interrogatee, @no_user_opts, @opts)) + grep SelfMethods.new(@interrogatee, @no_user_opts, @opts, _pry_) end def instance_vars - grep(InstanceVars.new(@interrogatee, @no_user_opts, @opts)) + grep InstanceVars.new(@interrogatee, @no_user_opts, @opts, _pry_) end def local_names - grep(LocalNames.new(@target, @no_user_opts, @sticky_locals, @args)) + grep LocalNames.new(@no_user_opts, @args, _pry_) end def local_vars - LocalVars.new(@target, @sticky_locals, @opts) + LocalVars.new(@opts, _pry_) end def entities [globals, constants, methods, self_methods, instance_vars, local_names, local_vars] end - end end end diff --git a/lib/pry/commands/ls/methods.rb b/lib/pry/commands/ls/methods.rb index 9bc108ee..c085f421 100644 --- a/lib/pry/commands/ls/methods.rb +++ b/lib/pry/commands/ls/methods.rb @@ -8,7 +8,8 @@ class Pry include Pry::Command::Ls::Interrogatable include Pry::Command::Ls::MethodsHelper - def initialize(interrogatee, no_user_opts, opts) + def initialize(interrogatee, no_user_opts, opts, _pry_) + super(_pry_) @interrogatee = interrogatee @no_user_opts = no_user_opts @default_switch = opts[:methods] @@ -42,11 +43,11 @@ class Pry def below_ceiling ceiling = if @quiet_switch [Pry::Method.safe_send(interrogatee_mod, :ancestors)[1]] + - Pry.config.ls.ceiling + _pry_.config.ls.ceiling elsif @verbose_switch [] else - Pry.config.ls.ceiling.dup + _pry_.config.ls.ceiling.dup end lambda { |klass| !ceiling.include?(klass) } end diff --git a/lib/pry/commands/ls/self_methods.rb b/lib/pry/commands/ls/self_methods.rb index cd68b227..91f3490b 100644 --- a/lib/pry/commands/ls/self_methods.rb +++ b/lib/pry/commands/ls/self_methods.rb @@ -4,11 +4,11 @@ require 'pry/commands/ls/methods_helper' class Pry class Command::Ls < Pry::ClassCommand class SelfMethods < Pry::Command::Ls::Formatter - include Pry::Command::Ls::Interrogatable include Pry::Command::Ls::MethodsHelper - def initialize(interrogatee, no_user_opts, opts) + def initialize(interrogatee, no_user_opts, opts, _pry_) + super(_pry_) @interrogatee = interrogatee @no_user_opts = no_user_opts end