pry 0.10.4 → 0.14.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +439 -16
  3. data/LICENSE +1 -1
  4. data/README.md +362 -302
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +84 -97
  10. data/lib/pry/code/code_file.rb +37 -26
  11. data/lib/pry/code/code_range.rb +7 -5
  12. data/lib/pry/code/loc.rb +26 -13
  13. data/lib/pry/code.rb +41 -32
  14. data/lib/pry/code_object.rb +53 -28
  15. data/lib/pry/color_printer.rb +46 -35
  16. data/lib/pry/command.rb +197 -369
  17. data/lib/pry/command_set.rb +89 -114
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -72
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -46
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +62 -54
  27. data/lib/pry/commands/cd.rb +40 -35
  28. data/lib/pry/commands/change_inspector.rb +29 -22
  29. data/lib/pry/commands/change_prompt.rb +48 -23
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  36. data/lib/pry/commands/edit.rb +185 -157
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -16
  40. data/lib/pry/commands/find_method.rb +168 -162
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +151 -149
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +35 -28
  47. data/lib/pry/commands/ls/constants.rb +59 -31
  48. data/lib/pry/commands/ls/formatter.rb +42 -36
  49. data/lib/pry/commands/ls/globals.rb +38 -36
  50. data/lib/pry/commands/ls/grep.rb +17 -15
  51. data/lib/pry/commands/ls/instance_vars.rb +29 -28
  52. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  53. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  54. data/lib/pry/commands/ls/local_names.rb +26 -24
  55. data/lib/pry/commands/ls/local_vars.rb +38 -30
  56. data/lib/pry/commands/ls/ls_entity.rb +47 -52
  57. data/lib/pry/commands/ls/methods.rb +49 -51
  58. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  59. data/lib/pry/commands/ls/self_methods.rb +23 -21
  60. data/lib/pry/commands/ls.rb +124 -103
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +92 -82
  63. data/lib/pry/commands/pry_backtrace.rb +22 -17
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +60 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -42
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +56 -29
  71. data/lib/pry/commands/shell_mode.rb +22 -18
  72. data/lib/pry/commands/show_doc.rb +80 -70
  73. data/lib/pry/commands/show_info.rb +193 -160
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +110 -42
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +20 -16
  79. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  80. data/lib/pry/commands/watch_expression.rb +89 -84
  81. data/lib/pry/commands/whereami.rb +155 -146
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +310 -20
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +22 -9
  90. data/lib/pry/editor.rb +56 -34
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +13 -18
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +20 -62
  96. data/lib/pry/helpers/command_helpers.rb +52 -62
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -12
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -85
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +81 -55
  104. data/lib/pry/hooks.rb +60 -110
  105. data/lib/pry/indent.rb +74 -68
  106. data/lib/pry/input_completer.rb +199 -158
  107. data/lib/pry/input_lock.rb +7 -10
  108. data/lib/pry/inspector.rb +36 -24
  109. data/lib/pry/last_exception.rb +45 -45
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/method/patcher.rb +14 -8
  112. data/lib/pry/method/weird_method_locator.rb +79 -45
  113. data/lib/pry/method.rb +178 -124
  114. data/lib/pry/object_path.rb +37 -28
  115. data/lib/pry/output.rb +102 -16
  116. data/lib/pry/pager.rb +187 -177
  117. data/lib/pry/prompt.rb +213 -25
  118. data/lib/pry/pry_class.rb +119 -98
  119. data/lib/pry/pry_instance.rb +261 -224
  120. data/lib/pry/repl.rb +83 -29
  121. data/lib/pry/repl_file_loader.rb +27 -22
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +34 -32
  138. data/lib/pry/wrapped_module.rb +67 -63
  139. data/lib/pry.rb +133 -149
  140. metadata +52 -63
  141. data/lib/pry/commands/disabled_commands.rb +0 -2
  142. data/lib/pry/commands/gem_cd.rb +0 -26
  143. data/lib/pry/commands/gem_install.rb +0 -32
  144. data/lib/pry/commands/gem_list.rb +0 -33
  145. data/lib/pry/commands/gem_open.rb +0 -29
  146. data/lib/pry/commands/gist.rb +0 -101
  147. data/lib/pry/commands/install_command.rb +0 -53
  148. data/lib/pry/commands/list_prompts.rb +0 -35
  149. data/lib/pry/commands/simple_prompt.rb +0 -22
  150. data/lib/pry/commands.rb +0 -6
  151. data/lib/pry/config/behavior.rb +0 -139
  152. data/lib/pry/config/convenience.rb +0 -25
  153. data/lib/pry/config/default.rb +0 -161
  154. data/lib/pry/history_array.rb +0 -121
  155. data/lib/pry/plugins.rb +0 -103
  156. data/lib/pry/rbx_path.rb +0 -22
  157. data/lib/pry/rubygem.rb +0 -82
  158. data/lib/pry/terminal.rb +0 -79
  159. data/lib/pry/test/helper.rb +0 -170
