-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_include.ms
72 lines (72 loc) · 2.54 KB
/
auto_include.ms
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
/*
Returns a tabcompleter closure that supports an ordered list of static or dynamic returns.
Example:
set_tabcompleter('player', _create_tabcompleter(
array(
'player.management': array('set', 'get') // requires permission to see
null: array('get')), // null key is used when all other conditions fail
null, // no completion
array('health', 'hunger', 'mode'), // simple list of options
array(
'<health|hunger': array('min', 'max', 'none') // requires previous to be health or hunger
'<mode': array('creative', 'survival', 'adventure', 'spectator')),
closure(@alias, @sender, @args){ ... }, // custom tabcompleter for this argument
));
*/
proc _create_tabcompleter() {
@argumentCompletions = @arguments;
return(closure(@alias, @sender, @args) {
if(array_size(@args) > array_size(@argumentCompletions)) {
// we have no completions for this arg
return(array());
}
@completions = @argumentCompletions[array_size(@args) - 1];
if(is_array(@completions)) {
// normal arg completion
@arg = @args[-1];
while(is_associative(@completions)) {
// handles nested conditional completions
@conditionalCompletions = array();
foreach(@condition: @array in @completions) {
// This is associative, so order cannot be used.
// Previous argument and permission conditionals should not be mixed.
// Use nested associative arrays instead when mixing conditionals.
if(!@condition) {
// null is default completions if nothing else matches
@conditionalCompletions = @array;
// continue loop
} else if(@condition[0] === '<') {
// previous argument conditional
@argOffset = 1;
while(@condition[@argOffset] === '<') {
@argOffset++;
}
@previousArg = @args[array_size(@args) - 1 - @argOffset];
foreach(@conditionalArg in split('|', substr(@condition, @argOffset))) {
if(@previousArg === @conditionalArg) {
@conditionalCompletions = @array;
break(2);
}
}
} else if(has_permission(@condition)) {
// permission conditional
@conditionalCompletions = @array;
break();
}
}
@completions = @conditionalCompletions;
}
if(!length(@arg)) {
return(@completions);
}
return(array_filter(@completions, closure(@key, @value) {
return(string_starts_with(@value, @arg));
}));
} else if(is_closure(@completions)) {
// custom arg completion
return(execute(@alias, @sender, @args, @completions));
}
// no completions for this arg (should use null for this)
return(array());
});
}