neonctl 2.37.1 → 2.38.1

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.
Files changed (159) hide show
  1. package/README.md +15 -700
  2. package/bin/cli.js +3 -0
  3. package/package.json +8 -75
  4. package/dist/analytics.js +0 -168
  5. package/dist/api.js +0 -720
  6. package/dist/auth.js +0 -125
  7. package/dist/callback.html +0 -60
  8. package/dist/cli.js +0 -9
  9. package/dist/commands/api.js +0 -278
  10. package/dist/commands/auth.js +0 -214
  11. package/dist/commands/bootstrap.js +0 -481
  12. package/dist/commands/branches.js +0 -488
  13. package/dist/commands/bucket.js +0 -549
  14. package/dist/commands/checkout.js +0 -321
  15. package/dist/commands/config.js +0 -626
  16. package/dist/commands/connection_string.js +0 -172
  17. package/dist/commands/data_api.js +0 -284
  18. package/dist/commands/databases.js +0 -82
  19. package/dist/commands/deploy.js +0 -26
  20. package/dist/commands/dev.js +0 -698
  21. package/dist/commands/diff.js +0 -222
  22. package/dist/commands/env.js +0 -189
  23. package/dist/commands/functions.js +0 -373
  24. package/dist/commands/index.js +0 -62
  25. package/dist/commands/init.js +0 -73
  26. package/dist/commands/inspect.js +0 -65
  27. package/dist/commands/ip_allow.js +0 -137
  28. package/dist/commands/link.js +0 -1121
  29. package/dist/commands/neon_auth.js +0 -1028
  30. package/dist/commands/operations.js +0 -28
  31. package/dist/commands/orgs.js +0 -24
  32. package/dist/commands/projects.js +0 -413
  33. package/dist/commands/psql.js +0 -62
  34. package/dist/commands/roles.js +0 -65
  35. package/dist/commands/schema_diff.js +0 -151
  36. package/dist/commands/set_context.js +0 -29
  37. package/dist/commands/snapshots.js +0 -455
  38. package/dist/commands/status.js +0 -40
  39. package/dist/commands/user.js +0 -15
  40. package/dist/commands/vpc_endpoints.js +0 -134
  41. package/dist/config.js +0 -11
  42. package/dist/config_format.js +0 -72
  43. package/dist/context.js +0 -231
  44. package/dist/current_branch_fast_path.js +0 -55
  45. package/dist/dev/env.js +0 -244
  46. package/dist/dev/functions.js +0 -70
  47. package/dist/dev/inputs.js +0 -63
  48. package/dist/dev/runtime.js +0 -146
  49. package/dist/env.js +0 -36
  50. package/dist/env_file.js +0 -159
  51. package/dist/errors.js +0 -80
  52. package/dist/functions_api.js +0 -48
  53. package/dist/help.js +0 -146
  54. package/dist/index.js +0 -240
  55. package/dist/log.js +0 -18
  56. package/dist/parameters.gen.js +0 -480
  57. package/dist/pkg.js +0 -25
  58. package/dist/psql/cli.js +0 -53
  59. package/dist/psql/command/cmd_cond.js +0 -437
  60. package/dist/psql/command/cmd_connect.js +0 -820
  61. package/dist/psql/command/cmd_copy.js +0 -1035
  62. package/dist/psql/command/cmd_describe.js +0 -1815
  63. package/dist/psql/command/cmd_format.js +0 -948
  64. package/dist/psql/command/cmd_io.js +0 -2193
  65. package/dist/psql/command/cmd_lo.js +0 -393
  66. package/dist/psql/command/cmd_meta.js +0 -969
  67. package/dist/psql/command/cmd_misc.js +0 -187
  68. package/dist/psql/command/cmd_pipeline.js +0 -1148
  69. package/dist/psql/command/cmd_restrict.js +0 -171
  70. package/dist/psql/command/cmd_show.js +0 -766
  71. package/dist/psql/command/dispatch.js +0 -343
  72. package/dist/psql/command/inputQueue.js +0 -42
  73. package/dist/psql/command/shared.js +0 -71
  74. package/dist/psql/complete/filenames.js +0 -139
  75. package/dist/psql/complete/index.js +0 -104
  76. package/dist/psql/complete/matcher.js +0 -315
  77. package/dist/psql/complete/psqlVars.js +0 -249
  78. package/dist/psql/complete/queries.js +0 -493
  79. package/dist/psql/complete/rules.js +0 -2424
  80. package/dist/psql/core/common.js +0 -1253
  81. package/dist/psql/core/help.js +0 -576
  82. package/dist/psql/core/mainloop.js +0 -1360
  83. package/dist/psql/core/prompt.js +0 -439
  84. package/dist/psql/core/settings.js +0 -686
  85. package/dist/psql/core/sqlHelp.js +0 -1066
  86. package/dist/psql/core/startup.js +0 -846
  87. package/dist/psql/core/syncVars.js +0 -116
  88. package/dist/psql/core/variables.js +0 -287
  89. package/dist/psql/describe/formatters.js +0 -1330
  90. package/dist/psql/describe/processNamePattern.js +0 -270
  91. package/dist/psql/describe/queries.js +0 -2452
  92. package/dist/psql/describe/versionGate.js +0 -44
  93. package/dist/psql/index.js +0 -2030
  94. package/dist/psql/io/history.js +0 -299
  95. package/dist/psql/io/input.js +0 -120
  96. package/dist/psql/io/lineEditor/buffer.js +0 -325
  97. package/dist/psql/io/lineEditor/complete.js +0 -227
  98. package/dist/psql/io/lineEditor/filename.js +0 -159
  99. package/dist/psql/io/lineEditor/index.js +0 -893
  100. package/dist/psql/io/lineEditor/keymap.js +0 -745
  101. package/dist/psql/io/lineEditor/vt100.js +0 -363
  102. package/dist/psql/io/pgpass.js +0 -202
  103. package/dist/psql/io/pgservice.js +0 -194
  104. package/dist/psql/io/psqlrc.js +0 -422
  105. package/dist/psql/print/aligned.js +0 -1748
  106. package/dist/psql/print/asciidoc.js +0 -230
  107. package/dist/psql/print/crosstab.js +0 -463
  108. package/dist/psql/print/csv.js +0 -76
  109. package/dist/psql/print/html.js +0 -240
  110. package/dist/psql/print/json.js +0 -96
  111. package/dist/psql/print/latex.js +0 -379
  112. package/dist/psql/print/pager.js +0 -267
  113. package/dist/psql/print/troff.js +0 -240
  114. package/dist/psql/print/unaligned.js +0 -99
  115. package/dist/psql/print/units.js +0 -188
  116. package/dist/psql/scanner/slash.js +0 -515
  117. package/dist/psql/scanner/sql.js +0 -914
  118. package/dist/psql/scanner/stringutils.js +0 -394
  119. package/dist/psql/types/backslash.js +0 -1
  120. package/dist/psql/types/connection.js +0 -1
  121. package/dist/psql/types/index.js +0 -7
  122. package/dist/psql/types/printer.js +0 -1
  123. package/dist/psql/types/repl.js +0 -1
  124. package/dist/psql/types/scanner.js +0 -24
  125. package/dist/psql/types/settings.js +0 -1
  126. package/dist/psql/types/variables.js +0 -1
  127. package/dist/psql/wire/connection.js +0 -2857
  128. package/dist/psql/wire/copy.js +0 -108
  129. package/dist/psql/wire/notify.js +0 -59
  130. package/dist/psql/wire/pipeline.js +0 -521
  131. package/dist/psql/wire/protocol.js +0 -466
  132. package/dist/psql/wire/sasl.js +0 -294
  133. package/dist/psql/wire/tls.js +0 -602
  134. package/dist/storage_api.js +0 -147
  135. package/dist/test_utils/fixtures.js +0 -121
  136. package/dist/test_utils/oauth_server.js +0 -9
  137. package/dist/types.js +0 -1
  138. package/dist/utils/ai_gateway_notice.js +0 -180
  139. package/dist/utils/api_enums.js +0 -33
  140. package/dist/utils/auth.js +0 -5
  141. package/dist/utils/branch_notice.js +0 -22
  142. package/dist/utils/branch_picker.js +0 -103
  143. package/dist/utils/compute_units.js +0 -28
  144. package/dist/utils/config_diff.js +0 -185
  145. package/dist/utils/enrichers.js +0 -161
  146. package/dist/utils/esbuild.js +0 -158
  147. package/dist/utils/formats.js +0 -18
  148. package/dist/utils/git_diff.js +0 -90
  149. package/dist/utils/inspect_db.js +0 -153
  150. package/dist/utils/inspect_queries.js +0 -372
  151. package/dist/utils/middlewares.js +0 -20
  152. package/dist/utils/openapi.js +0 -114
  153. package/dist/utils/package_manager.js +0 -68
  154. package/dist/utils/point_in_time.js +0 -56
  155. package/dist/utils/psql.js +0 -120
  156. package/dist/utils/string.js +0 -5
  157. package/dist/utils/ui.js +0 -59
  158. package/dist/utils/zip.js +0 -4
  159. package/dist/writer.js +0 -97