@@ -1,83 +1,93 @@
1
- require 'pry/commands/show_info'
1
+ # frozen_string_literal: true
2
2
 
3
3
  class Pry
4
- class Command::ShowDoc < Command::ShowInfo
5
- include Pry::Helpers::DocumentationHelpers
6
-
7
- match 'show-doc'
8
- group 'Introspection'
9
- description 'Show the documentation for a method or class.'
10
-
11
- banner <<-BANNER
12
- Usage: show-doc [OPTIONS] [METH]
13
- Aliases: ?
14
-
15
- Show the documentation for a method or class. Tries instance methods first and
16
- then methods by default.
17
-
18
- show-doc hi_method # docs for hi_method
19
- show-doc Pry # for Pry class
20
- show-doc Pry -a # for all definitions of Pry class (all monkey patches)
21
- BANNER
22
-
23
- # The docs for code_object prepared for display.
24
- def content_for(code_object)
25
- Code.new(render_doc_markup_for(code_object),
26
- start_line_for(code_object), :text).
27
- with_line_numbers(use_line_numbers?).to_s
28
- end
4
+ class Command
5
+ class ShowDoc < Command::ShowInfo
6
+ include Pry::Helpers::DocumentationHelpers
7
+
8
+ match 'show-doc'
9
+ group 'Introspection'
10
+ description 'Show the documentation for a method or class.'
11
+
12
+ banner <<-BANNER
13
+ Usage: show-doc [OPTIONS] [METH]
14
+ Aliases: ?
15
+
16
+ Show the documentation for a method or class. Tries instance methods first and
17
+ then methods by default.
18
+
19
+ show-doc hi_method # docs for hi_method
20
+ show-doc Pry # for Pry class
21
+ show-doc Pry -a # for all definitions of Pry class (all monkey patches)
22
+ BANNER
23
+
24
+ def process
25
+ super
26
+
27
+ output.puts(
28
+ "\nWARNING: the show-doc command is deprecated. It will be removed " \
29
+ "from future Pry versions.\nPlease use 'show-source' with the -d " \
30
+ "(or --doc) switch instead\nExample: show-source #{obj_name} -d"
31
+ )
32
+ end
33
+
34
+ # The docs for code_object prepared for display.
35
+ def content_for(code_object)
36
+ Code.new(
37
+ render_doc_markup_for(code_object),
38
+ start_line_for(code_object),
39
+ :text
40
+ ).with_line_numbers(use_line_numbers?).to_s
41
+ end
29
42
 
