pry 0.10.pre.1-i386-mswin32 → 0.10.0.pre3-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +2 -2
  4. data/{README.markdown → README.md} +41 -35
  5. data/lib/pry.rb +82 -139
  6. data/lib/pry/cli.rb +77 -30
  7. data/lib/pry/code.rb +122 -183
  8. data/lib/pry/code/code_file.rb +103 -0
  9. data/lib/pry/code/code_range.rb +71 -0
  10. data/lib/pry/code/loc.rb +92 -0
  11. data/lib/pry/code_object.rb +172 -0
  12. data/lib/pry/color_printer.rb +55 -0
  13. data/lib/pry/command.rb +184 -28
  14. data/lib/pry/command_set.rb +113 -59
  15. data/lib/pry/commands.rb +4 -27
  16. data/lib/pry/commands/amend_line.rb +99 -0
  17. data/lib/pry/commands/bang.rb +20 -0
  18. data/lib/pry/commands/bang_pry.rb +17 -0
  19. data/lib/pry/commands/cat.rb +62 -0
  20. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  21. data/lib/pry/commands/cat/exception_formatter.rb +77 -0
  22. data/lib/pry/commands/cat/file_formatter.rb +67 -0
  23. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  24. data/lib/pry/commands/cd.rb +41 -0
  25. data/lib/pry/commands/change_inspector.rb +27 -0
  26. data/lib/pry/commands/change_prompt.rb +26 -0
  27. data/lib/pry/commands/code_collector.rb +165 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/disabled_commands.rb +2 -0
  30. data/lib/pry/commands/easter_eggs.rb +112 -0
  31. data/lib/pry/commands/edit.rb +195 -0
  32. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +23 -0
  37. data/lib/pry/commands/find_method.rb +193 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +32 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +101 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +180 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +53 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/list_inspectors.rb +35 -0
  50. data/lib/pry/commands/list_prompts.rb +35 -0
  51. data/lib/pry/commands/ls.rb +114 -0
  52. data/lib/pry/commands/ls/constants.rb +47 -0
  53. data/lib/pry/commands/ls/formatter.rb +49 -0
  54. data/lib/pry/commands/ls/globals.rb +48 -0
  55. data/lib/pry/commands/ls/grep.rb +21 -0
  56. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  57. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  58. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  59. data/lib/pry/commands/ls/local_names.rb +35 -0
  60. data/lib/pry/commands/ls/local_vars.rb +39 -0
  61. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  62. data/lib/pry/commands/ls/methods.rb +57 -0
  63. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  64. data/lib/pry/commands/ls/self_methods.rb +32 -0
  65. data/lib/pry/commands/nesting.rb +25 -0
  66. data/lib/pry/commands/play.rb +103 -0
  67. data/lib/pry/commands/pry_backtrace.rb +25 -0
  68. data/lib/pry/commands/pry_version.rb +17 -0
  69. data/lib/pry/commands/raise_up.rb +32 -0
  70. data/lib/pry/commands/reload_code.rb +62 -0
  71. data/lib/pry/commands/reset.rb +18 -0
  72. data/lib/pry/commands/ri.rb +60 -0
  73. data/lib/pry/commands/save_file.rb +61 -0
  74. data/lib/pry/commands/shell_command.rb +48 -0
  75. data/lib/pry/commands/shell_mode.rb +25 -0
  76. data/lib/pry/commands/show_doc.rb +83 -0
  77. data/lib/pry/commands/show_info.rb +195 -0
  78. data/lib/pry/commands/show_input.rb +17 -0
  79. data/lib/pry/commands/show_source.rb +50 -0
  80. data/lib/pry/commands/simple_prompt.rb +22 -0
  81. data/lib/pry/commands/stat.rb +40 -0
  82. data/lib/pry/commands/switch_to.rb +23 -0
  83. data/lib/pry/commands/toggle_color.rb +24 -0
  84. data/lib/pry/commands/watch_expression.rb +105 -0
  85. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  86. data/lib/pry/commands/whereami.rb +190 -0
  87. data/lib/pry/commands/wtf.rb +57 -0
  88. data/lib/pry/config.rb +20 -229
  89. data/lib/pry/config/behavior.rb +139 -0
  90. data/lib/pry/config/convenience.rb +26 -0
  91. data/lib/pry/config/default.rb +165 -0
  92. data/lib/pry/core_extensions.rb +59 -38
  93. data/lib/pry/editor.rb +133 -0
  94. data/lib/pry/exceptions.rb +77 -0
  95. data/lib/pry/helpers.rb +1 -0
  96. data/lib/pry/helpers/base_helpers.rb +40 -154
  97. data/lib/pry/helpers/command_helpers.rb +19 -130
  98. data/lib/pry/helpers/documentation_helpers.rb +21 -11
  99. data/lib/pry/helpers/table.rb +109 -0
  100. data/lib/pry/helpers/text.rb +8 -9
  101. data/lib/pry/history.rb +61 -45
  102. data/lib/pry/history_array.rb +11 -1
  103. data/lib/pry/hooks.rb +10 -32
  104. data/lib/pry/indent.rb +110 -38
  105. data/lib/pry/input_completer.rb +242 -0
  106. data/lib/pry/input_lock.rb +132 -0
  107. data/lib/pry/inspector.rb +27 -0
  108. data/lib/pry/last_exception.rb +61 -0
  109. data/lib/pry/method.rb +199 -200
  110. data/lib/pry/method/disowned.rb +53 -0
  111. data/lib/pry/method/patcher.rb +125 -0
  112. data/lib/pry/method/weird_method_locator.rb +186 -0
  113. data/lib/pry/module_candidate.rb +39 -33
  114. data/lib/pry/object_path.rb +82 -0
  115. data/lib/pry/output.rb +50 -0
  116. data/lib/pry/pager.rb +234 -0
  117. data/lib/pry/plugins.rb +4 -3
  118. data/lib/pry/prompt.rb +26 -0
  119. data/lib/pry/pry_class.rb +199 -227
  120. data/lib/pry/pry_instance.rb +344 -403
  121. data/lib/pry/rbx_path.rb +1 -1
  122. data/lib/pry/repl.rb +202 -0
  123. data/lib/pry/repl_file_loader.rb +20 -26
  124. data/lib/pry/rubygem.rb +82 -0
  125. data/lib/pry/terminal.rb +79 -0
  126. data/lib/pry/test/helper.rb +170 -0
  127. data/lib/pry/version.rb +1 -1
  128. data/lib/pry/wrapped_module.rb +133 -48
  129. metadata +132 -197
  130. data/.document +0 -2
  131. data/.gemtest +0 -0
  132. data/.gitignore +0 -16
  133. data/.travis.yml +0 -17
  134. data/.yardopts +0 -1
  135. data/CHANGELOG +0 -387
  136. data/CONTRIBUTORS +0 -36
  137. data/Gemfile +0 -2
  138. data/Rakefile +0 -137
  139. data/TODO +0 -117
  140. data/examples/example_basic.rb +0 -15
  141. data/examples/example_command_override.rb +0 -32
  142. data/examples/example_commands.rb +0 -36
  143. data/examples/example_hooks.rb +0 -9
  144. data/examples/example_image_edit.rb +0 -67
  145. data/examples/example_input.rb +0 -7
  146. data/examples/example_input2.rb +0 -29
  147. data/examples/example_output.rb +0 -11
  148. data/examples/example_print.rb +0 -6
  149. data/examples/example_prompt.rb +0 -9
  150. data/examples/helper.rb +0 -6
  151. data/lib/pry/completion.rb +0 -221
  152. data/lib/pry/custom_completions.rb +0 -6
  153. data/lib/pry/default_commands/cd.rb +0 -81
  154. data/lib/pry/default_commands/commands.rb +0 -62
  155. data/lib/pry/default_commands/context.rb +0 -98
  156. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  157. data/lib/pry/default_commands/editing.rb +0 -420
  158. data/lib/pry/default_commands/find_method.rb +0 -169
  159. data/lib/pry/default_commands/gems.rb +0 -84
  160. data/lib/pry/default_commands/gist.rb +0 -187
  161. data/lib/pry/default_commands/help.rb +0 -127
  162. data/lib/pry/default_commands/hist.rb +0 -120
  163. data/lib/pry/default_commands/input_and_output.rb +0 -306
  164. data/lib/pry/default_commands/introspection.rb +0 -410
  165. data/lib/pry/default_commands/ls.rb +0 -272
  166. data/lib/pry/default_commands/misc.rb +0 -38
  167. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  168. data/lib/pry/default_commands/whereami.rb +0 -92
  169. data/lib/pry/extended_commands/experimental.rb +0 -7
  170. data/lib/pry/rbx_method.rb +0 -13
  171. data/man/pry.1 +0 -195
  172. data/man/pry.1.html +0 -204
  173. data/man/pry.1.ronn +0 -141
  174. data/pry.gemspec +0 -46
  175. data/test/candidate_helper1.rb +0 -11
  176. data/test/candidate_helper2.rb +0 -8
  177. data/test/helper.rb +0 -223
  178. data/test/test_cli.rb +0 -78
  179. data/test/test_code.rb +0 -201
  180. data/test/test_command.rb +0 -712
  181. data/test/test_command_helpers.rb +0 -9
  182. data/test/test_command_integration.rb +0 -668
  183. data/test/test_command_set.rb +0 -610
  184. data/test/test_completion.rb +0 -62
  185. data/test/test_control_d_handler.rb +0 -45
  186. data/test/test_default_commands/example.erb +0 -5
  187. data/test/test_default_commands/test_cd.rb +0 -318
  188. data/test/test_default_commands/test_context.rb +0 -280
  189. data/test/test_default_commands/test_documentation.rb +0 -314
  190. data/test/test_default_commands/test_find_method.rb +0 -50
  191. data/test/test_default_commands/test_gems.rb +0 -18
  192. data/test/test_default_commands/test_help.rb +0 -57
  193. data/test/test_default_commands/test_input.rb +0 -428
  194. data/test/test_default_commands/test_introspection.rb +0 -511
  195. data/test/test_default_commands/test_ls.rb +0 -151
  196. data/test/test_default_commands/test_shell.rb +0 -343
  197. data/test/test_default_commands/test_show_source.rb +0 -432
  198. data/test/test_exception_whitelist.rb +0 -21
  199. data/test/test_history_array.rb +0 -65
  200. data/test/test_hooks.rb +0 -521
  201. data/test/test_indent.rb +0 -277
  202. data/test/test_input_stack.rb +0 -86
  203. data/test/test_method.rb +0 -401
  204. data/test/test_pry.rb +0 -463
  205. data/test/test_pry_defaults.rb +0 -419
  206. data/test/test_pry_history.rb +0 -84
  207. data/test/test_pry_output.rb +0 -41
  208. data/test/test_sticky_locals.rb +0 -155
  209. data/test/test_syntax_checking.rb +0 -65
  210. data/test/test_wrapped_module.rb +0 -174
  211. data/test/testrc +0 -2
  212. data/test/testrcbad +0 -2
  213. data/wiki/Customizing-pry.md +0 -397
  214. data/wiki/Home.md +0 -4
