diff --git a/lib/pry/commands/gem_cd.rb b/lib/pry/commands/gem_cd.rb index a1aa1434..a51598b4 100644 --- a/lib/pry/commands/gem_cd.rb +++ b/lib/pry/commands/gem_cd.rb @@ -14,5 +14,9 @@ class Pry Dir.chdir(gem_spec(gem).full_gem_path) output.puts(Dir.pwd) end + + def complete(str) + gem_complete(str) + end end end diff --git a/lib/pry/commands/gem_list.rb b/lib/pry/commands/gem_list.rb index de010b0a..faa0aa99 100644 --- a/lib/pry/commands/gem_list.rb +++ b/lib/pry/commands/gem_list.rb @@ -12,11 +12,7 @@ class Pry def process(pattern=nil) pattern = Regexp.compile(pattern || '') - gems = if Gem::Specification.respond_to?(:each) - Gem::Specification.select{|spec| spec.name =~ pattern }.group_by(&:name) - else - Gem.source_index.gems.values.group_by(&:name).select { |gemname, specs| gemname =~ pattern } - end + gems = gem_list(pattern).group_by(&:name) gems.each do |gem, specs| specs.sort! do |a,b| diff --git a/lib/pry/commands/gem_open.rb b/lib/pry/commands/gem_open.rb index 8f443ff0..a4e2f7aa 100644 --- a/lib/pry/commands/gem_open.rb +++ b/lib/pry/commands/gem_open.rb @@ -16,5 +16,9 @@ class Pry invoke_editor(".", 0, false) end end + + def complete(str) + gem_complete(str) + end end end diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 2af4e10d..614afcd9 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -276,6 +276,28 @@ class Pry spec or raise CommandError, "Gem `#{gem}` not found" end + + # List gems matching a pattern + # @param [Regexp] pattern + # @return [Array] + def gem_list(pattern=/.*/) + if Gem::Specification.respond_to?(:each) + Gem::Specification.select{|spec| spec.name =~ pattern } + else + Gem.source_index.gems.values.select{|spec| spec.name =~ pattern } + end + end + + # Completion function for gem-cd and gem-open + # @param [String] so_far what the user's typed so far + # @return [Array] completions + def gem_complete(so_far) + if so_far =~ / ([^ ]*)\z/ + gem_list(%r{\A#{$2}}).map(&:name) + else + gem_list.map(&:name) + end + end end end end