-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathkeyDefinitions.ts
174 lines (159 loc) · 4.91 KB
/
keyDefinitions.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
import EventEmitter from './utils/eventEmitter';
import Session from './session';
import KeyHandler, { KeyStream } from './keyHandler';
import Cursor from './cursor';
import Path from './path';
import { ModeId, CursorOptions } from './types';
// NOTE: this is a special key, which accepts any motion keys.
// It causes definition functions to take an extra cursor argument.
// For more info/context, see keyBindings.ts and definitions of CHANGE/DELETE/YANK
export const motionKey = '<motion>';
type MotionName = string;
export type MotionFn = (cursor: Cursor, options: CursorOptions) => Promise<void>;
// a motion is a function taking a cursor and moving it
export type MotionDefinition = (
context: ActionContext
) => Promise<MotionFn>;
export class Motion {
public name: MotionName;
public description: string;
public definition: MotionDefinition;
constructor(name: MotionName, description: string, definition: MotionDefinition) {
this.name = name;
this.description = description;
this.definition = definition;
}
}
export enum SequenceAction {
DROP,
DROP_ALL,
KEEP,
};
export type ActionContext = {
mode: ModeId;
session: Session;
repeat: number;
keyStream: KeyStream;
keyHandler: KeyHandler;
motion?: MotionFn;
visual_line?: {
start_i: number,
end_i: number,
start: Path,
end: Path,
selected: Array<Path>,
parent: Path,
num_rows: number,
},
};
export type ActionDefinition = (context: ActionContext) => Promise<void>;
export type ActionName = string;
export type ActionMetadata = {
sequence?: SequenceAction;
acceptsMotion?: boolean;
};
export class Action {
public name: ActionName;
public description: string;
public definition: ActionDefinition;
public metadata: ActionMetadata;
constructor(name: MotionName, description: string, definition: ActionDefinition, metadata?: ActionMetadata) {
this.name = name;
this.description = description;
this.definition = definition;
this.metadata = metadata || {};
}
}
// NOTE: doesn't compose metadata
export function composeActions(
name: ActionName, description: string,
parts: Array<Action | Motion>
): Action {
const definition = async function(context: ActionContext) {
let i = 0;
while (i < parts.length) {
const part = parts[i];
if (part instanceof Motion) {
throw new Error('Cannot compose with motion without an action that accepts it');
}
if (part.metadata.acceptsMotion) {
i++;
const motion = parts[i];
if (motion instanceof Action) {
throw new Error(
`Error while composing action ${name}:
Action accepting motion was not followed by motion`
);
}
context.motion = await motion.definition.call(motion.definition, context);
}
await part.definition(context);
i++;
}
};
return new Action(name, description, definition);
}
export class KeyDefinitions extends EventEmitter {
private registry: {[name: string]: Action | Motion};
constructor() {
super();
this.registry = {};
}
public getRegistration(name: string): Action | Motion | null {
return this.registry[name] || null;
}
public registerMotion(motion: Motion) {
if (this.registry[motion.name]) {
throw new Error(`${motion.name} already defined!`);
}
this.registry[motion.name] = motion;
this.emit('update');
}
public deregisterMotion(motionName: MotionName) {
const motion = this.registry[motionName];
if (!motion) {
throw new Error(`Tried to deregister motion ${motionName}, but it was not registered!`);
}
if (motion instanceof Action) {
throw new Error(`Tried to deregister motion ${motionName}, but it was registered as an action!`);
}
delete this.registry[motionName];
this.emit('update');
}
public registerComposedAction(
name: ActionName, description: string,
part_names: Array<string>
) {
this.registerAction(composeActions(
name, description,
part_names.map((part_name) => {
const part = this.getRegistration(part_name);
if (part == null) {
throw new Error(
`Could not compose action ${name} with unregistered part ${part_name}`
);
}
return part;
})
));
}
public registerAction(action: Action) {
if (this.registry[action.name]) {
throw new Error(`${action.name} already defined!`);
}
this.registry[action.name] = action;
this.emit('update');
}
public deregisterAction(actionName: ActionName) {
const action = this.registry[actionName];
if (!action) {
throw new Error(`Tried to deregister action ${actionName}, but it was not registered!`);
}
if (action instanceof Motion) {
throw new Error(`Tried to deregister action ${actionName}, but it was registered as a motion!`);
}
delete this.registry[actionName];
this.emit('update');
}
}
export default new KeyDefinitions();