nextlua 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,110 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const beautify = require("./src/main");
4
+
5
+ const commandId = "nextlua.beautifyDocument";
6
+
7
+ function beautifyText(input) {
8
+ return beautify(input);
9
+ }
10
+
11
+ function activate(context) {
12
+ const vscode = require("vscode");
13
+ const selector = [
14
+ { language: "lua", scheme: "file" },
15
+ { language: "lua", scheme: "untitled" },
16
+ { language: "luau", scheme: "file" },
17
+ { language: "luau", scheme: "untitled" }
18
+ ];
19
+
20
+ const formatProvider = vscode.languages.registerDocumentFormattingEditProvider(selector, {
21
+ provideDocumentFormattingEdits(document) {
22
+ const fullRange = new vscode.Range(
23
+ document.positionAt(0),
24
+ document.lineAt(document.lineCount - 1).range.end
25
+ );
26
+
27
+ return [
28
+ vscode.TextEdit.replace(fullRange, beautifyText(document.getText()))
29
+ ];
30
+ }
31
+ });
32
+
33
+ const command = vscode.commands.registerCommand(commandId, async () => {
34
+ const editor = vscode.window.activeTextEditor;
35
+ if (!editor) {
36
+ vscode.window.showInformationMessage("Open a Lua or Luau file first.");
37
+ return;
38
+ }
39
+
40
+ const document = editor.document;
41
+ const fullRange = new vscode.Range(
42
+ document.positionAt(0),
43
+ document.lineAt(document.lineCount - 1).range.end
44
+ );
45
+ const output = beautifyText(document.getText());
46
+
47
+ await editor.edit(editBuilder => {
48
+ editBuilder.replace(fullRange, output);
49
+ });
50
+ });
51
+
52
+ context.subscriptions.push(formatProvider, command);
53
+ }
54
+
55
+ function deactivate() {}
56
+
57
+ function readStdin() {
58
+ return new Promise((resolve, reject) => {
59
+ let input = "";
60
+
61
+ process.stdin.setEncoding("utf8");
62
+ process.stdin.on("data", chunk => {
63
+ input += chunk;
64
+ });
65
+ process.stdin.on("end", () => resolve(input));
66
+ process.stdin.on("error", reject);
67
+ });
68
+ }
69
+
70
+ async function runCli(argv) {
71
+ const inputPath = argv[2];
72
+ const outputPath = argv[3];
73
+ let input;
74
+
75
+ if (inputPath) {
76
+ input = fs.readFileSync(path.resolve(inputPath), "utf8");
77
+ } else if (fs.existsSync("input.lua")) {
78
+ input = fs.readFileSync("input.lua", "utf8");
79
+ } else if (!process.stdin.isTTY) {
80
+ input = await readStdin();
81
+ } else {
82
+ throw new Error("Usage: node index.js [input.lua] [output.lua] or pipe Lua code through stdin.");
83
+ }
84
+
85
+ const output = beautifyText(input);
86
+
87
+ if (outputPath) {
88
+ fs.writeFileSync(path.resolve(outputPath), output, "utf8");
89
+ } else if (!inputPath && fs.existsSync("input.lua")) {
90
+ fs.writeFileSync("output.lua", output, "utf8");
91
+ } else {
92
+ process.stdout.write(output);
93
+ if (!output.endsWith("\n")) {
94
+ process.stdout.write("\n");
95
+ }
96
+ }
97
+ }
98
+
99
+ if (require.main === module) {
100
+ runCli(process.argv).catch(error => {
101
+ console.error(error.message);
102
+ process.exitCode = 1;
103
+ });
104
+ }
105
+
106
+ module.exports = {
107
+ activate,
108
+ deactivate,
109
+ beautifyText
110
+ };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "nextlua",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs"
13
+ }
@@ -0,0 +1,11 @@
1
+ module.exports = {
2
+ after: [
3
+ "do",
4
+ "then",
5
+ /(?:^|\s)(?:local\s+)?function\b\s*\([^)]*\)/
6
+ ],
7
+ before: [
8
+ "end",
9
+ "elseif"
10
+ ]
11
+ };
package/src/main.js ADDED
@@ -0,0 +1,566 @@
1
+ const indent = " ";
2
+
3
+ const multiCharTokens = [
4
+ "...",
5
+ "..=",
6
+ "..",
7
+ "==",
8
+ "~=",
9
+ "<=",
10
+ ">=",
11
+ "+=",
12
+ "-=",
13
+ "*=",
14
+ "/=",
15
+ "%=",
16
+ "^=",
17
+ "::",
18
+ "//",
19
+ "->",
20
+ "<<",
21
+ ">>"
22
+ ];
23
+
24
+ const blockMiddle = new Set(["else", "elseif"]);
25
+ const blockEnd = new Set(["end", "until"]);
26
+ const statementKeywords = new Set([
27
+ "local",
28
+ "return",
29
+ "break",
30
+ "continue",
31
+ "if",
32
+ "elseif",
33
+ "else",
34
+ "while",
35
+ "for",
36
+ "repeat",
37
+ "do",
38
+ "function"
39
+ ]);
40
+ const reservedKeywords = new Set([
41
+ "and",
42
+ "break",
43
+ "continue",
44
+ "do",
45
+ "else",
46
+ "elseif",
47
+ "end",
48
+ "false",
49
+ "for",
50
+ "function",
51
+ "if",
52
+ "in",
53
+ "local",
54
+ "nil",
55
+ "not",
56
+ "or",
57
+ "repeat",
58
+ "return",
59
+ "then",
60
+ "true",
61
+ "until",
62
+ "while"
63
+ ]);
64
+ const expressionContinuations = new Set([
65
+ "=",
66
+ "(",
67
+ "[",
68
+ "{",
69
+ ",",
70
+ ".",
71
+ ":",
72
+ "::",
73
+ "return",
74
+ "local",
75
+ "then",
76
+ "do",
77
+ "in",
78
+ "until",
79
+ "elseif",
80
+ "while",
81
+ "for",
82
+ "if",
83
+ "function",
84
+ "not",
85
+ "and",
86
+ "or"
87
+ ]);
88
+ const binaryOperators = new Set([
89
+ "=",
90
+ "+",
91
+ "-",
92
+ "*",
93
+ "/",
94
+ "//",
95
+ "%",
96
+ "^",
97
+ "..",
98
+ "..=",
99
+ "==",
100
+ "~=",
101
+ "<=",
102
+ ">=",
103
+ "<",
104
+ ">",
105
+ "and",
106
+ "or",
107
+ "+=",
108
+ "-=",
109
+ "*=",
110
+ "/=",
111
+ "%=",
112
+ "^="
113
+ ]);
114
+
115
+ function isIdentifierStart(ch) {
116
+ return /[A-Za-z_]/.test(ch);
117
+ }
118
+
119
+ function isIdentifierPart(ch) {
120
+ return /[A-Za-z0-9_]/.test(ch);
121
+ }
122
+
123
+ function isNumberStart(ch) {
124
+ return /\d/.test(ch);
125
+ }
126
+
127
+ function readLongBracket(input, index) {
128
+ const match = input.slice(index).match(/^\[(=*)\[/);
129
+ if (!match) {
130
+ return null;
131
+ }
132
+
133
+ const marker = match[1];
134
+ const closing = `]${marker}]`;
135
+ const end = input.indexOf(closing, index + match[0].length);
136
+ const closeIndex = end === -1 ? input.length : end + closing.length;
137
+
138
+ return {
139
+ value: input.slice(index, closeIndex),
140
+ nextIndex: closeIndex
141
+ };
142
+ }
143
+
144
+ function tokenize(input) {
145
+ const tokens = [];
146
+ let i = 0;
147
+
148
+ while (i < input.length) {
149
+ const ch = input[i];
150
+
151
+ if (/\s/.test(ch)) {
152
+ i++;
153
+ continue;
154
+ }
155
+
156
+ if (input.startsWith("--", i)) {
157
+ const longComment = readLongBracket(input, i + 2);
158
+ if (longComment) {
159
+ tokens.push(`--${longComment.value}`);
160
+ i = longComment.nextIndex;
161
+ continue;
162
+ }
163
+
164
+ let end = input.indexOf("\n", i);
165
+ if (end === -1) {
166
+ end = input.length;
167
+ }
168
+
169
+ tokens.push(input.slice(i, end));
170
+ i = end;
171
+ continue;
172
+ }
173
+
174
+ if (ch === '"' || ch === "'" || ch === "`") {
175
+ let value = ch;
176
+ i++;
177
+
178
+ while (i < input.length) {
179
+ value += input[i];
180
+ if (input[i] === ch && input[i - 1] !== "\\") {
181
+ i++;
182
+ break;
183
+ }
184
+ i++;
185
+ }
186
+
187
+ tokens.push(value);
188
+ continue;
189
+ }
190
+
191
+ const longString = readLongBracket(input, i);
192
+ if (longString) {
193
+ tokens.push(longString.value);
194
+ i = longString.nextIndex;
195
+ continue;
196
+ }
197
+
198
+ const matchedMulti = multiCharTokens.find(token => input.startsWith(token, i));
199
+ if (matchedMulti) {
200
+ tokens.push(matchedMulti);
201
+ i += matchedMulti.length;
202
+ continue;
203
+ }
204
+
205
+ if (isIdentifierStart(ch)) {
206
+ let end = i + 1;
207
+ while (end < input.length && isIdentifierPart(input[end])) {
208
+ end++;
209
+ }
210
+ tokens.push(input.slice(i, end));
211
+ i = end;
212
+ continue;
213
+ }
214
+
215
+ if (isNumberStart(ch)) {
216
+ let end = i + 1;
217
+ while (end < input.length && /[A-Fa-f0-9_xX.]/.test(input[end])) {
218
+ end++;
219
+ }
220
+ tokens.push(input.slice(i, end));
221
+ i = end;
222
+ continue;
223
+ }
224
+
225
+ tokens.push(ch);
226
+ i++;
227
+ }
228
+
229
+ return tokens;
230
+ }
231
+
232
+ function isComment(token) {
233
+ return token.startsWith("--");
234
+ }
235
+
236
+ function isIdentifier(token) {
237
+ return /^[A-Za-z_][A-Za-z0-9_]*$/.test(token);
238
+ }
239
+
240
+ function isLiteral(token) {
241
+ return /^["'`]/.test(token) || /^\d/.test(token) || token === "..." || token === "true" || token === "false" || token === "nil";
242
+ }
243
+
244
+ function canEndStatement(token) {
245
+ return (isIdentifier(token) && !reservedKeywords.has(token)) || isLiteral(token) || token === ")" || token === "]" || token === "}" || token === "end" || token === "break" || token === "continue";
246
+ }
247
+
248
+ function shouldStartNewStatement(token, prevToken) {
249
+ if (!prevToken || expressionContinuations.has(prevToken)) {
250
+ return false;
251
+ }
252
+
253
+ if (token === "then" || token === "do" || token === "until" || token === "in") {
254
+ return false;
255
+ }
256
+
257
+ return statementKeywords.has(token) || (isIdentifier(token) && !reservedKeywords.has(token));
258
+ }
259
+
260
+ function needsSpace(prev, current) {
261
+ if (!prev) {
262
+ return false;
263
+ }
264
+
265
+ if (current === "," || current === ";" || current === ")" || current === "]" || current === "}") {
266
+ return false;
267
+ }
268
+
269
+ if (prev === "(" || prev === "[" || prev === "{") {
270
+ return false;
271
+ }
272
+
273
+ if (current === "(") {
274
+ return false;
275
+ }
276
+
277
+ if (prev === "." || current === "." || prev === ":" || current === ":") {
278
+ return false;
279
+ }
280
+
281
+ if (prev === "::" || current === "::") {
282
+ return false;
283
+ }
284
+
285
+ if (prev === "#" || current === "#") {
286
+ return false;
287
+ }
288
+
289
+ if (binaryOperators.has(prev) || binaryOperators.has(current)) {
290
+ return true;
291
+ }
292
+
293
+ return true;
294
+ }
295
+
296
+ function renderLine(tokens) {
297
+ let text = "";
298
+ let prev = null;
299
+
300
+ for (const token of tokens) {
301
+ if (needsSpace(prev, token)) {
302
+ text += " ";
303
+ }
304
+ text += token;
305
+ prev = token;
306
+ }
307
+
308
+ return text.trim();
309
+ }
310
+
311
+ function beautify(input) {
312
+ const tokens = tokenize(input);
313
+ const lines = [];
314
+ let current = [];
315
+ let depth = 0;
316
+ let parenDepth = 0;
317
+ let braceDepth = 0;
318
+ let bracketDepth = 0;
319
+ let pendingHeader = null;
320
+ let functionHeaderDepth = null;
321
+ const inlineIfStack = [];
322
+ const blockBases = [{ paren: 0, brace: 0, bracket: 0 }];
323
+ const tableStack = [];
324
+
325
+ function flushCurrent() {
326
+ if (!current.length) {
327
+ return;
328
+ }
329
+
330
+ lines.push({
331
+ depth: Math.max(0, depth),
332
+ text: renderLine(current)
333
+ });
334
+ current = [];
335
+ }
336
+
337
+ function currentBase() {
338
+ return blockBases[blockBases.length - 1];
339
+ }
340
+
341
+ function atStatementLevel() {
342
+ const base = currentBase();
343
+ return parenDepth === base.paren && braceDepth === base.brace && bracketDepth === base.bracket;
344
+ }
345
+
346
+ function isInlineIf(token, prevToken) {
347
+ if (token !== "if") {
348
+ return false;
349
+ }
350
+
351
+ if (!current.length) {
352
+ return false;
353
+ }
354
+
355
+ return !canEndStatement(prevToken);
356
+ }
357
+
358
+ function currentTable() {
359
+ return tableStack[tableStack.length - 1];
360
+ }
361
+
362
+ function enterBlock() {
363
+ blockBases.push({
364
+ paren: parenDepth,
365
+ brace: braceDepth,
366
+ bracket: bracketDepth
367
+ });
368
+ }
369
+
370
+ function leaveBlock() {
371
+ if (blockBases.length > 1) {
372
+ blockBases.pop();
373
+ }
374
+ }
375
+
376
+ for (let i = 0; i < tokens.length; i++) {
377
+ const token = tokens[i];
378
+ const prevToken = current[current.length - 1];
379
+
380
+ if (isComment(token)) {
381
+ flushCurrent();
382
+ lines.push({
383
+ depth: Math.max(0, depth),
384
+ text: token
385
+ });
386
+ continue;
387
+ }
388
+
389
+ if (!inlineIfStack.length && atStatementLevel() && current.length && shouldStartNewStatement(token, prevToken) && canEndStatement(prevToken)) {
390
+ flushCurrent();
391
+ }
392
+
393
+ if (blockMiddle.has(token) && atStatementLevel()) {
394
+ if (token === "else" && inlineIfStack.length) {
395
+ inlineIfStack.pop();
396
+ current.push(token);
397
+ continue;
398
+ }
399
+
400
+ flushCurrent();
401
+ leaveBlock();
402
+ depth = Math.max(0, depth - 1);
403
+ current.push(token);
404
+ pendingHeader = token === "elseif" ? "then" : null;
405
+
406
+ if (token === "else") {
407
+ flushCurrent();
408
+ depth++;
409
+ enterBlock();
410
+ }
411
+ continue;
412
+ }
413
+
414
+ if (blockEnd.has(token) && atStatementLevel()) {
415
+ flushCurrent();
416
+ leaveBlock();
417
+ depth = Math.max(0, depth - 1);
418
+ current.push(token);
419
+
420
+ if (token === "until") {
421
+ continue;
422
+ }
423
+ continue;
424
+ }
425
+
426
+ current.push(token);
427
+
428
+ if (token === "(") {
429
+ parenDepth++;
430
+ if (functionHeaderDepth === null && current.includes("function")) {
431
+ functionHeaderDepth = parenDepth;
432
+ }
433
+ continue;
434
+ }
435
+
436
+ if (token === ")") {
437
+ parenDepth = Math.max(0, parenDepth - 1);
438
+ if (functionHeaderDepth !== null && parenDepth < functionHeaderDepth) {
439
+ functionHeaderDepth = null;
440
+ flushCurrent();
441
+ depth++;
442
+ enterBlock();
443
+ }
444
+ continue;
445
+ }
446
+
447
+ if (token === "{") {
448
+ braceDepth++;
449
+ const multiline = tokens[i + 1] !== "}";
450
+ tableStack.push({
451
+ brace: braceDepth,
452
+ paren: parenDepth,
453
+ bracket: bracketDepth,
454
+ blockDepth: blockBases.length,
455
+ multiline
456
+ });
457
+
458
+ if (multiline) {
459
+ flushCurrent();
460
+ depth++;
461
+ }
462
+ continue;
463
+ }
464
+
465
+ if (token === "}") {
466
+ const table = currentTable();
467
+ if (
468
+ table &&
469
+ table.multiline &&
470
+ braceDepth === table.brace &&
471
+ parenDepth === table.paren &&
472
+ bracketDepth === table.bracket &&
473
+ blockBases.length === table.blockDepth
474
+ ) {
475
+ current.pop();
476
+ flushCurrent();
477
+ depth = Math.max(0, depth - 1);
478
+ current.push(token);
479
+ }
480
+
481
+ braceDepth = Math.max(0, braceDepth - 1);
482
+ tableStack.pop();
483
+ continue;
484
+ }
485
+
486
+ if (token === "[") {
487
+ bracketDepth++;
488
+ continue;
489
+ }
490
+
491
+ if (token === "]") {
492
+ bracketDepth = Math.max(0, bracketDepth - 1);
493
+ continue;
494
+ }
495
+
496
+ if (token === ";") {
497
+ flushCurrent();
498
+ continue;
499
+ }
500
+
501
+ if (token === ",") {
502
+ const table = currentTable();
503
+ if (
504
+ table &&
505
+ table.multiline &&
506
+ braceDepth === table.brace &&
507
+ parenDepth === table.paren &&
508
+ bracketDepth === table.bracket &&
509
+ blockBases.length === table.blockDepth
510
+ ) {
511
+ flushCurrent();
512
+ }
513
+ continue;
514
+ }
515
+
516
+ if (isInlineIf(token, prevToken)) {
517
+ inlineIfStack.push("then");
518
+ continue;
519
+ }
520
+
521
+ if (token === "if" || token === "elseif") {
522
+ pendingHeader = "then";
523
+ continue;
524
+ }
525
+
526
+ if (token === "while" || token === "for") {
527
+ pendingHeader = "do";
528
+ continue;
529
+ }
530
+
531
+ if (token === "repeat") {
532
+ flushCurrent();
533
+ depth++;
534
+ enterBlock();
535
+ continue;
536
+ }
537
+
538
+ if (token === "do" && pendingHeader !== "do" && atStatementLevel()) {
539
+ flushCurrent();
540
+ depth++;
541
+ enterBlock();
542
+ continue;
543
+ }
544
+
545
+ if (token === "then" && inlineIfStack.length && inlineIfStack[inlineIfStack.length - 1] === "then") {
546
+ inlineIfStack[inlineIfStack.length - 1] = "else";
547
+ continue;
548
+ }
549
+
550
+ if ((token === "then" && pendingHeader === "then") || (token === "do" && pendingHeader === "do")) {
551
+ pendingHeader = null;
552
+ flushCurrent();
553
+ depth++;
554
+ enterBlock();
555
+ continue;
556
+ }
557
+ }
558
+
559
+ flushCurrent();
560
+
561
+ return lines
562
+ .map(line => `${indent.repeat(line.depth)}${line.text}`.trimEnd())
563
+ .join("\n");
564
+ }
565
+
566
+ module.exports = beautify;