Quantcast
Channel: Active questions tagged code-completion - Stack Overflow
Viewing all articles
Browse latest Browse all 96

How do I use rustyline::Editor::set_helper to react to tab-completion requests?

$
0
0

I'm going through Build your own shell, and I've got to the task of implementing command completion. A note suggests:

We recommend using a library like readline for your implementation. Most modern shells and REPLs (like the Python REPL) use readline under the hood. While you may need to override some of its default behaviors, it's typically less work than starting from scratch.

So I've searched for that library and found this page with the following example:

use rustyline::error::ReadlineError;use rustyline::{DefaultEditor, Result};fn main() -> Result<()> {    // `()` can be used when no completer is required    let mut rl = DefaultEditor::new()?;    #[cfg(feature = "with-file-history")]    if rl.load_history("history.txt").is_err() {        println!("No previous history.");    }    loop {        let readline = rl.readline(">> ");        match readline {            Ok(line) => {                rl.add_history_entry(line.as_str())?;                println!("Line: {}", line);            },            Err(ReadlineError::Interrupted) => {                println!("CTRL-C");                break            },            Err(ReadlineError::Eof) => {                println!("CTRL-D");                break            },            Err(err) => {                println!("Error: {:?}", err);                break            }        }    }    #[cfg(feature = "with-file-history")]    rl.save_history("history.txt");    Ok(())}

and checked that the command-line editing works. Now I need to implement Tab-completion, though.

I've searched a bit more and found that rustyline::Editor has got set_helper/helper_mut/helper; set_helper seem to be what I'm looking for, because the doc reads

Register a callback function to be called for tab-completion or to show hints to the user at the right of the prompt.

but I'm not sure what to do to achieve that. I'd have expected set_helper to accept a function that acceps a mutable reference to the state of the line and returns nothing, or something that would give equivalent "power". But it takes a () instead.

How do I proceed? I've tried instatiating a compelter for filenames:

let c = rustyline::completion::FilenameCompleter::new();

but passing it to rl.set_helper(...) (with or without Some around it) fails to typecheck because the method expects a ().

Indeed, this type-checks, expectedly, but it's obviously not doing anything:

rl.set_helper(Some(()));

Viewing all articles
Browse latest Browse all 96

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>