Skip to main content

nu_cli/completions/
base.rs

1use crate::completions::CompletionOptions;
2use nu_protocol::{
3    DynamicSuggestion, Span, SuggestionKind,
4    engine::{Stack, StateWorkingSet},
5};
6use reedline::Suggestion;
7
8pub trait Completer {
9    /// Fetch, filter, and sort completions
10    #[allow(clippy::too_many_arguments)]
11    fn fetch(
12        &mut self,
13        working_set: &StateWorkingSet,
14        stack: &Stack,
15        prefix: impl AsRef<str>,
16        span: Span,
17        offset: usize,
18        options: &CompletionOptions,
19    ) -> Vec<SemanticSuggestion>;
20}
21
22#[derive(Debug, Default, PartialEq)]
23pub struct SemanticSuggestion {
24    pub suggestion: Suggestion,
25    pub kind: Option<SuggestionKind>,
26}
27
28impl SemanticSuggestion {
29    pub fn from_dynamic_suggestion(
30        suggestion: DynamicSuggestion,
31        span: reedline::Span,
32        style: Option<nu_ansi_term::Style>,
33    ) -> Self {
34        SemanticSuggestion {
35            suggestion: Suggestion {
36                value: suggestion.value,
37                display_override: suggestion.display_override,
38                description: suggestion.description,
39                extra: suggestion.extra,
40                append_whitespace: suggestion.append_whitespace,
41                match_indices: suggestion.match_indices,
42                style,
43                span,
44            },
45            kind: suggestion.kind,
46        }
47    }
48}
49
50impl From<Suggestion> for SemanticSuggestion {
51    fn from(suggestion: Suggestion) -> Self {
52        Self {
53            suggestion,
54            ..Default::default()
55        }
56    }
57}