pry 0.2.0 → 0.2.5pre1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pry.rb CHANGED
@@ -8,106 +8,123 @@ require "#{direc}/pry/version"
8
8
  require "#{direc}/pry/input"
9
9
  require "#{direc}/pry/output"
10
10
 
11
- module Pry
11
+ class Pry
12
+ def self.start(target)
13
+ new.repl(target)
14
+ end
15
+
16
+ def self.view(obj)
17
+ case obj
18
+ when String, Symbol, nil
19
+ obj.inspect
20
+ else
21
+ obj.to_s
22
+ end
23
+ end
12
24
 
13
25
  # class accessors
14
26
  class << self
15
27
  attr_reader :nesting
16
- attr_reader :last_result
17
- attr_accessor :default_prompt
18
- attr_accessor :wait_prompt
19
- attr_accessor :input
20
- attr_accessor :output
28
+ attr_accessor :last_result
21
29
  end
30
+
31
+ attr_accessor :input, :output
32
+ attr_reader :default_prompt, :wait_prompt, :last_result
22
33
 
23
- @default_prompt = lambda do |v, nest|
24
- if nest == 0
25
- "pry(#{v.inspect})> "
26
- else
27
- "pry(#{v.inspect}):#{nest.inspect}> "
34
+ def initialize(input = Input.new, output = Output.new)
35
+ @input = input
36
+ @output = output
37
+
38
+ @default_prompt = proc do |v, nest|
39
+ if nest == 0
40
+ "pry(#{Pry.view(v)})> "
41
+ else
42
+ "pry(#{Pry.view(v)}):#{Pry.view(nest)}> "
43
+ end
28
44
  end
29
- end
30
-
31
- @wait_prompt = lambda do |v, nest|
32
- if nest == 0
33
- "pry(#{v.inspect})* "
34
- else
35
- "pry(#{v.inspect}):#{nest.inspect}* "
45
+
46
+ @wait_prompt = proc do |v, nest|
47
+ if nest == 0
48
+ "pry(#{Pry.view(v)})* "
49
+ else
50
+ "pry(#{Pry.view(v)}):#{Pry.view(nest)}* "
51
+ end
36
52
  end
37
53
  end
38
-
39
- @output = Output.new
40
- @input = Input.new
41
-
54
+
42
55
  @nesting = []
43
56
 
44
57
  def @nesting.level
45
58
  last.is_a?(Array) ? last.first : nil
46
59
  end
60
+
61
+ def nesting
62
+ self.class.nesting
63
+ end
64
+
65
+ def nesting=(v)
66
+ self.class.nesting = v
67
+ end
68
+
47
69
 
48
70
  # loop
49
- def self.repl(target=TOPLEVEL_BINDING)
71
+ def repl(target=TOPLEVEL_BINDING)
50
72
  target = binding_for(target)
51
73
  target_self = target.eval('self')
52
74
  output.session_start(target_self)
53
75
 
54
- nesting_level = @nesting.size
76
+ nesting_level = nesting.size
55
77
 
56
78
  # Make sure _ exists
57
79
  target.eval("_ = Pry.last_result")
58
80
 
59
- nesting_level_breakout = catch(:breakout) do
60
- @nesting << [@nesting.size, target_self]
81
+ break_level = catch(:breakout) do
82
+ nesting << [nesting.size, target_self]
61
83
  loop do
62
- rep(target)
84
+ rep(target)
63
85
  end
64
86
  end
65
87
 
66
- @nesting.pop
88
+ nesting.pop
67
89
  output.session_end(target_self)
68
90
 
69
91
  # we only enter here if :breakout has been thrown
70
- if nesting_level_breakout
71
- throw :breakout, nesting_level_breakout if nesting_level != nesting_level_breakout
92
+ if nesting_level != break_level
93
+ throw :breakout, break_level
72
94
  end
73
95
 
74
96
  target_self
75
97
  end
76
98
 
77
- class << self
78
- alias_method :into, :repl
79
- alias_method :start, :repl
80
- end
81
-
82
99
  # print
83
- def self.rep(target=TOPLEVEL_BINDING)
100
+ def rep(target=TOPLEVEL_BINDING)
84
101
  target = binding_for(target)
85
102
  output.print re(target)
86
103
  end
87
104
 
88
105
  # eval
