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

ruby-changes:65655

From: aycabta <ko1@a...>
Date: Thu, 25 Mar 2021 18:54:16 +0900 (JST)
Subject: [ruby-changes:65655] ad8842c06d (master): [ruby/irb] Cache completion files to require

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

From ad8842c06d26ee634f90008efecf1cd4d76342df Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Wed, 24 Mar 2021 15:33:07 +0900
Subject: [ruby/irb] Cache completion files to require

https://github.com/ruby/irb/commit/612ebcb311
---
 lib/irb/completion.rb       | 33 +++++++++++++++++++++------------
 test/irb/test_completion.rb | 12 ++++++++++++
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb
index 0385142..099dd85 100644
--- a/lib/irb/completion.rb
+++ b/lib/irb/completion.rb
@@ -40,6 +40,24 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L40
 
     BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
 
+    def self.retrieve_files_to_require_from_load_path
+      @@files_from_load_path ||= $LOAD_PATH.flat_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/, '')
+      }
+    end
+
+    def self.retrieve_files_to_require_relative_from_current_dir
+      @@files_from_current_dir ||= Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
+        path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
+      }
+    end
+
     CompletionRequireProc = lambda { |target, preposing = nil, postposing = nil|
       if target =~ /\A(['"])([^'"]+)\Z/
         quote = $1
@@ -55,26 +73,17 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L73
           break
         end
       end
+      result = []
       if tok && tok.event == :on_ident && tok.state == Ripper::EXPR_CMDARG
         case tok.tok
         when 'require'
-          result = $LOAD_PATH.flat_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/, '')
-          }.select { |path|
+          result = retrieve_files_to_require_from_load_path.select { |path|
             path.start_with?(actual_target)
           }.map { |path|
             quote + path
           }
         when 'require_relative'
-          result = Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
-            path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
-          }.select { |path|
+          result = retrieve_files_to_require_relative_from_current_dir.select { |path|
             path.start_with?(actual_target)
           }.map { |path|
             quote + path
diff --git a/test/irb/test_completion.rb b/test/irb/test_completion.rb
index 04ac6db..535690a 100644
--- a/test/irb/test_completion.rb
+++ b/test/irb/test_completion.rb
@@ -61,6 +61,11 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_completion.rb#L61
       %w['irb/init 'irb/ruby-lex].each do |word|
         assert_include candidates, word
       end
+      # Test cache
+      candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
+      %w['irb/init 'irb/ruby-lex].each do |word|
+        assert_include candidates, word
+      end
     end
 
     def test_complete_require_relative
@@ -70,6 +75,13 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_completion.rb#L75
       %w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
         assert_include candidates, word
       end
+      # Test cache
+      candidates = Dir.chdir(__dir__ + "/../..") do
+        IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
+      end
+      %w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
+        assert_include candidates, word
+      end
     end
   end
 end
-- 
cgit v1.1


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

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