pry 0.10.2-i386-mingw32 → 1.0.0.pre1-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. data/.document +2 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +21 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG +503 -0
  6. data/CONTRIBUTORS +55 -0
  7. data/Gemfile +9 -0
  8. data/Guardfile +62 -0
  9. data/LICENSE +2 -2
  10. data/{README.md → README.markdown} +31 -37
  11. data/Rakefile +144 -0
  12. data/TODO +117 -0
  13. data/lib/pry.rb +146 -33
  14. data/lib/pry/cli.rb +13 -35
  15. data/lib/pry/code.rb +63 -24
  16. data/lib/pry/code/loc.rb +2 -2
  17. data/lib/pry/code_object.rb +21 -40
  18. data/lib/pry/command.rb +6 -9
  19. data/lib/pry/command_set.rb +37 -80
  20. data/lib/pry/commands.rb +1 -1
  21. data/lib/pry/commands/amend_line.rb +1 -1
  22. data/lib/pry/commands/bang.rb +1 -1
  23. data/lib/pry/commands/cat.rb +2 -11
  24. data/lib/pry/commands/cat/abstract_formatter.rb +1 -1
  25. data/lib/pry/commands/cat/exception_formatter.rb +7 -6
  26. data/lib/pry/commands/cat/file_formatter.rb +32 -15
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  28. data/lib/pry/commands/cd.rb +3 -14
  29. data/lib/pry/commands/code_collector.rb +4 -4
  30. data/lib/pry/commands/easter_eggs.rb +3 -3
  31. data/lib/pry/commands/edit.rb +22 -10
  32. data/lib/pry/commands/edit/exception_patcher.rb +1 -1
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +2 -0
  34. data/lib/pry/{method/patcher.rb → commands/edit/method_patcher.rb} +37 -40
  35. data/lib/pry/commands/find_method.rb +22 -16
  36. data/lib/pry/commands/gem_install.rb +2 -5
  37. data/lib/pry/commands/gem_open.rb +1 -1
  38. data/lib/pry/commands/gist.rb +11 -10
  39. data/lib/pry/commands/help.rb +14 -14
  40. data/lib/pry/commands/hist.rb +5 -24
  41. data/lib/pry/commands/ls.rb +287 -56
  42. data/lib/pry/commands/play.rb +10 -44
  43. data/lib/pry/commands/pry_backtrace.rb +2 -1
  44. data/lib/pry/commands/raise_up.rb +1 -1
  45. data/lib/pry/commands/reload_code.rb +15 -31
  46. data/lib/pry/commands/ri.rb +3 -7
  47. data/lib/pry/commands/shell_command.rb +12 -17
  48. data/lib/pry/commands/shell_mode.rb +2 -2
  49. data/lib/pry/commands/show_doc.rb +0 -5
  50. data/lib/pry/commands/show_info.rb +10 -11
  51. data/lib/pry/commands/show_source.rb +3 -15
  52. data/lib/pry/commands/simple_prompt.rb +1 -1
  53. data/lib/pry/commands/toggle_color.rb +4 -8
  54. data/lib/pry/commands/whereami.rb +10 -18
  55. data/lib/pry/completion.rb +293 -0
  56. data/lib/pry/config.rb +233 -20
  57. data/lib/pry/core_extensions.rb +19 -29
  58. data/lib/pry/custom_completions.rb +6 -0
  59. data/lib/pry/editor.rb +103 -109
  60. data/lib/pry/helpers/base_helpers.rb +109 -22
  61. data/lib/pry/helpers/command_helpers.rb +8 -10
  62. data/lib/pry/helpers/documentation_helpers.rb +2 -1
  63. data/lib/pry/helpers/text.rb +5 -4
  64. data/lib/pry/history.rb +10 -21
  65. data/lib/pry/history_array.rb +0 -5
  66. data/lib/pry/hooks.rb +29 -9
  67. data/lib/pry/indent.rb +10 -5
  68. data/lib/pry/method.rb +86 -81
  69. data/lib/pry/method/weird_method_locator.rb +2 -4
  70. data/lib/pry/module_candidate.rb +14 -5
  71. data/lib/pry/pager.rb +48 -193
  72. data/lib/pry/plugins.rb +2 -2
  73. data/lib/pry/pry_class.rb +193 -104
  74. data/lib/pry/pry_instance.rb +154 -152
  75. data/lib/pry/rbx_method.rb +13 -0
  76. data/lib/pry/rbx_path.rb +1 -1
  77. data/lib/pry/repl.rb +14 -17
  78. data/lib/pry/repl_file_loader.rb +3 -8
  79. data/lib/pry/rubygem.rb +3 -3
  80. data/lib/pry/terminal.rb +3 -4
  81. data/lib/pry/test/helper.rb +11 -6
  82. data/lib/pry/version.rb +1 -1
  83. data/lib/pry/wrapped_module.rb +56 -49
  84. data/man/pry.1 +195 -0
  85. data/man/pry.1.html +204 -0
  86. data/man/pry.1.ronn +141 -0
  87. data/pry.gemspec +31 -0
  88. data/spec/Procfile +3 -0
  89. data/spec/cli_spec.rb +78 -0
  90. data/spec/code_object_spec.rb +277 -0
  91. data/spec/code_spec.rb +219 -0
  92. data/spec/command_helpers_spec.rb +29 -0
  93. data/spec/command_integration_spec.rb +562 -0
  94. data/spec/command_set_spec.rb +627 -0
  95. data/spec/command_spec.rb +821 -0
  96. data/spec/commands/amend_line_spec.rb +247 -0
  97. data/spec/commands/bang_spec.rb +18 -0
  98. data/spec/commands/cat_spec.rb +164 -0
  99. data/spec/commands/cd_spec.rb +250 -0
  100. data/spec/commands/disable_pry_spec.rb +25 -0
  101. data/spec/commands/edit_spec.rb +725 -0
  102. data/spec/commands/exit_all_spec.rb +27 -0
  103. data/spec/commands/exit_program_spec.rb +19 -0
  104. data/spec/commands/exit_spec.rb +28 -0
  105. data/spec/commands/find_method_spec.rb +70 -0
  106. data/spec/commands/gem_list_spec.rb +26 -0
  107. data/spec/commands/gist_spec.rb +79 -0
  108. data/spec/commands/help_spec.rb +56 -0
  109. data/spec/commands/hist_spec.rb +172 -0
  110. data/spec/commands/jump_to_spec.rb +15 -0
  111. data/spec/commands/ls_spec.rb +189 -0
  112. data/spec/commands/play_spec.rb +136 -0
  113. data/spec/commands/raise_up_spec.rb +56 -0
  114. data/spec/commands/save_file_spec.rb +177 -0
  115. data/spec/commands/show_doc_spec.rb +488 -0
  116. data/spec/commands/show_input_spec.rb +17 -0
  117. data/spec/commands/show_source_spec.rb +760 -0
  118. data/spec/commands/whereami_spec.rb +203 -0
  119. data/spec/completion_spec.rb +221 -0
  120. data/spec/control_d_handler_spec.rb +62 -0
  121. data/spec/documentation_helper_spec.rb +73 -0
  122. data/spec/editor_spec.rb +79 -0
  123. data/spec/exception_whitelist_spec.rb +21 -0
  124. data/spec/fixtures/candidate_helper1.rb +11 -0
  125. data/spec/fixtures/candidate_helper2.rb +8 -0
  126. data/spec/fixtures/example.erb +5 -0
  127. data/spec/fixtures/example_nesting.rb +33 -0
  128. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  129. data/spec/fixtures/testlinkrc +2 -0
  130. data/spec/fixtures/testrc +2 -0
  131. data/spec/fixtures/testrcbad +2 -0
  132. data/spec/fixtures/whereami_helper.rb +6 -0
  133. data/spec/helper.rb +35 -0
  134. data/spec/helpers/bacon.rb +86 -0
  135. data/spec/helpers/mock_pry.rb +44 -0
  136. data/spec/helpers/repl_tester.rb +112 -0
  137. data/spec/helpers/table_spec.rb +105 -0
  138. data/spec/history_array_spec.rb +67 -0
  139. data/spec/hooks_spec.rb +522 -0
  140. data/spec/indent_spec.rb +301 -0
  141. data/spec/method_spec.rb +482 -0
  142. data/spec/prompt_spec.rb +61 -0
  143. data/spec/pry_defaults_spec.rb +420 -0
  144. data/spec/pry_history_spec.rb +69 -0
  145. data/spec/pry_output_spec.rb +95 -0
  146. data/spec/pry_repl_spec.rb +86 -0
  147. data/spec/pry_spec.rb +394 -0
  148. data/spec/pryrc_spec.rb +97 -0
  149. data/spec/run_command_spec.rb +25 -0
  150. data/spec/sticky_locals_spec.rb +147 -0
  151. data/spec/syntax_checking_spec.rb +81 -0
  152. data/spec/wrapped_module_spec.rb +261 -0
  153. data/wiki/Customizing-pry.md +397 -0
  154. data/wiki/Home.md +4 -0
  155. metadata +272 -61
  156. checksums.yaml +0 -7
  157. data/CHANGELOG.md +0 -714
  158. data/lib/pry/code/code_file.rb +0 -103
  159. data/lib/pry/color_printer.rb +0 -55
  160. data/lib/pry/commands/change_inspector.rb +0 -27
  161. data/lib/pry/commands/change_prompt.rb +0 -26
  162. data/lib/pry/commands/list_inspectors.rb +0 -35
  163. data/lib/pry/commands/list_prompts.rb +0 -35
  164. data/lib/pry/commands/ls/constants.rb +0 -47
  165. data/lib/pry/commands/ls/formatter.rb +0 -49
  166. data/lib/pry/commands/ls/globals.rb +0 -48
  167. data/lib/pry/commands/ls/grep.rb +0 -21
  168. data/lib/pry/commands/ls/instance_vars.rb +0 -39
  169. data/lib/pry/commands/ls/interrogatable.rb +0 -18
  170. data/lib/pry/commands/ls/jruby_hacks.rb +0 -49
  171. data/lib/pry/commands/ls/local_names.rb +0 -35
  172. data/lib/pry/commands/ls/local_vars.rb +0 -39
  173. data/lib/pry/commands/ls/ls_entity.rb +0 -70
  174. data/lib/pry/commands/ls/methods.rb +0 -57
  175. data/lib/pry/commands/ls/methods_helper.rb +0 -46
  176. data/lib/pry/commands/ls/self_methods.rb +0 -32
  177. data/lib/pry/commands/watch_expression.rb +0 -105
  178. data/lib/pry/commands/watch_expression/expression.rb +0 -38
  179. data/lib/pry/config/behavior.rb +0 -139
  180. data/lib/pry/config/convenience.rb +0 -25
  181. data/lib/pry/config/default.rb +0 -161
  182. data/lib/pry/exceptions.rb +0 -78
  183. data/lib/pry/input_completer.rb +0 -242
  184. data/lib/pry/input_lock.rb +0 -132
  185. data/lib/pry/inspector.rb +0 -27
  186. data/lib/pry/last_exception.rb +0 -61
  187. data/lib/pry/object_path.rb +0 -82
  188. data/lib/pry/output.rb +0 -50
  189. data/lib/pry/prompt.rb +0 -26
