pry 0.12.2 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +158 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry/basic_object.rb +8 -4
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +44 -55
  10. data/lib/pry/code/code_file.rb +28 -24
  11. data/lib/pry/code/code_range.rb +4 -2
  12. data/lib/pry/code/loc.rb +15 -8
  13. data/lib/pry/code.rb +40 -28
  14. data/lib/pry/code_object.rb +40 -38
  15. data/lib/pry/color_printer.rb +47 -46
  16. data/lib/pry/command.rb +166 -369
  17. data/lib/pry/command_set.rb +76 -73
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -81
  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 +71 -60
  24. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +61 -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 +44 -39
  30. data/lib/pry/commands/clear_screen.rb +16 -10
  31. data/lib/pry/commands/code_collector.rb +148 -133
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +19 -30
  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 +184 -161
  37. data/lib/pry/commands/exit.rb +39 -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 -160
  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 -150
  44. data/lib/pry/commands/import_set.rb +20 -16
  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 -42
  48. data/lib/pry/commands/ls/formatter.rb +50 -46
  49. data/lib/pry/commands/ls/globals.rb +38 -34
  50. data/lib/pry/commands/ls/grep.rb +17 -13
  51. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  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 -22
  55. data/lib/pry/commands/ls/local_vars.rb +38 -28
  56. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  57. data/lib/pry/commands/ls/methods.rb +44 -43
  58. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  59. data/lib/pry/commands/ls/self_methods.rb +23 -22
  60. data/lib/pry/commands/ls.rb +124 -102
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -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 +27 -22
  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 +55 -45
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +51 -51
  71. data/lib/pry/commands/shell_mode.rb +21 -17
  72. data/lib/pry/commands/show_doc.rb +80 -68
  73. data/lib/pry/commands/show_info.rb +189 -171
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +110 -45
  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 -86
  81. data/lib/pry/commands/whereami.rb +156 -148
  82. data/lib/pry/commands/wtf.rb +75 -50
  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 +307 -25
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +9 -7
  90. data/lib/pry/editor.rb +48 -21
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +13 -16
  94. data/lib/pry/forwardable.rb +5 -1
  95. data/lib/pry/helpers/base_helpers.rb +68 -197
  96. data/lib/pry/helpers/command_helpers.rb +50 -61
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +14 -7
  99. data/lib/pry/helpers/platform.rb +7 -5
  100. data/lib/pry/helpers/table.rb +33 -26
  101. data/lib/pry/helpers/text.rb +22 -19
  102. data/lib/pry/helpers.rb +2 -0
  103. data/lib/pry/history.rb +48 -56
  104. data/lib/pry/hooks.rb +21 -12
  105. data/lib/pry/indent.rb +54 -50
  106. data/lib/pry/input_completer.rb +248 -230
  107. data/lib/pry/input_lock.rb +8 -9
  108. data/lib/pry/inspector.rb +36 -24
  109. data/lib/pry/last_exception.rb +45 -45
  110. data/lib/pry/method/disowned.rb +16 -4
  111. data/lib/pry/method/patcher.rb +12 -3
  112. data/lib/pry/method/weird_method_locator.rb +68 -44
  113. data/lib/pry/method.rb +141 -94
  114. data/lib/pry/object_path.rb +33 -25
  115. data/lib/pry/output.rb +121 -35
  116. data/lib/pry/pager.rb +41 -42
  117. data/lib/pry/prompt.rb +123 -54
  118. data/lib/pry/pry_class.rb +61 -103
  119. data/lib/pry/pry_instance.rb +217 -215
  120. data/lib/pry/repl.rb +18 -22
  121. data/lib/pry/repl_file_loader.rb +27 -21
  122. data/lib/pry/ring.rb +11 -6
  123. data/lib/pry/slop/commands.rb +164 -169
  124. data/lib/pry/slop/option.rb +172 -168
  125. data/lib/pry/slop.rb +574 -563
  126. data/lib/pry/syntax_highlighter.rb +26 -0
  127. data/lib/pry/system_command_handler.rb +17 -0
  128. data/lib/pry/testable/evalable.rb +21 -12
  129. data/lib/pry/testable/mockable.rb +18 -10
  130. data/lib/pry/testable/pry_tester.rb +71 -56
  131. data/lib/pry/testable/utility.rb +29 -21
  132. data/lib/pry/testable/variables.rb +49 -43
  133. data/lib/pry/testable.rb +59 -61
  134. data/lib/pry/version.rb +3 -1
  135. data/lib/pry/warning.rb +27 -0
  136. data/lib/pry/wrapped_module/candidate.rb +21 -14
  137. data/lib/pry/wrapped_module.rb +51 -42
  138. data/lib/pry.rb +132 -119
  139. metadata +34 -34
  140. data/lib/pry/commands/disabled_commands.rb +0 -2
  141. data/lib/pry/commands/gem_cd.rb +0 -26
  142. data/lib/pry/commands/gem_install.rb +0 -32
  143. data/lib/pry/commands/gem_list.rb +0 -33
  144. data/lib/pry/commands/gem_open.rb +0 -29
  145. data/lib/pry/commands/gem_readme.rb +0 -25
  146. data/lib/pry/commands/gem_search.rb +0 -40
  147. data/lib/pry/commands/gem_stats.rb +0 -83
  148. data/lib/pry/commands/gist.rb +0 -102
  149. data/lib/pry/commands/install_command.rb +0 -54
  150. data/lib/pry/commands.rb +0 -6
  151. data/lib/pry/config/behavior.rb +0 -255
  152. data/lib/pry/config/convenience.rb +0 -28
  153. data/lib/pry/config/default.rb +0 -159
  154. data/lib/pry/config/memoization.rb +0 -48
  155. data/lib/pry/platform.rb +0 -91
  156. data/lib/pry/plugins.rb +0 -122
  157. data/lib/pry/rubygem.rb +0 -84
  158. data/lib/pry/terminal.rb +0 -91
