nextlua 3.1.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +75 -13
  2. package/package.json +36 -3
  3. package/src/main.js +12 -2
package/index.js CHANGED
@@ -4,12 +4,18 @@ const fs = require("fs");
4
4
  const path = require("path");
5
5
  const { beautify, minify } = require("./src/main");
6
6
 
7
- const commandId = "nextlua.beautifyDocument";
7
+ const beautifyCommandId = "nextlua.beautifyDocument";
8
+ const minifyCommandId = "nextlua.minifyDocument";
9
+ const menuCommandId = "nextlua.showMenu";
8
10
 
9
11
  function beautifyText(input) {
10
12
  return beautify(input);
11
13
  }
12
14
 
15
+ function minifyText(input) {
16
+ return minify(input);
17
+ }
18
+
13
19
  function activate(context) {
14
20
  const vscode = require("vscode");
15
21
  const selector = [
@@ -32,26 +38,81 @@ function activate(context) {
32
38
  }
33
39
  });
34
40
 
35
- const command = vscode.commands.registerCommand(commandId, async () => {
41
+ function registerTransformCommand(commandId, transform) {
42
+ return vscode.commands.registerCommand(commandId, async () => {
43
+ const editor = vscode.window.activeTextEditor;
44
+ if (!editor) {
45
+ vscode.window.showInformationMessage("Open a Lua or Luau file first.");
46
+ return;
47
+ }
48
+
49
+ const document = editor.document;
50
+ const fullRange = new vscode.Range(
51
+ document.positionAt(0),
52
+ document.lineAt(document.lineCount - 1).range.end
53
+ );
54
+ const output = transform(document.getText());
55
+
56
+ await editor.edit(editBuilder => {
57
+ editBuilder.replace(fullRange, output);
58
+ });
59
+ });
60
+ }
61
+
62
+ const beautifyCommand = registerTransformCommand(beautifyCommandId, beautifyText);
63
+ const minifyCommand = registerTransformCommand(minifyCommandId, minifyText);
64
+
65
+ const transforms = {
66
+ Beautify: beautifyText,
67
+ Minify: minifyText
68
+ };
69
+
70
+ const menuCommand = vscode.commands.registerCommand(menuCommandId, async () => {
36
71
  const editor = vscode.window.activeTextEditor;
37
72
  if (!editor) {
38
- vscode.window.showInformationMessage("Open a Lua or Luau file first.");
73
+ vscode.window.showErrorMessage("No script content in current file");
39
74
  return;
40
75
  }
41
76
 
42
- const document = editor.document;
43
- const fullRange = new vscode.Range(
44
- document.positionAt(0),
45
- document.lineAt(document.lineCount - 1).range.end
46
- );
47
- const output = beautifyText(document.getText());
77
+ const script = editor.document.getText();
78
+ if (!script.length) {
79
+ vscode.window.showErrorMessage("No script content in current file");
80
+ return;
81
+ }
48
82
 
49
- await editor.edit(editBuilder => {
50
- editBuilder.replace(fullRange, output);
51
- });
83
+ const type = await vscode.window.showQuickPick(["Beautify", "Minify"]);
84
+ if (!type) return;
85
+
86
+ let output;
87
+ try {
88
+ output = transforms[type](script);
89
+ } catch (error) {
90
+ console.log(error);
91
+ vscode.window.showErrorMessage(`Failed to ${type}`);
92
+ return;
93
+ }
94
+
95
+ const range = new vscode.Range(
96
+ editor.document.positionAt(0),
97
+ editor.document.positionAt(script.length)
98
+ );
99
+ await editor.edit(editBuilder => editBuilder.replace(range, output));
100
+ vscode.window.showInformationMessage("Completed!");
52
101
  });
53
102
 
54
- context.subscriptions.push(formatProvider, command);
103
+ const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0);
104
+ statusBarItem.text = "nextlua";
105
+ statusBarItem.tooltip = "Lua Formatter";
106
+ statusBarItem.command = menuCommandId;
107
+ statusBarItem.show();
108
+
109
+ context.subscriptions.push(
110
+ formatProvider,
111
+ beautifyCommand,
112
+ minifyCommand,
113
+ menuCommand,
114
+ statusBarItem
115
+ );
55
116
  }
56
117
 
57
118
  function deactivate() {}
@@ -137,6 +198,7 @@ module.exports = {
137
198
  activate,
138
199
  deactivate,
139
200
  beautifyText,
201
+ minifyText,
140
202
  minify,
141
203
  beautify
142
204
  };
package/package.json CHANGED
@@ -1,17 +1,50 @@
1
1
  {
2
2
  "name": "nextlua",
3
- "version": "3.1.0",
3
+ "displayName": "nextlua",
4
+ "version": "3.2.1",
4
5
  "description": "A luau beautifier and minifier.",
5
- "main": "index.js",
6
+ "publisher": "centurion",
7
+ "main": "./index.js",
6
8
  "bin": {
7
9
  "nextlua": "./index.js"
8
10
  },
11
+ "engines": {
12
+ "vscode": "^1.75.0"
13
+ },
14
+ "categories": [
15
+ "Formatters"
16
+ ],
17
+ "activationEvents": [
18
+ "onLanguage:lua",
19
+ "onLanguage:luau"
20
+ ],
21
+ "contributes": {
22
+ "commands": [
23
+ {
24
+ "command": "nextlua.beautifyDocument",
25
+ "title": "Beautify"
26
+ },
27
+ {
28
+ "command": "nextlua.minifyDocument",
29
+ "title": "Minify"
30
+ },
31
+ {
32
+ "command": "nextlua.showMenu",
33
+ "title": "nextlua"
34
+ }
35
+ ]
36
+ },
9
37
  "files": [
10
38
  "index.js",
11
39
  "src"
12
40
  ],
13
41
  "scripts": {
14
- "test": "echo \"Error: no test specified\" && exit 1"
42
+ "test": "echo \"Error: no test specified\" && exit 1",
43
+ "package": "vsce package"
44
+ },
45
+ "devDependencies": {
46
+ "@types/vscode": "^1.75.0",
47
+ "@vscode/vsce": "^3.0.0"
15
48
  },
16
49
  "keywords": [],
17
50
  "author": "",
package/src/main.js CHANGED
@@ -566,13 +566,22 @@ function layout(input) {
566
566
  blockBases.push({
567
567
  paren: parenDepth,
568
568
  brace: braceDepth,
569
- bracket: bracketDepth
569
+ bracket: bracketDepth,
570
+ lineStart: lines.length
570
571
  });
571
572
  }
572
573
 
573
574
  function leaveBlock() {
574
575
  if (blockBases.length > 1) {
575
- blockBases.pop();
576
+ const block = blockBases.pop();
577
+ // No statement line was emitted since the block opened, so the body
578
+ // is empty — leave a placeholder comment at the block's indent.
579
+ if (lines.length === block.lineStart) {
580
+ lines.push({
581
+ depth: Math.max(0, depth),
582
+ tokens: ["-- empty block"]
583
+ });
584
+ }
576
585
  }
577
586
  }
578
587
 
@@ -626,6 +635,7 @@ function layout(input) {
626
635
  }
627
636
 
628
637
  if (blockEnd.has(token) && atStatementLevel()) {
638
+
629
639
  flushCurrent();
630
640
  leaveBlock();
631
641
  depth = Math.max(0, depth - 1);