@@ -1,6 +1,5 @@
1
1
  require 'pry/code/loc'
2
2
  require 'pry/code/code_range'
3
- require 'pry/code/code_file'
4
3
 
5
4
  class Pry
6
5
  class << self
@@ -30,6 +29,26 @@ class Pry
30
29
  # arbitrary chaining of formatting methods without mutating the original
31
30
  # object.
32
31
  class Code
32
+
33
+ # List of all supported languages.
34
+ # @return [Hash]
35
+ EXTENSIONS = {
36
+ %w(.py) => :python,
37
+ %w(.js) => :javascript,
38
+ %w(.css) => :css,
39
+ %w(.xml) => :xml,
40
+ %w(.php) => :php,
41
+ %w(.html) => :html,
42
+ %w(.diff) => :diff,
43
+ %w(.java) => :java,
44
+ %w(.json) => :json,
45
+ %w(.c .h) => :c,
46
+ %w(.rhtml) => :rhtml,
47
+ %w(.yaml .yml) => :yaml,
48
+ %w(.cpp .hpp .cc .h cxx) => :cpp,
49
+ %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
50
+ }
51
+
33
52
  class << self
34
53
  include MethodSource::CodeHelpers
35
54
 
@@ -39,9 +58,13 @@ class Pry
39
58
  # @param [String] filename The name of a file, or "(pry)".
40
59
  # @param [Symbol] code_type The type of code the file contains.
41
60
  # @return [Code]
42
- def from_file(filename, code_type = nil)
43
- code_file = CodeFile.new(filename, code_type)
44
- new(code_file.code, 1, code_file.code_type)
61
+ def from_file(filename, code_type = type_from_filename(filename))
62
+ code = if filename == Pry.eval_path
63
+ Pry.line_buffer.drop(1)
64
+ else
65
+ File.read(abs_path(filename))
66
+ end
67
+ new(code, 1, code_type)
45
68
  end
46
69
 
47
70
  # Instantiate a `Code` object containing code extracted from a
@@ -71,6 +94,35 @@ class Pry
71
94
  start_line ||= candidate.line
72
95
  new(candidate.source, start_line, :ruby)
73
96
  end
97
+
98
+ protected
99
+
100
+ # Guess the CodeRay type of a file from its extension, or nil if
101
+ # unknown.
102
+ #
103
+ # @param [String] filename
104
+ # @param [Symbol] default (:ruby) the file type to assume if none could be
105
+ # detected.
106
+ # @return [Symbol, nil]
107
+ def type_from_filename(filename, default = :ruby)
108
+ _, type = Pry::Code::EXTENSIONS.find do |k, _|
109
+ k.any? { |ext| ext == File.extname(filename) }
110
+ end
111
+
112
+ type || default
113
+ end
114
+
115
+ # @param [String] filename
116
+ # @raise [MethodSource::SourceNotFoundError] if the +filename+ is not
117
+ # readable for some reason.
118
+ # @return [String] absolute path for the given +filename+.
119
+ def abs_path(filename)
120
+ abs_path = [File.expand_path(filename, Dir.pwd),
121
+ File.expand_path(filename, Pry::INITIAL_PWD)
122
+ ].detect { |path| File.readable?(path) }
123
+ abs_path or raise MethodSource::SourceNotFoundError,
124
+ "Cannot open #{filename.inspect} for reading."
125
+ end
74
126
  end
