forked from Silverquark/dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.ts
208 lines (175 loc) · 5.28 KB
/
history.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
import * as vscode from "vscode";
import type { Argument, CommandDescriptor, RegisterOr } from ".";
import type { Context } from "../api";
import { ActiveRecording, Cursor, Entry } from "../state/recorder";
import type { Register } from "../state/registers";
import { ArgumentError } from "../utils/errors";
import { newRegExp } from "../utils/regexp";
/**
* Interact with history.
*/
declare module "./history";
/**
* Undo.
*
* @keys `u` (helix: normal), `u` (helix: visual)
*/
export function undo() {
return vscode.commands.executeCommand("undo");
}
/**
* Redo.
*
* @keys `s-u` (helix: normal), `s-u` (helix: visual)
*/
export function redo() {
return vscode.commands.executeCommand("redo");
}
/**
* Undo a change of selections.
*
* @keys `a-u` (kakoune: normal)
*/
export function undo_selections() {
return vscode.commands.executeCommand("cursorUndo");
}
/**
* Redo a change of selections.
*
* @keys `s-a-u` (kakoune: normal)
*/
export function redo_selections() {
return vscode.commands.executeCommand("cursorRedo");
}
/**
* Repeat last change.
*
* @noreplay
*
* | Title | Identifier | Keybinding | Commands |
* | ---------------------------- | ------------------ | ----------------------- | ----------------------------------------------------------------------------- |
* | Repeat last selection change | `repeat.selection` | | `[".history.repeat", { filter: "dance\\.(seek|select|selections)", +count }]` |
* | Repeat last seek | `repeat.seek` | `a-.` (kakoune: normal) | `[".history.repeat", { filter: "dance\\.seek", +count }]` |
*/
export async function repeat(
_: Context,
repetitions: number,
filter: Argument<string | RegExp> = /.+/,
) {
if (typeof filter === "string") {
filter = newRegExp(filter, "u");
}
let commandDescriptor: CommandDescriptor,
commandArgument: object;
const cursor = _.extension.recorder.cursorFromEnd();
for (;;) {
if (cursor.is(Entry.ExecuteCommand)) {
const entry = cursor.entry(),
descriptor = entry.descriptor();
if (descriptor.shouldBeReplayed && filter.test(descriptor.identifier)) {
commandDescriptor = descriptor;
commandArgument = entry.argument();
break;
}
}
if (!cursor.previous()) {
throw new Error("no previous command matching " + filter);
}
}
for (let i = 0; i < repetitions; i++) {
await commandDescriptor.replay(_, commandArgument);
}
}
/**
* Repeat last edit without a command.
*
* @keys `.` (helix: normal) , `NumPad_Decimal` (helix: normal) , `.` (helix: visual) , `NumPad_Decimal` (helix: visual)
* @noreplay
*/
export async function repeat_edit(_: Context, repetitions: number) {
_.doNotRecord();
const recorder = _.extension.recorder,
cursor = recorder.cursorFromEnd();
let startCursor: Cursor | undefined,
endCursor: Cursor | undefined;
for (;;) {
if (cursor.is(Entry.ChangeTextEditorMode)) {
const modeName = cursor.entry().mode().name;
if (modeName === "normal") {
cursor.previous();
endCursor = cursor.clone();
} else if (modeName === "insert" && endCursor !== undefined) {
cursor.next();
startCursor = cursor.clone();
break;
}
}
if (!cursor.previous()) {
throw new Error("cannot find switch to normal or insert mode");
}
}
for (let i = 0; i < repetitions; i++) {
for (let cursor = startCursor.clone(); cursor.isBeforeOrEqual(endCursor); cursor.next()) {
await cursor.replay(_);
}
}
}
/**
* Replay recording.
*
* @keys `q` (helix: normal), `q` (helix: visual)
* @noreplay
*/
export async function recording_play(
_: Context.WithoutActiveEditor,
repetitions: number,
register: RegisterOr<"arobase", Register.Flags.CanReadWriteMacros>,
) {
const recording = register.getRecording();
ArgumentError.validate(
"recording",
recording !== undefined,
() => `register "${register.name}" does not hold a recording`,
);
for (let i = 0; i < repetitions; i++) {
await recording.replay(_);
}
}
const recordingPerRegister = new WeakMap<Register, ActiveRecording>();
/**
* Start recording.
*
* @keys `s-q` (helix: normal, !recording) , `s-q` (helix: visual, !recording)
* @noreplay
*/
export function recording_start(
_: Context,
register: RegisterOr<"arobase", Register.Flags.CanReadWriteMacros>,
) {
ArgumentError.validate(
"register",
!recordingPerRegister.has(register),
"a recording is already active",
);
const recording = _.extension.recorder.startRecording();
recordingPerRegister.set(register, recording);
}
/**
* Stop recording.
*
* @keys `escape` (helix: normal, recording) , `s-q` (helix: normal, recording) , `escape` (helix: visual, recording) , `s-q` (helix: visual, recording)
* @noreplay
*/
export function recording_stop(
_: Context,
register: RegisterOr<"arobase", Register.Flags.CanReadWriteMacros>,
) {
const recording = recordingPerRegister.get(register);
ArgumentError.validate(
"register",
recording !== undefined,
"no recording is active in the given register",
);
recordingPerRegister.delete(register);
register.setRecording(recording.complete());
}