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

ruby-changes:67348

From: aycabta <ko1@a...>
Date: Mon, 30 Aug 2021 02:37:41 +0900 (JST)
Subject: [ruby-changes:67348] 935bb4c617 (master): [ruby/reline] Add autocompletion on emacs mode by Tab / S-Tab

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

From 935bb4c6173c7868e176e7ba73ea45a0688b124d Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Mon, 30 Aug 2021 01:23:57 +0900
Subject: [ruby/reline] Add autocompletion on emacs mode by Tab / S-Tab

https://github.com/ruby/reline/commit/22d0b4e5d8
---
 lib/reline.rb             |  1 +
 lib/reline/ansi.rb        |  1 +
 lib/reline/line_editor.rb | 18 ++++++++++++++++--
 lib/reline/windows.rb     | 12 ++++++++++++
 4 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/lib/reline.rb b/lib/reline.rb
index 188f1cb..f5ebe79 100644
--- a/lib/reline.rb
+++ b/lib/reline.rb
@@ -180,6 +180,7 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L180
 
     Reline::DEFAULT_DIALOG_PROC_AUTOCOMPLETE = ->() {
       # autocomplete
+      return nil unless config.autocompletion
       if just_cursor_moving and completion_journey_data.nil?
         # Auto complete starts only when edited
         return nil
diff --git a/lib/reline/ansi.rb b/lib/reline/ansi.rb
index f642060..f34d0b5 100644
--- a/lib/reline/ansi.rb
+++ b/lib/reline/ansi.rb
@@ -37,6 +37,7 @@ class Reline::ANSI https://github.com/ruby/ruby/blob/trunk/lib/reline/ansi.rb#L37
       # default bindings
       [27, 32] => :em_set_mark,             # M-<space>
       [24, 24] => :em_exchange_mark,        # C-x C-x
+      [27, 91, 90] => :completion_journey_up, # S-Tab
     }.each_pair do |key, func|
       config.add_default_key_binding_by_keymap(:emacs, key, func)
     end
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
index 5e3b836..c6d0c9f 100644
--- a/lib/reline/line_editor.rb
+++ b/lib/reline/line_editor.rb
@@ -479,8 +479,9 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L479
   end
 
   class DialogProcScope
-    def initialize(line_editor, proc_to_exec, context)
+    def initialize(line_editor, config, proc_to_exec, context)
       @line_editor = line_editor
+      @config = config
       @proc_to_exec = proc_to_exec
       @context = context
       @cursor_pos = Reline::CursorPos.new
@@ -519,6 +520,10 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L520
       @line_editor.instance_variable_get(:@completion_journey_data)
     end
 
+    def config
+      @config
+    end
+
     def call
       instance_exec(&@proc_to_exec)
     end
@@ -544,7 +549,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L549
 
   def add_dialog_proc(name, p, context = nil)
     return if @dialogs.any? { |d| d.name == name }
-    @dialogs << Dialog.new(name, DialogProcScope.new(self, p, context))
+    @dialogs << Dialog.new(name, DialogProcScope.new(self, @config, p, context))
   end
 
   DIALOG_HEIGHT = 20
@@ -1433,6 +1438,15 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L1438
           end
         end
       end
+    elsif @config.editing_mode_is?(:emacs, :vi_insert) and key.char == :completion_journey_up
+      if not @config.disable_completion and @config.autocompletion
+        result = call_completion_proc
+        if result.is_a?(Array)
+          completion_occurs = true
+          process_insert
+          move_completed_list(result, :up)
+        end
+      end
     elsif not @config.disable_completion and @config.editing_mode_is?(:vi_insert) and ["\C-p".ord, "\C-n".ord].include?(key.char)
       unless @config.disable_completion
         result = call_completion_proc
diff --git a/lib/reline/windows.rb b/lib/reline/windows.rb
index 9e542aa..b3443aa 100644
--- a/lib/reline/windows.rb
+++ b/lib/reline/windows.rb
@@ -42,6 +42,14 @@ class Reline::Windows https://github.com/ruby/ruby/blob/trunk/lib/reline/windows.rb#L42
     }.each_pair do |key, func|
       config.add_default_key_binding_by_keymap(:emacs, key, func)
     end
+
+    # Emulate ANSI key sequence.
+    {
+      [27, 91, 90] => :completion_journey_up, # S-Tab
+    }.each_pair do |key, func|
+      config.add_default_key_binding_by_keymap(:emacs, key, func)
+      config.add_default_key_binding_by_keymap(:vi_insert, key, func)
+    end
   end
 
   if defined? JRUBY_VERSION
@@ -106,6 +114,7 @@ class Reline::Windows https://github.com/ruby/ruby/blob/trunk/lib/reline/windows.rb#L114
   SCROLLLOCK_ON = 0x0040
   SHIFT_PRESSED = 0x0010
 
+  VK_TAB = 0x09
   VK_END = 0x23
   VK_HOME = 0x24
   VK_LEFT = 0x25
@@ -199,6 +208,9 @@ class Reline::Windows https://github.com/ruby/ruby/blob/trunk/lib/reline/windows.rb#L208
     [ { control_keys: [], virtual_key_code: VK_DELETE }, [0, 83] ],
     [ { control_keys: [], virtual_key_code: VK_HOME },   [0, 71] ],
     [ { control_keys: [], virtual_key_code: VK_END },    [0, 79] ],
+
+    # Emulate ANSI key sequence.
+    [ { control_keys: :SHIFT, virtual_key_code: VK_TAB }, [27, 91, 90] ],
   ]
 
   def self.process_key_event(repeat_count, virtual_key_code, virtual_scan_code, char_code, control_key_state)
-- 
cgit v1.1


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

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