@@ -1,4 +1,3 @@
1
-
2
1
  class Pry
3
2
 
4
3
  # Manage the processing of command line options
@@ -11,7 +10,9 @@ class Pry
11
10
  # @return [Proc] The Proc defining the valid command line options.
12
11
  attr_accessor :options
13
12
 
14
- # @return [Array] The Procs that process the parsed options.
13
+ # @return [Array] The Procs that process the parsed options. Plugins can
14
+ # utilize this facility in order to add and process their own Pry
15
+ # options.
15
16
  attr_accessor :option_processors
16
17
 
17
18
  # @return [Array<String>] The input array of strings to process
@@ -43,7 +44,7 @@ class Pry
43
44
  end
44
45
 
45
46
  # Add a block responsible for processing parsed options.
46
- def process_options(&block)
47
+ def add_option_processor(&block)
47
48
  self.option_processors ||= []
48
49
  option_processors << block
49
50
 
@@ -56,13 +57,31 @@ class Pry
56
57
  self.option_processors = nil
57
58
  end
58
59
 
59
- def parse_options(args=ARGV.dup)
60
- raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options." if !options
60
+ def parse_options(args=ARGV)
61
+ unless options
62
+ raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
63
+ end
61
64
 
62
65
  self.input_args = args
63
66
 
