Skip to main content

shrub_core/shell/
completion.rs

1//! Command completion support for shell instances.
2
3use crate::{completion, error, extensions};
4
5impl<SE: extensions::ShellExtensions> crate::Shell<SE> {
6    /// Completes the word at the cursor in a line being edited, with
7    /// programmable completion (see the [`completion`] module).
8    ///
9    /// # Arguments
10    ///
11    /// * `input` - The line being completed.
12    /// * `cursor` - The cursor's byte offset in the line.
13    ///
14    /// # Errors
15    ///
16    /// Returns an error if `cursor` isn't a char boundary in `input`.
17    pub async fn complete(
18        &mut self,
19        input: &str,
20        cursor: usize,
21    ) -> Result<completion::Completions, error::Error> {
22        completion::complete(self, input, cursor).await
23    }
24
25    /// Returns the options of the programmable completion in progress, if any
26    /// (e.g. while its completion function runs), which the `compopt`
27    /// builtin changes.
28    pub fn in_progress_completion_options_mut(
29        &mut self,
30    ) -> Option<&mut completion::GenerationOptions> {
31        self.completion
32            .in_progress
33            .as_mut()
34            .map(|in_progress| &mut in_progress.options)
35    }
36
37    /// Returns a mutable reference to the programmable completion in progress,
38    /// if any.
39    pub(crate) const fn in_progress_completion_mut(
40        &mut self,
41    ) -> &mut Option<completion::InProgressCompletion> {
42        &mut self.completion.in_progress
43    }
44}