pub struct DefaultCompleter { /* private fields */ }
Expand description

A default completer that can detect keywords

Example

use reedline::{DefaultCompleter, Reedline};

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.clone(), 2));

let mut line_editor = Reedline::create().with_completer(completer);

Implementations§

source§

impl DefaultCompleter

source

pub fn new(external_commands: Vec<String>) -> Self

Construct the default completer with a list of commands/keywords to highlight

source

pub fn new_with_wordlen( external_commands: Vec<String>, min_word_len: usize ) -> Self

Construct the default completer with a list of commands/keywords to highlight, given a minimum word length

source

pub fn insert(&mut self, words: Vec<String>)

Insert external_commands list in the object root

Arguments
  • line A vector of String containing the external commands
Example
use reedline::{DefaultCompleter,Completer};

let mut completions = DefaultCompleter::default();

// Insert multiple words
completions.insert(vec!["a","line","with","many","words"].iter().map(|s| s.to_string()).collect());

// The above line is equal to the following:
completions.insert(vec!["a","line","with"].iter().map(|s| s.to_string()).collect());
completions.insert(vec!["many","words"].iter().map(|s| s.to_string()).collect());
source

pub fn with_inclusions(incl: &[char]) -> Self

Create a new DefaultCompleter with provided non alphabet characters whitelisted. The default DefaultCompleter will only parse alphabet characters (a-z, A-Z). Use this to introduce additional accepted special characters.

Arguments
  • incl An array slice with allowed characters
Example
use reedline::{DefaultCompleter,Completer,Span,Suggestion};

let mut completions = DefaultCompleter::default();
completions.insert(vec!["test-hyphen","test_underscore"].iter().map(|s| s.to_string()).collect());
assert_eq!(
    completions.complete("te",2),
    vec![Suggestion {value: "test".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false}]);

let mut completions = DefaultCompleter::with_inclusions(&['-', '_']);
completions.insert(vec!["test-hyphen","test_underscore"].iter().map(|s| s.to_string()).collect());
assert_eq!(
    completions.complete("te",2),
    vec![
        Suggestion {value: "test-hyphen".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
        Suggestion {value: "test_underscore".into(), description: None, extra: None, span: Span { start: 0, end: 2 }, append_whitespace: false},
    ]);
source

pub fn clear(&mut self)

Clears all the data from the tree

Example
use reedline::{DefaultCompleter,Completer};

let mut completions = DefaultCompleter::default();
completions.insert(vec!["batman","robin","batmobile","batcave","robber"].iter().map(|s| s.to_string()).collect());
assert_eq!(completions.word_count(), 5);
assert_eq!(completions.size(), 24);
completions.clear();
assert_eq!(completions.size(), 1);
assert_eq!(completions.word_count(), 0);
source

pub fn word_count(&self) -> u32

Returns a count of how many words that exist in the tree

Example
use reedline::{DefaultCompleter,Completer};

let mut completions = DefaultCompleter::default();
completions.insert(vec!["batman","robin","batmobile","batcave","robber"].iter().map(|s| s.to_string()).collect());
assert_eq!(completions.word_count(), 5);
source

pub fn size(&self) -> u32

Returns the size of the tree, the amount of nodes, not words

Example
use reedline::{DefaultCompleter,Completer};

let mut completions = DefaultCompleter::default();
completions.insert(vec!["batman","robin","batmobile","batcave","robber"].iter().map(|s| s.to_string()).collect());
assert_eq!(completions.size(), 24);
source

pub fn min_word_len(&self) -> usize

Returns the minimum word length to complete. This allows you to pass full sentences to insert() and not worry about pruning out small words like “a” or “to”, because they will be ignored.

Example
use reedline::{DefaultCompleter,Completer};

let mut completions = DefaultCompleter::default().set_min_word_len(4);
completions.insert(vec!["one","two","three","four","five"].iter().map(|s| s.to_string()).collect());
assert_eq!(completions.word_count(), 3);

let mut completions = DefaultCompleter::default().set_min_word_len(1);
completions.insert(vec!["one","two","three","four","five"].iter().map(|s| s.to_string()).collect());
assert_eq!(completions.word_count(), 5);
source

pub fn set_min_word_len(self, len: usize) -> Self

Sets the minimum word length to complete on. Smaller words are ignored. This only affects future calls to insert() - changing this won’t start completing on smaller words that were added in the past, nor will it exclude larger words already inserted into the completion tree.

Trait Implementations§

source§

impl Clone for DefaultCompleter

source§

fn clone(&self) -> DefaultCompleter

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
source§

impl Completer for DefaultCompleter

source§

fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion>

Returns a vector of completions and the position in which they must be replaced; based on the provided input.

Arguments
  • line The line to complete
  • pos The cursor position
Example
use reedline::{DefaultCompleter,Completer,Span,Suggestion};

let mut completions = DefaultCompleter::default();
completions.insert(vec!["batman","robin","batmobile","batcave","robber"].iter().map(|s| s.to_string()).collect());
assert_eq!(
    completions.complete("bat",3),
    vec![
        Suggestion {value: "batcave".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
        Suggestion {value: "batman".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
        Suggestion {value: "batmobile".into(), description: None, extra: None, span: Span { start: 0, end: 3 }, append_whitespace: false},
    ]);

assert_eq!(
    completions.complete("to the bat",10),
    vec![
        Suggestion {value: "batcave".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
        Suggestion {value: "batman".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
        Suggestion {value: "batmobile".into(), description: None, extra: None, span: Span { start: 7, end: 10 }, append_whitespace: false},
    ]);
source§

fn partial_complete( &mut self, line: &str, pos: usize, start: usize, offset: usize ) -> Vec<Suggestion>

action that will return a partial section of available completions this command comes handy when trying to avoid to pull all the data at once from the completer
source§

fn total_completions(&mut self, line: &str, pos: usize) -> usize

number of available completions
source§

impl Debug for DefaultCompleter

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for DefaultCompleter

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.