30
- # process the markup (if necessary) and apply colors
31
- def render_doc_markup_for(code_object)
32
- docs = docs_for(code_object)
33
-
34
- if code_object.command?
35
- # command '--help' shouldn't use markup highlighting
36
- docs
37
- else
38
- if docs.empty?
39
- raise CommandError, "No docs found for: #{
40
- obj_name ? obj_name : 'current context'
41
- }"
43
+ # process the markup (if necessary) and apply colors
44
+ def render_doc_markup_for(code_object)
45
+ docs = docs_for(code_object)
46
+
47
+ if code_object.command?
48
+ # command '--help' shouldn't use markup highlighting
49
+ docs
50
+ else
51
+ if docs.empty?
52
+ raise CommandError, "No docs found for: #{obj_name || 'current context'}"
53
+ end
54
+
55
+ process_comment_markup(docs)
42
56
  end
43
- process_comment_markup(docs)
44
57
  end
45
- end
46
58
 
47
- # Return docs for the code_object, adjusting for whether the code_object
48
- # has yard docs available, in which case it returns those.
49
- # (note we only have to check yard docs for modules since they can
50
- # have multiple docs, but methods can only be doc'd once so we
51
- # dont need to check them)
52
- def docs_for(code_object)
53
- if code_object.module_with_yard_docs?
54
- # yard docs
55
- code_object.yard_doc
56
- else
57
- # normal docs (i.e comments above method/module/command)
58
- code_object.doc
59
+ # Return docs for the code_object, adjusting for whether the code_object
60
+ # has yard docs available, in which case it returns those.
61
+ # (note we only have to check yard docs for modules since they can
62
+ # have multiple docs, but methods can only be doc'd once so we
63
+ # dont need to check them)
64
+ def docs_for(code_object)
65
+ if code_object.module_with_yard_docs?
66
+ # yard docs
67
+ code_object.yard_doc
68
+ else
69
+ # normal docs (i.e comments above method/module/command)
70
+ code_object.doc
71
+ end
59
72
  end
60
- end
61
73
 
62
- # Which sections to include in the 'header', can toggle: :owner,
63
- # :signature and visibility.
64
- def header_options
65
- super.merge :signature => true
66
- end
74
+ # Which sections to include in the 'header', can toggle: :owner,
75
+ # :signature and visibility.
76
+ def header_options
77
+ super.merge signature: true
78
+ end
79
+
80
+ # figure out start line of docs by back-calculating based on
81
+ # number of lines in the comment and the start line of the code_object
82
+ # @return [Fixnum] start line of docs
83
+ def start_line_for(code_object)
84
+ return 1 if code_object.command? || opts.present?(:'base-one')
85
+ return 1 unless code_object.source_line
67
86
 
68
- # figure out start line of docs by back-calculating based on
69
- # number of lines in the comment and the start line of the code_object
70
- # @return [Fixnum] start line of docs
71
- def start_line_for(code_object)
72
- if code_object.command? || opts.present?(:'base-one')
73
- 1
74
- else
75
- code_object.source_line.nil? ? 1 :
76
- (code_object.source_line - code_object.doc.lines.count)
87
+ code_object.source_line - code_object.doc.lines.count
77
88
  end
78
89
  end
79
- end
80
90
 
81
- Pry::Commands.add_command(Pry::Command::ShowDoc)
82
- Pry::Commands.alias_command '?', 'show-doc'
91
+ Pry::Commands.add_command(Pry::Command::ShowDoc)
92
+ end
83
93
  end
@@ -1,200 +1,233 @@
1
- class Pry
2
- class Command::ShowInfo < Pry::ClassCommand
3
- extend Pry::Helpers::BaseHelpers
1
+ # frozen_string_literal: true
4
2
 
5
- command_options :shellwords => false, :interpolate => false
3
+ class Pry
4
+ class Command
5
+ class ShowInfo < Pry::ClassCommand
6
+ extend Pry::Helpers::BaseHelpers
6
7
 
7
- def initialize(*)
8
- super
8
+ command_options shellwords: false, interpolate: false
9
9
 
10
- @used_super = nil
11
- end
10
+ def initialize(*)
11
+ super
12
12
 
