pry 0.10.3 → 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 +42 -31
  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 +194 -155
  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 +156 -141
  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 -174
  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} +35 -32
  138. data/lib/pry/wrapped_module.rb +68 -63
  139. data/lib/pry.rb +133 -149
  140. metadata +58 -69
  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,5 +1,6 @@
1
- class Pry
1
+ # frozen_string_literal: true
2
2
 
3
+ class Pry
3
4
  # As a REPL, we often want to catch any unexpected exceptions that may have
4
5
  # been raised; however we don't want to go overboard and prevent the user
5
6
  # from exiting Pry when they want to.
@@ -14,7 +15,7 @@ class Pry
14
15
  # Don't catch signals (particularly not SIGTERM) as these are unlikely
15
16
  # to be intended for pry itself. We should also make sure that
16
17
  # Kernel#exit works.
17
- when *Pry.config.exception_whitelist
18
+ when *Pry.config.unrescued_exceptions
18
19
  false
19
20
  # All other exceptions will be caught.
20
21
  else
@@ -24,9 +25,13 @@ class Pry
24
25
  end
25
26
 
26
27
  # Catches SecurityErrors if $SAFE is set
27
- module Pry::TooSafeException
28
+ module TooSafeException
28
29
  def self.===(exception)
29
- $SAFE > 0 && SecurityError === exception
30
+ if Pry::HAS_SAFE_LEVEL
31
+ $SAFE > 0 && exception.is_a?(SecurityError)
32
+ else
33
+ exception.is_a?(SecurityError)
34
+ end
30
35
  end
31
36
  end
32
37
 
@@ -50,18 +55,14 @@ class Pry
50
55
  # the exception is just a vanilla RuntimeError.
51
56
  module FrozenObjectException
52
57
  def self.===(exception)
