[前][次][番号順一覧][スレッド一覧]

ruby-changes:67660

From: aycabta <ko1@a...>
Date: Wed, 8 Sep 2021 05:24:08 +0900 (JST)
Subject: [ruby-changes:67660] bc24af0f35 (master): [ruby/irb] Show lib name first because it's the most common use case

https://git.ruby-lang.org/ruby.git/commit/?id=bc24af0f35

From bc24af0f355c71ccc8e57ec95f771fa0737bd27d Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Wed, 8 Sep 2021 05:07:24 +0900
Subject: [ruby/irb] Show lib name first because it's the most common use case

https://github.com/ruby/irb/commit/74d635758b
---
 lib/irb/completion.rb       | 11 +++++++----
 test/irb/test_completion.rb |  5 +++++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb
index 597dcda..812ebb0 100644
--- a/lib/irb/completion.rb
+++ b/lib/irb/completion.rb
@@ -64,15 +64,18 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L64
     end
 
     def self.retrieve_files_to_require_from_load_path
-      @@files_from_load_path ||= retrieve_gem_and_system_load_path.flat_map { |path|
+      @@files_from_load_path ||= retrieve_gem_and_system_load_path.map { |path|
         begin
           Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: path)
         rescue Errno::ENOENT
           []
         end
-      }.uniq.map { |path|
-        path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
-      }
+      }.inject([]) { |result, names|
+        shortest, *rest = names.map{ |n| n.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '') }.sort
+        result.unshift(shortest) if shortest
+        result.concat(rest)
+        result
+      }.uniq
     end
 
     def self.retrieve_files_to_require_relative_from_current_dir
diff --git a/test/irb/test_completion.rb b/test/irb/test_completion.rb
index c534301..c3462f4 100644
--- a/test/irb/test_completion.rb
+++ b/test/irb/test_completion.rb
@@ -68,6 +68,11 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_completion.rb#L68
       end
     end
 
+    def test_complete_require_library_name_first
+      candidates = IRB::InputCompletor::CompletionProc.("'cgi", "require ", "")
+      assert_equal candidates.first, "'cgi"
+    end
+
     def test_complete_require_relative
       candidates = Dir.chdir(__dir__ + "/../..") do
         IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]