pry 0.10.pre.1-i386-mingw32 → 0.10.0.pre3-i386-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +702 -0
- data/LICENSE +2 -2
- data/{README.markdown → README.md} +41 -35
- data/lib/pry.rb +82 -139
- data/lib/pry/cli.rb +77 -30
- data/lib/pry/code.rb +122 -183
- data/lib/pry/code/code_file.rb +103 -0
- data/lib/pry/code/code_range.rb +71 -0
- data/lib/pry/code/loc.rb +92 -0
- data/lib/pry/code_object.rb +172 -0
- data/lib/pry/color_printer.rb +55 -0
- data/lib/pry/command.rb +184 -28
- data/lib/pry/command_set.rb +113 -59
- data/lib/pry/commands.rb +4 -27
- data/lib/pry/commands/amend_line.rb +99 -0
- data/lib/pry/commands/bang.rb +20 -0
- data/lib/pry/commands/bang_pry.rb +17 -0
- data/lib/pry/commands/cat.rb +62 -0
- data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
- data/lib/pry/commands/cat/exception_formatter.rb +77 -0
- data/lib/pry/commands/cat/file_formatter.rb +67 -0
- data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
- data/lib/pry/commands/cd.rb +41 -0
- data/lib/pry/commands/change_inspector.rb +27 -0
- data/lib/pry/commands/change_prompt.rb +26 -0
- data/lib/pry/commands/code_collector.rb +165 -0
- data/lib/pry/commands/disable_pry.rb +27 -0
- data/lib/pry/commands/disabled_commands.rb +2 -0
- data/lib/pry/commands/easter_eggs.rb +112 -0
- data/lib/pry/commands/edit.rb +195 -0
- data/lib/pry/commands/edit/exception_patcher.rb +25 -0
- data/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
- data/lib/pry/commands/exit.rb +42 -0
- data/lib/pry/commands/exit_all.rb +29 -0
- data/lib/pry/commands/exit_program.rb +23 -0
- data/lib/pry/commands/find_method.rb +193 -0
- data/lib/pry/commands/fix_indent.rb +19 -0
- data/lib/pry/commands/gem_cd.rb +26 -0
- data/lib/pry/commands/gem_install.rb +32 -0
- data/lib/pry/commands/gem_list.rb +33 -0
- data/lib/pry/commands/gem_open.rb +29 -0
- data/lib/pry/commands/gist.rb +101 -0
- data/lib/pry/commands/help.rb +164 -0
- data/lib/pry/commands/hist.rb +180 -0
- data/lib/pry/commands/import_set.rb +22 -0
- data/lib/pry/commands/install_command.rb +53 -0
- data/lib/pry/commands/jump_to.rb +29 -0
- data/lib/pry/commands/list_inspectors.rb +35 -0
- data/lib/pry/commands/list_prompts.rb +35 -0
- data/lib/pry/commands/ls.rb +114 -0
- data/lib/pry/commands/ls/constants.rb +47 -0
- data/lib/pry/commands/ls/formatter.rb +49 -0
- data/lib/pry/commands/ls/globals.rb +48 -0
- data/lib/pry/commands/ls/grep.rb +21 -0
- data/lib/pry/commands/ls/instance_vars.rb +39 -0
- data/lib/pry/commands/ls/interrogatable.rb +18 -0
- data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
- data/lib/pry/commands/ls/local_names.rb +35 -0
- data/lib/pry/commands/ls/local_vars.rb +39 -0
- data/lib/pry/commands/ls/ls_entity.rb +70 -0
- data/lib/pry/commands/ls/methods.rb +57 -0
- data/lib/pry/commands/ls/methods_helper.rb +46 -0
- data/lib/pry/commands/ls/self_methods.rb +32 -0
- data/lib/pry/commands/nesting.rb +25 -0
- data/lib/pry/commands/play.rb +103 -0
- data/lib/pry/commands/pry_backtrace.rb +25 -0
- data/lib/pry/commands/pry_version.rb +17 -0
- data/lib/pry/commands/raise_up.rb +32 -0
- data/lib/pry/commands/reload_code.rb +62 -0
- data/lib/pry/commands/reset.rb +18 -0
- data/lib/pry/commands/ri.rb +60 -0
- data/lib/pry/commands/save_file.rb +61 -0
- data/lib/pry/commands/shell_command.rb +48 -0
- data/lib/pry/commands/shell_mode.rb +25 -0
- data/lib/pry/commands/show_doc.rb +83 -0
- data/lib/pry/commands/show_info.rb +195 -0
- data/lib/pry/commands/show_input.rb +17 -0
- data/lib/pry/commands/show_source.rb +50 -0
- data/lib/pry/commands/simple_prompt.rb +22 -0
- data/lib/pry/commands/stat.rb +40 -0
- data/lib/pry/commands/switch_to.rb +23 -0
- data/lib/pry/commands/toggle_color.rb +24 -0
- data/lib/pry/commands/watch_expression.rb +105 -0
- data/lib/pry/commands/watch_expression/expression.rb +38 -0
- data/lib/pry/commands/whereami.rb +190 -0
- data/lib/pry/commands/wtf.rb +57 -0
- data/lib/pry/config.rb +20 -229
- data/lib/pry/config/behavior.rb +139 -0
- data/lib/pry/config/convenience.rb +26 -0
- data/lib/pry/config/default.rb +165 -0
- data/lib/pry/core_extensions.rb +59 -38
- data/lib/pry/editor.rb +133 -0
- data/lib/pry/exceptions.rb +77 -0
- data/lib/pry/helpers.rb +1 -0
- data/lib/pry/helpers/base_helpers.rb +40 -154
- data/lib/pry/helpers/command_helpers.rb +19 -130
- data/lib/pry/helpers/documentation_helpers.rb +21 -11
- data/lib/pry/helpers/table.rb +109 -0
- data/lib/pry/helpers/text.rb +8 -9
- data/lib/pry/history.rb +61 -45
- data/lib/pry/history_array.rb +11 -1
- data/lib/pry/hooks.rb +10 -32
- data/lib/pry/indent.rb +110 -38
- data/lib/pry/input_completer.rb +242 -0
- data/lib/pry/input_lock.rb +132 -0
- data/lib/pry/inspector.rb +27 -0
- data/lib/pry/last_exception.rb +61 -0
- data/lib/pry/method.rb +199 -200
- data/lib/pry/method/disowned.rb +53 -0
- data/lib/pry/method/patcher.rb +125 -0
- data/lib/pry/method/weird_method_locator.rb +186 -0
- data/lib/pry/module_candidate.rb +39 -33
- data/lib/pry/object_path.rb +82 -0
- data/lib/pry/output.rb +50 -0
- data/lib/pry/pager.rb +234 -0
- data/lib/pry/plugins.rb +4 -3
- data/lib/pry/prompt.rb +26 -0
- data/lib/pry/pry_class.rb +199 -227
- data/lib/pry/pry_instance.rb +344 -403
- data/lib/pry/rbx_path.rb +1 -1
- data/lib/pry/repl.rb +202 -0
- data/lib/pry/repl_file_loader.rb +20 -26
- data/lib/pry/rubygem.rb +82 -0
- data/lib/pry/terminal.rb +79 -0
- data/lib/pry/test/helper.rb +170 -0
- data/lib/pry/version.rb +1 -1
- data/lib/pry/wrapped_module.rb +133 -48
- metadata +132 -197
- data/.document +0 -2
- data/.gemtest +0 -0
- data/.gitignore +0 -16
- data/.travis.yml +0 -17
- data/.yardopts +0 -1
- data/CHANGELOG +0 -387
- data/CONTRIBUTORS +0 -36
- data/Gemfile +0 -2
- data/Rakefile +0 -137
- data/TODO +0 -117
- data/examples/example_basic.rb +0 -15
- data/examples/example_command_override.rb +0 -32
- data/examples/example_commands.rb +0 -36
- data/examples/example_hooks.rb +0 -9
- data/examples/example_image_edit.rb +0 -67
- data/examples/example_input.rb +0 -7
- data/examples/example_input2.rb +0 -29
- data/examples/example_output.rb +0 -11
- data/examples/example_print.rb +0 -6
- data/examples/example_prompt.rb +0 -9
- data/examples/helper.rb +0 -6
- data/lib/pry/completion.rb +0 -221
- data/lib/pry/custom_completions.rb +0 -6
- data/lib/pry/default_commands/cd.rb +0 -81
- data/lib/pry/default_commands/commands.rb +0 -62
- data/lib/pry/default_commands/context.rb +0 -98
- data/lib/pry/default_commands/easter_eggs.rb +0 -95
- data/lib/pry/default_commands/editing.rb +0 -420
- data/lib/pry/default_commands/find_method.rb +0 -169
- data/lib/pry/default_commands/gems.rb +0 -84
- data/lib/pry/default_commands/gist.rb +0 -187
- data/lib/pry/default_commands/help.rb +0 -127
- data/lib/pry/default_commands/hist.rb +0 -120
- data/lib/pry/default_commands/input_and_output.rb +0 -306
- data/lib/pry/default_commands/introspection.rb +0 -410
- data/lib/pry/default_commands/ls.rb +0 -272
- data/lib/pry/default_commands/misc.rb +0 -38
- data/lib/pry/default_commands/navigating_pry.rb +0 -110
- data/lib/pry/default_commands/whereami.rb +0 -92
- data/lib/pry/extended_commands/experimental.rb +0 -7
- data/lib/pry/rbx_method.rb +0 -13
- data/man/pry.1 +0 -195
- data/man/pry.1.html +0 -204
- data/man/pry.1.ronn +0 -141
- data/pry.gemspec +0 -46
- data/test/candidate_helper1.rb +0 -11
- data/test/candidate_helper2.rb +0 -8
- data/test/helper.rb +0 -223
- data/test/test_cli.rb +0 -78
- data/test/test_code.rb +0 -201
- data/test/test_command.rb +0 -712
- data/test/test_command_helpers.rb +0 -9
- data/test/test_command_integration.rb +0 -668
- data/test/test_command_set.rb +0 -610
- data/test/test_completion.rb +0 -62
- data/test/test_control_d_handler.rb +0 -45
- data/test/test_default_commands/example.erb +0 -5
- data/test/test_default_commands/test_cd.rb +0 -318
- data/test/test_default_commands/test_context.rb +0 -280
- data/test/test_default_commands/test_documentation.rb +0 -314
- data/test/test_default_commands/test_find_method.rb +0 -50
- data/test/test_default_commands/test_gems.rb +0 -18
- data/test/test_default_commands/test_help.rb +0 -57
- data/test/test_default_commands/test_input.rb +0 -428
- data/test/test_default_commands/test_introspection.rb +0 -511
- data/test/test_default_commands/test_ls.rb +0 -151
- data/test/test_default_commands/test_shell.rb +0 -343
- data/test/test_default_commands/test_show_source.rb +0 -432
- data/test/test_exception_whitelist.rb +0 -21
- data/test/test_history_array.rb +0 -65
- data/test/test_hooks.rb +0 -521
- data/test/test_indent.rb +0 -277
- data/test/test_input_stack.rb +0 -86
- data/test/test_method.rb +0 -401
- data/test/test_pry.rb +0 -463
- data/test/test_pry_defaults.rb +0 -419
- data/test/test_pry_history.rb +0 -84
- data/test/test_pry_output.rb +0 -41
- data/test/test_sticky_locals.rb +0 -155
- data/test/test_syntax_checking.rb +0 -65
- data/test/test_wrapped_module.rb +0 -174
- data/test/testrc +0 -2
- data/test/testrcbad +0 -2
- data/wiki/Customizing-pry.md +0 -397
- data/wiki/Home.md +0 -4
data/lib/pry/cli.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
class Pry
|
3
2
|
|
4
3
|
# Manage the processing of command line options
|
@@ -11,7 +10,9 @@ class Pry
|
|
11
10
|
# @return [Proc] The Proc defining the valid command line options.
|
12
11
|
attr_accessor :options
|
13
12
|
|
14
|
-
# @return [Array] The Procs that process the parsed options.
|
13
|
+
# @return [Array] The Procs that process the parsed options. Plugins can
|
14
|
+
# utilize this facility in order to add and process their own Pry
|
15
|
+
# options.
|
15
16
|
attr_accessor :option_processors
|
16
17
|
|
17
18
|
# @return [Array<String>] The input array of strings to process
|
@@ -43,7 +44,7 @@ class Pry
|
|
43
44
|
end
|
44
45
|
|
45
46
|
# Add a block responsible for processing parsed options.
|
46
|
-
def
|
47
|
+
def add_option_processor(&block)
|
47
48
|
self.option_processors ||= []
|
48
49
|
option_processors << block
|
49
50
|
|
@@ -56,13 +57,31 @@ class Pry
|
|
56
57
|
self.option_processors = nil
|
57
58
|
end
|
58
59
|
|
59
|
-
def parse_options(args=ARGV
|
60
|
-
|
60
|
+
def parse_options(args=ARGV)
|
61
|
+
unless options
|
62
|
+
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
|
63
|
+
end
|
61
64
|
|
62
65
|
self.input_args = args
|
63
66
|
|
64
|
-
|
65
|
-
|
67
|
+
begin
|
68
|
+
opts = Slop.parse!(
|
69
|
+
args,
|
70
|
+
:help => true,
|
71
|
+
:multiple_switches => false,
|
72
|
+
:strict => true,
|
73
|
+
&options
|
74
|
+
)
|
75
|
+
rescue Slop::InvalidOptionError
|
76
|
+
# Display help message on unknown switches and exit.
|
77
|
+
puts Slop.new(&options)
|
78
|
+
exit
|
79
|
+
end
|
80
|
+
|
81
|
+
# Option processors are optional.
|
82
|
+
if option_processors
|
83
|
+
option_processors.each { |processor| processor.call(opts) }
|
84
|
+
end
|
66
85
|
|
67
86
|
self
|
68
87
|
end
|
@@ -73,18 +92,32 @@ class Pry
|
|
73
92
|
end
|
74
93
|
end
|
75
94
|
|
95
|
+
|
96
|
+
# String that is built to be executed on start (created by -e and -exec switches)
|
97
|
+
exec_string = ""
|
98
|
+
|
76
99
|
# Bring in options defined by plugins
|
77
|
-
|
100
|
+
Slop.new do
|
101
|
+
on "no-plugins" do
|
102
|
+
Pry.config.should_load_plugins = false
|
103
|
+
end
|
104
|
+
end.parse(ARGV.dup)
|
105
|
+
|
106
|
+
if Pry.config.should_load_plugins
|
107
|
+
Pry::CLI.add_plugin_options
|
108
|
+
end
|
78
109
|
|
79
110
|
# The default Pry command line options (before plugin options are included)
|
80
111
|
Pry::CLI.add_options do
|
81
112
|
banner %{Usage: pry [OPTIONS]
|
82
113
|
Start a Pry session.
|
83
|
-
See
|
84
|
-
Copyright (c)
|
114
|
+
See http://pryrepl.org/ for more information.
|
115
|
+
Copyright (c) 2013 John Mair (banisterfiend)
|
85
116
|
--
|
86
117
|
}
|
87
|
-
on :e, :exec
|
118
|
+
on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
|
119
|
+
exec_string << input << "\n"
|
120
|
+
end
|
88
121
|
|
89
122
|
on "no-pager", "Disable pager for long output" do
|
90
123
|
Pry.config.pager = false
|
@@ -95,20 +128,20 @@ Copyright (c) 2011 John Mair (banisterfiend)
|
|
95
128
|
end
|
96
129
|
|
97
130
|
on "no-color", "Disable syntax highlighting for session" do
|
98
|
-
Pry.color = false
|
131
|
+
Pry.config.color = false
|
99
132
|
end
|
100
133
|
|
101
|
-
on :f, "Suppress loading of ~/.pryrc" do
|
102
|
-
# load ~/.pryrc, if not suppressed with -f option
|
134
|
+
on :f, "Suppress loading of ~/.pryrc and ./.pryrc" do
|
103
135
|
Pry.config.should_load_rc = false
|
136
|
+
Pry.config.should_load_local_rc = false
|
104
137
|
end
|
105
138
|
|
106
|
-
on :s, "select-plugin", "Only load specified plugin (and no others)."
|
139
|
+
on :s, "select-plugin=", "Only load specified plugin (and no others)." do |plugin_name|
|
107
140
|
Pry.config.should_load_plugins = false
|
108
141
|
Pry.plugins[plugin_name].activate!
|
109
142
|
end
|
110
143
|
|
111
|
-
on :d, "disable-plugin", "Disable a specific plugin."
|
144
|
+
on :d, "disable-plugin=", "Disable a specific plugin." do |plugin_name|
|
112
145
|
Pry.plugins[plugin_name].disable!
|
113
146
|
end
|
114
147
|
|
@@ -116,11 +149,11 @@ Copyright (c) 2011 John Mair (banisterfiend)
|
|
116
149
|
Pry.config.should_load_plugins = false
|
117
150
|
end
|
118
151
|
|
119
|
-
on "
|
152
|
+
on "plugins", "List installed plugins." do
|
120
153
|
puts "Installed Plugins:"
|
121
154
|
puts "--"
|
122
155
|
Pry.locate_plugins.each do |plugin|
|
123
|
-
puts "#{plugin.name}".ljust(18)
|
156
|
+
puts "#{plugin.name}".ljust(18) << plugin.spec.summary
|
124
157
|
end
|
125
158
|
exit
|
126
159
|
end
|
@@ -129,12 +162,27 @@ Copyright (c) 2011 John Mair (banisterfiend)
|
|
129
162
|
Pry.config.prompt = Pry::SIMPLE_PROMPT
|
130
163
|
end
|
131
164
|
|
132
|
-
on
|
165
|
+
on "noprompt", "No prompt mode" do
|
166
|
+
Pry.config.prompt = Pry::NO_PROMPT
|
167
|
+
end
|
168
|
+
|
169
|
+
on :r, :require=, "`require` a Ruby script at startup" do |file|
|
133
170
|
Pry.config.requires << file
|
134
171
|
end
|
135
172
|
|
136
|
-
on :I
|
137
|
-
|
173
|
+
on :I=, "Add a path to the $LOAD_PATH", :as => Array, :delimiter => ":" do |load_path|
|
174
|
+
load_path.map! do |path|
|
175
|
+
/\A\.\// =~ path ? path : File.expand_path(path)
|
176
|
+
end
|
177
|
+
|
178
|
+
$LOAD_PATH.unshift(*load_path)
|
179
|
+
end
|
180
|
+
|
181
|
+
on "gem", "Shorthand for -I./lib -rgemname" do |load_path|
|
182
|
+
$LOAD_PATH.unshift("./lib")
|
183
|
+
Dir["./lib/*.rb"].each do |file|
|
184
|
+
Pry.config.requires << file
|
185
|
+
end
|
138
186
|
end
|
139
187
|
|
140
188
|
on :v, :version, "Display the Pry version" do
|
@@ -142,12 +190,11 @@ Copyright (c) 2011 John Mair (banisterfiend)
|
|
142
190
|
exit
|
143
191
|
end
|
144
192
|
|
145
|
-
on(:c, :context
|
193
|
+
on(:c, :context=,
|
146
194
|
"Start the session in the specified context. Equivalent to `context.pry` in a session.",
|
147
|
-
:argument => true,
|
148
195
|
:default => "Pry.toplevel_binding"
|
149
196
|
)
|
150
|
-
end.
|
197
|
+
end.add_option_processor do |opts|
|
151
198
|
|
152
199
|
exit if opts.help?
|
153
200
|
|
@@ -155,19 +202,19 @@ end.process_options do |opts|
|
|
155
202
|
Pry.cli = true
|
156
203
|
|
157
204
|
# create the actual context
|
158
|
-
|
205
|
+
if opts[:context]
|
206
|
+
Pry.initial_session_setup
|
207
|
+
context = Pry.binding_for(eval(opts[:context]))
|
208
|
+
else
|
209
|
+
context = Pry.toplevel_binding
|
210
|
+
end
|
159
211
|
|
160
212
|
if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
|
161
213
|
full_name = File.expand_path(Pry::CLI.input_args.first)
|
162
214
|
Pry.load_file_through_repl(full_name)
|
163
215
|
exit
|
164
|
-
elsif opts[:exec]
|
165
|
-
exec_string = opts[:exec] + "\n"
|
166
|
-
else
|
167
|
-
exec_string = ""
|
168
216
|
end
|
169
217
|
|
170
218
|
# Start the session (running any code passed with -e, if there is any)
|
171
219
|
Pry.start(context, :input => StringIO.new(exec_string))
|
172
220
|
end
|
173
|
-
|
data/lib/pry/code.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'pry/code/loc'
|
2
|
+
require 'pry/code/code_range'
|
3
|
+
require 'pry/code/code_file'
|
4
|
+
|
1
5
|
class Pry
|
2
6
|
class << self
|
3
7
|
# Convert the given object into an instance of `Pry::Code`, if it isn't
|
@@ -32,23 +36,12 @@ class Pry
|
|
32
36
|
# Instantiate a `Code` object containing code loaded from a file or
|
33
37
|
# Pry's line buffer.
|
34
38
|
#
|
35
|
-
# @param [String]
|
39
|
+
# @param [String] filename The name of a file, or "(pry)".
|
36
40
|
# @param [Symbol] code_type The type of code the file contains.
|
37
41
|
# @return [Code]
|
38
|
-
def from_file(
|
39
|
-
|
40
|
-
|
41
|
-
else
|
42
|
-
if File.readable?(fn)
|
43
|
-
f = File.open(fn, 'r')
|
44
|
-
code_type = type_from_filename(fn)
|
45
|
-
else
|
46
|
-
raise MethodSource::SourceNotFoundError, "Cannot open #{fn.inspect} for reading."
|
47
|
-
end
|
48
|
-
end
|
49
|
-
new(f, 1, code_type || :ruby)
|
50
|
-
ensure
|
51
|
-
f.close if f.respond_to?(:close)
|
42
|
+
def from_file(filename, code_type = nil)
|
43
|
+
code_file = CodeFile.new(filename, code_type)
|
44
|
+
new(code_file.code, 1, code_file.code_type)
|
52
45
|
end
|
53
46
|
|
54
47
|
# Instantiate a `Code` object containing code extracted from a
|
@@ -56,10 +49,10 @@ class Pry
|
|
56
49
|
#
|
57
50
|
# @param [::Method, UnboundMethod, Proc, Pry::Method] meth The method
|
58
51
|
# object.
|
59
|
-
# @param [
|
52
|
+
# @param [Integer, nil] start_line The line number to start on, or nil to
|
60
53
|
# use the method's original line numbers.
|
61
54
|
# @return [Code]
|
62
|
-
def from_method(meth, start_line=nil)
|
55
|
+
def from_method(meth, start_line = nil)
|
63
56
|
meth = Pry::Method(meth)
|
64
57
|
start_line ||= meth.source_line || 1
|
65
58
|
new(meth.source, start_line, meth.source_type)
|
@@ -68,48 +61,16 @@ class Pry
|
|
68
61
|
# Attempt to extract the source code for module (or class) `mod`.
|
69
62
|
#
|
70
63
|
# @param [Module, Class] mod The module (or class) of interest.
|
71
|
-
# @param [
|
72
|
-
# method's original line numbers.
|
73
|
-
# @param [Fixnum] candidate_rank The module candidate (by rank)
|
64
|
+
# @param [Integer] candidate_rank The module candidate (by rank)
|
74
65
|
# to use (see `Pry::WrappedModule::Candidate` for more information).
|
66
|
+
# @param [Integer, nil] start_line The line number to start on, or nil to
|
67
|
+
# use the method's original line numbers.
|
75
68
|
# @return [Code]
|
76
|
-
def from_module(mod,
|
69
|
+
def from_module(mod, candidate_rank = 0, start_line=nil)
|
77
70
|
candidate = Pry::WrappedModule(mod).candidate(candidate_rank)
|
78
|
-
|
79
71
|
start_line ||= candidate.line
|
80
72
|
new(candidate.source, start_line, :ruby)
|
81
73
|
end
|
82
|
-
|
83
|
-
protected
|
84
|
-
# Guess the CodeRay type of a file from its extension, or nil if
|
85
|
-
# unknown.
|
86
|
-
#
|
87
|
-
# @param [String] filename
|
88
|
-
# @return [Symbol, nil]
|
89
|
-
def type_from_filename(filename)
|
90
|
-
map = {
|
91
|
-
%w(.c .h) => :c,
|
92
|
-
%w(.cpp .hpp .cc .h cxx) => :cpp,
|
93
|
-
%w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
|
94
|
-
%w(.py) => :python,
|
95
|
-
%w(.diff) => :diff,
|
96
|
-
%w(.css) => :css,
|
97
|
-
%w(.html) => :html,
|
98
|
-
%w(.yaml .yml) => :yaml,
|
99
|
-
%w(.xml) => :xml,
|
100
|
-
%w(.php) => :php,
|
101
|
-
%w(.js) => :javascript,
|
102
|
-
%w(.java) => :java,
|
103
|
-
%w(.rhtml) => :rhtml,
|
104
|
-
%w(.json) => :json
|
105
|
-
}
|
106
|
-
|
107
|
-
_, type = map.find do |k, _|
|
108
|
-
k.any? { |ext| ext == File.extname(filename) }
|
109
|
-
end
|
110
|
-
|
111
|
-
type
|
112
|
-
end
|
113
74
|
end
|
114
75
|
|
115
76
|
# @return [Symbol] The type of code stored in this wrapper.
|
@@ -121,37 +82,39 @@ class Pry
|
|
121
82
|
# empty `Code` object and then use `#push` to insert the lines.
|
122
83
|
#
|
123
84
|
# @param [Array<String>, String, IO] lines
|
124
|
-
# @param [
|
85
|
+
# @param [Integer?] start_line
|
125
86
|
# @param [Symbol?] code_type
|
126
|
-
def initialize(lines=[], start_line=1, code_type
|
87
|
+
def initialize(lines = [], start_line = 1, code_type = :ruby)
|
127
88
|
if lines.is_a? String
|
128
89
|
lines = lines.lines
|
129
90
|
end
|
130
|
-
|
131
|
-
|
91
|
+
@lines = lines.each_with_index.map { |line, lineno|
|
92
|
+
LOC.new(line, lineno + start_line.to_i) }
|
132
93
|
@code_type = code_type
|
133
94
|
end
|
134
95
|
|
135
|
-
# Append the given line.
|
96
|
+
# Append the given line. +lineno+ is one more than the last existing
|
136
97
|
# line, unless specified otherwise.
|
137
98
|
#
|
138
99
|
# @param [String] line
|
139
|
-
# @param [
|
100
|
+
# @param [Integer?] lineno
|
140
101
|
# @return [String] The inserted line.
|
141
|
-
def push(line,
|
142
|
-
|
143
|
-
|
102
|
+
def push(line, lineno = nil)
|
103
|
+
if lineno.nil?
|
104
|
+
lineno = @lines.last.lineno + 1
|
105
|
+
end
|
106
|
+
@lines.push(LOC.new(line, lineno))
|
144
107
|
line
|
145
108
|
end
|
146
109
|
alias << push
|
147
110
|
|
148
111
|
# Filter the lines using the given block.
|
149
112
|
#
|
150
|
-
# @yield [
|
113
|
+
# @yield [LOC]
|
151
114
|
# @return [Code]
|
152
|
-
def select(&
|
115
|
+
def select(&block)
|
153
116
|
alter do
|
154
|
-
@lines = @lines.select(&
|
117
|
+
@lines = @lines.select(&block)
|
155
118
|
end
|
156
119
|
end
|
157
120
|
|
@@ -159,92 +122,74 @@ class Pry
|
|
159
122
|
# `Range` object or a first and last line number (inclusive). Negative
|
160
123
|
# indices count from the end of the array of lines.
|
161
124
|
#
|
162
|
-
# @param [Range,
|
163
|
-
# @param [
|
125
|
+
# @param [Range, Integer] start_line
|
126
|
+
# @param [Integer?] end_line
|
164
127
|
# @return [Code]
|
165
|
-
def between(start_line, end_line=nil)
|
128
|
+
def between(start_line, end_line = nil)
|
166
129
|
return self unless start_line
|
167
130
|
|
168
|
-
|
169
|
-
end_line = start_line.last
|
170
|
-
end_line -= 1 if start_line.exclude_end?
|
171
|
-
|
172
|
-
start_line = start_line.first
|
173
|
-
else
|
174
|
-
end_line ||= start_line
|
175
|
-
end
|
176
|
-
|
177
|
-
if start_line > 0
|
178
|
-
start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
|
179
|
-
else
|
180
|
-
start_idx = start_line
|
181
|
-
end
|
182
|
-
|
183
|
-
if end_line > 0
|
184
|
-
end_idx = (@lines.index { |l| l.last > end_line } || 0) - 1
|
185
|
-
else
|
186
|
-
end_idx = end_line
|
187
|
-
end
|
131
|
+
code_range = CodeRange.new(start_line, end_line)
|
188
132
|
|
189
133
|
alter do
|
190
|
-
@lines = @lines[
|
134
|
+
@lines = @lines[code_range.indices_range(@lines)] || []
|
191
135
|
end
|
192
136
|
end
|
193
137
|
|
194
|
-
# Take `num_lines` from `start_line`, forward or backwards
|
138
|
+
# Take `num_lines` from `start_line`, forward or backwards.
|
195
139
|
#
|
196
|
-
# @param [
|
197
|
-
# @param [
|
140
|
+
# @param [Integer] start_line
|
141
|
+
# @param [Integer] num_lines
|
198
142
|
# @return [Code]
|
199
143
|
def take_lines(start_line, num_lines)
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
144
|
+
start_idx =
|
145
|
+
if start_line >= 0
|
146
|
+
@lines.index { |loc| loc.lineno >= start_line } || @lines.length
|
147
|
+
else
|
148
|
+
[@lines.length + start_line, 0].max
|
149
|
+
end
|
205
150
|
|
206
151
|
alter do
|
207
152
|
@lines = @lines.slice(start_idx, num_lines)
|
208
153
|
end
|
209
154
|
end
|
210
155
|
|
211
|
-
# Remove all lines except for the
|
156
|
+
# Remove all lines except for the +lines+ up to and excluding +lineno+.
|
212
157
|
#
|
213
|
-
# @param [
|
214
|
-
# @param [
|
158
|
+
# @param [Integer] lineno
|
159
|
+
# @param [Integer] lines
|
215
160
|
# @return [Code]
|
216
|
-
def before(
|
217
|
-
return self unless
|
161
|
+
def before(lineno, lines = 1)
|
162
|
+
return self unless lineno
|
218
163
|
|
219
|
-
select do |
|
220
|
-
|
164
|
+
select do |loc|
|
165
|
+
loc.lineno >= lineno - lines && loc.lineno < lineno
|
221
166
|
end
|
222
167
|
end
|
223
168
|
|
224
|
-
# Remove all lines except for the
|
225
|
-
#
|
169
|
+
# Remove all lines except for the +lines+ on either side of and including
|
170
|
+
# +lineno+.
|
226
171
|
#
|
227
|
-
# @param [
|
228
|
-
# @param [
|
172
|
+
# @param [Integer] lineno
|
173
|
+
# @param [Integer] lines
|
229
174
|
# @return [Code]
|
230
|
-
def around(
|
231
|
-
return self unless
|
175
|
+
def around(lineno, lines = 1)
|
176
|
+
return self unless lineno
|
232
177
|
|
233
|
-
select do |
|
234
|
-
|
178
|
+
select do |loc|
|
179
|
+
loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
|
235
180
|
end
|
236
181
|
end
|
237
182
|
|
238
|
-
# Remove all lines except for the
|
183
|
+
# Remove all lines except for the +lines+ after and excluding +lineno+.
|
239
184
|
#
|
240
|
-
# @param [
|
241
|
-
# @param [
|
185
|
+
# @param [Integer] lineno
|
186
|
+
# @param [Integer] lines
|
242
187
|
# @return [Code]
|
243
|
-
def after(
|
244
|
-
return self unless
|
188
|
+
def after(lineno, lines = 1)
|
189
|
+
return self unless lineno
|
245
190
|
|
246
|
-
select do |
|
247
|
-
|
191
|
+
select do |loc|
|
192
|
+
loc.lineno > lineno && loc.lineno <= lineno + lines
|
248
193
|
end
|
249
194
|
end
|
250
195
|
|
@@ -256,8 +201,8 @@ class Pry
|
|
256
201
|
return self unless pattern
|
257
202
|
pattern = Regexp.new(pattern)
|
258
203
|
|
259
|
-
select do |
|
260
|
-
|
204
|
+
select do |loc|
|
205
|
+
loc.line =~ pattern
|
261
206
|
end
|
262
207
|
end
|
263
208
|
|
@@ -265,30 +210,30 @@ class Pry
|
|
265
210
|
#
|
266
211
|
# @param [Boolean?] y_n
|
267
212
|
# @return [Code]
|
268
|
-
def with_line_numbers(y_n=true)
|
213
|
+
def with_line_numbers(y_n = true)
|
269
214
|
alter do
|
270
215
|
@with_line_numbers = y_n
|
271
216
|
end
|
272
217
|
end
|
273
218
|
|
274
|
-
# Format output with a marker next to the given
|
275
|
-
#
|
219
|
+
# Format output with a marker next to the given +lineno+, unless +lineno+ is
|
220
|
+
# falsy.
|
276
221
|
#
|
277
|
-
# @param [
|
222
|
+
# @param [Integer?] lineno
|
278
223
|
# @return [Code]
|
279
|
-
def with_marker(
|
224
|
+
def with_marker(lineno = 1)
|
280
225
|
alter do
|
281
|
-
@with_marker
|
282
|
-
@
|
226
|
+
@with_marker = !!lineno
|
227
|
+
@marker_lineno = lineno
|
283
228
|
end
|
284
229
|
end
|
285
230
|
|
286
231
|
# Format output with the specified number of spaces in front of every line,
|
287
232
|
# unless `spaces` is falsy.
|
288
233
|
#
|
289
|
-
# @param [
|
234
|
+
# @param [Integer?] spaces
|
290
235
|
# @return [Code]
|
291
|
-
def with_indentation(spaces=0)
|
236
|
+
def with_indentation(spaces = 0)
|
292
237
|
alter do
|
293
238
|
@with_indentation = !!spaces
|
294
239
|
@indentation_num = spaces
|
@@ -300,72 +245,67 @@ class Pry
|
|
300
245
|
Object.instance_method(:to_s).bind(self).call
|
301
246
|
end
|
302
247
|
|
303
|
-
#
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
def to_s
|
308
|
-
lines = @lines.map(&:dup)
|
309
|
-
|
310
|
-
if Pry.color
|
311
|
-
lines.each do |l|
|
312
|
-
l[0] = CodeRay.scan(l[0], @code_type).term
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
if @with_line_numbers
|
317
|
-
max_width = lines.last.last.to_s.length if lines.length > 0
|
318
|
-
lines.each do |l|
|
319
|
-
padded_line_num = l[1].to_s.rjust(max_width)
|
320
|
-
l[0] = "#{Pry::Helpers::BaseHelpers.colorize_code(padded_line_num.to_s)}: #{l[0]}"
|
321
|
-
end
|
322
|
-
end
|
248
|
+
# @return [Integer] the number of digits in the last line.
|
249
|
+
def max_lineno_width
|
250
|
+
@lines.length > 0 ? @lines.last.lineno.to_s.length : 0
|
251
|
+
end
|
323
252
|
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
l[0] = " #{l[0]}"
|
330
|
-
end
|
331
|
-
end
|
332
|
-
end
|
253
|
+
# @return [String] a formatted representation (based on the configuration of
|
254
|
+
# the object).
|
255
|
+
def to_s
|
256
|
+
print_to_output("")
|
257
|
+
end
|
333
258
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
259
|
+
# Writes a formatted representation (based on the configuration of the
|
260
|
+
# object) to the given output, which must respond to `#<<`.
|
261
|
+
def print_to_output(output, color=false)
|
262
|
+
@lines.each do |loc|
|
263
|
+
loc = loc.dup
|
264
|
+
loc.colorize(@code_type)
|
265
|
+
loc.add_line_number(max_lineno_width, color) if @with_line_numbers
|
266
|
+
loc.add_marker(@marker_lineno) if @with_marker
|
267
|
+
loc.indent(@indentation_num) if @with_indentation
|
268
|
+
output << loc.line
|
269
|
+
output << "\n"
|
338
270
|
end
|
339
|
-
|
340
|
-
lines.map { |l| "#{l.first}\n" }.join
|
271
|
+
output
|
341
272
|
end
|
342
273
|
|
343
274
|
# Get the comment that describes the expression on the given line number.
|
344
275
|
#
|
345
|
-
# @param [
|
346
|
-
# @return [String]
|
276
|
+
# @param [Integer] line_number (1-based)
|
277
|
+
# @return [String] the code.
|
347
278
|
def comment_describing(line_number)
|
348
279
|
self.class.comment_describing(raw, line_number)
|
349
280
|
end
|
350
281
|
|
351
282
|
# Get the multiline expression that starts on the given line number.
|
352
283
|
#
|
353
|
-
# @param [
|
354
|
-
# @return [String]
|
355
|
-
def expression_at(line_number, consume=0)
|
284
|
+
# @param [Integer] line_number (1-based)
|
285
|
+
# @return [String] the code.
|
286
|
+
def expression_at(line_number, consume = 0)
|
356
287
|
self.class.expression_at(raw, line_number, :consume => consume)
|
357
288
|
end
|
358
289
|
|
290
|
+
# Get the (approximate) Module.nesting at the give line number.
|
291
|
+
#
|
292
|
+
# @param [Integer] line_number line number starting from 1
|
293
|
+
# @param [Module] top_module the module in which this code exists
|
294
|
+
# @return [Array<Module>] a list of open modules.
|
295
|
+
def nesting_at(line_number, top_module = Object)
|
296
|
+
Pry::Indent.nesting_at(raw, line_number)
|
297
|
+
end
|
298
|
+
|
359
299
|
# Return an unformatted String of the code.
|
360
300
|
#
|
361
301
|
# @return [String]
|
362
302
|
def raw
|
363
|
-
@lines.map(&:
|
303
|
+
@lines.map(&:line).join("\n") << "\n"
|
364
304
|
end
|
365
305
|
|
366
306
|
# Return the number of lines stored.
|
367
307
|
#
|
368
|
-
# @return [
|
308
|
+
# @return [Integer]
|
369
309
|
def length
|
370
310
|
@lines ? @lines.length : 0
|
371
311
|
end
|
@@ -377,26 +317,25 @@ class Pry
|
|
377
317
|
# @return [Boolean]
|
378
318
|
def ==(other)
|
379
319
|
if other.is_a?(Code)
|
380
|
-
|
381
|
-
@lines.each_with_index.all?
|
382
|
-
l == @other_lines[i].first && ln == @other_lines[i].last
|
383
|
-
end
|
320
|
+
other_lines = other.instance_variable_get(:@lines)
|
321
|
+
@lines.each_with_index.all? { |loc, i| loc == other_lines[i] }
|
384
322
|
else
|
385
323
|
to_s.chomp == other.to_s.chomp
|
386
324
|
end
|
387
325
|
end
|
388
326
|
|
389
327
|
# Forward any missing methods to the output of `#to_s`.
|
390
|
-
def method_missing(name, *args, &
|
391
|
-
to_s.send(name, *args, &
|
328
|
+
def method_missing(name, *args, &block)
|
329
|
+
to_s.send(name, *args, &block)
|
392
330
|
end
|
393
331
|
undef =~
|
394
332
|
|
395
333
|
protected
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
334
|
+
|
335
|
+
# An abstraction of the `dup.instance_eval` pattern used throughout this
|
336
|
+
# class.
|
337
|
+
def alter(&block)
|
338
|
+
dup.tap { |o| o.instance_eval(&block) }
|
339
|
+
end
|
401
340
|
end
|
402
341
|
end
|