pry 0.9.10pre1-java → 0.9.11-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG +63 -2
  3. data/CONTRIBUTORS +43 -25
  4. data/Gemfile +7 -0
  5. data/Guardfile +62 -0
  6. data/README.markdown +4 -4
  7. data/Rakefile +34 -35
  8. data/lib/pry.rb +107 -54
  9. data/lib/pry/cli.rb +34 -11
  10. data/lib/pry/code.rb +165 -182
  11. data/lib/pry/code/code_range.rb +70 -0
  12. data/lib/pry/code/loc.rb +92 -0
  13. data/lib/pry/code_object.rb +153 -0
  14. data/lib/pry/command.rb +160 -22
  15. data/lib/pry/command_set.rb +37 -26
  16. data/lib/pry/commands.rb +4 -27
  17. data/lib/pry/commands/amend_line.rb +99 -0
  18. data/lib/pry/commands/bang.rb +20 -0
  19. data/lib/pry/commands/bang_pry.rb +17 -0
  20. data/lib/pry/commands/cat.rb +53 -0
  21. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  22. data/lib/pry/commands/cat/exception_formatter.rb +78 -0
  23. data/lib/pry/commands/cat/file_formatter.rb +84 -0
  24. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  25. data/lib/pry/commands/cd.rb +30 -0
  26. data/lib/pry/commands/code_collector.rb +165 -0
  27. data/lib/pry/commands/deprecated_commands.rb +2 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/easter_eggs.rb +112 -0
  30. data/lib/pry/commands/edit.rb +206 -0
  31. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  32. data/lib/pry/commands/edit/file_and_line_locator.rb +38 -0
  33. data/lib/pry/commands/edit/method_patcher.rb +122 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +24 -0
  37. data/lib/pry/commands/find_method.rb +199 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +29 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +95 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +161 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +51 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/ls.rb +339 -0
  50. data/lib/pry/commands/nesting.rb +25 -0
  51. data/lib/pry/commands/play.rb +69 -0
  52. data/lib/pry/commands/pry_backtrace.rb +26 -0
  53. data/lib/pry/commands/pry_version.rb +17 -0
  54. data/lib/pry/commands/raise_up.rb +32 -0
  55. data/lib/pry/commands/reload_code.rb +39 -0
  56. data/lib/pry/commands/reset.rb +18 -0
  57. data/lib/pry/commands/ri.rb +56 -0
  58. data/lib/pry/commands/save_file.rb +61 -0
  59. data/lib/pry/commands/shell_command.rb +43 -0
  60. data/lib/pry/commands/shell_mode.rb +27 -0
  61. data/lib/pry/commands/show_doc.rb +78 -0
  62. data/lib/pry/commands/show_info.rb +139 -0
  63. data/lib/pry/commands/show_input.rb +17 -0
  64. data/lib/pry/commands/show_source.rb +37 -0
  65. data/lib/pry/commands/simple_prompt.rb +22 -0
  66. data/lib/pry/commands/stat.rb +40 -0
  67. data/lib/pry/commands/switch_to.rb +23 -0
  68. data/lib/pry/commands/toggle_color.rb +20 -0
  69. data/lib/pry/commands/whereami.rb +114 -0
  70. data/lib/pry/commands/wtf.rb +57 -0
  71. data/lib/pry/completion.rb +120 -46
  72. data/lib/pry/config.rb +11 -0
  73. data/lib/pry/core_extensions.rb +30 -19
  74. data/lib/pry/editor.rb +129 -0
  75. data/lib/pry/helpers.rb +1 -0
  76. data/lib/pry/helpers/base_helpers.rb +89 -119
  77. data/lib/pry/helpers/command_helpers.rb +7 -122
  78. data/lib/pry/helpers/table.rb +100 -0
  79. data/lib/pry/helpers/text.rb +4 -4
  80. data/lib/pry/history_array.rb +5 -0
  81. data/lib/pry/hooks.rb +1 -3
  82. data/lib/pry/indent.rb +104 -30
  83. data/lib/pry/method.rb +66 -22
  84. data/lib/pry/module_candidate.rb +26 -15
  85. data/lib/pry/pager.rb +70 -0
  86. data/lib/pry/plugins.rb +1 -2
  87. data/lib/pry/pry_class.rb +63 -22
  88. data/lib/pry/pry_instance.rb +58 -37
  89. data/lib/pry/rubygem.rb +74 -0
  90. data/lib/pry/terminal_info.rb +43 -0
  91. data/lib/pry/test/helper.rb +185 -0
  92. data/lib/pry/version.rb +1 -1
  93. data/lib/pry/wrapped_module.rb +58 -24
  94. data/pry.gemspec +21 -37
  95. data/{test/test_cli.rb → spec/cli_spec.rb} +0 -0
  96. data/spec/code_object_spec.rb +277 -0
  97. data/{test/test_code.rb → spec/code_spec.rb} +19 -1
  98. data/{test/test_command_helpers.rb → spec/command_helpers_spec.rb} +0 -0
  99. data/{test/test_command_integration.rb → spec/command_integration_spec.rb} +38 -46
  100. data/{test/test_command_set.rb → spec/command_set_spec.rb} +18 -1
  101. data/{test/test_command.rb → spec/command_spec.rb} +250 -149
  102. data/spec/commands/amend_line_spec.rb +247 -0
  103. data/spec/commands/bang_spec.rb +19 -0
  104. data/spec/commands/cat_spec.rb +164 -0
  105. data/spec/commands/cd_spec.rb +250 -0
  106. data/spec/commands/disable_pry_spec.rb +25 -0
  107. data/spec/commands/edit_spec.rb +727 -0
  108. data/spec/commands/exit_all_spec.rb +34 -0
  109. data/spec/commands/exit_program_spec.rb +19 -0
  110. data/spec/commands/exit_spec.rb +34 -0
  111. data/{test/test_default_commands/test_find_method.rb → spec/commands/find_method_spec.rb} +27 -7
  112. data/spec/commands/gem_list_spec.rb +26 -0
  113. data/spec/commands/gist_spec.rb +75 -0
  114. data/{test/test_default_commands/test_help.rb → spec/commands/help_spec.rb} +8 -9
  115. data/spec/commands/hist_spec.rb +181 -0
  116. data/spec/commands/jump_to_spec.rb +15 -0
  117. data/spec/commands/ls_spec.rb +177 -0
  118. data/spec/commands/play_spec.rb +140 -0
  119. data/spec/commands/raise_up_spec.rb +56 -0
  120. data/spec/commands/save_file_spec.rb +177 -0
  121. data/spec/commands/show_doc_spec.rb +378 -0
  122. data/spec/commands/show_input_spec.rb +17 -0
  123. data/spec/commands/show_source_spec.rb +597 -0
  124. data/spec/commands/whereami_spec.rb +154 -0
  125. data/spec/completion_spec.rb +233 -0
  126. data/spec/control_d_handler_spec.rb +58 -0
  127. data/spec/editor_spec.rb +79 -0
  128. data/{test/test_exception_whitelist.rb → spec/exception_whitelist_spec.rb} +0 -0
  129. data/{test → spec/fixtures}/candidate_helper1.rb +0 -0
  130. data/{test → spec/fixtures}/candidate_helper2.rb +0 -0
  131. data/{test/test_default_commands → spec/fixtures}/example.erb +0 -0
  132. data/spec/fixtures/example_nesting.rb +33 -0
  133. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  134. data/{test → spec/fixtures}/testrc +0 -0
  135. data/{test → spec/fixtures}/testrcbad +0 -0
  136. data/spec/helper.rb +34 -0
  137. data/spec/helpers/bacon.rb +86 -0
  138. data/spec/helpers/mock_pry.rb +43 -0
  139. data/spec/helpers/table_spec.rb +83 -0
  140. data/{test/test_history_array.rb → spec/history_array_spec.rb} +21 -19
  141. data/{test/test_hooks.rb → spec/hooks_spec.rb} +0 -0
  142. data/{test/test_indent.rb → spec/indent_spec.rb} +24 -0
  143. data/{test/test_input_stack.rb → spec/input_stack_spec.rb} +4 -0
  144. data/{test/test_method.rb → spec/method_spec.rb} +65 -1
  145. data/{test/test_prompt.rb → spec/prompt_spec.rb} +0 -0
  146. data/{test/test_pry_defaults.rb → spec/pry_defaults_spec.rb} +14 -14
  147. data/{test/test_pry_history.rb → spec/pry_history_spec.rb} +15 -0
  148. data/spec/pry_output_spec.rb +95 -0
  149. data/{test/test_pry.rb → spec/pry_spec.rb} +74 -32
  150. data/{test/test_sticky_locals.rb → spec/sticky_locals_spec.rb} +27 -25
  151. data/{test/test_syntax_checking.rb → spec/syntax_checking_spec.rb} +17 -1
  152. data/{test/test_wrapped_module.rb → spec/wrapped_module_spec.rb} +92 -5
  153. metadata +239 -115
  154. data/examples/example_basic.rb +0 -15
  155. data/examples/example_command_override.rb +0 -32
  156. data/examples/example_commands.rb +0 -36
  157. data/examples/example_hooks.rb +0 -9
  158. data/examples/example_image_edit.rb +0 -67
  159. data/examples/example_input.rb +0 -7
  160. data/examples/example_input2.rb +0 -29
  161. data/examples/example_output.rb +0 -11
  162. data/examples/example_print.rb +0 -6
  163. data/examples/example_prompt.rb +0 -9
  164. data/examples/helper.rb +0 -6
  165. data/lib/pry/default_commands/cd.rb +0 -81
  166. data/lib/pry/default_commands/commands.rb +0 -62
  167. data/lib/pry/default_commands/context.rb +0 -98
  168. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  169. data/lib/pry/default_commands/editing.rb +0 -420
  170. data/lib/pry/default_commands/find_method.rb +0 -169
  171. data/lib/pry/default_commands/gems.rb +0 -84
  172. data/lib/pry/default_commands/gist.rb +0 -187
  173. data/lib/pry/default_commands/help.rb +0 -127
  174. data/lib/pry/default_commands/hist.rb +0 -120
  175. data/lib/pry/default_commands/input_and_output.rb +0 -306
  176. data/lib/pry/default_commands/introspection.rb +0 -410
  177. data/lib/pry/default_commands/ls.rb +0 -272
  178. data/lib/pry/default_commands/misc.rb +0 -38
  179. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  180. data/lib/pry/default_commands/whereami.rb +0 -92
  181. data/lib/pry/extended_commands/experimental.rb +0 -7
  182. data/test/helper.rb +0 -223
  183. data/test/test_completion.rb +0 -62
  184. data/test/test_control_d_handler.rb +0 -45
  185. data/test/test_default_commands/test_cd.rb +0 -321
  186. data/test/test_default_commands/test_context.rb +0 -288
  187. data/test/test_default_commands/test_documentation.rb +0 -315
  188. data/test/test_default_commands/test_gems.rb +0 -18
  189. data/test/test_default_commands/test_input.rb +0 -428
  190. data/test/test_default_commands/test_introspection.rb +0 -511
  191. data/test/test_default_commands/test_ls.rb +0 -151
  192. data/test/test_default_commands/test_shell.rb +0 -343
  193. data/test/test_default_commands/test_show_source.rb +0 -432
  194. data/test/test_pry_output.rb +0 -41
