at main 2.3 kB view raw
1use nu_protocol::Span; 2use serde::Serialize; 3 4#[derive(Debug, Serialize)] 5pub struct Suggestion { 6 pub name: String, 7 pub description: Option<String>, 8 pub rendered: String, 9 pub span_start: usize, // char index (not byte) 10 pub span_end: usize, // char index (not byte) 11} 12 13impl PartialEq for Suggestion { 14 fn eq(&self, other: &Self) -> bool { 15 self.name == other.name 16 } 17} 18impl Eq for Suggestion {} 19impl PartialOrd for Suggestion { 20 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { 21 self.name.partial_cmp(&other.name) 22 } 23} 24impl Ord for Suggestion { 25 fn cmp(&self, other: &Self) -> std::cmp::Ordering { 26 self.name.cmp(&other.name) 27 } 28} 29 30#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] 31pub enum CompletionKind { 32 Command { 33 parent_command: Option<String>, // If Some, only show subcommands of this command 34 }, 35 Argument, 36 Flag { 37 command_name: String, 38 }, 39 CommandArgument { 40 command_name: String, 41 arg_index: usize, 42 }, 43 Variable, // prefix is without the $ prefix 44 CellPath { 45 var_id: nu_protocol::VarId, // variable ID for evaluation 46 path_so_far: Vec<String>, // path members accessed before current one 47 }, 48} 49 50#[derive(Debug)] 51pub struct CompletionContext { 52 pub kind: CompletionKind, 53 pub prefix: String, // the partial text being completed 54 pub span: Span, 55} 56 57impl PartialEq for CompletionContext { 58 fn eq(&self, other: &Self) -> bool { 59 self.kind == other.kind && self.prefix == other.prefix 60 } 61} 62 63impl Eq for CompletionContext {} 64 65impl PartialOrd for CompletionContext { 66 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { 67 match self.kind.partial_cmp(&other.kind) { 68 Some(std::cmp::Ordering::Equal) => self.prefix.partial_cmp(&other.prefix), 69 other => other, 70 } 71 } 72} 73 74impl Ord for CompletionContext { 75 fn cmp(&self, other: &Self) -> std::cmp::Ordering { 76 match self.kind.cmp(&other.kind) { 77 std::cmp::Ordering::Equal => self.prefix.cmp(&other.prefix), 78 other => other, 79 } 80 } 81} 82 83impl std::hash::Hash for CompletionContext { 84 fn hash<H: std::hash::Hasher>(&self, state: &mut H) { 85 self.kind.hash(state); 86 self.prefix.hash(state); 87 } 88}