neonctl 2.22.2 → 2.23.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.
- package/README.md +84 -0
- package/analytics.js +5 -2
- package/commands/branches.js +9 -1
- package/commands/connection_string.js +9 -1
- package/commands/functions.js +268 -0
- package/commands/index.js +4 -0
- package/commands/neon_auth.js +1013 -0
- package/commands/projects.js +9 -1
- package/commands/psql.js +6 -1
- package/functions_api.js +43 -0
- package/package.json +15 -5
- package/psql/cli.js +51 -0
- package/psql/command/cmd_cond.js +437 -0
- package/psql/command/cmd_connect.js +815 -0
- package/psql/command/cmd_copy.js +1025 -0
- package/psql/command/cmd_describe.js +1810 -0
- package/psql/command/cmd_format.js +909 -0
- package/psql/command/cmd_io.js +2187 -0
- package/psql/command/cmd_lo.js +385 -0
- package/psql/command/cmd_meta.js +970 -0
- package/psql/command/cmd_misc.js +187 -0
- package/psql/command/cmd_pipeline.js +1141 -0
- package/psql/command/cmd_restrict.js +171 -0
- package/psql/command/cmd_show.js +751 -0
- package/psql/command/dispatch.js +343 -0
- package/psql/command/inputQueue.js +42 -0
- package/psql/command/shared.js +71 -0
- package/psql/complete/filenames.js +139 -0
- package/psql/complete/index.js +104 -0
- package/psql/complete/matcher.js +314 -0
- package/psql/complete/psqlVars.js +247 -0
- package/psql/complete/queries.js +491 -0
- package/psql/complete/rules.js +2387 -0
- package/psql/core/common.js +1250 -0
- package/psql/core/help.js +576 -0
- package/psql/core/mainloop.js +1353 -0
- package/psql/core/prompt.js +437 -0
- package/psql/core/settings.js +684 -0
- package/psql/core/sqlHelp.js +1066 -0
- package/psql/core/startup.js +840 -0
- package/psql/core/syncVars.js +116 -0
- package/psql/core/variables.js +287 -0
- package/psql/describe/formatters.js +1277 -0
- package/psql/describe/processNamePattern.js +270 -0
- package/psql/describe/queries.js +2373 -0
- package/psql/describe/versionGate.js +43 -0
- package/psql/index.js +2005 -0
- package/psql/io/history.js +299 -0
- package/psql/io/input.js +120 -0
- package/psql/io/lineEditor/buffer.js +323 -0
- package/psql/io/lineEditor/complete.js +227 -0
- package/psql/io/lineEditor/filename.js +159 -0
- package/psql/io/lineEditor/index.js +891 -0
- package/psql/io/lineEditor/keymap.js +738 -0
- package/psql/io/lineEditor/vt100.js +363 -0
- package/psql/io/pgpass.js +202 -0
- package/psql/io/pgservice.js +194 -0
- package/psql/io/psqlrc.js +422 -0
- package/psql/print/aligned.js +1756 -0
- package/psql/print/asciidoc.js +248 -0
- package/psql/print/crosstab.js +460 -0
- package/psql/print/csv.js +92 -0
- package/psql/print/html.js +258 -0
- package/psql/print/json.js +96 -0
- package/psql/print/latex.js +396 -0
- package/psql/print/pager.js +265 -0
- package/psql/print/troff.js +258 -0
- package/psql/print/unaligned.js +118 -0
- package/psql/print/units.js +135 -0
- package/psql/scanner/slash.js +513 -0
- package/psql/scanner/sql.js +910 -0
- package/psql/scanner/stringutils.js +390 -0
- package/psql/types/backslash.js +1 -0
- package/psql/types/connection.js +1 -0
- package/psql/types/index.js +7 -0
- package/psql/types/printer.js +1 -0
- package/psql/types/repl.js +1 -0
- package/psql/types/scanner.js +24 -0
- package/psql/types/settings.js +1 -0
- package/psql/types/variables.js +1 -0
- package/psql/wire/connection.js +2844 -0
- package/psql/wire/copy.js +108 -0
- package/psql/wire/notify.js +59 -0
- package/psql/wire/pipeline.js +519 -0
- package/psql/wire/protocol.js +466 -0
- package/psql/wire/sasl.js +296 -0
- package/psql/wire/tls.js +596 -0
- package/test_utils/fixtures.js +1 -0
- package/utils/esbuild.js +147 -0
- package/utils/psql.js +107 -11
- package/utils/zip.js +4 -0
- package/writer.js +1 -1
- package/commands/auth.test.js +0 -211
- package/commands/branches.test.js +0 -460
- package/commands/checkout.test.js +0 -170
- package/commands/connection_string.test.js +0 -196
- package/commands/data_api.test.js +0 -169
- package/commands/databases.test.js +0 -39
- package/commands/help.test.js +0 -9
- package/commands/init.test.js +0 -56
- package/commands/ip_allow.test.js +0 -59
- package/commands/link.test.js +0 -381
- package/commands/operations.test.js +0 -7
- package/commands/orgs.test.js +0 -7
- package/commands/projects.test.js +0 -144
- package/commands/psql.test.js +0 -49
- package/commands/roles.test.js +0 -37
- package/commands/set_context.test.js +0 -159
- package/commands/vpc_endpoints.test.js +0 -69
- package/context.test.js +0 -119
- package/env.test.js +0 -55
- package/utils/formats.test.js +0 -32
- package/writer.test.js +0 -104
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure line-buffer state for the psql line editor.
|
|
3
|
+
*
|
|
4
|
+
* We avoid an actual gap buffer because edited lines are short (~100 chars
|
|
5
|
+
* typical, a few KB worst case for pasted SQL) and JavaScript string
|
|
6
|
+
* concatenation is fast enough at that scale. The "buffer-ish" API still
|
|
7
|
+
* exposes insert/delete-at-cursor primitives so callers stay code-point
|
|
8
|
+
* aware rather than byte aware.
|
|
9
|
+
*
|
|
10
|
+
* Everything in this module operates on Unicode *code points*. Internally
|
|
11
|
+
* we store the line as an array of code-point strings (each element is one
|
|
12
|
+
* scalar value, typically one UTF-16 code unit but two for astral chars).
|
|
13
|
+
* The `cursor` is an index into that array, in the range `[0, length]`.
|
|
14
|
+
*
|
|
15
|
+
* Editing primitives mutate the state in place and push the previous
|
|
16
|
+
* snapshot onto the undo stack. Killed text (^K, ^U, ^W) is appended to
|
|
17
|
+
* the most recent kill ring entry when killing continues in the same
|
|
18
|
+
* direction; yank pulls from the most recent entry.
|
|
19
|
+
*
|
|
20
|
+
* No I/O, no rendering, no terminal awareness — that lives in
|
|
21
|
+
* `keymap.ts` / `index.ts`. Tests cover this module exhaustively.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Encode a string as an array of code-point characters. Each element is
|
|
25
|
+
* one Unicode scalar (`"A"`, `"é"`, `"\u{1F600}"`). This lets cursor
|
|
26
|
+
* arithmetic stay code-point-aligned without dealing with surrogate pairs
|
|
27
|
+
* at every callsite.
|
|
28
|
+
*/
|
|
29
|
+
const toCodePoints = (s) => Array.from(s);
|
|
30
|
+
/**
|
|
31
|
+
* Pure line buffer plus kill ring plus undo. Used by the keymap layer.
|
|
32
|
+
*/
|
|
33
|
+
export class LineBuffer {
|
|
34
|
+
constructor(initial = '', cursor) {
|
|
35
|
+
/** One code point per element. */
|
|
36
|
+
this.chars = [];
|
|
37
|
+
/** Code-point index in `[0, chars.length]`. */
|
|
38
|
+
this._cursor = 0;
|
|
39
|
+
/** Most-recent-first ring of killed text. Bounded to keep memory sane. */
|
|
40
|
+
this.killRing = [];
|
|
41
|
+
/** Index into killRing for the next yank-pop (^Y / Alt-Y). */
|
|
42
|
+
this.yankIndex = -1;
|
|
43
|
+
/** Direction of the previous kill so consecutive kills concatenate. */
|
|
44
|
+
this.lastKill = 'none';
|
|
45
|
+
/** Snapshots for ^_ / undo. */
|
|
46
|
+
this.undoStack = [];
|
|
47
|
+
this.chars = toCodePoints(initial);
|
|
48
|
+
this._cursor =
|
|
49
|
+
cursor === undefined ? this.chars.length : this.clampCursor(cursor);
|
|
50
|
+
}
|
|
51
|
+
// -------------------------------------------------------------------------
|
|
52
|
+
// Accessors
|
|
53
|
+
// -------------------------------------------------------------------------
|
|
54
|
+
get text() {
|
|
55
|
+
return this.chars.join('');
|
|
56
|
+
}
|
|
57
|
+
get cursor() {
|
|
58
|
+
return this._cursor;
|
|
59
|
+
}
|
|
60
|
+
get length() {
|
|
61
|
+
return this.chars.length;
|
|
62
|
+
}
|
|
63
|
+
snapshot() {
|
|
64
|
+
return { text: this.text, cursor: this._cursor };
|
|
65
|
+
}
|
|
66
|
+
restore(snap) {
|
|
67
|
+
this.chars = toCodePoints(snap.text);
|
|
68
|
+
this._cursor = this.clampCursor(snap.cursor);
|
|
69
|
+
this.lastKill = 'none';
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Replace the entire buffer in one go. Used when navigating history or
|
|
73
|
+
* accepting a completion. Does NOT push an undo entry on its own — call
|
|
74
|
+
* `pushUndo()` first if you want to record the previous state.
|
|
75
|
+
*/
|
|
76
|
+
setText(text, cursor) {
|
|
77
|
+
this.chars = toCodePoints(text);
|
|
78
|
+
this._cursor =
|
|
79
|
+
cursor === undefined ? this.chars.length : this.clampCursor(cursor);
|
|
80
|
+
this.lastKill = 'none';
|
|
81
|
+
}
|
|
82
|
+
// -------------------------------------------------------------------------
|
|
83
|
+
// Cursor movement (all in code points, never bytes / UTF-16 units)
|
|
84
|
+
// -------------------------------------------------------------------------
|
|
85
|
+
moveLeft() {
|
|
86
|
+
if (this._cursor > 0)
|
|
87
|
+
this._cursor--;
|
|
88
|
+
this.lastKill = 'none';
|
|
89
|
+
}
|
|
90
|
+
moveRight() {
|
|
91
|
+
if (this._cursor < this.chars.length)
|
|
92
|
+
this._cursor++;
|
|
93
|
+
this.lastKill = 'none';
|
|
94
|
+
}
|
|
95
|
+
moveHome() {
|
|
96
|
+
this._cursor = 0;
|
|
97
|
+
this.lastKill = 'none';
|
|
98
|
+
}
|
|
99
|
+
moveEnd() {
|
|
100
|
+
this._cursor = this.chars.length;
|
|
101
|
+
this.lastKill = 'none';
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Move left over one word. Word ≙ run of alphanumerics. Skip the
|
|
105
|
+
* non-alphanumerics immediately to the left first (matches readline's
|
|
106
|
+
* `M-b`). Pure stdlib `\w` is locale-sensitive in some runtimes; we
|
|
107
|
+
* use a `RegExp` against the actual code-point string.
|
|
108
|
+
*/
|
|
109
|
+
moveWordLeft() {
|
|
110
|
+
let i = this._cursor;
|
|
111
|
+
while (i > 0 && !isWordChar(this.chars[i - 1]))
|
|
112
|
+
i--;
|
|
113
|
+
while (i > 0 && isWordChar(this.chars[i - 1]))
|
|
114
|
+
i--;
|
|
115
|
+
this._cursor = i;
|
|
116
|
+
this.lastKill = 'none';
|
|
117
|
+
}
|
|
118
|
+
moveWordRight() {
|
|
119
|
+
let i = this._cursor;
|
|
120
|
+
while (i < this.chars.length && !isWordChar(this.chars[i]))
|
|
121
|
+
i++;
|
|
122
|
+
while (i < this.chars.length && isWordChar(this.chars[i]))
|
|
123
|
+
i++;
|
|
124
|
+
this._cursor = i;
|
|
125
|
+
this.lastKill = 'none';
|
|
126
|
+
}
|
|
127
|
+
// -------------------------------------------------------------------------
|
|
128
|
+
// Insertion / deletion
|
|
129
|
+
// -------------------------------------------------------------------------
|
|
130
|
+
/** Insert a string at the cursor (split into code points first). */
|
|
131
|
+
insert(s) {
|
|
132
|
+
if (s.length === 0)
|
|
133
|
+
return;
|
|
134
|
+
this.pushUndo();
|
|
135
|
+
const cps = toCodePoints(s);
|
|
136
|
+
this.chars.splice(this._cursor, 0, ...cps);
|
|
137
|
+
this._cursor += cps.length;
|
|
138
|
+
this.lastKill = 'none';
|
|
139
|
+
}
|
|
140
|
+
/** Backspace: delete the code point to the left of cursor. */
|
|
141
|
+
deleteLeft() {
|
|
142
|
+
if (this._cursor === 0)
|
|
143
|
+
return;
|
|
144
|
+
this.pushUndo();
|
|
145
|
+
this.chars.splice(this._cursor - 1, 1);
|
|
146
|
+
this._cursor--;
|
|
147
|
+
this.lastKill = 'none';
|
|
148
|
+
}
|
|
149
|
+
/** Delete the code point to the right of cursor. */
|
|
150
|
+
deleteRight() {
|
|
151
|
+
if (this._cursor === this.chars.length)
|
|
152
|
+
return;
|
|
153
|
+
this.pushUndo();
|
|
154
|
+
this.chars.splice(this._cursor, 1);
|
|
155
|
+
this.lastKill = 'none';
|
|
156
|
+
}
|
|
157
|
+
// -------------------------------------------------------------------------
|
|
158
|
+
// Kill ring operations
|
|
159
|
+
// -------------------------------------------------------------------------
|
|
160
|
+
/** ^K: kill from cursor to end-of-line. Appends if previous kill was forward. */
|
|
161
|
+
killToEnd() {
|
|
162
|
+
if (this._cursor === this.chars.length)
|
|
163
|
+
return;
|
|
164
|
+
this.pushUndo();
|
|
165
|
+
const killed = this.chars.slice(this._cursor).join('');
|
|
166
|
+
this.chars.length = this._cursor;
|
|
167
|
+
this.recordKill(killed, 'forward');
|
|
168
|
+
}
|
|
169
|
+
/** ^U: kill from start-of-line to cursor. */
|
|
170
|
+
killToStart() {
|
|
171
|
+
if (this._cursor === 0)
|
|
172
|
+
return;
|
|
173
|
+
this.pushUndo();
|
|
174
|
+
const killed = this.chars.slice(0, this._cursor).join('');
|
|
175
|
+
this.chars.splice(0, this._cursor);
|
|
176
|
+
this._cursor = 0;
|
|
177
|
+
this.recordKill(killed, 'backward');
|
|
178
|
+
}
|
|
179
|
+
/** ^W: kill the word (backward) to the left of cursor. */
|
|
180
|
+
killWordLeft() {
|
|
181
|
+
if (this._cursor === 0)
|
|
182
|
+
return;
|
|
183
|
+
let i = this._cursor;
|
|
184
|
+
while (i > 0 && !isWordChar(this.chars[i - 1]))
|
|
185
|
+
i--;
|
|
186
|
+
while (i > 0 && isWordChar(this.chars[i - 1]))
|
|
187
|
+
i--;
|
|
188
|
+
if (i === this._cursor)
|
|
189
|
+
return;
|
|
190
|
+
this.pushUndo();
|
|
191
|
+
const killed = this.chars.slice(i, this._cursor).join('');
|
|
192
|
+
this.chars.splice(i, this._cursor - i);
|
|
193
|
+
this._cursor = i;
|
|
194
|
+
this.recordKill(killed, 'backward');
|
|
195
|
+
}
|
|
196
|
+
/** M-d: kill the word (forward) starting at cursor. */
|
|
197
|
+
killWordRight() {
|
|
198
|
+
if (this._cursor === this.chars.length)
|
|
199
|
+
return;
|
|
200
|
+
let i = this._cursor;
|
|
201
|
+
while (i < this.chars.length && !isWordChar(this.chars[i]))
|
|
202
|
+
i++;
|
|
203
|
+
while (i < this.chars.length && isWordChar(this.chars[i]))
|
|
204
|
+
i++;
|
|
205
|
+
if (i === this._cursor)
|
|
206
|
+
return;
|
|
207
|
+
this.pushUndo();
|
|
208
|
+
const killed = this.chars.slice(this._cursor, i).join('');
|
|
209
|
+
this.chars.splice(this._cursor, i - this._cursor);
|
|
210
|
+
this.recordKill(killed, 'forward');
|
|
211
|
+
}
|
|
212
|
+
/** ^Y: yank most recent kill at cursor. No-op if ring is empty. */
|
|
213
|
+
yank() {
|
|
214
|
+
if (this.killRing.length === 0)
|
|
215
|
+
return undefined;
|
|
216
|
+
const top = this.killRing[0];
|
|
217
|
+
this.insert(top);
|
|
218
|
+
this.yankIndex = 0;
|
|
219
|
+
return top;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* M-y: rotate yank ring. Caller must have just yanked. Removes the
|
|
223
|
+
* previously yanked text and inserts the next entry from the ring.
|
|
224
|
+
*/
|
|
225
|
+
yankPop(prevYank) {
|
|
226
|
+
if (this.killRing.length === 0)
|
|
227
|
+
return undefined;
|
|
228
|
+
this.yankIndex = (this.yankIndex + 1) % this.killRing.length;
|
|
229
|
+
if (this._cursor < prevYank.length)
|
|
230
|
+
return undefined;
|
|
231
|
+
const start = this._cursor - toCodePoints(prevYank).length;
|
|
232
|
+
this.pushUndo();
|
|
233
|
+
this.chars.splice(start, toCodePoints(prevYank).length);
|
|
234
|
+
this._cursor = start;
|
|
235
|
+
const next = this.killRing[this.yankIndex];
|
|
236
|
+
const cps = toCodePoints(next);
|
|
237
|
+
this.chars.splice(this._cursor, 0, ...cps);
|
|
238
|
+
this._cursor += cps.length;
|
|
239
|
+
return next;
|
|
240
|
+
}
|
|
241
|
+
/** Reset the kill-merge tracker. Called when a non-kill action runs. */
|
|
242
|
+
resetKillTracking() {
|
|
243
|
+
this.lastKill = 'none';
|
|
244
|
+
}
|
|
245
|
+
/** Test helper / introspection. */
|
|
246
|
+
getKillRing() {
|
|
247
|
+
return this.killRing;
|
|
248
|
+
}
|
|
249
|
+
// -------------------------------------------------------------------------
|
|
250
|
+
// Undo
|
|
251
|
+
// -------------------------------------------------------------------------
|
|
252
|
+
/** Push the current state onto the undo stack. */
|
|
253
|
+
pushUndo() {
|
|
254
|
+
this.undoStack.push({ text: this.text, cursor: this._cursor });
|
|
255
|
+
if (this.undoStack.length > LineBuffer.UNDO_STACK_MAX) {
|
|
256
|
+
this.undoStack.shift();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/** ^_: pop the most recent snapshot. Returns true if anything happened. */
|
|
260
|
+
undo() {
|
|
261
|
+
const snap = this.undoStack.pop();
|
|
262
|
+
if (!snap)
|
|
263
|
+
return false;
|
|
264
|
+
this.chars = toCodePoints(snap.text);
|
|
265
|
+
this._cursor = this.clampCursor(snap.cursor);
|
|
266
|
+
this.lastKill = 'none';
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
// -------------------------------------------------------------------------
|
|
270
|
+
// Internals
|
|
271
|
+
// -------------------------------------------------------------------------
|
|
272
|
+
clampCursor(c) {
|
|
273
|
+
if (c < 0)
|
|
274
|
+
return 0;
|
|
275
|
+
if (c > this.chars.length)
|
|
276
|
+
return this.chars.length;
|
|
277
|
+
return c;
|
|
278
|
+
}
|
|
279
|
+
recordKill(text, dir) {
|
|
280
|
+
if (text.length === 0)
|
|
281
|
+
return;
|
|
282
|
+
if (this.lastKill === dir && this.killRing.length > 0) {
|
|
283
|
+
// Merge with the previous kill so consecutive ^K / ^W feel like one
|
|
284
|
+
// logical kill in the yank ring.
|
|
285
|
+
this.killRing[0] =
|
|
286
|
+
dir === 'forward' ? this.killRing[0] + text : text + this.killRing[0];
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
this.killRing.unshift(text);
|
|
290
|
+
if (this.killRing.length > LineBuffer.KILL_RING_MAX) {
|
|
291
|
+
this.killRing.length = LineBuffer.KILL_RING_MAX;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
this.lastKill = dir;
|
|
295
|
+
this.yankIndex = -1;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
LineBuffer.KILL_RING_MAX = 32;
|
|
299
|
+
LineBuffer.UNDO_STACK_MAX = 256;
|
|
300
|
+
/**
|
|
301
|
+
* Word character classifier. Matches readline's default: ASCII alphanumeric
|
|
302
|
+
* plus underscore. Non-ASCII letters are treated as word chars too so
|
|
303
|
+
* "señor" moves as one word.
|
|
304
|
+
*/
|
|
305
|
+
const isWordChar = (ch) => {
|
|
306
|
+
if (ch.length === 0)
|
|
307
|
+
return false;
|
|
308
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
309
|
+
if (cp >= 0x30 && cp <= 0x39)
|
|
310
|
+
return true; // 0-9
|
|
311
|
+
if (cp >= 0x41 && cp <= 0x5a)
|
|
312
|
+
return true; // A-Z
|
|
313
|
+
if (cp >= 0x61 && cp <= 0x7a)
|
|
314
|
+
return true; // a-z
|
|
315
|
+
if (cp === 0x5f)
|
|
316
|
+
return true; // _
|
|
317
|
+
if (cp > 0x7f) {
|
|
318
|
+
// Treat any non-ASCII printable as word-ish; cheap heuristic but
|
|
319
|
+
// sufficient for editor word motion.
|
|
320
|
+
return cp >= 0xa0;
|
|
321
|
+
}
|
|
322
|
+
return false;
|
|
323
|
+
};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tab-completion plumbing for the line editor.
|
|
3
|
+
*
|
|
4
|
+
* Holds the small bit of state that lives between consecutive Tab presses
|
|
5
|
+
* (last completion result, cycle index, last-tab timestamp). The renderer
|
|
6
|
+
* calls `apply` on each Tab; the function mutates the buffer in place and
|
|
7
|
+
* returns a `CompletionStep` telling the driver whether to ring the bell,
|
|
8
|
+
* insert text, or list candidates below the prompt.
|
|
9
|
+
*
|
|
10
|
+
* State machine:
|
|
11
|
+
*
|
|
12
|
+
* - First Tab: ask the completer. If 0 candidates → `bell`. If 1, insert
|
|
13
|
+
* it. If >1, insert the common prefix (if any) and remember the result.
|
|
14
|
+
* - Second Tab within DOUBLE_TAP_MS: emit a `list` action so the driver
|
|
15
|
+
* prints the candidates below the prompt.
|
|
16
|
+
* - Third Tab and onward: cycle through candidates. The previously
|
|
17
|
+
* inserted candidate is removed before inserting the next.
|
|
18
|
+
*
|
|
19
|
+
* Any non-Tab keystroke (handled by the keymap) calls `reset()` to drop
|
|
20
|
+
* the completion state.
|
|
21
|
+
*/
|
|
22
|
+
/** Threshold for "second Tab" detection. Roughly 500ms by spec. */
|
|
23
|
+
const DOUBLE_TAP_MS = 500;
|
|
24
|
+
export class CompletionState {
|
|
25
|
+
constructor() {
|
|
26
|
+
/** Most recent completion result. Reset to null after non-Tab input. */
|
|
27
|
+
this.result = null;
|
|
28
|
+
/** When the previous tab fired. */
|
|
29
|
+
this.lastTapAt = 0;
|
|
30
|
+
/** How many tabs in a row (the first tab inserts prefix; second lists). */
|
|
31
|
+
this.tabCount = 0;
|
|
32
|
+
/**
|
|
33
|
+
* When cycling, the index of the candidate currently inserted in the
|
|
34
|
+
* buffer. -1 means "no candidate inserted yet (common prefix only)".
|
|
35
|
+
*/
|
|
36
|
+
this.cycleIndex = -1;
|
|
37
|
+
/** Length (in code points) of the candidate currently inserted. */
|
|
38
|
+
this.cycleLen = 0;
|
|
39
|
+
}
|
|
40
|
+
reset() {
|
|
41
|
+
this.result = null;
|
|
42
|
+
this.tabCount = 0;
|
|
43
|
+
this.cycleIndex = -1;
|
|
44
|
+
this.cycleLen = 0;
|
|
45
|
+
this.lastTapAt = 0;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Index of the candidate currently inserted in the buffer, or `-1` if no
|
|
49
|
+
* specific candidate is active (e.g. the user has only seen the common
|
|
50
|
+
* prefix). Used by the listing renderer to reverse-video the active
|
|
51
|
+
* candidate while cycling.
|
|
52
|
+
*/
|
|
53
|
+
getCycleIndex() {
|
|
54
|
+
return this.cycleIndex;
|
|
55
|
+
}
|
|
56
|
+
/** Snapshot of the candidate list, for re-rendering during a cycle. */
|
|
57
|
+
getCandidates() {
|
|
58
|
+
return this.result?.candidates ?? [];
|
|
59
|
+
}
|
|
60
|
+
async apply(buffer, completer, now = Date.now()) {
|
|
61
|
+
const elapsed = now - this.lastTapAt;
|
|
62
|
+
this.lastTapAt = now;
|
|
63
|
+
if (this.result === null || elapsed > DOUBLE_TAP_MS * 4) {
|
|
64
|
+
// Fresh start: ask the completer.
|
|
65
|
+
const res = await Promise.resolve(completer(buffer.text, buffer.cursor));
|
|
66
|
+
this.result = res;
|
|
67
|
+
this.tabCount = 1;
|
|
68
|
+
this.cycleIndex = -1;
|
|
69
|
+
this.cycleLen = 0;
|
|
70
|
+
if (res.candidates.length === 0)
|
|
71
|
+
return { kind: 'bell' };
|
|
72
|
+
if (res.candidates.length === 1) {
|
|
73
|
+
// Mirror upstream readline: a unique completion gets a trailing space
|
|
74
|
+
// (rl_completion_append_character defaults to ' ') unless the result
|
|
75
|
+
// explicitly suppresses it (e.g. schema prefix `public.` that the user
|
|
76
|
+
// is expected to continue typing through) or the candidate itself
|
|
77
|
+
// already ends in a punctuator that shouldn't be followed by a space.
|
|
78
|
+
const cand = res.candidates[0];
|
|
79
|
+
const text = shouldAppendSpace(cand, res) ? cand + ' ' : cand;
|
|
80
|
+
replaceBeforeCursor(buffer, res.replaceLength, text);
|
|
81
|
+
this.reset();
|
|
82
|
+
return { kind: 'inserted' };
|
|
83
|
+
}
|
|
84
|
+
// Multiple candidates: insert the common prefix when it differs from
|
|
85
|
+
// what's currently in the buffer at that position. The diff can be
|
|
86
|
+
// length (more chars to insert) OR case (COMP_KEYWORD_CASE flipping
|
|
87
|
+
// `CO` to `co`) — both should redraw with the canonical form so the
|
|
88
|
+
// user sees what they'd commit on the next keystroke.
|
|
89
|
+
const before = buffer.text.slice(buffer.cursor - res.replaceLength, buffer.cursor);
|
|
90
|
+
if (res.commonPrefix !== before) {
|
|
91
|
+
replaceBeforeCursor(buffer, res.replaceLength, res.commonPrefix);
|
|
92
|
+
}
|
|
93
|
+
// Update the "what's currently inserted" to the common prefix length
|
|
94
|
+
// so the cycle path knows how many code points to overwrite when it
|
|
95
|
+
// swaps in the next candidate.
|
|
96
|
+
this.cycleLen = countCodePoints(res.commonPrefix);
|
|
97
|
+
return { kind: 'inserted' };
|
|
98
|
+
}
|
|
99
|
+
// We already have a result, and the second Tab arrived in time.
|
|
100
|
+
this.tabCount++;
|
|
101
|
+
if (this.tabCount === 2 && elapsed <= DOUBLE_TAP_MS) {
|
|
102
|
+
// List the candidates.
|
|
103
|
+
return { kind: 'list', candidates: this.result.candidates.slice() };
|
|
104
|
+
}
|
|
105
|
+
// Third or later: cycle.
|
|
106
|
+
const cands = this.result.candidates;
|
|
107
|
+
this.cycleIndex = (this.cycleIndex + 1) % cands.length;
|
|
108
|
+
const cand = cands[this.cycleIndex];
|
|
109
|
+
replaceBeforeCursor(buffer, this.cycleLen, cand);
|
|
110
|
+
this.cycleLen = countCodePoints(cand);
|
|
111
|
+
return { kind: 'cycled', candidate: cand };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Replace `replaceLen` code points before cursor with `text`. The buffer
|
|
116
|
+
* loses an undo entry per call; that's intentional so Ctrl-_ can undo a
|
|
117
|
+
* mistaken completion.
|
|
118
|
+
*/
|
|
119
|
+
const replaceBeforeCursor = (buffer, replaceLen, text) => {
|
|
120
|
+
// Move left over the bytes we're replacing, delete them, then insert.
|
|
121
|
+
for (let i = 0; i < replaceLen; i++)
|
|
122
|
+
buffer.deleteLeft();
|
|
123
|
+
buffer.insert(text);
|
|
124
|
+
};
|
|
125
|
+
const countCodePoints = (s) => Array.from(s).length;
|
|
126
|
+
/**
|
|
127
|
+
* Decide whether to append a trailing space after a unique completion.
|
|
128
|
+
* Mirrors upstream readline's `rl_completion_append_character` convention:
|
|
129
|
+
* default is `' '`, but psql clears it for partial completions the user
|
|
130
|
+
* is expected to continue typing (schema prefix, open-quoted identifier,
|
|
131
|
+
* directory path).
|
|
132
|
+
*
|
|
133
|
+
* Rules:
|
|
134
|
+
* - If the result explicitly sets `suppressTrailingSpace: true`, don't.
|
|
135
|
+
* - Candidates already ending in whitespace are left untouched.
|
|
136
|
+
* - Candidates ending in `.`, `/`, `(` are "in progress" (schema
|
|
137
|
+
* namespace, path component, function open-paren) — no space.
|
|
138
|
+
* - Candidates ending in `'` or `"` get a space only when quote count
|
|
139
|
+
* is BALANCED (closed string / closed quoted identifier). An odd
|
|
140
|
+
* count means the user is still inside the quoted region.
|
|
141
|
+
* - Everything else gets a space.
|
|
142
|
+
*/
|
|
143
|
+
const shouldAppendSpace = (candidate, result) => {
|
|
144
|
+
if (result.suppressTrailingSpace === true)
|
|
145
|
+
return false;
|
|
146
|
+
if (candidate.length === 0)
|
|
147
|
+
return false;
|
|
148
|
+
const last = candidate[candidate.length - 1];
|
|
149
|
+
if (last === ' ' || last === '\t' || last === '\n')
|
|
150
|
+
return false;
|
|
151
|
+
if (last === '.' || last === '/' || last === '(')
|
|
152
|
+
return false;
|
|
153
|
+
if (last === '"') {
|
|
154
|
+
// Even count means quotes are balanced (e.g. `"mixedName"`) — completed
|
|
155
|
+
// identifier, append a space. Odd count means we're still inside an
|
|
156
|
+
// open quote (e.g. `"foo`) — leave the user to type more.
|
|
157
|
+
const count = countChar(candidate, '"');
|
|
158
|
+
return count % 2 === 0;
|
|
159
|
+
}
|
|
160
|
+
if (last === "'") {
|
|
161
|
+
const count = countChar(candidate, "'");
|
|
162
|
+
return count % 2 === 0;
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
};
|
|
166
|
+
const countChar = (s, ch) => {
|
|
167
|
+
let n = 0;
|
|
168
|
+
for (const c of s)
|
|
169
|
+
if (c === ch)
|
|
170
|
+
n++;
|
|
171
|
+
return n;
|
|
172
|
+
};
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
// Listing helper: format candidates in column layout for display.
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
/** ANSI reverse-video escape (SGR 7) used to mark the active candidate. */
|
|
177
|
+
const SGR_REVERSE = '\x1b[7m';
|
|
178
|
+
const SGR_NO_REVERSE = '\x1b[27m';
|
|
179
|
+
/**
|
|
180
|
+
* Lay out candidates into a multi-column block. Returns the formatted
|
|
181
|
+
* string (without a trailing newline — caller decides). Columns are sized
|
|
182
|
+
* to fit `termWidth`; we use the longest candidate plus a 2-space gutter.
|
|
183
|
+
*
|
|
184
|
+
* Falls back to one-per-line if `termWidth` is too narrow.
|
|
185
|
+
*
|
|
186
|
+
* If `highlightIndex` is provided and points to a valid candidate, that
|
|
187
|
+
* candidate is wrapped in `\x1b[7m...\x1b[27m` (reverse video) so the user
|
|
188
|
+
* can see which entry is currently inserted in the line during a Tab cycle.
|
|
189
|
+
* Out-of-range indices are silently ignored.
|
|
190
|
+
*/
|
|
191
|
+
export const formatCandidates = (candidates, termWidth, highlightIndex) => {
|
|
192
|
+
if (candidates.length === 0)
|
|
193
|
+
return '';
|
|
194
|
+
const maxLen = candidates.reduce((m, c) => Math.max(m, c.length), 0);
|
|
195
|
+
const colWidth = maxLen + 2;
|
|
196
|
+
const cols = Math.max(1, Math.floor(termWidth / colWidth));
|
|
197
|
+
const rows = Math.ceil(candidates.length / cols);
|
|
198
|
+
const hl = highlightIndex !== undefined &&
|
|
199
|
+
highlightIndex >= 0 &&
|
|
200
|
+
highlightIndex < candidates.length
|
|
201
|
+
? highlightIndex
|
|
202
|
+
: -1;
|
|
203
|
+
const lines = [];
|
|
204
|
+
for (let r = 0; r < rows; r++) {
|
|
205
|
+
const parts = [];
|
|
206
|
+
for (let c = 0; c < cols; c++) {
|
|
207
|
+
const idx = r * cols + c;
|
|
208
|
+
if (idx >= candidates.length)
|
|
209
|
+
break;
|
|
210
|
+
const cand = candidates[idx];
|
|
211
|
+
const padded = cand + ' '.repeat(colWidth - cand.length);
|
|
212
|
+
if (idx === hl) {
|
|
213
|
+
// Wrap only the candidate text, not the gutter spaces, so adjacent
|
|
214
|
+
// columns don't get a colored stripe between them.
|
|
215
|
+
parts.push(SGR_REVERSE +
|
|
216
|
+
cand +
|
|
217
|
+
SGR_NO_REVERSE +
|
|
218
|
+
' '.repeat(colWidth - cand.length));
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
parts.push(padded);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
lines.push(parts.join('').trimEnd());
|
|
225
|
+
}
|
|
226
|
+
return lines.join('\n');
|
|
227
|
+
};
|