Skip to content

Appended completion is erased from line_buffer when pressing any key #1075

@ymcx

Description

@ymcx

Platform Linux, affects all platforms.
Terminal software kgx

When performing a command and going back in history to the same command and trying to select an item from the autocompletion menu, the selected/already appended item is removed when any key is pressed after the selection.

Steps to reproduce (with Nushell)

  1. ls / [enter] # Perform some command
  2. [up] # Scroll back to the same command
  3. [tab] # Open the autocompletion menu on 'ls /'
  4. [enter] # Select the first completion suggestion, doesn't matter
  5. [any key, g] # Press any key and notice how the selected suggestion disappears.
  6. ls / [tab] # Try to autocomplete 'ls /' by manually typing it rather than selecting it from the history
  7. [enter]
  8. [any key, g] # Notice how the pressed key is appended to the end of the selected completion item.

Steps to reproduce (without Nushell using cargo run --example completions)

  1. l [enter]
  2. [up]
  3. [tab]
  4. [enter]
  5. [any key, g] # The input should now correspond to "lg" instead of "loging"
  6. l [tab]
  7. [enter]
  8. [any key, g] # The input should now be "loging"

This bug has persisted for years and it has honestly driven me nuts, especially since I wasn't able to find any way to trigger the bug continuously before I realized that the bug was only able to be triggered if the current command to be autocompleted was selected from the history rather than being typed out manually.

It seems like the error happens in src/engine.rs in the run_edit_commands function, where self.history_cursor.string_at_cursor() returns the copy of line_buffer that hasn't been updated with the new value after having selected the autocompletion entry. Because of this, the line_buffer gets replaced with the older value which didn't contain the selected entry.

I was able to fix this issue in specific and pass all unit test by creating a new line right after the aforementioned string initialization and writing this line:

let string = self.editor.line_buffer().get_buffer().to_string();

i.e. overwriting the string with the updated line_buffer value. I'm unsure if this breaks anything else, especially considering there has been many PRs (#97, #584, #704) reverting and restoring this functionality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions