Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Reline core to stay in sync with the line_editor's completion_append_character #756

Open
sjanusz-r7 opened this issue Oct 4, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@sjanusz-r7
Copy link

Description

When updating Reline.completion_append_character in Reline.completion_proc, the change does not get reflected until you enter a new Reline.readline or Reline.readmultiline context. It seems like the value of Reline.completion_append_character and Reline.line_editor.completion_append_character is out of sync.

Scenario: We are updating the value of Reline.completion_append_character = ' ' inside of the completion_proc, but it's not reflecting in the line_editor

Expected Behaviour

Being able to change Reline.completion_append_character in the completion proc and have it update the line_editor.

i.e. this code:

reline/lib/reline.rb

Lines 83 to 93 in 7534f7f

def completion_append_character=(val)
if val.nil?
@completion_append_character = nil
elsif val.size == 1
@completion_append_character = val.encode(encoding)
elsif val.size > 1
@completion_append_character = val[0].encode(encoding)
else
@completion_append_character = nil
end
end

To immediately update the line_editor's completion_append_character value

    def completion_append_character=(val)
      if val.nil?
        @completion_append_character = nil
      elsif val.size == 1
        @completion_append_character = val.encode(encoding)
      elsif val.size > 1
        @completion_append_character = val[0].encode(encoding)
      else
        @completion_append_character = nil
      end
      # Ensure the line_editor is updated too:
      line_editor.completion_append_character = @completion_append_character
    end

Current Behaviour

Changing Reline.completion_append_character in Reline.completion_append_character does not take effect until a new call to Reline.readline.

Terminal Emulator

MacOS

Reline Version

0.5.10

@tompng
Copy link
Member

tompng commented Oct 4, 2024

Some configurations are hard to make stay in sync.
For example, this code updates completion_proc itself in completion_proc. It also updates basic_word_break_characters that changes completion target.

def new_proc(a)
  ->k{
    Readline.completion_proc = new_proc(a.succ)
    Readline.basic_word_break_characters = [';', '.'].sample
    [a]
  }
end
Readline.completion_proc = new_proc('a')

This configurations looks stayed in sync in GNU Readline but not in Reline.
However, making completion_proc and basic_word_break_characters stay in sync will significantly impairs code maintainability.

Only changing completion_append_character to be synchronized is not so hard, but I think it is not a good idea to guarantee it to be stay in sync.

In my opinion, it feel like changing completion_append_character inside completion_proc is a hack that only works in the current version of GNU Readline.

@tompng tompng closed this as completed Oct 4, 2024
@sjanusz-r7
Copy link
Author

In my opinion, it feel like changing completion_append_character inside completion_proc is a hack that only works in the current version of GNU Readline.

What would be a more idiomatic Reline solution? For our use case - the scenario is: Some of our completions require a different value for completion_append_character

It looks like currently GNU Readline and RbReadline support this functionality, but it's not supported by Reline unfortunately without a patch to sync the Readline and line_editor values

As a feature request - if there was a different API to tell Reline about individual append characters as part of the completion_proc result that would be great

@tompng
Copy link
Member

tompng commented Oct 14, 2024

I think this is a bug that should be fixed an incompatibility to readline only because GNU Readline documents that rl_completion_append_character can be changed from completion function.

From GNU Readline's documentation https://tiswww.case.edu/php/chet/readline/readline.html#index-rl_005fcompletion_005fappend_005fcharacter

When a single completion alternative matches at the end of the command line, this character is appended to the inserted completion text. The default is a space character (‘ ’). Setting this to the null character (‘\0’) prevents anything being appended automatically. This can be changed in application-specific completion functions to provide the “most sensible word separator character” according to an application-specific command line syntax specification. It is set to the default before any application-specific completion function is called, and may only be changed within such a function.

rl_completion_append_character can only be set from completion function and always reset to default value before completion function is called.
Note that the documented behavior is reported as a bug in Ruby's readline binding and carefully fixed. https://bugs.ruby-lang.org/issues/4635.

@tompng tompng reopened this Oct 14, 2024
@tompng tompng added the bug Something isn't working label Oct 14, 2024
@tompng tompng added enhancement New feature or request and removed bug Something isn't working labels Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants