reedline icon indicating copy to clipboard operation
reedline copied to clipboard

Completions not working

Open mcastorina opened this issue 3 years ago • 8 comments

Platform linux Terminal software rxvt-unicode

The example completions program compiles, runs, and displays a prompt. Typing a few characters then TAB or Ctrl+X does not complete the expected word or display a menu of possible completions. It appears to have the same behavior as without calling with_completions.

Steps to reproduce

[dependencies]
reedline = { git = "https://github.com/nushell/reedline", branch = "main" }

I did this today so I believe it's 8c565e4f1de2c6dd4f67b18876d011756bfbe16d though I don't know how to confirm this in the project.

use reedline::{DefaultCompleter, DefaultPrompt, Reedline, Signal};

fn main() {
    let prompt = DefaultPrompt::default();

    let commands = vec![
        "test".into(),
        "hello world".into(),
        "hello world reedline".into(),
        "this is the reedline crate".into(),
    ];
    let completer = Box::new(DefaultCompleter::new_with_wordlen(commands, 2));
    let mut line_editor = Reedline::create().unwrap().with_completer(completer);

    loop {
        let sig = line_editor.read_line(&prompt).unwrap();
        match sig {
            Signal::Success(buffer) => {
                println!("We processed: {}", buffer);
            }
            Signal::CtrlD | Signal::CtrlC => {
                println!("\nAborted!");
                break;
            }
            Signal::CtrlL => {
                line_editor.clear_screen().unwrap();
            }
        }
    }
}

mcastorina avatar Feb 09 '22 16:02 mcastorina

Hi, the completion menu is still not working for me -

Platform macos-aarch64 Terminal software: kitty

The example completions program compiles, runs, and displays a prompt. Typing a few characters then TAB or Ctrl+X does not complete the expected word or display a menu of possible completions. It appears to have the same behavior as without calling with_completions.

Steps to reproduce -

[dependencies]
reedline = { version = "0.3", features = ["clipboard", "system_clipboard"] }
use anyhow::Result;
use nu_ansi_term::{Color, Style};
use reedline::{
    CompletionMenu, DefaultCompleter, DefaultHinter, DefaultPrompt, ExampleHighlighter,
    FileBackedHistory, Reedline, Signal,
};

fn main() -> Result<()> {
    let prompt = DefaultPrompt::default();
    let history = Box::new(
        FileBackedHistory::with_file(5, "history.txt".into())
            .expect("Error configuring history with file"),
    );

    let commands = vec![
        "test".into(),
        "hello world".into(),
        "hello world reedline".into(),
        "this is the reedline crate".into(),
    ];

    let completer = Box::new(DefaultCompleter::new(commands.clone()));
    // Use the interactive menu to select options from the completer
    let completion_menu = Box::new(CompletionMenu::default());

    let mut line_editor = Reedline::create()?
        .with_completer(completer)
        .with_menu(completion_menu)
        .with_history(history)
        .expect("Error configuring reedline with history")
        .with_highlighter(Box::new(ExampleHighlighter::new(commands)))
        .with_hinter(Box::new(
            DefaultHinter::default().with_style(Style::new().italic().fg(Color::LightGray)),
        ));

    loop {
        let sig = line_editor.read_line(&prompt)?;
        match sig {
            Signal::Success(buffer) => {
                println!("We processed: {}", buffer);
            }
            Signal::CtrlD | Signal::CtrlC => {
                println!("\nAborted!");
                break Ok(());
            }
            Signal::CtrlL => {
                line_editor.clear_screen().unwrap();
            }
        }
    }
}

DhruvDh avatar Apr 06 '22 18:04 DhruvDh

I should have dug around the new commits after release more, I think I got it to work after reading main.rs. Keybindings for the completion menu were missing, and ListMenu just wasn't working for me, but ColumnarMenu was. I am not familiar with the codebase so I am not confident in saying whether I did something wrong or it's a bug with ListMenu.

DhruvDh avatar Apr 06 '22 19:04 DhruvDh

Mhh @elferherrera, did something relating the general API change with your latest menu changes?

sholderbach avatar Apr 06 '22 19:04 sholderbach

Not really.

@DhruvDh when you say the list menu doesn't work, what do you mean?

elferherrera avatar Apr 07 '22 05:04 elferherrera

Sorry for not having a minimal reproduction, but this is from the repo https://github.com/DhruvDh/umm/tree/next at https://github.com/DhruvDh/umm/blob/next/src/main.rs.

This is how I am creating a line editor - https://github.com/DhruvDh/umm/blob/06f046abbaa1dc2500692c70c7d05b846d358b8a/src/main.rs#L61-L103

    let mut line_editor = Reedline::create()
        .with_history(Box::new(
            FileBackedHistory::with_file(5, "history.txt".into())
                .expect("Error configuring history with file"),
        ))
        .with_highlighter(Box::new(ExampleHighlighter::new(commands.clone())))
        .with_hinter(Box::new(
            DefaultHinter::default().with_style(Style::new().italic().fg(Color::LightGray)),
        ))
        .with_completer({
            let mut inclusions = vec!['-', '_'];
            for i in '0'..='9' {
                inclusions.push(i);
            }


            let mut completer = DefaultCompleter::with_inclusions(&inclusions);
            completer.insert(commands.clone());
            Box::new(completer)
        })
        .with_quick_completions(true)
        .with_partial_completions(true)
        .with_ansi_colors(true)
        .with_menu(ReedlineMenu::EngineCompleter(Box::new(
            ListMenu::default().with_name("completion_menu"),
        )))
        .with_edit_mode({
            let mut keybindings = default_emacs_keybindings();
            keybindings.add_binding(
                KeyModifiers::NONE,
                KeyCode::Tab,
                ReedlineEvent::UntilFound(vec![
                    ReedlineEvent::Menu("completion_menu".to_string()),
                    ReedlineEvent::MenuNext,
                ]),
            );


            keybindings.add_binding(
                KeyModifiers::SHIFT,
                KeyCode::BackTab,
                ReedlineEvent::MenuPrevious,
            );
            Box::new(Emacs::new(keybindings))
        });

With ListMenu::default().with_name("completion_menu") at line 84 (video) - https://capture.dropbox.com/2qUrH8tykkUAudM1

With ColumnarMenu::default().with_name("completion_menu") at line 84 (video) - https://capture.dropbox.com/v2R58EmyUEgfMnjx

I will add a minimal reproduction later as soon as I have time, sorry.

DhruvDh avatar Apr 07 '22 07:04 DhruvDh

I see what is happening. The list type of menu doesn't take as input what is already written, but what you write after you activate it. Try to trigger the menu and then type something.

I'm writing a chapter to explain the differences between these menus and how they can be used with different completers

Another option is to create the menu and then use this function with the constructor with_only_buffer_difference

ListMenu::default().with_name("completion_menu").with_only_buffer_difference(false)

That should that what has been written

elferherrera avatar Apr 07 '22 07:04 elferherrera

Ah, thank you. I figured I probably was using it incorrectly. Anyway the library is great! Thanks for all of your guys' work.

DhruvDh avatar Apr 07 '22 08:04 DhruvDh

Hello all, what is the current status of completion functionality? Is it available? Latest crate version or master branch from git (as of today) does not provide CompletionMenu from reedline crate for me, and there is no visible behavior on TAB key pressing when i leave out the "non-existent" CompletionMenu rows from example code and use with_completer only...

RustyJoeM avatar May 01 '22 21:05 RustyJoeM