data/bin/pry CHANGED
@@ -1,4 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # (C) John Mair (banisterfiend)
5
+ # MIT license
6
+
2
7
  $0 = 'pry'
3
8
 
4
9
  require 'pry'
@@ -1,6 +1,10 @@
1
- class Pry::BasicObject < BasicObject
2
- [:Kernel, :Pry].each do |constant|
3
- const_set constant, ::Object.const_get(constant)
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ class BasicObject < BasicObject
5
+ [:Kernel, :File, :Dir, :LoadError, :ENV, :Pry].each do |constant|
6
+ const_set constant, ::Object.const_get(constant)
7
+ end
8
+ include Kernel
4
9
  end
5
- include Kernel
6
10
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # A super-class for Commands that are created with a single block.
5
+ #
6
+ # This class ensures that the block is called with the correct number of
7
+ # arguments and the right context.
8
+ #
9
+ # Create subclasses using {Pry::CommandSet#command}.
10
+ class BlockCommand < Command
11
+ # Call the block that was registered with this command.
12
+ # @param [Array<String>] args The arguments passed
13
+ # @return [Object] The return value of the block
14
+ def call(*args)
15
+ instance_exec(*normalize_method_args(block, args), &block)
16
+ end
17
+
18
+ def help
19
+ "#{command_options[:listing].to_s.ljust(18)} #{description}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # A super-class of Commands with structure.
5
+ #
6
+ # This class implements the bare-minimum functionality that a command should
7
+ # have, namely a --help switch, and then delegates actual processing to its
8
+ # subclasses.
9
+ #
10
+ # Create subclasses using {Pry::CommandSet#create_command}, and override the
11
+ # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
12
+ # method to actually run the command. If necessary, you can also override
13
+ # `setup` which will be called before `options`, for example to require any
14
+ # gems your command needs to run, or to set up state.
15
+ class ClassCommand < Command
16
+ class << self
17
+ # Ensure that subclasses inherit the options, description and
18
+ # match from a ClassCommand super class.
19
+ def inherited(klass)
20
+ klass.match match
21
+ klass.description description
22
+ klass.command_options options
23
+ end
24
+
25
+ def source
26
+ source_object.source
27
+ end
28
+
29
+ def doc
30
+ new.help
31
+ end
32
+
33
+ def source_location
34
+ source_object.source_location
35
+ end
36
+
37
+ def source_file
38
+ source_object.source_file
39
+ end
40
+ alias file source_file
41
+
42
+ def source_line
43
+ source_object.source_line
44
+ end
45
+ alias line source_line
46
+
47
+ private
48
+
49
+ # The object used to extract the source for the command.
50
+ #
51
+ # This should be a `Pry::Method(block)` for a command made with `create_command`
52
+ # and a `Pry::WrappedModule(self)` for a command that's a standard class.
53
+ # @return [Pry::WrappedModule, Pry::Method]
54
+ def source_object
55
+ @source_object ||= if name =~ /^[A-Z]/
56
+ Pry::WrappedModule(self)
57
+ else
58
+ Pry::Method(block)
59
+ end
60
+ end
61
+ end
62
+
63
+ attr_accessor :opts
64
+ attr_accessor :args
65
+
66
+ # Set up `opts` and `args`, and then call `process`.
67
+ #
68
+ # This method will display help if necessary.
69
+ #
70
+ # @param [Array<String>] args The arguments passed
71
+ # @return [Object] The return value of `process` or VOID_VALUE
72
+ def call(*args)
73
+ setup
74
+
75
+ self.opts = slop
76
+ self.args = opts.parse!(args)
77
+
78
+ if opts.present?(:help)
79
+ output.puts slop.help
80
+ void
81
+ else
82
+ process(*normalize_method_args(method(:process), args))
83
+ end
84
+ end
85
+
86
+ # Return the help generated by Pry::Slop for this command.
87
+ def help
88
+ slop.help
89
+ end
90
+
91
+ # Return an instance of Pry::Slop that can parse either subcommands or the
92
+ # options that this command accepts.
93
+ def slop
94
+ Pry::Slop.new do |opt|
95
+ opt.banner(unindent(self.class.banner))
96
+ subcommands(opt)
97
+ options(opt)
98
+ opt.on :h, :help, 'Show this message.'
99
+ end
100
+ end
101
+
102
+ # Generate shell completions
103
+ # @param [String] search The line typed so far
104
+ # @return [Array<String>] the words to complete
105
+ def complete(search)
106
+ slop.flat_map do |opt|
107
+ [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
108
+ end.compact + super
109
+ end
110
+
111
+ # A method called just before `options(opt)` as part of `call`.
112
+ #
113
+ # This method can be used to set up any context your command needs to run,
114
+ # for example requiring gems, or setting default values for options.
115
+ #
116
+ # @example
117
+ # def setup
118
+ # require 'gist'
119
+ # @action = :method
120
+ # end
121
+ def setup; end
122
+
123
+ # A method to setup Pry::Slop commands so it can parse the subcommands your
124
+ # command expects. If you need to set up default values, use `setup`
125
+ # instead.
126
+ #
127
+ # @example A minimal example
128
+ # def subcommands(cmd)
129
+ # cmd.command :download do |opt|
130
+ # description 'Downloads a content from a server'
131
+ #
132
+ # opt.on :verbose, 'Use verbose output'
133
+ #
134
+ # run do |options, arguments|
135
+ # ContentDownloader.download(options, arguments)
136
+ # end
137
+ # end
138
+ # end
139
+ #
140
+ # @example Define the invokation block anywhere you want
141
+ # def subcommands(cmd)
142
+ # cmd.command :download do |opt|
143
+ # description 'Downloads a content from a server'
144
+ #
145
+ # opt.on :verbose, 'Use verbose output'
146
+ # end
147
+ # end
148
+ #
149
+ # def process
150
+ # # Perform calculations...
151
+ # opts.fetch_command(:download).run do |options, arguments|
152
+ # ContentDownloader.download(options, arguments)
153
+ # end
154
+ # # More calculations...
155
+ # end
156
+ def subcommands(cmd); end
157
+
158
+ # A method to setup Pry::Slop so it can parse the options your command expects.
159
+ #
160
+ # @note Please don't do anything side-effecty in the main part of this
161
+ # method, as it may be called by Pry at any time for introspection reasons.
162
+ # If you need to set up default values, use `setup` instead.
163
+ #
164
+ # @example
165
+ # def options(opt)
166
+ # opt.banner "Gists methods or classes"
167
+ # opt.on(:c, :class, "gist a class") do
168
+ # @action = :class
169
+ # end
170
+ # end
171
+ def options(opt); end
172
+
173
+ # The actual body of your command should go here.
174
+ #
175
+ # The `opts` mehod can be called to get the options that Pry::Slop has passed,
176
+ # and `args` gives the remaining, unparsed arguments.
177
+ #
178
+ # The return value of this method is discarded unless the command was
179
+ # created with `:keep_retval => true`, in which case it is returned to the
180
+ # repl.
181
+ #
182
+ # @example
183
+ # def process
184
+ # if opts.present?(:class)
185
+ # gist_class
186
+ # else
187
+ # gist_method
188
+ # end
189
+ # end
190
+ def process
191
+ raise CommandError, "command '#{command_name}' not implemented"
192
+ end
193
+ end
194
+ end
data/lib/pry/cli.rb CHANGED
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
1
5
  class Pry
2
6
  # Manage the processing of command line options
3
7
  class CLI
@@ -31,15 +35,6 @@ class Pry
31
35
  self
32
36
  end
33
37
 
34
- # Bring in options defined in plugins
35
- def add_plugin_options
36
- Pry.plugins.values.each do |plugin|
37
- plugin.load_cli_options
38
- end
39
-
40
- self
41
- end
42
-
43
38
  # Add a block responsible for processing parsed options.
44
39
  def add_option_processor(&block)
45
40
  self.option_processors ||= []
@@ -56,14 +51,22 @@ class Pry
56
51
 
57
52
  def parse_options(args = ARGV)
58
53
  unless options
59
- raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
54
+ raise NoOptionsError,
55
+ "No command line options defined! Use Pry::CLI.add_options to " \
56
+ "add command line options."
60
57
  end
61
58
 
62
- self.input_args = args
59
+ @pass_argv = args.index { |cli_arg| %w[- --].include?(cli_arg) }
60
+ if @pass_argv
61
+ slop_args = args[0...@pass_argv]
62
+ self.input_args = args.replace(args[@pass_argv + 1..-1])
63
+ else
64
+ self.input_args = slop_args = args
65
+ end
63
66
 
64
67
  begin
65
68
  opts = Pry::Slop.parse!(
66
- args,
69
+ slop_args,
67
70
  help: true,
68
71
  multiple_switches: false,
69
72
  strict: true,
@@ -72,22 +75,20 @@ class Pry
72
75
  rescue Pry::Slop::InvalidOptionError
73
76
  # Display help message on unknown switches and exit.
74
77
  puts Pry::Slop.new(&options)
75
- exit
78
+ Kernel.exit
76
79
  end
77
80
 
78
81
  Pry.initial_session_setup
79
82
  Pry.final_session_setup
80
83
 
81
84
  # Option processors are optional.
82
- if option_processors
83
- option_processors.each { |processor| processor.call(opts) }
84
- end
85
+ option_processors.each { |processor| processor.call(opts) } if option_processors
85
86
 
86
87
  opts
87
88
  end
88
89
 
89
90
  def start(opts)
90
- exit if opts.help?
91
+ Kernel.exit if opts.help?
91
92
 
92
93
  # invoked via cli
93
94
  Pry.cli = true
@@ -95,16 +96,16 @@ class Pry
95
96
  # create the actual context
96
97
  if opts[:context]
97
98
  Pry.initial_session_setup
98
- context = Pry.binding_for(eval(opts[:context]))
99
+ context = Pry.binding_for(eval(opts[:context])) # rubocop:disable Security/Eval
99
100
  Pry.final_session_setup
100
101
  else
101
102
  context = Pry.toplevel_binding
102
103
  end
103
104
 
104
- if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
105
+ if !@pass_argv && Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
105
106
  full_name = File.expand_path(Pry::CLI.input_args.first)
106
107
  Pry.load_file_through_repl(full_name)
107
- exit
108
+ Kernel.exit
108
109
  end
109
110
 
110
111
  # Start the session (running any code passed with -e, if there is any)
@@ -116,27 +117,19 @@ class Pry
116
117
  end
117
118
  end
118
119
 
119
- # Bring in options defined by plugins
120
- Pry::Slop.new do
121
- on "no-plugins" do
122
- Pry.config.should_load_plugins = false
123
- end
124
- end.parse(ARGV.dup)
125
-
126
- if Pry.config.should_load_plugins
127
- Pry::CLI.add_plugin_options
128
- end
129
-
130
120
  # The default Pry command line options (before plugin options are included)
131
121
  Pry::CLI.add_options do
132
122
  banner(
133
123
  "Usage: pry [OPTIONS]\n" \
134
124
  "Start a Pry session.\n" \
135
- "See http://pryrepl.org/ for more information.\n"
125
+ "See http://pryrepl.org/ for more information.\n" \
126
+ "Copyright (c) 2016 John Mair (banisterfiend)" \
136
127
  )
137
128
 
138
- on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
139
- Pry.config.exec_string += "\n" if Pry.config.exec_string.length > 0
129
+ on(
130
+ :e, :exec=, "A line of code to execute in context before the session starts"
131
+ ) do |input|
132
+ Pry.config.exec_string += "\n" unless Pry.config.exec_string.empty?
140
133
  Pry.config.exec_string += input
141
134
  end
142
135
 
@@ -157,49 +150,45 @@ Pry::CLI.add_options do
157
150
  Pry.config.should_load_local_rc = false
158
151
  end
159
152
 
160
- on :s, "select-plugin=", "Only load specified plugin (and no others)." do |plugin_name|
161
- Pry.config.should_load_plugins = false
162
- Pry.plugins[plugin_name].activate!
153
+ on :s, "select-plugin=", "Only load specified plugin (and no others)." do |_plugin_name|
154
+ warn "The --select-plugin option is deprecated and has no effect"
163
155
  end
164
156
 
165
- on :d, "disable-plugin=", "Disable a specific plugin." do |plugin_name|
166
- Pry.plugins[plugin_name].disable!
157
+ on :d, "disable-plugin=", "Disable a specific plugin." do |_plugin_name|
158
+ warn "The --disable-plugin option is deprecated and has no effect"
167
159
  end
168
160
 
169
161
  on "no-plugins", "Suppress loading of plugins." do
170
- Pry.config.should_load_plugins = false
162
+ warn "The --no-plugins option is deprecated and has no effect"
171
163
  end
172
164
 
173
165
  on "plugins", "List installed plugins." do
174
- puts "Installed Plugins:"
175
- puts "--"
176
- Pry.locate_plugins.each do |plugin|
177
- puts "#{plugin.name}".ljust(18) << plugin.spec.summary
178
- end
179
- exit
166
+ warn "The --plugins option is deprecated and has no effect"
167
+ warn "Try using `gem list pry-`"
168
+ Kernel.exit
180
169
  end
181
170
 
182
171
  on "simple-prompt", "Enable simple prompt mode" do
183
- Pry.config.prompt = Pry::Prompt[:simple][:value]
172
+ Pry.config.prompt = Pry::Prompt[:simple]
184
173
  end
185
174
 
186
175
  on "noprompt", "No prompt mode" do
187
- Pry.config.prompt = Pry::Prompt[:none][:value]
176
+ Pry.config.prompt = Pry::Prompt[:none]
188
177
  end
189
178
 
190
179
  on :r, :require=, "`require` a Ruby script at startup" do |file|
191
180
  Pry.config.requires << file
192
181
  end
193
182
 
194
- on :I=, "Add a path to the $LOAD_PATH", as: Array, delimiter: ":" do |load_path|
183
+ on(:I=, "Add a path to the $LOAD_PATH", as: Array, delimiter: ":") do |load_path|
195
184
  load_path.map! do |path|
196
- /\A\.\// =~ path ? path : File.expand_path(path)
185
+ %r{\A\./} =~ path ? path : File.expand_path(path)
197
186
  end
198
187
 
199
188
  $LOAD_PATH.unshift(*load_path)
200
189
  end
201
190
 
202
- on "gem", "Shorthand for -I./lib -rgemname" do |load_path|
191
+ on "gem", "Shorthand for -I./lib -rgemname" do |_load_path|
203
192
  $LOAD_PATH.unshift("./lib")
204
193
  Dir["./lib/*.rb"].each do |file|
205
194
  Pry.config.requires << file
@@ -208,11 +197,11 @@ Pry::CLI.add_options do
208
197
 
209
198
  on :v, :version, "Display the Pry version" do
210
199
  puts "Pry version #{Pry::VERSION} on Ruby #{RUBY_VERSION}"
211
- exit
200
+ Kernel.exit
212
201
  end
213
202
 
214
- on(:c, :context=,
215
- "Start the session in the specified context. Equivalent to `context.pry` in a session.",
203
+ on :c, :context=,
204
+ "Start the session in the specified context. Equivalent to " \
205
+ "`context.pry` in a session.",
216
206
  default: "Pry.toplevel_binding"
217
- )
218
207
  end
@@ -1,29 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'method_source'
4
+
1
5
  class Pry
2
6
  class CodeFile
3
- DEFAULT_EXT = '.rb'
7
+ DEFAULT_EXT = '.rb'.freeze
4
8
 
5
9
  # List of all supported languages.
6
10
  # @return [Hash]
7
11
  EXTENSIONS = {
8
- %w(.py) => :python,
9
- %w(.js) => :javascript,
10
- %w(.css) => :css,
11
- %w(.xml) => :xml,
12
- %w(.php) => :php,
13
- %w(.html) => :html,
14
- %w(.diff) => :diff,
15
- %w(.java) => :java,
16
- %w(.json) => :json,
17
- %w(.c .h) => :c,
18
- %w(.rhtml) => :rhtml,
19
- %w(.yaml .yml) => :yaml,
20
- %w(.cpp .hpp .cc .h .cxx) => :cpp,
21
- %w(.rb .ru .irbrc .gemspec .pryrc .rake) => :ruby,
22
- }
12
+ %w[.py] => :python,
13
+ %w[.js] => :javascript,
14
+ %w[.css] => :css,
15
+ %w[.xml] => :xml,
16
+ %w[.php] => :php,
17
+ %w[.html] => :html,
18
+ %w[.diff] => :diff,
19
+ %w[.java] => :java,
20
+ %w[.json] => :json,
21
+ %w[.c .h] => :c,
22
+ %w[.rhtml] => :rhtml,
23
+ %w[.yaml .yml] => :yaml,
24
+ %w[.cpp .hpp .cc .h .cxx] => :cpp,
25
+ %w[.rb .ru .irbrc .gemspec .pryrc .rake] => :ruby
26
+ }.freeze
23
27
 
24
28
  FILES = {
25
- %w(Gemfile Rakefile Guardfile Capfile) => :ruby
26
- }
29
+ %w[Gemfile Rakefile Guardfile Capfile] => :ruby
30
+ }.freeze
27
31
 
