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    /// Optional description for the replacement
18    pub description: Option<String>,
19    /// Optional vector of strings in the suggestion. These can be used to
20    /// represent examples coming from a suggestion
21    pub extra: Option<Vec<String>>,
22    /// Whether to append a space after selecting this suggestion.
23    /// This helps to avoid that a completer repeats the complete suggestion.
24    pub append_whitespace: bool,
25    /// Indices of the graphemes in the suggestion that matched the typed text.
26    /// Useful if using fuzzy matching.
27    pub match_indices: Option<Vec<usize>>,
28    /// Replacement span in the buffer, if any.
29    pub span: Option<Span>,
30    pub kind: Option<SuggestionKind>,
31}
32
33impl Default for DynamicSuggestion {
34    fn default() -> Self {
35        Self {
36            append_whitespace: true,
37            value: String::new(),
38            description: None,
39            extra: None,
40            match_indices: None,
41            kind: None,
42            span: None,
43        }
44    }
45}
46
47#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
48pub enum SuggestionKind {
49    Command(CommandType, Option<DeclId>),
50    Value(Type),
51    CellPath,
52    Directory,
53    File,
54    Flag,
55    Module,
56    Operator,
57    Variable,
58}
59
60/// A simple wrapper for [`ast::Call`] which contains additional context about completion.
61/// It's used only at nushell side, to avoid unnecessary clone.
62#[derive(Clone, Debug, PartialEq)]
63pub struct DynamicCompletionCallRef<'a> {
64    /// the real call, which is generated during parse time.
65    pub call: &'a ast::Call,
66    /// Indicates if there is a placeholder in input buffer.
67    pub strip: bool,
68    /// The position in input buffer, which is useful to find placeholder from arguments.
69    pub pos: usize,
70}