64
- opts = Slop.parse!(args, :help => true, :multiple_switches => false, &options)
65
- option_processors.each { |processor| processor.call(opts) } if option_processors # option processors are optional
67
+ begin
68
+ opts = Slop.parse!(
69
+ args,
70
+ :help => true,
71
+ :multiple_switches => false,
72
+ :strict => true,
73
+ &options
74
+ )
75
+ rescue Slop::InvalidOptionError
76
+ # Display help message on unknown switches and exit.
77
+ puts Slop.new(&options)
78
+ exit
79
+ end
80
+
81
+ # Option processors are optional.
82
+ if option_processors
83
+ option_processors.each { |processor| processor.call(opts) }
84
+ end
66
85
 
67
86
  self
68
87
  end
@@ -73,18 +92,32 @@ class Pry
73
92
  end
74
93
  end
75
94
 
95
+
96
+ # String that is built to be executed on start (created by -e and -exec switches)
97
+ exec_string = ""
98
+
76
99
  # Bring in options defined by plugins
77
- Pry::CLI.add_plugin_options
100
+ Slop.new do
101
+ on "no-plugins" do
102
+ Pry.config.should_load_plugins = false
103
+ end
104
+ end.parse(ARGV.dup)
105
+
106
+ if Pry.config.should_load_plugins
107
+ Pry::CLI.add_plugin_options
108
+ end
78
109
 
79
110
  # The default Pry command line options (before plugin options are included)
80
111
  Pry::CLI.add_options do
