Skip to main content

nu_protocol/
completion.rs

1use crate::{DeclId, Span, Type, ast, engine::CommandType};
2use serde::{Deserialize, Serialize};
3
4/// A simple semantics suggestion just like nu_cli::SemanticSuggestion, but it
5/// derives `Serialize` and `Deserialize`, so plugins are allowed to use it
6/// to provide dynamic completion items.
7///
8/// Why define a new one rather than put `nu_cli::SemanticSuggestion` here?
9///
10/// If bringing `nu_cli::SemanticSuggestion` here, it brings reedline::Suggestion too,
11/// then it requires this crates depends on `reedline`, this is not good because
12/// protocol should not rely on a cli relative interface.
13#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
14pub struct DynamicSuggestion {
15    /// String replacement that will be introduced to the the buffer
16    pub value: String,
17    /// If given, overrides `value` as text displayed to user
18    pub display_override: Option<String>,
19    /// Optional description for the replacement
20    pub description: Option<String>,
21    /// Optional vector of strings in the suggestion. These can be used to
22    /// represent examples coming from a suggestion
23    pub extra: Option<Vec<String>>,
24    /// Whether to append a space after selecting this suggestion.
25    /// This helps to avoid that a completer repeats the complete suggestion.
26    pub append_whitespace: bool,
27    /// Indices of the graphemes in the suggestion that matched the typed text.
28    /// Useful if using fuzzy matching.
29    pub match_indices: Option<Vec<usize>>,
30    /// Replacement span in the buffer, if any.
31    pub span: Option<Span>,
32    pub kind: Option<SuggestionKind>,
33}
34
35impl Default for DynamicSuggestion {
36    fn default() -> Self {
37        Self {
38            append_whitespace: true,
39            value: String::new(),
40            display_override: None,
41            description: None,
42            extra: None,
43            match_indices: None,
44            kind: None,
45            span: None,
46        }
47    }
48}
49
50#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
51pub enum SuggestionKind {
52    Command(CommandType, Option<DeclId>),
53    Value(Type),
54    CellPath,
55    Directory,
56    File,
57    Flag,
58    Module,
59    Operator,
60    Variable,
61}
62
63/// A simple wrapper for [`ast::Call`] which contains additional context about completion.
64/// It's used only at nushell side, to avoid unnecessary clone.
65#[derive(Clone, Debug, PartialEq)]
66pub struct DynamicCompletionCallRef<'a> {
67    /// the real call, which is generated during parse time.
68    pub call: &'a ast::Call,
69    /// Indicates if there is a placeholder in input buffer.
70    pub strip: bool,
71    /// The position in input buffer, which is useful to find placeholder from arguments.
72    pub pos: usize,
73}