forked from Silverquark/dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
179 lines (150 loc) · 5.04 KB
/
index.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
import * as vscode from "vscode";
import { Context, EditorRequiredError } from "../api";
import type { Extension } from "../state/extension";
import type { Register, Registers } from "../state/registers";
/**
* Indicates that a register is expected; if no register is given, the
* specified default should be given instead.
*/
export type RegisterOr<_Default extends keyof Registers,
Flags extends Register.Flags | Register.Flags[] = Register.Flags.None>
= Register.WithFlags<Flags>;
/**
* Indicates that an input is expected; if no input is given, the specified
* function will be used to update the input value in subsequent executions of
* this command.
*/
export interface InputOr<ArgumentName extends string, T> {
(promptDefaultInput: () => T): T;
(promptDefaultInput: () => Thenable<T>): Thenable<T>;
}
/**
* Indicates that a value passed as a command argument is expected.
*/
export type Argument<T> = T;
/**
* The type of a `Context` passed to a command, based on whether the command
* requires an active text editor or not.
*/
export type ContextType<RequiresActiveEditor extends boolean = boolean>
= RequiresActiveEditor extends true ? Context : Context.WithoutActiveEditor;
/**
* The type of the handler of a `CommandDescriptor`.
*/
export interface Handler<RequiresActiveEditor extends boolean = boolean> {
(context: ContextType<RequiresActiveEditor>,
argument: Record<string, any>): unknown | Thenable<unknown>;
}
/**
* The descriptor of a command.
*/
export class CommandDescriptor<Flags extends CommandDescriptor.Flags = CommandDescriptor.Flags> {
public get requiresActiveEditor() {
return (this.flags & CommandDescriptor.Flags.RequiresActiveEditor) !== 0;
}
public get shouldBeReplayed() {
return (this.flags & CommandDescriptor.Flags.DoNotReplay) === 0;
}
public constructor(
/**
* The unique identifier of the command.
*/
public readonly identifier: string,
/**
* The handler of the command.
*/
public readonly handler: Handler<Flags extends CommandDescriptor.Flags.RequiresActiveEditor
? true : false>,
/**
* The flags of the command.
*/
public readonly flags: Flags,
) {
Object.freeze(this);
}
/**
* Executes the command with the given argument.
*/
public replay(context: ContextType<Flags extends CommandDescriptor.Flags.RequiresActiveEditor
? true : false>, argument: Record<string, any>) {
return this.handler(context, argument);
}
/**
* Invokes the command with the given argument.
*/
public async invoke(extension: Extension, argument: unknown) {
const context = Context.create(extension, this);
if (this.requiresActiveEditor && !(context instanceof Context)) {
throw new EditorRequiredError();
}
const ownedArgument = Object.assign({}, argument) as Record<string, unknown>;
if (ownedArgument["count"] === undefined && extension.currentCount !== 0) {
ownedArgument["count"] = extension.currentCount;
}
if (ownedArgument["register"] === undefined && extension.currentRegister !== undefined) {
ownedArgument["register"] = extension.currentRegister;
}
if (ownedArgument["record"] === false) {
context.doNotRecord();
}
extension.currentCount = 0;
extension.currentRegister = undefined;
let result: unknown;
try {
result = await this.handler(context as any, ownedArgument);
} catch (e) {
if ((ownedArgument as { readonly try: boolean }).try) {
return;
}
throw e;
}
// Record command *after* executing it, to ensure it did not encounter
// an error.
if (context.shouldRecord()) {
extension.recorder.recordCommand(this, ownedArgument);
}
if (this.requiresActiveEditor) {
await (context as Context).insertUndoStop();
}
return result;
}
/**
* Invokes the command with the given argument, ensuring that errors are
* reporting to the user instead of throwing them.
*/
public invokeSafely(extension: Extension, argument: unknown) {
return extension.runPromiseSafely(
() => this.invoke(extension, argument),
() => undefined,
(e) => `error executing command "${this.identifier}": ${e.message}`,
);
}
/**
* Registers the command for use by VS Code.
*/
public register(extension: Extension): vscode.Disposable {
return vscode.commands.registerCommand(
this.identifier,
(argument) => this.invokeSafely(extension, argument),
);
}
}
export declare namespace CommandDescriptor {
/**
* Flags describing the behavior of some commands.
*/
export const enum Flags {
/** No specific behavior. */
None = 0b0000,
/** An active editor must be available. */
RequiresActiveEditor = 0b0001,
/** The command should not be replayed in macros and repeats. */
DoNotReplay = 0b0010,
}
}
/**
* A record from command identifier to command descriptor.
*/
export interface Commands {
readonly [commandIdentifier: string]: CommandDescriptor;
}