forked from railsware/upterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutocompletion.ts
75 lines (64 loc) · 2.83 KB
/
Autocompletion.ts
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
import * as _ from "lodash";
import Job from "./Job";
import {
choice, token, executable, decorate, sequence, string, many1,
optionalContinuation, spacesWithoutSuggestion, many, noisySuggestions, InputMethod, Parser,
} from "./Parser";
import {commandDescriptions} from "./plugins/autocompletion_providers/Executable";
import {description, style, styles} from "./plugins/autocompletion_providers/Suggestions";
import {makeAlias} from "./plugins/autocompletion_providers/Alias";
import {file} from "./plugins/autocompletion_providers/File";
import {compose, mapObject} from "./utils/Common";
import {command} from "./plugins/autocompletion_providers/Command";
import Aliases from "./Aliases";
import {redirect} from "./plugins/autocompletion_providers/Redirect";
import {environmentVariable} from "./plugins/autocompletion_providers/EnvironmentVariable";
export const makeGrammar = (aliases: Dictionary<string>) => {
const exec = sequence(
choice(mapObject(commandDescriptions, (key, value) => decorate(string(key), compose(description(value), style(styles.executable))))),
optionalContinuation(many1(choice([file, environmentVariable])))
);
const sudo = sequence(executable("sudo"), command);
const anyCommand = sequence(
choice([
sudo,
command,
exec,
makeAlias(aliases),
]),
redirect
);
const separator = choice([
noisySuggestions(sequence(many(spacesWithoutSuggestion), token(string("&&")))),
noisySuggestions(sequence(many(spacesWithoutSuggestion), token(string(";")))),
]);
return sequence(sequence(anyCommand, separator), anyCommand);
};
export const suggestionsLimit = 9;
export const {getSuggestions} = new class {
private grammar: Parser;
getSuggestions = async (job: Job, inputMethod: InputMethod) => {
if (!this.grammar) {
const aliases = await Aliases.all();
this.grammar = makeGrammar(aliases);
}
if (window.DEBUG) {
/* tslint:disable:no-console */
console.time(`suggestion for '${job.prompt.value}'`);
}
const results = await this.grammar({
input: job.prompt.value,
directory: job.session.directory,
historicalCurrentDirectoriesStack: job.session.historicalCurrentDirectoriesStack,
environment: job.environment,
inputMethod: inputMethod,
});
const suggestions = results.map(result => result.suggestions.map(suggestion => suggestion.withPrefix(result.parse)));
const unique = _.uniqBy(_.flatten(suggestions), suggestion => suggestion.value).slice(0, suggestionsLimit);
if (window.DEBUG) {
/* tslint:disable:no-console */
console.timeEnd(`suggestion for '${job.prompt.value}'`);
}
return unique;
};
};