pry 0.10.3 → 0.14.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +439 -16
- data/LICENSE +1 -1
- data/README.md +362 -302
- data/bin/pry +4 -7
- data/lib/pry/basic_object.rb +10 -0
- data/lib/pry/block_command.rb +22 -0
- data/lib/pry/class_command.rb +194 -0
- data/lib/pry/cli.rb +84 -97
- data/lib/pry/code/code_file.rb +37 -26
- data/lib/pry/code/code_range.rb +7 -5
- data/lib/pry/code/loc.rb +26 -13
- data/lib/pry/code.rb +42 -31
- data/lib/pry/code_object.rb +53 -28
- data/lib/pry/color_printer.rb +46 -35
- data/lib/pry/command.rb +197 -369
- data/lib/pry/command_set.rb +89 -114
- data/lib/pry/command_state.rb +31 -0
- data/lib/pry/commands/amend_line.rb +86 -82
- data/lib/pry/commands/bang.rb +18 -14
- data/lib/pry/commands/bang_pry.rb +15 -11
- data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
- data/lib/pry/commands/cat/exception_formatter.rb +85 -72
- data/lib/pry/commands/cat/file_formatter.rb +56 -46
- data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
- data/lib/pry/commands/cat.rb +62 -54
- data/lib/pry/commands/cd.rb +40 -35
- data/lib/pry/commands/change_inspector.rb +29 -22
- data/lib/pry/commands/change_prompt.rb +48 -23
- data/lib/pry/commands/clear_screen.rb +20 -0
- data/lib/pry/commands/code_collector.rb +148 -131
- data/lib/pry/commands/disable_pry.rb +23 -19
- data/lib/pry/commands/easter_eggs.rb +23 -34
- data/lib/pry/commands/edit/exception_patcher.rb +21 -17
- data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
- data/lib/pry/commands/edit.rb +185 -157
- data/lib/pry/commands/exit.rb +40 -35
- data/lib/pry/commands/exit_all.rb +24 -20
- data/lib/pry/commands/exit_program.rb +20 -16
- data/lib/pry/commands/find_method.rb +168 -162
- data/lib/pry/commands/fix_indent.rb +16 -12
- data/lib/pry/commands/help.rb +140 -133
- data/lib/pry/commands/hist.rb +151 -149
- data/lib/pry/commands/import_set.rb +20 -15
- data/lib/pry/commands/jump_to.rb +25 -21
- data/lib/pry/commands/list_inspectors.rb +35 -28
- data/lib/pry/commands/ls/constants.rb +59 -31
- data/lib/pry/commands/ls/formatter.rb +42 -36
- data/lib/pry/commands/ls/globals.rb +38 -36
- data/lib/pry/commands/ls/grep.rb +17 -15
- data/lib/pry/commands/ls/instance_vars.rb +29 -28
- data/lib/pry/commands/ls/interrogatable.rb +18 -12
- data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
- data/lib/pry/commands/ls/local_names.rb +26 -24
- data/lib/pry/commands/ls/local_vars.rb +38 -30
- data/lib/pry/commands/ls/ls_entity.rb +47 -52
- data/lib/pry/commands/ls/methods.rb +49 -51
- data/lib/pry/commands/ls/methods_helper.rb +46 -42
- data/lib/pry/commands/ls/self_methods.rb +23 -21
- data/lib/pry/commands/ls.rb +124 -103
- data/lib/pry/commands/nesting.rb +21 -17
- data/lib/pry/commands/play.rb +92 -82
- data/lib/pry/commands/pry_backtrace.rb +22 -17
- data/lib/pry/commands/pry_version.rb +15 -11
- data/lib/pry/commands/raise_up.rb +33 -27
- data/lib/pry/commands/reload_code.rb +60 -48
- data/lib/pry/commands/reset.rb +16 -12
- data/lib/pry/commands/ri.rb +57 -42
- data/lib/pry/commands/save_file.rb +45 -43
- data/lib/pry/commands/shell_command.rb +56 -29
- data/lib/pry/commands/shell_mode.rb +22 -18
- data/lib/pry/commands/show_doc.rb +80 -70
- data/lib/pry/commands/show_info.rb +194 -155
- data/lib/pry/commands/show_input.rb +16 -11
- data/lib/pry/commands/show_source.rb +110 -42
- data/lib/pry/commands/stat.rb +35 -31
- data/lib/pry/commands/switch_to.rb +21 -15
- data/lib/pry/commands/toggle_color.rb +20 -16
- data/lib/pry/commands/watch_expression/expression.rb +32 -27
- data/lib/pry/commands/watch_expression.rb +89 -84
- data/lib/pry/commands/whereami.rb +156 -141
- data/lib/pry/commands/wtf.rb +78 -40
- data/lib/pry/config/attributable.rb +22 -0
- data/lib/pry/config/lazy_value.rb +29 -0
- data/lib/pry/config/memoized_value.rb +34 -0
- data/lib/pry/config/value.rb +24 -0
- data/lib/pry/config.rb +310 -20
- data/lib/pry/control_d_handler.rb +28 -0
- data/lib/pry/core_extensions.rb +22 -9
- data/lib/pry/editor.rb +56 -34
- data/lib/pry/env.rb +18 -0
- data/lib/pry/exception_handler.rb +43 -0
- data/lib/pry/exceptions.rb +13 -18
- data/lib/pry/forwardable.rb +27 -0
- data/lib/pry/helpers/base_helpers.rb +20 -62
- data/lib/pry/helpers/command_helpers.rb +52 -62
- data/lib/pry/helpers/documentation_helpers.rb +21 -12
- data/lib/pry/helpers/options_helpers.rb +15 -8
- data/lib/pry/helpers/platform.rb +55 -0
- data/lib/pry/helpers/table.rb +44 -32
- data/lib/pry/helpers/text.rb +96 -85
- data/lib/pry/helpers.rb +3 -0
- data/lib/pry/history.rb +81 -55
- data/lib/pry/hooks.rb +60 -110
- data/lib/pry/indent.rb +74 -68
- data/lib/pry/input_completer.rb +199 -158
- data/lib/pry/input_lock.rb +7 -10
- data/lib/pry/inspector.rb +36 -24
- data/lib/pry/last_exception.rb +45 -45
- data/lib/pry/method/disowned.rb +19 -5
- data/lib/pry/method/patcher.rb +14 -8
- data/lib/pry/method/weird_method_locator.rb +79 -45
- data/lib/pry/method.rb +178 -124
- data/lib/pry/object_path.rb +37 -28
- data/lib/pry/output.rb +102 -16
- data/lib/pry/pager.rb +187 -174
- data/lib/pry/prompt.rb +213 -25
- data/lib/pry/pry_class.rb +119 -98
- data/lib/pry/pry_instance.rb +261 -224
- data/lib/pry/repl.rb +83 -29
- data/lib/pry/repl_file_loader.rb +27 -22
- data/lib/pry/ring.rb +89 -0
- data/lib/pry/slop/LICENSE +20 -0
- data/lib/pry/slop/commands.rb +190 -0
- data/lib/pry/slop/option.rb +210 -0
- data/lib/pry/slop.rb +672 -0
- data/lib/pry/syntax_highlighter.rb +26 -0
- data/lib/pry/system_command_handler.rb +17 -0
- data/lib/pry/testable/evalable.rb +24 -0
- data/lib/pry/testable/mockable.rb +22 -0
- data/lib/pry/testable/pry_tester.rb +88 -0
- data/lib/pry/testable/utility.rb +34 -0
- data/lib/pry/testable/variables.rb +52 -0
- data/lib/pry/testable.rb +68 -0
- data/lib/pry/version.rb +3 -1
- data/lib/pry/warning.rb +20 -0
- data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +35 -32
- data/lib/pry/wrapped_module.rb +68 -63
- data/lib/pry.rb +133 -149
- metadata +58 -69
- data/lib/pry/commands/disabled_commands.rb +0 -2
- data/lib/pry/commands/gem_cd.rb +0 -26
- data/lib/pry/commands/gem_install.rb +0 -32
- data/lib/pry/commands/gem_list.rb +0 -33
- data/lib/pry/commands/gem_open.rb +0 -29
- data/lib/pry/commands/gist.rb +0 -101
- data/lib/pry/commands/install_command.rb +0 -53
- data/lib/pry/commands/list_prompts.rb +0 -35
- data/lib/pry/commands/simple_prompt.rb +0 -22
- data/lib/pry/commands.rb +0 -6
- data/lib/pry/config/behavior.rb +0 -139
- data/lib/pry/config/convenience.rb +0 -25
- data/lib/pry/config/default.rb +0 -161
- data/lib/pry/history_array.rb +0 -121
- data/lib/pry/plugins.rb +0 -103
- data/lib/pry/rbx_path.rb +0 -22
- data/lib/pry/rubygem.rb +0 -82
- data/lib/pry/terminal.rb +0 -79
- data/lib/pry/test/helper.rb +0 -170
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Pry
|
4
|
+
module Testable
|
5
|
+
module Evalable
|
6
|
+
def pry_tester(*args, &block)
|
7
|
+
args.unshift(Pry.toplevel_binding) if args.empty? || args[0].is_a?(Hash)
|
8
|
+
Pry::Testable::PryTester.new(*args).tap do |t|
|
9
|
+
t.singleton_class.class_eval(&block) if block
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def pry_eval(*eval_strs)
|
14
|
+
b =
|
15
|
+
if eval_strs.first.is_a?(String)
|
16
|
+
Pry.toplevel_binding
|
17
|
+
else
|
18
|
+
Pry.binding_for(eval_strs.shift)
|
19
|
+
end
|
20
|
+
pry_tester(b).eval(*eval_strs)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class Pry
|
6
|
+
module Testable
|
7
|
+
module Mockable
|
8
|
+
def mock_command(cmd, args = [], opts = {})
|
9
|
+
output = StringIO.new
|
10
|
+
pry = Pry.new(output: output)
|
11
|
+
ret = cmd.new(opts.merge(pry_instance: pry, output: output)).call_safely(*args)
|
12
|
+
Struct.new(:output, :return).new(output.string, ret)
|
13
|
+
end
|
14
|
+
|
15
|
+
def mock_exception(*mock_backtrace)
|
16
|
+
StandardError.new.tap do |e|
|
17
|
+
e.define_singleton_method(:backtrace) { mock_backtrace }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class Pry
|
6
|
+
module Testable
|
7
|
+
class PryTester
|
8
|
+
extend Pry::Forwardable
|
9
|
+
attr_reader :pry, :out
|
10
|
+
def_delegators :@pry, :eval_string, :eval_string=
|
11
|
+
|
12
|
+
def initialize(target = TOPLEVEL_BINDING, options = {})
|
13
|
+
@pry = Pry.new(options.merge(target: target))
|
14
|
+
@history = options[:history]
|
15
|
+
@pry.inject_sticky_locals!
|
16
|
+
reset_output
|
17
|
+
end
|
18
|
+
|
19
|
+
def eval(*strs)
|
20
|
+
reset_output
|
21
|
+
result = nil
|
22
|
+
|
23
|
+
strs.flatten.each do |str|
|
24
|
+
# Check for space prefix. See #1369.
|
25
|
+
str = "#{str.strip}\n" if str !~ /^\s\S/
|
26
|
+
@history.push str if @history
|
27
|
+
|
28
|
+
result =
|
29
|
+
if @pry.process_command(str)
|
30
|
+
last_command_result_or_output
|
31
|
+
else
|
32
|
+
# Check if this is a multiline paste.
|
33
|
+
begin
|
34
|
+
complete_expr = Pry::Code.complete_expression?(str)
|
35
|
+
rescue SyntaxError => exception
|
36
|
+
@pry.output.puts(
|
37
|
+
"SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
|
38
|
+
)
|
39
|
+
end
|
40
|
+
@pry.evaluate_ruby(str) if complete_expr
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
def push(*lines)
|
48
|
+
Array(lines).flatten.each do |line|
|
49
|
+
@pry.eval(line)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def push_binding(context)
|
54
|
+
@pry.push_binding context
|
55
|
+
end
|
56
|
+
|
57
|
+
def last_output
|
58
|
+
@out.string if @out
|
59
|
+
end
|
60
|
+
|
61
|
+
def process_command(command_str)
|
62
|
+
@pry.process_command(command_str) || raise("Not a valid command")
|
63
|
+
last_command_result_or_output
|
64
|
+
end
|
65
|
+
|
66
|
+
def last_command_result
|
67
|
+
result = Pry.current[:pry_cmd_result]
|
68
|
+
result.retval if result
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def last_command_result_or_output
|
74
|
+
result = last_command_result
|
75
|
+
if result != Pry::Command::VOID_VALUE
|
76
|
+
result
|
77
|
+
else
|
78
|
+
last_output
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def reset_output
|
83
|
+
@out = StringIO.new
|
84
|
+
@pry.output = @out
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
class Pry
|
6
|
+
module Testable
|
7
|
+
module Utility
|
8
|
+
#
|
9
|
+
# Creates a Tempfile then unlinks it after the block has yielded.
|
10
|
+
#
|
11
|
+
# @yieldparam [String] file
|
12
|
+
# The path of the temp file
|
13
|
+
#
|
14
|
+
# @return [void]
|
15
|
+
#
|
16
|
+
def temp_file(ext = '.rb')
|
17
|
+
file = Tempfile.open(['pry', ext])
|
18
|
+
yield file
|
19
|
+
ensure
|
20
|
+
file.close(true) if file
|
21
|
+
end
|
22
|
+
|
23
|
+
def unindent(*args)
|
24
|
+
Pry::Helpers::CommandHelpers.unindent(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
def inner_scope
|
28
|
+
catch(:inner_scope) do
|
29
|
+
yield -> { throw(:inner_scope, self) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Pry
|
4
|
+
module Testable
|
5
|
+
module Variables
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# temporary_constants(:Foo, :Bar) do
|
9
|
+
# Foo = Class.new(RuntimeError)
|
10
|
+
# Bar = Class.new(RuntimeError)
|
11
|
+
# end
|
12
|
+
# Foo # => NameError
|
13
|
+
# Bar # => NameError
|
14
|
+
#
|
15
|
+
# @param [Array<Symbol>] names
|
16
|
+
# An array of constant names that be defined by a block,
|
17
|
+
# and removed by this method afterwards.
|
18
|
+
#
|
19
|
+
# @return [void]
|
20
|
+
#
|
21
|
+
def temporary_constants(*names)
|
22
|
+
names.each do |name|
|
23
|
+
Object.remove_const name if Object.const_defined?(name)
|
24
|
+
end
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
names.each do |name|
|
28
|
+
Object.remove_const name if Object.const_defined?(name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# @param [String] name
|
34
|
+
# The name of a variable.
|
35
|
+
#
|
36
|
+
# @param [String] value
|
37
|
+
# Its value.
|
38
|
+
#
|
39
|
+
# @param [Binding] binding
|
40
|
+
# The binding object to insert a variable into.
|
41
|
+
#
|
42
|
+
# @return [void]
|
43
|
+
#
|
44
|
+
def insert_variable(name, value, binding)
|
45
|
+
Pry.current[:pry_local] = value
|
46
|
+
binding.eval("#{name} = ::Pry.current[:pry_local]")
|
47
|
+
ensure
|
48
|
+
Pry.current[:pry_local] = nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/pry/testable.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# good idea ???
|
4
|
+
# if you're testing pry plugin you should require pry by yourself, no?
|
5
|
+
require 'pry' unless defined?(Pry)
|
6
|
+
|
7
|
+
class Pry
|
8
|
+
module Testable
|
9
|
+
require_relative "testable/pry_tester"
|
10
|
+
require_relative "testable/evalable"
|
11
|
+
require_relative "testable/mockable"
|
12
|
+
require_relative "testable/variables"
|
13
|
+
require_relative "testable/utility"
|
14
|
+
|
15
|
+
#
|
16
|
+
# When {Pry::Testable} is included into another module or class,
|
17
|
+
# the following modules are also included: {Pry::Testable::Mockable},
|
18
|
+
# {Pry::Testable::Evalable}, {Pry::Testable::Variables}, and
|
19
|
+
# {Pry::Testable::Utility}.
|
20
|
+
#
|
21
|
+
# @note
|
22
|
+
# Each of the included modules mentioned above may also be used
|
23
|
+
# standalone or in a pick-and-mix fashion.
|
24
|
+
#
|
25
|
+
# @param [Module] mod
|
26
|
+
# A class or module.
|
27
|
+
#
|
28
|
+
# @return [void]
|
29
|
+
#
|
30
|
+
def self.included(mod)
|
31
|
+
mod.module_eval do
|
32
|
+
include Pry::Testable::Mockable
|
33
|
+
include Pry::Testable::Evalable
|
34
|
+
include Pry::Testable::Variables
|
35
|
+
include Pry::Testable::Utility
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Sets various configuration options that make Pry optimal for a test
|
41
|
+
# environment, see source code for complete details.
|
42
|
+
#
|
43
|
+
# @return [void]
|
44
|
+
#
|
45
|
+
def self.set_testenv_variables
|
46
|
+
Pry.config = Pry::Config.new.merge(
|
47
|
+
color: false,
|
48
|
+
pager: false,
|
49
|
+
should_load_rc: false,
|
50
|
+
should_load_local_rc: false,
|
51
|
+
correct_indent: false,
|
52
|
+
collision_warning: false,
|
53
|
+
history_save: false,
|
54
|
+
history_load: false,
|
55
|
+
hooks: Pry::Hooks.new
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Reset the Pry configuration to their default values.
|
61
|
+
#
|
62
|
+
# @return [void]
|
63
|
+
#
|
64
|
+
def self.unset_testenv_variables
|
65
|
+
Pry.config = Pry::Config.new
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/pry/version.rb
CHANGED
data/lib/pry/warning.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Pry
|
4
|
+
# @api private
|
5
|
+
# @since v0.13.0
|
6
|
+
module Warning
|
7
|
+
# Prints a warning message with exact file and line location, similar to how
|
8
|
+
# Ruby's -W prints warnings.
|
9
|
+
#
|
10
|
+
# @param [String] message
|
11
|
+
# @return [void]
|
12
|
+
def self.warn(message)
|
13
|
+
location = caller_locations(2..2).first
|
14
|
+
path = location.path
|
15
|
+
lineno = location.lineno
|
16
|
+
|
17
|
+
Kernel.warn("#{path}:#{lineno}: warning: #{message}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,36 +1,31 @@
|
|
1
|
-
|
2
|
-
require 'forwardable'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
class Pry
|
5
4
|
class WrappedModule
|
6
|
-
|
7
5
|
# This class represents a single candidate for a module/class definition.
|
8
6
|
# It provides access to the source, documentation, line and file
|
9
7
|
# for a monkeypatch (reopening) of a class/module.
|
10
8
|
class Candidate
|
11
9
|
include Pry::Helpers::DocumentationHelpers
|
12
10
|
include Pry::CodeObject::Helpers
|
13
|
-
extend Forwardable
|
11
|
+
extend Pry::Forwardable
|
14
12
|
|
15
13
|
# @return [String] The file where the module definition is located.
|
16
14
|
attr_reader :file
|
17
|
-
|
15
|
+
alias source_file file
|
18
16
|
|
19
17
|
# @return [Fixnum] The line where the module definition is located.
|
20
18
|
attr_reader :line
|
21
|
-
|
19
|
+
alias source_line line
|
22
20
|
|
23
21
|
# Methods to delegate to associated `Pry::WrappedModule
|
24
22
|
# instance`.
|
25
|
-
private_delegates = [
|
26
|
-
|
27
|
-
|
28
|
-
public_delegates = [:wrapped, :module?, :class?, :name, :nonblank_name,
|
29
|
-
:number_of_candidates]
|
23
|
+
private_delegates = %i[lines_for_file method_candidates yard_docs? name]
|
24
|
+
public_delegates = %i[wrapped module? class? nonblank_name
|
25
|
+
number_of_candidates]
|
30
26
|
|
31
|
-
def_delegators :@wrapper, *
|
32
|
-
|
33
|
-
public(*public_delegates)
|
27
|
+
def_delegators :@wrapper, *public_delegates
|
28
|
+
def_private_delegators :@wrapper, *private_delegates
|
34
29
|
|
35
30
|
# @raise [Pry::CommandError] If `rank` is out of bounds.
|
36
31
|
# @param [Pry::WrappedModule] wrapper The associated
|
@@ -45,10 +40,15 @@ class Pry
|
|
45
40
|
|
46
41
|
if number_of_candidates <= 0
|
47
42
|
raise CommandError, "Cannot find a definition for #{name} module!"
|
48
|
-
elsif rank > (number_of_candidates - 1)
|
49
|
-
raise CommandError, "No such module candidate. Allowed candidates range is from 0 to #{number_of_candidates - 1}"
|
50
43
|
end
|
51
44
|
|
45
|
+
if rank > (number_of_candidates - 1)
|
46
|
+
raise CommandError,
|
47
|
+
"No such module candidate. Allowed candidates range is " \
|
48
|
+
"from 0 to #{number_of_candidates - 1}"
|
49
|
+
end
|
50
|
+
|
51
|
+
@source = @source_location = nil
|
52
52
|
@rank = rank
|
53
53
|
@file, @line = source_location
|
54
54
|
end
|
@@ -60,16 +60,17 @@ class Pry
|
|
60
60
|
return nil if file.nil?
|
61
61
|
return @source if @source
|
62
62
|
|
63
|
-
@source
|
63
|
+
@source ||= strip_leading_whitespace(
|
64
|
+
Pry::Code.from_file(file).expression_at(line, number_of_lines_in_first_chunk)
|
65
|
+
)
|
64
66
|
end
|
65
67
|
|
66
68
|
# @raise [Pry::CommandError] If documentation cannot be found.
|
67
69
|
# @return [String] The documentation for the candidate.
|
68
70
|
def doc
|
69
71
|
return nil if file.nil?
|
70
|
-
return @doc if @doc
|
71
72
|
|
72
|
-
@doc
|
73
|
+
@doc ||= get_comment_content(Pry::Code.from_file(file).comment_describing(line))
|
73
74
|
end
|
74
75
|
|
75
76
|
# @return [Array, nil] A `[String, Fixnum]` pair representing the
|
@@ -79,9 +80,9 @@ class Pry
|
|
79
80
|
return @source_location if @source_location
|
80
81
|
|
81
82
|
file, line = first_method_source_location
|
82
|
-
return nil
|
83
|
+
return nil unless file.is_a?(String)
|
83
84
|
|
84
|
-
@source_location = [file,
|
85
|
+
@source_location = [file, first_line_of_module_definition(file, line)]
|
85
86
|
rescue Pry::RescuableException
|
86
87
|
nil
|
87
88
|
end
|
@@ -97,14 +98,15 @@ class Pry
|
|
97
98
|
# line number is one-indexed.
|
98
99
|
def first_line_of_module_definition(file, line)
|
99
100
|
searchable_lines = lines_for_file(file)[0..(line - 2)]
|
100
|
-
searchable_lines.rindex { |v|
|
101
|
+
searchable_lines.rindex { |v| module_definition_first_line?(v) } + 1
|
101
102
|
end
|
102
103
|
|
103
|
-
def
|
104
|
+
def module_definition_first_line?(line)
|
104
105
|
mod_type_string = wrapped.class.to_s.downcase
|
105
|
-
|
106
|
-
|
107
|
-
|
106
|
+
wrapped_name_last = wrapped.name.split(/::/).last
|
107
|
+
/(^|=)\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped_name_last}/ =~ line ||
|
108
|
+
/^\s*(::)?#{wrapped_name_last}\s*?=\s*?#{wrapped.class}/ =~ line ||
|
109
|
+
/^\s*(::)?#{wrapped_name_last}\.(class|instance)_eval/ =~ line
|
108
110
|
end
|
109
111
|
|
110
112
|
# This method is used by `Candidate#source_location` as a
|
@@ -118,14 +120,15 @@ class Pry
|
|
118
120
|
# @return [Array] The source location of the last method in this
|
119
121
|
# candidate's module definition.
|
120
122
|
def last_method_source_location
|
121
|
-
@
|
123
|
+
@last_method_source_location ||= method_candidates[@rank].last.source_location
|
122
124
|
end
|
123
125
|
|
124
|
-
# Return the number of lines between the start of the class definition
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
126
|
+
# Return the number of lines between the start of the class definition and
|
127
|
+
# the start of the last method. We use this value so we can quickly grab
|
128
|
+
# these lines from the file (without having to check each intervening line
|
129
|
+
# for validity, which is expensive) speeding up source extraction.
|
130
|
+
#
|
131
|
+
# @return [Integer] number of lines.
|
129
132
|
def number_of_lines_in_first_chunk
|
130
133
|
end_method_line = last_method_source_location.last
|
131
134
|
|