28
32
  # Store the current working directory. This allows show-source etc. to work if
29
33
  # your process has changed directory since boot. [Issue #675]
@@ -58,16 +62,16 @@ class Pry
58
62
  # readable for some reason.
59
63
  # @return [String] absolute path for the given `filename`.
60
64
  def abs_path
61
- code_path.detect { |path| readable?(path) } or
62
- raise MethodSource::SourceNotFoundError,
63
- "Cannot open #{ @filename.inspect } for reading."
65
+ code_path.detect { |path| readable?(path) } ||
66
+ raise(MethodSource::SourceNotFoundError,
67
+ "Cannot open #{@filename.inspect} for reading.")
64
68
  end
65
69
 
66
70
  # @param [String] path
67
71
  # @return [Boolean] if the path, with or without the default ext,
68
72
  # is a readable file then `true`, otherwise `false`.
69
73
  def readable?(path)
70
- File.readable?(path) && !File.directory?(path) or
74
+ File.readable?(path) && !File.directory?(path) ||
71
75
  File.readable?(path << DEFAULT_EXT)
72
76
  end
73
77
 
@@ -80,8 +84,8 @@ class Pry
80
84
  # @param [String] filename
81
85
  # @param [Symbol] default (:unknown) the file type to assume if none could be
82
86
  # detected.