81
112
  banner %{Usage: pry [OPTIONS]
82
113
  Start a Pry session.
83
- See: `https://github.com/pry` for more information.
84
- Copyright (c) 2011 John Mair (banisterfiend)
114
+ See http://pryrepl.org/ for more information.
115
+ Copyright (c) 2013 John Mair (banisterfiend)
85
116
  --
86
117
  }
87
- on :e, :exec, "A line of code to execute in context before the session starts", :argument => true
118
+ on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
119
+ exec_string << input << "\n"
120
+ end
88
121
 
89
122
  on "no-pager", "Disable pager for long output" do
90
123
  Pry.config.pager = false
@@ -95,20 +128,20 @@ Copyright (c) 2011 John Mair (banisterfiend)
95
128
  end
96
129
 
97
130
  on "no-color", "Disable syntax highlighting for session" do
98
- Pry.color = false
131
+ Pry.config.color = false
99
132
  end
100
133
 
101
- on :f, "Suppress loading of ~/.pryrc" do
102
- # load ~/.pryrc, if not suppressed with -f option
134
+ on :f, "Suppress loading of ~/.pryrc and ./.pryrc" do
103
135
  Pry.config.should_load_rc = false
136
+ Pry.config.should_load_local_rc = false
104
137
  end
105
138
 
106
- on :s, "select-plugin", "Only load specified plugin (and no others).", :argument => true do |plugin_name|
139
+ on :s, "select-plugin=", "Only load specified plugin (and no others)." do |plugin_name|
107
140
  Pry.config.should_load_plugins = false
108
141
  Pry.plugins[plugin_name].activate!
109
142
  end
110
143
 
111
- on :d, "disable-plugin", "Disable a specific plugin.", :argument => true do |plugin_name|
144
+ on :d, "disable-plugin=", "Disable a specific plugin." do |plugin_name|
112
145
  Pry.plugins[plugin_name].disable!
113
146
  end
114
147
 
@@ -116,11 +149,11 @@ Copyright (c) 2011 John Mair (banisterfiend)
116
149
  Pry.config.should_load_plugins = false
117
150
  end
118
151
 
119
- on "installed-plugins", "List installed plugins." do
152
+ on "plugins", "List installed plugins." do
120
153
  puts "Installed Plugins:"
121
154
  puts "--"
122
155
  Pry.locate_plugins.each do |plugin|
123
- puts "#{plugin.name}".ljust(18) + plugin.spec.summary
156
+ puts "#{plugin.name}".ljust(18) << plugin.spec.summary
124
157
  end
125
158
  exit
126
159
  end
@@ -129,12 +162,27 @@ Copyright (c) 2011 John Mair (banisterfiend)
129
162
  Pry.config.prompt = Pry::SIMPLE_PROMPT
130
163
  end
131
164
 
132
- on :r, :require, "`require` a Ruby script at startup", :argument => true do |file|
165
+ on "noprompt", "No prompt mode" do
166
+ Pry.config.prompt = Pry::NO_PROMPT
167
+ end
168
+
169
+ on :r, :require=, "`require` a Ruby script at startup" do |file|
133
170
  Pry.config.requires << file
134
171
  end
135
172
 
136
- on :I, "Add a path to the $LOAD_PATH", :argument => true do |path|
137
- $LOAD_PATH << path
173
+ on :I=, "Add a path to the $LOAD_PATH", :as => Array, :delimiter => ":" do |load_path|
174
+ load_path.map! do |path|
175
+ /\A\.\// =~ path ? path : File.expand_path(path)
176
+ end
177
+
178
+ $LOAD_PATH.unshift(*load_path)
179
+ end
180
+
181
+ on "gem", "Shorthand for -I./lib -rgemname" do |load_path|
182
+ $LOAD_PATH.unshift("./lib")
183
+ Dir["./lib/*.rb"].each do |file|
184
+ Pry.config.requires << file
185
+ end
138
186
  end
139
187
 
140
188
  on :v, :version, "Display the Pry version" do
@@ -142,12 +190,11 @@ Copyright (c) 2011 John Mair (banisterfiend)
142
190
  exit
143
191
  end
144
192
 