13
- def options(opt)
14
- opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors", :as => :count
15
- opt.on :l, "line-numbers", "Show line numbers"
16
- opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)"
17
- opt.on :a, :all, "Show all definitions and monkeypatches of the module/class"
18
- end
13
+ @used_super = nil
14
+ end
19
15
 
20
- def process
21
- code_object = Pry::CodeObject.lookup(obj_name, _pry_, :super => opts[:super])
22
- raise CommandError, no_definition_message if !code_object
23
- @original_code_object = code_object
16
+ def options(opt)
17
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to " \
18
+ "traverse the ancestors", as: :count
19
+ opt.on :l, "line-numbers", "Show line numbers"
20
+ opt.on :b, "base-one", "Show line numbers but start numbering at 1 " \
21
+ "(useful for `amend-line` and `play` commands)"
22
+ opt.on :a, :all, "Show all definitions and monkeypatches of the " \
23
+ "module/class"
24
+ end
24
25
 
25
- if show_all_modules?(code_object)
26
- # show all monkey patches for a module
26
+ def process
27
+ code_object = Pry::CodeObject.lookup(obj_name, pry_instance, super: opts[:super])
28
+ raise CommandError, no_definition_message unless code_object
29
+
30
+ @original_code_object = code_object
31
+
32
+ if !obj_name && code_object.c_module? && !opts[:all]
33
+ result = "You're inside an object, whose class is defined by means of " \
34
+ "the C Ruby API.\nPry cannot display the information for this " \
35
+ "class."
36
+ if code_object.candidates.any?
37
+ result += "\nHowever, you can view monkey-patches applied to this " \
38
+ "class.\n.Just execute the same command with the '--all' " \
39
+ "switch."
40
+ end
41
+ elsif show_all_modules?(code_object)
42
+ # show all monkey patches for a module
43
+
44
+ result = content_and_headers_for_all_module_candidates(code_object)
45
+ else
46
+ # show a specific code object
47
+ co = code_object_with_accessible_source(code_object)
48
+ result = content_and_header_for_code_object(co)
49
+ end
27
50
 
28
- result = content_and_headers_for_all_module_candidates(code_object)
29
- else
30
- # show a specific code object
31
- co = code_object_with_accessible_source(code_object)
32
- result = content_and_header_for_code_object(co)
51
+ set_file_and_dir_locals(code_object.source_file)
52
+ pry_instance.pager.page result
33
53
  end
34
54
 
35
- set_file_and_dir_locals(code_object.source_file)
36
- _pry_.pager.page result
37
- end
55
+ # This method checks whether the `code_object` is a WrappedModule, if it
56
+ # is, then it returns the first candidate (monkeypatch) with accessible
57
+ # source (or docs). If `code_object` is not a WrappedModule (i.e a method
58
+ # or a command) then the `code_object` itself is just returned.
59
+ #
60
+ # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
61
+ def code_object_with_accessible_source(code_object)
62
+ return code_object unless code_object.is_a?(WrappedModule)
38
63
 
39
- # This method checks whether the `code_object` is a WrappedModule,
40
- # if it is, then it returns the first candidate (monkeypatch) with
41
- # accessible source (or docs). If `code_object` is not a WrappedModule (i.e a
42
- # method or a command) then the `code_object` itself is just
43
- # returned.
44
- #
45
- # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
46
- def code_object_with_accessible_source(code_object)
47
- if code_object.is_a?(WrappedModule)
48
64
  candidate = code_object.candidates.find(&:source)
49
- if candidate
50
- return candidate
51
- else
52
- raise CommandError, no_definition_message if !valid_superclass?(code_object)
65
+ return candidate if candidate
53
66
 
54
- @used_super = true
55
- code_object_with_accessible_source(code_object.super)
56
- end
57
- else
58
- code_object
67
+ raise CommandError, no_definition_message unless valid_superclass?(code_object)
68
+
69
+ @used_super = true
70
+ code_object_with_accessible_source(code_object.super)
59
71
  end
