-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcompletion.rs
More file actions
190 lines (172 loc) · 5.61 KB
/
completion.rs
File metadata and controls
190 lines (172 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use rustyline::{
Context,
completion::{Completer, Pair},
error::ReadlineError,
};
use crate::editor::DuvaHinter;
// This function gathers all available commands for completion
pub(crate) static COMMANDS: &[&str] = &[
"get",
"mget",
"set",
"append",
"cluster",
"ping",
"keys",
"info",
"exists",
"del",
"incr",
"incrby",
"decr",
"decrby",
"ttl",
// subcommands
"cluster info",
"cluster nodes",
"cluster forget",
"cluster meet",
"cluster reshard",
"info replication",
"replicaof",
"lpush",
"lpushx",
"rpush",
"rpushx",
"llen",
"lset",
"lpop",
"rpop",
"lrange",
"ltrim",
"lindex",
];
macro_rules! new_pair {
($display:expr) => {
Pair { display: $display.to_string(), replacement: $display.to_string() }
};
($display:expr, $replacement: expr) => {
Pair { display: $display.to_string(), replacement: $replacement.to_string() }
};
}
impl Completer for DuvaHinter {
type Candidate = Pair;
fn complete(
&self,
line: &str,
pos: usize,
_ctx: &Context<'_>,
) -> Result<(usize, Vec<Self::Candidate>), ReadlineError> {
// Calculate the start of the current word
let start = if line[..pos].ends_with(' ') {
pos
} else {
line[..pos].rfind(' ').map_or(0, |i| i + 1)
};
// Get the text before the start of the current word
let before_start = &line[..start];
// Split into previous words
let previous_words: Vec<&str> = before_start.split_whitespace().collect();
// Get the current prefix being typed
let current_prefix = &line[start..pos];
let mut candidates = Vec::new();
if previous_words.is_empty() {
// Suggest top-level commands that start with current_prefix
for cmd in self.commands {
if cmd.starts_with(current_prefix) {
candidates
.push(Pair { display: cmd.to_string(), replacement: cmd.to_string() });
}
}
return Ok((start, candidates));
}
macro_rules! suggest_by_pos {
(
[$($args:expr),+]) => {
let suggestions = [$($args),+];
if previous_words.len() > 0 && previous_words.len() <= suggestions.len() {
candidates.push(new_pair!(suggestions[previous_words.len() - 1]));
}
};
([$($args:expr),+], repeat_last) => {
let suggestions = [$($args),+];
if previous_words.len() > 0 {
let idx = if previous_words.len() <= suggestions.len() {
previous_words.len() - 1
} else {
suggestions.len() - 1 // Keep repeating the last argument
};
candidates.push(new_pair!(suggestions[idx]));
}
};
};
let command = previous_words[0].to_lowercase();
match command.as_str() {
| "cluster" => {
if previous_words.len() == 1 {
// Suggest subcommands for cluster that start with current_prefix
let subcommands = ["info", "nodes", "forget", "meet", "reshard"];
candidates.extend(
subcommands
.iter()
.filter(|s| s.starts_with(current_prefix))
.map(|s| new_pair!(s)),
);
} else if previous_words.len() == 2 {
let subcommand = previous_words[1].to_lowercase();
if subcommand == "forget" || subcommand == "meet" {
// Suggest "node" for cluster forget
candidates.push(new_pair!("node"));
}
}
},
| "info" => {
if previous_words.len() == 1 {
// Suggest subcommands for info that start with current_prefix
let subcommands = ["replication", "section"];
candidates.extend(
subcommands
.iter()
.filter(|s| s.starts_with(current_prefix))
.map(|s| new_pair!(s)),
);
}
},
| "set" => {
suggest_by_pos!(["key", "value", "px expr"]);
},
| "lset" => {
suggest_by_pos!(["key", "index", "value"]);
},
| "incrby" => {
suggest_by_pos!(["key", "increment"]);
},
| "decrby" => {
suggest_by_pos!(["key", "decrement"]);
},
| "exists" | "del" | "mget" => {
suggest_by_pos!(["key"], repeat_last);
},
| "lpush" | "lpushx" | "rpush" | "rpushx" => {
suggest_by_pos!(["key", "value"], repeat_last);
},
| "lpop" | "rpop" => {
suggest_by_pos!(["key", "count"]);
},
| "get" | "incr" | "decr" | "ttl" | "llen" => {
suggest_by_pos!(["key"]);
},
| "keys" => {
suggest_by_pos!(["pattern"]);
},
| "replicaof" => {
suggest_by_pos!(["host port"]);
},
| "lrange" | "ltrim" => {
suggest_by_pos!(["key", "start", "end"]);
},
| _ => {},
}
Ok((start, candidates))
}
}