145
- on(:c, :context,
193
+ on(:c, :context=,
146
194
  "Start the session in the specified context. Equivalent to `context.pry` in a session.",
147
- :argument => true,
148
195
  :default => "Pry.toplevel_binding"
149
196
  )
150
- end.process_options do |opts|
197
+ end.add_option_processor do |opts|
151
198
 
152
199
  exit if opts.help?
153
200
 
@@ -155,19 +202,19 @@ end.process_options do |opts|
155
202
  Pry.cli = true
156
203
 
157
204
  # create the actual context
158
- context = Pry.binding_for(eval(opts[:context]))
205
+ if opts[:context]
206
+ Pry.initial_session_setup
207
+ context = Pry.binding_for(eval(opts[:context]))
208
+ else
209
+ context = Pry.toplevel_binding
210
+ end
159
211
 
160
212
  if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
161
213
  full_name = File.expand_path(Pry::CLI.input_args.first)
162
214
  Pry.load_file_through_repl(full_name)
163
215
  exit
164
- elsif opts[:exec]
165
- exec_string = opts[:exec] + "\n"
166
- else
167
- exec_string = ""
168
216
  end
169
217
 
170
218
  # Start the session (running any code passed with -e, if there is any)
171
219
  Pry.start(context, :input => StringIO.new(exec_string))
172
220
  end
173
-
@@ -1,3 +1,7 @@
1
+ require 'pry/code/loc'
2
+ require 'pry/code/code_range'
3
+ require 'pry/code/code_file'
4
+
1
5
  class Pry
2
6
  class << self
3
7
  # Convert the given object into an instance of `Pry::Code`, if it isn't
@@ -32,23 +36,12 @@ class Pry
32
36
  # Instantiate a `Code` object containing code loaded from a file or
33
37
  # Pry's line buffer.
34
38
  #
35
- # @param [String] fn The name of a file, or "(pry)".
39
+ # @param [String] filename The name of a file, or "(pry)".
36
40
  # @param [Symbol] code_type The type of code the file contains.
37
41
  # @return [Code]
38
- def from_file(fn, code_type = nil)
39
- if fn == Pry.eval_path
40
- f = Pry.line_buffer.drop(1)
41
- else
42
- if File.readable?(fn)
43
- f = File.open(fn, 'r')
44
- code_type = type_from_filename(fn)
45
- else
46
- raise MethodSource::SourceNotFoundError, "Cannot open #{fn.inspect} for reading."
47
- end
48
- end
49
- new(f, 1, code_type || :ruby)
50
- ensure
51
- f.close if f.respond_to?(:close)
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)
52
45
  end
53
46
 
54
47
  # Instantiate a `Code` object containing code extracted from a
@@ -56,10 +49,10 @@ class Pry
56
49
  #
57
50
  # @param [::Method, UnboundMethod, Proc, Pry::Method] meth The method
58
51
  # object.
59
- # @param [Fixnum, nil] start_line The line number to start on, or nil to
52
+ # @param [Integer, nil] start_line The line number to start on, or nil to
60
53
  # use the method's original line numbers.
61
54
  # @return [Code]
62
- def from_method(meth, start_line=nil)
55
+ def from_method(meth, start_line = nil)
63
56
  meth = Pry::Method(meth)
64
57
  start_line ||= meth.source_line || 1
65
58
  new(meth.source, start_line, meth.source_type)
@@ -68,48 +61,16 @@ class Pry
68
61
  # Attempt to extract the source code for module (or class) `mod`.
69
62
  #
70
63
  # @param [Module, Class] mod The module (or class) of interest.
71
- # @param [Fixnum, nil] start_line The line number to start on, or nil to use the
72
- # method's original line numbers.
73
- # @param [Fixnum] candidate_rank The module candidate (by rank)
64
+ # @param [Integer] candidate_rank The module candidate (by rank)
74
65
  # to use (see `Pry::WrappedModule::Candidate` for more information).
66
+ # @param [Integer, nil] start_line The line number to start on, or nil to
67
+ # use the method's original line numbers.
75
68
  # @return [Code]
76
- def from_module(mod, start_line=nil, candidate_rank=0)
69
+ def from_module(mod, candidate_rank = 0, start_line=nil)
77
70
  candidate = Pry::WrappedModule(mod).candidate(candidate_rank)
78
-
79
71
  start_line ||= candidate.line
80
72
  new(candidate.source, start_line, :ruby)
81
73
  end
82
-
83
- protected
84
- # Guess the CodeRay type of a file from its extension, or nil if
85
- # unknown.
86
- #
87
- # @param [String] filename
88
- # @return [Symbol, nil]
89
- def type_from_filename(filename)
90
- map = {
91
- %w(.c .h) => :c,
92
- %w(.cpp .hpp .cc .h cxx) => :cpp,
93
- %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
94
- %w(.py) => :python,
95
- %w(.diff) => :diff,
96
- %w(.css) => :css,
97
- %w(.html) => :html,
98
- %w(.yaml .yml) => :yaml,
99
- %w(.xml) => :xml,
100
- %w(.php) => :php,
101
- %w(.js) => :javascript,
102
- %w(.java) => :java,
103
- %w(.rhtml) => :rhtml,
104
- %w(.json) => :json
105
- }
106
-
107
- _, type = map.find do |k, _|
108
- k.any? { |ext| ext == File.extname(filename) }
109
- end
110
-
111
- type
112
- end
113
74
  end
