pry 0.9.9.6pre2-i386-mswin32 → 0.9.10-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/CHANGELOG +41 -0
  2. data/CONTRIBUTORS +27 -26
  3. data/README.markdown +4 -4
  4. data/Rakefile +2 -2
  5. data/lib/pry.rb +25 -19
  6. data/lib/pry/cli.rb +31 -10
  7. data/lib/pry/code.rb +41 -83
  8. data/lib/pry/command.rb +87 -76
  9. data/lib/pry/command_set.rb +13 -20
  10. data/lib/pry/completion.rb +139 -121
  11. data/lib/pry/config.rb +4 -0
  12. data/lib/pry/core_extensions.rb +88 -31
  13. data/lib/pry/default_commands/cd.rb +31 -8
  14. data/lib/pry/default_commands/context.rb +4 -58
  15. data/lib/pry/default_commands/easter_eggs.rb +1 -1
  16. data/lib/pry/default_commands/editing.rb +21 -14
  17. data/lib/pry/default_commands/find_method.rb +5 -7
  18. data/lib/pry/default_commands/gist.rb +187 -0
  19. data/lib/pry/default_commands/hist.rb +6 -6
  20. data/lib/pry/default_commands/input_and_output.rb +73 -129
  21. data/lib/pry/default_commands/introspection.rb +107 -52
  22. data/lib/pry/default_commands/ls.rb +1 -1
  23. data/lib/pry/default_commands/misc.rb +0 -5
  24. data/lib/pry/default_commands/whereami.rb +92 -0
  25. data/lib/pry/helpers/base_helpers.rb +6 -1
  26. data/lib/pry/helpers/command_helpers.rb +30 -9
  27. data/lib/pry/helpers/documentation_helpers.rb +7 -7
  28. data/lib/pry/helpers/options_helpers.rb +1 -1
  29. data/lib/pry/helpers/text.rb +7 -9
  30. data/lib/pry/history.rb +15 -2
  31. data/lib/pry/hooks.rb +1 -1
  32. data/lib/pry/indent.rb +17 -10
  33. data/lib/pry/method.rb +35 -19
  34. data/lib/pry/module_candidate.rb +130 -0
  35. data/lib/pry/pry_class.rb +54 -22
  36. data/lib/pry/pry_instance.rb +71 -14
  37. data/lib/pry/repl_file_loader.rb +80 -0
  38. data/lib/pry/version.rb +1 -1
  39. data/lib/pry/wrapped_module.rb +121 -142
  40. data/pry.gemspec +13 -13
  41. data/test/candidate_helper1.rb +11 -0
  42. data/test/candidate_helper2.rb +8 -0
  43. data/test/helper.rb +16 -0
  44. data/test/test_code.rb +1 -1
  45. data/test/test_command.rb +364 -270
  46. data/test/test_command_integration.rb +235 -267
  47. data/test/test_completion.rb +36 -0
  48. data/test/test_control_d_handler.rb +45 -0
  49. data/test/test_default_commands/example.erb +5 -0
  50. data/test/test_default_commands/test_cd.rb +316 -11
  51. data/test/test_default_commands/test_context.rb +143 -192
  52. data/test/test_default_commands/test_documentation.rb +81 -14
  53. data/test/test_default_commands/test_find_method.rb +10 -2
  54. data/test/test_default_commands/test_input.rb +102 -111
  55. data/test/test_default_commands/test_introspection.rb +17 -12
  56. data/test/test_default_commands/test_ls.rb +8 -6
  57. data/test/test_default_commands/test_shell.rb +18 -15
  58. data/test/test_default_commands/test_show_source.rb +170 -44
  59. data/test/test_exception_whitelist.rb +6 -2
  60. data/test/test_hooks.rb +32 -0
  61. data/test/test_input_stack.rb +19 -16
  62. data/test/test_method.rb +0 -4
  63. data/test/test_prompt.rb +60 -0
  64. data/test/test_pry.rb +23 -31
  65. data/test/test_pry_defaults.rb +75 -57
  66. data/test/test_syntax_checking.rb +12 -11
  67. data/test/test_wrapped_module.rb +103 -0
  68. metadata +72 -26