60
- end
61
72
 
62
- def valid_superclass?(code_object)
63
- code_object.super && code_object.super.wrapped != Object
64
- end
73
+ def valid_superclass?(code_object)
74
+ code_object.super && code_object.super.wrapped != Object
75
+ end
65
76
 
66
- def content_and_header_for_code_object(code_object)
67
- header(code_object) << content_for(code_object)
68
- end
77
+ def content_and_header_for_code_object(code_object)
78
+ header(code_object) + content_for(code_object)
79
+ end
69
80
 
70
- def content_and_headers_for_all_module_candidates(mod)
71
- result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
72
- mod.number_of_candidates.times do |v|
73
- candidate = mod.candidate(v)
74
- begin
75
- result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.source_file} @ line #{candidate.source_line}:\n"
76
- content = content_for(candidate)
77
-
78
- result << "Number of lines: #{content.lines.count}\n\n" << content
79
- rescue Pry::RescuableException
80
- result << "\nNo content found.\n"
81
- next
81
+ def content_and_headers_for_all_module_candidates(mod)
82
+ result = "Found #{mod.number_of_candidates} candidates for " \
83
+ "`#{mod.name}` definition:\n"
84
+ mod.number_of_candidates.times do |v|
85
+ candidate = mod.candidate(v)
86
+ begin
87
+ result += "\nCandidate #{v + 1}/#{mod.number_of_candidates}: " \
88
+ "#{candidate.source_file}:#{candidate.source_line}\n"
89
+ content = content_for(candidate)
90
+
91
+ result += "Number of lines: #{content.lines.count}\n\n" + content
92
+ rescue Pry::RescuableException
93
+ result += "\nNo content found.\n"
94
+ next
95
+ end
82
96
  end
97
+ result
83
98
  end
84
- result
85
- end
86
99
 
87
- def no_definition_message
88
- "Couldn't locate a definition for #{obj_name}!"
89
- end
100
+ def no_definition_message
101
+ "Couldn't locate a definition for #{obj_name}"
102
+ end
90
103
 
91
- # Generate a header (meta-data information) for all the code
92
- # object types: methods, modules, commands, procs...
93
- def header(code_object)
94
- file_name, line_num = file_and_line_for(code_object)
95
- h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
96
- h << code_object_header(code_object, line_num)
97
- h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
98
- "#{content_for(code_object).lines.count}\n\n"
99
- h << Helpers::Text.bold('** Warning:') << " Cannot find code for #{@original_code_object.nonblank_name}. Showing superclass #{code_object.nonblank_name} instead. **\n\n" if @used_super
100
- h
101
- end
104
+ # Generate a header (meta-data information) for all the code
105
+ # object types: methods, modules, commands, procs...
106
+ def header(code_object)
107
+ file_name, line_num = file_and_line_for(code_object)
108
+ content = content_for(code_object)
109
+
110
+ h = "\n#{bold('From:')} #{file_name}"
111
+ h += code_object_header(code_object, line_num)
112
+ h += "\n#{bold('Number of lines:')} " + "#{content.lines.count}\n\n"
113
+ if @used_super
114
+ h += bold('** Warning:')
115
+ h += " Cannot find code for #{@original_code_object.nonblank_name}. " \
116
+ "Showing superclass #{code_object.nonblank_name} instead. **\n\n"
117
+ end
102
118
 
103
- def code_object_header(code_object, line_num)
104
- if code_object.real_method_object?
105
- method_header(code_object, line_num)
119
+ if content.lines.none?
120
+ h += bold('** Warning:')
121
+ h += " Cannot find code for '#{code_object.name}' (source_location is nil)"
122
+ end
106
123
 
