-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDart.hx
81 lines (70 loc) · 2.55 KB
/
Dart.hx
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
/*
* SPDX-FileCopyrightText: © Sebastian Thomschke and contributors
* SPDX-FileContributor: Sebastian Thomschke
* SPDX-License-Identifier: MIT
* SPDX-ArtifactOfProjectHomePage: https://github.com/sebthom/more-clink-completions
*/
package more_clink_completions.completions;
import clink.api.LineState;
import clink.util.LuaArray;
import clink.api.ArgMatcher;
import clink.api.Clink;
import more_clink_completions.util.Utils;
using clink.util.Strings;
class Dart {
public static function register() {
Clink.argMatcher("dart").setDelayedInitializer(registerNow);
}
static function registerNow(parser:ArgMatcher, commandWord:String) {
parser.addFlags([
"-h", "--help",
"-v", "--verbose",
"--version",
"--enable-analytics",
"--disable-analytics"
])
.addArg(suggestMainCommands)
.addArg(suggestSubCommands)
.noFiles();
}
static final COMMANDS_CACHE = new Map<String, LuaArray<String>>();
static function extractCommandsFromHelp(helpCommand:String, commandSectionMarker:String):LuaArray<String> {
final commands = new LuaArray<String>();
var isCommandsSection = false;
for (line in Utils.exec(helpCommand)) {
if (isCommandsSection) {
if (line == "")
isCommandsSection = false;
else {
final command = line.findMatch("^%s+([a-zA-Z-_]+)");
if (command != null)
commands.add(command);
}
} else if (line.hasMatch(commandSectionMarker))
isCommandsSection = true;
}
return commands;
}
static function suggestMainCommands():LuaArray<String> {
var mainCommands = COMMANDS_CACHE[""];
if (mainCommands == null) {
mainCommands = extractCommandsFromHelp("dart --help", "Available commands:");
if (mainCommands.length() > 0) {
COMMANDS_CACHE[""] = mainCommands;
}
}
return mainCommands;
}
static function suggestSubCommands(word:String, wordIndex:Int, lineState:LineState):LuaArray<String> {
suggestMainCommands(); // trigger cache population
var mainCommand = lineState.getWord(wordIndex - 1);
if (@:nullSafety(Off) !COMMANDS_CACHE[""].contains(mainCommand)) {
return [];
}
var subCommands = COMMANDS_CACHE[mainCommand];
if (subCommands == null) {
subCommands = COMMANDS_CACHE[mainCommand] = extractCommandsFromHelp('dart ${mainCommand} --help', "Available subcommands:");
}
return subCommands;
}
}