75
127
 
76
128
  # @return [Symbol] The type of code stored in this wrapper.
@@ -145,7 +197,7 @@ class Pry
145
197
  if start_line >= 0
146
198
  @lines.index { |loc| loc.lineno >= start_line } || @lines.length
147
199
  else
148
- [@lines.length + start_line, 0].max
200
+ @lines.length + start_line
149
201
  end
150
202
 
151
203
  alter do
@@ -253,27 +305,14 @@ class Pry
253
305
  # @return [String] a formatted representation (based on the configuration of
254
306
  # the object).
255
307
  def to_s
256
- print_to_output("", false)
257
- end
258
-
259
- # @return [String] a (possibly highlighted) copy of the source code.
260
- def highlighted
261
- print_to_output("", true)
262
- end
263
-
264
- # Writes a formatted representation (based on the configuration of the
265
- # object) to the given output, which must respond to `#<<`.
266
- def print_to_output(output, color=false)
267
- @lines.each do |loc|
308
+ @lines.map { |loc|
268
309
  loc = loc.dup
269
- loc.colorize(@code_type) if color
270
- loc.add_line_number(max_lineno_width, color) if @with_line_numbers
310
+ loc.colorize(@code_type) if Pry.color
311
+ loc.add_line_number(max_lineno_width) if @with_line_numbers
271
312
  loc.add_marker(@marker_lineno) if @with_marker
272
313
  loc.indent(@indentation_num) if @with_indentation
273
- output << loc.line
274
- output << "\n"
275
- end
276
- output
314
+ loc.line
315
+ }.join("\n") + "\n"
277
316
  end
278
317
 
279
318
  # Get the comment that describes the expression on the given line number.
@@ -305,7 +344,7 @@ class Pry
305
344
  #
306
345
  # @return [String]