53
- ["can't modify frozen class/module",
54
- "can't modify frozen Class",
55
- "can't modify frozen object"
58
+ [
59
+ "can't modify frozen class/module",
60
+ "can't modify frozen Class",
61
+ "can't modify frozen object"
56
62
  ].include?(exception.message)
57
63
  end
58
64
  end
59
65
 
60
- # Don't catch these exceptions
61
- DEFAULT_EXCEPTION_WHITELIST = [SystemExit,
62
- SignalException,
63
- Pry::TooSafeException]
64
-
65
66
  # CommandErrors are caught by the REPL loop and displayed to the user. They
66
67
  # indicate an exceptional condition that's fatal to the current command.
67
68
  class CommandError < StandardError; end
@@ -69,10 +70,4 @@ class Pry
69
70
 
70
71
  # indicates obsolete API
71
72
  class ObsoleteError < StandardError; end
72
-
73
- # This is to keep from breaking under Rails 3.2 for people who are doing that
74
- # IRB = Pry thing.
75
- module ExtendCommandBundle
76
- end
77
-
78
73
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ module Forwardable
5
+ require 'forwardable'
6
+ include ::Forwardable
7
+
8
+ #
9
+ # Since Ruby 2.4, Forwardable will print a warning when
10
+ # calling a method that is private on a delegate, and
11
+ # in the future it could be an error: https://bugs.ruby-lang.org/issues/12782#note-3
12
+ #
13
+ # That's why we revert to a custom implementation for delegating one
14
+ # private method to another.
15
+ #
16
+ def def_private_delegators(target, *private_delegates)
17
+ private_delegates.each do |private_delegate|
18
+ define_method(private_delegate) do |*a, &b|
19
+ instance_variable_get(target).__send__(private_delegate, *a, &b)
20
+ end
21
+ end
22
+ class_eval do
23
+ private(*private_delegates) # rubocop:disable Style/AccessModifierDeclarations
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,9 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
-
4
5
  module BaseHelpers
5
-
6
- module_function
6
+ extend self
7
7
 
8
8
  def silence_warnings
9
9
  old_verbose = $VERBOSE
@@ -20,38 +20,34 @@ class Pry
20
20
  # This is required to introspect methods on objects like Net::HTTP::Get that
21
21
  # have overridden the `method` method.
22
22
  def safe_send(obj, method, *args, &block)
23
- (Module === obj ? Module : Object).instance_method(method).bind(obj).call(*args, &block)
23
+ (obj.is_a?(Module) ? Module : Object).instance_method(method)
24
+ .bind(obj).call(*args, &block)
24
25
  end
25
- public :safe_send
26
26
 
27
27
  def find_command(name, set = Pry::Commands)
28
28
  command_match = set.find do |_, command|
29
- (listing = command.options[:listing]) == name && listing != nil
29
+ (listing = command.options[:listing]) == name && !listing.nil?
30
30
  end
31
31
  command_match.last if command_match
32
32
  end
33
33
 
34
34
  def not_a_real_file?(file)
35
- file =~ /(\(.*\))|<.*>/ || file =~ /__unknown__/ || file == "" || file == "-e"
36
- end
37
-
38
- def command_dependencies_met?(options)
39
- return true if !options[:requires_gem]
40
- Array(options[:requires_gem]).all? do |g|
41
- Rubygem.installed?(g)
42
- end
35
+ file =~ /^(\(.*\))$|^<.*>$/ || file =~ /__unknown__/ || file == "" || file == "-e"
43
36
  end
44
37
 
45
38
  def use_ansi_codes?
46
- windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
39
+ Pry::Helpers::Platform.windows_ansi? ||
40
+ ((term = Pry::Env['TERM']) && term != "dumb")
47
41
  end
48
42
 
49
43
  def colorize_code(code)
50
- CodeRay.scan(code, :ruby).term
44
+ SyntaxHighlighter.highlight(code)
51
45
  end
52
46
 
53
- def highlight(string, regexp, highlight_color=:bright_yellow)
54
- string.gsub(regexp) { |match| "<#{highlight_color}>#{match}</#{highlight_color}>" }
47
+ def highlight(string, regexp, highlight_color = :bright_yellow)
48
+ string.gsub(regexp) do |match|
49
+ "<#{highlight_color}>#{match}</#{highlight_color}>"
50
+ end
55
51
  end
56
52
 
57
53
  # formatting
@@ -60,53 +56,15 @@ class Pry
60
56
  "\e[1m#{text}\e[0m"
61
57
  end
62
58
 
63
- # have fun on the Windows platform.
64
- def windows?
65
- RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
66
- end
67
-
68
- # are we able to use ansi on windows?
69
- def windows_ansi?
70
- defined?(Win32::Console) || ENV['ANSICON'] || (windows? && mri_2?)
71
- end
72
-
73
- def jruby?
74
- RbConfig::CONFIG['ruby_install_name'] == 'jruby'
75
- end
76
-
77
- def jruby_19?
78
- jruby? && RbConfig::CONFIG['ruby_version'] == '1.9'
79
- end
80
-
81
- def rbx?
82
- RbConfig::CONFIG['ruby_install_name'] == 'rbx'
83
- end
84
-
85
- def mri?
86
- RbConfig::CONFIG['ruby_install_name'] == 'ruby'
87
- end
88
-
89
- def mri_19?
90
- mri? && RUBY_VERSION =~ /^1\.9/
91
- end
92
-
93
- def mri_2?
94
- mri? && RUBY_VERSION =~ /^2/
95
- end
96
-
97
- def mri_20?
98
- mri? && RUBY_VERSION =~ /^2\.0/
99
- end
100
-
101
- def mri_21?
102
- mri? && RUBY_VERSION =~ /^2\.1/
103
- end
104
-
105
59
  # Send the given text through the best available pager (if Pry.config.pager is
106
60
  # enabled). Infers where to send the output if used as a mixin.
107
61
  # DEPRECATED.
108
- def stagger_output(text, out = nil)
109
- Pry.new.pager.page text
62
+ def stagger_output(text, _out = nil)
63
+ if defined?(pry_instance) && pry_instance
64
+ pry_instance.pager.page text
65
+ else
66
+ Pry.new.pager.page text
67
+ end
110
68
  end
111
69
  end
112
70
  end
@@ -1,90 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+
1
5
  class Pry
2
6
  module Helpers
3
-
4
7
  module CommandHelpers
5
8
  include OptionsHelpers
6
9
 
7
- module_function
10
+ extend self
8
11
 
9
12
  # Open a temp file and yield it to the block, closing it after
10
13
  # @return [String] The path of the temp file
11
- def temp_file(ext='.rb')
14
+ def temp_file(ext = '.rb')
12
15
  file = Tempfile.new(['pry', ext])
13
- yield file
16
+ yield(file)
14
17
  ensure
15
- file.close(true) if file
18
+ file.close(true)
16
19
  end
17
20
 
18
- def internal_binding?(target)
19
- m = target.eval("::Kernel.__method__").to_s
21
+ def internal_binding?(context)
22
+ method_name = context.eval("::Kernel.__method__").to_s
20
23
  # class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
21
- ["__binding__", "__pry__", "class_eval"].include?(m)
24
+ %w[__binding__ __pry__ class_eval].include?(method_name)
25
+ # TODO: codehaus is dead, there was no test for this and the
26
+ # description for the commit doesn't exist. Probably a candidate for
27
+ # removal so we have a chance to introduce a regression and document it
28
+ # properly.
22
29
  end
23
30
 
24
- def get_method_or_raise(name, target, opts={}, omit_help=false)
25
- meth = Pry::Method.from_str(name, target, opts)
26
-
27
- if name && !meth
28
- command_error("The method '#{name}' could not be found.", omit_help, MethodNotFound)
31
+ def get_method_or_raise(method_name, context, opts = {})
32
+ method = Pry::Method.from_str(method_name, context, opts)
33
+ if !method && method_name
34
+ raise Pry::MethodNotFound, "method '#{method_name}' could not be found."
29
35
  end
30
36
 
31
37
  (opts[:super] || 0).times do
32
- if meth.super
33
- meth = meth.super
38
+ if method.super
39
+ method = method.super
34
40
  else
35
- command_error("'#{meth.name_with_owner}' has no super method.", omit_help, MethodNotFound)
41
+ raise Pry::MethodNotFound,
42
+ "'#{method.name_with_owner}' has no super method"
36
43
  end
37
44
  end
38
45
 
39
- if !meth || (!name && internal_binding?(target))
40
- command_error("No method name given, and context is not a method.", omit_help, MethodNotFound)
46
+ if !method || (!method_name && internal_binding?(context))
47
+ raise Pry::MethodNotFound,
48
+ 'no method name given, and context is not a method'
41
49
  end
42
50
 
43
- set_file_and_dir_locals(meth.source_file)
44
- meth
51
+ set_file_and_dir_locals(method.source_file)
52
+ method
45
53
  end
46
54
 
47
- def command_error(message, omit_help, klass=CommandError)
48
- message += " Type `#{command_name} --help` for help." unless omit_help
49
- raise klass, message
50
- end
51
-
52
- # Remove any common leading whitespace from every line in `text`.
53
- #
54
- # This can be used to make a HEREDOC line up with the left margin, without
55
+ # Remove any common leading whitespace from every line in `text`. This
56
+ # can be used to make a HEREDOC line up with the left margin, without
55
57
  # sacrificing the indentation level of the source code.
56
58
  #
57
- # e.g.
58
- # opt.banner unindent <<-USAGE
59
+ # @example
60
+ # opt.banner(unindent(<<-USAGE))
59
61
  # Lorem ipsum dolor sit amet, consectetur adipisicing elit,
60
62
  # sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
61
63
  # "Ut enim ad minim veniam."
62
64
  # USAGE
63
65
  #
64
- # Heavily based on textwrap.dedent from Python, which is:
65
- # Copyright (C) 1999-2001 Gregory P. Ward.
66
- # Copyright (C) 2002, 2003 Python Software Foundation.
67
- # Written by Greg Ward <gward@python.net>
68
- #
69
- # Licensed under <http://docs.python.org/license.html>
70
- # From <http://hg.python.org/cpython/file/6b9f0a6efaeb/Lib/textwrap.py>
71
- #
72
- # @param [String] text The text from which to remove indentation
73
- # @return [String] The text with indentation stripped.
74
- def unindent(text, left_padding = 0)
75
- # Empty blank lines
76
- text = text.sub(/^[ \t]+$/, '')
77
-
78
- # Find the longest common whitespace to all indented lines
79
- # Ignore lines containing just -- or ++ as these seem to be used by
80
- # comment authors as delimeters.
81
- margin = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/).inject do |current_margin, next_indent|
66
+ # @param [String] dirty_text The text from which to remove indentation
67
+ # @return [String] the text with indentation stripped
68
+ def unindent(dirty_text, left_padding = 0)
69
+ text = dirty_text.sub(/\A[ \t]+\z/, '') # Empty blank lines.
70
+
71
+ # Find the longest common whitespace to all indented lines. Ignore lines
72
+ # containing just -- or ++ as these seem to be used by comment authors
73
+ # as delimeters.
74
+ scanned_text = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/)
75
+ margin = scanned_text.inject do |current_margin, next_indent|
82
76
  if next_indent.start_with?(current_margin)