83
- # @return [Symbol, nil] The CodeRay type of a file from its extension, or
84
- # `nil` if `:unknown`.
87
+ # @return [Symbol, nil] The SyntaxHighlighter type of a file from its
88
+ # extension, or `nil` if `:unknown`.
85
89
  def type_from_filename(filename, default = :unknown)
86
90
  _, @code_type = EXTENSIONS.find do |k, _|
87
91
  k.any? { |ext| ext == File.extname(filename) }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  class Code
3
5
  # Represents a range of lines in a code listing.
@@ -20,9 +22,9 @@ class Pry
20
22
 
21
23
  private
22
24
 
23
- def start_line; @start_line; end
25
+ attr_reader :start_line
24
26
 
25
- def end_line; @end_line; end
27
+ attr_reader :end_line
26
28
 
27
29
  # If `end_line` is equal to `nil`, then calculate it from the first
28
30
  # parameter, `start_line`. Otherwise, leave it as it is.
data/lib/pry/code/loc.rb CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  class Code
3
- # Represents a line of code (which may, in fact, contain multiple lines if the
4
- # entirety was eval'd as a single unit following the `edit` command).
5
+ # Represents a line of code (which may, in fact, contain multiple lines if
6
+ # the entirety was eval'd as a single unit following the `edit` command).
5
7
  #
6
8
  # A line of code is a tuple, which consists of a line and a line number. A
7
9
  # `LOC` object's state (namely, the line parameter) can be changed via
@@ -52,7 +54,7 @@ class Pry
52
54
  # @param [Symbol] code_type
53
55
  # @return [void]
54
56
  def colorize(code_type)
55
- tuple[0] = CodeRay.scan(line, code_type).term
57
+ tuple[0] = SyntaxHighlighter.highlight(line, code_type)
56
58
  end
57
59
 
58
60
  # Prepends the line number `lineno` to the `line`.
@@ -61,9 +63,14 @@ class Pry
61
63
  # @return [void]
62
64
  def add_line_number(max_width = 0, color = false)
63
65
  padded = lineno.to_s.rjust(max_width)
64
- colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
66
+ colorized_lineno =
67
+ if color
68
+ Pry::Helpers::BaseHelpers.colorize_code(padded)
69
+ else
70
+ padded
71
+ end
65
72
  properly_padded_line = handle_multiline_entries_from_edit_command(line, max_width)
66
- tuple[0] = "#{ colorized_lineno }: #{ properly_padded_line }"
73
+ tuple[0] = "#{colorized_lineno}: #{properly_padded_line}"
67
74
  end
68
75
 
69
76
  # Prepends a marker "=>" or an empty marker to the +line+.
@@ -74,9 +81,9 @@ class Pry
74
81
  def add_marker(marker_lineno)
75
82
  tuple[0] =
76
83
  if lineno == marker_lineno
77
- " => #{ line }"
84
+ " => #{line}"
78
85
  else
79
- " #{ line }"
86
+ " #{line}"
80
87
  end
81
88
  end
82
89
 
@@ -85,7 +92,7 @@ class Pry
85
92
  # @param [Integer] distance
86
93
  # @return [void]
87
94
  def indent(distance)
88
- tuple[0] = "#{ ' ' * distance }#{ line }"
95
+ tuple[0] = "#{' ' * distance}#{line}"
89
96
  end
90
97
 
91
98
  def handle_multiline_entries_from_edit_command(line, max_width)