307
346
  def raw
308
- @lines.map(&:line).join("\n") << "\n"
347
+ @lines.map(&:line).join("\n") + "\n"
309
348
  end
310
349
 
311
350
  # Return the number of lines stored.
@@ -59,9 +59,9 @@ class Pry
59
59
  #
60
60
  # @param [Integer] max_width
61
61
  # @return [void]
62
- def add_line_number(max_width = 0, color = false)
62
+ def add_line_number(max_width = 0)
63
63
  padded = lineno.to_s.rjust(max_width)
64
- colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
64
+ colorized_lineno = Pry::Helpers::BaseHelpers.colorize_code(padded)
65
65
  tuple[0] = "#{ colorized_lineno }: #{ line }"
66
66
  end
67
67
 
@@ -1,23 +1,4 @@
1
1
  class Pry
2
-
3
- # This class is responsible for taking a string (identifying a
4
- # command/class/method/etc) and returning the relevant type of object.
5
- # For example, if the user looks up "show-source" then a
6
- # `Pry::Command` will be returned. Alternatively, if the user passes in "Pry#repl" then
7
- # a `Pry::Method` object will be returned.
8
- #
9
- # The `CodeObject.lookup` method is responsible for 1. figuring out what kind of
10
- # object the user wants (applying precedence rules in doing so -- i.e methods
11
- # get precedence over commands with the same name) and 2. Returning
12
- # the appropriate object. If the user fails to provide a string
13
- # identifer for the object (i.e they pass in `nil` or "") then the
14
- # object looked up will be the 'current method' or 'current class'
15
- # associated with the Binding.
16
- #
17
- # TODO: This class is a clusterfuck. We need a much more robust
18
- # concept of what a "Code Object" really is. Currently
19
- # commands/classes/candidates/methods and so on just share a very
20
- # ill-defined interface.
21
2
  class CodeObject
22
3
  module Helpers
23
4
  # we need this helper as some Pry::Method objects can wrap Procs
@@ -52,7 +33,7 @@ class Pry
52
33
 
53
34
  attr_accessor :str
54
35
  attr_accessor :target
55
- attr_accessor :_pry_
36
+ attr_accessor :pry
56
37
  attr_accessor :super_level
57
38
 
58
39
  def initialize(str, _pry_, options={})
@@ -61,7 +42,7 @@ class Pry
61
42
  }.merge!(options)
62
43
 
63
44
  @str = str
64
- @_pry_ = _pry_
45
+ @pry = _pry_
65
46
  @target = _pry_.current_context
66
47
  @super_level = options[:super]
67
48
  end
@@ -69,24 +50,18 @@ class Pry
69
50
  def command_lookup
70
51
  # TODO: just make it so find_command_by_match_or_listing doesn't
71
52
  # raise?
72
- _pry_.commands.find_command_by_match_or_listing(str) rescue nil
53
+ pry.commands.find_command_by_match_or_listing(str) rescue nil
73
54
  end
74
55
 
75
- # when no paramter is given (i.e CodeObject.lookup(nil)), then we
76
- # lookup the 'current object' from the binding.
77
56
  def empty_lookup
78
57
  return nil if str && !str.empty?
79
58
 
80
- obj = if internal_binding?(target)
81
- mod = target_self.is_a?(Module) ? target_self : target_self.class
82
- Pry::WrappedModule(mod)
83
- else
84
- Pry::Method.from_binding(target)
85
- end
86
-
87
- # respect the super level (i.e user might have specified a
88
- # --super flag to show-source)
89
- lookup_super(obj, super_level)
59
+ if internal_binding?(target)
60
+ mod = target_self.is_a?(Module) ? target_self : target_self.class
61
+ Pry::WrappedModule(mod)
62
+ else
63
+ Pry::Method.from_binding(target)
64
+ end
90
65
  end
91
66
 
92
67
  # lookup variables and constants and `self` that are not modules
@@ -112,12 +87,17 @@ class Pry
112
87
  end
113
88
 
114
89
  def method_or_class_lookup