83
77
  current_margin
84
78
  elsif current_margin.start_with?(next_indent)
85
79
  next_indent
86
80
  else
87
- ""
81
+ ''
88
82
  end
89
83
  end
90
84
 
@@ -93,7 +87,7 @@ class Pry
93
87
 
94
88
  # Restrict a string to the given range of lines (1-indexed)
95
89
  # @param [String] content The string.
96
- # @param [Range, Fixnum] lines The line(s) to restrict it to.
90
+ # @param [Range, Integer] lines The line(s) to restrict it to.
97
91
  # @return [String] The resulting string.
98
92
  def restrict_to_lines(content, lines)
99
93
  line_range = one_index_range_or_number(lines)
@@ -101,11 +95,7 @@ class Pry
101
95
  end
102
96
 
103
97
  def one_index_number(line_number)
104
- if line_number > 0
105
- line_number - 1
106
- else
107
- line_number
108
- end
98
+ line_number > 0 ? line_number - 1 : line_number
109
99
  end
110
100
 
111
101
  # convert a 1-index range to a 0-indexed one
@@ -142,15 +132,15 @@ class Pry
142
132
  Range.new(a, b)
143
133
  end
144
134
 
145
- def set_file_and_dir_locals(file_name, _pry_=_pry_(), target=target())
146
- return if !target or !file_name
147
- _pry_.last_file = File.expand_path(file_name)
148
- _pry_.inject_local("_file_", _pry_.last_file, target)
135
+ def set_file_and_dir_locals(file_name, pry = pry_instance, ctx = target)
136
+ return if !ctx || !file_name
137
+
138
+ pry.last_file = File.expand_path(file_name)
139
+ pry.inject_local("_file_", pry.last_file, ctx)
149
140
 
