forked from GreenXenith/luanti-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
187 lines (166 loc) · 5.75 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
const snippets = require("./smartsnippets.json");
const rootPath = vscode.workspace.workspaceFolders != undefined ? vscode.workspace.workspaceFolders[0].uri.fsPath : "";
const luacheckrc = `read_globals = {
"DIR_DELIM", "INIT",
"minetest", "core",
"dump", "dump2",
"Raycast",
"Settings",
"PseudoRandom",
"PerlinNoise",
"VoxelManip",
"SecureRandom",
"VoxelArea",
"PerlinNoiseMap",
"PcgRandom",
"ItemStack",
"AreaStore",
"vector",
table = {
fields = {
"copy",
"indexof",
"insert_all",
"key_value_swap",
"shuffle",
}
},
string = {
fields = {
"split",
"trim",
}
},
math = {
fields = {
"hypot",
"sign",
"factorial"
}
},
}`;
function makeFiles(files, folders) {
for (const folder of folders) {
const fullpath = path.join(rootPath, folder);
if (!fs.existsSync(fullpath)) {
fs.mkdirSync(fullpath);
}
}
for (const file of files) {
const fullpath = path.join(rootPath, file.name);
if (!fs.existsSync(fullpath)) {
fs.writeFileSync(fullpath, file.content);
}
}
}
function activate(context) {
// Intellisense
let completion = vscode.languages.registerCompletionItemProvider({language: "lua", scheme: "file"}, {
provideCompletionItems(document, position) {
// Only show snippets if in a Minetest workspace
if (vscode.workspace.getConfiguration("minetest-tools").get("workspaceOnly") &&
!(fs.existsSync(path.join(rootPath, "init.lua")) ||
fs.existsSync(path.join(rootPath, "mods")) ||
fs.existsSync(path.join(rootPath, "modpack.txt")))
) return [];
const line = document.getText(new vscode.Range(new vscode.Position(position.line, 0), position));
const afterpos = new vscode.Range(position, new vscode.Position(position.line, position.character + 1));
const after = document.getText(afterpos);
let items = [];
for (const snippet of snippets) {
if (snippet.token && line.match(new RegExp(`${snippet.token.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")}(?![^\\w\\n\\s\\r\\-])[\\w\\n\\s\\r\\-]*`))) {
let item = new vscode.CompletionItem(snippet.prefix);
item.insertText = new vscode.SnippetString(snippet.body);
item.documentation = new vscode.MarkdownString(snippet.desc);
item.kind = snippet.kind;
item.detail = snippet.detail || snippet.prefix;
item.additionalTextEdits = (snippet.token.match(/^[\(\[\{]$/) && after.match(/[\}\]\)]/)) ? [vscode.TextEdit.delete(afterpos)] : null;
items.push(item);
}
}
return items;
},
}, ":", ".", "[");
// Mod boilerplate
let modproject = vscode.commands.registerCommand("extension.modProject", () => {
if (rootPath == "") return;
const name = vscode.workspace.name;
const files = [
{
"name": "init.lua",
"content": ""
},
{
"name": "mod.conf",
"content": `name = ${name}\ndescription = \ndepends = \noptional_depends = `
},
{
"name": "README.md",
"content": ""
},
{
"name": "LICENSE.txt",
"content": ""
},
{
"name": ".luacheckrc",
"content": luacheckrc
}
];
const folders = ["textures", "models", "sounds"];
makeFiles(files, folders);
});
// Game boilerplate
let gameproject = vscode.commands.registerCommand("extension.gameProject", () => {
if (rootPath == "") return;
const name = vscode.workspace.name;
const files = [
{
"name": "game.conf",
"content": `name = ${name.replace(/[_-]/g, " ").replace(/(^| )(\w)/g, function(m) {
return m.toUpperCase();
})}\nauthor = \ndescription = `
},
{
"name": "README.md",
"content": ""
},
{
"name": "LICENSE.txt",
"content": ""
},
{
"name": ".luacheckrc",
"content": luacheckrc
}
];
const folders = ["menu", "mods"];
makeFiles(files, folders);
});
// .luacheckrc generator
let luacheck = vscode.commands.registerCommand("extension.luacheckrc", () => {
if (rootPath == "") return;
makeFiles([
{
"name": ".luacheckrc",
"content": luacheckrc
}
], []);
})
// Toggle workspace-only snippets
let toggle = vscode.commands.registerCommand("extension.workspaceToggle", () => {
const conf = vscode.workspace.getConfiguration("minetest-tools");
const newVal = conf.get("workspaceOnly") ? false : true;
conf.update("workspaceOnly", newVal, true);
vscode.window.showInformationMessage(newVal ? "Minetest Intellisense active in workspace only." : "Minetest Intellisense active for all Lua files.");
})
context.subscriptions.push(completion, modproject, gameproject, luacheck, toggle);
console.log("Minetest Tools extension is active.");
}
exports.activate = activate;
module.exports = {
activate
}