8000 Completion State Bug not allowing for subsequent tab-completions · Issue #760 · ruby/reline · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Completion State Bug not allowing for subsequent tab-completions #760
Closed
@sjanusz-r7

Description

@sjanusz-r7

Reline Completion State Bug

The Reline Completion State when auto-completing causes an issue where subsequent tab-completions do not work. To fix this and get working tab-completion suggestions, you can move the cursor left and right to reset the completion state to NORMAL.

If the intended usage of this tab completion proc is to return all potential auto-completion options and their values, let us know as if so, it will not be optimal for a use-case where we support passin 874E g multiple options in the single prompt, each of which has an array of potential values. For example:

['opt=127.0.0.1', 'opt=192.168.1.x', 'opt=en0', 'opt=wlan0', 'opt=eth0', ...]

Therefore, returning all permutations of all options and their values when tab-completing gets out of hand rapidly.

Our work-around, whilst not optimal, makes this less noticeable:

Reline.dig_perfect_match_proc = ->(_matched) do  
  Reline.line_editor.instance_variable_set(:@completion_state, Reline::LineEditor::CompletionState::MENU_WITH_PERFECT_MATCH)  
end

Reproducible example:

require 'reline'  
  
tab_complete_lambda = proc do |str_wip, preposing = nil, postposing = nil|  
  if preposing.eql?('--option=')  
    ['value_1', 'value_2']  
  elsif str_wip.start_with?('--')  
    ['--option=']  
  elsif preposing.empty? && str_wip.empty?  
    ['--option=value_1', '--option=value_2']  
  else  
    []  
  end  
end  
  
prompt = 'Prompt > '  
use_history = false  
 
Reline.completion_proc = tab_complete_lambda  
Reline.completion_case_fold = false  

# This is the work-around.
Reline.dig_perfect_match_proc = ->(_matched) do  
  Reline.line_editor.instance_variable_set(:@completion_state, Reline::LineEditor::CompletionState::MENU_WITH_PERFECT_MATCH)  
end  
  
while true do  
  _text = Reline.readline(prompt, use_history)  
end

An example test to show how I would have expected this functionality to work:

diff --git a/test/reline/yamatanooroti/multiline_repl b/test/reline/yamatanooroti/multiline_repl
index 8b82be6..2f3d729 100755
--- a/test/reline/yamatanooroti/multiline_repl
+++ b/test/reline/yamatanooroti/multiline_repl
@@ -193,6 +193,22 @@ opt.on('--autocomplete-super-long') {
   }
 }
 
+opt.on('--autocomplete-option-names') {
+  Reline.autocompletion = false
+  # This is the work-around.
+  Reline.dig_perfect_match_proc = ->(_matched) do
+    Reline.line_editor.instance_variable_set(:@completion_state, Reline::LineEditor::CompletionState::NORMAL)
+  end
+  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
+    if preposing.eql?('--option=')
+      ['value_1', 'value_2']
+    elsif target.start_with?('--')
+      ['--option=']
+    else
+      []
+    end
+  }
+}
+
 opt.on('--autocomplete-width-long') {
   Reline.autocompletion = true
   Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
diff --git a/test/reline/yamatanooroti/test_rendering.rb b/test/reline/yamatanooroti/test_rendering.rb
index 414bca5..70c2b56 100644
--- a/test/reline/yamatanooroti/test_rendering.rb
+++ b/test/reline/yamatanooroti/test_rendering.rb
@@ -1416,6 +1416,22 @@ begin
       close
     end
 
+    def test_autocomplete_with_option_names
+      start_terminal(20, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete-option-names}, startup_message: 'Multiline REPL.')
+      write('--op' + "\C-i")
+      assert_screen(<<~'EOC')
+        Multiline REPL.
+        prompt> --option=
+      EOC
+      write("\C-i" * 3)
+      assert_screen(<<~'EOC')
+        Multiline REPL.
+        prompt> --option=value_
+        value_1  value_2
+      EOC
+      close
+    end
+
     def test_autocomplete_super_long_and_backspace
       start_terminal(20, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete-super-long}, startup_message: 'Multiline REPL.')
       shift_tab = [27, 91, 90]

Current Behavior

I am unable to continue auto-completing after the initial --option= tab-completion.

Steps:

  • Type --
  • Press TAB
  • Verify you have --option= in the line buffer
  • Verify you can't auto-complete anymore even though the tab completion proc returns the correct values
  • Move your cursor left and right using the arrow keys
  • Verify that you can now auto-complete the values at the end of the option

Expected Behavior

I would have expected the ability to continue tab-completing without needing to change my cursor position first.
e.g. I would have expected the values value_1 and value_2 to be suggested. In this case, the prefix value_ would be auto-completed as it matches both suggestions.

Terminal

MacOS

Version

0.5.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0