Skip to content

Commit 6adfe82

Browse files
committed
Enable to set multiple actions for completion
1 parent e8fd3fc commit 6adfe82

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/core/builtins/complete.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ fn print_each_complete(name: &str, info: &CompletionEntry) -> i32 {
5959
}
6060
} else if !info.function.is_empty() {
6161
print!("complete {}-F {} ", &o_options, &info.function);
62-
} else if !info.action.is_empty() {
63-
let symbol = action_to_reduce_symbol(&info.action);
62+
} else if !info.actions.is_empty() {
63+
let symbol = action_to_reduce_symbol(&info.actions[0]);
6464

6565
if symbol.is_empty() {
66-
print!("complete {}-A {} ", &o_options, &info.action);
66+
print!("complete {}-A {} ", &o_options, &info.actions[0]);
6767
} else {
6868
print!("complete {}-{} ", &o_options, &symbol);
6969
}
@@ -195,17 +195,27 @@ pub fn complete(core: &mut ShellCore, args: &[String]) -> i32 {
195195
}
196196
}
197197

198-
let action = opt_to_action(&args[1]);
199-
if !action.is_empty() {
200-
for command in &args[2..] {
198+
let mut actions = vec![];
199+
for i in 1..args.len() {
200+
let action = opt_to_action(&args[i]);
201+
if action.is_empty() {
202+
break;
203+
}
204+
actions.push(action);
205+
}
206+
207+
//let action = opt_to_action(&args[1]);
208+
if !actions.is_empty() {
209+
let start = actions.len() + 1;
210+
for command in &args[start..] {
201211
if !core.completion.entries.contains_key(command) {
202212
core.completion
203213
.entries
204214
.insert(command.clone(), CompletionEntry::default());
205215
}
206216

207217
let info = &mut core.completion.entries.get_mut(command).unwrap();
208-
info.action = action.clone();
218+
info.actions = actions.clone();
209219
info.options = options.clone();
210220
info.o_options = o_options.clone();
211221
}
@@ -221,7 +231,7 @@ pub fn complete(core: &mut ShellCore, args: &[String]) -> i32 {
221231
}
222232

223233
let info = &mut core.completion.entries.get_mut(command).unwrap();
224-
info.action = args[2].clone();
234+
info.actions.push(args[2].clone());
225235
info.options = options.clone();
226236
}
227237

src/core/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Completion {
1414
pub struct CompletionEntry {
1515
pub function: String,
1616
pub o_options: Vec<String>,
17-
pub action: String,
17+
pub actions: Vec<String>,
1818
pub options: HashMap<String, String>,
1919
pub large_w_cands: String,
2020
}

src/feeder/terminal/completion.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl Terminal {
135135

136136
let command = format!(
137137
"COMPREPLY=($(compgen -A \"{}\" \"{}\"))",
138-
&info.action, &target_word
139-
);
138+
&info.actions[0], &target_word
139+
); //TODO: use actions other than the first one
140140
let mut feeder = Feeder::new(&command);
141141

142142
if let Ok(Some(mut a)) = SimpleCommand::parse(&mut feeder, core) {
@@ -168,7 +168,7 @@ impl Terminal {
168168
core.completion.current = info.clone();
169169
if !info.function.is_empty() {
170170
Self::exec_complete_function(&org_word, prev_pos, cur_pos, core)?;
171-
} else if !info.action.is_empty() {
171+
} else if !info.actions.is_empty() {
172172
Self::exec_action(cur_pos, core)?;
173173
}
174174

@@ -221,11 +221,11 @@ impl Terminal {
221221
pos: &str,
222222
) -> Vec<String> {
223223
if core.completion.entries.contains_key(com) {
224-
let action = core.completion.entries[com].action.clone();
224+
let action = core.completion.entries[com].actions.clone();
225225
let options = core.completion.entries[com].options.clone();
226226

227227
if !action.is_empty() {
228-
let mut cands = match action.as_ref() {
228+
let mut cands = match action[0].as_ref() {
229229
"alias" => compgen::compgen_a(core, args),
230230
"command" => compgen::compgen_c(core, args),
231231
"job" => compgen::compgen_j(core, args),

0 commit comments

Comments
 (0)