@@ -1,745 +0,0 @@
1
- /**
2
- * Emacs keymap dispatch.
3
- *
4
- * Pure function: take a `KeyEvent` plus the current editor state, mutate
5
- * the state, and return an `Action` describing what the renderer / outer
6
- * loop should do (redraw, submit, signal cancel, request completion, …).
7
- *
8
- * The state lives in `EditorState`, which wraps a `LineBuffer` plus a
9
- * little extra context (history navigation, last-tab timestamp, search
10
- * mode). Keeping dispatch separate from rendering means we can write
11
- * deterministic key-by-key tests without touching a TTY.
12
- */
13
- import { LineBuffer } from "./buffer.js";
14
- export const makeState = (history = [], mode = "emacs") => ({
15
- buffer: new LineBuffer(),
16
- history: [...history],
17
- historyIndex: -1,
18
- liveSnapshot: null,
19
- lastYank: null,
20
- pasting: false,
21
- mode,
22
- viPending: null,
23
- exBuffer: "",
24
- });
25
- /**
26
- * Apply one key event. Returns the resulting action for the outer loop.
27
- * All mutation happens in `state.buffer` (or the navigation fields).
28
- */
29
- export const dispatch = (state, ev) => {
30
- if (state.pasting) {
31
- // Inside a bracketed-paste block we treat every event as a literal char,
32
- // except the closing marker.
33
- if (ev.key === "paste-end") {
34
- state.pasting = false;
35
- return { kind: "paste-end" };
36
- }
37
- if (ev.key === "char" &&
38
- ev.char !== undefined &&
39
- !ev.ctrl &&
40
- !ev.meta) {
41
- state.buffer.insert(ev.char);
42
- return { kind: "redraw" };
43
- }
44
- if (ev.key === "enter") {
45
- state.buffer.insert("\n");
46
- return { kind: "redraw" };
47
- }
48
- if (ev.key === "tab") {
49
- state.buffer.insert("\t");
50
- return { kind: "redraw" };
51
- }
52
- return { kind: "noop" };
53
- }
54
- // ^C always cancels regardless of mode.
55
- if (ev.key === "char" && ev.ctrl && ev.char === "c") {
56
- state.viPending = null;
57
- return { kind: "cancel" };
58
- }
59
- // Route to vi dispatch when in vi modes.
60
- if (state.mode === "ex") {
61
- return dispatchViEx(state, ev);
62
- }
63
- if (state.mode === "normal") {
64
- return dispatchViNormal(state, ev);
65
- }
66
- if (state.mode === "insert") {
67
- return dispatchViInsert(state, ev);
68
- }
69
- switch (ev.key) {
70
- case "paste-start":
71
- state.pasting = true;
72
- return { kind: "paste-start" };
73
- case "paste-end":
74
- state.pasting = false;
75
- return { kind: "paste-end" };
76
- case "enter":
77
- return { kind: "submit" };
78
- case "tab":
79
- return { kind: "complete" };
80
- case "backspace":
81
- if (ev.meta) {
82
- state.buffer.killWordLeft();
83
- }
84
- else {
85
- state.buffer.deleteLeft();
86
- }
87
- return { kind: "redraw" };
88
- case "delete":
89
- state.buffer.deleteRight();
90
- return { kind: "redraw" };
91
- case "left":
92
- if (ev.meta)
93
- state.buffer.moveWordLeft();
94
- else
95
- state.buffer.moveLeft();
96
- return { kind: "redraw" };
97
- case "right":
98
- if (ev.meta)
99
- state.buffer.moveWordRight();
100
- else
101
- state.buffer.moveRight();
102
- return { kind: "redraw" };
103
- case "up":
104
- return navigateHistory(state, -1);
105
- case "down":
106
- return navigateHistory(state, +1);
107
- case "home":
108
- state.buffer.moveHome();
109
- return { kind: "redraw" };
110
- case "end":
111
- state.buffer.moveEnd();
112
- return { kind: "redraw" };
113
- case "escape":
114
- // Bare Escape: ignore (Alt prefixes are decoded into meta:true).
115
- return { kind: "noop" };
116
- case "char":
117
- return handleChar(state, ev);
118
- case "pageup":
119
- case "pagedown":
120
- case "unknown":
121
- return { kind: "bell" };
122
- }
123
- };
124
- const handleChar = (state, ev) => {
125
- const ch = ev.char ?? "";
126
- if (ch.length === 0)
127
- return { kind: "noop" };
128
- if (ev.ctrl) {
129
- switch (ch) {
130
- case "a":
131
- state.buffer.moveHome();
132
- return { kind: "redraw" };
133
- case "e":
134
- state.buffer.moveEnd();
135
- return { kind: "redraw" };
136
- case "b":
137
- state.buffer.moveLeft();
138
- return { kind: "redraw" };
139
- case "f":
140
- state.buffer.moveRight();
141
- return { kind: "redraw" };
142
- case "p":
143
- return navigateHistory(state, -1);
144
- case "n":
145
- return navigateHistory(state, +1);
146
- case "k":
147
- state.buffer.killToEnd();
148
- return { kind: "redraw" };
149
- case "u":
150
- state.buffer.killToStart();
151
- return { kind: "redraw" };
152
- case "w":
153
- state.buffer.killWordLeft();
154
- return { kind: "redraw" };
155
- case "y": {
156
- const yanked = state.buffer.yank();
157
- state.lastYank = yanked ?? null;
158
- return yanked === undefined
159
- ? { kind: "bell" }
160
- : { kind: "redraw" };
161
- }
162
- case "c":
163
- return { kind: "cancel" };
164
- case "d":
165
- if (state.buffer.length === 0)
166
- return { kind: "eof" };
167
- state.buffer.deleteRight();
168
- return { kind: "redraw" };
169
- case "l":
170
- return { kind: "clear-screen" };
171
- case "h":
172
- state.buffer.deleteLeft();
173
- return { kind: "redraw" };
174
- case "r":
175
- return { kind: "search-start" };
176
- case "t": {
177
- // Transpose two chars before cursor. Edge cases per readline:
178
- // - at end-of-line, transpose the two chars before cursor
179
- // - at the very start with <2 chars, bell
180
- transpose(state.buffer);
181
- return { kind: "redraw" };
182
- }
183
- case "_":
184
- case "/": // some terminals send ^/ as 0x1f
185
- return state.buffer.undo()
186
- ? { kind: "redraw" }
187
- : { kind: "bell" };
188
- case "g":
189
- // ^G outside of search is a no-op bell.
190
- return { kind: "bell" };
191
- default:
192
- return { kind: "bell" };
193
- }
194
- }
195
- if (ev.meta) {
196
- switch (ch) {
197
- case "b":
198
- case "B":
199
- state.buffer.moveWordLeft();
200
- return { kind: "redraw" };
201
- case "f":
202
- case "F":
203
- state.buffer.moveWordRight();
204
- return { kind: "redraw" };
205
- case "d":
206
- case "D":
207
- state.buffer.killWordRight();
208
- return { kind: "redraw" };
209
- case "y":
210
- case "Y":
211
- if (state.lastYank === null)
212
- return { kind: "bell" };
213
- {
214
- const next = state.buffer.yankPop(state.lastYank);
215
- if (next === undefined)
216
- return { kind: "bell" };
217
- state.lastYank = next;
218
- }
219
- return { kind: "redraw" };
220
- default:
221
- return { kind: "bell" };
222
- }
223
- }
224
- // Plain printable.
225
- state.buffer.insert(ch);
226
- return { kind: "redraw" };
227
- };
228
- /**
229
- * Move history index by delta and load the corresponding entry. Saves
230
- * the live line on the first upward step so the user can return to it.
231
- */
232
- const navigateHistory = (state, delta) => {
233
- if (state.history.length === 0)
234
- return { kind: "bell" };
235
- if (state.historyIndex === -1) {
236
- if (delta < 0) {
237
- state.liveSnapshot = state.buffer.text;
238
- state.historyIndex = state.history.length - 1;
239
- state.buffer.setText(state.history[state.historyIndex]);
240
- return { kind: "redraw" };
241
- }
242
- return { kind: "bell" };
243
- }
244
- const next = state.historyIndex + delta;
245
- if (next < 0)
246
- return { kind: "bell" };
247
- if (next >= state.history.length) {
248
- // Stepped past newest: restore the live snapshot.
249
- state.historyIndex = -1;
250
- state.buffer.setText(state.liveSnapshot ?? "");
251
- state.liveSnapshot = null;
252
- return { kind: "redraw" };
253
- }
254
- state.historyIndex = next;
255
- state.buffer.setText(state.history[next]);
256
- return { kind: "redraw" };
257
- };
258
- // ---------------------------------------------------------------------------
259
- // vi mode
260
- // ---------------------------------------------------------------------------
261
- /**
262
- * Vi insert-mode dispatch. Behaves mostly like emacs for editing primitives —
263
- * printable keys insert, backspace/delete/arrows work the same way — but Esc
264
- * switches into normal mode. Ctrl keys are intentionally NOT vi commands here
265
- * (^C is handled at the top of `dispatch`).
266
- */
267
- const dispatchViInsert = (state, ev) => {
268
- switch (ev.key) {
269
- case "paste-start":
270
- state.pasting = true;
271
- return { kind: "paste-start" };
272
- case "paste-end":
273
- state.pasting = false;
274
- return { kind: "paste-end" };
275
- case "enter":
276
- return { kind: "submit" };
277
- case "tab":
278
- return { kind: "complete" };
279
- case "escape":
280
- // Leave insert mode; vi convention: cursor steps left so it sits on the
281
- // last inserted char (unless we were already at column 0).
282
- state.mode = "normal";
283
- state.viPending = null;
284
- if (state.buffer.cursor > 0)
285
- state.buffer.moveLeft();
286
- return { kind: "redraw" };
287
- case "backspace":
288
- state.buffer.deleteLeft();
289
- return { kind: "redraw" };
290
- case "delete":
291
- state.buffer.deleteRight();
292
- return { kind: "redraw" };
293
- case "left":
294
- state.buffer.moveLeft();
295
- return { kind: "redraw" };
296
- case "right":
297
- state.buffer.moveRight();
298
- return { kind: "redraw" };
299
- case "up":
300
- return navigateHistory(state, -1);
301
- case "down":
302
- return navigateHistory(state, +1);
303
- case "home":
304
- state.buffer.moveHome();
305
- return { kind: "redraw" };
306
- case "end":
307
- state.buffer.moveEnd();
308
- return { kind: "redraw" };
309
- case "char": {
310
- const ch = ev.char ?? "";
311
- if (ch.length === 0)
312
- return { kind: "noop" };
313
- // ^D on empty buffer still acts as EOF in either vi mode.
314
- if (ev.ctrl && ch === "d" && state.buffer.length === 0) {
315
- return { kind: "eof" };
316
- }
317
- // Ignore other control combos in vi insert; just insert plain printables.
318
- if (ev.ctrl || ev.meta)
319
- return { kind: "noop" };
320
- state.buffer.insert(ch);
321
- return { kind: "redraw" };
322
- }
323
- case "pageup":
324
- case "pagedown":
325
- case "unknown":
326
- return { kind: "bell" };
327
- }
328
- };
329
- /**
330
- * Vi normal-mode dispatch. Implements the core motion + edit subset documented
331
- * in the WP-24 plan: hjkl/0$^/bwe movement, x/X/dd/D/cc/cw/r/~ edits, i/a/I/A
332
- * to switch back to insert. Multi-key sequences (dd, cw, r<char>) are tracked
333
- * via `state.viPending`.
334
- */
335
- const dispatchViNormal = (state, ev) => {
336
- // Pending operator awaiting its next char.
337
- if (state.viPending !== null) {
338
- return continueViPending(state, ev);
339
- }
340
- switch (ev.key) {
341
- case "enter":
342
- return { kind: "submit" };
343
- case "tab":
344
- // No completion in normal mode (matches vim/readline-vi).
345
- return { kind: "bell" };
346
- case "escape":
347
- // Already normal; clear any half-formed operator.
348
- state.viPending = null;
349
- return { kind: "noop" };
350
- case "backspace":
351
- // In normal mode bare backspace is "move left" per readline-vi.
352
- state.buffer.moveLeft();
353
- return { kind: "redraw" };
354
- case "delete":
355
- state.buffer.deleteRight();
356
- return { kind: "redraw" };
357
- case "left":
358
- state.buffer.moveLeft();
359
- return { kind: "redraw" };
360
- case "right":
361
- state.buffer.moveRight();
362
- return { kind: "redraw" };
363
- case "up":
364
- return navigateHistory(state, -1);
365
- case "down":
366
- return navigateHistory(state, +1);
367
- case "home":
368
- state.buffer.moveHome();
369
- return { kind: "redraw" };
370
- case "end":
371
- state.buffer.moveEnd();
372
- return { kind: "redraw" };
373
- case "char":
374
- return handleViNormalChar(state, ev);
375
- case "paste-start":
376
- state.pasting = true;
377
- return { kind: "paste-start" };
378
- case "paste-end":
379
- state.pasting = false;
380
- return { kind: "paste-end" };
381
- case "pageup":
382
- case "pagedown":
383
- case "unknown":
384
- return { kind: "bell" };
385
- }
386
- };
387
- const handleViNormalChar = (state, ev) => {
388
- const ch = ev.char ?? "";
389
- if (ch.length === 0)
390
- return { kind: "noop" };
391
- // ^D on empty buffer is EOF in vi normal mode too.
392
- if (ev.ctrl && ch === "d" && state.buffer.length === 0) {
393
- return { kind: "eof" };
394
- }
395
- // Other ctrl/meta sequences: not bound in normal mode → bell.
396
- if (ev.ctrl || ev.meta)
397
- return { kind: "bell" };
398
- switch (ch) {
399
- // Movement
400
- case "h":
401
- state.buffer.moveLeft();
402
- return { kind: "redraw" };
403
- case "l":
404
- state.buffer.moveRight();
405
- return { kind: "redraw" };
406
- case "b":
407
- state.buffer.moveWordLeft();
408
- return { kind: "redraw" };
409
- case "w":
410
- state.buffer.moveWordRight();
411
- return { kind: "redraw" };
412
- case "e":
413
- viMoveEndOfWord(state.buffer);
414
- return { kind: "redraw" };
415
- case "0":
416
- state.buffer.moveHome();
417
- return { kind: "redraw" };
418
- case "$":
419
- state.buffer.moveEnd();
420
- return { kind: "redraw" };
421
- case "^":
422
- viMoveFirstNonBlank(state.buffer);
423
- return { kind: "redraw" };
424
- // History (vi-style j/k).
425
- case "j":
426
- return navigateHistory(state, +1);
427
- case "k":
428
- return navigateHistory(state, -1);
429
- // Mode switches.
430
- case "i":
431
- state.mode = "insert";
432
- return { kind: "redraw" };
433
- case "a":
434
- if (state.buffer.cursor < state.buffer.length)
435
- state.buffer.moveRight();
436
- state.mode = "insert";
437
- return { kind: "redraw" };
438
- case "I":
439
- state.buffer.moveHome();
440
- state.mode = "insert";
441
- return { kind: "redraw" };
442
- case "A":
443
- state.buffer.moveEnd();
444
- state.mode = "insert";
445
- return { kind: "redraw" };
446
- // Edits.
447
- case "x":
448
- state.buffer.deleteRight();
449
- return { kind: "redraw" };
450
- case "X":
451
- state.buffer.deleteLeft();
452
- return { kind: "redraw" };
453
- case "D":
454
- state.buffer.killToEnd();
455
- return { kind: "redraw" };
456
- case "~":
457
- viToggleCaseAtCursor(state.buffer);
458
- return { kind: "redraw" };
459
- // Multi-key operators: wait for next char.
460
- case "r":
461
- state.viPending = "r";
462
- return { kind: "noop" };
463
- case "d":
464
- state.viPending = "d";
465
- return { kind: "noop" };
466
- case "c":
467
- state.viPending = "c";
468
- return { kind: "noop" };
469
- case "g":
470
- // Stub: only 'gg' (go to first history) might be desirable; for now,
471
- // just consume the prefix and bell on the follow-up.
472
- state.viPending = "g";
473
- return { kind: "noop" };
474
- case ":":
475
- // Enter ex-prompt mode. The renderer draws a `:` line; printable keys
476
- // accumulate in `exBuffer`; Enter executes; Esc returns to normal.
477
- state.mode = "ex";
478
- state.exBuffer = "";
479
- return { kind: "ex-update" };
480
- default:
481
- return { kind: "bell" };
482
- }
483
- };
484
- /**
485
- * Vi ex-prompt dispatch (`:`-line). Mirrors a tiny slice of vim's ex grammar:
486
- *
487
- * - `q`, `quit`, `q!` — abort the current readLine (same as ^C).
488
- * - `w` (no arg) — bell + ignore (we don't have a file to write to).
489
- * - Esc — abort ex; return to normal mode.
490
- * - Enter — execute the accumulated command.
491
- *
492
- * Printable characters extend `state.exBuffer`; Backspace shrinks it. Anything
493
- * else bells. The renderer is responsible for drawing `: ` + exBuffer on its
494
- * own row; we expose `ex-update` actions so it knows when to redraw.
495
- */
496
- const dispatchViEx = (state, ev) => {
497
- switch (ev.key) {
498
- case "escape":
499
- // Abort ex; back to normal without executing anything.
500
- state.mode = "normal";
501
- state.exBuffer = "";
502
- return { kind: "redraw" };
503
- case "enter":
504
- return executeExCommand(state);
505
- case "backspace":
506
- if (state.exBuffer.length === 0) {
507
- // Backspace through the implicit `:` returns to normal mode (matches
508
- // vim's "backspace at column 1 of ex line").
509
- state.mode = "normal";
510
- return { kind: "redraw" };
511
- }
512
- state.exBuffer = state.exBuffer.slice(0, -1);
513
- return { kind: "ex-update" };
514
- case "char": {
515
- const ch = ev.char ?? "";
516
- // Ctrl/Meta combos aren't bound in ex.
517
- if (ch.length === 0 || ev.ctrl || ev.meta)
518
- return { kind: "bell" };
519
- state.exBuffer += ch;
520
- return { kind: "ex-update" };
521
- }
522
- case "paste-start":
523
- case "paste-end":
524
- case "tab":
525
- case "delete":
526
- case "left":
527
- case "right":
528
- case "up":
529
- case "down":
530
- case "home":
531
- case "end":
532
- case "pageup":
533
- case "pagedown":
534
- case "unknown":
535
- return { kind: "bell" };
536
- }
537
- };
538
- /**
539
- * Interpret the accumulated ex buffer. Returns the action the driver should
540
- * apply (cancel = abort current readLine; bell = unknown command; redraw =
541
- * fall back to normal mode without side effects).
542
- */
543
- const executeExCommand = (state) => {
544
- const cmd = state.exBuffer.trim();
545
- // Always leave ex mode after Enter, even on unknown commands.
546
- state.exBuffer = "";
547
- state.mode = "normal";
548
- switch (cmd) {
549
- case "q":
550
- case "q!":
551
- case "quit":
552
- // Abort the readLine. Same outcome as ^C — driver throws SignalError.
553
- return { kind: "cancel" };
554
- case "w":
555
- // We don't have a file to write to; bell and return to normal.
556
- return { kind: "bell" };
557
- case "":
558
- // Bare `:` then Enter — just return to normal silently.
559
- return { kind: "redraw" };
560
- default:
561
- return { kind: "bell" };
562
- }
563
- };
564
- /**
565
- * Resolve a pending vi operator using the next key event. `r<char>` replaces
566
- * one char; `dd` / `cc` operate on the whole line; `dw` / `cw` operate on a
567
- * word; anything else bells.
568
- */
569
- const continueViPending = (state, ev) => {
570
- const pending = state.viPending;
571
- state.viPending = null;
572
- // Escape cancels a pending operator without bell (matches vi convention).
573
- if (ev.key === "escape")
574
- return { kind: "noop" };
575
- if (pending === "r") {
576
- // r<char> replaces one char at cursor with <char>.
577
- if (ev.key !== "char" || ev.char === undefined || ev.ctrl || ev.meta) {
578
- return { kind: "bell" };
579
- }
580
- if (state.buffer.cursor >= state.buffer.length)
581
- return { kind: "bell" };
582
- viReplaceCharAtCursor(state.buffer, ev.char);
583
- return { kind: "redraw" };
584
- }
585
- if (pending === "g") {
586
- // Only 'gg' is recognised; we don't actually implement first-history yet.
587
- return { kind: "bell" };
588
- }
589
- // dd / cc / dw / cw all key off ev.char.
590
- if (ev.key !== "char" || ev.char === undefined || ev.ctrl || ev.meta) {
591
- return { kind: "bell" };
592
- }
593
- const c = ev.char;
594
- if (pending === "d") {
595
- if (c === "d") {
596
- // dd: kill whole line.
597
- state.buffer.moveHome();
598
- state.buffer.killToEnd();
599
- return { kind: "redraw" };
600
- }
601
- if (c === "w") {
602
- state.buffer.killWordRight();
603
- return { kind: "redraw" };
604
- }
605
- if (c === "b") {
606
- state.buffer.killWordLeft();
607
- return { kind: "redraw" };
608
- }
609
- if (c === "$") {
610
- state.buffer.killToEnd();
611
- return { kind: "redraw" };
612
- }
613
- if (c === "0") {
614
- state.buffer.killToStart();
615
- return { kind: "redraw" };
616
- }
617
- return { kind: "bell" };
618
- }
619
- if (pending === "c") {
620
- if (c === "c") {
621
- // cc: kill whole line, enter insert.
622
- state.buffer.moveHome();
623
- state.buffer.killToEnd();
624
- state.mode = "insert";
625
- return { kind: "redraw" };
626
- }
627
- if (c === "w") {
628
- state.buffer.killWordRight();
629
- state.mode = "insert";
630
- return { kind: "redraw" };
631
- }
632
- if (c === "b") {
633
- state.buffer.killWordLeft();
634
- state.mode = "insert";
635
- return { kind: "redraw" };
636
- }
637
- if (c === "$") {
638
- state.buffer.killToEnd();
639
- state.mode = "insert";
640
- return { kind: "redraw" };
641
- }
642
- if (c === "0") {
643
- state.buffer.killToStart();
644
- state.mode = "insert";
645
- return { kind: "redraw" };
646
- }
647
- return { kind: "bell" };
648
- }
649
- return { kind: "bell" };
650
- };
651
- /** `e` motion: jump to the end of the current word (or the next word). */
652
- const viMoveEndOfWord = (buf) => {
653
- const text = buf.text;
654
- const cps = Array.from(text);
655
- let i = buf.cursor;
656
- // If we're between/inside a word, move to the last char of the word that
657
- // ends at or after cursor.
658
- // Step 1: if cursor is at a non-word, skip to the next word's first char.
659
- while (i < cps.length && !isWordChar(cps[i]))
660
- i++;
661
- // Step 2: if we landed inside a word, advance to its last char (one before
662
- // the next non-word boundary).
663
- while (i < cps.length - 1 && isWordChar(cps[i + 1]))
664
- i++;
665
- // Vi's `e` leaves the cursor ON the last char, not past it. Our buffer uses
666
- // cursor "between code points", so put it after that last char.
667
- if (i < cps.length)
668
- i++;
669
- buf.setText(text, i);
670
- };
671
- /** `^` motion: first non-blank char on the line. */
672
- const viMoveFirstNonBlank = (buf) => {
673
- const text = buf.text;
674
- const cps = Array.from(text);
675
- let i = 0;
676
- while (i < cps.length && (cps[i] === " " || cps[i] === "\t"))
677
- i++;
678
- buf.setText(text, i);
679
- };
680
- /** `~` toggles the case of the character at the cursor; advances cursor. */
681
- const viToggleCaseAtCursor = (buf) => {
682
- const text = buf.text;
683
- const cps = Array.from(text);
684
- const i = buf.cursor;
685
- if (i >= cps.length)
686
- return;
687
- const ch = cps[i];
688
- const flipped = ch === ch.toLowerCase() ? ch.toUpperCase() : ch.toLowerCase();
689
- if (flipped === ch) {
690
- // Non-alpha: just advance.
691
- buf.setText(text, Math.min(i + 1, cps.length));
692
- return;
693
- }
694
- buf.pushUndo();
695
- cps[i] = flipped;
696
- buf.setText(cps.join(""), Math.min(i + 1, cps.length));
697
- };
698
- /** `r<char>` replaces the character at cursor and leaves cursor in place. */
699
- const viReplaceCharAtCursor = (buf, ch) => {
700
- const text = buf.text;
701
- const cps = Array.from(text);
702
- const i = buf.cursor;
703
- if (i >= cps.length)
704
- return;
705
- buf.pushUndo();
706
- cps[i] = ch;
707
- buf.setText(cps.join(""), i);
708
- };
709
- // Re-export for vi helpers that need word classification (matches buffer.ts).
710
- const isWordChar = (ch) => {
711
- if (ch.length === 0)
712
- return false;
713
- const cp = ch.codePointAt(0) ?? 0;
714
- if (cp >= 0x30 && cp <= 0x39)
715
- return true; // 0-9
716
- if (cp >= 0x41 && cp <= 0x5a)
717
- return true; // A-Z
718
- if (cp >= 0x61 && cp <= 0x7a)
719
- return true; // a-z
720
- if (cp === 0x5f)
721
- return true; // _
722
- if (cp > 0x7f)
723
- return cp >= 0xa0;
724
- return false;
725
- };
726
- /** ^T: swap the character before the cursor with the one before it. */
727
- const transpose = (buf) => {
728
- const len = buf.length;
729
- if (len < 2)
730
- return;
731
- const text = buf.text;
732
- const cps = Array.from(text);
733
- let i = buf.cursor;
734
- if (i === 0)
735
- return;
736
- if (i === len)
737
- i--; // at EOL: act on the last two chars
738
- if (i < 1)
739
- return;
740
- buf.pushUndo();
741
- const tmp = cps[i - 1];
742
- cps[i - 1] = cps[i];
743
- cps[i] = tmp;
744
- buf.setText(cps.join(""), Math.min(i + 1, len));
745
- };