89
- def self.re(target=TOPLEVEL_BINDING)
106
+ def re(target=TOPLEVEL_BINDING)
90
107
  target = binding_for(target)
91
- @last_result = target.eval r(target)
108
+ Pry.last_result = target.eval r(target)
92
109
  target.eval("_ = Pry.last_result")
93
- rescue StandardError => e
110
+ rescue Exception => e
94
111
  e
95
112
  end
96
113
 
97
114
  # read
98
- def self.r(target=TOPLEVEL_BINDING)
115
+ def r(target=TOPLEVEL_BINDING)
99
116
  target = binding_for(target)
100
117
  eval_string = ""
101
118
  loop do
102
119
  val = input.read(prompt(eval_string, target, nesting.level))
103
- eval_string += "#{val}\n"
120
+ eval_string += "#{val.chomp}\n"
104
121
  process_commands(val, eval_string, target)
105
122
 
106
123
  break eval_string if valid_expression?(eval_string)
107
124
  end
108
125
  end
109
126
 
110
- def self.process_commands(val, eval_string, target)
127
+ def process_commands(val, eval_string, target)
111
128
  def eval_string.clear() replace("") end
112
129
 
113
130
  case val
@@ -131,20 +148,31 @@ module Pry
131
148
  when "exit", "quit", "back"
132
149
  output.exit
133
150
  throw(:breakout, nesting.level)
151
+ when /show_method\s*(\w*)/
152
+ meth_name = ($~.captures).first
153
+ file, line = target.eval("method(:#{meth_name}).source_location")
154
+ tp = Pry.new.tap { |v| v.input = SourceInput.new(file, line) }
155
+ tp.r.display
156
+ eval_string.clear
134
157
  when /jump_to\s*(\d*)/
135
- nesting_level_breakout = ($~.captures).first.to_i
136
- output.jump_to(nesting_level_breakout)
137
-
138
- if nesting_level_breakout < 0 || nesting_level_breakout >= nesting.level
139
- output.error_invalid_nest_level(nesting_level_breakout, nesting.level - 1)
158
+ break_level = ($~.captures).first.to_i
159
+ output.jump_to(break_level)
160
+
161
+ case break_level
162
+ when nesting.level
163
+ output.warn_already_at_level(nesting.level)
140
164
  eval_string.clear
165
+ when (0...nesting.level)
166
+ throw(:breakout, break_level + 1)
141
167
  else
142
- throw(:breakout, nesting_level_breakout + 1)
168
+ output.err_invalid_nest_level(break_level,
169
+ nesting.level - 1)
170
+ eval_string.clear
143
171
  end
144
172
  end
145
173
  end
146
174
 
147
- def self.prompt(eval_string, target, nest)
175
+ def prompt(eval_string, target, nest)
148
176
  target_self = target.eval('self')
149
177
 
150
178
  if eval_string.empty?
@@ -154,7 +182,29 @@ module Pry
154
182
  end
155
183
  end
156
184
 
157
- def self.valid_expression?(code)
185
+ def valid_expression?(code)
186
+ test_bed = Object.new.instance_eval { binding }
187
+
188
+ begin
189
+ test_bed.eval(code)
190
+ rescue Exception => e
191
+ case e
192
+ when SyntaxError
193
+ case e.message
194
+ when /(parse|syntax) error.*?\$end/i, /unterminated/i
195
+ return false
196
+ else
197
+ return true
198
+ end
199
+ else
200
+ true
201
+ end
202
+ true
203
+ end
204
+ true
205
+ end
206
+
207
+ def old_valid_expression?(code)
158
208
  RubyParser.new.parse(code)
159
209
  rescue Racc::ParseError, SyntaxError
160
210
  false
@@ -162,7 +212,7 @@ module Pry
162
212
  true
163
213
  end
164
214
 
165
- def self.binding_for(target)
215
+ def binding_for(target)
166
216
  if target.is_a?(Binding)
167
217
  target
168
218
  else
@@ -176,7 +226,7 @@ module Pry
176
226
 
177
227
  module ObjectExtensions
178
228
  def pry(target=self)
179
- Pry.start(Pry.binding_for(target))
229
+ Pry.new.repl(target)
180
230
  end
181
231
  end
182
232
  end
data/lib/pry/input.rb CHANGED
@@ -1,9 +1,20 @@
1
1
  require 'readline'
2
2
 
3
- module Pry
3
+ class Pry
4
4
  class Input
