Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add get_context abstraction to cd.

  • Loading branch information...
commit 5e4463b50d42cbb06c82209a24d0d49638b67c38 1 parent ef482bb
Ingrid ingrid authored jasonLaster committed
Showing with 59 additions and 49 deletions.
  1. +59 −49 lib/pry/default_commands/cd.rb
108 lib/pry/default_commands/cd.rb
View
@@ -22,60 +22,70 @@ module DefaultCommands
BANNER
def process
- # 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
- old_stack = state.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
+ stack, old_stack = Pry.get_context(arg_string, _pry_, state.old_stack||[])
+ state.old_stack = old_stack
+ _pry_.binding_stack = stack unless stack.nil?
+ 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
- end
- end
+ end
+ end
+ end
+
+ def self.get_context(arg_string, pry=nil, old_stack=[])
- _pry_.binding_stack = 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
+
Please sign in to comment.
Something went wrong with that request. Please try again.