115
- obj = case str
116
- when /\S+\(\)\z/
117
- Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target)
118
- else
119
- Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
120
- end
90
+ # we need this here because stupid Pry::Method.from_str() does a
91
+ # Pry::Method.from_binding when str is nil.
92
+ # Once we refactor Pry::Method.from_str() so it doesnt lookup
93
+ # from bindings, we can get rid of this check
94
+ return nil if str.to_s.empty?
95
+
96
+ obj = if str =~ /::(?:\S+)\Z/
97
+ Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
98
+ else
99
+ Pry::Method.from_str(str,target) || Pry::WrappedModule.from_str(str, target)
100
+ end
121
101
 
122
102
  lookup_super(obj, super_level)
123
103
  end
@@ -128,6 +108,7 @@ class Pry
128
108
  [::Proc, ::Method, ::UnboundMethod].any? { |o| obj.is_a?(o) }
129
109
  end
130
110
 
111
+
131
112
  # Returns true if `str` looks like a method, i.e Klass#method
132
113
  # We need to consider this case because method lookups should fall
133
114
  # through to the `method_or_class_lookup()` method but a
@@ -175,7 +175,7 @@ class Pry
175
175
  end
176
176
 
177
177
  def command_regex
178
- pr = Pry.respond_to?(:config) ? Pry.config.command_prefix : ""
178
+ pr = defined?(Pry.config.command_prefix) ? Pry.config.command_prefix : ""
179
179
  prefix = convert_to_regex(pr)
180
180
  prefix = "(?:#{prefix})?" unless options[:use_prefix]
181
181
 
@@ -194,7 +194,6 @@ class Pry
194
194
  # The group in which the command should be displayed in "help" output.
195
195
  # This is usually auto-generated from directory naming, but it can be
196
196
  # manually overridden if necessary.
197
- # Group should not be changed once it is initialized.
198
197
  def group(name=nil)
199
198
  @group ||= if name
200
199
  name
@@ -243,13 +242,12 @@ class Pry
243
242
  # @example
244
243
  # run "amend-line", "5", 'puts "hello world"'
245
244
  def run(command_string, *args)
246
- command_string = _pry_.config.command_prefix.to_s + command_string
247
245
  complete_string = "#{command_string} #{args.join(" ")}".rstrip
248
246
  command_set.process_line(complete_string, context)
249
247
  end
250
248
 
251
249
  def commands
252
- command_set.to_hash
250
+ command_set.commands
253
251
  end
254
252
 
255
253
  def text
@@ -285,7 +283,7 @@ class Pry
285
283
  # state.my_state = "my state" # this will not conflict with any
286
284
  # # `state.my_state` used in another command.
287
285
  def state
288
- _pry_.command_state[match] ||= Pry::Config.from_hash({})
286
+ _pry_.command_state[match] ||= OpenStruct.new
289
287
  end
290
288
 
291
289
  # Revaluate the string (str) and perform interpolation.
@@ -309,7 +307,8 @@ class Pry
309
307
  collision_type ||= 'local-variable' if arg_string.match(%r{\A\s*[-+*/%&|^]*=})
310
308
 
311
309
  if collision_type
312
- output.puts "#{text.bold('WARNING:')} Calling Pry command '#{command_match}', which conflicts with a #{collision_type}.\n\n"
310
+ output.puts "#{text.bold('WARNING:')} Calling Pry command '#{command_match}'," +
311
+ "which conflicts with a #{collision_type}.\n\n"
313
312
  end
314
313
  rescue Pry::RescuableException
315
314
  end
@@ -439,9 +438,7 @@ class Pry
439
438
  #
440
439
  # @param [String] search The line typed so far
441
440
  # @return [Array<String>] Completion words
442
- def complete(search)
443
- []
444
- end
441
+ def complete(search); Bond::DefaultMission.completions; end
445
442
 
446
443
  private
447
444
 
@@ -10,15 +10,19 @@ class Pry
10
10
  class CommandSet
11
11
  include Enumerable
12
12
  include Pry::Helpers::BaseHelpers
13
+
14
+ attr_reader :commands
13
15
  attr_reader :helper_module
14
16
 
15
- # @param [Array<Commandset>] imported_sets
16
- # Sets which will be imported automatically
17
+ # @param [Array<CommandSet>] imported_sets Sets which will be imported
18
+ # automatically
17
19
  # @yield Optional block run to define commands