107
- # It sucks we have to test for both Pry::WrappedModule and WrappedModule::Candidate,
108
- # probably indicates a deep refactor needs to happen in those classes.
109
- elsif code_object.is_a?(Pry::WrappedModule) || code_object.is_a?(Pry::WrappedModule::Candidate)
110
- module_header(code_object, line_num)
111
- else
112
- ""
124
+ h
113
125
  end
114
- end
115
126
 
116
- def method_header(code_object, line_num)
117
- h = ""
118
- h << (code_object.c_method? ? "(C Method):" : "@ line #{line_num}:")
119
- h << method_sections(code_object)[:owner]
120
- h << method_sections(code_object)[:visibility]
121
- h << method_sections(code_object)[:signature]
122
- h
123
- end
127
+ def code_object_header(code_object, line_num)
128
+ if code_object.real_method_object?
129
+ method_header(code_object, line_num)
124
130
 
125
- def module_header(code_object, line_num)
126
- h = ""
127
- h << "@ line #{line_num}:\n"
128
- h << text.bold(code_object.module? ? "Module" : "Class")
129
- h << " #{text.bold('name:')} #{code_object.nonblank_name}"
131
+ # It sucks we have to test for both Pry::WrappedModule and
132
+ # WrappedModule::Candidate, probably indicates a deep refactor needs
133
+ # to happen in those classes.
134
+ elsif code_object.is_a?(Pry::WrappedModule) ||
135
+ code_object.is_a?(Pry::WrappedModule::Candidate)
136
+ module_header(code_object, line_num)
137
+ else
138
+ ""
139
+ end
140
+ end
130
141
 
131
- if code_object.number_of_candidates > 1
132
- h << (text.bold("\nNumber of monkeypatches: ") << code_object.number_of_candidates.to_s)
133
- h << ". Use the `-a` option to display all available monkeypatches"
142
+ def method_header(code_object, line_num)
143
+ h = ""
144
+ h += (code_object.c_method? ? ' (C Method):' : ":#{line_num}:")
145
+ h += method_sections(code_object)[:owner]
146
+ h += method_sections(code_object)[:visibility]
147
+ h += method_sections(code_object)[:signature]
148
+ h
134
149
  end
135
- h
136
- end
137
150
 
138
- def method_sections(code_object)
139
- {
140
- :owner => "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n",
141
- :visibility => "#{text.bold("Visibility:")} #{code_object.visibility}",
142
- :signature => "\n#{text.bold("Signature:")} #{code_object.signature}"
143
- }.merge(header_options) { |key, old, new| (new && old).to_s }
144
- end
151
+ def module_header(code_object, line_num)
152
+ h = ""
153
+ h += ":#{line_num}\n"
154
+ h += bold(code_object.module? ? "Module" : "Class")
155
+ h += " #{bold('name:')} #{code_object.nonblank_name}"
145
156
 
146
- def header_options
147
- {
148
- :owner => true,
149
- :visibility => true,
150
- :signature => nil
151
- }
152
- end
157
+ if code_object.number_of_candidates > 1
158
+ h += bold("\nNumber of monkeypatches: ")
159
+ h += code_object.number_of_candidates.to_s
160
+ h += ". Use the `-a` option to display all available monkeypatches"
161
+ end
162
+ h
163
+ end
153
164
 
154
- def show_all_modules?(code_object)
155
- code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
156
- end
165
+ def method_sections(code_object)
166
+ {
167
+ owner: "\n#{bold('Owner:')} #{code_object.owner || 'N/A'}\n",
168
+ visibility: "#{bold('Visibility:')} #{code_object.visibility}",
169
+ signature: "\n#{bold('Signature:')} #{code_object.signature}"
170
+ }.merge(header_options) { |_key, old, new| (new && old).to_s }
171
+ end
157
172
 
158
- def obj_name
159
- @obj_name ||= args.empty? ? nil : args.join(' ')
160
- end
173
+ def header_options
174
+ {
175
+ owner: true,
176
+ visibility: true,
177
+ signature: nil
178
+ }
179
+ end
161
180
 
162
- def use_line_numbers?
163
- opts.present?(:b) || opts.present?(:l)
164
- end
181
+ def show_all_modules?(code_object)
182
+ code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
183
+ end
165
184
 
166
- def start_line_for(code_object)
167
- if opts.present?(:'base-one')
168
- 1
169
- else
170
- code_object.source_line || 1
185
+ def obj_name
186
+ @obj_name ||= args.empty? ? nil : args.join(' ')
171
187
  end
172
- end
173
188
 
174
- # takes into account possible yard docs, and returns yard_file / yard_line
175
- # Also adjusts for start line of comments (using start_line_for), which it has to infer
176
- # by subtracting number of lines of comment from start line of code_object
177
- def file_and_line_for(code_object)
178
- if code_object.module_with_yard_docs?
179
- [code_object.yard_file, code_object.yard_line]
180
- else
181
- [code_object.source_file, start_line_for(code_object)]
189
+ def use_line_numbers?
190
+ opts.present?(:b) || opts.present?(:l)
182
191
  end
183
- end
184
192
 
185
- def complete(input)
186
- if input =~ /([^ ]*)#([a-z0-9_]*)\z/
187
- prefix, search = [$1, $2]
188
- methods = begin
189
- Pry::Method.all_from_class(binding.eval(prefix))
190
- rescue RescuableException
191
- return super
192
- end
193
- methods.map do |method|
194
- [prefix, method.name].join('#') if method.name.start_with?(search)
195
- end.compact
196
- else
197
- super
193
+ def start_line_for(code_object)
194
+ if opts.present?(:'base-one')
195
+ 1
196
+ else
197
+ code_object.source_line || 1
198
+ end
199
+ end
200
+
201
+ # takes into account possible yard docs, and returns yard_file / yard_line
202
+ # Also adjusts for start line of comments (using start_line_for), which it
203
+ # has to infer by subtracting number of lines of comment from start line
204
+ # of code_object
205
+ def file_and_line_for(code_object)
206
+ if code_object.module_with_yard_docs?
207
+ [code_object.yard_file, code_object.yard_line]
208
+ else
209
+ [code_object.source_file, start_line_for(code_object)]
210
+ end
211
+ end
212
+
213
+ def complete(input)
214
+ if input =~ /([^ ]*)#([a-z0-9_]*)\z/
215
+ prefix = Regexp.last_match(1)
216
+ search = Regexp.last_match(2)
217
+ methods =
218
+ begin
219
+ # rubocop:disable Security/Eval
220
+ Pry::Method.all_from_class(binding.eval(prefix))
221
+ # rubocop:enable Security/Eval
222
+ rescue RescuableException
223
+ return super
224
+ end
225
+ methods.map do |method|
226
+ [prefix, method.name].join('#') if method.name.start_with?(search)
227
+ end.compact
228
+ else
229
+ super
230
+ end
198
231
  end
199
232
  end
200
233
  end
@@ -1,17 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  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.'
4
+ class Command
5
+ class ShowInput < Pry::ClassCommand
6
+ match 'show-input'
7
+ group 'Editing'
8
+ description 'Show the contents of the input buffer for the current ' \
9
+ 'multi-line expression.'
6
10
 
7
- banner <<-'BANNER'
8
- Show the contents of the input buffer for the current multi-line expression.
9
- BANNER
11
+ banner <<-'BANNER'
12
+ Show the contents of the input buffer for the current multi-line expression.
13
+ BANNER
10
14
 
11
- def process
12
- output.puts Code.new(eval_string).with_line_numbers
15
+ def process
16
+ output.puts Code.new(eval_string).with_line_numbers
17
+ end
13
18
  end
14
- end
15
19
 
16
- Pry::Commands.add_command(Pry::Command::ShowInput)
20
+ Pry::Commands.add_command(Pry::Command::ShowInput)
21
+ end
17
22
  end