@mwillbanks/tuil-editor 0.0.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +90 -29
- package/buffer.d.ts +25 -0
- package/buffer.d.ts.map +1 -0
- package/buffer.js +466 -0
- package/buffer.js.map +12 -0
- package/index.d.ts +133 -0
- package/index.d.ts.map +1 -0
- package/index.js +90 -0
- package/index.js.map +10 -0
- package/package.json +45 -6
- package/rich.d.ts +81 -0
- package/rich.d.ts.map +1 -0
- package/rich.js +1081 -0
- package/rich.js.map +13 -0
- package/session-commands.d.ts +6 -0
- package/session-commands.d.ts.map +1 -0
- package/testing.d.ts +8 -0
- package/testing.d.ts.map +1 -0
- package/testing.js +55 -0
- package/testing.js.map +10 -0
- package/vim.d.ts +31 -0
- package/vim.d.ts.map +1 -0
- package/vim.js +894 -0
- package/vim.js.map +13 -0
package/vim.js
ADDED
|
@@ -0,0 +1,894 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
function validateEditorProvider(providers, defaultId, provider, options) {
|
|
4
|
+
if (!provider.id.trim() || !provider.version.trim()) {
|
|
5
|
+
throw new Error("Editor providers require a stable id and version");
|
|
6
|
+
}
|
|
7
|
+
if (providers.has(provider.id) && !options.replace) {
|
|
8
|
+
throw new Error(`Editor provider "${provider.id}" is already registered`);
|
|
9
|
+
}
|
|
10
|
+
const replacesDefault = options.default && defaultId && defaultId !== provider.id;
|
|
11
|
+
if (replacesDefault && !options.replace) {
|
|
12
|
+
throw new Error(`Default editor "${defaultId}" cannot be replaced accidentally`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function supportsOptional(required, provided) {
|
|
16
|
+
return !required || !provided || provided.includes(required);
|
|
17
|
+
}
|
|
18
|
+
function supportsEditorRequirements(provider, requirements) {
|
|
19
|
+
if (requirements.documentType && provider.documentTypes && !provider.documentTypes.includes(requirements.documentType)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const capabilities = requirements.capabilities ?? [];
|
|
23
|
+
if (!capabilities.every((capability) => provider.capabilities.has(capability)))
|
|
24
|
+
return false;
|
|
25
|
+
return supportsOptional(requirements.renderer, provider.rendererCapabilities) && supportsOptional(requirements.input, provider.inputCapabilities) && supportsOptional(requirements.staticMode, provider.staticModes);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class EditorProviderRegistry {
|
|
29
|
+
#providers;
|
|
30
|
+
#defaultId;
|
|
31
|
+
constructor() {
|
|
32
|
+
this.#providers = new Map;
|
|
33
|
+
this.#defaultId = undefined;
|
|
34
|
+
}
|
|
35
|
+
register(provider, options = {}) {
|
|
36
|
+
validateEditorProvider(this.#providers, this.#defaultId, provider, options);
|
|
37
|
+
this.#providers.set(provider.id, provider);
|
|
38
|
+
if (options.default || !this.#defaultId)
|
|
39
|
+
this.#defaultId = provider.id;
|
|
40
|
+
return Object.freeze({
|
|
41
|
+
provider,
|
|
42
|
+
default: this.#defaultId === provider.id,
|
|
43
|
+
dispose: this.#providerDisposer(provider)
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
#providerDisposer(provider) {
|
|
47
|
+
let disposed = false;
|
|
48
|
+
return () => {
|
|
49
|
+
if (disposed || this.#providers.get(provider.id) !== provider)
|
|
50
|
+
return;
|
|
51
|
+
disposed = true;
|
|
52
|
+
this.#providers.delete(provider.id);
|
|
53
|
+
if (this.#defaultId === provider.id) {
|
|
54
|
+
this.#defaultId = this.#providers.keys().next().value;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
resolve(id, requirements = {}) {
|
|
59
|
+
const preferred = this.#defaultId ? this.#providers.get(this.#defaultId) : undefined;
|
|
60
|
+
const candidates = id ? [this.#providers.get(id)] : [preferred, ...this.#providers.values()];
|
|
61
|
+
const provider = candidates.find((candidate) => candidate !== undefined && supportsEditorRequirements(candidate, requirements));
|
|
62
|
+
if (!provider) {
|
|
63
|
+
throw new Error(`No compatible editor provider found${id ? ` for "${id}"` : ""}`);
|
|
64
|
+
}
|
|
65
|
+
return provider;
|
|
66
|
+
}
|
|
67
|
+
list() {
|
|
68
|
+
return Object.freeze([...this.#providers.values()]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function position(line, column) {
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
line: Math.max(0, Math.floor(line)),
|
|
74
|
+
column: Math.max(0, Math.floor(column))
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function selection(anchor, head = anchor, primary = true) {
|
|
78
|
+
return Object.freeze({
|
|
79
|
+
anchor: position(anchor.line, anchor.column),
|
|
80
|
+
head: position(head.line, head.column),
|
|
81
|
+
primary
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/buffer.ts
|
|
86
|
+
import { createGlobalSearchExpression } from "@mwillbanks/tuil-core";
|
|
87
|
+
|
|
88
|
+
// src/session-commands.ts
|
|
89
|
+
function executeClipboardCommand(session, command, argument) {
|
|
90
|
+
if (command === "copy")
|
|
91
|
+
return Promise.resolve(session.copy()).then(() => true);
|
|
92
|
+
if (command === "cut")
|
|
93
|
+
return Promise.resolve(session.cut()).then(() => true);
|
|
94
|
+
if (command === "paste") {
|
|
95
|
+
return session.paste(typeof argument === "string" ? argument : undefined);
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
function replaceSelections(session, content) {
|
|
100
|
+
const snapshot = session.snapshot();
|
|
101
|
+
if (content === undefined || snapshot.readOnly)
|
|
102
|
+
return false;
|
|
103
|
+
session.dispatch({
|
|
104
|
+
changes: snapshot.selections.map((range) => ({
|
|
105
|
+
range,
|
|
106
|
+
insert: content
|
|
107
|
+
}))
|
|
108
|
+
});
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
function cutSelections(session, copied) {
|
|
112
|
+
const remove = (value) => {
|
|
113
|
+
replaceSelections(session, "");
|
|
114
|
+
return value;
|
|
115
|
+
};
|
|
116
|
+
return copied instanceof Promise ? copied.then(remove) : remove(copied);
|
|
117
|
+
}
|
|
118
|
+
function pasteSelections(session, clipboard, value) {
|
|
119
|
+
if (value !== undefined)
|
|
120
|
+
return replaceSelections(session, value);
|
|
121
|
+
if (!clipboard)
|
|
122
|
+
return false;
|
|
123
|
+
return Promise.resolve(clipboard.read()).then((content) => replaceSelections(session, content));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/buffer.ts
|
|
127
|
+
var graphemeSegmenter = new Intl.Segmenter(undefined, {
|
|
128
|
+
granularity: "grapheme"
|
|
129
|
+
});
|
|
130
|
+
function graphemes(value) {
|
|
131
|
+
return Object.freeze([...graphemeSegmenter.segment(value)].map((segment) => segment.segment));
|
|
132
|
+
}
|
|
133
|
+
function maskText(value) {
|
|
134
|
+
return value.split(`
|
|
135
|
+
`).map((line) => "\u2022".repeat(graphemes(line).length)).join(`
|
|
136
|
+
`);
|
|
137
|
+
}
|
|
138
|
+
function starts(value) {
|
|
139
|
+
const result = [0];
|
|
140
|
+
for (let index = 0;index < value.length; index += 1) {
|
|
141
|
+
if (value[index] === `
|
|
142
|
+
`)
|
|
143
|
+
result.push(index + 1);
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
function clamp(value, target) {
|
|
148
|
+
const lines = value.split(`
|
|
149
|
+
`);
|
|
150
|
+
const line = Math.min(Math.max(0, target.line), Math.max(0, lines.length - 1));
|
|
151
|
+
return position(line, Math.min(Math.max(0, target.column), graphemes(lines[line] ?? "").length));
|
|
152
|
+
}
|
|
153
|
+
function offsetAt(value, target) {
|
|
154
|
+
const safe = clamp(value, target);
|
|
155
|
+
return (starts(value)[safe.line] ?? 0) + graphemes(value.split(`
|
|
156
|
+
`)[safe.line] ?? "").slice(0, safe.column).join("").length;
|
|
157
|
+
}
|
|
158
|
+
function positionAt(value, offset) {
|
|
159
|
+
const safe = Math.max(0, Math.min(value.length, offset));
|
|
160
|
+
const lineStarts = starts(value);
|
|
161
|
+
let line = 0;
|
|
162
|
+
for (let index = 0;index < lineStarts.length; index += 1) {
|
|
163
|
+
if ((lineStarts[index] ?? 0) <= safe)
|
|
164
|
+
line = index;
|
|
165
|
+
else
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
return position(line, graphemes(value.slice(lineStarts[line] ?? 0, safe)).length);
|
|
169
|
+
}
|
|
170
|
+
function offsets(value, range) {
|
|
171
|
+
const anchor = offsetAt(value, range.anchor);
|
|
172
|
+
const head = offsetAt(value, range.head);
|
|
173
|
+
return anchor <= head ? [anchor, head] : [head, anchor];
|
|
174
|
+
}
|
|
175
|
+
function freezeSelections(value, items) {
|
|
176
|
+
return Object.freeze(items.map((item, index) => selection(clamp(value, item.anchor), clamp(value, item.head), item.primary ?? index === 0)));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
class TextBufferSession {
|
|
180
|
+
#id;
|
|
181
|
+
#type;
|
|
182
|
+
#readOnly;
|
|
183
|
+
#masked;
|
|
184
|
+
#clipboard;
|
|
185
|
+
#onDocumentChange;
|
|
186
|
+
#observers = new Set;
|
|
187
|
+
#undo = [];
|
|
188
|
+
#redo = [];
|
|
189
|
+
#text;
|
|
190
|
+
#version = 0;
|
|
191
|
+
#selections;
|
|
192
|
+
#decorations = [];
|
|
193
|
+
#diagnostics = [];
|
|
194
|
+
#mode;
|
|
195
|
+
#viewportAnchor;
|
|
196
|
+
#disposed = false;
|
|
197
|
+
constructor(options = {}) {
|
|
198
|
+
this.#id = options.id ?? "document";
|
|
199
|
+
this.#type = options.documentType ?? "text/plain";
|
|
200
|
+
this.#text = options.value ?? "";
|
|
201
|
+
this.#readOnly = options.readOnly ?? false;
|
|
202
|
+
this.#masked = options.masked ?? false;
|
|
203
|
+
this.#clipboard = options.clipboard;
|
|
204
|
+
this.#onDocumentChange = options.onDocumentChange;
|
|
205
|
+
this.#mode = options.mode ?? "insert";
|
|
206
|
+
this.#viewportAnchor = options.viewportAnchor;
|
|
207
|
+
this.#selections = [selection(position(0, 0))];
|
|
208
|
+
}
|
|
209
|
+
snapshot() {
|
|
210
|
+
return Object.freeze({
|
|
211
|
+
document: Object.freeze({
|
|
212
|
+
id: this.#id,
|
|
213
|
+
type: this.#type,
|
|
214
|
+
version: this.#version,
|
|
215
|
+
text: this.#exportText()
|
|
216
|
+
}),
|
|
217
|
+
selections: this.#selections,
|
|
218
|
+
decorations: this.#decorations,
|
|
219
|
+
diagnostics: this.#diagnostics,
|
|
220
|
+
mode: this.#mode,
|
|
221
|
+
readOnly: this.#readOnly,
|
|
222
|
+
canUndo: this.#undo.length > 0,
|
|
223
|
+
canRedo: this.#redo.length > 0,
|
|
224
|
+
viewportAnchor: this.#viewportAnchor
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
dispatch(transaction) {
|
|
228
|
+
this.#assertActive();
|
|
229
|
+
const changes = [...transaction.changes ?? []];
|
|
230
|
+
if (changes.length > 0 && this.#readOnly) {
|
|
231
|
+
throw new Error("Editor session is read-only");
|
|
232
|
+
}
|
|
233
|
+
this.#recordTransactionHistory(changes, transaction.addToHistory);
|
|
234
|
+
const edits = prepareEdits(this.#text, changes);
|
|
235
|
+
this.#text = applyEdits(this.#text, edits);
|
|
236
|
+
this.#applyTransactionSelection(edits, transaction.selections);
|
|
237
|
+
if (changes.length > 0 || transaction.selections)
|
|
238
|
+
this.#version += 1;
|
|
239
|
+
if (changes.length > 0)
|
|
240
|
+
this.#onDocumentChange?.(this.#text);
|
|
241
|
+
this.#notify();
|
|
242
|
+
return this.snapshot();
|
|
243
|
+
}
|
|
244
|
+
#recordTransactionHistory(changes, addToHistory) {
|
|
245
|
+
if (changes.length === 0 || addToHistory === false)
|
|
246
|
+
return;
|
|
247
|
+
this.#undo.push({ text: this.#text, selections: this.#selections });
|
|
248
|
+
this.#redo.length = 0;
|
|
249
|
+
}
|
|
250
|
+
#applyTransactionSelection(edits, selections) {
|
|
251
|
+
if (selections) {
|
|
252
|
+
this.#selections = freezeSelections(this.#text, selections);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (edits.length === 0)
|
|
256
|
+
return;
|
|
257
|
+
this.#selections = edits.toSorted((left, right) => left.index - right.index).map((edit) => {
|
|
258
|
+
const precedingDelta = edits.filter((candidate) => candidate.offsets[0] < edit.offsets[0]).reduce((total, candidate) => total + candidate.change.insert.length - (candidate.offsets[1] - candidate.offsets[0]), 0);
|
|
259
|
+
return selection(positionAt(this.#text, edit.offsets[0] + edit.change.insert.length + precedingDelta));
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
execute(command, argument) {
|
|
263
|
+
this.#assertActive();
|
|
264
|
+
if (typeof command !== "string")
|
|
265
|
+
return command.execute(this, argument);
|
|
266
|
+
const clipboard = executeClipboardCommand(this, command, argument);
|
|
267
|
+
if (clipboard !== undefined)
|
|
268
|
+
return clipboard;
|
|
269
|
+
return this.#executeSelectionCommand(command);
|
|
270
|
+
}
|
|
271
|
+
#executeSelectionCommand(command) {
|
|
272
|
+
if (command === "select-all") {
|
|
273
|
+
this.#selections = [
|
|
274
|
+
selection(position(0, 0), positionAt(this.#text, this.#text.length))
|
|
275
|
+
];
|
|
276
|
+
} else if (command === "delete-selection") {
|
|
277
|
+
this.dispatch({
|
|
278
|
+
changes: this.#selections.map((range) => ({ range, insert: "" }))
|
|
279
|
+
});
|
|
280
|
+
return true;
|
|
281
|
+
} else if (command === "cursor-start") {
|
|
282
|
+
this.dispatch({
|
|
283
|
+
selections: [selection(position(0, 0))],
|
|
284
|
+
addToHistory: false
|
|
285
|
+
});
|
|
286
|
+
return true;
|
|
287
|
+
} else if (command === "cursor-end") {
|
|
288
|
+
this.dispatch({
|
|
289
|
+
selections: [selection(positionAt(this.#text, this.#text.length))],
|
|
290
|
+
addToHistory: false
|
|
291
|
+
});
|
|
292
|
+
return true;
|
|
293
|
+
} else {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
this.#notify();
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
copy() {
|
|
300
|
+
this.#assertActive();
|
|
301
|
+
const copied = this.#selections.map((range) => {
|
|
302
|
+
const [start, end] = offsets(this.#text, range);
|
|
303
|
+
const value = this.#text.slice(start, end);
|
|
304
|
+
return this.#masked ? maskText(value) : value;
|
|
305
|
+
}).join(`
|
|
306
|
+
`);
|
|
307
|
+
if (!this.#clipboard)
|
|
308
|
+
return copied;
|
|
309
|
+
return Promise.resolve(this.#clipboard.write(copied)).then(() => copied);
|
|
310
|
+
}
|
|
311
|
+
cut() {
|
|
312
|
+
return cutSelections(this, this.copy());
|
|
313
|
+
}
|
|
314
|
+
paste(value) {
|
|
315
|
+
return pasteSelections(this, this.#clipboard, value);
|
|
316
|
+
}
|
|
317
|
+
undo() {
|
|
318
|
+
return this.#restoreHistory(this.#undo, this.#redo);
|
|
319
|
+
}
|
|
320
|
+
redo() {
|
|
321
|
+
return this.#restoreHistory(this.#redo, this.#undo);
|
|
322
|
+
}
|
|
323
|
+
#restoreHistory(source, destination) {
|
|
324
|
+
this.#assertActive();
|
|
325
|
+
const entry = source.pop();
|
|
326
|
+
if (!entry)
|
|
327
|
+
return false;
|
|
328
|
+
destination.push({ text: this.#text, selections: this.#selections });
|
|
329
|
+
this.#text = entry.text;
|
|
330
|
+
this.#selections = entry.selections;
|
|
331
|
+
this.#version += 1;
|
|
332
|
+
this.#onDocumentChange?.(this.#text);
|
|
333
|
+
this.#notify();
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
search(query) {
|
|
337
|
+
this.#assertActive();
|
|
338
|
+
if (typeof query === "string" && query.length === 0)
|
|
339
|
+
return [];
|
|
340
|
+
const expression = createGlobalSearchExpression(query);
|
|
341
|
+
const ranges = [];
|
|
342
|
+
for (const match of this.#text.matchAll(expression)) {
|
|
343
|
+
ranges.push(Object.freeze({
|
|
344
|
+
anchor: positionAt(this.#text, match.index),
|
|
345
|
+
head: positionAt(this.#text, match.index + match[0].length)
|
|
346
|
+
}));
|
|
347
|
+
if (match[0].length === 0)
|
|
348
|
+
expression.lastIndex += 1;
|
|
349
|
+
}
|
|
350
|
+
return Object.freeze(ranges);
|
|
351
|
+
}
|
|
352
|
+
replace(query, replacement, all = true) {
|
|
353
|
+
const ranges = this.search(query);
|
|
354
|
+
const selected = all ? ranges : ranges.slice(0, 1);
|
|
355
|
+
if (selected.length === 0)
|
|
356
|
+
return 0;
|
|
357
|
+
this.dispatch({
|
|
358
|
+
changes: selected.map((range) => ({
|
|
359
|
+
range,
|
|
360
|
+
insert: replacement
|
|
361
|
+
}))
|
|
362
|
+
});
|
|
363
|
+
return selected.length;
|
|
364
|
+
}
|
|
365
|
+
serialize(format = "text") {
|
|
366
|
+
const text = this.#exportText();
|
|
367
|
+
return format === "json" ? JSON.stringify(this.#masked ? { type: this.#type, text, masked: true } : { type: this.#type, text }) : text;
|
|
368
|
+
}
|
|
369
|
+
#exportText() {
|
|
370
|
+
return this.#masked ? maskText(this.#text) : this.#text;
|
|
371
|
+
}
|
|
372
|
+
subscribe(observer) {
|
|
373
|
+
this.#assertActive();
|
|
374
|
+
this.#observers.add(observer);
|
|
375
|
+
return () => this.#observers.delete(observer);
|
|
376
|
+
}
|
|
377
|
+
setDecorations(decorations) {
|
|
378
|
+
this.#decorations = Object.freeze([...decorations]);
|
|
379
|
+
this.#notify();
|
|
380
|
+
}
|
|
381
|
+
setDiagnostics(diagnostics) {
|
|
382
|
+
this.#diagnostics = Object.freeze([...diagnostics]);
|
|
383
|
+
this.#notify();
|
|
384
|
+
}
|
|
385
|
+
setMode(mode) {
|
|
386
|
+
this.#mode = mode;
|
|
387
|
+
this.#notify();
|
|
388
|
+
}
|
|
389
|
+
setViewportAnchor(anchor) {
|
|
390
|
+
this.#viewportAnchor = clamp(this.#text, anchor);
|
|
391
|
+
this.#notify();
|
|
392
|
+
}
|
|
393
|
+
dispose() {
|
|
394
|
+
this.#disposed = true;
|
|
395
|
+
this.#observers.clear();
|
|
396
|
+
this.#undo.length = 0;
|
|
397
|
+
this.#redo.length = 0;
|
|
398
|
+
}
|
|
399
|
+
#notify() {
|
|
400
|
+
const snapshot = this.snapshot();
|
|
401
|
+
for (const observer of this.#observers)
|
|
402
|
+
observer(snapshot);
|
|
403
|
+
}
|
|
404
|
+
#assertActive() {
|
|
405
|
+
if (this.#disposed)
|
|
406
|
+
throw new Error("Editor session is disposed");
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function prepareEdits(text, changes) {
|
|
410
|
+
const edits = changes.map((change, index) => ({
|
|
411
|
+
index,
|
|
412
|
+
change,
|
|
413
|
+
offsets: offsets(text, change.range)
|
|
414
|
+
})).sort((left, right) => right.offsets[0] - left.offsets[0]);
|
|
415
|
+
for (let index = 1;index < edits.length; index += 1) {
|
|
416
|
+
const previous = edits[index - 1];
|
|
417
|
+
const current = edits[index];
|
|
418
|
+
if (previous && current && current.offsets[1] > previous.offsets[0]) {
|
|
419
|
+
throw new Error("Editor transaction changes cannot overlap");
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return edits;
|
|
423
|
+
}
|
|
424
|
+
function applyEdits(text, edits) {
|
|
425
|
+
let result = text;
|
|
426
|
+
for (const edit of edits) {
|
|
427
|
+
result = result.slice(0, edit.offsets[0]) + edit.change.insert + result.slice(edit.offsets[1]);
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
430
|
+
}
|
|
431
|
+
var textBufferProvider = Object.freeze({
|
|
432
|
+
id: "tuil-buffer",
|
|
433
|
+
version: "1",
|
|
434
|
+
capabilities: new Set([
|
|
435
|
+
"single-line",
|
|
436
|
+
"multiline",
|
|
437
|
+
"multiple-selections",
|
|
438
|
+
"history",
|
|
439
|
+
"search",
|
|
440
|
+
"replace",
|
|
441
|
+
"decorations",
|
|
442
|
+
"diagnostics",
|
|
443
|
+
"clipboard",
|
|
444
|
+
"masked",
|
|
445
|
+
"static"
|
|
446
|
+
]),
|
|
447
|
+
documentTypes: [
|
|
448
|
+
"text/plain",
|
|
449
|
+
"text/markdown",
|
|
450
|
+
"text/code",
|
|
451
|
+
"application/query"
|
|
452
|
+
],
|
|
453
|
+
staticModes: ["static", "json", "silent"],
|
|
454
|
+
create: (options) => new TextBufferSession(options)
|
|
455
|
+
});
|
|
456
|
+
function insertText(session, value) {
|
|
457
|
+
const changes = session.snapshot().selections.map((range) => ({ range, insert: value }));
|
|
458
|
+
return session.dispatch({ changes });
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// src/vim.ts
|
|
462
|
+
var graphemeSegmenter2 = new Intl.Segmenter(undefined, {
|
|
463
|
+
granularity: "grapheme"
|
|
464
|
+
});
|
|
465
|
+
function normalizeNormalKey(key) {
|
|
466
|
+
const aliases = {
|
|
467
|
+
left: "h",
|
|
468
|
+
right: "l",
|
|
469
|
+
up: "k",
|
|
470
|
+
down: "j"
|
|
471
|
+
};
|
|
472
|
+
return aliases[key] ?? key;
|
|
473
|
+
}
|
|
474
|
+
function lineGraphemes(value) {
|
|
475
|
+
return [...graphemeSegmenter2.segment(value)].map((entry) => entry.segment);
|
|
476
|
+
}
|
|
477
|
+
function removeLastGrapheme(value) {
|
|
478
|
+
return lineGraphemes(value).slice(0, -1).join("");
|
|
479
|
+
}
|
|
480
|
+
function codeUnitToColumn(value, offset) {
|
|
481
|
+
return lineGraphemes(value.slice(0, offset)).length;
|
|
482
|
+
}
|
|
483
|
+
function documentOffset(value, target) {
|
|
484
|
+
const lines = value.split(`
|
|
485
|
+
`);
|
|
486
|
+
const line = Math.max(0, Math.min(target.line, lines.length - 1));
|
|
487
|
+
const before = lines.slice(0, line).reduce((sum, entry) => sum + entry.length + 1, 0);
|
|
488
|
+
return before + lineGraphemes(lines[line] ?? "").slice(0, Math.max(0, target.column)).join("").length;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
class VimEditorSession extends TextBufferSession {
|
|
492
|
+
#registers = new Map;
|
|
493
|
+
#marks = new Map;
|
|
494
|
+
#keymap;
|
|
495
|
+
#mode = "normal";
|
|
496
|
+
#count = "";
|
|
497
|
+
#pendingOperator;
|
|
498
|
+
#commandLine = "";
|
|
499
|
+
#lastInsert = "";
|
|
500
|
+
#currentInsert = "";
|
|
501
|
+
#markPending = false;
|
|
502
|
+
#jumpPending = false;
|
|
503
|
+
#registerPending = false;
|
|
504
|
+
#activeRegister = '"';
|
|
505
|
+
#lastSearch = "";
|
|
506
|
+
#leaderPending = false;
|
|
507
|
+
#leader;
|
|
508
|
+
constructor(options = {}) {
|
|
509
|
+
super({ ...options, mode: "normal" });
|
|
510
|
+
this.#keymap = Object.freeze({ ...options.keymap });
|
|
511
|
+
this.#leader = options.leader ?? "\\";
|
|
512
|
+
}
|
|
513
|
+
vimState() {
|
|
514
|
+
return Object.freeze({
|
|
515
|
+
mode: this.#mode,
|
|
516
|
+
count: Number(this.#count || "1"),
|
|
517
|
+
pendingOperator: this.#pendingOperator,
|
|
518
|
+
commandLine: this.#commandLine,
|
|
519
|
+
registers: Object.freeze(Object.fromEntries(this.#registers)),
|
|
520
|
+
marks: Object.freeze(Object.fromEntries(this.#marks))
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
key(input) {
|
|
524
|
+
let key = this.#keymap[input] ?? input;
|
|
525
|
+
if (this.#mode === "insert")
|
|
526
|
+
return this.#insertKey(key);
|
|
527
|
+
key = normalizeNormalKey(key);
|
|
528
|
+
if (this.#mode === "command-line")
|
|
529
|
+
return this.#commandKey(key);
|
|
530
|
+
return this.#normalKey(key);
|
|
531
|
+
}
|
|
532
|
+
#normalKey(initialKey) {
|
|
533
|
+
let key = initialKey;
|
|
534
|
+
const leaderKey = this.#resolveLeaderKey(key);
|
|
535
|
+
if (leaderKey === undefined)
|
|
536
|
+
return true;
|
|
537
|
+
key = leaderKey;
|
|
538
|
+
if (this.#consumeCount(key))
|
|
539
|
+
return true;
|
|
540
|
+
if (this.#resolvePendingKey(key))
|
|
541
|
+
return true;
|
|
542
|
+
const state = this.#resolveStateKey(key);
|
|
543
|
+
if (state !== undefined)
|
|
544
|
+
return state;
|
|
545
|
+
const operator = this.#resolveOperatorKey(key);
|
|
546
|
+
return operator ?? this.#resolveMotionKey(key);
|
|
547
|
+
}
|
|
548
|
+
#consumeCount(key) {
|
|
549
|
+
const countKey = /^[1-9]$/.test(key) || Boolean(this.#count) && key === "0";
|
|
550
|
+
if (!countKey)
|
|
551
|
+
return false;
|
|
552
|
+
this.#count += key;
|
|
553
|
+
return true;
|
|
554
|
+
}
|
|
555
|
+
#insertKey(key) {
|
|
556
|
+
if (key === "escape") {
|
|
557
|
+
if (this.#currentInsert)
|
|
558
|
+
this.#lastInsert = this.#currentInsert;
|
|
559
|
+
return this.#setMode("normal");
|
|
560
|
+
}
|
|
561
|
+
if (key === "backspace") {
|
|
562
|
+
this.#currentInsert = removeLastGrapheme(this.#currentInsert);
|
|
563
|
+
return this.#backspace();
|
|
564
|
+
}
|
|
565
|
+
if (["left", "right", "up", "down", "enter"].includes(key))
|
|
566
|
+
return false;
|
|
567
|
+
insertText(this, key);
|
|
568
|
+
this.#currentInsert += key;
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
#resolveLeaderKey(key) {
|
|
572
|
+
if (this.#leaderPending) {
|
|
573
|
+
this.#leaderPending = false;
|
|
574
|
+
return this.#keymap[`${this.#leader}${key}`] ?? key;
|
|
575
|
+
}
|
|
576
|
+
if (key !== this.#leader)
|
|
577
|
+
return key;
|
|
578
|
+
this.#leaderPending = true;
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
#resolvePendingKey(key) {
|
|
582
|
+
if (key.length !== 1)
|
|
583
|
+
return false;
|
|
584
|
+
if (this.#markPending) {
|
|
585
|
+
const cursor = this.snapshot().selections[0]?.head;
|
|
586
|
+
if (cursor)
|
|
587
|
+
this.#marks.set(key, cursor);
|
|
588
|
+
this.#markPending = false;
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
if (this.#jumpPending) {
|
|
592
|
+
const mark = this.#marks.get(key);
|
|
593
|
+
if (mark) {
|
|
594
|
+
this.dispatch({ selections: [selection(mark)], addToHistory: false });
|
|
595
|
+
}
|
|
596
|
+
this.#jumpPending = false;
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
if (!this.#registerPending)
|
|
600
|
+
return false;
|
|
601
|
+
this.#activeRegister = key;
|
|
602
|
+
this.#registerPending = false;
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
#resolveStateKey(key) {
|
|
606
|
+
const mode = this.#resolveModeKey(key);
|
|
607
|
+
if (mode !== undefined)
|
|
608
|
+
return mode;
|
|
609
|
+
const prefix = this.#resolvePrefixKey(key);
|
|
610
|
+
if (prefix !== undefined)
|
|
611
|
+
return prefix;
|
|
612
|
+
return this.#resolveActionKey(key);
|
|
613
|
+
}
|
|
614
|
+
#resolveModeKey(key) {
|
|
615
|
+
const modes = {
|
|
616
|
+
i: "insert",
|
|
617
|
+
v: "visual",
|
|
618
|
+
V: "visual-line"
|
|
619
|
+
};
|
|
620
|
+
const mode = modes[key];
|
|
621
|
+
if (mode) {
|
|
622
|
+
if (mode === "insert")
|
|
623
|
+
this.#currentInsert = "";
|
|
624
|
+
if (mode === "visual" || mode === "visual-line") {
|
|
625
|
+
this.#selectVisualUnit(mode);
|
|
626
|
+
}
|
|
627
|
+
return this.#setMode(mode);
|
|
628
|
+
}
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
#resolvePrefixKey(key) {
|
|
632
|
+
const pending = {
|
|
633
|
+
m: () => {
|
|
634
|
+
this.#markPending = true;
|
|
635
|
+
},
|
|
636
|
+
"'": () => {
|
|
637
|
+
this.#jumpPending = true;
|
|
638
|
+
},
|
|
639
|
+
'"': () => {
|
|
640
|
+
this.#registerPending = true;
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
const action = pending[key];
|
|
644
|
+
if (action) {
|
|
645
|
+
action();
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
if (key === ":" || key === "/") {
|
|
649
|
+
this.#commandLine = key;
|
|
650
|
+
return this.#setMode("command-line");
|
|
651
|
+
}
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
#resolveActionKey(key) {
|
|
655
|
+
const actions = {
|
|
656
|
+
escape: () => this.#cancel(),
|
|
657
|
+
u: () => this.undo(),
|
|
658
|
+
"ctrl+r": () => this.redo(),
|
|
659
|
+
p: () => this.#paste()
|
|
660
|
+
};
|
|
661
|
+
const action = actions[key];
|
|
662
|
+
if (action)
|
|
663
|
+
return action();
|
|
664
|
+
if (key === "n" && this.#lastSearch)
|
|
665
|
+
return this.#nextSearch();
|
|
666
|
+
if (key === "." && this.#lastInsert) {
|
|
667
|
+
insertText(this, this.#lastInsert);
|
|
668
|
+
return true;
|
|
669
|
+
}
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
#resolveOperatorKey(key) {
|
|
673
|
+
const operators = {
|
|
674
|
+
d: "delete",
|
|
675
|
+
c: "change",
|
|
676
|
+
y: "yank"
|
|
677
|
+
};
|
|
678
|
+
const operator = operators[key];
|
|
679
|
+
if (!operator)
|
|
680
|
+
return;
|
|
681
|
+
if (this.#mode === "visual" || this.#mode === "visual-line") {
|
|
682
|
+
return this.#visualOperator(operator);
|
|
683
|
+
}
|
|
684
|
+
if (this.#pendingOperator === operator)
|
|
685
|
+
return this.#lineOperator(operator);
|
|
686
|
+
this.#pendingOperator = operator;
|
|
687
|
+
return this.#setMode("operator-pending", false);
|
|
688
|
+
}
|
|
689
|
+
#resolveMotionKey(key) {
|
|
690
|
+
return ["h", "j", "k", "l", "w", "b", "0", "$"].includes(key) ? this.#motion(key) : false;
|
|
691
|
+
}
|
|
692
|
+
#paste() {
|
|
693
|
+
const value = this.#registers.get(this.#activeRegister) ?? this.#registers.get('"') ?? "";
|
|
694
|
+
if (!value)
|
|
695
|
+
return false;
|
|
696
|
+
insertText(this, value);
|
|
697
|
+
return true;
|
|
698
|
+
}
|
|
699
|
+
#nextSearch() {
|
|
700
|
+
const cursor = this.snapshot().selections[0]?.head ?? position(0, 0);
|
|
701
|
+
const matches = this.search(this.#lastSearch);
|
|
702
|
+
const match = matches.find((range) => range.anchor.line > cursor.line || range.anchor.line === cursor.line && range.anchor.column > cursor.column) ?? matches[0];
|
|
703
|
+
if (match) {
|
|
704
|
+
this.dispatch({
|
|
705
|
+
selections: [selection(match.anchor, match.head)],
|
|
706
|
+
addToHistory: false
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
711
|
+
#setMode(mode, clear = true) {
|
|
712
|
+
this.#mode = mode;
|
|
713
|
+
if (clear && mode !== "operator-pending") {
|
|
714
|
+
this.#pendingOperator = undefined;
|
|
715
|
+
}
|
|
716
|
+
super.setMode(mode);
|
|
717
|
+
return true;
|
|
718
|
+
}
|
|
719
|
+
#cancel() {
|
|
720
|
+
this.#count = "";
|
|
721
|
+
this.#pendingOperator = undefined;
|
|
722
|
+
this.#commandLine = "";
|
|
723
|
+
this.#markPending = false;
|
|
724
|
+
this.#jumpPending = false;
|
|
725
|
+
this.#registerPending = false;
|
|
726
|
+
this.#leaderPending = false;
|
|
727
|
+
return this.#setMode("normal");
|
|
728
|
+
}
|
|
729
|
+
#motion(key) {
|
|
730
|
+
const snapshot = this.snapshot();
|
|
731
|
+
const { cursor, lines, count } = this.#motionState();
|
|
732
|
+
const next = this.#motionTarget(key, cursor, lines, count);
|
|
733
|
+
const current = snapshot.selections[0];
|
|
734
|
+
const range = this.#pendingOperator ? selection(cursor, next) : this.#mode === "visual-line" ? selection(position(current?.anchor.line ?? cursor.line, 0), position(next.line, lineGraphemes(lines[next.line] ?? "").length)) : this.#mode === "visual" ? selection(current?.anchor ?? cursor, next) : selection(next);
|
|
735
|
+
if (this.#pendingOperator) {
|
|
736
|
+
this.#operator(this.#pendingOperator, range);
|
|
737
|
+
} else {
|
|
738
|
+
this.dispatch({ selections: [range], addToHistory: false });
|
|
739
|
+
}
|
|
740
|
+
this.#count = "";
|
|
741
|
+
return true;
|
|
742
|
+
}
|
|
743
|
+
#motionTarget(key, cursor, lines, count) {
|
|
744
|
+
const offsets2 = {
|
|
745
|
+
h: [0, -count],
|
|
746
|
+
l: [0, count],
|
|
747
|
+
j: [count, 0],
|
|
748
|
+
k: [-count, 0]
|
|
749
|
+
};
|
|
750
|
+
const offset = offsets2[key];
|
|
751
|
+
if (offset) {
|
|
752
|
+
return position(cursor.line + offset[0], cursor.column + offset[1]);
|
|
753
|
+
}
|
|
754
|
+
if (key === "0")
|
|
755
|
+
return position(cursor.line, 0);
|
|
756
|
+
const line = lines[cursor.line] ?? "";
|
|
757
|
+
const width = lineGraphemes(line).length;
|
|
758
|
+
if (key === "$")
|
|
759
|
+
return position(cursor.line, width);
|
|
760
|
+
const boundaries = [...line.matchAll(/\w+/gu)].map((match) => codeUnitToColumn(line, match.index));
|
|
761
|
+
const column = key === "w" ? boundaries.find((value) => value > cursor.column) ?? width : [...boundaries].reverse().find((value) => value < cursor.column) ?? 0;
|
|
762
|
+
return position(cursor.line, column);
|
|
763
|
+
}
|
|
764
|
+
#operator(operator, range) {
|
|
765
|
+
const value = this.#selectedText(range);
|
|
766
|
+
this.#registers.set('"', value);
|
|
767
|
+
this.#registers.set(this.#activeRegister, value);
|
|
768
|
+
this.#activeRegister = '"';
|
|
769
|
+
if (operator !== "yank") {
|
|
770
|
+
this.dispatch({ changes: [{ range, insert: "" }] });
|
|
771
|
+
} else {
|
|
772
|
+
this.dispatch({
|
|
773
|
+
selections: [selection(range.anchor)],
|
|
774
|
+
addToHistory: false
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
if (operator === "change")
|
|
778
|
+
this.#currentInsert = "";
|
|
779
|
+
this.#setMode(operator === "change" ? "insert" : "normal");
|
|
780
|
+
}
|
|
781
|
+
#visualOperator(operator) {
|
|
782
|
+
const range = this.snapshot().selections[0];
|
|
783
|
+
if (!range)
|
|
784
|
+
return false;
|
|
785
|
+
this.#operator(operator, range);
|
|
786
|
+
this.#count = "";
|
|
787
|
+
return true;
|
|
788
|
+
}
|
|
789
|
+
#selectVisualUnit(mode) {
|
|
790
|
+
const { cursor, lines } = this.#motionState();
|
|
791
|
+
const lineWidth = lineGraphemes(lines[cursor.line] ?? "").length;
|
|
792
|
+
const range = mode === "visual-line" ? {
|
|
793
|
+
anchor: position(cursor.line, 0),
|
|
794
|
+
head: cursor.line < lines.length - 1 ? position(cursor.line + 1, 0) : position(cursor.line, lineWidth)
|
|
795
|
+
} : {
|
|
796
|
+
anchor: cursor,
|
|
797
|
+
head: position(cursor.line, Math.min(lineWidth, cursor.column + 1))
|
|
798
|
+
};
|
|
799
|
+
this.dispatch({ selections: [selection(range.anchor, range.head)] });
|
|
800
|
+
}
|
|
801
|
+
#lineOperator(operator) {
|
|
802
|
+
const { cursor, lines, count } = this.#motionState();
|
|
803
|
+
const end = Math.min(lines.length - 1, cursor.line + count - 1);
|
|
804
|
+
const nextLine = end < lines.length - 1;
|
|
805
|
+
this.#operator(operator, {
|
|
806
|
+
anchor: position(cursor.line, 0),
|
|
807
|
+
head: position(nextLine ? end + 1 : end, nextLine ? 0 : lineGraphemes(lines[end] ?? "").length)
|
|
808
|
+
});
|
|
809
|
+
this.#count = "";
|
|
810
|
+
return true;
|
|
811
|
+
}
|
|
812
|
+
#motionState() {
|
|
813
|
+
const snapshot = this.snapshot();
|
|
814
|
+
return {
|
|
815
|
+
cursor: snapshot.selections[0]?.head ?? position(0, 0),
|
|
816
|
+
lines: snapshot.document.text.split(`
|
|
817
|
+
`),
|
|
818
|
+
count: Number(this.#count || "1")
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
#selectedText(range) {
|
|
822
|
+
const value = this.serialize();
|
|
823
|
+
const anchor = documentOffset(value, range.anchor);
|
|
824
|
+
const head = documentOffset(value, range.head);
|
|
825
|
+
return value.slice(Math.min(anchor, head), Math.max(anchor, head));
|
|
826
|
+
}
|
|
827
|
+
#backspace() {
|
|
828
|
+
const cursor = this.snapshot().selections[0]?.head;
|
|
829
|
+
if (!cursor || cursor.column === 0)
|
|
830
|
+
return false;
|
|
831
|
+
this.dispatch({
|
|
832
|
+
changes: [
|
|
833
|
+
{
|
|
834
|
+
range: {
|
|
835
|
+
anchor: position(cursor.line, cursor.column - 1),
|
|
836
|
+
head: cursor
|
|
837
|
+
},
|
|
838
|
+
insert: ""
|
|
839
|
+
}
|
|
840
|
+
]
|
|
841
|
+
});
|
|
842
|
+
return true;
|
|
843
|
+
}
|
|
844
|
+
#commandKey(key) {
|
|
845
|
+
if (key === "escape")
|
|
846
|
+
return this.#cancel();
|
|
847
|
+
if (key === "enter") {
|
|
848
|
+
if (this.#commandLine.startsWith("/")) {
|
|
849
|
+
this.#lastSearch = this.#commandLine.slice(1);
|
|
850
|
+
const match = this.search(this.#lastSearch)[0];
|
|
851
|
+
if (match) {
|
|
852
|
+
this.dispatch({
|
|
853
|
+
selections: [selection(match.anchor, match.head)],
|
|
854
|
+
addToHistory: false
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
} else if (this.#commandLine === ":undo") {
|
|
858
|
+
this.undo();
|
|
859
|
+
} else if (this.#commandLine === ":redo") {
|
|
860
|
+
this.redo();
|
|
861
|
+
}
|
|
862
|
+
this.#commandLine = "";
|
|
863
|
+
return this.#setMode("normal");
|
|
864
|
+
}
|
|
865
|
+
this.#commandLine = key === "backspace" ? this.#commandLine.slice(0, -1) : this.#commandLine + key;
|
|
866
|
+
return true;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
var vimEditorProvider = Object.freeze({
|
|
870
|
+
id: "tuil-vim",
|
|
871
|
+
version: "1",
|
|
872
|
+
capabilities: new Set([
|
|
873
|
+
"multiline",
|
|
874
|
+
"history",
|
|
875
|
+
"search",
|
|
876
|
+
"replace",
|
|
877
|
+
"clipboard",
|
|
878
|
+
"vim",
|
|
879
|
+
"static"
|
|
880
|
+
]),
|
|
881
|
+
documentTypes: [
|
|
882
|
+
"text/plain",
|
|
883
|
+
"text/markdown",
|
|
884
|
+
"text/code",
|
|
885
|
+
"application/query"
|
|
886
|
+
],
|
|
887
|
+
create: (options) => new VimEditorSession(options)
|
|
888
|
+
});
|
|
889
|
+
export {
|
|
890
|
+
vimEditorProvider,
|
|
891
|
+
VimEditorSession
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
//# debugId=28F29E7FE54CC11D64756E2164756E21
|