@@ -0,0 +1,187 @@
1
+ class Pry
2
+ module DefaultCommands
3
+ Gist = Pry::CommandSet.new do
4
+ create_command "gist", "Gist a method or expression history to GitHub.", :requires_gem => "jist" do
5
+ include Pry::Helpers::DocumentationHelpers
6
+
7
+ banner <<-USAGE
8
+ Usage: gist [OPTIONS] [METH]
9
+ Gist method (doc or source) or input expression to GitHub.
10
+
11
+ If you'd like to permanently associate your gists with your GitHub account run `gist --login`.
12
+
13
+ e.g: gist -m my_method # gist the method my_method
14
+ e.g: gist -d my_method # gist the documentation for my_method
15
+ e.g: gist -i 1..10 # gist the input expressions from 1 to 10
16
+ e.g: gist -k show-method # gist the command show-method
17
+ e.g: gist -c Pry # gist the Pry class
18
+ e.g: gist -m hello_world --lines 2..-2 # gist from lines 2 to the second-last of the hello_world method
19
+ e.g: gist -m my_method --clip # Copy my_method source to clipboard, do not gist it.
20
+ USAGE
21
+
22
+ command_options :shellwords => false
23
+
24
+ attr_accessor :content
25
+ attr_accessor :filename
26
+
27
+ def setup
28
+ require 'jist'
29
+ self.content = ""
30
+ self.filename = "a.rb"
31
+ end
32
+
33
+ def options(opt)
34
+ opt.on :login, "Authenticate the jist gem with GitHub"
35
+ opt.on :m, :method, "Gist a method's source.", :argument => true do |meth_name|
36
+ meth = get_method_or_raise(meth_name, target, {})
37
+ self.content << meth.source << "\n"
38
+ self.filename = meth.source_file
39
+ end
40
+ opt.on :d, :doc, "Gist a method's documentation.", :argument => true do |meth_name|
41
+ meth = get_method_or_raise(meth_name, target, {})
42
+ text.no_color do
43
+ self.content << process_comment_markup(meth.doc) << "\n"
44
+ end
45
+ self.filename = meth.source_file + ".doc"
46
+ end
47
+ opt.on :k, :command, "Gist a command's source.", :argument => true do |command_name|
48
+ command = find_command(command_name)
49
+ block = Pry::Method.new(command.block)
50
+ self.content << block.source << "\n"
51
+ self.filename = block.source_file
52
+ end
53
+ opt.on :c, :class, "Gist a class or module's source.", :argument => true do |class_name|
54
+ mod = Pry::WrappedModule.from_str(class_name, target)
55
+ self.content << mod.source << "\n"
56
+ self.filename = mod.source_file
57
+ end
58
+ opt.on :var, "Gist a variable's content.", :argument => true do |variable_name|
59
+ begin
60
+ obj = target.eval(variable_name)
61
+ rescue Pry::RescuableException
62
+ raise CommandError, "Gist failed: Invalid variable name: #{variable_name}"
63
+ end
64
+
65
+ self.content << Pry.config.gist.inspecter.call(obj) << "\n"
66
+ end
67
+ opt.on :hist, "Gist a range of Readline history lines.", :optional_argument => true, :as => Range, :default => -20..-1 do |range|
68
+ h = Pry.history.to_a
69
+ self.content << h[one_index_range(convert_to_range(range))].join("\n") << "\n"
70
+ end
71
+
72
+ opt.on :f, :file, "Gist a file.", :argument => true do |file|
73
+ self.content << File.read(File.expand_path(file)) << "\n"
74
+ self.filename = file
75
+ end
76
+ opt.on :o, :out, "Gist entries from Pry's output result history. Takes an index or range.", :optional_argument => true,
77
+ :as => Range, :default => -1 do |range|
78
+ range = convert_to_range(range)
79
+
80
+ range.each do |v|
81
+ self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
82
+ end
83
+
84
+ self.content << "\n"
85
+ end
86
+ opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it.", :default => false
87
+ opt.on :p, :public, "Create a public gist (default: false)", :default => false
88
+ opt.on :l, :lines, "Only gist a subset of lines from the gistable content.", :optional_argument => true, :as => Range, :default => 1..-1
89
+ opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional_argument => true,
90
+ :as => Range, :default => -1 do |range|
91
+ range = convert_to_range(range)
92
+ input_expressions = _pry_.input_array[range] || []
93
+ Array(input_expressions).each_with_index do |code, index|
94
+ corrected_index = index + range.first
95
+ if code && code != ""
96
+ self.content << code
97
+ if code !~ /;\Z/
98
+ self.content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ def process
106
+ return Jist.login! if opts.present?(:login)
107
+
108
+ if self.content =~ /\A\s*\z/
109
+ raise CommandError, "Found no code to gist."
110
+ end
111
+
112
+ if opts.present?(:clip)
113
+ perform_clipboard
114
+ else
115
+ perform_gist
116
+ end
117
+ end
118
+
119
+ # copy content to clipboard instead (only used with --clip flag)
120
+ def perform_clipboard
121
+ copy(self.content)
122
+ output.puts "Copied content to clipboard!"
123
+ end
124
+
125
+ def perform_gist
126
+ if opts.present?(:lines)
127
+ self.content = restrict_to_lines(content, opts[:l])
128
+ end
129
+
130
+ response = Jist.gist(content, :filename => File.basename(filename),
131
+ :public => !!opts[:p])
132
+
133
+ if response
134
+ copy(response['html_url'])
135
+ output.puts "Gist created at #{response['html_url']} and added to clipboard."
136
+ end
137
+ end
138
+
139
+ def convert_to_range(n)
140
+ if !n.is_a?(Range)
141
+ (n..n)
142
+ else
143
+ n
144
+ end
145
+ end
146
+
147
+ def comment_expression_result_for_gist(result)
148
+ content = ""
149
+ result.lines.each_with_index do |line, index|
150
+ if index == 0
151
+ content << "# => #{line}"
152
+ else
153
+ content << "# #{line}"
154
+ end
155
+ end
156
+ content
157
+ end
158
+
159
+ # Copy a string to the clipboard.
160
+ #
161
+ # @param [String] content
162
+ #
163
+ # @copyright Copyright (c) 2008 Chris Wanstrath (MIT)
164
+ # @see https://github.com/defunkt/gist/blob/master/lib/gist.rb#L178
165
+ def copy(content)
166
+ cmd = case true
167
+ when system("type pbcopy > /dev/null 2>&1")
168
+ :pbcopy
169
+ when system("type xclip > /dev/null 2>&1")
170
+ :xclip
171
+ when system("type putclip > /dev/null 2>&1")
172
+ :putclip
173
+ end
174
+
175
+ if cmd
176
+ IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
177
+ end
178
+
179
+ content
180
+ end
181
+ end
182
+
183
+ alias_command "clipit", "gist --clip"
184
+ alias_command "jist", "gist"
185
+ end
186
+ end
187
+ end
@@ -16,13 +16,13 @@ class Pry
16
16
  USAGE
17
17
 
18
18
  def options(opt)
19
- opt.on :H, :head, "Display the first N items.", :optional => true, :as => Integer
20
- opt.on :T, :tail, "Display the last N items.", :optional => true, :as => Integer
21
- opt.on :s, :show, "Show the given range of lines.", :optional => true, :as => Range
22
- opt.on :G, :grep, "Show lines matching the given pattern.", true, :as => String
19
+ opt.on :H, :head, "Display the first N items.", :optional_argument => true, :as => Integer
20
+ opt.on :T, :tail, "Display the last N items.", :optional_argument => true, :as => Integer
21
+ opt.on :s, :show, "Show the given range of lines.", :optional_argument => true, :as => Range
22
+ opt.on :G, :grep, "Show lines matching the given pattern.", :argument => true, :as => String
23
23
  opt.on :c, :clear, "Clear the current session's history."
24
- opt.on :r, :replay, "Replay a line or range of lines.", true, :as => Range
25
- opt.on :save, "Save history to a file.", true, :as => Range
24
+ opt.on :r, :replay, "Replay a line or range of lines.", :argument => true, :as => Range
25
+ opt.on :save, "Save history to a file.", :argument => true, :as => Range
26
26
 
27
27
  opt.on :e, :'exclude-pry', "Exclude Pry commands from the history."
28
28
  opt.on :n, :'no-numbers', "Omit line numbers."
@@ -1,9 +1,11 @@
1
1
  require 'tempfile'
2
+ require 'pry/default_commands/gist'
2
3
 
3
4
  class Pry
4
5
  module DefaultCommands
5
6
 
6
7
  InputAndOutput = Pry::CommandSet.new do
8
+ import Gist
7
9
  command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>", :use_prefix => false, :takes_block => true) do |cmd|
8
10
  if cmd =~ /^cd\s+(.+)/i
9
11
  dest = $1
@@ -37,129 +39,13 @@ class Pry
37
39
  end
38
40
  alias_command "file-mode", "shell-mode"
39
41
 