@@ -0,0 +1,139 @@
1
+ class Pry
2
+ class Command::ShowInfo < Pry::ClassCommand
3
+ extend Pry::Helpers::BaseHelpers
4
+
5
+ command_options :shellwords => false
6
+ command_options :requires_gem => "ruby18_source_location" if mri_18?
7
+
8
+ def setup
9
+ require 'ruby18_source_location' if mri_18?
10
+ end
11
+
12
+ def options(opt)
13
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors", :as => :count
14
+ opt.on :l, "line-numbers", "Show line numbers"
15
+ opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)"
16
+ opt.on :f, :flood, "Do not use a pager to view text longer than one screen"
17
+ opt.on :a, :all, "Show all definitions and monkeypatches of the module/class"
18
+ end
19
+
20
+ def process
21
+ code_object = Pry::CodeObject.lookup(obj_name, _pry_, :super => opts[:super])
22
+ raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
23
+
24
+ if show_all_modules?(code_object)
25
+ # show all monkey patches for a module
26
+ result = content_and_headers_for_all_module_candidates(code_object)
27
+ else
28
+ # show a specific code object
29
+ result = content_and_header_for_code_object(code_object)
30
+ end
31
+
32
+ set_file_and_dir_locals(code_object.source_file)
33
+ stagger_output result
34
+ end
35
+
36
+ def content_and_header_for_code_object(code_object)
37
+ header(code_object) << content_for(code_object)
38
+ end
39
+
40
+ def content_and_headers_for_all_module_candidates(mod)
41
+ result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
42
+ mod.number_of_candidates.times do |v|
43
+ candidate = mod.candidate(v)
44
+ begin
45
+ result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.source_file} @ line #{candidate.source_line}:\n"
46
+ content = content_for(candidate)
47
+
48
+ result << "Number of lines: #{content.lines.count}\n\n" << content
49
+ rescue Pry::RescuableException
50
+ result << "\nNo content found.\n"
51
+ next
52
+ end
53
+ end
54
+ result
55
+ end
56
+
57
+ # Generate a header (meta-data information) for all the code
58
+ # object types: methods, modules, commands, procs...
59
+ def header(code_object)
60
+ file_name, line_num = file_and_line_for(code_object)
61
+ h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
62
+ h << method_header(code_object, line_num) if code_object.real_method_object?
63
+ h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
64
+ "#{content_for(code_object).lines.count}\n\n"
65
+ end
66
+
67
+ def method_header(code_object, line_num)
68
+ h = ""
69
+ h << (code_object.c_method? ? "(C Method):" : "@ line #{line_num}:")
70
+ h << method_sections(code_object)[:owner]
71
+ h << method_sections(code_object)[:visibility]
72
+ h << method_sections(code_object)[:signature]
73
+ h
74
+ end
75
+
76
+ def method_sections(code_object)
77
+ {
78
+ :owner => "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n",
79
+ :visibility => "#{text.bold("Visibility:")} #{code_object.visibility}",
80
+ :signature => "\n#{text.bold("Signature:")} #{code_object.signature}"
81
+ }.merge(header_options) { |key, old, new| (new && old).to_s }
82
+ end
83
+
84
+ def header_options
85
+ {
86
+ :owner => true,
87
+ :visibility => true,
88
+ :signature => nil
89
+ }
90
+ end
91
+
92
+ def show_all_modules?(code_object)
93
+ code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
94
+ end
95
+
96
+ def obj_name
97
+ @obj_name ||= args.empty? ? nil : args.join(" ")
98
+ end
99
+
100
+ def use_line_numbers?
101
+ opts.present?(:b) || opts.present?(:l)
102
+ end
103
+
104
+ def start_line_for(code_object)
105
+ if opts.present?(:'base-one')
106
+ 1
107
+ else
108
+ code_object.source_line || 1
109
+ end
110
+ end
111
+
112
+ # takes into account possible yard docs, and returns yard_file / yard_line
113
+ # Also adjusts for start line of comments (using start_line_for), which it has to infer
114
+ # by subtracting number of lines of comment from start line of code_object
115
+ def file_and_line_for(code_object)
116
+ if code_object.module_with_yard_docs?
117
+ [code_object.yard_file, code_object.yard_line]
118
+ else
119
+ [code_object.source_file, start_line_for(code_object)]
120
+ end
121
+ end
122
+
123
+ def complete(input)
124
+ if input =~ /([^ ]*)#([a-z0-9_]*)\z/
125
+ prefix, search = [$1, $2]
126
+ methods = begin
127
+ Pry::Method.all_from_class(binding.eval(prefix))
128
+ rescue RescuableException => e
129
+ return super
130
+ end
131
+ methods.map do |method|
132
+ [prefix, method.name].join('#') if method.name.start_with?(search)
133
+ end.compact
134
+ else
135
+ super
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,17 @@
1
+ class Pry
2
+ class Command::ShowInput < Pry::ClassCommand
3
+ match 'show-input'
4
+ group 'Editing'
5
+ description 'Show the contents of the input buffer for the current multi-line expression.'
6
+
7
+ banner <<-'BANNER'
8
+ Show the contents of the input buffer for the current multi-line expression.
9
+ BANNER
10
+
11
+ def process
12
+ output.puts Code.new(eval_string).with_line_numbers
13
+ end
14
+ end
15
+
16
+ Pry::Commands.add_command(Pry::Command::ShowInput)
17
+ end
@@ -0,0 +1,37 @@
1
+ require 'pry/commands/show_info'
2
+
3
+ class Pry
4
+ class Command::ShowSource < Command::ShowInfo
5
+ match 'show-source'
6
+ group 'Introspection'
7
+ description 'Show the source for a method or class.'
8
+
9
+ banner <<-'BANNER'
10
+ Usage: show-source [OPTIONS] [METH|CLASS]
11
+ Aliases: $, show-method
12
+
13
+ Show the source for a method or class. Tries instance methods first and then
14
+ methods by default.
15
+
16
+ show-source hi_method
17
+ show-source hi_method
18
+ show-source Pry#rep # source for Pry#rep method
19
+ show-source Pry # for Pry class
20
+ show-source Pry -a # for all Pry class definitions (all monkey patches)
21
+ show-source Pry --super # for superclass of Pry (Object class)
22
+
23
+ https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
24
+ BANNER
25
+
26
+ # The source for code_object prepared for display.
27
+ def content_for(code_object)
28
+ raise CommandError, "Cannot locate source!" if !code_object.source
29
+ Code.new(code_object.source, start_line_for(code_object)).
30
+ with_line_numbers(use_line_numbers?).to_s
31
+ end
32
+ end
33
+
34
+ Pry::Commands.add_command(Pry::Command::ShowSource)
35
+ Pry::Commands.alias_command 'show-method', 'show-source'
36
+ Pry::Commands.alias_command '$', 'show-source'
37
+ end
@@ -0,0 +1,22 @@
1
+ class Pry
2
+ class Command::SimplePrompt < Pry::ClassCommand
3
+ match 'simple-prompt'
4
+ group 'Misc'
5
+ description 'Toggle the simple prompt.'
6
+
7
+ banner <<-'BANNER'
8
+ Toggle the simple prompt.
9
+ BANNER
10
+
11
+ def process
12
+ case _pry_.prompt
13
+ when Pry::SIMPLE_PROMPT
14
+ _pry_.pop_prompt
15
+ else
16
+ _pry_.push_prompt Pry::SIMPLE_PROMPT
17
+ end
18
+ end
19
+ end
20
+
21
+ Pry::Commands.add_command(Pry::Command::SimplePrompt)
22
+ end
@@ -0,0 +1,40 @@
1
+ class Pry
2
+ class Command::Stat < Pry::ClassCommand
3
+ match 'stat'
4
+ group 'Introspection'
5
+ description 'View method information and set _file_ and _dir_ locals.'
6
+ command_options :shellwords => false
7
+
8
+ banner <<-'BANNER'
9
+ Usage: stat [OPTIONS] [METH]
10
+
11
+ Show method information for method METH and set _file_ and _dir_ locals.
12
+
13
+ stat hello_method
14
+ BANNER
15
+
16
+ def options(opt)
17
+ method_options(opt)
18
+ end
19
+
20
+ def process
21
+ meth = method_object
22
+ aliases = meth.aliases
23
+
24
+ output.puts unindent <<-EOS
25
+ Method Information:
26
+ --
27
+ Name: #{meth.name}
28
+ Alias#{ "es" if aliases.length > 1 }: #{ aliases.any? ? aliases.join(", ") : "None." }
29
+ Owner: #{meth.owner ? meth.owner : "Unknown"}
30
+ Visibility: #{meth.visibility}
31
+ Type: #{meth.is_a?(::Method) ? "Bound" : "Unbound"}
32
+ Arity: #{meth.arity}
33
+ Method Signature: #{meth.signature}
34
+ Source Location: #{meth.source_location ? meth.source_location.join(":") : "Not found."}
35
+ EOS
36
+ end
37
+ end
38
+
39
+ Pry::Commands.add_command(Pry::Command::Stat)
40
+ end
@@ -0,0 +1,23 @@
1
+ class Pry
2
+ class Command::SwitchTo < Pry::ClassCommand
3
+ match 'switch-to'
4
+ group 'Navigating Pry'
5
+ description 'Start a new subsession on a binding in the current stack.'
6
+
7
+ banner <<-'BANNER'
8
+ Start a new subsession on a binding in the current stack (numbered by nesting).
9
+ BANNER
10
+
11
+ def process(selection)
12
+ selection = selection.to_i
13
+
14
+ if selection < 0 || selection > _pry_.binding_stack.size - 1
15
+ raise CommandError, "Invalid binding index #{selection} - use `nesting` command to view valid indices."
16
+ else
17
+ Pry.start(_pry_.binding_stack[selection])
18
+ end
19
+ end
20
+ end
21
+
22
+ Pry::Commands.add_command(Pry::Command::SwitchTo)
23
+ end
@@ -0,0 +1,20 @@
1
+ class Pry
2
+ class Command::ToggleColor < Pry::ClassCommand
3
+ match 'toggle-color'
4
+ group 'Misc'
5
+ description 'Toggle syntax highlighting.'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: toggle-color
9
+
10
+ Toggle syntax highlighting.
11
+ BANNER
12
+
13
+ def process
14
+ Pry.color = !Pry.color
15
+ output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
16
+ end
17
+ end
18
+
19
+ Pry::Commands.add_command(Pry::Command::ToggleColor)
20
+ end
@@ -0,0 +1,114 @@
1
+ class Pry
2
+ class Command::Whereami < Pry::ClassCommand
3
+ match 'whereami'
4
+ description 'Show code surrounding the current context.'
5
+ group 'Context'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: whereami [-qn] [N]
9
+
10
+ Describe the current location. If you use `binding.pry` inside a method then
11
+ whereami will print out the source for that method.
12
+
13
+ If a number is passed, then N lines before and after the current line will be
14
+ shown instead of the method itself.
15
+
16
+ The `-q` flag can be used to suppress error messages in the case that there's
17
+ no code to show. This is used by pry in the default before_session hook to show
18
+ you when you arrive at a `binding.pry`.
19
+
20
+ The `-n` flag can be used to hide line numbers so that code can be copy/pasted
21
+ effectively.
22
+
23
+ When pry was started on an Object and there is no associated method, whereami
24
+ will instead output a brief description of the current object.
25
+ BANNER
26
+
27
+ def setup
28
+ @method = Pry::Method.from_binding(target)
29
+ @file = target.eval('__FILE__')
30
+ @line = target.eval('__LINE__')
31
+ end
32
+
33
+ def options(opt)
34
+ opt.on :q, :quiet, "Don't display anything in case of an error"
35
+ opt.on :n, :"no-line-numbers", "Do not display line numbers"
36
+ end
37
+
38
+ def code
39
+ @code ||= if show_method?
40
+ Pry::Code.from_method(@method)
41
+ else
42
+ Pry::Code.from_file(@file).around(@line, window_size)
43
+ end
44
+ end
45
+
46
+ def location
47
+ "#{@file} @ line #{@line} #{@method && @method.name_with_owner}"
48
+ end
49
+
50
+ def process
51
+ if nothing_to_do?
52
+ return
53
+ elsif internal_binding?(target)
54
+ handle_internal_binding
55
+ return
56
+ end
57
+
58
+ set_file_and_dir_locals(@file)
59
+
60
+ output.puts "\n#{text.bold('From:')} #{location}:\n\n"
61
+ output.puts code.with_line_numbers(use_line_numbers?).with_marker(marker)
62
+ output.puts
63
+ end
64
+
65
+ private
66
+
67
+ def nothing_to_do?
68
+ opts.quiet? && (internal_binding?(target) || !code?)
69
+ end
70
+
71
+ def use_line_numbers?
72
+ !opts.present?(:n)
73
+ end
74
+
75
+ def marker
76
+ !opts.present?(:n) && @line
77
+ end
78
+
79
+ def top_level?
80
+ target_self == TOPLEVEL_BINDING.eval("self")
81
+ end
82
+
83
+ def handle_internal_binding
84
+ if top_level?
85
+ output.puts "At the top level."
86
+ else
87
+ output.puts "Inside #{Pry.view_clip(target_self)}."
88
+ end
89
+ end
90
+
91
+ def show_method?
92
+ args.empty? && @method && @method.source? && @method.source_range.count < 20 &&
93
+ # These checks are needed in case of an eval with a binding and file/line
94
+ # numbers set to outside the function. As in rails' use of ERB.
95
+ @method.source_file == @file && @method.source_range.include?(@line)
96
+ end
97
+
98
+ def code?
99
+ !!code
100
+ rescue MethodSource::SourceNotFoundError
101
+ false
102
+ end
103
+
104
+ def window_size
105
+ if args.empty?
106
+ Pry.config.default_window_size
107
+ else
108
+ args.first.to_i
109
+ end
110
+ end
111
+ end
112
+
113
+ Pry::Commands.add_command(Pry::Command::Whereami)
114
+ end
@@ -0,0 +1,57 @@
1
+ class Pry
2
+ class Command::Wtf < Pry::ClassCommand
3
+ match /wtf([?!]*)/
4
+ group 'Context'
5
+ description 'Show the backtrace of the most recent exception.'
6
+ options :listing => 'wtf?'
7
+
8
+ banner <<-'BANNER'
9
+ Usage: wtf[?|!]
10
+
11
+ Show's a few lines of the backtrace of the most recent exception (also available
12
+ as `_ex_.backtrace`). If you want to see more lines, add more question marks or
13
+ exclamation marks.
14
+
15
+ wtf?
16
+ wtf?!???!?!?
17
+
18
+ # To see the entire backtrace, pass the `-v` or `--verbose` flag.
19
+ wtf -v
20
+ BANNER
21
+
22
+ def options(opt)
23
+ opt.on :v, :verbose, "Show the full backtrace"
24
+ end
25
+
26
+ def process
27
+ raise Pry::CommandError, "No most-recent exception" unless exception
28
+
29
+ output.puts "#{text.bold('Exception:')} #{exception.class}: #{exception}\n--"
30
+ if opts.verbose?
31
+ output.puts with_line_numbers(backtrace)
32
+ else
33
+ output.puts with_line_numbers(backtrace.first(size_of_backtrace))
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def exception
40
+ _pry_.last_exception
41
+ end
42
+
43
+ def with_line_numbers(bt)
44
+ Pry::Code.new(bt, 0, :text).with_line_numbers.to_s
45
+ end
46
+
47
+ def backtrace
48
+ exception.backtrace
49
+ end
50
+
51
+ def size_of_backtrace
52
+ [captures[0].size, 0.5].max * 10
53
+ end
54
+ end
55
+
56
+ Pry::Commands.add_command(Pry::Command::Wtf)
57
+ end