From 02c6b34555e6fffaaf22cbd74476dd71d4e583e7 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@gmail.com>
Date: Wed, 9 Nov 2022 23:38:29 -0800
Subject: [PATCH 1/4] Transform ls's --grep/-G option to keyword args

---
 lib/irb/cmd/ls.rb    |  9 +++++++++
 test/irb/test_cmd.rb | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb
index f4a7348bd..f9dea6b4d 100644
--- a/lib/irb/cmd/ls.rb
+++ b/lib/irb/cmd/ls.rb
@@ -9,6 +9,15 @@ module IRB
 
   module ExtendCommand
     class Ls < Nop
+      def self.transform_args(args)
+        if match = args&.match(/\A(?<prefix>.+\s|)(--grep|-G)\s+(?<grep>[^\s]+)(?<suffix>\s.+|)\n\z/)
+          args = match[:prefix] + match[:suffix]
+          "#{args}#{',' unless args.chomp.empty?} grep: /#{match[:grep]}/"
+        else
+          args
+        end
+      end
+
       def execute(*arg, grep: nil)
         o = Output.new(grep: grep)
 
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index d233cbb9b..a59975ee4 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -480,6 +480,46 @@ def test_ls
       assert_match(/C.methods:\s+m5\n/m, out)
     end
 
+    def test_ls_grep
+      pend if RUBY_ENGINE == 'truffleruby'
+      out, err = execute_lines("ls 42\n")
+      assert_empty err
+      assert_match(/times/, out)
+      assert_match(/polar/, out)
+
+      [
+        "ls 42, grep: /times/\n",
+        "ls 42 --grep times\n",
+        "ls 42 -G times\n",
+        "ls --grep times 42\n",
+        "ls -G times 42\n",
+      ].each do |line|
+        out, err = execute_lines(line)
+        assert_empty err
+        assert_match(/times/, out)
+        assert_not_match(/polar/, out)
+      end
+    end
+
+    def test_ls_grep_empty
+      pend if RUBY_ENGINE == 'truffleruby'
+      out, err = execute_lines("ls\n")
+      assert_empty err
+      assert_match(/whereami/, out)
+      assert_match(/show_source/, out)
+
+      [
+        "ls grep: /whereami/\n",
+        "ls --grep whereami\n",
+        "ls -G whereami\n",
+      ].each do |line|
+        out, err = execute_lines(line)
+        assert_empty err
+        assert_match(/whereami/, out)
+        assert_not_match(/show_source/, out)
+      end
+    end
+
     def test_ls_with_no_singleton_class
       out, err = execute_lines(
         "ls 42",

From 0af18498606b5604f89a4049ead1fd037eeb23dc Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@gmail.com>
Date: Thu, 10 Nov 2022 14:39:17 -0800
Subject: [PATCH 2/4] Make --grep less flexible

---
 lib/irb/cmd/ls.rb    | 4 ++--
 test/irb/test_cmd.rb | 2 --
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb
index f9dea6b4d..439c06ddf 100644
--- a/lib/irb/cmd/ls.rb
+++ b/lib/irb/cmd/ls.rb
@@ -10,8 +10,8 @@ module IRB
   module ExtendCommand
     class Ls < Nop
       def self.transform_args(args)
-        if match = args&.match(/\A(?<prefix>.+\s|)(--grep|-G)\s+(?<grep>[^\s]+)(?<suffix>\s.+|)\n\z/)
-          args = match[:prefix] + match[:suffix]
+        if match = args&.match(/\A(?<args>.+\s|)(--grep|-G)\s+(?<grep>[^\s]+)\s*\n\z/)
+          args = match[:args]
           "#{args}#{',' unless args.chomp.empty?} grep: /#{match[:grep]}/"
         else
           args
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index a59975ee4..e08cdb9ac 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -491,8 +491,6 @@ def test_ls_grep
         "ls 42, grep: /times/\n",
         "ls 42 --grep times\n",
         "ls 42 -G times\n",
-        "ls --grep times 42\n",
-        "ls -G times 42\n",
       ].each do |line|
         out, err = execute_lines(line)
         assert_empty err

From 9e535719a073a4fd44aaacea27b22cab516cf76a Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@gmail.com>
Date: Thu, 10 Nov 2022 14:40:09 -0800
Subject: [PATCH 3/4] Support -g instead of --grep

---
 lib/irb/cmd/ls.rb    | 2 +-
 test/irb/test_cmd.rb | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb
index 439c06ddf..77cf07178 100644
--- a/lib/irb/cmd/ls.rb
+++ b/lib/irb/cmd/ls.rb
@@ -10,7 +10,7 @@ module IRB
   module ExtendCommand
     class Ls < Nop
       def self.transform_args(args)
-        if match = args&.match(/\A(?<args>.+\s|)(--grep|-G)\s+(?<grep>[^\s]+)\s*\n\z/)
+        if match = args&.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\n\z/)
           args = match[:args]
           "#{args}#{',' unless args.chomp.empty?} grep: /#{match[:grep]}/"
         else
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index e08cdb9ac..eafa8be38 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -489,7 +489,7 @@ def test_ls_grep
 
       [
         "ls 42, grep: /times/\n",
-        "ls 42 --grep times\n",
+        "ls 42 -g times\n",
         "ls 42 -G times\n",
       ].each do |line|
         out, err = execute_lines(line)
@@ -508,7 +508,7 @@ def test_ls_grep_empty
 
       [
         "ls grep: /whereami/\n",
-        "ls --grep whereami\n",
+        "ls -g whereami\n",
         "ls -G whereami\n",
       ].each do |line|
         out, err = execute_lines(line)

From 14877538f5778ef060bcbf595a297dcb931e6866 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@gmail.com>
Date: Thu, 10 Nov 2022 14:52:19 -0800
Subject: [PATCH 4/4] Suppress warnings from symbol aliases

---
 lib/irb.rb | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lib/irb.rb b/lib/irb.rb
index 57ec9ebae..04009664e 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -427,6 +427,7 @@ def initialize(workspace = nil, input_method = nil)
       @context = Context.new(self, workspace, input_method)
       @context.main.extend ExtendCommandBundle
       @context.command_aliases.each do |alias_name, cmd_name|
+        next if @context.symbol_alias(alias_name)
         @context.main.install_alias_method(alias_name, cmd_name)
       end
       @signal_status = :IN_IRB