5
5
  def read(prompt)
6
6
  Readline.readline(prompt, true)
7
7
  end
8
8
  end
9
+
10
+ class SourceInput
11
+ def initialize(file, line)
12
+ @f = File.open(file)
13
+ (line - 1).times { @f.readline }
14
+ end
15
+
16
+ def read(prompt)
17
+ @f.readline
18
+ end
19
+ end
9
20
  end
data/lib/pry/output.rb CHANGED
@@ -1,15 +1,15 @@
1
- module Pry
1
+ class Pry
2
2
  class Output
3
3
  def refresh
4
4
  puts "Refreshed REPL"
5
5
  end
6
6
 
7
7
  def session_start(obj)
8
- puts "Beginning Pry session for #{obj.inspect}"
8
+ puts "Beginning Pry session for #{Pry.view(obj)}"
9
9
  end
10
10
 
11
11
  def session_end(obj)
12
- puts "Ending Pry session for #{obj.inspect}"
12
+ puts "Ending Pry session for #{Pry.view(obj)}"
13
13
  end
14
14
 
15
15
  # the print component of READ-EVAL-PRINT-LOOP
@@ -18,7 +18,7 @@ module Pry
18
18
  when Exception
19
19
  puts "#{value.class}: #{value.message}"
20
20
  else
21
- puts "=> #{value.inspect}"
21
+ puts "=> #{Pry.view(value)}"
22
22
  end
23
23
  end
24
24
 
@@ -40,9 +40,9 @@ module Pry
40
40
  puts "--"
41
41
  nesting.each do |level, obj|
42
42
  if level == 0
43
- puts "#{level}. #{obj.inspect} (Pry top level)"
43
+ puts "#{level}. #{Pry.view(obj)} (Pry top level)"
44
44
  else
45
- puts "#{level}. #{obj.inspect}"
45
+ puts "#{level}. #{Pry.view(obj)}"
46
46
  end
47
47
  end
48
48
  end
@@ -50,13 +50,17 @@ module Pry
50
50
  def show_status(nesting, target)
51
51
  puts "Status:"
52
52
  puts "--"
53
- puts "Receiver: #{target.eval('self').inspect}"
53
+ puts "Receiver: #{Pry.view(target.eval('self'))}"
54
54
  puts "Nesting level: #{nesting.level}"
55
- puts "Local variables: #{target.eval("local_variables").inspect}"
56
- puts "Last result: #{Pry.last_result.inspect}"
55
+ puts "Local variables: #{Pry.view(target.eval("local_variables"))}"
56
+ puts "Last result: #{Pry.view(Pry.last_result)}"
57
57
  end
58
58
 
59
- def error_invalid_nest_level(nest_level, max_nest_level)
59
+ def warn_already_at_level(nesting_level)
60
+ puts "Already at nesting level #{nesting_level}"
61
+ end
62
+
63
+ def err_invalid_nest_level(nest_level, max_nest_level)
60
64
  puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{nest_level}."
61
65
  end
62
66
 
data/lib/pry/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Pry
2
- VERSION = "0.2.0"
1
+ class Pry
2
+ VERSION = "0.2.5pre1"
3
3
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
4
+ prerelease: true
6
5
  segments:
7
6
  - 0
8
7
  - 2
9
- - 0
10
- version: 0.2.0
8
+ - 5pre1
9
+ version: 0.2.5pre1
11
10
  platform: ruby
12
11
  authors:
13
12
  - John Mair (banisterfiend)
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-12-11 00:00:00 +13:00
17
+ date: 2010-12-14 00:00:00 +13:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 5
30
28
  segments:
31
29
  - 2
32
30
  - 0
@@ -64,19 +62,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
62
  requirements:
65
63
  - - ">="
66
64
  - !ruby/object:Gem::Version
67
- hash: 3
68
65
  segments:
69
66
  - 0
70
67
  version: "0"
71
68
  required_rubygems_version: !ruby/object:Gem::Requirement
72
69
  none: false
73
70
  requirements:
74
- - - ">="
71
+ - - ">"
75
72
  - !ruby/object:Gem::Version
76
- hash: 3
77
73
  segments:
78
- - 0
79
- version: "0"
74
+ - 1
75
+ - 3
76
+ - 1
77
+ version: 1.3.1
80
78
  requirements: []
81
79
 
82
80
  rubyforge_project: