-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_include.ms
60 lines (60 loc) · 1.92 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
/*
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)) {
return(array());
}
@completions = @argumentCompletions[array_size(@args) - 1];
if(is_array(@completions)) {
@arg = to_lower(@args[-1]);
while(is_associative(@completions)) {
@conditionalCompletions = array();
foreach(@condition: @array in @completions) {
if(!@condition) {
@conditionalCompletions = @array;
} else if(@condition[0] == '<') {
@count = 1;
while(@condition[@count] == '<') {
@count++;
}
@previous = @args[array_size(@args) - 1 - @count];
foreach(@s in split('|', substr(@condition, @count))) {
if(@previous == @s) {
@conditionalCompletions = @array;
break(2);
}
}
} else if(has_permission(@condition)) {
@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)) {
return(execute(@completions));
}
return(array());
});
}