114
75
 
115
76
  # @return [Symbol] The type of code stored in this wrapper.
@@ -121,37 +82,39 @@ class Pry
121
82
  # empty `Code` object and then use `#push` to insert the lines.
122
83
  #
123
84
  # @param [Array<String>, String, IO] lines
124
- # @param [Fixnum?] start_line
85
+ # @param [Integer?] start_line
125
86
  # @param [Symbol?] code_type
126
- def initialize(lines=[], start_line=1, code_type=:ruby)
87
+ def initialize(lines = [], start_line = 1, code_type = :ruby)
127
88
  if lines.is_a? String
128
89
  lines = lines.lines
129
90
  end
130
-
131
- @lines = lines.each_with_index.map { |l, i| [l.chomp, i + start_line.to_i] }
91
+ @lines = lines.each_with_index.map { |line, lineno|
92
+ LOC.new(line, lineno + start_line.to_i) }
132
93
  @code_type = code_type
133
94
  end
134
95
 
135
- # Append the given line. `line_num` is one more than the last existing
96
+ # Append the given line. +lineno+ is one more than the last existing
136
97
  # line, unless specified otherwise.
137
98
  #
138
99
  # @param [String] line
139
- # @param [Fixnum?] line_num
100
+ # @param [Integer?] lineno
140
101
  # @return [String] The inserted line.
141
- def push(line, line_num=nil)
142
- line_num = @lines.last.last + 1 unless line_num
143
- @lines.push([line.chomp, line_num])
102
+ def push(line, lineno = nil)
103
+ if lineno.nil?
104
+ lineno = @lines.last.lineno + 1
105
+ end
106
+ @lines.push(LOC.new(line, lineno))
144
107
  line
145
108
  end
146
109
  alias << push
147
110
 
148
111
  # Filter the lines using the given block.
149
112
  #
150
- # @yield [line]
113
+ # @yield [LOC]
151
114
  # @return [Code]
152
- def select(&blk)
115
+ def select(&block)
153
116
  alter do
154
- @lines = @lines.select(&blk)
117
+ @lines = @lines.select(&block)
155
118
  end
156
119
  end
157
120
 
@@ -159,92 +122,74 @@ class Pry
159
122
  # `Range` object or a first and last line number (inclusive). Negative
160
123
  # indices count from the end of the array of lines.
161
124
  #
162
- # @param [Range, Fixnum] start_line
163
- # @param [Fixnum?] end_line
125
+ # @param [Range, Integer] start_line
126
+ # @param [Integer?] end_line
164
127
  # @return [Code]
165
- def between(start_line, end_line=nil)
128
+ def between(start_line, end_line = nil)
166
129
  return self unless start_line
167
130
 
168
- if start_line.is_a? Range
169
- end_line = start_line.last
170
- end_line -= 1 if start_line.exclude_end?
171
-
172
- start_line = start_line.first
173
- else
174
- end_line ||= start_line
175
- end
176
-
177
- if start_line > 0
178
- start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
179
- else
180
- start_idx = start_line
181
- end
182
-
183
- if end_line > 0
184
- end_idx = (@lines.index { |l| l.last > end_line } || 0) - 1
185
- else
186
- end_idx = end_line
187
- end
131
+ code_range = CodeRange.new(start_line, end_line)
188
132
 
189
133
  alter do
190
- @lines = @lines[start_idx..end_idx] || []
134
+ @lines = @lines[code_range.indices_range(@lines)] || []
191
135
  end
192
136
  end
193
137
 
194
- # Take `num_lines` from `start_line`, forward or backwards
138
+ # Take `num_lines` from `start_line`, forward or backwards.
195
139
  #
196
- # @param [Fixnum] start_line
197
- # @param [Fixnum] num_lines
140
+ # @param [Integer] start_line
141
+ # @param [Integer] num_lines
198
142
  # @return [Code]
199
143
  def take_lines(start_line, num_lines)
200
- if start_line >= 0
201
- start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
202
- else
203
- start_idx = @lines.length + start_line
204
- end
144
+ start_idx =
145
+ if start_line >= 0
146
+ @lines.index { |loc| loc.lineno >= start_line } || @lines.length
147
+ else
148
+ [@lines.length + start_line, 0].max
149
+ end
205
150
 
206
151
  alter do
207
152
  @lines = @lines.slice(start_idx, num_lines)
208
153
  end
209
154
  end
210
155
 
211
- # Remove all lines except for the `lines` up to and excluding `line_num`.
156
+ # Remove all lines except for the +lines+ up to and excluding +lineno+.
212
157
  #