40
- create_command "gist", "Gist a method or expression history to github.", :requires_gem => "gist", :shellwords => false do
41
- banner <<-USAGE
42
- Usage: gist [OPTIONS] [METH]
43
- Gist method (doc or source) or input expression to github.
44
- Ensure the `gist` gem is properly working before use. http://github.com/defunkt/gist for instructions.
45
- e.g: gist -m my_method
46
- e.g: gist -d my_method
47
- e.g: gist -i 1..10
48
- e.g: gist -c show-method
49
- e.g: gist -m hello_world --lines 2..-2
50
- USAGE
51
-
52
- attr_accessor :content
53
- attr_accessor :code_type
54
-
55
- def setup
56
- require 'gist'
57
- self.content = ""
58
- self.code_type = :ruby
59
- end
60
-
61
- def options(opt)
62
- opt.on :m, :method, "Gist a method's source.", true do |meth_name|
63
- meth = get_method_or_raise(meth_name, target, {})
64
- self.content << meth.source
65
- self.code_type = meth.source_type
66
- end
67
- opt.on :d, :doc, "Gist a method's documentation.", true do |meth_name|
68
- meth = get_method_or_raise(meth_name, target, {})
69
- text.no_color do
70
- self.content << process_comment_markup(meth.doc, self.code_type)
71
- end
72
- self.code_type = :plain
73
- end
74
- opt.on :c, :command, "Gist a command's source.", true do |command_name|
75
- command = find_command(command_name)
76
- block = Pry::Method.new(find_command(command_name).block)
77
- self.content << block.source
78
- end
79
- opt.on :f, :file, "Gist a file.", true do |file|
80
- self.content << File.read(File.expand_path(file))
81
- end
82
- opt.on :p, :public, "Create a public gist (default: false)", :default => false
83
- opt.on :l, :lines, "Only gist a subset of lines.", :optional => true, :as => Range, :default => 1..-1
84
- opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional => true,
85
- :as => Range, :default => -5..-1 do |range|
86
- range = convert_to_range(range)
87
- input_expressions = _pry_.input_array[range] || []
88
- Array(input_expressions).each_with_index do |code, index|
89
- corrected_index = index + range.first
90
- if code && code != ""
91
- self.content << code
92
- if code !~ /;\Z/
93
- self.content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
94
- end
95
- end
96
- end
97
- end
98
- end
99
-
100
- def process
101
- perform_gist
102
- end
103
-
104
- def perform_gist
105
- type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
106
-
107
- if self.content =~ /\A\s*\z/
108
- raise CommandError, "Found no code to gist."
109
- end
110
-
111
- # prevent Gist from exiting the session on error
112
- begin
113
- extname = opts.present?(:file) ? ".#{gist_file_extension(opts[:f])}" : ".#{type_map[self.code_type]}"
114
-
115
- if opts.present?(:lines)
116
- self.content = restrict_to_lines(content, opts[:l])
117
- end
118
-
119
- link = Gist.write([:extension => extname,
120
- :input => self.content],
121
- !opts[:p])
122
- rescue SystemExit
123
- end
124
-
125
- if link
126
- Gist.copy(link)
127
- output.puts "Gist created at #{link} and added to clipboard."
128
- end
129
- end
130
-
131
- def gist_file_extension(file_name)
132
- file_name.split(".").last
133
- end
134
-
135
- def convert_to_range(n)
136
- if !n.is_a?(Range)
137
- (n..n)
138
- else
139
- n
140
- end
141
- end
142
-
143
- def comment_expression_result_for_gist(result)
144
- content = ""
145
- result.lines.each_with_index do |line, index|
146
- if index == 0
147
- content << "# => #{line}"
148
- else
149
- content << "# #{line}"
150
- end
151
- end
152
- content
153
- end
154
- end
155
-
156
42
  create_command "save-file", "Export to a file using content from the REPL." do
157
43
  banner <<-USAGE
158
44
  Usage: save-file [OPTIONS] [FILE]
159
45
  Save REPL content to a file.
160
46
  e.g: save-file -m my_method -m my_method2 ./hello.rb
161
47
  e.g: save-file -i 1..10 ./hello.rb --append
162
- e.g: save-file -c show-method ./my_command.rb
48
+ e.g: save-file -k show-method ./my_command.rb
163
49
  e.g: save-file -f sample_file --lines 2..10 ./output_file.rb
164
50
  USAGE
165
51
 
@@ -170,21 +56,43 @@ class Pry
170
56
  self.content = ""
171
57
  end
172
58
 
59
+ def convert_to_range(n)
60
+ if !n.is_a?(Range)
61
+ (n..n)
62
+ else
63
+ n
64
+ end
65
+ end
66
+
173
67
  def options(opt)
174
- opt.on :m, :method, "Save a method's source.", true do |meth_name|
68
+ opt.on :m, :method, "Save a method's source.", :argument => true do |meth_name|
175
69
  meth = get_method_or_raise(meth_name, target, {})
176
70
  self.content << meth.source
177
71
  end
178
- opt.on :c, :command, "Save a command's source.", true do |command_name|
72
+ opt.on :c, :class, "Save a class's source.", :argument => true do |class_name|
73
+ mod = Pry::WrappedModule.from_str(class_name, target)
74
+ self.content << mod.source
75
+ end
76
+ opt.on :k, :command, "Save a command's source.", :argument => true do |command_name|
179
77
  command = find_command(command_name)
180
- block = Pry::Method.new(find_command(command_name).block)
78
+ block = Pry::Method.new(command.block)
181
79
  self.content << block.source
182
80
  end
183
- opt.on :f, :file, "Save a file.", true do |file|
81
+ opt.on :f, :file, "Save a file.", :argument => true do |file|
184
82
  self.content << File.read(File.expand_path(file))
185
83
  end
186
- opt.on :l, :lines, "Only save a subset of lines.", :optional => true, :as => Range, :default => 1..-1
187
- opt.on :i, :in, "Save entries from Pry's input expression history. Takes an index or range.", :optional => true,
84
+ opt.on :l, :lines, "Only save a subset of lines.", :optional_argument => true, :as => Range, :default => 1..-1
85
+ opt.on :o, :out, "Save entries from Pry's output result history. Takes an index or range.", :optional_argument => true,
86
+ :as => Range, :default => -5..-1 do |range|
87
+ range = convert_to_range(range)
88
+
89
+ range.each do |v|
90
+ self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
91
+ end
92
+
93
+ self.content << "\n"
94
+ end
95
+ opt.on :i, :in, "Save entries from Pry's input expression history. Takes an index or range.", :optional_argument => true,
188
96
  :as => Range, :default => -5..-1 do |range|
189
97
  input_expressions = _pry_.input_array[range] || []
190
98
  Array(input_expressions).each { |v| self.content << v }
@@ -240,13 +148,13 @@ class Pry
240
148
  USAGE
241
149
 
242
150
  def options(opt)
243
- opt.on :ex, "Show the context of the last exception.", :optional => true, :as => Integer
244
- opt.on :i, :in, "Show one or more entries from Pry's expression history.", :optional => true, :as => Range, :default => -5..-1
151
+ opt.on :ex, "Show the context of the last exception.", :optional_argument => true, :as => Integer
152
+ opt.on :i, :in, "Show one or more entries from Pry's expression history.", :optional_argument => true, :as => Range, :default => -5..-1
245
153
 
246
- opt.on :s, :start, "Starting line (defaults to the first line).", :optional => true, :as => Integer
247
- opt.on :e, :end, "Ending line (defaults to the last line).", :optional => true, :as => Integer
154
+ opt.on :s, :start, "Starting line (defaults to the first line).", :optional_argument => true, :as => Integer
155
+ opt.on :e, :end, "Ending line (defaults to the last line).", :optional_argument => true, :as => Integer
248
156
  opt.on :l, :'line-numbers', "Show line numbers."
249
- opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python').", true, :as => Symbol
157
+ opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python').", :argument => true, :as => Symbol
250
158
 
251
159
  opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
252
160
  end
@@ -282,6 +190,8 @@ class Pry
282
190
  ex.inc_bt_index
283
191
  else
284
192
  bt_index = opts[:ex]
193
+ ex.bt_index = bt_index
194
+ ex.inc_bt_index
285
195
  end
286
196
 
287
197
  ex_file, ex_line = ex.bt_source_location_for(bt_index)
@@ -348,6 +258,7 @@ class Pry
348
258
 
349
259
  code = yield(Pry::Code.from_file(file_name))
350
260
 
261
+ code.code_type = opts[:type] || detect_code_type_from_file(file_name)
351
262
  if line_num
352
263
  code = code.around(line_num.to_i,
353
264
  Pry.config.default_window_size || 7)
@@ -355,8 +266,41 @@ class Pry
355
266
 
356
267
  code
357
268
  end
358
- end
359
269
 
270
+ def detect_code_type_from_file(file_name)
271
+ name, ext = File.basename(file_name).split('.', 2)
272
+
273
+ if ext
274
+ case ext
275
+ when "py"
276
+ :python
277
+ when "rb", "gemspec", "rakefile", "ru"
278
+ :ruby
279
+ when "js"
280
+ return :javascript
281
+ when "yml", "prytheme"
282
+ :yaml
283
+ when "groovy"
284
+ :groovy
285
+ when "c"
286
+ :c
287
+ when "cpp"
288
+ :cpp
289
+ when "java"
290
+ :java
291
+ else
292
+ :text
293
+ end
294
+ else
295
+ case name
296
+ when "Rakefile", "Gemfile"
297
+ :ruby
298
+ else
299
+ :text
300
+ end
301
+ end
302
+ end
303
+ end
360
304
  end
361
305
  end
362
306
  end