forked from ctf0/macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
172 lines (147 loc) · 4.13 KB
/
extension.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const vscode = require('vscode')
let disposables = []
function activate(context) {
loadMacros(context)
context.subscriptions.push(
vscode.commands.registerCommand('macros.execute', async () => {
vscode.window.showQuickPick(getQPList()).then((selection) => {
if (selection) {
vscode.commands.executeCommand(`macros.${selection}`)
}
})
})
)
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('macros.list')) {
disposeMacros()
loadMacros(context)
}
})
}
/**
* [getSettings description]
*
* @return {[type]} [return description]
*/
function getSettings() {
return vscode.workspace.getConfiguration('macros')
}
/**
* [getMacrosList description]
*
* @return array macro names list
*/
function getMacrosList() {
let ignore = ['has', 'get', 'update', 'inspect']
return Object
.keys(getSettings().get('list'))
.filter((prop) => ignore.indexOf(prop) < 0)
}
/**
* [getQPList description]
*
* @return {[type]} [return description]
*/
function getQPList() {
let list = getMacrosList()
let allow = getSettings().get('qp-allow')
let ignore = getSettings().get('qp-ignore')
if (allow.length) {
list = list.filter((item) => allow.indexOf(item) > 0)
}
if (ignore.length) {
list = list.filter((item) => ignore.indexOf(item) < 0)
}
return list
}
/**
* [executeDelayCommand description]
*
* @param {[type]} action [action description]
*
* @return {[type]} [return description]
*/
function executeDelayCommand(time) {
return new Promise((resolve) => setTimeout(() => resolve(), time))
}
/**
* [executeCommandTimesOther description]
*
* @param {[type]} command [command description]
* @param {[type]} args [args description]
*
* @return {[type]} [return description]
*/
async function executeCommandTimesOther(command, otherCmnd) {
const settings = getSettings().get('list')
let range = settings[otherCmnd].length
for (const index of Array(range)) {
await vscode.commands.executeCommand(command)
}
}
/**
* [executeCommandRepeat description]
*
* @param {[type]} command [command description]
* @param {[type]} repeat [repeat description]
*
* @return {[type]} [return description]
*/
async function executeCommandRepeat(command, times) {
for (const index of Array(times)) {
await vscode.commands.executeCommand(`macros.${command}`)
}
}
/**
* [executeCommand description]
*
* @param {[type]} action [action description]
*
* @return {[type]} [return description]
*/
function executeCommand(action) {
if (typeof action === 'object') {
let command = action.command
let args = action.args
if (command === '$delay') {
return executeDelayCommand(args.delay)
}
if (args.hasOwnProperty('command')) {
return executeCommandTimesOther(command, args.command)
} else if (args.hasOwnProperty('times')) {
return executeCommandRepeat(command, args.times)
}
return vscode.commands.executeCommand(command, args)
}
return vscode.commands.executeCommand(action)
}
/**
* [loadMacros description]
*
* @param {[type]} context [context description]
*
* @return {[type]} [return description]
*/
function loadMacros(context) {
const settings = getSettings().get('list')
getMacrosList().forEach((name) => {
const disposable = vscode.commands.registerCommand(`macros.${name}`, () => {
return settings[name].reduce((promise, action) => promise.then(() => executeCommand(action)), Promise.resolve())
})
context.subscriptions.push(disposable)
disposables.push(disposable)
})
}
/**
* [disposeMacros description]
*
* @return {[type]} [return description]
*/
function disposeMacros() {
for (let disposable of disposables) {
disposable.dispose()
}
}
function deactivate() { }
exports.deactivate = deactivate
exports.activate = activate