Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from pry/tabcomplete_take2

Renamed Pry.get_context and moved to a more suitable place
  • Loading branch information...
commit 25de725da05ed2b07039d0db627552749bbeb8ac 2 parents cfd36d8 + f4af4ea
@jasonLaster jasonLaster authored
Showing with 72 additions and 68 deletions.
  1. +1 −57 lib/pry/default_commands/cd.rb
  2. +71 −11 lib/pry/helpers/base_helpers.rb
View
58 lib/pry/default_commands/cd.rb
@@ -22,7 +22,7 @@ module DefaultCommands
BANNER
def process
- stack, old_stack = Pry.get_context(arg_string, _pry_, state.old_stack||[])
+ stack, old_stack = context_from_object_path(arg_string, _pry_, state.old_stack||[])
state.old_stack = old_stack
_pry_.binding_stack = stack unless stack.nil?
end
@@ -31,61 +31,5 @@ def process
end
end
- def self.get_context(arg_string, pry=nil, old_stack=[])
-
- # Extract command arguments. Delete blank arguments like " ", but
- # don't delete empty strings like "".
- path = arg_string.split(/\//).delete_if { |a| a =~ /\A\s+\z/ }
- stack = pry.binding_stack.dup
- state_old_stack = old_stack
-
- # Special case when we only get a single "/", return to root.
- if path.empty?
- state_old_stack = stack.dup unless old_stack.empty?
- stack = [stack.first]
- end
-
- path.each_with_index do |context, i|
- begin
- case context.chomp
- when ""
- state_old_stack = stack.dup
- stack = [stack.first]
- when "::"
- state_old_stack = stack.dup
- stack.push(TOPLEVEL_BINDING)
- when "."
- next
- when ".."
- unless stack.size == 1
- # Don't rewrite old_stack if we're in complex expression
- # (e.g.: `cd 1/2/3/../4).
- state_old_stack = stack.dup if path.first == ".."
- stack.pop
- end
- when "-"
- unless old_stack.empty?
- # Interchange current stack and old stack with each other.
- stack, state_old_stack = state_old_stack, stack
- end
- else
- state_old_stack = stack.dup if i == 0
- stack.push(Pry.binding_for(stack.last.eval(context)))
- end
-
- rescue RescuableException => e
-
- # Restore old stack to its initial values.
- state_old_stack = old_stack
-
- output.puts "Bad object path: #{arg_string.chomp}. Failed trying to resolve: #{context}"
- output.puts e.inspect
- return nil, state_old_stack
- end
- end
- return stack, state_old_stack
- end
-
-
end
View
82 lib/pry/helpers/base_helpers.rb
@@ -70,7 +70,7 @@ def create_command_stub(names, description, options, block)
end
def use_ansi_codes?
- windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
+ windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
end
def colorize_code(code)
@@ -141,16 +141,16 @@ def simple_pager(text, output=output())
# FIXME! Another JRuby hack
def stagger_output(text, out = nil)
out ||= case
- when respond_to?(:output)
- # Mixin.
- output
- when Pry.respond_to?(:output)
- # Parent.
- Pry.output
- else
- # Sys.
- $stdout
- end
+ when respond_to?(:output)
+ # Mixin.
+ output
+ when Pry.respond_to?(:output)
+ # Parent.
+ Pry.output
+ else
+ # Sys.
+ $stdout
+ end
if text.lines.count < page_size || !Pry.pager
out.puts text
@@ -222,6 +222,66 @@ def lesspipe(*args)
end
end
+ # @param [String] arg_string The object path expressed as a string.
+ # @param [Pry] _pry_ The relevant Pry instance.
+ # @param [Array<Binding>] old_stack The state of the old binding stack
+ # @return [Array<Array<Binding>, Array<Binding>>] An array
+ # containing two elements: The new `binding_stack` and the old `binding_stack`.
+ def context_from_object_path(arg_string, _pry_=nil, old_stack=[])
+
+ # Extract command arguments. Delete blank arguments like " ", but
+ # don't delete empty strings like "".
+ path = arg_string.split(/\//).delete_if { |a| a =~ /\A\s+\z/ }
+ stack = _pry_.binding_stack.dup
+ state_old_stack = old_stack
+
+ # Special case when we only get a single "/", return to root.
+ if path.empty?
+ state_old_stack = stack.dup unless old_stack.empty?
+ stack = [stack.first]
+ end
+
+ path.each_with_index do |context, i|
+ begin
+ case context.chomp
+ when ""
+ state_old_stack = stack.dup
+ stack = [stack.first]
+ when "::"
+ state_old_stack = stack.dup
+ stack.push(TOPLEVEL_BINDING)
+ when "."
+ next
+ when ".."
+ unless stack.size == 1
+ # Don't rewrite old_stack if we're in complex expression
+ # (e.g.: `cd 1/2/3/../4).
+ state_old_stack = stack.dup if path.first == ".."
+ stack.pop
+ end
+ when "-"
+ unless old_stack.empty?
+ # Interchange current stack and old stack with each other.
+ stack, state_old_stack = state_old_stack, stack
+ end
+ else
+ state_old_stack = stack.dup if i == 0
+ stack.push(Pry.binding_for(stack.last.eval(context)))
+ end
+
+ rescue RescuableException => e
+
+ # Restore old stack to its initial values.
+ state_old_stack = old_stack
+
+ output.puts "Bad object path: #{arg_string.chomp}. Failed trying to resolve: #{context}"
+ output.puts e.inspect
+ return nil, state_old_stack
+ end
+ end
+ return stack, state_old_stack
+ end
+
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.