Trait inquire::autocompletion::Autocomplete

source ·
pub trait Autocomplete: DynClone {
    // Required methods
    fn get_suggestions(
        &mut self,
        input: &str
    ) -> Result<Vec<String>, CustomUserError>;
    fn get_completion(
        &mut self,
        input: &str,
        highlighted_suggestion: Option<String>
    ) -> Result<Replacement, CustomUserError>;
}
Expand description

Mechanism to implement autocompletion features for text inputs. The Autocomplete trait has two provided methods: get_suggestions and get_completion.

  • get_suggestions is called whenever the user’s text input is modified, e.g. a new letter is typed, returning a Vec<String>. The Vec<String> is the list of suggestions that the prompt displays to the user according to their text input. The user can then navigate through the list and if they submit while highlighting one of these suggestions, the suggestion is treated as the final answer.
  • get_completion is called whenever the user presses the autocompletion hotkey (tab by default), with the current text input and the text of the currently highlighted suggestion, if any, as parameters. This method should return whether any text replacement (an autocompletion) should be made. If the prompt receives a replacement to be made, it substitutes the current text input for the string received from the get_completion call.

For example, in the complex_autocompletion.rs example file, the FilePathCompleter scans the file system based on the current text input, storing a list of paths that match the current text input.

Every time get_suggestions is called, the method returns the list of paths that match the user input. When the user presses the autocompletion hotkey, the FilePathCompleter checks whether there is any path selected from the list, if there is, it decides to replace the current text input for it. The interesting piece of functionality is that if there isn’t a path selected from the list, the FilePathCompleter calculates the longest common prefix amongst all scanned paths and updates the text input to an unambiguous new value. Similar to how terminals work when traversing paths.

Required Methods§

source

fn get_suggestions( &mut self, input: &str ) -> Result<Vec<String>, CustomUserError>

List of input suggestions to be displayed to the user upon typing the text input.

If the user presses the autocompletion hotkey (tab as default) with a suggestion highlighted, the user’s text input will be replaced by the content of the suggestion string.

source

fn get_completion( &mut self, input: &str, highlighted_suggestion: Option<String> ) -> Result<Replacement, CustomUserError>

Standalone autocompletion that can be implemented based solely on the user’s input.

If the user presses the autocompletion hotkey (tab as default) and there are no suggestions highlighted (1), this function will be called in an attempt to autocomplete the user’s input.

If the returned value is of the Some variant, the text input will be replaced by the content of the string.

(1) This applies where either there are no suggestions at all, or there are some displayed but the user hasn’t highlighted any.

Trait Implementations§

source§

impl Clone for Box<dyn Autocomplete>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Implementors§