18
20
  def initialize(*imported_sets, &block)
19
21
  @commands = {}
20
22
  @helper_module = Module.new
23
+
21
24
  import(*imported_sets)
25
+
22
26
  instance_eval(&block) if block
23
27
  end
24
28
 
@@ -79,7 +83,7 @@ class Pry
79
83
  description, options = ["No description.", description] if description.is_a?(Hash)
80
84
  options = Pry::Command.default_options(match).merge!(options)
81
85
 
82
- @commands[match] = Pry::BlockCommand.subclass(match, description, options, helper_module, &block)
86
+ commands[match] = Pry::BlockCommand.subclass(match, description, options, helper_module, &block)
83
87
  end
84
88
  alias_method :command, :block_command
85
89
 
@@ -111,9 +115,9 @@ class Pry
111
115
  description, options = ["No description.", description] if description.is_a?(Hash)
112
116
  options = Pry::Command.default_options(match).merge!(options)
113
117
 
114
- @commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block)
115
- @commands[match].class_eval(&block)
116
- @commands[match]
118
+ commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block)
119
+ commands[match].class_eval(&block)
120
+ commands[match]
117
121
  end
118
122
 
119
123
  # Execute a block of code before a command is invoked. The block also
@@ -122,7 +126,7 @@ class Pry
122
126
  # @param [String, Regexp] search The match or listing of the command.
123
127
  # @yield The block to be run before the command.
124
128
  # @example Display parameter before invoking command
125
- # Pry.config.commands.before_command("whereami") do |n|
129
+ # Pry.commands.before_command("whereami") do |n|
126
130
  # output.puts "parameter passed was #{n}"
127
131
  # end
128
132
  def before_command(search, &block)
@@ -136,7 +140,7 @@ class Pry
136
140
  # @param [String, Regexp] search The match or listing of the command.
137
141
  # @yield The block to be run after the command.
138
142
  # @example Display text 'command complete' after invoking command
139
- # Pry.config.commands.after_command("whereami") do |n|
143
+ # Pry.commands.after_command("whereami") do |n|
140
144
  # output.puts "command complete!"
141
145
  # end
142
146
  def after_command(search, &block)
@@ -148,12 +152,18 @@ class Pry
148
152
  @commands.each(&block)
149
153
  end
150
154
 
155
+ # Add a given command object to this set.
156
+ # @param [Command] command The subclass of Pry::Command you wish to add.
157
+ def add_command(command)
158
+ commands[command.match] = command
159
+ end
160
+
151
161
  # Removes some commands from the set
152
162
  # @param [Array<String>] searches the matches or listings of the commands to remove
153
163
  def delete(*searches)
154
164
  searches.each do |search|
155
165
  cmd = find_command_by_match_or_listing(search)
156
- @commands.delete cmd.match
166
+ commands.delete cmd.match
157
167
  end
158
168
  end
159
169
 
@@ -163,7 +173,7 @@ class Pry
163
173
  # @return [Pry::CommandSet] Returns the reciever (a command set).
164
174
  def import(*sets)
165
175
  sets.each do |set|
166
- @commands.merge! set.to_hash
176
+ commands.merge! set.commands
167
177
  helper_module.send :include, set.helper_module
168
178
  end
169
179
  self
@@ -177,7 +187,7 @@ class Pry
177
187
  helper_module.send :include, set.helper_module
178
188
  matches.each do |match|
179
189
  cmd = set.find_command_by_match_or_listing(match)
180
- @commands[cmd.match] = cmd
190
+ commands[cmd.match] = cmd
181
191
  end
182
192
  self
183
193
  end
@@ -186,8 +196,8 @@ class Pry
186
196
  # of the command to retrieve.
187
197
  # @return [Command] The command object matched.
188
198
  def find_command_by_match_or_listing(match_or_listing)
189
- cmd = (@commands[match_or_listing] ||
190
- Pry::Helpers::BaseHelpers.find_command(match_or_listing, @commands))
199
+ cmd = (commands[match_or_listing] ||
200
+ Pry::Helpers::BaseHelpers.find_command(match_or_listing, commands))
191
201
  cmd or raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!"
192
202
  end
193
203
 
@@ -246,11 +256,11 @@ class Pry
246
256
  :description => cmd.description
247
257
  }.merge!(options)
248
258
 
249
- @commands[new_match] = cmd.dup
250
- @commands[new_match].match = new_match
251
- @commands[new_match].description = options.delete(:description)
252
- @commands[new_match].options.merge!(options)
253
- @commands.delete(cmd.match)
259
+ commands[new_match] = cmd.dup
260
+ commands[new_match].match = new_match
261
+ commands[new_match].description = options.delete(:description)
262
+ commands[new_match].options.merge!(options)
263
+ commands.delete(cmd.match)
254
264
  end
255
265
 
256
266
  def disabled_command(name_of_disabled_command, message, matcher=name_of_disabled_command)
@@ -299,71 +309,18 @@ class Pry
299
309
  end
300
310
 
301
311
 
302
- # @return [Array]
303
- # The list of commands provided by the command set.
312
+ # @return [Array] The list of commands provided by the command set.
304
313
  def list_commands
305
- @commands.keys
306
- end
307
- alias_method :keys, :list_commands
308
-
309
- def to_hash
310
- @commands.dup
314
+ commands.keys
311
315
  end
312
- alias_method :to_h, :to_hash
313
316
 
314
317
  # Find a command that matches the given line
315
- # @param [String] pattern The line that might be a command invocation
318
+ # @param [String] val The line that might be a command invocation
316
319
  # @return [Pry::Command, nil]
317
- def [](pattern)
318
- @commands.values.select do |command|
319
- command.matches?(pattern)
320
- end.sort_by do |command|
321
- command.match_score(pattern)
322
- end.last
323
- end
324
- alias_method :find_command, :[]
325
-
326
- #
327
- # Re-assign the command found at _pattern_ with _command_.
328
- #
329
- # @param [Regexp, String] pattern
330
- # The command to add or replace(found at _pattern_).
331
- #
332
- # @param [Pry::Command] command
333
- # The command to add.
334
- #
335
- # @return [Pry::Command]
336
- # Returns the new command (matched with "pattern".)
337
- #
338
- # @example
339
- # Pry.config.commands["help"] = MyHelpCommand
340
- #
341
- def []=(pattern, command)
342
- if command.equal?(nil)
343
- return @commands.delete(pattern)
344
- end
345
- unless Class === command && command < Pry::Command
346
- raise TypeError, "command is not a subclass of Pry::Command"
347
- end
348
- bind_command_to_pattern = pattern != command.match
349
- if bind_command_to_pattern
350
- command_copy = command.dup
351
- command_copy.match = pattern
352
- @commands[pattern] = command_copy
353
- else
354
- @commands[pattern] = command
355
- end
356
- end
357
-
358
- #
359
- # Add a command to set.
360
- #
361
- # @param [Command] command
362
- # a subclass of Pry::Command.
363
- #
364
- def add_command(command)
365
- self[command.match] = command
320
+ def find_command(val)
321
+ commands.values.select{ |c| c.matches?(val) }.sort_by{ |c| c.match_score(val) }.last
366
322
  end
323
+ alias_method :[], :find_command
367
324
 
368
325
  # Find the command that the user might be trying to refer to.
369
326
  # @param [String] search The user's search.
@@ -399,7 +356,7 @@ class Pry
399
356
 
400
357
  # @private (used for testing)
401
358
  def run_command(context, match, *args)
402
- command = @commands[match] or raise NoCommandError.new(match, self)
359
+ command = commands[match] or raise NoCommandError.new(match, self)
403
360
  command.new(context).call_safely(*args)
404
361
  end
405
362
 
@@ -411,9 +368,9 @@ class Pry
411
368
  if command = find_command(search)
412
369
  command.new(context).complete(search)
413
370
  else
414
- @commands.keys.select do |key|
371
+ commands.keys.select do |key|
415
372
  String === key && key.start_with?(search)
416
- end.map{ |key| key + " " }
373
+ end.map{ |key| key + " " } + Bond::DefaultMission.completions
417
374
  end
418
375
  end
419
376
  end