150
- _pry_.last_dir = File.dirname(_pry_.last_file)
151
- _pry_.inject_local("_dir_", _pry_.last_dir, target)
141
+ pry.last_dir = File.dirname(pry.last_file)
142
+ pry.inject_local("_dir_", pry.last_dir, ctx)
152
143
  end
153
144
  end
154
-
155
145
  end
156
146
  end
@@ -1,20 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
-
4
5
  # This class contains methods useful for extracting
5
6
  # documentation from methods and classes.
6
7
  module DocumentationHelpers
8
+ YARD_TAGS = %w[
9
+ param return option yield attr attr_reader attr_writer deprecate example
10
+ raise
11
+ ].freeze
7
12
 
8
13
  module_function
9
14
 
10
15
  def process_rdoc(comment)
11
16
  comment = comment.dup
12
- comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
13
- gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
14
- gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{$1}\e[0m" }.
15
- gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
16
- gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
17
- gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
17
+ last_match_ruby = proc do
18
+ SyntaxHighlighter.highlight(Regexp.last_match(1))
19
+ end
20
+
21
+ comment.gsub(%r{<code>(?:\s*\n)?(.*?)\s*</code>}m, &last_match_ruby)
22
+ .gsub(%r{<em>(?:\s*\n)?(.*?)\s*</em>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
23
+ .gsub(%r{<i>(?:\s*\n)?(.*?)\s*</i>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
24
+ .gsub(%r{<tt>(?:\s*\n)?(.*?)\s*</tt>}m, &last_match_ruby)
25
+ .gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{Regexp.last_match(1)}\e[0m" }
26
+ .gsub(/((?:^[ \t]+(?:(?!.+\e\[)).+(?:\n+|\Z))+)/, &last_match_ruby)
27
+ .gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{last_match_ruby.call}`" }
18
28
  end
19
29
 
20
30
  def process_yardoc_tag(comment, tag)
@@ -33,10 +43,9 @@ class Pry
33
43
  end
34
44
 
35
45
  def process_yardoc(comment)
36
- yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
37
- "deprecate", "example", "raise"]
38
- (yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
39
- gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{$1}\e[0m" }
46
+ (YARD_TAGS - %w[example])
47
+ .inject(comment) { |a, v| process_yardoc_tag(a, v) }
48
+ .gsub(/^@(#{YARD_TAGS.join("|")})/) { "\e[33m#{Regexp.last_match(1)}\e[0m" }
40
49
  end
41
50
 
42
51
  def process_comment_markup(comment)
@@ -46,7 +55,7 @@ class Pry
46
55
  # @param [String] code
47
56
  # @return [String]
48
57
  def strip_comments_from_c_code(code)
49
- code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
58
+ code.sub(%r{\A\s*/\*.*?\*/\s*}m, '')
50
59
  end
51
60
 
52
61
  # Given a string that makes up a comment in a source-code file parse out the content
@@ -1,26 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
5
  module OptionsHelpers
4
6
  module_function
5
7
 
6
- # Add method options to the Slop instance
8
+ # Add method options to the Pry::Slop instance
7
9
  def method_options(opt)
8
10
  @method_target = target
9
11
  opt.on :M, "instance-methods", "Operate on instance methods."
10
12
  opt.on :m, :methods, "Operate on methods."
11
- opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
12
- opt.on :c, :context, "Select object context to run under.", :argument => true do |context|
13
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to " \
14
+ "traverse the ancestors.", as: :count
15
+ opt.on(
16
+ :c, :context, "Select object context to run under.", argument: true
17
+ ) do |context|
13
18
  @method_target = Pry.binding_for(target.eval(context))
14
19
  end
15
20
  end
16
21
 
17
22
  # Get the method object parsed by the slop instance
18
23
  def method_object
19
- @method_object ||= get_method_or_raise(args.empty? ? nil : args.join(" "), @method_target,
20
- :super => opts[:super],
21
- :instance => opts.present?(:'instance-methods') && !opts.present?(:'methods'),
22
- :methods => opts.present?(:'methods') && !opts.present?(:'instance-methods')
23
- )
24
+ @method_object ||= get_method_or_raise(
25
+ args.empty? ? nil : args.join(" "),
26
+ @method_target,
27
+ super: opts[:super],
28
+ instance: opts.present?(:'instance-methods') && !opts.present?(:methods),
29
+ methods: opts.present?(:methods) && !opts.present?(:'instance-methods')
30
+ )
24
31
  end
25
32
  end
26
33
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbconfig'
4
+
5
+ class Pry
6
+ module Helpers
7
+ # Contains methods for querying the platform that Pry is running on
8
+ # @api public
9
+ # @since v0.12.0
10
+ module Platform
11
+ # @return [Boolean]
12
+ def self.mac_osx?
13
+ !!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
14
+ end
15
+
16
+ # @return [Boolean]
17
+ def self.linux?
18
+ !!(RbConfig::CONFIG['host_os'] =~ /linux/i)
19
+ end
20
+
21
+ # @return [Boolean] true when Pry is running on Windows with ANSI support,
22
+ # false otherwise
23
+ def self.windows?
24
+ !!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def self.windows_ansi?
29
+ return false unless windows?
30
+
31
+ !!(defined?(Win32::Console) || Pry::Env['ANSICON'] || mri?)
32
+ end
33
+
34
+ # @return [Boolean]
35
+ def self.jruby?
36
+ RbConfig::CONFIG['ruby_install_name'] == 'jruby'
37
+ end
38
+
39
+ # @return [Boolean]
40
+ def self.jruby_19?
41
+ jruby? && RbConfig::CONFIG['ruby_version'] == '1.9'
42
+ end
43
+
44
+ # @return [Boolean]
45
+ def self.mri?
46
+ RbConfig::CONFIG['ruby_install_name'] == 'ruby'
47
+ end
48
+
49
+ # @return [Boolean]
50
+ def self.mri_2?
51
+ mri? && RUBY_VERSION.start_with?('2')
52
+ end
53
+ end
54
+ end
55
+ end