rsvim-types 0.1.3
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/.gitmodules +3 -0
- package/LICENSE.txt +78 -0
- package/README.md +75 -0
- package/package.json +22 -0
- package/runtime/00__web.ts +669 -0
- package/runtime/01__rsvim.ts +2003 -0
- package/tsconfig.json +40 -0
- package/types/00__web.d.ts +252 -0
- package/types/01__rsvim.d.ts +1335 -0
- package/types/index.d.ts +2 -0
|
@@ -0,0 +1,2003 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ---
|
|
3
|
+
* title: Rsvim API
|
|
4
|
+
* sidebar_position: 2
|
|
5
|
+
* ---
|
|
6
|
+
*
|
|
7
|
+
* The `Rsvim` global object, it contains two groups:
|
|
8
|
+
* - General APIs.
|
|
9
|
+
* - Editor APIs.
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*
|
|
13
|
+
* @categoryDescription Global Object
|
|
14
|
+
* The global object.
|
|
15
|
+
*
|
|
16
|
+
* @categoryDescription Editor APIs
|
|
17
|
+
* These APIs are specific for editor, such as buffers, windows, key mappings, etc.
|
|
18
|
+
*
|
|
19
|
+
* @categoryDescription General APIs
|
|
20
|
+
* These APIs are general for common javascript-based runtime, similar to [Deno APIs](https://docs.deno.com/api/deno/).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** @hidden */
|
|
24
|
+
function assertTrue(condition: boolean, message: string) {
|
|
25
|
+
if (!condition) {
|
|
26
|
+
throw new Error(message);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @hidden */
|
|
31
|
+
function assertFalse(condition: boolean, message: string) {
|
|
32
|
+
if (!!condition) {
|
|
33
|
+
throw new Error(message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @hidden */
|
|
38
|
+
function isNull(arg: any): boolean {
|
|
39
|
+
return arg === undefined || arg === null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @hidden */
|
|
43
|
+
function isString(arg: any): boolean {
|
|
44
|
+
return typeof arg === "string";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @hidden */
|
|
48
|
+
function checkNotNull(arg: any, msg: string) {
|
|
49
|
+
if (isNull(arg)) {
|
|
50
|
+
throw new TypeError(`${msg} cannot be undefined or null`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @hidden */
|
|
55
|
+
function checkIsNumber(arg: any, msg: string) {
|
|
56
|
+
if (typeof arg !== "number") {
|
|
57
|
+
throw new TypeError(`${msg} must be a number, but found ${typeof arg}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @hidden */
|
|
62
|
+
function checkIsInteger(arg: any, msg: string) {
|
|
63
|
+
checkIsNumber(arg, msg);
|
|
64
|
+
if (!Number.isInteger(arg)) {
|
|
65
|
+
throw new TypeError(`${msg} must be an integer, but found ${typeof arg}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @hidden */
|
|
70
|
+
function checkIsBoolean(arg: any, msg: string) {
|
|
71
|
+
if (typeof arg !== "boolean") {
|
|
72
|
+
throw new TypeError(`${msg} must be a boolean, but found ${typeof arg}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @hidden */
|
|
77
|
+
function checkIsString(arg: any, msg: string) {
|
|
78
|
+
if (!isString(arg)) {
|
|
79
|
+
throw new TypeError(`${msg} must be a string, but found ${typeof arg}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** @hidden */
|
|
84
|
+
function checkOptionalString(arg: any, msg: string) {
|
|
85
|
+
if (!(isString(arg) || isNull(arg))) {
|
|
86
|
+
throw new TypeError(`${msg} must be a string?, but found ${typeof arg}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @hidden */
|
|
91
|
+
function checkMatchPattern(arg: any, pat: RegExp, msg: string) {
|
|
92
|
+
checkIsString(arg, msg);
|
|
93
|
+
if (!pat.test(arg)) {
|
|
94
|
+
throw new Error(`${msg} is invalid pattern: ${arg}"`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @hidden */
|
|
99
|
+
function checkIsFunction(arg: any, msg: string) {
|
|
100
|
+
if (typeof arg !== "function") {
|
|
101
|
+
throw new TypeError(`${msg} must be a function, but found ${typeof arg}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @hidden */
|
|
106
|
+
function checkIsArray(arg: any, msg: string) {
|
|
107
|
+
if (!Array.isArray(arg)) {
|
|
108
|
+
throw new TypeError(`${msg} must be an array, but found ${typeof arg}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** @hidden */
|
|
113
|
+
function checkIsObject(arg: any, msg: string) {
|
|
114
|
+
if (typeof arg !== "object") {
|
|
115
|
+
throw new TypeError(`${msg} must be an object, but found ${typeof arg}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @hidden */
|
|
120
|
+
function checkIsUint8Array(arg: any, msg: string) {
|
|
121
|
+
if (!(arg instanceof Uint8Array)) {
|
|
122
|
+
throw new TypeError(`${msg} must be a Uint8Array, buf found ${typeof arg}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** @hidden */
|
|
127
|
+
function checkIsOptions(arg: any, options: any[], msg: string) {
|
|
128
|
+
if (!options.includes(arg)) {
|
|
129
|
+
throw new RangeError(`${msg} is an invalid option: ${arg}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** @hidden */
|
|
134
|
+
function boundByIntegers(arg: any, bound: [number, number]) {
|
|
135
|
+
if (arg < bound[0]) {
|
|
136
|
+
return bound[0];
|
|
137
|
+
}
|
|
138
|
+
if (arg > bound[1]) {
|
|
139
|
+
return bound[1];
|
|
140
|
+
}
|
|
141
|
+
return arg;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** @hidden */
|
|
145
|
+
function setDefaultFields(arg: object, defaults: object) {
|
|
146
|
+
for (const [key, val] of Object.entries(defaults)) {
|
|
147
|
+
if (!Object.hasOwn(arg, key)) {
|
|
148
|
+
Object.defineProperty(arg, key, { value: val, writable: true });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The `Rsvim.buf` global object for Vim buffers.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```javascript
|
|
158
|
+
* // Create a alias to 'Rsvim.buf'.
|
|
159
|
+
* const buf = Rsvim.buf;
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @category Editor APIs
|
|
163
|
+
*/
|
|
164
|
+
export namespace RsvimBuf {
|
|
165
|
+
/**
|
|
166
|
+
* Get current buffer's ID.
|
|
167
|
+
*
|
|
168
|
+
* The "current" buffer is the buffer that the window where your cursor is
|
|
169
|
+
* located is binded to.
|
|
170
|
+
*
|
|
171
|
+
* :::warning
|
|
172
|
+
* When the editor is not initialized, i.e. there's no buffer/window created. It
|
|
173
|
+
* will return `undefined`. Once the editor is initialized, there will always have a
|
|
174
|
+
* valid buffer binded to the "current" window (where your cursor is). It will return
|
|
175
|
+
* the valid buffer ID.
|
|
176
|
+
* :::
|
|
177
|
+
*
|
|
178
|
+
* @returns {(number | undefined)} It returns a valid buffer ID if the editor is initialized.
|
|
179
|
+
* Otherwise it returns `undefined` if the editor is not initialized.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```javascript
|
|
183
|
+
* const bufId = Rsvim.buf.current();
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export function current(): number | undefined {
|
|
187
|
+
// @ts-ignore Ignore warning
|
|
188
|
+
return __InternalRsvimGlobalObject.buf_current();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* List all buffers' IDs.
|
|
193
|
+
*
|
|
194
|
+
* :::warning
|
|
195
|
+
* When the editor is not initialized, i.e. there's no buffer/window created. It
|
|
196
|
+
* will return an empty array. Once the editor is initialized, there will have at least 1
|
|
197
|
+
* buffer binded to the "current" window (where your cursor is). It will return all the
|
|
198
|
+
* buffer IDs as an array.
|
|
199
|
+
* :::
|
|
200
|
+
*
|
|
201
|
+
* @returns {number[]} All the buffers' IDs as an array. If there's no
|
|
202
|
+
* buffer (i.e. the editor is not initialized), it returns an empty array.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```javascript
|
|
206
|
+
* const bufIds = Rsvim.buf.list();
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export function list(): number[] {
|
|
210
|
+
// @ts-ignore Ignore warning
|
|
211
|
+
return __InternalRsvimGlobalObject.buf_list();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Write (save) buffer's text contents to local filesystem synchronizely.
|
|
216
|
+
*
|
|
217
|
+
* @param {number} bufId - The buffer's ID that you want to write to filesystem.
|
|
218
|
+
*
|
|
219
|
+
* @returns {number} It returns a positive integer to indicate how many bytes
|
|
220
|
+
* have been written to the file, if written successfully.
|
|
221
|
+
*
|
|
222
|
+
* @throws Throws {@link !TypeError} if the parameter is invalid, or {@link !Error} if failed to write buffer to file system.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```javascript
|
|
226
|
+
* const bufId = Rsvim.buf.currentBufferId();
|
|
227
|
+
* try {
|
|
228
|
+
* const bytes = Rsvim.buf.writeSync(bufId);
|
|
229
|
+
* Rsvim.cmd.echo(`Buffer ${bufId} has been saved, ${bytes} bytes written`);
|
|
230
|
+
* } catch (e) {
|
|
231
|
+
* Rsvim.cmd.echo(`Error: failed to save buffer ${bufId}, exception: ${e}`);
|
|
232
|
+
* }
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export function writeSync(bufId: number): number {
|
|
236
|
+
checkIsInteger(bufId, `"Rsvim.buf.writeSync" bufId`);
|
|
237
|
+
|
|
238
|
+
// @ts-ignore Ignore warning
|
|
239
|
+
return __InternalRsvimGlobalObject.buf_write_sync(bufId);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The `Rsvim.cmd` global object for Ex commands.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```javascript
|
|
248
|
+
* // Create a alias to 'Rsvim.cmd'.
|
|
249
|
+
* const cmd = Rsvim.cmd;
|
|
250
|
+
* ```
|
|
251
|
+
*
|
|
252
|
+
* @category Editor APIs
|
|
253
|
+
*/
|
|
254
|
+
export namespace RsvimCmd {
|
|
255
|
+
/**
|
|
256
|
+
* Create a ex command with a callback function.
|
|
257
|
+
*
|
|
258
|
+
* :::warning
|
|
259
|
+
* The builtin command `js` cannot be override.
|
|
260
|
+
* :::
|
|
261
|
+
*
|
|
262
|
+
* @param {string} name - Command name that is going to create. Only letters (`a-z` and `A-Z`), digits (`0-9`), underscore (`_`) and exclamation (`!`) are allowed in a command name. Command name must not begin with a digit.
|
|
263
|
+
* @param {RsvimCmd.CommandCallback} callback - Async callback function that implements the command. It accepts an `ctx` parameter that contains all the information when user is running it. See {@link RsvimCmd.CommandCallback}.
|
|
264
|
+
* @param {RsvimCmd.CommandAttributes} attributes - (Optional) Attributes that control the command behavior, by default is `{bang:false, nargs:"0"}`, see {@link RsvimCmd.CommandAttributes}.
|
|
265
|
+
* @param {RsvimCmd.CommandOptions} options - (Optional) Options that control how the command is created, by default is `{force:true}`, see {@link RsvimCmd.CommandOptions}.
|
|
266
|
+
* @returns {(RsvimCmd.CommandDefinition | undefined)} It returns `undefined` is the command is newly created. Or it returns a command definition that was defined previously.
|
|
267
|
+
*
|
|
268
|
+
* @throws Throws {@link !TypeError} if any parameters are invalid. Or throws {@link Error} if command name or alias already exists, but `force` option is not set to override existing command forcibly.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```javascript
|
|
272
|
+
* async function write(ctx: RsvimCmd.CommandContext): void {
|
|
273
|
+
* try {
|
|
274
|
+
* const bytes = Rsvim.buf.writeSync(ctx.currentBufferId);
|
|
275
|
+
*
|
|
276
|
+
* // Call other async APIs
|
|
277
|
+
* const file = await Rsvim.fs.open("message.txt");
|
|
278
|
+
* const buffer = new Uint8Array(100);
|
|
279
|
+
* const read = await file.read(buffer);
|
|
280
|
+
* const message = new TextDecoder().decode(buffer);
|
|
281
|
+
*
|
|
282
|
+
* Rsvim.cmd.echo(`Buffer ${bufId} has been saved, ${bytes} bytes written with message: ${message}`);
|
|
283
|
+
* } catch (e) {
|
|
284
|
+
* Rsvim.cmd.echo(`Error: failed to save buffer ${bufId}, exception: ${e}`);
|
|
285
|
+
* }
|
|
286
|
+
* }
|
|
287
|
+
* Rsvim.cmd.create("write", write);
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
export function create(
|
|
291
|
+
name: string,
|
|
292
|
+
callback: RsvimCmd.CommandCallback,
|
|
293
|
+
attributes?: RsvimCmd.CommandAttributes,
|
|
294
|
+
options?: RsvimCmd.CommandOptions,
|
|
295
|
+
): RsvimCmd.CommandDefinition | undefined {
|
|
296
|
+
checkMatchPattern(
|
|
297
|
+
name,
|
|
298
|
+
/^[A-Za-z_!][A-Za-z0-9_!]*$/,
|
|
299
|
+
`"Rsvim.cmd.create" name`,
|
|
300
|
+
);
|
|
301
|
+
checkIsFunction(callback, `"Rsvim.cmd.create" callback`);
|
|
302
|
+
|
|
303
|
+
attributes = attributes ?? { bang: false, nargs: "0" };
|
|
304
|
+
checkIsObject(attributes, `"Rsvim.cmd.create" attributes`);
|
|
305
|
+
setDefaultFields(attributes, { bang: false, nargs: "0" });
|
|
306
|
+
checkIsBoolean(attributes.bang, `"Rsvim.cmd.create" bang attribute`);
|
|
307
|
+
checkIsOptions(
|
|
308
|
+
attributes.nargs,
|
|
309
|
+
["0", "1", "?", "+", "*"],
|
|
310
|
+
`"Rsvim.cmd.create" nargs attribute`,
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
options = options ?? { force: true };
|
|
314
|
+
checkIsObject(options, `"Rsvim.cmd.create" options`);
|
|
315
|
+
setDefaultFields(options, { force: true });
|
|
316
|
+
checkIsBoolean(options.force, `"Rsvim.cmd.create" force option`);
|
|
317
|
+
checkOptionalString(options.alias, `"Rsvim.cmd.create" alias option`);
|
|
318
|
+
|
|
319
|
+
// @ts-ignore Ignore warning
|
|
320
|
+
return __InternalRsvimGlobalObject.cmd_create(
|
|
321
|
+
name,
|
|
322
|
+
callback,
|
|
323
|
+
attributes,
|
|
324
|
+
options,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Echo message to the command-line.
|
|
330
|
+
*
|
|
331
|
+
* @param {any} message - It accepts string and other primitive types, except `null` and `undefined`.
|
|
332
|
+
*
|
|
333
|
+
* @throws Throws {@link !TypeError} if the parameter is `null` or `undefined` or no parameter provided.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```javascript
|
|
337
|
+
* Rsvim.cmd.echo("Hello Rsvim!");
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
export function echo(message: any): void {
|
|
341
|
+
checkNotNull(message, `"Rsvim.cmd.echo" message`);
|
|
342
|
+
|
|
343
|
+
// @ts-ignore Ignore warning
|
|
344
|
+
__InternalRsvimGlobalObject.cmd_echo(message);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* List all registered ex command names.
|
|
349
|
+
*
|
|
350
|
+
* :::warning
|
|
351
|
+
* The builtin `js` command will not be listed here.
|
|
352
|
+
* :::
|
|
353
|
+
*
|
|
354
|
+
* @returns {string[]} Returns all registered ex command names, except the `js` command.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```javascript
|
|
358
|
+
* Rsvim.cmd.list().forEach((name) => {
|
|
359
|
+
* Rsvim.cmd.echo(`Command: ${name}`);
|
|
360
|
+
* });
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
export function list(): string[] {
|
|
364
|
+
// @ts-ignore Ignore warning
|
|
365
|
+
return __InternalRsvimGlobalObject.cmd_list();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Get ex command definition by name.
|
|
370
|
+
*
|
|
371
|
+
* :::warning
|
|
372
|
+
* The builtin `js` command cannot be get.
|
|
373
|
+
* :::
|
|
374
|
+
*
|
|
375
|
+
* @returns {(RsvimCmd.CommandDefinition | undefined)} Returns command definition by its name, except the `js` command.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```javascript
|
|
379
|
+
* const def = Rsvim.cmd.get("write");
|
|
380
|
+
* Rsvim.cmd.echo(`Command: ${def.name}`);
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
export function get(name: string): RsvimCmd.CommandDefinition | undefined {
|
|
384
|
+
checkIsString(name, `"Rsvim.cmd.get" name`);
|
|
385
|
+
|
|
386
|
+
// @ts-ignore Ignore warning
|
|
387
|
+
return __InternalRsvimGlobalObject.cmd_get(name);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Remove an ex command by name.
|
|
392
|
+
*
|
|
393
|
+
* :::warning
|
|
394
|
+
* The builtin command `js` cannot be removed.
|
|
395
|
+
* :::
|
|
396
|
+
*
|
|
397
|
+
* @param {string} name - The command name to be removed.
|
|
398
|
+
* @returns {(RsvimCmd.CommandDefinition | undefined)} Returns the removed {@link RsvimCmd.CommandDefinition}, or `undefined` if no command is been removed.
|
|
399
|
+
*
|
|
400
|
+
* @throws Throws {@link !TypeError} if name is not a string.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```javascript
|
|
404
|
+
* Rsvim.cmd.list().forEach((cmd) => {
|
|
405
|
+
* // Remove all registered commands.
|
|
406
|
+
* Rsvim.cmd.remove(cmd.name);
|
|
407
|
+
* });
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
export function remove(name: string): RsvimCmd.CommandDefinition | undefined {
|
|
411
|
+
checkIsString(name, `"Rsvim.cmd.remove" name`);
|
|
412
|
+
|
|
413
|
+
// @ts-ignore Ignore warning
|
|
414
|
+
return __InternalRsvimGlobalObject.cmd_remove(name);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Command attributes.
|
|
419
|
+
*
|
|
420
|
+
* @see {@link RsvimCmd.create}
|
|
421
|
+
*/
|
|
422
|
+
export type CommandAttributes = {
|
|
423
|
+
/**
|
|
424
|
+
* Whether the command can take a `!` modifier, for example: `:w!`, `:qall!`.
|
|
425
|
+
*
|
|
426
|
+
* @defaultValue `false`
|
|
427
|
+
, */
|
|
428
|
+
bang?: boolean;
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Whether The command can take any arguments, and how many it can take:
|
|
432
|
+
*
|
|
433
|
+
* - `0`: No arguments are allowed.
|
|
434
|
+
* - `1`: Exactly 1 argument is required.
|
|
435
|
+
* - `*`: Any number of arguments are allowed, i.e. 0, 1 or more.
|
|
436
|
+
* - `?`: 0 or 1 arguments are allowed.
|
|
437
|
+
* - `+`: At least 1 arguments are required.
|
|
438
|
+
*
|
|
439
|
+
* @defaultValue `0`
|
|
440
|
+
, */
|
|
441
|
+
nargs?: "0" | "1" | "*" | "+" | "?";
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Command options when creating a command.
|
|
446
|
+
*
|
|
447
|
+
* @see {@link RsvimCmd.create}
|
|
448
|
+
*/
|
|
449
|
+
export type CommandOptions = {
|
|
450
|
+
/**
|
|
451
|
+
* Whether force override the command if there's already an existing one.
|
|
452
|
+
*
|
|
453
|
+
* @defaultValue `true`
|
|
454
|
+
*/
|
|
455
|
+
force?: boolean;
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Command alias, i.e. short name.
|
|
459
|
+
*
|
|
460
|
+
* For example, the `w` is alias for `write`.
|
|
461
|
+
*
|
|
462
|
+
* @defaultValue `undefined`
|
|
463
|
+
*/
|
|
464
|
+
alias?: string;
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Command callback function, this is the backend logic that implements a user ex command.
|
|
469
|
+
*
|
|
470
|
+
* It accepts a `ctx` parameter that indicates runtime information when the command is executed.
|
|
471
|
+
*
|
|
472
|
+
* @see {@link RsvimCmd.create}
|
|
473
|
+
* @see {@link CommandContext}
|
|
474
|
+
, */
|
|
475
|
+
export type CommandCallback = (ctx: CommandContext) => Promise<void>;
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Command definition.
|
|
479
|
+
*/
|
|
480
|
+
export type CommandDefinition = {
|
|
481
|
+
name: string;
|
|
482
|
+
callback: CommandCallback;
|
|
483
|
+
attributes: CommandAttributes;
|
|
484
|
+
options: CommandOptions;
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Command runtime context.
|
|
489
|
+
*
|
|
490
|
+
* When a command is been execute, runtime information will be passed to the command callback function.
|
|
491
|
+
*/
|
|
492
|
+
export type CommandContext = {
|
|
493
|
+
/**
|
|
494
|
+
* Whether the command is executed with a bang "!".
|
|
495
|
+
*/
|
|
496
|
+
bang: boolean;
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Arguments that are passed to the command when executed.
|
|
500
|
+
*/
|
|
501
|
+
args: string[];
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Current buffer ID when the command is executed.
|
|
505
|
+
*/
|
|
506
|
+
currentBufferId: number;
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Current window ID when the command is executed.
|
|
510
|
+
*/
|
|
511
|
+
currentWindowId: number;
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* The `Rsvim.fs` global object for file system and file IO.
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* ```javascript
|
|
520
|
+
* // Create a alias to 'Rsvim.fs'.
|
|
521
|
+
* const fs = Rsvim.fs;
|
|
522
|
+
* ```
|
|
523
|
+
*
|
|
524
|
+
* @category General APIs
|
|
525
|
+
*/
|
|
526
|
+
export namespace RsvimFs {
|
|
527
|
+
/**
|
|
528
|
+
* Open a file and resolve to an instance of {@link RsvimFs.File}. The file does not need to previously exist if using the `create` or `createNew` open options.
|
|
529
|
+
* The caller have to close the file to prevent resource leaking, see {@link RsvimFs.File.close}.
|
|
530
|
+
*
|
|
531
|
+
* @param {string} path - File path.
|
|
532
|
+
* @param {RsvimFs.OpenOptions} options - (Optional) Open options, by default is `{read: true}`. See {@link RsvimFs.OpenOptions}.
|
|
533
|
+
* @returns {Promise<RsvimFs.File>} It resolves to an instance of {@link RsvimFs.File}.
|
|
534
|
+
*
|
|
535
|
+
* @throws Throws {@link !TypeError} if any parameters are invalid. Or throws {@link Error} if failed to open/create the file.
|
|
536
|
+
*
|
|
537
|
+
* @example
|
|
538
|
+
* ```javascript
|
|
539
|
+
* const file = await Rsvim.fs.open("README.md");
|
|
540
|
+
* ```
|
|
541
|
+
*/
|
|
542
|
+
export async function open(
|
|
543
|
+
path: string,
|
|
544
|
+
options?: RsvimFs.OpenOptions,
|
|
545
|
+
): Promise<RsvimFs.File> {
|
|
546
|
+
checkIsString(path, `"Rsvim.fs.open" path`);
|
|
547
|
+
|
|
548
|
+
options = options ?? { read: true };
|
|
549
|
+
checkIsObject(options, `"Rsvim.fs.open" options`);
|
|
550
|
+
setDefaultFields(options, {
|
|
551
|
+
append: false,
|
|
552
|
+
create: false,
|
|
553
|
+
createNew: false,
|
|
554
|
+
read: false,
|
|
555
|
+
truncate: false,
|
|
556
|
+
write: false,
|
|
557
|
+
});
|
|
558
|
+
checkIsBoolean(options.append, `"Rsvim.fs.open" append option`);
|
|
559
|
+
checkIsBoolean(options.create, `"Rsvim.fs.open" create option`);
|
|
560
|
+
checkIsBoolean(options.createNew, `"Rsvim.fs.open" createNew option`);
|
|
561
|
+
checkIsBoolean(options.read, `"Rsvim.fs.open" read option`);
|
|
562
|
+
checkIsBoolean(options.truncate, `"Rsvim.fs.open" truncate option`);
|
|
563
|
+
checkIsBoolean(options.write, `"Rsvim.fs.open" write option`);
|
|
564
|
+
|
|
565
|
+
// @ts-ignore Ignore warning
|
|
566
|
+
const handle = await __InternalRsvimGlobalObject.fs_open(path, options);
|
|
567
|
+
return new RsvimFs.File(handle);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* The sync version of {@link open}.
|
|
572
|
+
*
|
|
573
|
+
* @param {string} path
|
|
574
|
+
* @param {RsvimFs.OpenOptions} options
|
|
575
|
+
* @returns {RsvimFs.File}
|
|
576
|
+
*
|
|
577
|
+
* @throws
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```javascript
|
|
581
|
+
* const file = Rsvim.fs.openSync("README.md");
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
export function openSync(
|
|
585
|
+
path: string,
|
|
586
|
+
options?: RsvimFs.OpenOptions,
|
|
587
|
+
): RsvimFs.File {
|
|
588
|
+
checkIsString(path, `"Rsvim.fs.openSync" path`);
|
|
589
|
+
|
|
590
|
+
options = options ?? { read: true };
|
|
591
|
+
checkIsObject(options, `"Rsvim.fs.open" options`);
|
|
592
|
+
setDefaultFields(options, {
|
|
593
|
+
append: false,
|
|
594
|
+
create: false,
|
|
595
|
+
createNew: false,
|
|
596
|
+
read: false,
|
|
597
|
+
truncate: false,
|
|
598
|
+
write: false,
|
|
599
|
+
});
|
|
600
|
+
checkIsBoolean(options.append, `"Rsvim.fs.open" append option`);
|
|
601
|
+
checkIsBoolean(options.create, `"Rsvim.fs.open" create option`);
|
|
602
|
+
checkIsBoolean(options.createNew, `"Rsvim.fs.open" createNew option`);
|
|
603
|
+
checkIsBoolean(options.read, `"Rsvim.fs.open" read option`);
|
|
604
|
+
checkIsBoolean(options.truncate, `"Rsvim.fs.open" truncate option`);
|
|
605
|
+
checkIsBoolean(options.write, `"Rsvim.fs.open" write option`);
|
|
606
|
+
|
|
607
|
+
// @ts-ignore Ignore warning
|
|
608
|
+
const handle = __InternalRsvimGlobalObject.fs_open_sync(path, options);
|
|
609
|
+
return new RsvimFs.File(handle);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Read a file in binary mode, i.e. into an array of bytes buffer, without open/close a file descriptor/handle.
|
|
614
|
+
*
|
|
615
|
+
* @param {string} path - File path to read.
|
|
616
|
+
* @returns {Promise<Uint8Array>} It resolves to {@link !Uint8Array} that contains all the file contents as bytes array.
|
|
617
|
+
*
|
|
618
|
+
* @throws Throws {@link !TypeError} if the file name is invalid. Or throws {@link Error} if failed to read the file.
|
|
619
|
+
*
|
|
620
|
+
* @example
|
|
621
|
+
* ```javascript
|
|
622
|
+
* const buffer = await Rsvim.fs.readFile("README.md");
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
export async function readFile(path: string): Promise<Uint8Array> {
|
|
626
|
+
checkIsString(path, `"Rsvim.fs.readFile" path`);
|
|
627
|
+
|
|
628
|
+
// @ts-ignore Ignore warning
|
|
629
|
+
return await __InternalRsvimGlobalObject.fs_read_file(path);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* The sync version of {@link readFile}.
|
|
634
|
+
*
|
|
635
|
+
* @param {string} path
|
|
636
|
+
* @returns {Uint8Array}
|
|
637
|
+
*
|
|
638
|
+
* @throws
|
|
639
|
+
*
|
|
640
|
+
* @example
|
|
641
|
+
* ```javascript
|
|
642
|
+
* const buffer = Rsvim.fs.readFileSync("README.md");
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
645
|
+
export function readFileSync(path: string): Uint8Array {
|
|
646
|
+
checkIsString(path, `"Rsvim.fs.readFileSync" path`);
|
|
647
|
+
|
|
648
|
+
// @ts-ignore Ignore warning
|
|
649
|
+
return __InternalRsvimGlobalObject.fs_read_file_sync(path);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Read a file in text mode, i.e. into a string, without open/close a file descriptor/handle.
|
|
654
|
+
*
|
|
655
|
+
* @param {string} path - File path to read.
|
|
656
|
+
* @returns {Promise<string>} It resolves to text string that contains all the file contents.
|
|
657
|
+
*
|
|
658
|
+
* @throws Throws {@link !TypeError} if the file name is invalid. Or throws {@link Error} if failed to read the file.
|
|
659
|
+
*
|
|
660
|
+
* @example
|
|
661
|
+
* ```javascript
|
|
662
|
+
* const payload = await Rsvim.fs.readTextFile("README.md");
|
|
663
|
+
* ```
|
|
664
|
+
*/
|
|
665
|
+
export async function readTextFile(path: string): Promise<string> {
|
|
666
|
+
checkIsString(path, `"Rsvim.fs.readTextFile" path`);
|
|
667
|
+
|
|
668
|
+
// @ts-ignore Ignore warning
|
|
669
|
+
return await __InternalRsvimGlobalObject.fs_read_text_file(path);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* The sync version of {@link readTextFile}.
|
|
674
|
+
*
|
|
675
|
+
* @param {string} path
|
|
676
|
+
* @returns {string}
|
|
677
|
+
*
|
|
678
|
+
* @throws
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* ```javascript
|
|
682
|
+
* const payload = Rsvim.fs.readTextFileSync("README.md");
|
|
683
|
+
* ```
|
|
684
|
+
*/
|
|
685
|
+
export function readTextFileSync(path: string): string {
|
|
686
|
+
checkIsString(path, `"Rsvim.fs.readTextFileSync" path`);
|
|
687
|
+
|
|
688
|
+
// @ts-ignore Ignore warning
|
|
689
|
+
return __InternalRsvimGlobalObject.fs_read_text_file_sync(path);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Open options.
|
|
694
|
+
*
|
|
695
|
+
* :::tip
|
|
696
|
+
* It is same with [std::fs::OpenOptions](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html) in rust std library.
|
|
697
|
+
* :::
|
|
698
|
+
*
|
|
699
|
+
* @see {@link RsvimFs.open}
|
|
700
|
+
*/
|
|
701
|
+
export type OpenOptions = {
|
|
702
|
+
/**
|
|
703
|
+
* Set the file for append mode.
|
|
704
|
+
*
|
|
705
|
+
* @defaultValue `false`
|
|
706
|
+
, */
|
|
707
|
+
append?: boolean;
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Create a new file or open it if it already exists.
|
|
711
|
+
*
|
|
712
|
+
* In order for the file to be created, `write` or `append` access must be used.
|
|
713
|
+
*
|
|
714
|
+
* @defaultValue `false`
|
|
715
|
+
, */
|
|
716
|
+
create?: boolean;
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Create a new file, failing if it already exists.
|
|
720
|
+
*
|
|
721
|
+
* If this option is set, `create` and `truncate` options are ignored.
|
|
722
|
+
*
|
|
723
|
+
* @defaultValue `false`
|
|
724
|
+
, */
|
|
725
|
+
createNew?: boolean;
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Set the file for read access.
|
|
729
|
+
*
|
|
730
|
+
* @defaultValue `false`
|
|
731
|
+
, */
|
|
732
|
+
read?: boolean;
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Open the file and truncate the file to `0` length if it already exists.
|
|
736
|
+
*
|
|
737
|
+
* @defaultValue `false`
|
|
738
|
+
, */
|
|
739
|
+
truncate?: boolean;
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Set the file for write access. If the file already exists, any "write" calls on it will
|
|
743
|
+
* overwrite the contents, without truncating it.
|
|
744
|
+
*
|
|
745
|
+
* @defaultValue `false`
|
|
746
|
+
, */
|
|
747
|
+
write?: boolean;
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* The File object that access to an open file on filesystem.
|
|
752
|
+
*
|
|
753
|
+
* @see {@link RsvimFs.open}
|
|
754
|
+
*/
|
|
755
|
+
export class File {
|
|
756
|
+
/** @hidden */
|
|
757
|
+
#rid: number | null | undefined;
|
|
758
|
+
|
|
759
|
+
/** @hidden */
|
|
760
|
+
constructor(rid: number) {
|
|
761
|
+
this.#rid = rid;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Close the file.
|
|
766
|
+
*
|
|
767
|
+
* @throws Throws {@link !Error} if the file is already been closed.
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* ```javascript
|
|
771
|
+
* const file = await Rsvim.fs.open("README.md");
|
|
772
|
+
* // do work with the `file` object
|
|
773
|
+
* file.close();
|
|
774
|
+
*
|
|
775
|
+
* // Or
|
|
776
|
+
*
|
|
777
|
+
* using file = await Rsvim.fs.open("README.md");
|
|
778
|
+
* // do work with the `file` object
|
|
779
|
+
* ```
|
|
780
|
+
*/
|
|
781
|
+
close(): void {
|
|
782
|
+
assertFalse(isNull(this.#rid), "RsvimFs.File is already closed!");
|
|
783
|
+
// @ts-ignore Ignore warning
|
|
784
|
+
__InternalRsvimGlobalObject.fs_close(this.#rid);
|
|
785
|
+
this.#rid = null;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Close the file with `using` API instead of `close`.
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* ```javascript
|
|
793
|
+
* using file = await Rsvim.fs.open("README.md");
|
|
794
|
+
* // do work with the `file` object
|
|
795
|
+
* ```
|
|
796
|
+
*
|
|
797
|
+
* @see {@link close}
|
|
798
|
+
*/
|
|
799
|
+
[Symbol.dispose](): void {
|
|
800
|
+
this.close();
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/**
|
|
804
|
+
* File is already been closed.
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```javascript
|
|
808
|
+
* const file = await Rsvim.fs.open("README.md");
|
|
809
|
+
* if (!file.isDisposed()) {
|
|
810
|
+
* file.close();
|
|
811
|
+
* }
|
|
812
|
+
* ```
|
|
813
|
+
*/
|
|
814
|
+
get isDisposed(): boolean {
|
|
815
|
+
return isNull(this.#rid);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Read a file into a buffer.
|
|
820
|
+
*
|
|
821
|
+
* :::warning
|
|
822
|
+
* It is not guaranteed that the full buffer will be read in a single call.
|
|
823
|
+
* :::
|
|
824
|
+
*
|
|
825
|
+
* @param {Uint8Array} buf - Read bytes into buffer.
|
|
826
|
+
* @returns {Promise<number>} It resolves to either the number of bytes read during the operation or `0`(EOF) if there was no more to read.
|
|
827
|
+
*
|
|
828
|
+
* @throws Throws {@link !TypeError} if buf is not a Uint8Array.
|
|
829
|
+
*
|
|
830
|
+
* @example
|
|
831
|
+
* ```javascript
|
|
832
|
+
* using file = await Rsvim.fs.open("README.md");
|
|
833
|
+
* const buf = new Uint8Array(100);
|
|
834
|
+
* const n = await file.read(buf); // read 11 bytes
|
|
835
|
+
* const text = new TextDecoder().decode(buf); // decode into UTF-8 string "hello world"
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
838
|
+
async read(buf: Uint8Array): Promise<number> {
|
|
839
|
+
checkIsUint8Array(buf, `"RsvimFs.File.read" buf`);
|
|
840
|
+
|
|
841
|
+
// @ts-ignore Ignore warning
|
|
842
|
+
const n = await __InternalRsvimGlobalObject.fs_read(
|
|
843
|
+
this.#rid,
|
|
844
|
+
buf.buffer,
|
|
845
|
+
);
|
|
846
|
+
return n;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* The sync version of {@link read}.
|
|
851
|
+
*
|
|
852
|
+
* @param {Uint8Array} buf
|
|
853
|
+
* @returns {number}
|
|
854
|
+
*
|
|
855
|
+
* @throws
|
|
856
|
+
*
|
|
857
|
+
* @example
|
|
858
|
+
* ```javascript
|
|
859
|
+
* using file = await Rsvim.fs.open("README.md");
|
|
860
|
+
* const buf = new Uint8Array(100);
|
|
861
|
+
* const n = file.readSync(buf); // read 11 bytes
|
|
862
|
+
* const text = new TextDecoder().decode(buf); // decode into UTF-8 string "hello world"
|
|
863
|
+
* ```
|
|
864
|
+
*/
|
|
865
|
+
readSync(buf: Uint8Array): number {
|
|
866
|
+
checkIsUint8Array(buf, `"RsvimFs.File.readSync" buf`);
|
|
867
|
+
|
|
868
|
+
// @ts-ignore Ignore warning
|
|
869
|
+
return __InternalRsvimGlobalObject.fs_read_sync(this.#rid, buf.buffer);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* Write a buffer into a file.
|
|
874
|
+
*
|
|
875
|
+
* :::warning
|
|
876
|
+
* It is not guaranteed that the full buffer will be written in a single call.
|
|
877
|
+
* :::
|
|
878
|
+
*
|
|
879
|
+
* @param {Uint8Array} buf - Read bytes into buffer.
|
|
880
|
+
* @returns {Promise<number>} It resolves to either the number of bytes written during the operation or `0` if there was nothing to write.
|
|
881
|
+
*
|
|
882
|
+
* @throws Throws {@link !TypeError} if buf is not a Uint8Array.
|
|
883
|
+
*
|
|
884
|
+
* @example
|
|
885
|
+
* ```javascript
|
|
886
|
+
* using file = await Rsvim.fs.open("README.md", {write:true,create:true});
|
|
887
|
+
* const buf = new TextEncoder().encode("hello world");
|
|
888
|
+
* const n = await file.write(buf); // write 11 bytes
|
|
889
|
+
* ```
|
|
890
|
+
*/
|
|
891
|
+
async write(buf: Uint8Array): Promise<number> {
|
|
892
|
+
checkIsUint8Array(buf, `"RsvimFs.File.write" buf`);
|
|
893
|
+
|
|
894
|
+
// @ts-ignore Ignore warning
|
|
895
|
+
const n = await __InternalRsvimGlobalObject.fs_write(
|
|
896
|
+
this.#rid,
|
|
897
|
+
buf.buffer,
|
|
898
|
+
);
|
|
899
|
+
return n;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* The sync version of {@link write}.
|
|
904
|
+
*
|
|
905
|
+
* @param {Uint8Array} buf
|
|
906
|
+
* @returns {number}
|
|
907
|
+
*
|
|
908
|
+
* @throws
|
|
909
|
+
*
|
|
910
|
+
* @example
|
|
911
|
+
* ```javascript
|
|
912
|
+
* using file = await Rsvim.fs.open("README.md", {write:true,create:true});
|
|
913
|
+
* const buf = new TextEncoder().encode("hello world");
|
|
914
|
+
* const n = file.writeSync(buf); // write 11 bytes
|
|
915
|
+
* ```
|
|
916
|
+
*/
|
|
917
|
+
writeSync(buf: Uint8Array): number {
|
|
918
|
+
checkIsUint8Array(buf, `"RsvimFs.File.writeSync" buf`);
|
|
919
|
+
|
|
920
|
+
// @ts-ignore Ignore warning
|
|
921
|
+
return __InternalRsvimGlobalObject.fs_write_sync(this.#rid, buf.buffer);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
export namespace RsvimOpt {
|
|
927
|
+
/**
|
|
928
|
+
* @see {@link RsvimOpt.fileEncoding}
|
|
929
|
+
* @inline
|
|
930
|
+
* */
|
|
931
|
+
export type FileEncodingOption = "utf-8";
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* @see {@link RsvimOpt.fileFormat}
|
|
935
|
+
* @inline
|
|
936
|
+
*/
|
|
937
|
+
export type FileFormatOption = "dos" | "unix" | "mac";
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* The `Rsvim.opt` global object for global editor options. These options will change the editor's behavior
|
|
942
|
+
* and suit user's personal habits.
|
|
943
|
+
*
|
|
944
|
+
* There are 3 kinds of editor option:
|
|
945
|
+
* - Global options: Options that are global that you use one value for all Rsvim component instances such
|
|
946
|
+
* as buffer, window, statusline, etc. When you change the option, it will take effect immediately and
|
|
947
|
+
* affect all existing instances.
|
|
948
|
+
* - Local options: Options that only apply to one component instance, each instance has its own copy of
|
|
949
|
+
* this option, thus each can have its own value. This allow you to set an option in one instance, without
|
|
950
|
+
* modifying other instances.
|
|
951
|
+
* - Global local options: Options that are global, and will be copy to a newly created Rsvim component
|
|
952
|
+
* instance. A global-local-option always has its corresponding local-option. When you change the option,
|
|
953
|
+
* it only will apply to the newly created instances, but cannot modify existing instances.
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* ```javascript
|
|
957
|
+
* // Create a alias to 'Rsvim.opt'.
|
|
958
|
+
* const opt = Rsvim.opt;
|
|
959
|
+
* ```
|
|
960
|
+
*
|
|
961
|
+
* @category Editor APIs
|
|
962
|
+
* @hideconstructor
|
|
963
|
+
*/
|
|
964
|
+
export class RsvimOpt {
|
|
965
|
+
/**
|
|
966
|
+
* Get the _expand-tab_ option. Local to buffer.
|
|
967
|
+
*
|
|
968
|
+
* When in insert mode, inserts [spaces](https://en.wikipedia.org/wiki/Whitespace_character) (ASCII `32`)
|
|
969
|
+
* instead of a [horizontal tab](https://en.wikipedia.org/wiki/Tab_key) (ASCII `9`).
|
|
970
|
+
*
|
|
971
|
+
* See {@link shiftWidth} to get the number of spaces when inserting.
|
|
972
|
+
*
|
|
973
|
+
* @returns {boolean}
|
|
974
|
+
*
|
|
975
|
+
* @defaultValue `false`
|
|
976
|
+
*
|
|
977
|
+
* @example
|
|
978
|
+
* ```javascript
|
|
979
|
+
* // Get the 'expand-tab' option.
|
|
980
|
+
* const value = Rsvim.opt.expandTab;
|
|
981
|
+
* ```
|
|
982
|
+
*/
|
|
983
|
+
get expandTab(): boolean {
|
|
984
|
+
// @ts-ignore Ignore warning
|
|
985
|
+
return __InternalRsvimGlobalObject.opt_get_expand_tab();
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* Set the _expand-tab_ option.
|
|
990
|
+
*
|
|
991
|
+
* @param {boolean} value - The _expand-tab_ option.
|
|
992
|
+
* @throws Throws {@link !TypeError} if value is not a boolean.
|
|
993
|
+
*
|
|
994
|
+
* @example
|
|
995
|
+
* ```javascript
|
|
996
|
+
* // Set the 'expand-tab' option.
|
|
997
|
+
* Rsvim.opt.expandTab = true;
|
|
998
|
+
* ```
|
|
999
|
+
*/
|
|
1000
|
+
set expandTab(value: boolean) {
|
|
1001
|
+
checkIsBoolean(value, `"Rsvim.opt.expandTab" value`);
|
|
1002
|
+
// @ts-ignore Ignore warning
|
|
1003
|
+
__InternalRsvimGlobalObject.opt_set_expand_tab(value);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* Get the _file-encoding_ option. Local to buffer.
|
|
1008
|
+
*
|
|
1009
|
+
* Sets the [character encoding](https://en.wikipedia.org/wiki/Character_encoding) for the file of this buffer.
|
|
1010
|
+
* This will determine which character encoding is used when RSVIM read/write a file from file system.
|
|
1011
|
+
*
|
|
1012
|
+
* :::warning
|
|
1013
|
+
* For now, only **utf-8** encoding is supported.
|
|
1014
|
+
* :::
|
|
1015
|
+
*
|
|
1016
|
+
* @returns {RsvimOpt.FileEncodingOption}
|
|
1017
|
+
*
|
|
1018
|
+
* @defaultValue `"utf-8"`
|
|
1019
|
+
*
|
|
1020
|
+
* @example
|
|
1021
|
+
* ```javascript
|
|
1022
|
+
* // Get the 'file-encoding' option.
|
|
1023
|
+
* const value = Rsvim.opt.fileEncoding;
|
|
1024
|
+
* ```
|
|
1025
|
+
*/
|
|
1026
|
+
get fileEncoding(): RsvimOpt.FileEncodingOption {
|
|
1027
|
+
// @ts-ignore Ignore warning
|
|
1028
|
+
return __InternalRsvimGlobalObject.opt_get_file_encoding();
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/**
|
|
1032
|
+
* Set the _file-encoding_ option.
|
|
1033
|
+
*
|
|
1034
|
+
* @param {RsvimOpt.FileEncodingOption} value - The _file-encoding_ option.
|
|
1035
|
+
* @throws Throws {@link !RangeError} if value is an invalid option.
|
|
1036
|
+
*
|
|
1037
|
+
* @example
|
|
1038
|
+
* ```javascript
|
|
1039
|
+
* // Set the 'file-encoding' option.
|
|
1040
|
+
* Rsvim.opt.fileEncoding = "utf-8";
|
|
1041
|
+
* ```
|
|
1042
|
+
*/
|
|
1043
|
+
set fileEncoding(value: RsvimOpt.FileEncodingOption) {
|
|
1044
|
+
checkIsOptions(value, ["utf-8"], `"Rsvim.opt.fileEncoding" value`);
|
|
1045
|
+
// @ts-ignore Ignore warning
|
|
1046
|
+
__InternalRsvimGlobalObject.opt_set_file_encoding(value);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Get the _file-format_ option. Local to buffer.
|
|
1051
|
+
*
|
|
1052
|
+
* Sets the [line end](https://en.wikipedia.org/wiki/Newline) for the buffer. There are 3 kinds of line end:
|
|
1053
|
+
* - `CRLF`: used by [Windows](https://www.microsoft.com/windows).
|
|
1054
|
+
* - `LF`: used by [Linux](https://en.wikipedia.org/wiki/Linux) and [Unix](https://en.wikipedia.org/wiki/Unix) (include [MacOS](https://www.apple.com/macos/)).
|
|
1055
|
+
* - `CR`: used by [classic MacOS](https://en.wikipedia.org/wiki/Classic_Mac_OS).
|
|
1056
|
+
*
|
|
1057
|
+
* :::warning
|
|
1058
|
+
* Today's Mac also uses `LF` as line end, you should never use `CR` any more.
|
|
1059
|
+
* :::
|
|
1060
|
+
*
|
|
1061
|
+
* For this option, it has below choices:
|
|
1062
|
+
* - `"dos"`: equivalent to `CRLF` line end.
|
|
1063
|
+
* - `"unix"`: equivalent to `LF` line end.
|
|
1064
|
+
* - `"mac"`: equivalent to `CR` line end.
|
|
1065
|
+
*
|
|
1066
|
+
* @returns {RsvimOpt.FileFormatOption}
|
|
1067
|
+
*
|
|
1068
|
+
* @defaultValue `"dos"` for Windows/MS-DOS, `"unix"` for Linux/Unix/MacOS.
|
|
1069
|
+
*
|
|
1070
|
+
* @example
|
|
1071
|
+
* ```javascript
|
|
1072
|
+
* // Get the 'file-format' option.
|
|
1073
|
+
* const value = Rsvim.opt.fileFormat;
|
|
1074
|
+
* ```
|
|
1075
|
+
*/
|
|
1076
|
+
get fileFormat(): RsvimOpt.FileFormatOption {
|
|
1077
|
+
// @ts-ignore Ignore warning
|
|
1078
|
+
return __InternalRsvimGlobalObject.opt_get_file_format();
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
/**
|
|
1082
|
+
* Set the _file-format_ option.
|
|
1083
|
+
*
|
|
1084
|
+
* @param {RsvimOpt.FileFormatOption} value - The _file-format_ option.
|
|
1085
|
+
* @throws Throws {@link !RangeError} if value is an invalid option.
|
|
1086
|
+
*
|
|
1087
|
+
* @example
|
|
1088
|
+
* ```javascript
|
|
1089
|
+
* // Set the 'file-format' option.
|
|
1090
|
+
* Rsvim.opt.fileFormat = "unix";
|
|
1091
|
+
* ```
|
|
1092
|
+
*/
|
|
1093
|
+
set fileFormat(value: RsvimOpt.FileFormatOption) {
|
|
1094
|
+
checkIsOptions(
|
|
1095
|
+
value,
|
|
1096
|
+
["dos", "unix", "mac"],
|
|
1097
|
+
`"Rsvim.opt.fileFormat" value`,
|
|
1098
|
+
);
|
|
1099
|
+
// @ts-ignore Ignore warning
|
|
1100
|
+
__InternalRsvimGlobalObject.opt_set_file_format(value);
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
/**
|
|
1104
|
+
* Get the _fix-end-of-line_ option. Local to buffer.
|
|
1105
|
+
*
|
|
1106
|
+
* WHen writing a file and this options is enabled, `EOL` (`LF`, `CR`, `CRLF`) at the end of file will be restored if missing. Disable this option if you want to preserve the situation from the original file.
|
|
1107
|
+
*
|
|
1108
|
+
* @see {@link fileFormat}
|
|
1109
|
+
*
|
|
1110
|
+
* @returns {boolean}
|
|
1111
|
+
*
|
|
1112
|
+
* @defaultValue `true`
|
|
1113
|
+
*
|
|
1114
|
+
* @example
|
|
1115
|
+
* ```javascript
|
|
1116
|
+
* // Get the 'fix-end-of-line' option.
|
|
1117
|
+
* const value = Rsvim.opt.fixEndOfLine;
|
|
1118
|
+
* ```
|
|
1119
|
+
*/
|
|
1120
|
+
get fixEndOfLine(): boolean {
|
|
1121
|
+
// @ts-ignore Ignore warning
|
|
1122
|
+
return __InternalRsvimGlobalObject.opt_get_fix_end_of_line();
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/**
|
|
1126
|
+
* Set the _fix-end-of-line_ option.
|
|
1127
|
+
*
|
|
1128
|
+
* @param {boolean} value - The _fix-end-of-line_ option.
|
|
1129
|
+
* @throws Throws {@link !RangeError} if value is not a boolean.
|
|
1130
|
+
*
|
|
1131
|
+
* @example
|
|
1132
|
+
* ```javascript
|
|
1133
|
+
* // Set the 'fix-end-of-line' option.
|
|
1134
|
+
* Rsvim.opt.fixEndOfLine = false;
|
|
1135
|
+
* ```
|
|
1136
|
+
*/
|
|
1137
|
+
set fixEndOfLine(value: boolean) {
|
|
1138
|
+
checkIsBoolean(value, `"Rsvim.opt.fixEndOfLine" value`);
|
|
1139
|
+
// @ts-ignore Ignore warning
|
|
1140
|
+
__InternalRsvimGlobalObject.opt_set_fix_end_of_line(value);
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* Get the _line-break_ option. This options is also known as
|
|
1145
|
+
* [word wrap](https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap). Local to window.
|
|
1146
|
+
*
|
|
1147
|
+
* If `true`, Vim will wrap long lines by a word boundary rather than at the last character that fits on the screen.
|
|
1148
|
+
* It only affects the way the file is displayed, not its contents.
|
|
1149
|
+
*
|
|
1150
|
+
* This option is not used when the {@link wrap} option is `false`.
|
|
1151
|
+
*
|
|
1152
|
+
* @returns {boolean}
|
|
1153
|
+
*
|
|
1154
|
+
* @defaultValue `false`
|
|
1155
|
+
*
|
|
1156
|
+
* @example
|
|
1157
|
+
* ```javascript
|
|
1158
|
+
* // Get the 'lineBreak' option.
|
|
1159
|
+
* const value = Rsvim.opt.lineBreak;
|
|
1160
|
+
* ```
|
|
1161
|
+
*/
|
|
1162
|
+
get lineBreak(): boolean {
|
|
1163
|
+
// @ts-ignore Ignore warning
|
|
1164
|
+
return __InternalRsvimGlobalObject.opt_get_line_break();
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* Set the _line-break_ option.
|
|
1169
|
+
*
|
|
1170
|
+
* @param {boolean} value - The _line-break_ option.
|
|
1171
|
+
* @throws Throws {@link !TypeError} if value is not a boolean.
|
|
1172
|
+
*
|
|
1173
|
+
* @example
|
|
1174
|
+
* ```javascript
|
|
1175
|
+
* // Set the 'lineBreak' option.
|
|
1176
|
+
* Rsvim.opt.lineBreak = true;
|
|
1177
|
+
* ```
|
|
1178
|
+
*/
|
|
1179
|
+
set lineBreak(value: boolean) {
|
|
1180
|
+
checkIsBoolean(value, `"Rsvim.opt.lineBreak" value`);
|
|
1181
|
+
// @ts-ignore Ignore warning
|
|
1182
|
+
__InternalRsvimGlobalObject.opt_set_line_break(value);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* Get the _shift-width_ option. Local to buffer.
|
|
1187
|
+
*
|
|
1188
|
+
* When {@link expandTab} is `true`, the number of spaces that is used when inserts a
|
|
1189
|
+
* [horizontal tab](https://en.wikipedia.org/wiki/Tab_key) (ASCII `9`).
|
|
1190
|
+
*
|
|
1191
|
+
* When {@link expandTab} is `false`, this option is not been used.
|
|
1192
|
+
*
|
|
1193
|
+
*
|
|
1194
|
+
* @returns {number}
|
|
1195
|
+
*
|
|
1196
|
+
* @defaultValue `8`
|
|
1197
|
+
*
|
|
1198
|
+
* @example
|
|
1199
|
+
* ```javascript
|
|
1200
|
+
* // Get the 'shift-width' option.
|
|
1201
|
+
* const value = Rsvim.opt.shiftWidth;
|
|
1202
|
+
* ```
|
|
1203
|
+
*/
|
|
1204
|
+
get shiftWidth(): number {
|
|
1205
|
+
// @ts-ignore Ignore warning
|
|
1206
|
+
return __InternalRsvimGlobalObject.opt_get_shift_width();
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* Set the _shift-width_ option. It only accepts an integer between `[1,255]`, if the value is out of range, it will be bound to this range.
|
|
1211
|
+
*
|
|
1212
|
+
*
|
|
1213
|
+
* @param {number} value - The _shift-width_ option.
|
|
1214
|
+
* @throws Throws {@link !TypeError} if value is not an integer.
|
|
1215
|
+
*
|
|
1216
|
+
* @example
|
|
1217
|
+
* ```javascript
|
|
1218
|
+
* // Set the 'shift-width' option.
|
|
1219
|
+
* Rsvim.opt.shiftWidth = 4;
|
|
1220
|
+
* ```
|
|
1221
|
+
*/
|
|
1222
|
+
set shiftWidth(value: number) {
|
|
1223
|
+
checkIsInteger(value, `"Rsvim.opt.shiftWidth" value`);
|
|
1224
|
+
value = boundByIntegers(value, [1, 255]);
|
|
1225
|
+
// @ts-ignore Ignore warning
|
|
1226
|
+
__InternalRsvimGlobalObject.opt_set_shift_width(value);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
/**
|
|
1230
|
+
* Get the _syntax-parser-lib-path_ option. Global option.
|
|
1231
|
+
*
|
|
1232
|
+
* By default the syntax parser libs are stored in `${RSVIM_CONFIG_HOME}/.tree-sitter-parsers` folder. `${RSVIM_CONFIG_HOME}` is the configuration home for rsvim.
|
|
1233
|
+
*
|
|
1234
|
+
* @see [Rsvim Configuration](https://rsvim.github.io/docs/manual/configuration)
|
|
1235
|
+
*
|
|
1236
|
+
* @returns {string}
|
|
1237
|
+
*
|
|
1238
|
+
* @defaultValue `${RSVIM_CONFIG_HOME}/.tree-sitter-parsers`
|
|
1239
|
+
*
|
|
1240
|
+
* @example
|
|
1241
|
+
* ```javascript
|
|
1242
|
+
* // Get the 'syntax-parser-lib-path' option.
|
|
1243
|
+
* const value = Rsvim.opt.syntaxParserLibPath;
|
|
1244
|
+
* ```
|
|
1245
|
+
*/
|
|
1246
|
+
get syntaxParserLibPath(): string {
|
|
1247
|
+
// @ts-ignore Ignore warning
|
|
1248
|
+
return __InternalRsvimGlobalObject.opt_get_syntax_parser_lib_path();
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Set the _syntax-parser-lib-path_ option. It only accepts a string which represents the file path on your local machine, which is used to save all compiled tree-sitter parser dynamic libraries.
|
|
1253
|
+
*
|
|
1254
|
+
* @param {string} value - The _syntax-parser-lib-path_ option.
|
|
1255
|
+
* @throws Throws {@link !TypeError} if value is not an string.
|
|
1256
|
+
*
|
|
1257
|
+
* @example
|
|
1258
|
+
* ```javascript
|
|
1259
|
+
* // Set the 'syntax-parser-lib-path' option.
|
|
1260
|
+
* Rsvim.opt.syntaxParserLibPath = ".";
|
|
1261
|
+
* ```
|
|
1262
|
+
*/
|
|
1263
|
+
set syntaxParserLibPath(value: string) {
|
|
1264
|
+
checkIsString(value, `"Rsvim.opt.syntaxParserLibPath" value`);
|
|
1265
|
+
// @ts-ignore Ignore warning
|
|
1266
|
+
__InternalRsvimGlobalObject.opt_set_syntax_parser_lib_path(value);
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
/**
|
|
1270
|
+
* Get the _tab-stop_ option. This option is also known as
|
|
1271
|
+
* [tab-size](https://developer.mozilla.org/en-US/docs/Web/CSS/tab-size).
|
|
1272
|
+
* Local to buffer.
|
|
1273
|
+
*
|
|
1274
|
+
* This option changes how text is displayed.
|
|
1275
|
+
*
|
|
1276
|
+
* Defines how many columns (on the terminal) used to display the
|
|
1277
|
+
* [horizontal tab](https://en.wikipedia.org/wiki/Tab_key) (ASCII `9`). This value should be between `[1,255]`.
|
|
1278
|
+
*
|
|
1279
|
+
*
|
|
1280
|
+
* @returns {number}
|
|
1281
|
+
*
|
|
1282
|
+
* @defaultValue `8`
|
|
1283
|
+
*
|
|
1284
|
+
* @example
|
|
1285
|
+
* ```javascript
|
|
1286
|
+
* // Get the 'tab-stop' option.
|
|
1287
|
+
* const value = Rsvim.opt.tabStop;
|
|
1288
|
+
* ```
|
|
1289
|
+
*/
|
|
1290
|
+
get tabStop(): number {
|
|
1291
|
+
// @ts-ignore Ignore warning
|
|
1292
|
+
return __InternalRsvimGlobalObject.opt_get_tab_stop();
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
/**
|
|
1296
|
+
* Set the _tab-stop_ option. It only accepts an integer between `[1,255]`, if the value is out of range, it will be bound to this range.
|
|
1297
|
+
*
|
|
1298
|
+
* @param {number} value - The _tab-stop_ option.
|
|
1299
|
+
* @throws Throws {@link !TypeError} if value is not an integer.
|
|
1300
|
+
*
|
|
1301
|
+
* @example
|
|
1302
|
+
* ```javascript
|
|
1303
|
+
* // Set the 'tab-stop' option.
|
|
1304
|
+
* Rsvim.opt.tabStop = 4;
|
|
1305
|
+
* ```
|
|
1306
|
+
*/
|
|
1307
|
+
set tabStop(value: number) {
|
|
1308
|
+
checkIsInteger(value, `"Rsvim.opt.tabStop" value`);
|
|
1309
|
+
value = boundByIntegers(value, [1, 255]);
|
|
1310
|
+
// @ts-ignore Ignore warning
|
|
1311
|
+
__InternalRsvimGlobalObject.opt_set_tab_stop(value);
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* Get the _wrap_ option. This option is also known as
|
|
1316
|
+
* [line wrap](https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap). Local to window.
|
|
1317
|
+
*
|
|
1318
|
+
* This option changes how text is displayed.
|
|
1319
|
+
*
|
|
1320
|
+
* When `true`, lines longer than the width of the window will wrap and
|
|
1321
|
+
* displaying continues on the next line. When `false` lines will not wrap
|
|
1322
|
+
* and only part of long lines will be displayed. When the cursor is
|
|
1323
|
+
* moved to a part that is not shown, the screen will scroll horizontally.
|
|
1324
|
+
*
|
|
1325
|
+
* The line will be broken in the middle of a word if necessary. See {@link lineBreak}
|
|
1326
|
+
* to get the break at a word boundary.
|
|
1327
|
+
*
|
|
1328
|
+
* @returns {boolean}
|
|
1329
|
+
*
|
|
1330
|
+
* @defaultValue `true`
|
|
1331
|
+
*
|
|
1332
|
+
* @example
|
|
1333
|
+
* ```javascript
|
|
1334
|
+
* // Get the 'wrap' option.
|
|
1335
|
+
* const value = Rsvim.opt.wrap;
|
|
1336
|
+
* ```
|
|
1337
|
+
*/
|
|
1338
|
+
get wrap(): boolean {
|
|
1339
|
+
// @ts-ignore Ignore warning
|
|
1340
|
+
return __InternalRsvimGlobalObject.opt_get_wrap();
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* Set the _wrap_ option.
|
|
1345
|
+
*
|
|
1346
|
+
* @param {boolean} value - The _wrap_ option.
|
|
1347
|
+
* @throws Throws {@link !TypeError} if value is not a boolean.
|
|
1348
|
+
*
|
|
1349
|
+
* @example
|
|
1350
|
+
* ```javascript
|
|
1351
|
+
* // Set the 'wrap' option.
|
|
1352
|
+
* Rsvim.opt.wrap = true;
|
|
1353
|
+
* ```
|
|
1354
|
+
*/
|
|
1355
|
+
set wrap(value: boolean) {
|
|
1356
|
+
checkIsBoolean(value, `"Rsvim.opt.wrap" value`);
|
|
1357
|
+
// @ts-ignore Ignore warning
|
|
1358
|
+
__InternalRsvimGlobalObject.opt_set_wrap(value);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
/**
|
|
1363
|
+
* The `Rsvim.proc` global object for child process.
|
|
1364
|
+
*
|
|
1365
|
+
* @example
|
|
1366
|
+
* ```javascript
|
|
1367
|
+
* // Create a alias to 'Rsvim.proc'.
|
|
1368
|
+
* const proc = Rsvim.proc;
|
|
1369
|
+
* ```
|
|
1370
|
+
*
|
|
1371
|
+
* @category General APIs
|
|
1372
|
+
*/
|
|
1373
|
+
export namespace RsvimProc {
|
|
1374
|
+
/**
|
|
1375
|
+
* The command that create a child process.
|
|
1376
|
+
*/
|
|
1377
|
+
export class Command {
|
|
1378
|
+
/** @hidden */
|
|
1379
|
+
#execPath: string;
|
|
1380
|
+
/** @hidden */
|
|
1381
|
+
#options: RsvimProc.CommandOptions;
|
|
1382
|
+
|
|
1383
|
+
constructor(execPath: string, options?: RsvimProc.CommandOptions) {
|
|
1384
|
+
checkIsString(execPath, `"Rsvim.proc.Command" execPath`);
|
|
1385
|
+
|
|
1386
|
+
options = options ?? {
|
|
1387
|
+
args: [],
|
|
1388
|
+
clearEnv: false,
|
|
1389
|
+
detached: false,
|
|
1390
|
+
env: { __proto__: null },
|
|
1391
|
+
stdin: "null",
|
|
1392
|
+
stdout: "piped",
|
|
1393
|
+
stderr: "piped",
|
|
1394
|
+
};
|
|
1395
|
+
checkIsObject(options, `"Rsvim.proc.Command" options`);
|
|
1396
|
+
setDefaultFields(options, {
|
|
1397
|
+
args: [],
|
|
1398
|
+
clearEnv: false,
|
|
1399
|
+
detached: false,
|
|
1400
|
+
env: { __proto__: null },
|
|
1401
|
+
stdin: "null",
|
|
1402
|
+
stdout: "piped",
|
|
1403
|
+
stderr: "piped",
|
|
1404
|
+
});
|
|
1405
|
+
checkIsArray(options.args, `"Rsvim.proc.Command" args option`);
|
|
1406
|
+
checkIsBoolean(options.clearEnv, `"Rsvim.proc.Command" clearEnv option`);
|
|
1407
|
+
checkIsBoolean(options.detached, `"Rsvim.proc.Command" detached option`);
|
|
1408
|
+
checkIsObject(options.env, `"Rsvim.proc.Command" env option`);
|
|
1409
|
+
checkIsOptions(
|
|
1410
|
+
options.stdin,
|
|
1411
|
+
["null", "piped", "inherit"],
|
|
1412
|
+
`"Rsvim.proc.Command" stdin option`,
|
|
1413
|
+
);
|
|
1414
|
+
checkIsOptions(
|
|
1415
|
+
options.stdout,
|
|
1416
|
+
["null", "piped", "inherit"],
|
|
1417
|
+
`"Rsvim.proc.Command" stdout option`,
|
|
1418
|
+
);
|
|
1419
|
+
checkIsOptions(
|
|
1420
|
+
options.stderr,
|
|
1421
|
+
["null", "piped", "inherit"],
|
|
1422
|
+
`"Rsvim.proc.Command" stderr option`,
|
|
1423
|
+
);
|
|
1424
|
+
|
|
1425
|
+
this.#execPath = execPath;
|
|
1426
|
+
this.#options = options;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
get execPath(): string {
|
|
1430
|
+
return this.#execPath;
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
get options(): RsvimProc.CommandOptions {
|
|
1434
|
+
return this.#options;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
/**
|
|
1438
|
+
* Spawn a child process.
|
|
1439
|
+
*
|
|
1440
|
+
* @returns {RsvimProc.ChildProcess} It returns a child process.
|
|
1441
|
+
* @throws Throws {@link !Error} if failed to spawn the child process.
|
|
1442
|
+
*
|
|
1443
|
+
* @example
|
|
1444
|
+
* ```javascript
|
|
1445
|
+
* try {
|
|
1446
|
+
* const cmd = new Rsvim.proc.Command("ls");
|
|
1447
|
+
* const child = cmd.spawn();
|
|
1448
|
+
* } catch (e) {
|
|
1449
|
+
* Rsvim.cmd.echo(`Failed to spawn child process: ${e}`);
|
|
1450
|
+
* }
|
|
1451
|
+
* ```
|
|
1452
|
+
*/
|
|
1453
|
+
spawn(): RsvimProc.ChildProcess {
|
|
1454
|
+
// @ts-ignore Ignore warning
|
|
1455
|
+
const child = __InternalRsvimGlobalObject.proc_spawn_child(
|
|
1456
|
+
this.#execPath,
|
|
1457
|
+
this.#options,
|
|
1458
|
+
) as {
|
|
1459
|
+
execPath: string;
|
|
1460
|
+
options: RsvimProc.CommandOptions;
|
|
1461
|
+
rid: number;
|
|
1462
|
+
stdinRid: number | undefined;
|
|
1463
|
+
stdoutRid: number | undefined;
|
|
1464
|
+
stderrRid: number | undefined;
|
|
1465
|
+
};
|
|
1466
|
+
return new RsvimProc.ChildProcess(
|
|
1467
|
+
child.execPath,
|
|
1468
|
+
child.options,
|
|
1469
|
+
child.rid,
|
|
1470
|
+
child.stdinRid,
|
|
1471
|
+
child.stdoutRid,
|
|
1472
|
+
child.stderrRid,
|
|
1473
|
+
);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/**
|
|
1478
|
+
* Child process spawned from command.
|
|
1479
|
+
*/
|
|
1480
|
+
export class ChildProcess {
|
|
1481
|
+
/** @hidden */
|
|
1482
|
+
#execPath: string;
|
|
1483
|
+
/** @hidden */
|
|
1484
|
+
#options: RsvimProc.CommandOptions;
|
|
1485
|
+
/** @hidden */
|
|
1486
|
+
#rid: number | null | undefined;
|
|
1487
|
+
/** @hidden */
|
|
1488
|
+
#exitStatus: RsvimProc.ChildProcessExitStatus | null | undefined;
|
|
1489
|
+
/** @hidden */
|
|
1490
|
+
#stdinRid: number | null | undefined;
|
|
1491
|
+
/** @hidden */
|
|
1492
|
+
#stdout: RsvimProc.ChildProcessReadableStream | null | undefined;
|
|
1493
|
+
/** @hidden */
|
|
1494
|
+
#stderr: RsvimProc.ChildProcessReadableStream | null | undefined;
|
|
1495
|
+
|
|
1496
|
+
/** @hideconstructor */
|
|
1497
|
+
constructor(
|
|
1498
|
+
execPath: string,
|
|
1499
|
+
options: RsvimProc.CommandOptions,
|
|
1500
|
+
rid: number,
|
|
1501
|
+
stdinRid: number | null | undefined,
|
|
1502
|
+
stdoutRid: number | null | undefined,
|
|
1503
|
+
stderrRid: number | null | undefined,
|
|
1504
|
+
) {
|
|
1505
|
+
this.#execPath = execPath;
|
|
1506
|
+
this.#options = options;
|
|
1507
|
+
this.#rid = rid;
|
|
1508
|
+
this.#exitStatus = null;
|
|
1509
|
+
this.#stdinRid = stdinRid;
|
|
1510
|
+
this.#stdout =
|
|
1511
|
+
stdoutRid != null
|
|
1512
|
+
? new RsvimProc.ChildProcessReadableStream(stdoutRid)
|
|
1513
|
+
: null;
|
|
1514
|
+
this.#stderr =
|
|
1515
|
+
stderrRid != null
|
|
1516
|
+
? new RsvimProc.ChildProcessReadableStream(stderrRid)
|
|
1517
|
+
: null;
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
get execPath(): string {
|
|
1521
|
+
return this.#execPath;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
get options(): RsvimProc.CommandOptions {
|
|
1525
|
+
return this.#options;
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
get stdout(): RsvimProc.ChildProcessReadableStream | null | undefined {
|
|
1529
|
+
return this.#stdout;
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
get stderr(): RsvimProc.ChildProcessReadableStream | null | undefined {
|
|
1533
|
+
return this.#stderr;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Child process is already completed.
|
|
1538
|
+
*
|
|
1539
|
+
* @example
|
|
1540
|
+
* ```javascript
|
|
1541
|
+
* const child = new Rsvim.proc.Command("ls").spawn();
|
|
1542
|
+
* {
|
|
1543
|
+
* await using usedChild = child;
|
|
1544
|
+
* Rsvim.cmd.echo(usedChild.isDisposed); // false
|
|
1545
|
+
* }
|
|
1546
|
+
* Rsvim.cmd.echo(usedChild.isDisposed); // true
|
|
1547
|
+
* ```
|
|
1548
|
+
*/
|
|
1549
|
+
get isDisposed(): boolean {
|
|
1550
|
+
return isNull(this.#rid);
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
/**
|
|
1554
|
+
* Wait for the child process finish with `await using` API instead of `wait`.
|
|
1555
|
+
*
|
|
1556
|
+
* @returns {void} It returns nothing.
|
|
1557
|
+
*/
|
|
1558
|
+
async [Symbol.asyncDispose](): Promise<void> {
|
|
1559
|
+
await this.wait();
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
/**
|
|
1563
|
+
* Wait for child process complete.
|
|
1564
|
+
*
|
|
1565
|
+
* @returns {RsvimProc.ChildProcessExitStatus} It returns a child process exit status.
|
|
1566
|
+
* @throws Throws {@link !Error} if the child process is already finished, or failed to wait.
|
|
1567
|
+
*
|
|
1568
|
+
* @example
|
|
1569
|
+
* ```javascript
|
|
1570
|
+
* try {
|
|
1571
|
+
* const cmd = new Rsvim.proc.Command("ls");
|
|
1572
|
+
* const child = cmd.spawn();
|
|
1573
|
+
* const output = await child.stdout.text();
|
|
1574
|
+
* const exitStatus = await child.wait();
|
|
1575
|
+
* Rsvim.cmd.echo(`"ls" command is completed successfully: ${exitStatus.success}.`);
|
|
1576
|
+
* } catch (e) {
|
|
1577
|
+
* Rsvim.cmd.echo(`Failed to run "ls" command.`);
|
|
1578
|
+
* }
|
|
1579
|
+
* ```
|
|
1580
|
+
*/
|
|
1581
|
+
async wait(): Promise<RsvimProc.ChildProcessExitStatus> {
|
|
1582
|
+
assertFalse(
|
|
1583
|
+
isNull(this.#rid),
|
|
1584
|
+
"RsvimProc.ChildProcess is already finished!",
|
|
1585
|
+
);
|
|
1586
|
+
this.#exitStatus =
|
|
1587
|
+
// @ts-ignore Ignore warning
|
|
1588
|
+
(await __InternalRsvimGlobalObject.proc_wait_child(
|
|
1589
|
+
this.#rid,
|
|
1590
|
+
)) as RsvimProc.ChildProcessExitStatus;
|
|
1591
|
+
this.#rid = null;
|
|
1592
|
+
return this.#exitStatus as RsvimProc.ChildProcessExitStatus;
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
/**
|
|
1596
|
+
* Get child process exit status.
|
|
1597
|
+
*
|
|
1598
|
+
* @returns {RsvimProc.ChildProcessExitStatus | null | undefined} It returns exit status if the child process is already finished, otherwise it returns `null`.
|
|
1599
|
+
*
|
|
1600
|
+
* @example
|
|
1601
|
+
* ```javascript
|
|
1602
|
+
* const child = new Rsvim.proc.Command("ls").spawn();
|
|
1603
|
+
* await child.wait();
|
|
1604
|
+
* const exitStatus = child.exitStatus;
|
|
1605
|
+
* ```
|
|
1606
|
+
*/
|
|
1607
|
+
get exitStatus(): RsvimProc.ChildProcessExitStatus | null | undefined {
|
|
1608
|
+
return this.#exitStatus;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
/**
|
|
1613
|
+
* Child process readable stream.
|
|
1614
|
+
*/
|
|
1615
|
+
export class ChildProcessReadableStream {
|
|
1616
|
+
/** @hidden */
|
|
1617
|
+
#rid: number;
|
|
1618
|
+
|
|
1619
|
+
/** @hideconstructor */
|
|
1620
|
+
constructor(rid: number) {
|
|
1621
|
+
this.#rid = rid;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
/**
|
|
1625
|
+
* Read text from the stream.
|
|
1626
|
+
*
|
|
1627
|
+
* @returns {string} It returns the read text from child process stdio channel.
|
|
1628
|
+
* @throws Throws {@link !Error} if failed to read.
|
|
1629
|
+
*
|
|
1630
|
+
* @example
|
|
1631
|
+
* ```javascript
|
|
1632
|
+
* try {
|
|
1633
|
+
* const cmd = new Rsvim.proc.Command("ls");
|
|
1634
|
+
* const child = cmd.spawn();
|
|
1635
|
+
* const output = await child.stdout.text();
|
|
1636
|
+
* Rsvim.cmd.echo(`"ls" command output:${output}`);
|
|
1637
|
+
* } catch (e) {
|
|
1638
|
+
* Rsvim.cmd.echo(`Failed to run "ls" command.`);
|
|
1639
|
+
* }
|
|
1640
|
+
* ```
|
|
1641
|
+
*/
|
|
1642
|
+
async text(): Promise<string> {
|
|
1643
|
+
const payload =
|
|
1644
|
+
// @ts-ignore Ignore warning
|
|
1645
|
+
await __InternalRsvimGlobalObject.proc_read_text_from_child(this.#rid);
|
|
1646
|
+
return payload;
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/**
|
|
1651
|
+
* Command options when creating a child-process command.
|
|
1652
|
+
*
|
|
1653
|
+
* @see {@link RsvimProc.Command}
|
|
1654
|
+
*/
|
|
1655
|
+
export type CommandOptions = {
|
|
1656
|
+
/**
|
|
1657
|
+
* Command arguments.
|
|
1658
|
+
*
|
|
1659
|
+
* @defaultValue `[]`
|
|
1660
|
+
*/
|
|
1661
|
+
args?: string[];
|
|
1662
|
+
|
|
1663
|
+
/**
|
|
1664
|
+
* Current working directory.
|
|
1665
|
+
*
|
|
1666
|
+
* @defaultValue `undefined`
|
|
1667
|
+
*/
|
|
1668
|
+
cwd?: string;
|
|
1669
|
+
|
|
1670
|
+
/**
|
|
1671
|
+
* Whether to clear environment variables when the command creating a child-process.
|
|
1672
|
+
*
|
|
1673
|
+
* @defaultValue `false`
|
|
1674
|
+
*/
|
|
1675
|
+
clearEnv?: boolean;
|
|
1676
|
+
|
|
1677
|
+
/**
|
|
1678
|
+
* Whether to detach spawned child process from current process (editor process).
|
|
1679
|
+
* This allows the spawned child process to continue running after current process exits.
|
|
1680
|
+
*
|
|
1681
|
+
* @defaultValue `false`
|
|
1682
|
+
*/
|
|
1683
|
+
detached?: boolean;
|
|
1684
|
+
|
|
1685
|
+
/**
|
|
1686
|
+
* Environment variables to pass to the child-process.
|
|
1687
|
+
*
|
|
1688
|
+
* @defaultValue `{}`
|
|
1689
|
+
*/
|
|
1690
|
+
env?: Record<string, string | undefined | null>;
|
|
1691
|
+
|
|
1692
|
+
/**
|
|
1693
|
+
* How `stdin` of spawned child process should be handled.
|
|
1694
|
+
*
|
|
1695
|
+
* @defaultValue `null`
|
|
1696
|
+
*/
|
|
1697
|
+
stdin?: "piped" | "inherit" | "null";
|
|
1698
|
+
|
|
1699
|
+
/**
|
|
1700
|
+
* How `stdout` of spawned child process should be handled.
|
|
1701
|
+
*
|
|
1702
|
+
* @defaultValue `piped`
|
|
1703
|
+
*/
|
|
1704
|
+
stdout?: "piped" | "inherit" | "null";
|
|
1705
|
+
|
|
1706
|
+
/**
|
|
1707
|
+
* How `stderr` of spawned child process should be handled.
|
|
1708
|
+
*
|
|
1709
|
+
* @defaultValue `piped`
|
|
1710
|
+
*/
|
|
1711
|
+
stderr?: "piped" | "inherit" | "null";
|
|
1712
|
+
};
|
|
1713
|
+
|
|
1714
|
+
/**
|
|
1715
|
+
* Child process exit status.
|
|
1716
|
+
*
|
|
1717
|
+
* @see {@link RsvimProc.ChildProcess}
|
|
1718
|
+
*/
|
|
1719
|
+
export type ChildProcessExitStatus = {
|
|
1720
|
+
/**
|
|
1721
|
+
* Exit successfully.
|
|
1722
|
+
*/
|
|
1723
|
+
success: boolean;
|
|
1724
|
+
|
|
1725
|
+
/**
|
|
1726
|
+
* Exit code.
|
|
1727
|
+
*/
|
|
1728
|
+
exit?: number;
|
|
1729
|
+
|
|
1730
|
+
/**
|
|
1731
|
+
* Terminated by signal.
|
|
1732
|
+
*/
|
|
1733
|
+
signal?: number;
|
|
1734
|
+
};
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
/**
|
|
1738
|
+
* The `Rsvim.rt` global object for javascript runtime (editor process).
|
|
1739
|
+
*
|
|
1740
|
+
* @example
|
|
1741
|
+
* ```javascript
|
|
1742
|
+
* // Create a alias to 'Rsvim.rt'.
|
|
1743
|
+
* const rt = Rsvim.rt;
|
|
1744
|
+
* ```
|
|
1745
|
+
*
|
|
1746
|
+
* @category General APIs
|
|
1747
|
+
*/
|
|
1748
|
+
export namespace RsvimRt {
|
|
1749
|
+
/**
|
|
1750
|
+
* Exit editor.
|
|
1751
|
+
*
|
|
1752
|
+
* :::tip
|
|
1753
|
+
* To ensure file system data safety, editor will wait for all the ongoing file write operations
|
|
1754
|
+
* to complete before actually exiting, however any new write requests will be rejected.
|
|
1755
|
+
* :::
|
|
1756
|
+
*
|
|
1757
|
+
* @param {exitCode?} exitCode - (Optional) The editor process exit with this exit code, by default with code `0`.
|
|
1758
|
+
*
|
|
1759
|
+
* @throws Throws {@link !TypeError} if `exitCode` is neither an integer nor `undefined`.
|
|
1760
|
+
*
|
|
1761
|
+
* @example
|
|
1762
|
+
* ```javascript
|
|
1763
|
+
* // Exit with default exit code `0`.
|
|
1764
|
+
* Rsvim.rt.exit();
|
|
1765
|
+
*
|
|
1766
|
+
* // Exit with error exit code `-1`.
|
|
1767
|
+
* Rsvim.rt.exit(-1);
|
|
1768
|
+
* ```
|
|
1769
|
+
*/
|
|
1770
|
+
export function exit(exitCode?: number): void {
|
|
1771
|
+
exitCode = exitCode ?? 0;
|
|
1772
|
+
checkIsInteger(exitCode, `"Rsvim.rt.exit" code`);
|
|
1773
|
+
// @ts-ignore Ignore warning
|
|
1774
|
+
__InternalRsvimGlobalObject.rt_exit(exitCode);
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* The `Rsvim.syn` global object for javascript runtime (editor process).
|
|
1780
|
+
*
|
|
1781
|
+
* @example
|
|
1782
|
+
* ```javascript
|
|
1783
|
+
* // Create a alias to 'Rsvim.syn'.
|
|
1784
|
+
* const syn = Rsvim.syn;
|
|
1785
|
+
* ```
|
|
1786
|
+
*
|
|
1787
|
+
* @category Editor APIs
|
|
1788
|
+
*/
|
|
1789
|
+
export namespace RsvimSyn {
|
|
1790
|
+
/**
|
|
1791
|
+
* Load tree-sitter parsers.
|
|
1792
|
+
*
|
|
1793
|
+
* @see [tree-sitter - List of parsers](https://github.com/tree-sitter/tree-sitter/wiki/List-of-parsers)
|
|
1794
|
+
*
|
|
1795
|
+
* @param {RsvimSyn.LoadParserOptions} options - Load options.
|
|
1796
|
+
*
|
|
1797
|
+
* @returns {string[]} It returns all the loaded parser names.
|
|
1798
|
+
*
|
|
1799
|
+
* @throws Throws {@link !TypeError} if `options` is an invalid option, throws {@link !Error} if failed to load.
|
|
1800
|
+
*
|
|
1801
|
+
* @example
|
|
1802
|
+
* ```javascript
|
|
1803
|
+
* // Load `tree-sitter-c` parser.
|
|
1804
|
+
* const parserNames = await Rsvim.syn.loadParser({grammarPath: "./tree-sitter-c"});
|
|
1805
|
+
* Rsvim.cmd.echo(`Loaded parsers: ${parserNames}`);
|
|
1806
|
+
* ```
|
|
1807
|
+
*/
|
|
1808
|
+
export async function loadParser(
|
|
1809
|
+
options: RsvimSyn.LoadParserOptions,
|
|
1810
|
+
): Promise<string[]> {
|
|
1811
|
+
checkIsObject(options, `"Rsvim.syn.loadParser" options`);
|
|
1812
|
+
checkIsString(
|
|
1813
|
+
options.grammarPath,
|
|
1814
|
+
`"Rsvim.syn.loadParser" grammarPath option`,
|
|
1815
|
+
);
|
|
1816
|
+
const parserNames =
|
|
1817
|
+
// @ts-ignore Ignore warning
|
|
1818
|
+
await __InternalRsvimGlobalObject.syn_load_parser(options);
|
|
1819
|
+
return parserNames;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
/**
|
|
1823
|
+
* Load tree-sitter parsers synchronizely.
|
|
1824
|
+
*
|
|
1825
|
+
* @see {@link loadParser}
|
|
1826
|
+
*
|
|
1827
|
+
* @param {RsvimSyn.LoadParserOptions} options - Load options.
|
|
1828
|
+
*
|
|
1829
|
+
* @returns {string[]} It returns all the loaded parser names.
|
|
1830
|
+
*
|
|
1831
|
+
* @throws Throws {@link !TypeError} if `options` is an invalid option, throws {@link !Error} if failed to load.
|
|
1832
|
+
*
|
|
1833
|
+
* @example
|
|
1834
|
+
* ```javascript
|
|
1835
|
+
* // Load `tree-sitter-c` parser synchronizely.
|
|
1836
|
+
* const parserNames = Rsvim.syn.loadParserSync({grammarPath: "./tree-sitter-c"});
|
|
1837
|
+
* Rsvim.cmd.echo(`Loaded parsers: ${parserNames}`);
|
|
1838
|
+
* ```
|
|
1839
|
+
*/
|
|
1840
|
+
export function loadParserSync(
|
|
1841
|
+
options: RsvimSyn.LoadParserOptions,
|
|
1842
|
+
): string[] {
|
|
1843
|
+
checkIsObject(options, `"Rsvim.syn.loadParserSync" options`);
|
|
1844
|
+
checkIsString(
|
|
1845
|
+
options.grammarPath,
|
|
1846
|
+
`"Rsvim.syn.loadParserSync" grammarPath option`,
|
|
1847
|
+
);
|
|
1848
|
+
const parserNames =
|
|
1849
|
+
// @ts-ignore Ignore warning
|
|
1850
|
+
__InternalRsvimGlobalObject.syn_load_parser_sync(options);
|
|
1851
|
+
return parserNames;
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
/**
|
|
1855
|
+
* List all loaded tree-sitter parsers.
|
|
1856
|
+
*
|
|
1857
|
+
* @returns {string[]} It returns all the loaded parser names.
|
|
1858
|
+
*
|
|
1859
|
+
* @example
|
|
1860
|
+
* ```javascript
|
|
1861
|
+
* // Print all loaded parser names.
|
|
1862
|
+
* const allParserNames = Rsvim.syn.listParsers();
|
|
1863
|
+
* Rsvim.cmd.echo(`All loaded parsers: ${allParserNames}`);
|
|
1864
|
+
* ```
|
|
1865
|
+
*/
|
|
1866
|
+
export function listParsers(): string[] {
|
|
1867
|
+
// @ts-ignore Ignore warning
|
|
1868
|
+
return __InternalRsvimGlobalObject.syn_list_parsers();
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
/**
|
|
1872
|
+
* Get tree-sitter parser metadata by parser name.
|
|
1873
|
+
*
|
|
1874
|
+
* @param {string} name - The parser's name.
|
|
1875
|
+
*
|
|
1876
|
+
* @returns {RsvimSyn.ParserMetadata | undefined} It returns all the loaded parser names.
|
|
1877
|
+
*
|
|
1878
|
+
* @example
|
|
1879
|
+
* ```javascript
|
|
1880
|
+
* // Get parser metadata by name.
|
|
1881
|
+
* const parserMetadata = Rsvim.syn.getParserMetadata("rust");
|
|
1882
|
+
* Rsvim.cmd.echo(`Rust parser metadata: ${parserMetadata}`);
|
|
1883
|
+
* ```
|
|
1884
|
+
*/
|
|
1885
|
+
export function getParserMetadata(
|
|
1886
|
+
name: string,
|
|
1887
|
+
): RsvimSyn.ParserMetadata | undefined {
|
|
1888
|
+
checkIsString(name, `"Rsvim.syn.getParserMetadata" name`);
|
|
1889
|
+
|
|
1890
|
+
// @ts-ignore Ignore warning
|
|
1891
|
+
return __InternalRsvimGlobalObject.syn_get_parser_metadata(name);
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
/**
|
|
1895
|
+
* Options to load a tree-sitter parser.
|
|
1896
|
+
*
|
|
1897
|
+
* @see {@link RsvimSyn.loadParser}
|
|
1898
|
+
*/
|
|
1899
|
+
export type LoadParserOptions = {
|
|
1900
|
+
/**
|
|
1901
|
+
* The tree-sitter parser path to load.
|
|
1902
|
+
*/
|
|
1903
|
+
grammarPath: string;
|
|
1904
|
+
};
|
|
1905
|
+
|
|
1906
|
+
/**
|
|
1907
|
+
* Tree-sitter parser metadata.
|
|
1908
|
+
*
|
|
1909
|
+
* @see {@link RsvimSyn.getParserMetadata}
|
|
1910
|
+
*/
|
|
1911
|
+
export type ParserMetadata = {
|
|
1912
|
+
/**
|
|
1913
|
+
* The tree-sitter parser name.
|
|
1914
|
+
*/
|
|
1915
|
+
name: string;
|
|
1916
|
+
|
|
1917
|
+
/**
|
|
1918
|
+
* The tree-sitter parser name's camelcase.
|
|
1919
|
+
*/
|
|
1920
|
+
camelcase: string;
|
|
1921
|
+
|
|
1922
|
+
/**
|
|
1923
|
+
* The tree-sitter parser scope.
|
|
1924
|
+
*/
|
|
1925
|
+
scope: string;
|
|
1926
|
+
|
|
1927
|
+
/**
|
|
1928
|
+
* The tree-sitter parser path.
|
|
1929
|
+
*/
|
|
1930
|
+
path: string;
|
|
1931
|
+
|
|
1932
|
+
/**
|
|
1933
|
+
* The tree-sitter parser file types.
|
|
1934
|
+
*/
|
|
1935
|
+
fileTypes: string[];
|
|
1936
|
+
|
|
1937
|
+
/**
|
|
1938
|
+
* The tree-sitter parser highlights query path.
|
|
1939
|
+
*/
|
|
1940
|
+
highlightsPath?: string;
|
|
1941
|
+
|
|
1942
|
+
/**
|
|
1943
|
+
* The tree-sitter parser highlights query.
|
|
1944
|
+
*/
|
|
1945
|
+
highlightsQuery?: string;
|
|
1946
|
+
|
|
1947
|
+
/**
|
|
1948
|
+
* The tree-sitter parser tags query path.
|
|
1949
|
+
*/
|
|
1950
|
+
tagsPath?: string;
|
|
1951
|
+
|
|
1952
|
+
/**
|
|
1953
|
+
* The tree-sitter parser tags query.
|
|
1954
|
+
*/
|
|
1955
|
+
tagsQuery?: string;
|
|
1956
|
+
|
|
1957
|
+
/**
|
|
1958
|
+
* The tree-sitter parser injections query path.
|
|
1959
|
+
*/
|
|
1960
|
+
injectionsPath?: string;
|
|
1961
|
+
|
|
1962
|
+
/**
|
|
1963
|
+
* The tree-sitter parser injections query.
|
|
1964
|
+
*/
|
|
1965
|
+
injectionsQuery?: string;
|
|
1966
|
+
|
|
1967
|
+
/**
|
|
1968
|
+
* The tree-sitter parser injection regex.
|
|
1969
|
+
*/
|
|
1970
|
+
injectionRegex?: string;
|
|
1971
|
+
};
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
/**
|
|
1975
|
+
* The `Rsvim` global object.
|
|
1976
|
+
*
|
|
1977
|
+
* @example
|
|
1978
|
+
* ```javascript
|
|
1979
|
+
* // Create a alias to 'Rsvim'.
|
|
1980
|
+
* const vim = Rsvim;
|
|
1981
|
+
* ```
|
|
1982
|
+
*
|
|
1983
|
+
* @category Global Object
|
|
1984
|
+
*/
|
|
1985
|
+
export namespace Rsvim {
|
|
1986
|
+
export import buf = RsvimBuf;
|
|
1987
|
+
export import cmd = RsvimCmd;
|
|
1988
|
+
export import fs = RsvimFs;
|
|
1989
|
+
export const opt = new RsvimOpt();
|
|
1990
|
+
export import proc = RsvimProc;
|
|
1991
|
+
export import rt = RsvimRt;
|
|
1992
|
+
export import syn = RsvimSyn;
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
(function (globalThis: { Rsvim: typeof Rsvim }) {
|
|
1996
|
+
globalThis.Rsvim = Rsvim;
|
|
1997
|
+
})(globalThis as unknown as { Rsvim: typeof Rsvim });
|
|
1998
|
+
|
|
1999
|
+
/// Declarations for .d.ts
|
|
2000
|
+
declare global {
|
|
2001
|
+
// @ts-ignore Ignore warning
|
|
2002
|
+
var Rsvim: typeof Rsvim;
|
|
2003
|
+
}
|