213
- # @param [Fixnum] line_num
214
- # @param [Fixnum] lines
158
+ # @param [Integer] lineno
159
+ # @param [Integer] lines
215
160
  # @return [Code]
216
- def before(line_num, lines=1)
217
- return self unless line_num
161
+ def before(lineno, lines = 1)
162
+ return self unless lineno
218
163
 
219
- select do |l, ln|
220
- ln >= line_num - lines && ln < line_num
164
+ select do |loc|
165
+ loc.lineno >= lineno - lines && loc.lineno < lineno
221
166
  end
222
167
  end
223
168
 
224
- # Remove all lines except for the `lines` on either side of and including
225
- # `line_num`.
169
+ # Remove all lines except for the +lines+ on either side of and including
170
+ # +lineno+.
226
171
  #
227
- # @param [Fixnum] line_num
228
- # @param [Fixnum] lines
172
+ # @param [Integer] lineno
173
+ # @param [Integer] lines
229
174
  # @return [Code]
230
- def around(line_num, lines=1)
231
- return self unless line_num
175
+ def around(lineno, lines = 1)
176
+ return self unless lineno
232
177
 
233
- select do |l, ln|
234
- ln >= line_num - lines && ln <= line_num + lines
178
+ select do |loc|
179
+ loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
235
180
  end
236
181
  end
237
182
 
238
- # Remove all lines except for the `lines` after and excluding `line_num`.
183
+ # Remove all lines except for the +lines+ after and excluding +lineno+.
239
184
  #
240
- # @param [Fixnum] line_num
241
- # @param [Fixnum] lines
185
+ # @param [Integer] lineno
186
+ # @param [Integer] lines
242
187
  # @return [Code]
243
- def after(line_num, lines=1)
244
- return self unless line_num
188
+ def after(lineno, lines = 1)
189
+ return self unless lineno
245
190
 
246
- select do |l, ln|
247
- ln > line_num && ln <= line_num + lines
191
+ select do |loc|
192
+ loc.lineno > lineno && loc.lineno <= lineno + lines
248
193
  end
249
194
  end
250
195
 
@@ -256,8 +201,8 @@ class Pry
256
201
  return self unless pattern
257
202
  pattern = Regexp.new(pattern)
258
203
 
259
- select do |l, ln|
260
- l =~ pattern
204
+ select do |loc|
205
+ loc.line =~ pattern
261
206
  end
262
207
  end
263
208
 
@@ -265,30 +210,30 @@ class Pry
265
210
  #
266
211
  # @param [Boolean?] y_n
267
212
  # @return [Code]
268
- def with_line_numbers(y_n=true)
213
+ def with_line_numbers(y_n = true)
269
214
  alter do
270
215
  @with_line_numbers = y_n
271
216
  end
272
217
  end
273
218
 
274
- # Format output with a marker next to the given `line_num`, unless `line_num`
275
- # is falsy.
219
+ # Format output with a marker next to the given +lineno+, unless +lineno+ is
220
+ # falsy.
276
221
  #
277
- # @param [Fixnum?] line_num
222
+ # @param [Integer?] lineno
278
223
  # @return [Code]
279
- def with_marker(line_num=1)
224
+ def with_marker(lineno = 1)
280
225
  alter do
281
- @with_marker = !!line_num
282
- @marker_line_num = line_num
226
+ @with_marker = !!lineno
227
+ @marker_lineno = lineno
283
228
  end
284
229
  end
285
230
 
286
231
  # Format output with the specified number of spaces in front of every line,
287
232
  # unless `spaces` is falsy.
288
233
  #
289
- # @param [Fixnum?] spaces
234
+ # @param [Integer?] spaces
290
235
  # @return [Code]
291
- def with_indentation(spaces=0)
236
+ def with_indentation(spaces = 0)
292
237
  alter do
293
238
  @with_indentation = !!spaces
294
239
  @indentation_num = spaces
@@ -300,72 +245,67 @@ class Pry
300
245
  Object.instance_method(:to_s).bind(self).call
301
246
  end
302
247
 
303
- # Based on the configuration of the object, return a formatted String
304
- # representation.
305
- #
306
- # @return [String]
307
- def to_s
308
- lines = @lines.map(&:dup)
309
-
310
- if Pry.color
311
- lines.each do |l|
312
- l[0] = CodeRay.scan(l[0], @code_type).term
313
- end
314
- end
315
-
316
- if @with_line_numbers
317
- max_width = lines.last.last.to_s.length if lines.length > 0
318
- lines.each do |l|
319
- padded_line_num = l[1].to_s.rjust(max_width)
320
- l[0] = "#{Pry::Helpers::BaseHelpers.colorize_code(padded_line_num.to_s)}: #{l[0]}"
321
- end
322
- end
248
+ # @return [Integer] the number of digits in the last line.
249
+ def max_lineno_width
250
+ @lines.length > 0 ? @lines.last.lineno.to_s.length : 0
251
+ end
323
252
 
