forked from 71/dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.ts
281 lines (236 loc) · 7.43 KB
/
misc.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import * as api from "../api";
import { Argument, InputOr, RegisterOr } from ".";
import { ArgumentError, Context, InputError, keypress, Menu, prompt, showLockedMenu, showMenu, validateMenu } from "../api";
import { Extension } from "../state/extension";
import { Register } from "../state/registers";
/**
* Miscellaneous commands that don't deserve their own category.
*
* By default, Dance also exports the following keybindings for existing
* commands:
*
* | Keybinding | Command |
* | -------------- | ----------------------------------- |
* | `s-;` (normal) | `["workbench.action.showCommands"]` |
*/
declare module "./misc";
/**
* Cancel Dance operation.
*
* @keys `escape` (normal), `escape` (input)
*/
export function cancel(extension: Extension) {
// Calling a new command resets pending operations, so we don't need to do
// anything special here.
extension.cancelLastOperation();
}
/**
* Ignore key.
*/
export function ignore() {
// Used to intercept and ignore key presses in a given mode.
}
const runHistory: string[] = [];
/**
* Run code.
*/
export async function run(
_: Context,
inputOr: InputOr<string | readonly string[]>,
commands?: Argument<api.command.Any[]>,
) {
if (Array.isArray(commands)) {
return api.commands(...commands);
}
let code = await inputOr(() => prompt({
prompt: "Code to run",
validateInput(value) {
try {
api.run.compileFunction(value);
return;
} catch (e) {
if (e instanceof SyntaxError) {
return `invalid syntax: ${e.message}`;
}
return e?.message ?? `${e}`;
}
},
history: runHistory,
}, _));
if (Array.isArray(code)) {
code = code.join("\n");
} else if (typeof code !== "string") {
return new InputError(`expected code to be a string or an array, but it was ${code}`);
}
return _.run(() => api.run(code as string));
}
/**
* Select register for next command.
*
* When selecting a register, the next key press is used to determine what
* register is selected. If this key is a `space` character, then a new key
* press is awaited again and the returned register will be specific to the
* current document.
*
* @keys `"` (normal)
* @noreplay
*/
export async function selectRegister(_: Context, inputOr: InputOr<string | Register>) {
const input = await inputOr(() => keypress.forRegister(_));
if (typeof input === "string") {
if (input.length === 0) {
return;
}
_.extension.currentRegister = _.extension.registers.getPossiblyScoped(input, _.document);
} else {
_.extension.currentRegister = input;
}
}
let lastUpdateRegisterText: string | undefined;
/**
* Update the contents of a register.
*
* @noreplay
*/
export async function updateRegister(
_: Context,
register: RegisterOr<"dquote", Register.Flags.CanWrite>,
copyFrom: Argument<Register | string | undefined>,
inputOr: InputOr<string>,
) {
if (copyFrom !== undefined) {
const copyFromRegister: Register = typeof copyFrom === "string"
? _.extension.registers.getPossiblyScoped(copyFrom, _.document)
: copyFrom;
copyFromRegister.ensureCanRead();
await register.set(await copyFromRegister.get());
return;
}
const input = await inputOr(() => prompt({
prompt: "New register contents",
value: lastUpdateRegisterText,
validateInput(value) {
lastUpdateRegisterText = value;
return undefined;
},
}));
await register.set([input]);
}
/**
* Update Dance count.
*
* Update the current counter used to repeat the next command.
*
* #### Additional keybindings
*
* | Title | Keybinding | Command |
* | ------------------------------ | ------------ | ------------------------------------ |
* | Add the digit 0 to the counter | `0` (normal) | `[".updateCount", { addDigits: 0 }]` |
* | Add the digit 1 to the counter | `1` (normal) | `[".updateCount", { addDigits: 1 }]` |
* | Add the digit 2 to the counter | `2` (normal) | `[".updateCount", { addDigits: 2 }]` |
* | Add the digit 3 to the counter | `3` (normal) | `[".updateCount", { addDigits: 3 }]` |
* | Add the digit 4 to the counter | `4` (normal) | `[".updateCount", { addDigits: 4 }]` |
* | Add the digit 5 to the counter | `5` (normal) | `[".updateCount", { addDigits: 5 }]` |
* | Add the digit 6 to the counter | `6` (normal) | `[".updateCount", { addDigits: 6 }]` |
* | Add the digit 7 to the counter | `7` (normal) | `[".updateCount", { addDigits: 7 }]` |
* | Add the digit 8 to the counter | `8` (normal) | `[".updateCount", { addDigits: 8 }]` |
* | Add the digit 9 to the counter | `9` (normal) | `[".updateCount", { addDigits: 9 }]` |
*
* @noreplay
*/
export async function updateCount(
_: Context,
count: number,
extension: Extension,
inputOr: InputOr<number>,
addDigits?: Argument<number>,
) {
if (typeof addDigits === "number") {
let nextPowerOfTen = 1;
if (addDigits <= 0) {
addDigits = 0;
nextPowerOfTen = 10;
}
while (nextPowerOfTen <= addDigits) {
nextPowerOfTen *= 10;
}
extension.currentCount = count * nextPowerOfTen + addDigits;
return;
}
const input = +await inputOr(() => prompt.number({ integer: true, range: [0, 1_000_000] }, _));
InputError.validateInput(!isNaN(input), "value is not a number");
InputError.validateInput(input >= 0, "value is negative");
extension.currentCount = input;
}
let lastPickedMenu: string | undefined;
/**
* Open menu.
*
* If no input is specified, a prompt will ask for the name of the menu to open.
*
* Alternatively, a `menu` can be inlined in the arguments.
*
* Pass a `prefix` argument to insert the prefix string followed by the typed
* key if it does not match any menu entry. This can be used to implement chords
* like `jj`.
*
* @noreplay
*/
export async function openMenu(
_: Context.WithoutActiveEditor,
inputOr: InputOr<string>,
menu?: Argument<Menu>,
prefix?: Argument<string>,
pass: Argument<any[]> = [],
locked: Argument<boolean> = false,
) {
if (typeof menu === "object") {
const errors = validateMenu(menu);
if (errors.length > 0) {
throw new Error(`invalid menu: ${errors.join(", ")}`);
}
if (locked) {
return showLockedMenu(menu, pass);
}
return showMenu(menu, pass, prefix);
}
const menus = _.extension.menus;
const input = await inputOr(() => prompt({
prompt: "Menu name",
validateInput(value) {
if (menus.has(value)) {
lastPickedMenu = value;
return;
}
return `menu ${JSON.stringify(value)} does not exist`;
},
placeHolder: [...menus.keys()].sort().join(", ") || "no menu defined",
value: lastPickedMenu,
}, _));
if (locked) {
return showLockedMenu.byName(input, pass);
}
return showMenu.byName(input, pass, prefix);
}
/**
* Change current input.
*
* When showing some menus, Dance can navigate their history:
*
* | Keybinding | Command |
* | --------------- | ------------------------------------------ |
* | `up` (prompt) | `[".changeInput", { action: "previous" }]` |
* | `down` (prompt) | `[".changeInput", { action: "next" }]` |
*
* @noreplay
*/
export function changeInput(
action: Argument<Parameters<typeof prompt.notifyActionRequested>[0]>,
) {
ArgumentError.validate(
"action",
["clear", "previous", "next"].includes(action),
`must be "previous" or "next"`,
);
prompt.notifyActionRequested(action);
}