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

ruby-changes:65639

From: aycabta <ko1@a...>
Date: Wed, 24 Mar 2021 15:11:59 +0900 (JST)
Subject: [ruby-changes:65639] 0927756e58 (master): [ruby/irb] Complete require and require_relative

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

From 0927756e58c7d68517a1468f2327ce50989ff3f2 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Wed, 24 Mar 2021 14:55:05 +0900
Subject: [ruby/irb] Complete require and require_relative

https://github.com/ruby/irb/commit/1c61178b4c
---
 lib/irb/completion.rb       | 58 +++++++++++++++++++++++++++++++++++++++++++--
 lib/irb/input-method.rb     |  1 +
 test/irb/test_completion.rb | 14 +++++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb
index 22a1ad1..0385142 100644
--- a/lib/irb/completion.rb
+++ b/lib/irb/completion.rb
@@ -9,6 +9,8 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L9
 
 autoload :RDoc, "rdoc"
 
+require_relative 'ruby-lex'
+
 module IRB
   module InputCompletor # :nodoc:
 
@@ -38,8 +40,60 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L40
 
     BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
 
-    CompletionProc = proc { |input|
-      retrieve_completion_data(input).compact.map{ |i| i.encode(Encoding.default_external) }
+    CompletionRequireProc = lambda { |target, preposing = nil, postposing = nil|
+      if target =~ /\A(['"])([^'"]+)\Z/
+        quote = $1
+        actual_target = $2
+      else
+        return nil # It's not String literal
+      end
+      tokens = RubyLex.ripper_lex_without_warning(preposing.gsub(/\s*\z/, ''))
+      tok = nil
+      tokens.reverse_each do |t|
+        unless [:on_lparen, :on_sp, :on_ignored_sp, :on_nl, :on_ignored_nl, :on_comment].include?(t.event)
+          tok = t
+          break
+        end
+      end
+      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|
+            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|
+            path.start_with?(actual_target)
+          }.map { |path|
+            quote + path
+          }
+        end
+      end
+      result
+    }
+
+    CompletionProc = lambda { |target, preposing = nil, postposing = nil|
+      if preposing && postposing
+        result = CompletionRequireProc.(target, preposing, postposing)
+        unless result
+          result = retrieve_completion_data(target).compact.map{ |i| i.encode(Encoding.default_external) }
+        end
+        result
+      else
+        retrieve_completion_data(target).compact.map{ |i| i.encode(Encoding.default_external) }
+      end
     }
 
     def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace.binding, doc_namespace: false)
diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index e223672..1854567 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -280,6 +280,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L280
         Reline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
       end
       Reline.completion_append_character = nil
+      Reline.completer_quote_characters = ''
       Reline.completion_proc = IRB::InputCompletor::CompletionProc
       Reline.output_modifier_proc =
         if IRB.conf[:USE_COLORIZE]
diff --git a/test/irb/test_completion.rb b/test/irb/test_completion.rb
index 984453d..de043c7 100644
--- a/test/irb/test_completion.rb
+++ b/test/irb/test_completion.rb
@@ -55,5 +55,19 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_completion.rb#L55
       namespace = IRB::InputCompletor.retrieve_completion_data("1.positive?", bind: binding, doc_namespace: true)
       assert_equal "Integer.positive?", namespace
     end
+
+    def test_complete_require
+      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
+      candidates = IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
+      %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/

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