324
- if @with_marker
325
- lines.each do |l|
326
- if l[1] == @marker_line_num
327
- l[0] = " => #{l[0]}"
328
- else
329
- l[0] = " #{l[0]}"
330
- end
331
- end
332
- end
253
+ # @return [String] a formatted representation (based on the configuration of
254
+ # the object).
255
+ def to_s
256
+ print_to_output("")
257
+ end
333
258
 
334
- if @with_indentation
335
- lines.each do |l|
336
- l[0] = "#{' ' * @indentation_num}#{l[0]}"
337
- end
259
+ # Writes a formatted representation (based on the configuration of the
260
+ # object) to the given output, which must respond to `#<<`.
261
+ def print_to_output(output, color=false)
262
+ @lines.each do |loc|
263
+ loc = loc.dup
264
+ loc.colorize(@code_type)
265
+ loc.add_line_number(max_lineno_width, color) if @with_line_numbers
266
+ loc.add_marker(@marker_lineno) if @with_marker
267
+ loc.indent(@indentation_num) if @with_indentation
268
+ output << loc.line
269
+ output << "\n"
338
270
  end
339
-
340
- lines.map { |l| "#{l.first}\n" }.join
271
+ output
341
272
  end
342
273
 
343
274
  # Get the comment that describes the expression on the given line number.
344
275
  #
345
- # @param [Fixnum] line_number (1-based)
346
- # @return [String] the code.
276
+ # @param [Integer] line_number (1-based)
277
+ # @return [String] the code.
347
278
  def comment_describing(line_number)
348
279
  self.class.comment_describing(raw, line_number)
349
280
  end
350
281
 
351
282
  # Get the multiline expression that starts on the given line number.
352
283
  #
353
- # @param [Fixnum] line_number (1-based)
354
- # @return [String] the code.
355
- def expression_at(line_number, consume=0)
284
+ # @param [Integer] line_number (1-based)
285
+ # @return [String] the code.
286
+ def expression_at(line_number, consume = 0)
356
287
  self.class.expression_at(raw, line_number, :consume => consume)
357
288
  end
358
289
 
290
+ # Get the (approximate) Module.nesting at the give line number.
291
+ #
292
+ # @param [Integer] line_number line number starting from 1
293
+ # @param [Module] top_module the module in which this code exists
294
+ # @return [Array<Module>] a list of open modules.
295
+ def nesting_at(line_number, top_module = Object)
296
+ Pry::Indent.nesting_at(raw, line_number)
297
+ end
298
+
359
299
  # Return an unformatted String of the code.
360
300
  #
361
301
  # @return [String]
362
302
  def raw
363
- @lines.map(&:first).join("\n") + "\n"
303
+ @lines.map(&:line).join("\n") << "\n"
364
304
  end
365
305
 
366
306
  # Return the number of lines stored.
367
307
  #
368
- # @return [Fixnum]
308
+ # @return [Integer]
369
309
  def length
370
310
  @lines ? @lines.length : 0
371
311
  end
@@ -377,26 +317,25 @@ class Pry
377
317
  # @return [Boolean]
378
318
  def ==(other)
379
319
  if other.is_a?(Code)
380
- @other_lines = other.instance_variable_get(:@lines)
381
- @lines.each_with_index.all? do |(l, ln), i|
382
- l == @other_lines[i].first && ln == @other_lines[i].last
383
- end
320
+ other_lines = other.instance_variable_get(:@lines)
321
+ @lines.each_with_index.all? { |loc, i| loc == other_lines[i] }
384
322
  else
385
323
  to_s.chomp == other.to_s.chomp
386
324
  end
387
325
  end
388
326
 
389
327
  # Forward any missing methods to the output of `#to_s`.
390
- def method_missing(name, *args, &blk)
391
- to_s.send(name, *args, &blk)
328
+ def method_missing(name, *args, &block)
329
+ to_s.send(name, *args, &block)
392
330
  end
393
331
  undef =~
394
332
 
395
333
  protected
396
- # An abstraction of the `dup.instance_eval` pattern used throughout this
397
- # class.
398
- def alter(&blk)
399
- dup.tap { |o| o.instance_eval(&blk) }
400
- end
334
+
335
+ # An abstraction of the `dup.instance_eval` pattern used throughout this
336
+ # class.
337
+ def alter(&block)
338
+ dup.tap { |o| o.instance_eval(&block) }
339
+ end
401
340
  end
402
341
  end